diff options
Diffstat (limited to 'tests/PDFPrimitivesTest.cpp')
-rw-r--r-- | tests/PDFPrimitivesTest.cpp | 141 |
1 files changed, 97 insertions, 44 deletions
diff --git a/tests/PDFPrimitivesTest.cpp b/tests/PDFPrimitivesTest.cpp index d4d1eea958..b3f510528a 100644 --- a/tests/PDFPrimitivesTest.cpp +++ b/tests/PDFPrimitivesTest.cpp @@ -18,11 +18,13 @@ #include "Test.h" #include "SkData.h" +#include "SkFlate.h" #include "SkPDFCatalog.h" #include "SkPDFStream.h" #include "SkPDFTypes.h" #include "SkScalar.h" #include "SkStream.h" +#include "SkTypes.h" class SkPDFTestDict : public SkPDFDict { public: @@ -52,16 +54,20 @@ static bool stream_equals(const SkDynamicMemoryWStream& stream, size_t offset, } static void CheckObjectOutput(skiatest::Reporter* reporter, SkPDFObject* obj, - const std::string& representation, - bool indirect) { - SkPDFCatalog catalog; + const char* expectedData, size_t expectedSize, + bool indirect, bool compression) { + SkPDFDocument::Flags docFlags = (SkPDFDocument::Flags) 0; + if (!compression) { + docFlags = SkTBitOr(docFlags, SkPDFDocument::kNoCompression_Flag); + } + SkPDFCatalog catalog(docFlags); size_t directSize = obj->getOutputSize(&catalog, false); - REPORTER_ASSERT(reporter, directSize == representation.size()); + REPORTER_ASSERT(reporter, directSize == expectedSize); SkDynamicMemoryWStream buffer; obj->emit(&buffer, &catalog, false); REPORTER_ASSERT(reporter, directSize == buffer.getOffset()); - REPORTER_ASSERT(reporter, stream_equals(buffer, 0, representation.c_str(), + REPORTER_ASSERT(reporter, stream_equals(buffer, 0, expectedData, directSize)); if (indirect) { @@ -81,16 +87,74 @@ static void CheckObjectOutput(skiatest::Reporter* reporter, SkPDFObject* obj, obj->emit(&buffer, &catalog, true); REPORTER_ASSERT(reporter, indirectSize == buffer.getOffset()); REPORTER_ASSERT(reporter, stream_equals(buffer, 0, header, headerLen)); - REPORTER_ASSERT(reporter, stream_equals(buffer, headerLen, - representation.c_str(), + REPORTER_ASSERT(reporter, stream_equals(buffer, headerLen, expectedData, directSize)); REPORTER_ASSERT(reporter, stream_equals(buffer, headerLen + directSize, footer, footerLen)); } } +static void SimpleCheckObjectOutput(skiatest::Reporter* reporter, + SkPDFObject* obj, + const std::string& expectedResult) { + CheckObjectOutput(reporter, obj, expectedResult.c_str(), + expectedResult.length(), true, false); +} + +static void TestPDFStream(skiatest::Reporter* reporter) { + char streamBytes[] = "Test\nFoo\tBar"; + SkRefPtr<SkMemoryStream> streamData = new SkMemoryStream( + streamBytes, strlen(streamBytes), true); + streamData->unref(); // SkRefPtr and new both took a reference. + SkRefPtr<SkPDFStream> stream = new SkPDFStream(streamData.get()); + stream->unref(); // SkRefPtr and new both took a reference. + SimpleCheckObjectOutput( + reporter, stream.get(), + "<</Length 12\n>> stream\nTest\nFoo\tBar\nendstream"); + stream->insert("Attribute", new SkPDFInt(42))->unref(); + SimpleCheckObjectOutput(reporter, stream.get(), + "<</Length 12\n/Attribute 42\n>> stream\n" + "Test\nFoo\tBar\nendstream"); + + if (SkFlate::HaveFlate()) { + char streamBytes2[] = "This is a longer string, so that compression " + "can do something with it. With shorter strings, " + "the short circuit logic cuts in and we end up " + "with an uncompressed string."; + SkAutoDataUnref streamData2(SkData::NewWithCopy(streamBytes2, + strlen(streamBytes2))); + SkRefPtr<SkPDFStream> stream = new SkPDFStream(streamData2.get()); + stream->unref(); // SkRefPtr and new both took a reference. + + SkDynamicMemoryWStream compressedByteStream; + SkFlate::Deflate(streamData2.get(), &compressedByteStream); + SkAutoDataUnref compressedData(compressedByteStream.copyToData()); + + // Check first without compression. + SkDynamicMemoryWStream expectedResult1; + expectedResult1.writeText("<</Length 167\n>> stream\n"); + expectedResult1.writeText(streamBytes2); + expectedResult1.writeText("\nendstream"); + SkAutoDataUnref expectedResultData1(expectedResult1.copyToData()); + CheckObjectOutput(reporter, stream.get(), + (const char*) expectedResultData1.data(), + expectedResultData1.size(), true, false); + + // Then again with compression. + SkDynamicMemoryWStream expectedResult2; + expectedResult2.writeText("<</Filter /FlateDecode\n/Length 116\n" + ">> stream\n"); + expectedResult2.write(compressedData.data(), compressedData.size()); + expectedResult2.writeText("\nendstream"); + SkAutoDataUnref expectedResultData2(expectedResult2.copyToData()); + CheckObjectOutput(reporter, stream.get(), + (const char*) expectedResultData2.data(), + expectedResultData2.size(), true, true); + } +} + static void TestCatalog(skiatest::Reporter* reporter) { - SkPDFCatalog catalog; + SkPDFCatalog catalog((SkPDFDocument::Flags)0); SkRefPtr<SkPDFInt> int1 = new SkPDFInt(1); int1->unref(); // SkRefPtr and new both took a reference. SkRefPtr<SkPDFInt> int2 = new SkPDFInt(2); @@ -125,7 +189,7 @@ static void TestObjectRef(skiatest::Reporter* reporter) { SkRefPtr<SkPDFObjRef> int2ref = new SkPDFObjRef(int2.get()); int2ref->unref(); // SkRefPtr and new both took a reference. - SkPDFCatalog catalog; + SkPDFCatalog catalog((SkPDFDocument::Flags)0); catalog.addObject(int1.get(), false); catalog.addObject(int2.get(), false); REPORTER_ASSERT(reporter, catalog.getObjectNumberSize(int1.get()) == 3); @@ -155,7 +219,7 @@ static void TestSubstitute(skiatest::Reporter* reporter) { stubResource->insert("InnerValue", int44.get()); stub->addResource(stubResource.get()); - SkPDFCatalog catalog; + SkPDFCatalog catalog((SkPDFDocument::Flags)0); catalog.addObject(proxy.get(), false); catalog.setSubstitute(proxy.get(), stub.get()); @@ -173,90 +237,79 @@ static void TestSubstitute(skiatest::Reporter* reporter) { static void TestPDFPrimitives(skiatest::Reporter* reporter) { SkRefPtr<SkPDFInt> int42 = new SkPDFInt(42); int42->unref(); // SkRefPtr and new both took a reference. - CheckObjectOutput(reporter, int42.get(), "42", true); + SimpleCheckObjectOutput(reporter, int42.get(), "42"); SkRefPtr<SkPDFScalar> realHalf = new SkPDFScalar(SK_ScalarHalf); realHalf->unref(); // SkRefPtr and new both took a reference. - CheckObjectOutput(reporter, realHalf.get(), "0.5", true); + SimpleCheckObjectOutput(reporter, realHalf.get(), "0.5"); #if defined(SK_SCALAR_IS_FLOAT) SkRefPtr<SkPDFScalar> bigScalar = new SkPDFScalar(110999.75); bigScalar->unref(); // SkRefPtr and new both took a reference. #if !defined(SK_ALLOW_LARGE_PDF_SCALARS) - CheckObjectOutput(reporter, bigScalar.get(), "111000", true); + SimpleCheckObjectOutput(reporter, bigScalar.get(), "111000"); #else - CheckObjectOutput(reporter, bigScalar.get(), "110999.75", true); + SimpleCheckObjectOutput(reporter, bigScalar.get(), "110999.75"); SkRefPtr<SkPDFScalar> biggerScalar = new SkPDFScalar(50000000.1); biggerScalar->unref(); // SkRefPtr and new both took a reference. - CheckObjectOutput(reporter, biggerScalar.get(), "50000000", true); + SimpleCheckObjectOutput(reporter, biggerScalar.get(), "50000000"); SkRefPtr<SkPDFScalar> smallestScalar = new SkPDFScalar(1.0/65536); smallestScalar->unref(); // SkRefPtr and new both took a reference. - CheckObjectOutput(reporter, smallestScalar.get(), "0.00001526", true); + SimpleCheckObjectOutput(reporter, smallestScalar.get(), "0.00001526"); #endif #endif SkRefPtr<SkPDFString> stringSimple = new SkPDFString("test ) string ( foo"); stringSimple->unref(); // SkRefPtr and new both took a reference. - CheckObjectOutput(reporter, stringSimple.get(), "(test \\) string \\( foo)", - true); + SimpleCheckObjectOutput(reporter, stringSimple.get(), + "(test \\) string \\( foo)"); SkRefPtr<SkPDFString> stringComplex = new SkPDFString("\ttest ) string ( foo"); stringComplex->unref(); // SkRefPtr and new both took a reference. - CheckObjectOutput(reporter, stringComplex.get(), - "<0974657374202920737472696E67202820666F6F>", true); + SimpleCheckObjectOutput(reporter, stringComplex.get(), + "<0974657374202920737472696E67202820666F6F>"); SkRefPtr<SkPDFName> name = new SkPDFName("Test name\twith#tab"); name->unref(); // SkRefPtr and new both took a reference. - CheckObjectOutput(reporter, name.get(), "/Test#20name#09with#23tab", false); + const char expectedResult[] = "/Test#20name#09with#23tab"; + CheckObjectOutput(reporter, name.get(), expectedResult, + strlen(expectedResult), false, false); SkRefPtr<SkPDFArray> array = new SkPDFArray; array->unref(); // SkRefPtr and new both took a reference. - CheckObjectOutput(reporter, array.get(), "[]", true); + SimpleCheckObjectOutput(reporter, array.get(), "[]"); array->append(int42.get()); - CheckObjectOutput(reporter, array.get(), "[42]", true); + SimpleCheckObjectOutput(reporter, array.get(), "[42]"); array->append(realHalf.get()); - CheckObjectOutput(reporter, array.get(), "[42 0.5]", true); + SimpleCheckObjectOutput(reporter, array.get(), "[42 0.5]"); SkRefPtr<SkPDFInt> int0 = new SkPDFInt(0); int0->unref(); // SkRefPtr and new both took a reference. array->append(int0.get()); - CheckObjectOutput(reporter, array.get(), "[42 0.5 0]", true); + SimpleCheckObjectOutput(reporter, array.get(), "[42 0.5 0]"); SkRefPtr<SkPDFInt> int1 = new SkPDFInt(1); int1->unref(); // SkRefPtr and new both took a reference. array->setAt(0, int1.get()); - CheckObjectOutput(reporter, array.get(), "[1 0.5 0]", true); + SimpleCheckObjectOutput(reporter, array.get(), "[1 0.5 0]"); SkRefPtr<SkPDFDict> dict = new SkPDFDict; dict->unref(); // SkRefPtr and new both took a reference. - CheckObjectOutput(reporter, dict.get(), "<<>>", true); + SimpleCheckObjectOutput(reporter, dict.get(), "<<>>"); SkRefPtr<SkPDFName> n1 = new SkPDFName("n1"); n1->unref(); // SkRefPtr and new both took a reference. dict->insert(n1.get(), int42.get()); - CheckObjectOutput(reporter, dict.get(), "<</n1 42\n>>", true); + SimpleCheckObjectOutput(reporter, dict.get(), "<</n1 42\n>>"); SkRefPtr<SkPDFName> n2 = new SkPDFName("n2"); n2->unref(); // SkRefPtr and new both took a reference. SkRefPtr<SkPDFName> n3 = new SkPDFName("n3"); n3->unref(); // SkRefPtr and new both took a reference. dict->insert(n2.get(), realHalf.get()); dict->insert(n3.get(), array.get()); - CheckObjectOutput(reporter, dict.get(), - "<</n1 42\n/n2 0.5\n/n3 [1 0.5 0]\n>>", true); + SimpleCheckObjectOutput(reporter, dict.get(), + "<</n1 42\n/n2 0.5\n/n3 [1 0.5 0]\n>>"); - char streamBytes[] = "Test\nFoo\tBar"; - SkRefPtr<SkMemoryStream> streamData = new SkMemoryStream( - streamBytes, strlen(streamBytes), true); - streamData->unref(); // SkRefPtr and new both took a reference. - SkRefPtr<SkPDFStream> stream = new SkPDFStream(streamData.get()); - stream->unref(); // SkRefPtr and new both took a reference. - CheckObjectOutput(reporter, stream.get(), - "<</Length 12\n>> stream\nTest\nFoo\tBar\nendstream", - true); - stream->insert(n1.get(), int42.get()); - CheckObjectOutput(reporter, stream.get(), - "<</Length 12\n/n1 42\n>> stream\nTest\nFoo\tBar" - "\nendstream", - true); + TestPDFStream(reporter); TestCatalog(reporter); |