SkPDF: clean up uses of deprecated calls in tests

BUG=skia:3585

Review URL: https://codereview.chromium.org/1106163002
This commit is contained in:
halcanary 2015-05-01 07:35:45 -07:00 committed by Commit bot
parent 0b9d4118ba
commit f7a169e225
2 changed files with 238 additions and 173 deletions

View File

@ -77,6 +77,8 @@ public:
static SkPDFUnion Int(int32_t); static SkPDFUnion Int(int32_t);
static SkPDFUnion Int(size_t);
static SkPDFUnion Bool(bool); static SkPDFUnion Bool(bool);
static SkPDFUnion Scalar(SkScalar); static SkPDFUnion Scalar(SkScalar);

View File

@ -24,102 +24,70 @@
#define DUMMY_TEXT "DCT compessed stream." #define DUMMY_TEXT "DCT compessed stream."
static bool stream_equals(const SkDynamicMemoryWStream& stream, size_t offset, namespace {
const void* buffer, size_t len) { struct Catalog {
SkAutoDataUnref data(stream.copyToData()); SkPDFSubstituteMap substitutes;
if (offset + len > data->size()) { SkPDFObjNumMap numbers;
return false; };
} } // namespace
return memcmp(data->bytes() + offset, buffer, len) == 0;
}
static void emit_object(SkPDFObject* object, template <typename T>
SkWStream* stream, static SkString emit_to_string(T& obj, Catalog* catPtr = NULL) {
const SkPDFObjNumMap& objNumMap, Catalog catalog;
const SkPDFSubstituteMap& substitutes,
bool indirect) {
SkPDFObject* realObject = substitutes.getSubstitute(object);
if (indirect) {
stream->writeDecAsText(objNumMap.getObjectNumber(realObject));
stream->writeText(" 0 obj\n"); // Generation number is always 0.
realObject->emitObject(stream, objNumMap, substitutes);
stream->writeText("\nendobj\n");
} else {
realObject->emitObject(stream, objNumMap, substitutes);
}
}
static size_t get_output_size(SkPDFObject* object,
const SkPDFObjNumMap& objNumMap,
const SkPDFSubstituteMap& substitutes,
bool indirect) {
SkDynamicMemoryWStream buffer; SkDynamicMemoryWStream buffer;
emit_object(object, &buffer, objNumMap, substitutes, indirect); if (!catPtr) {
return buffer.getOffset(); catPtr = &catalog;
}
static void CheckObjectOutput(skiatest::Reporter* reporter, SkPDFObject* obj,
const char* expectedData, size_t expectedSize,
bool indirect) {
SkPDFSubstituteMap substituteMap;
SkPDFObjNumMap catalog;
size_t directSize = get_output_size(obj, catalog, substituteMap, false);
REPORTER_ASSERT(reporter, directSize == expectedSize);
SkDynamicMemoryWStream buffer;
emit_object(obj, &buffer, catalog, substituteMap, false);
REPORTER_ASSERT(reporter, directSize == buffer.getOffset());
if (!stream_equals(buffer, 0, expectedData, directSize)) {
SkAutoTDelete<SkStreamAsset> asset(buffer.detachAsStream());
SkString s(asset->getLength());
asset->read(s.writable_str(), s.size());
ERRORF(reporter, "!stream_equals() '%s' '%s'", expectedData, s.c_str());
}
if (indirect) {
// Indirect output.
static char header[] = "1 0 obj\n";
static size_t headerLen = strlen(header);
static char footer[] = "\nendobj\n";
static size_t footerLen = strlen(footer);
catalog.addObject(obj);
size_t indirectSize =
get_output_size(obj, catalog, substituteMap, true);
REPORTER_ASSERT(reporter,
indirectSize == directSize + headerLen + footerLen);
buffer.reset();
emit_object(obj, &buffer, catalog, substituteMap, true);
REPORTER_ASSERT(reporter, indirectSize == buffer.getOffset());
REPORTER_ASSERT(reporter, stream_equals(buffer, 0, header, headerLen));
REPORTER_ASSERT(reporter, stream_equals(buffer, headerLen, expectedData,
directSize));
REPORTER_ASSERT(reporter, stream_equals(buffer, headerLen + directSize,
footer, footerLen));
} }
obj.emitObject(&buffer, catPtr->numbers, catPtr->substitutes);
SkAutoTDelete<SkStreamAsset> asset(buffer.detachAsStream());
SkString tmp(asset->getLength());
asset->read(tmp.writable_str(), asset->getLength());
return tmp;
} }
static void SimpleCheckObjectOutput(skiatest::Reporter* reporter, static bool eq(const SkString& str, const char* strPtr, size_t len) {
SkPDFObject* obj, return len == str.size() && 0 == memcmp(str.c_str(), strPtr, len);
const char* expectedResult) {
CheckObjectOutput(reporter, obj, expectedResult,
strlen(expectedResult), true);
} }
#define ASSERT_EQL(REPORTER, SKSTRING, STRING, LEN) \
do { \
const char* strptr = STRING; \
const SkString& sks = SKSTRING; \
if (!eq(sks, strptr, LEN)) { \
REPORT_FAILURE( \
REPORTER, \
"", \
SkStringPrintf("'%s' != '%s'", strptr, sks.c_str())); \
} \
} while (false)
#define ASSERT_EQ(REPORTER, SKSTRING, STRING) \
do { \
const char* str = STRING; \
ASSERT_EQL(REPORTER, SKSTRING, str, strlen(str)); \
} while (false)
#define ASSERT_EMIT_EQ(REPORTER, OBJECT, STRING) \
do { \
SkString result = emit_to_string(OBJECT); \
ASSERT_EQ(REPORTER, result, STRING); \
} while (false)
static void TestPDFStream(skiatest::Reporter* reporter) { static void TestPDFStream(skiatest::Reporter* reporter) {
char streamBytes[] = "Test\nFoo\tBar"; char streamBytes[] = "Test\nFoo\tBar";
SkAutoTDelete<SkMemoryStream> streamData(new SkMemoryStream( SkAutoTDelete<SkMemoryStream> streamData(new SkMemoryStream(
streamBytes, strlen(streamBytes), true)); streamBytes, strlen(streamBytes), true));
SkAutoTUnref<SkPDFStream> stream(new SkPDFStream(streamData.get())); SkAutoTUnref<SkPDFStream> stream(new SkPDFStream(streamData.get()));
SimpleCheckObjectOutput( ASSERT_EMIT_EQ(reporter,
reporter, stream.get(), *stream,
"<</Length 12>> stream\nTest\nFoo\tBar\nendstream"); "<</Length 12>> stream\nTest\nFoo\tBar\nendstream");
stream->insert("Attribute", new SkPDFInt(42))->unref(); stream->insertInt("Attribute", 42);
SimpleCheckObjectOutput(reporter, stream.get(), ASSERT_EMIT_EQ(reporter,
"<</Length 12\n/Attribute 42>> stream\n" *stream,
"Test\nFoo\tBar\nendstream"); "<</Length 12\n/Attribute 42>> stream\n"
"Test\nFoo\tBar\nendstream");
{ {
char streamBytes2[] = "This is a longer string, so that compression " char streamBytes2[] = "This is a longer string, so that compression "
@ -139,57 +107,55 @@ static void TestPDFStream(skiatest::Reporter* reporter) {
expected.write(compressedData->data(), compressedData->size()); expected.write(compressedData->data(), compressedData->size());
expected.writeText("\nendstream"); expected.writeText("\nendstream");
SkAutoDataUnref expectedResultData2(expected.copyToData()); SkAutoDataUnref expectedResultData2(expected.copyToData());
CheckObjectOutput(reporter, stream.get(), SkString result = emit_to_string(*stream);
(const char*) expectedResultData2->data(), ASSERT_EQL(reporter,
expectedResultData2->size(), true); result,
(const char*)expectedResultData2->data(),
expectedResultData2->size());
} }
} }
static void TestCatalog(skiatest::Reporter* reporter) { static void TestObjectNumberMap(skiatest::Reporter* reporter) {
SkPDFSubstituteMap substituteMap; SkPDFObjNumMap objNumMap;
SkPDFObjNumMap catalog; SkAutoTUnref<SkPDFArray> a1(new SkPDFArray);
SkAutoTUnref<SkPDFInt> int1(new SkPDFInt(1)); SkAutoTUnref<SkPDFArray> a2(new SkPDFArray);
SkAutoTUnref<SkPDFInt> int2(new SkPDFInt(2)); SkAutoTUnref<SkPDFArray> a3(new SkPDFArray);
SkAutoTUnref<SkPDFInt> int3(new SkPDFInt(3));
int1.get()->ref();
SkAutoTUnref<SkPDFInt> int1Again(int1.get());
catalog.addObject(int1.get()); objNumMap.addObject(a1.get());
catalog.addObject(int2.get()); objNumMap.addObject(a2.get());
catalog.addObject(int3.get()); objNumMap.addObject(a3.get());
REPORTER_ASSERT(reporter, catalog.getObjectNumber(int1.get()) == 1); // The objects should be numbered in the order they are added,
REPORTER_ASSERT(reporter, catalog.getObjectNumber(int2.get()) == 2); // starting with 1.
REPORTER_ASSERT(reporter, catalog.getObjectNumber(int3.get()) == 3); REPORTER_ASSERT(reporter, objNumMap.getObjectNumber(a1.get()) == 1);
REPORTER_ASSERT(reporter, catalog.getObjectNumber(int1Again.get()) == 1); REPORTER_ASSERT(reporter, objNumMap.getObjectNumber(a2.get()) == 2);
REPORTER_ASSERT(reporter, objNumMap.getObjectNumber(a3.get()) == 3);
// Assert that repeated calls to get the object number return
// consistent result.
REPORTER_ASSERT(reporter, objNumMap.getObjectNumber(a1.get()) == 1);
} }
static void TestObjectRef(skiatest::Reporter* reporter) { static void TestObjectRef(skiatest::Reporter* reporter) {
SkAutoTUnref<SkPDFInt> int1(new SkPDFInt(1)); SkAutoTUnref<SkPDFArray> a1(new SkPDFArray);
SkAutoTUnref<SkPDFInt> int2(new SkPDFInt(2)); SkAutoTUnref<SkPDFArray> a2(new SkPDFArray);
SkAutoTUnref<SkPDFObjRef> int2ref(new SkPDFObjRef(int2.get())); a2->appendObjRef(SkRef(a1.get()));
SkPDFSubstituteMap substituteMap; Catalog catalog;
SkPDFObjNumMap catalog; catalog.numbers.addObject(a1.get());
catalog.addObject(int1.get()); REPORTER_ASSERT(reporter, catalog.numbers.getObjectNumber(a1.get()) == 1);
catalog.addObject(int2.get());
REPORTER_ASSERT(reporter, catalog.getObjectNumber(int1.get()) == 1);
REPORTER_ASSERT(reporter, catalog.getObjectNumber(int2.get()) == 2);
char expectedResult[] = "2 0 R"; SkString result = emit_to_string(*a2, &catalog);
SkDynamicMemoryWStream buffer; // If appendObjRef misbehaves, then the result would
int2ref->emitObject(&buffer, catalog, substituteMap); // be [[]], not [1 0 R].
REPORTER_ASSERT(reporter, buffer.getOffset() == strlen(expectedResult)); ASSERT_EQ(reporter, result, "[1 0 R]");
REPORTER_ASSERT(reporter, stream_equals(buffer, 0, expectedResult,
buffer.getOffset()));
} }
static void TestSubstitute(skiatest::Reporter* reporter) { static void TestSubstitute(skiatest::Reporter* reporter) {
SkAutoTUnref<SkPDFDict> proxy(new SkPDFDict()); SkAutoTUnref<SkPDFDict> proxy(new SkPDFDict());
SkAutoTUnref<SkPDFDict> stub(new SkPDFDict()); SkAutoTUnref<SkPDFDict> stub(new SkPDFDict());
proxy->insert("Value", new SkPDFInt(33))->unref(); proxy->insertInt("Value", 33);
stub->insert("Value", new SkPDFInt(44))->unref(); stub->insertInt("Value", 44);
SkPDFSubstituteMap substituteMap; SkPDFSubstituteMap substituteMap;
substituteMap.setSubstitute(proxy.get(), stub.get()); substituteMap.setSubstitute(proxy.get(), stub.get());
@ -217,81 +183,178 @@ static void test_issue1083() {
doc->close(); doc->close();
} }
DEF_TEST(PDFPrimitives, reporter) { static void TestPDFUnion(skiatest::Reporter* reporter) {
SkAutoTUnref<SkPDFInt> int42(new SkPDFInt(42)); SkPDFUnion boolTrue = SkPDFUnion::Bool(true);
SimpleCheckObjectOutput(reporter, int42.get(), "42"); ASSERT_EMIT_EQ(reporter, boolTrue, "true");
SkAutoTUnref<SkPDFScalar> realHalf(new SkPDFScalar(SK_ScalarHalf)); SkPDFUnion boolFalse = SkPDFUnion::Bool(false);
SimpleCheckObjectOutput(reporter, realHalf.get(), "0.5"); ASSERT_EMIT_EQ(reporter, boolFalse, "false");
SkAutoTUnref<SkPDFScalar> bigScalar(new SkPDFScalar(110999.75f)); SkPDFUnion int42 = SkPDFUnion::Int(42);
ASSERT_EMIT_EQ(reporter, int42, "42");
SkPDFUnion realHalf = SkPDFUnion::Scalar(SK_ScalarHalf);
ASSERT_EMIT_EQ(reporter, realHalf, "0.5");
SkPDFUnion bigScalar = SkPDFUnion::Scalar(110999.75f);
#if !defined(SK_ALLOW_LARGE_PDF_SCALARS) #if !defined(SK_ALLOW_LARGE_PDF_SCALARS)
SimpleCheckObjectOutput(reporter, bigScalar.get(), "111000"); ASSERT_EMIT_EQ(reporter, bigScalar, "111000");
#else #else
SimpleCheckObjectOutput(reporter, bigScalar.get(), "110999.75"); ASSERT_EMIT_EQ(reporter, bigScalar, "110999.75");
SkAutoTUnref<SkPDFScalar> biggerScalar(new SkPDFScalar(50000000.1)); SkPDFUnion biggerScalar = SkPDFUnion::Scalar(50000000.1);
SimpleCheckObjectOutput(reporter, biggerScalar.get(), "50000000"); ASSERT_EMIT_EQ(reporter, biggerScalar, "50000000");
SkAutoTUnref<SkPDFScalar> smallestScalar(new SkPDFScalar(1.0/65536)); SkPDFUnion smallestScalar = SkPDFUnion::Scalar(1.0 / 65536);
SimpleCheckObjectOutput(reporter, smallestScalar.get(), "0.00001526"); ASSERT_EMIT_EQ(reporter, smallestScalar, "0.00001526");
#endif #endif
SkAutoTUnref<SkPDFString> stringSimple( SkPDFUnion stringSimple = SkPDFUnion::String("test ) string ( foo");
new SkPDFString("test ) string ( foo")); ASSERT_EMIT_EQ(reporter, stringSimple, "(test \\) string \\( foo)");
SimpleCheckObjectOutput(reporter, stringSimple.get(),
"(test \\) string \\( foo)");
SkAutoTUnref<SkPDFString> stringComplex(
new SkPDFString("\ttest ) string ( foo"));
SimpleCheckObjectOutput(reporter, stringComplex.get(),
"<0974657374202920737472696E67202820666F6F>");
SkAutoTUnref<SkPDFName> name(new SkPDFName("Test name\twith#tab")); SkString stringComplexInput("\ttest ) string ( foo");
const char expectedResult[] = "/Test#20name#09with#23tab"; SkPDFUnion stringComplex = SkPDFUnion::String(stringComplexInput);
CheckObjectOutput(reporter, name.get(), expectedResult, ASSERT_EMIT_EQ(reporter,
strlen(expectedResult), false); stringComplex,
"<0974657374202920737472696E67202820666F6F>");
SkAutoTUnref<SkPDFName> escapedName(new SkPDFName("A#/%()<>[]{}B")); SkString nameInput("Test name\twith#tab");
const char escapedNameExpected[] = "/A#23#2F#25#28#29#3C#3E#5B#5D#7B#7DB"; SkPDFUnion name = SkPDFUnion::Name(nameInput);
CheckObjectOutput(reporter, escapedName.get(), escapedNameExpected, ASSERT_EMIT_EQ(reporter, name, "/Test#20name#09with#23tab");
strlen(escapedNameExpected), false);
SkString nameInput2("A#/%()<>[]{}B");
SkPDFUnion name2 = SkPDFUnion::Name(nameInput2);
ASSERT_EMIT_EQ(reporter, name2, "/A#23#2F#25#28#29#3C#3E#5B#5D#7B#7DB");
SkPDFUnion name3 = SkPDFUnion::Name("SimpleNameWithOnlyPrintableASCII");
ASSERT_EMIT_EQ(reporter, name3, "/SimpleNameWithOnlyPrintableASCII");
// Test that we correctly handle characters with the high-bit set. // Test that we correctly handle characters with the high-bit set.
const unsigned char highBitCString[] = {0xDE, 0xAD, 'b', 'e', 0xEF, 0}; SkString highBitString("\xDE\xAD" "be\xEF");
SkAutoTUnref<SkPDFName> highBitName( SkPDFUnion highBitName = SkPDFUnion::Name(highBitString);
new SkPDFName((const char*)highBitCString)); ASSERT_EMIT_EQ(reporter, highBitName, "/#DE#ADbe#EF");
const char highBitExpectedResult[] = "/#DE#ADbe#EF"; }
CheckObjectOutput(reporter, highBitName.get(), highBitExpectedResult,
strlen(highBitExpectedResult), false);
static void TestPDFArray(skiatest::Reporter* reporter) {
SkAutoTUnref<SkPDFArray> array(new SkPDFArray); SkAutoTUnref<SkPDFArray> array(new SkPDFArray);
SimpleCheckObjectOutput(reporter, array.get(), "[]"); ASSERT_EMIT_EQ(reporter, *array, "[]");
array->append(int42.get());
SimpleCheckObjectOutput(reporter, array.get(), "[42]");
array->append(realHalf.get());
SimpleCheckObjectOutput(reporter, array.get(), "[42 0.5]");
SkAutoTUnref<SkPDFInt> int0(new SkPDFInt(0));
array->append(int0.get());
SimpleCheckObjectOutput(reporter, array.get(), "[42 0.5 0]");
array->appendInt(42);
ASSERT_EMIT_EQ(reporter, *array, "[42]");
array->appendScalar(SK_ScalarHalf);
ASSERT_EMIT_EQ(reporter, *array, "[42 0.5]");
array->appendInt(0);
ASSERT_EMIT_EQ(reporter, *array, "[42 0.5 0]");
array->appendBool(true);
ASSERT_EMIT_EQ(reporter, *array, "[42 0.5 0 true]");
array->appendName("ThisName");
ASSERT_EMIT_EQ(reporter, *array, "[42 0.5 0 true /ThisName]");
array->appendName(SkString("AnotherName"));
ASSERT_EMIT_EQ(reporter, *array, "[42 0.5 0 true /ThisName /AnotherName]");
array->appendString("This String");
ASSERT_EMIT_EQ(reporter, *array,
"[42 0.5 0 true /ThisName /AnotherName (This String)]");
array->appendString(SkString("Another String"));
ASSERT_EMIT_EQ(reporter, *array,
"[42 0.5 0 true /ThisName /AnotherName (This String) "
"(Another String)]");
SkAutoTUnref<SkPDFArray> innerArray(new SkPDFArray);
innerArray->appendInt(-1);
array->appendObject(innerArray.detach());
ASSERT_EMIT_EQ(reporter, *array,
"[42 0.5 0 true /ThisName /AnotherName (This String) "
"(Another String) [-1]]");
SkAutoTUnref<SkPDFArray> referencedArray(new SkPDFArray);
Catalog catalog;
catalog.numbers.addObject(referencedArray.get());
REPORTER_ASSERT(reporter, catalog.numbers.getObjectNumber(
referencedArray.get()) == 1);
array->appendObjRef(referencedArray.detach());
SkString result = emit_to_string(*array, &catalog);
ASSERT_EQ(reporter, result,
"[42 0.5 0 true /ThisName /AnotherName (This String) "
"(Another String) [-1] 1 0 R]");
}
static void TestPDFDict(skiatest::Reporter* reporter) {
SkAutoTUnref<SkPDFDict> dict(new SkPDFDict); SkAutoTUnref<SkPDFDict> dict(new SkPDFDict);
SimpleCheckObjectOutput(reporter, dict.get(), "<<>>"); ASSERT_EMIT_EQ(reporter, *dict, "<<>>");
dict->insert("n1", int42.get());
SimpleCheckObjectOutput(reporter, dict.get(), "<</n1 42>>"); dict->insertInt("n1", SkToSizeT(42));
ASSERT_EMIT_EQ(reporter, *dict, "<</n1 42>>");
dict.reset(new SkPDFDict);
ASSERT_EMIT_EQ(reporter, *dict, "<<>>");
dict->insertInt("n1", 42);
ASSERT_EMIT_EQ(reporter, *dict, "<</n1 42>>");
dict->insertScalar("n2", SK_ScalarHalf);
SkString n3("n3"); SkString n3("n3");
dict->insert("n2", realHalf.get()); SkAutoTUnref<SkPDFArray> innerArray(new SkPDFArray);
dict->insertObject(n3, array.detach()); innerArray->appendInt(-100);
SimpleCheckObjectOutput(reporter, dict.get(), dict->insertObject(n3, innerArray.detach());
"<</n1 42\n/n2 0.5\n/n3 [42 0.5 0]>>"); ASSERT_EMIT_EQ(reporter, *dict, "<</n1 42\n/n2 0.5\n/n3 [-100]>>");
dict.reset(new SkPDFDict);
ASSERT_EMIT_EQ(reporter, *dict, "<<>>");
dict->insertInt("n1", 24);
ASSERT_EMIT_EQ(reporter, *dict, "<</n1 24>>");
dict->insertInt("n2", SkToSizeT(99));
ASSERT_EMIT_EQ(reporter, *dict, "<</n1 24\n/n2 99>>");
dict->insertScalar("n3", SK_ScalarHalf);
ASSERT_EMIT_EQ(reporter, *dict, "<</n1 24\n/n2 99\n/n3 0.5>>");
dict->insertName("n4", "AName");
ASSERT_EMIT_EQ(reporter, *dict, "<</n1 24\n/n2 99\n/n3 0.5\n/n4 /AName>>");
dict->insertName("n5", SkString("AnotherName"));
ASSERT_EMIT_EQ(reporter, *dict, "<</n1 24\n/n2 99\n/n3 0.5\n/n4 /AName\n"
"/n5 /AnotherName>>");
dict->insertString("n6", "A String");
ASSERT_EMIT_EQ(reporter, *dict, "<</n1 24\n/n2 99\n/n3 0.5\n/n4 /AName\n"
"/n5 /AnotherName\n/n6 (A String)>>");
dict->insertString("n7", SkString("Another String"));
ASSERT_EMIT_EQ(reporter, *dict, "<</n1 24\n/n2 99\n/n3 0.5\n/n4 /AName\n"
"/n5 /AnotherName\n/n6 (A String)\n/n7 (Another String)>>");
dict.reset(new SkPDFDict("DType"));
ASSERT_EMIT_EQ(reporter, *dict, "<</Type /DType>>");
SkAutoTUnref<SkPDFArray> referencedArray(new SkPDFArray);
Catalog catalog;
catalog.numbers.addObject(referencedArray.get());
REPORTER_ASSERT(reporter, catalog.numbers.getObjectNumber(
referencedArray.get()) == 1);
dict->insertObjRef("n1", referencedArray.detach());
SkString result = emit_to_string(*dict, &catalog);
ASSERT_EQ(reporter, result, "<</Type /DType\n/n1 1 0 R>>");
}
DEF_TEST(PDFPrimitives, reporter) {
TestPDFUnion(reporter);
TestPDFArray(reporter);
TestPDFDict(reporter);
TestPDFStream(reporter); TestPDFStream(reporter);
TestObjectNumberMap(reporter);
TestCatalog(reporter);
TestObjectRef(reporter); TestObjectRef(reporter);
TestSubstitute(reporter); TestSubstitute(reporter);
test_issue1083(); test_issue1083();
} }