Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(93)

Side by Side Diff: tests/PDFPrimitivesTest.cpp

Issue 1106163002: SkPDF: clean up uses of deprecated calls in tests (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: 2015-05-01 (Friday) 10:31:59 EDT Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/pdf/SkPDFTypes.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2010 The Android Open Source Project 2 * Copyright 2010 The Android Open Source Project
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "SkBitmap.h" 8 #include "SkBitmap.h"
9 #include "SkCanvas.h" 9 #include "SkCanvas.h"
10 #include "SkData.h" 10 #include "SkData.h"
11 #include "SkDocument.h" 11 #include "SkDocument.h"
12 #include "SkFlate.h" 12 #include "SkFlate.h"
13 #include "SkImageEncoder.h" 13 #include "SkImageEncoder.h"
14 #include "SkMatrix.h" 14 #include "SkMatrix.h"
15 #include "SkPDFCanon.h" 15 #include "SkPDFCanon.h"
16 #include "SkPDFDevice.h" 16 #include "SkPDFDevice.h"
17 #include "SkPDFStream.h" 17 #include "SkPDFStream.h"
18 #include "SkPDFTypes.h" 18 #include "SkPDFTypes.h"
19 #include "SkReadBuffer.h" 19 #include "SkReadBuffer.h"
20 #include "SkScalar.h" 20 #include "SkScalar.h"
21 #include "SkStream.h" 21 #include "SkStream.h"
22 #include "SkTypes.h" 22 #include "SkTypes.h"
23 #include "Test.h" 23 #include "Test.h"
24 24
25 #define DUMMY_TEXT "DCT compessed stream." 25 #define DUMMY_TEXT "DCT compessed stream."
26 26
27 static bool stream_equals(const SkDynamicMemoryWStream& stream, size_t offset, 27 namespace {
28 const void* buffer, size_t len) { 28 struct Catalog {
29 SkAutoDataUnref data(stream.copyToData()); 29 SkPDFSubstituteMap substitutes;
30 if (offset + len > data->size()) { 30 SkPDFObjNumMap numbers;
31 return false; 31 };
32 } // namespace
33
34 template <typename T>
35 static SkString emit_to_string(T& obj, Catalog* catPtr = NULL) {
36 Catalog catalog;
37 SkDynamicMemoryWStream buffer;
38 if (!catPtr) {
39 catPtr = &catalog;
32 } 40 }
33 return memcmp(data->bytes() + offset, buffer, len) == 0; 41 obj.emitObject(&buffer, catPtr->numbers, catPtr->substitutes);
42 SkAutoTDelete<SkStreamAsset> asset(buffer.detachAsStream());
43 SkString tmp(asset->getLength());
44 asset->read(tmp.writable_str(), asset->getLength());
45 return tmp;
34 } 46 }
35 47
36 static void emit_object(SkPDFObject* object, 48 static bool eq(const SkString& str, const char* strPtr, size_t len) {
37 SkWStream* stream, 49 return len == str.size() && 0 == memcmp(str.c_str(), strPtr, len);
38 const SkPDFObjNumMap& objNumMap,
39 const SkPDFSubstituteMap& substitutes,
40 bool indirect) {
41 SkPDFObject* realObject = substitutes.getSubstitute(object);
42 if (indirect) {
43 stream->writeDecAsText(objNumMap.getObjectNumber(realObject));
44 stream->writeText(" 0 obj\n"); // Generation number is always 0.
45 realObject->emitObject(stream, objNumMap, substitutes);
46 stream->writeText("\nendobj\n");
47 } else {
48 realObject->emitObject(stream, objNumMap, substitutes);
49 }
50 } 50 }
51 51
52 static size_t get_output_size(SkPDFObject* object, 52 #define ASSERT_EQL(REPORTER, SKSTRING, STRING, LEN) \
53 const SkPDFObjNumMap& objNumMap, 53 do { \
54 const SkPDFSubstituteMap& substitutes, 54 const char* strptr = STRING; \
55 bool indirect) { 55 const SkString& sks = SKSTRING; \
56 SkDynamicMemoryWStream buffer; 56 if (!eq(sks, strptr, LEN)) { \
57 emit_object(object, &buffer, objNumMap, substitutes, indirect); 57 REPORT_FAILURE( \
58 return buffer.getOffset(); 58 REPORTER, \
59 } 59 "", \
60 SkStringPrintf("'%s' != '%s'", strptr, sks.c_str())); \
61 } \
62 } while (false)
60 63
61 static void CheckObjectOutput(skiatest::Reporter* reporter, SkPDFObject* obj, 64 #define ASSERT_EQ(REPORTER, SKSTRING, STRING) \
62 const char* expectedData, size_t expectedSize, 65 do { \
63 bool indirect) { 66 const char* str = STRING; \
64 SkPDFSubstituteMap substituteMap; 67 ASSERT_EQL(REPORTER, SKSTRING, str, strlen(str)); \
65 SkPDFObjNumMap catalog; 68 } while (false)
66 size_t directSize = get_output_size(obj, catalog, substituteMap, false);
67 REPORTER_ASSERT(reporter, directSize == expectedSize);
68 69
69 SkDynamicMemoryWStream buffer; 70 #define ASSERT_EMIT_EQ(REPORTER, OBJECT, STRING) \
70 emit_object(obj, &buffer, catalog, substituteMap, false); 71 do { \
71 REPORTER_ASSERT(reporter, directSize == buffer.getOffset()); 72 SkString result = emit_to_string(OBJECT); \
72 if (!stream_equals(buffer, 0, expectedData, directSize)) { 73 ASSERT_EQ(REPORTER, result, STRING); \
73 SkAutoTDelete<SkStreamAsset> asset(buffer.detachAsStream()); 74 } while (false)
74 SkString s(asset->getLength());
75 asset->read(s.writable_str(), s.size());
76 ERRORF(reporter, "!stream_equals() '%s' '%s'", expectedData, s.c_str());
77 }
78 75
79 if (indirect) {
80 // Indirect output.
81 static char header[] = "1 0 obj\n";
82 static size_t headerLen = strlen(header);
83 static char footer[] = "\nendobj\n";
84 static size_t footerLen = strlen(footer);
85 76
86 catalog.addObject(obj);
87
88 size_t indirectSize =
89 get_output_size(obj, catalog, substituteMap, true);
90 REPORTER_ASSERT(reporter,
91 indirectSize == directSize + headerLen + footerLen);
92
93 buffer.reset();
94 emit_object(obj, &buffer, catalog, substituteMap, true);
95 REPORTER_ASSERT(reporter, indirectSize == buffer.getOffset());
96 REPORTER_ASSERT(reporter, stream_equals(buffer, 0, header, headerLen));
97 REPORTER_ASSERT(reporter, stream_equals(buffer, headerLen, expectedData,
98 directSize));
99 REPORTER_ASSERT(reporter, stream_equals(buffer, headerLen + directSize,
100 footer, footerLen));
101 }
102 }
103
104 static void SimpleCheckObjectOutput(skiatest::Reporter* reporter,
105 SkPDFObject* obj,
106 const char* expectedResult) {
107 CheckObjectOutput(reporter, obj, expectedResult,
108 strlen(expectedResult), true);
109 }
110 77
111 static void TestPDFStream(skiatest::Reporter* reporter) { 78 static void TestPDFStream(skiatest::Reporter* reporter) {
112 char streamBytes[] = "Test\nFoo\tBar"; 79 char streamBytes[] = "Test\nFoo\tBar";
113 SkAutoTDelete<SkMemoryStream> streamData(new SkMemoryStream( 80 SkAutoTDelete<SkMemoryStream> streamData(new SkMemoryStream(
114 streamBytes, strlen(streamBytes), true)); 81 streamBytes, strlen(streamBytes), true));
115 SkAutoTUnref<SkPDFStream> stream(new SkPDFStream(streamData.get())); 82 SkAutoTUnref<SkPDFStream> stream(new SkPDFStream(streamData.get()));
116 SimpleCheckObjectOutput( 83 ASSERT_EMIT_EQ(reporter,
117 reporter, stream.get(), 84 *stream,
118 "<</Length 12>> stream\nTest\nFoo\tBar\nendstream"); 85 "<</Length 12>> stream\nTest\nFoo\tBar\nendstream");
119 stream->insert("Attribute", new SkPDFInt(42))->unref(); 86 stream->insertInt("Attribute", 42);
120 SimpleCheckObjectOutput(reporter, stream.get(), 87 ASSERT_EMIT_EQ(reporter,
121 "<</Length 12\n/Attribute 42>> stream\n" 88 *stream,
122 "Test\nFoo\tBar\nendstream"); 89 "<</Length 12\n/Attribute 42>> stream\n"
90 "Test\nFoo\tBar\nendstream");
123 91
124 { 92 {
125 char streamBytes2[] = "This is a longer string, so that compression " 93 char streamBytes2[] = "This is a longer string, so that compression "
126 "can do something with it. With shorter strings, " 94 "can do something with it. With shorter strings, "
127 "the short circuit logic cuts in and we end up " 95 "the short circuit logic cuts in and we end up "
128 "with an uncompressed string."; 96 "with an uncompressed string.";
129 SkAutoDataUnref streamData2(SkData::NewWithCopy(streamBytes2, 97 SkAutoDataUnref streamData2(SkData::NewWithCopy(streamBytes2,
130 strlen(streamBytes2))); 98 strlen(streamBytes2)));
131 SkAutoTUnref<SkPDFStream> stream(new SkPDFStream(streamData2.get())); 99 SkAutoTUnref<SkPDFStream> stream(new SkPDFStream(streamData2.get()));
132 100
133 SkDynamicMemoryWStream compressedByteStream; 101 SkDynamicMemoryWStream compressedByteStream;
134 SkFlate::Deflate(streamData2.get(), &compressedByteStream); 102 SkFlate::Deflate(streamData2.get(), &compressedByteStream);
135 SkAutoDataUnref compressedData(compressedByteStream.copyToData()); 103 SkAutoDataUnref compressedData(compressedByteStream.copyToData());
136 104
137 SkDynamicMemoryWStream expected; 105 SkDynamicMemoryWStream expected;
138 expected.writeText("<</Filter /FlateDecode\n/Length 116>> stream\n"); 106 expected.writeText("<</Filter /FlateDecode\n/Length 116>> stream\n");
139 expected.write(compressedData->data(), compressedData->size()); 107 expected.write(compressedData->data(), compressedData->size());
140 expected.writeText("\nendstream"); 108 expected.writeText("\nendstream");
141 SkAutoDataUnref expectedResultData2(expected.copyToData()); 109 SkAutoDataUnref expectedResultData2(expected.copyToData());
142 CheckObjectOutput(reporter, stream.get(), 110 SkString result = emit_to_string(*stream);
143 (const char*) expectedResultData2->data(), 111 ASSERT_EQL(reporter,
144 expectedResultData2->size(), true); 112 result,
113 (const char*)expectedResultData2->data(),
114 expectedResultData2->size());
145 } 115 }
146 } 116 }
147 117
148 static void TestCatalog(skiatest::Reporter* reporter) { 118 static void TestObjectNumberMap(skiatest::Reporter* reporter) {
149 SkPDFSubstituteMap substituteMap; 119 SkPDFObjNumMap objNumMap;
150 SkPDFObjNumMap catalog; 120 SkAutoTUnref<SkPDFArray> a1(new SkPDFArray);
151 SkAutoTUnref<SkPDFInt> int1(new SkPDFInt(1)); 121 SkAutoTUnref<SkPDFArray> a2(new SkPDFArray);
152 SkAutoTUnref<SkPDFInt> int2(new SkPDFInt(2)); 122 SkAutoTUnref<SkPDFArray> a3(new SkPDFArray);
153 SkAutoTUnref<SkPDFInt> int3(new SkPDFInt(3));
154 int1.get()->ref();
155 SkAutoTUnref<SkPDFInt> int1Again(int1.get());
156 123
157 catalog.addObject(int1.get()); 124 objNumMap.addObject(a1.get());
158 catalog.addObject(int2.get()); 125 objNumMap.addObject(a2.get());
159 catalog.addObject(int3.get()); 126 objNumMap.addObject(a3.get());
160 127
161 REPORTER_ASSERT(reporter, catalog.getObjectNumber(int1.get()) == 1); 128 // The objects should be numbered in the order they are added,
162 REPORTER_ASSERT(reporter, catalog.getObjectNumber(int2.get()) == 2); 129 // starting with 1.
163 REPORTER_ASSERT(reporter, catalog.getObjectNumber(int3.get()) == 3); 130 REPORTER_ASSERT(reporter, objNumMap.getObjectNumber(a1.get()) == 1);
164 REPORTER_ASSERT(reporter, catalog.getObjectNumber(int1Again.get()) == 1); 131 REPORTER_ASSERT(reporter, objNumMap.getObjectNumber(a2.get()) == 2);
132 REPORTER_ASSERT(reporter, objNumMap.getObjectNumber(a3.get()) == 3);
133 // Assert that repeated calls to get the object number return
134 // consistent result.
135 REPORTER_ASSERT(reporter, objNumMap.getObjectNumber(a1.get()) == 1);
165 } 136 }
166 137
167 static void TestObjectRef(skiatest::Reporter* reporter) { 138 static void TestObjectRef(skiatest::Reporter* reporter) {
168 SkAutoTUnref<SkPDFInt> int1(new SkPDFInt(1)); 139 SkAutoTUnref<SkPDFArray> a1(new SkPDFArray);
169 SkAutoTUnref<SkPDFInt> int2(new SkPDFInt(2)); 140 SkAutoTUnref<SkPDFArray> a2(new SkPDFArray);
170 SkAutoTUnref<SkPDFObjRef> int2ref(new SkPDFObjRef(int2.get())); 141 a2->appendObjRef(SkRef(a1.get()));
171 142
172 SkPDFSubstituteMap substituteMap; 143 Catalog catalog;
173 SkPDFObjNumMap catalog; 144 catalog.numbers.addObject(a1.get());
174 catalog.addObject(int1.get()); 145 REPORTER_ASSERT(reporter, catalog.numbers.getObjectNumber(a1.get()) == 1);
175 catalog.addObject(int2.get());
176 REPORTER_ASSERT(reporter, catalog.getObjectNumber(int1.get()) == 1);
177 REPORTER_ASSERT(reporter, catalog.getObjectNumber(int2.get()) == 2);
178 146
179 char expectedResult[] = "2 0 R"; 147 SkString result = emit_to_string(*a2, &catalog);
180 SkDynamicMemoryWStream buffer; 148 // If appendObjRef misbehaves, then the result would
181 int2ref->emitObject(&buffer, catalog, substituteMap); 149 // be [[]], not [1 0 R].
182 REPORTER_ASSERT(reporter, buffer.getOffset() == strlen(expectedResult)); 150 ASSERT_EQ(reporter, result, "[1 0 R]");
183 REPORTER_ASSERT(reporter, stream_equals(buffer, 0, expectedResult,
184 buffer.getOffset()));
185 } 151 }
186 152
187 static void TestSubstitute(skiatest::Reporter* reporter) { 153 static void TestSubstitute(skiatest::Reporter* reporter) {
188 SkAutoTUnref<SkPDFDict> proxy(new SkPDFDict()); 154 SkAutoTUnref<SkPDFDict> proxy(new SkPDFDict());
189 SkAutoTUnref<SkPDFDict> stub(new SkPDFDict()); 155 SkAutoTUnref<SkPDFDict> stub(new SkPDFDict());
190 156
191 proxy->insert("Value", new SkPDFInt(33))->unref(); 157 proxy->insertInt("Value", 33);
192 stub->insert("Value", new SkPDFInt(44))->unref(); 158 stub->insertInt("Value", 44);
193 159
194 SkPDFSubstituteMap substituteMap; 160 SkPDFSubstituteMap substituteMap;
195 substituteMap.setSubstitute(proxy.get(), stub.get()); 161 substituteMap.setSubstitute(proxy.get(), stub.get());
196 SkPDFObjNumMap catalog; 162 SkPDFObjNumMap catalog;
197 catalog.addObject(proxy.get()); 163 catalog.addObject(proxy.get());
198 164
199 REPORTER_ASSERT(reporter, stub.get() == substituteMap.getSubstitute(proxy)); 165 REPORTER_ASSERT(reporter, stub.get() == substituteMap.getSubstitute(proxy));
200 REPORTER_ASSERT(reporter, proxy.get() != substituteMap.getSubstitute(stub)); 166 REPORTER_ASSERT(reporter, proxy.get() != substituteMap.getSubstitute(stub));
201 } 167 }
202 168
203 // This test used to assert without the fix submitted for 169 // This test used to assert without the fix submitted for
204 // http://code.google.com/p/skia/issues/detail?id=1083. 170 // http://code.google.com/p/skia/issues/detail?id=1083.
205 // SKP files might have invalid glyph ids. This test ensures they are ignored, 171 // SKP files might have invalid glyph ids. This test ensures they are ignored,
206 // and there is no assert on input data in Debug mode. 172 // and there is no assert on input data in Debug mode.
207 static void test_issue1083() { 173 static void test_issue1083() {
208 SkDynamicMemoryWStream outStream; 174 SkDynamicMemoryWStream outStream;
209 SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(&outStream)); 175 SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(&outStream));
210 SkCanvas* canvas = doc->beginPage(100.0f, 100.0f); 176 SkCanvas* canvas = doc->beginPage(100.0f, 100.0f);
211 SkPaint paint; 177 SkPaint paint;
212 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); 178 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
213 179
214 uint16_t glyphID = 65000; 180 uint16_t glyphID = 65000;
215 canvas->drawText(&glyphID, 2, 0, 0, paint); 181 canvas->drawText(&glyphID, 2, 0, 0, paint);
216 182
217 doc->close(); 183 doc->close();
218 } 184 }
219 185
220 DEF_TEST(PDFPrimitives, reporter) { 186 static void TestPDFUnion(skiatest::Reporter* reporter) {
221 SkAutoTUnref<SkPDFInt> int42(new SkPDFInt(42)); 187 SkPDFUnion boolTrue = SkPDFUnion::Bool(true);
222 SimpleCheckObjectOutput(reporter, int42.get(), "42"); 188 ASSERT_EMIT_EQ(reporter, boolTrue, "true");
223 189
224 SkAutoTUnref<SkPDFScalar> realHalf(new SkPDFScalar(SK_ScalarHalf)); 190 SkPDFUnion boolFalse = SkPDFUnion::Bool(false);
225 SimpleCheckObjectOutput(reporter, realHalf.get(), "0.5"); 191 ASSERT_EMIT_EQ(reporter, boolFalse, "false");
226 192
227 SkAutoTUnref<SkPDFScalar> bigScalar(new SkPDFScalar(110999.75f)); 193 SkPDFUnion int42 = SkPDFUnion::Int(42);
194 ASSERT_EMIT_EQ(reporter, int42, "42");
195
196 SkPDFUnion realHalf = SkPDFUnion::Scalar(SK_ScalarHalf);
197 ASSERT_EMIT_EQ(reporter, realHalf, "0.5");
198
199 SkPDFUnion bigScalar = SkPDFUnion::Scalar(110999.75f);
228 #if !defined(SK_ALLOW_LARGE_PDF_SCALARS) 200 #if !defined(SK_ALLOW_LARGE_PDF_SCALARS)
229 SimpleCheckObjectOutput(reporter, bigScalar.get(), "111000"); 201 ASSERT_EMIT_EQ(reporter, bigScalar, "111000");
230 #else 202 #else
231 SimpleCheckObjectOutput(reporter, bigScalar.get(), "110999.75"); 203 ASSERT_EMIT_EQ(reporter, bigScalar, "110999.75");
232 204
233 SkAutoTUnref<SkPDFScalar> biggerScalar(new SkPDFScalar(50000000.1)); 205 SkPDFUnion biggerScalar = SkPDFUnion::Scalar(50000000.1);
234 SimpleCheckObjectOutput(reporter, biggerScalar.get(), "50000000"); 206 ASSERT_EMIT_EQ(reporter, biggerScalar, "50000000");
235 207
236 SkAutoTUnref<SkPDFScalar> smallestScalar(new SkPDFScalar(1.0/65536)); 208 SkPDFUnion smallestScalar = SkPDFUnion::Scalar(1.0 / 65536);
237 SimpleCheckObjectOutput(reporter, smallestScalar.get(), "0.00001526"); 209 ASSERT_EMIT_EQ(reporter, smallestScalar, "0.00001526");
238 #endif 210 #endif
239 211
240 SkAutoTUnref<SkPDFString> stringSimple( 212 SkPDFUnion stringSimple = SkPDFUnion::String("test ) string ( foo");
241 new SkPDFString("test ) string ( foo")); 213 ASSERT_EMIT_EQ(reporter, stringSimple, "(test \\) string \\( foo)");
242 SimpleCheckObjectOutput(reporter, stringSimple.get(),
243 "(test \\) string \\( foo)");
244 SkAutoTUnref<SkPDFString> stringComplex(
245 new SkPDFString("\ttest ) string ( foo"));
246 SimpleCheckObjectOutput(reporter, stringComplex.get(),
247 "<0974657374202920737472696E67202820666F6F>");
248 214
249 SkAutoTUnref<SkPDFName> name(new SkPDFName("Test name\twith#tab")); 215 SkString stringComplexInput("\ttest ) string ( foo");
250 const char expectedResult[] = "/Test#20name#09with#23tab"; 216 SkPDFUnion stringComplex = SkPDFUnion::String(stringComplexInput);
251 CheckObjectOutput(reporter, name.get(), expectedResult, 217 ASSERT_EMIT_EQ(reporter,
252 strlen(expectedResult), false); 218 stringComplex,
219 "<0974657374202920737472696E67202820666F6F>");
253 220
254 SkAutoTUnref<SkPDFName> escapedName(new SkPDFName("A#/%()<>[]{}B")); 221 SkString nameInput("Test name\twith#tab");
255 const char escapedNameExpected[] = "/A#23#2F#25#28#29#3C#3E#5B#5D#7B#7DB"; 222 SkPDFUnion name = SkPDFUnion::Name(nameInput);
256 CheckObjectOutput(reporter, escapedName.get(), escapedNameExpected, 223 ASSERT_EMIT_EQ(reporter, name, "/Test#20name#09with#23tab");
257 strlen(escapedNameExpected), false); 224
225 SkString nameInput2("A#/%()<>[]{}B");
226 SkPDFUnion name2 = SkPDFUnion::Name(nameInput2);
227 ASSERT_EMIT_EQ(reporter, name2, "/A#23#2F#25#28#29#3C#3E#5B#5D#7B#7DB");
228
229 SkPDFUnion name3 = SkPDFUnion::Name("SimpleNameWithOnlyPrintableASCII");
230 ASSERT_EMIT_EQ(reporter, name3, "/SimpleNameWithOnlyPrintableASCII");
258 231
259 // Test that we correctly handle characters with the high-bit set. 232 // Test that we correctly handle characters with the high-bit set.
260 const unsigned char highBitCString[] = {0xDE, 0xAD, 'b', 'e', 0xEF, 0}; 233 SkString highBitString("\xDE\xAD" "be\xEF");
261 SkAutoTUnref<SkPDFName> highBitName( 234 SkPDFUnion highBitName = SkPDFUnion::Name(highBitString);
262 new SkPDFName((const char*)highBitCString)); 235 ASSERT_EMIT_EQ(reporter, highBitName, "/#DE#ADbe#EF");
263 const char highBitExpectedResult[] = "/#DE#ADbe#EF"; 236 }
264 CheckObjectOutput(reporter, highBitName.get(), highBitExpectedResult,
265 strlen(highBitExpectedResult), false);
266 237
238 static void TestPDFArray(skiatest::Reporter* reporter) {
267 SkAutoTUnref<SkPDFArray> array(new SkPDFArray); 239 SkAutoTUnref<SkPDFArray> array(new SkPDFArray);
268 SimpleCheckObjectOutput(reporter, array.get(), "[]"); 240 ASSERT_EMIT_EQ(reporter, *array, "[]");
269 array->append(int42.get());
270 SimpleCheckObjectOutput(reporter, array.get(), "[42]");
271 array->append(realHalf.get());
272 SimpleCheckObjectOutput(reporter, array.get(), "[42 0.5]");
273 SkAutoTUnref<SkPDFInt> int0(new SkPDFInt(0));
274 array->append(int0.get());
275 SimpleCheckObjectOutput(reporter, array.get(), "[42 0.5 0]");
276 241
242 array->appendInt(42);
243 ASSERT_EMIT_EQ(reporter, *array, "[42]");
244
245 array->appendScalar(SK_ScalarHalf);
246 ASSERT_EMIT_EQ(reporter, *array, "[42 0.5]");
247
248 array->appendInt(0);
249 ASSERT_EMIT_EQ(reporter, *array, "[42 0.5 0]");
250
251 array->appendBool(true);
252 ASSERT_EMIT_EQ(reporter, *array, "[42 0.5 0 true]");
253
254 array->appendName("ThisName");
255 ASSERT_EMIT_EQ(reporter, *array, "[42 0.5 0 true /ThisName]");
256
257 array->appendName(SkString("AnotherName"));
258 ASSERT_EMIT_EQ(reporter, *array, "[42 0.5 0 true /ThisName /AnotherName]");
259
260 array->appendString("This String");
261 ASSERT_EMIT_EQ(reporter, *array,
262 "[42 0.5 0 true /ThisName /AnotherName (This String)]");
263
264 array->appendString(SkString("Another String"));
265 ASSERT_EMIT_EQ(reporter, *array,
266 "[42 0.5 0 true /ThisName /AnotherName (This String) "
267 "(Another String)]");
268
269 SkAutoTUnref<SkPDFArray> innerArray(new SkPDFArray);
270 innerArray->appendInt(-1);
271 array->appendObject(innerArray.detach());
272 ASSERT_EMIT_EQ(reporter, *array,
273 "[42 0.5 0 true /ThisName /AnotherName (This String) "
274 "(Another String) [-1]]");
275
276 SkAutoTUnref<SkPDFArray> referencedArray(new SkPDFArray);
277 Catalog catalog;
278 catalog.numbers.addObject(referencedArray.get());
279 REPORTER_ASSERT(reporter, catalog.numbers.getObjectNumber(
280 referencedArray.get()) == 1);
281 array->appendObjRef(referencedArray.detach());
282
283 SkString result = emit_to_string(*array, &catalog);
284 ASSERT_EQ(reporter, result,
285 "[42 0.5 0 true /ThisName /AnotherName (This String) "
286 "(Another String) [-1] 1 0 R]");
287 }
288
289 static void TestPDFDict(skiatest::Reporter* reporter) {
277 SkAutoTUnref<SkPDFDict> dict(new SkPDFDict); 290 SkAutoTUnref<SkPDFDict> dict(new SkPDFDict);
278 SimpleCheckObjectOutput(reporter, dict.get(), "<<>>"); 291 ASSERT_EMIT_EQ(reporter, *dict, "<<>>");
279 dict->insert("n1", int42.get()); 292
280 SimpleCheckObjectOutput(reporter, dict.get(), "<</n1 42>>"); 293 dict->insertInt("n1", SkToSizeT(42));
294 ASSERT_EMIT_EQ(reporter, *dict, "<</n1 42>>");
295
296 dict.reset(new SkPDFDict);
297 ASSERT_EMIT_EQ(reporter, *dict, "<<>>");
298
299 dict->insertInt("n1", 42);
300 ASSERT_EMIT_EQ(reporter, *dict, "<</n1 42>>");
301
302 dict->insertScalar("n2", SK_ScalarHalf);
303
281 SkString n3("n3"); 304 SkString n3("n3");
282 dict->insert("n2", realHalf.get()); 305 SkAutoTUnref<SkPDFArray> innerArray(new SkPDFArray);
283 dict->insertObject(n3, array.detach()); 306 innerArray->appendInt(-100);
284 SimpleCheckObjectOutput(reporter, dict.get(), 307 dict->insertObject(n3, innerArray.detach());
285 "<</n1 42\n/n2 0.5\n/n3 [42 0.5 0]>>"); 308 ASSERT_EMIT_EQ(reporter, *dict, "<</n1 42\n/n2 0.5\n/n3 [-100]>>");
286 309
310 dict.reset(new SkPDFDict);
311 ASSERT_EMIT_EQ(reporter, *dict, "<<>>");
312
313 dict->insertInt("n1", 24);
314 ASSERT_EMIT_EQ(reporter, *dict, "<</n1 24>>");
315
316 dict->insertInt("n2", SkToSizeT(99));
317 ASSERT_EMIT_EQ(reporter, *dict, "<</n1 24\n/n2 99>>");
318
319 dict->insertScalar("n3", SK_ScalarHalf);
320 ASSERT_EMIT_EQ(reporter, *dict, "<</n1 24\n/n2 99\n/n3 0.5>>");
321
322 dict->insertName("n4", "AName");
323 ASSERT_EMIT_EQ(reporter, *dict, "<</n1 24\n/n2 99\n/n3 0.5\n/n4 /AName>>");
324
325 dict->insertName("n5", SkString("AnotherName"));
326 ASSERT_EMIT_EQ(reporter, *dict, "<</n1 24\n/n2 99\n/n3 0.5\n/n4 /AName\n"
327 "/n5 /AnotherName>>");
328
329 dict->insertString("n6", "A String");
330 ASSERT_EMIT_EQ(reporter, *dict, "<</n1 24\n/n2 99\n/n3 0.5\n/n4 /AName\n"
331 "/n5 /AnotherName\n/n6 (A String)>>");
332
333 dict->insertString("n7", SkString("Another String"));
334 ASSERT_EMIT_EQ(reporter, *dict, "<</n1 24\n/n2 99\n/n3 0.5\n/n4 /AName\n"
335 "/n5 /AnotherName\n/n6 (A String)\n/n7 (Another String)>>");
336
337 dict.reset(new SkPDFDict("DType"));
338 ASSERT_EMIT_EQ(reporter, *dict, "<</Type /DType>>");
339
340 SkAutoTUnref<SkPDFArray> referencedArray(new SkPDFArray);
341 Catalog catalog;
342 catalog.numbers.addObject(referencedArray.get());
343 REPORTER_ASSERT(reporter, catalog.numbers.getObjectNumber(
344 referencedArray.get()) == 1);
345 dict->insertObjRef("n1", referencedArray.detach());
346 SkString result = emit_to_string(*dict, &catalog);
347 ASSERT_EQ(reporter, result, "<</Type /DType\n/n1 1 0 R>>");
348 }
349
350 DEF_TEST(PDFPrimitives, reporter) {
351 TestPDFUnion(reporter);
352 TestPDFArray(reporter);
353 TestPDFDict(reporter);
287 TestPDFStream(reporter); 354 TestPDFStream(reporter);
288 355 TestObjectNumberMap(reporter);
289 TestCatalog(reporter);
290
291 TestObjectRef(reporter); 356 TestObjectRef(reporter);
292
293 TestSubstitute(reporter); 357 TestSubstitute(reporter);
294
295 test_issue1083(); 358 test_issue1083();
296 } 359 }
297 360
298 namespace { 361 namespace {
299 362
300 class DummyImageFilter : public SkImageFilter { 363 class DummyImageFilter : public SkImageFilter {
301 public: 364 public:
302 DummyImageFilter(bool visited = false) : SkImageFilter(0, NULL), fVisited(vi sited) {} 365 DummyImageFilter(bool visited = false) : SkImageFilter(0, NULL), fVisited(vi sited) {}
303 ~DummyImageFilter() override {} 366 ~DummyImageFilter() override {}
304 virtual bool onFilterImage(Proxy*, const SkBitmap& src, const Context&, 367 virtual bool onFilterImage(Proxy*, const SkBitmap& src, const Context&,
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
343 // Filter just created; should be unvisited. 406 // Filter just created; should be unvisited.
344 REPORTER_ASSERT(reporter, !filter->visited()); 407 REPORTER_ASSERT(reporter, !filter->visited());
345 SkPaint paint; 408 SkPaint paint;
346 paint.setImageFilter(filter.get()); 409 paint.setImageFilter(filter.get());
347 canvas->drawRect(SkRect::MakeWH(100, 100), paint); 410 canvas->drawRect(SkRect::MakeWH(100, 100), paint);
348 doc->close(); 411 doc->close();
349 412
350 // Filter was used in rendering; should be visited. 413 // Filter was used in rendering; should be visited.
351 REPORTER_ASSERT(reporter, filter->visited()); 414 REPORTER_ASSERT(reporter, filter->visited());
352 } 415 }
OLDNEW
« no previous file with comments | « src/pdf/SkPDFTypes.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698