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

Side by Side Diff: tests/PDFDocumentTest.cpp

Issue 1916093002: SkDocument/PDF: new API (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: 2016-04-26 (Tuesday) 15:55:33 EDT Created 4 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 | « tests/CanvasTest.cpp ('k') | tests/PDFInvalidBitmapTest.cpp » ('j') | 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 2013 Google Inc. 2 * Copyright 2013 Google Inc.
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 #include "Test.h" 7 #include "Test.h"
8 8
9 #include "Resources.h" 9 #include "Resources.h"
10 #include "SkCanvas.h" 10 #include "SkCanvas.h"
11 #include "SkDocument.h" 11 #include "SkDocument.h"
12 #include "SkOSFile.h" 12 #include "SkOSFile.h"
13 #include "SkStream.h" 13 #include "SkStream.h"
14 #include "SkPixelSerializer.h" 14 #include "SkPixelSerializer.h"
15 15
16 static void test_empty(skiatest::Reporter* reporter) { 16 static void test_empty(skiatest::Reporter* reporter) {
17 SkDynamicMemoryWStream stream; 17 SkDynamicMemoryWStream stream;
18 18
19 SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(&stream)); 19 sk_sp<SkDocument> doc(SkDocument::MakePDF(&stream));
20 20
21 doc->close(); 21 doc->close();
22 22
23 REPORTER_ASSERT(reporter, stream.bytesWritten() == 0); 23 REPORTER_ASSERT(reporter, stream.bytesWritten() == 0);
24 } 24 }
25 25
26 static void test_abort(skiatest::Reporter* reporter) { 26 static void test_abort(skiatest::Reporter* reporter) {
27 SkDynamicMemoryWStream stream; 27 SkDynamicMemoryWStream stream;
28 SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(&stream)); 28 sk_sp<SkDocument> doc(SkDocument::MakePDF(&stream));
29 29
30 SkCanvas* canvas = doc->beginPage(100, 100); 30 SkCanvas* canvas = doc->beginPage(100, 100);
31 canvas->drawColor(SK_ColorRED); 31 canvas->drawColor(SK_ColorRED);
32 doc->endPage(); 32 doc->endPage();
33 33
34 doc->abort(); 34 doc->abort();
35 35
36 // Test that only the header is written, not the full document. 36 // Test that only the header is written, not the full document.
37 REPORTER_ASSERT(reporter, stream.bytesWritten() < 256); 37 REPORTER_ASSERT(reporter, stream.bytesWritten() < 256);
38 } 38 }
39 39
40 static void test_abortWithFile(skiatest::Reporter* reporter) { 40 static void test_abortWithFile(skiatest::Reporter* reporter) {
41 SkString tmpDir = skiatest::GetTmpDir(); 41 SkString tmpDir = skiatest::GetTmpDir();
42 42
43 if (tmpDir.isEmpty()) { 43 if (tmpDir.isEmpty()) {
44 return; // TODO(edisonn): unfortunatelly this pattern is used in other 44 return; // TODO(edisonn): unfortunatelly this pattern is used in other
45 // tests, but if GetTmpDir() starts returning and empty dir 45 // tests, but if GetTmpDir() starts returning and empty dir
46 // allways, then all these tests will be disabled. 46 // allways, then all these tests will be disabled.
47 } 47 }
48 48
49 SkString path = SkOSPath::Join(tmpDir.c_str(), "aborted.pdf"); 49 SkString path = SkOSPath::Join(tmpDir.c_str(), "aborted.pdf");
50 50
51 // Make sure doc's destructor is called to flush. 51 // Make sure doc's destructor is called to flush.
52 { 52 {
53 SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(path.c_str())); 53 sk_sp<SkDocument> doc(SkDocument::MakePDF(path.c_str()));
54 54
55 SkCanvas* canvas = doc->beginPage(100, 100); 55 SkCanvas* canvas = doc->beginPage(100, 100);
56 canvas->drawColor(SK_ColorRED); 56 canvas->drawColor(SK_ColorRED);
57 doc->endPage(); 57 doc->endPage();
58 58
59 doc->abort(); 59 doc->abort();
60 } 60 }
61 61
62 FILE* file = fopen(path.c_str(), "r"); 62 FILE* file = fopen(path.c_str(), "r");
63 // The created file should be empty. 63 // The created file should be empty.
64 char buffer[100]; 64 char buffer[100];
65 REPORTER_ASSERT(reporter, fread(buffer, 1, 1, file) == 0); 65 REPORTER_ASSERT(reporter, fread(buffer, 1, 1, file) == 0);
66 fclose(file); 66 fclose(file);
67 } 67 }
68 68
69 static void test_file(skiatest::Reporter* reporter) { 69 static void test_file(skiatest::Reporter* reporter) {
70 SkString tmpDir = skiatest::GetTmpDir(); 70 SkString tmpDir = skiatest::GetTmpDir();
71 if (tmpDir.isEmpty()) { 71 if (tmpDir.isEmpty()) {
72 return; // TODO(edisonn): unfortunatelly this pattern is used in other 72 return; // TODO(edisonn): unfortunatelly this pattern is used in other
73 // tests, but if GetTmpDir() starts returning and empty dir 73 // tests, but if GetTmpDir() starts returning and empty dir
74 // allways, then all these tests will be disabled. 74 // allways, then all these tests will be disabled.
75 } 75 }
76 76
77 SkString path = SkOSPath::Join(tmpDir.c_str(), "file.pdf"); 77 SkString path = SkOSPath::Join(tmpDir.c_str(), "file.pdf");
78 78
79 SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(path.c_str())); 79 sk_sp<SkDocument> doc(SkDocument::MakePDF(path.c_str()));
80 80
81 SkCanvas* canvas = doc->beginPage(100, 100); 81 SkCanvas* canvas = doc->beginPage(100, 100);
82 82
83 canvas->drawColor(SK_ColorRED); 83 canvas->drawColor(SK_ColorRED);
84 doc->endPage(); 84 doc->endPage();
85 doc->close(); 85 doc->close();
86 86
87 FILE* file = fopen(path.c_str(), "r"); 87 FILE* file = fopen(path.c_str(), "r");
88 REPORTER_ASSERT(reporter, file != nullptr); 88 REPORTER_ASSERT(reporter, file != nullptr);
89 char header[100]; 89 char header[100];
90 REPORTER_ASSERT(reporter, fread(header, 4, 1, file) != 0); 90 REPORTER_ASSERT(reporter, fread(header, 4, 1, file) != 0);
91 REPORTER_ASSERT(reporter, strncmp(header, "%PDF", 4) == 0); 91 REPORTER_ASSERT(reporter, strncmp(header, "%PDF", 4) == 0);
92 fclose(file); 92 fclose(file);
93 } 93 }
94 94
95 static void test_close(skiatest::Reporter* reporter) { 95 static void test_close(skiatest::Reporter* reporter) {
96 SkDynamicMemoryWStream stream; 96 SkDynamicMemoryWStream stream;
97 SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(&stream)); 97 sk_sp<SkDocument> doc(SkDocument::MakePDF(&stream));
98 98
99 SkCanvas* canvas = doc->beginPage(100, 100); 99 SkCanvas* canvas = doc->beginPage(100, 100);
100 canvas->drawColor(SK_ColorRED); 100 canvas->drawColor(SK_ColorRED);
101 doc->endPage(); 101 doc->endPage();
102 102
103 doc->close(); 103 doc->close();
104 104
105 REPORTER_ASSERT(reporter, stream.bytesWritten() != 0); 105 REPORTER_ASSERT(reporter, stream.bytesWritten() != 0);
106 } 106 }
107 107
(...skipping 17 matching lines...) Expand all
125 pixmap.ctable(), 125 pixmap.ctable(),
126 nullptr, nullptr) 126 nullptr, nullptr)
127 ? SkImageEncoder::EncodeData(bm, SkImageEncoder::kJPEG_Type, 85) 127 ? SkImageEncoder::EncodeData(bm, SkImageEncoder::kJPEG_Type, 85)
128 : nullptr; 128 : nullptr;
129 } 129 }
130 }; 130 };
131 } // namespace 131 } // namespace
132 132
133 size_t count_bytes(const SkBitmap& bm, bool useDCT) { 133 size_t count_bytes(const SkBitmap& bm, bool useDCT) {
134 SkDynamicMemoryWStream stream; 134 SkDynamicMemoryWStream stream;
135 SkAutoTUnref<SkDocument> doc; 135 sk_sp<SkDocument> doc;
136 if (useDCT) { 136 if (useDCT) {
137 SkAutoTUnref<SkPixelSerializer> serializer(new JPEGSerializer); 137 doc = SkDocument::MakePDF(&stream, SK_ScalarDefaultRasterDPI,
138 doc.reset(SkDocument::CreatePDF( 138 SkDocument::PDFMetadata(),
139 &stream, SK_ScalarDefaultRasterDPI, serializer)); 139 sk_make_sp<JPEGSerializer>(), false);
140 } else { 140 } else {
141 doc.reset(SkDocument::CreatePDF(&stream)); 141 doc = SkDocument::MakePDF(&stream);
142 } 142 }
143 SkCanvas* canvas = doc->beginPage(64, 64); 143 SkCanvas* canvas = doc->beginPage(64, 64);
144 canvas->drawBitmap(bm, 0, 0); 144 canvas->drawBitmap(bm, 0, 0);
145 doc->endPage(); 145 doc->endPage();
146 doc->close(); 146 doc->close();
147 return stream.bytesWritten(); 147 return stream.bytesWritten();
148 } 148 }
149 149
150 DEF_TEST(document_dct_encoder, r) { 150 DEF_TEST(document_dct_encoder, r) {
151 REQUIRE_PDF_DOCUMENT(document_dct_encoder, r); 151 REQUIRE_PDF_DOCUMENT(document_dct_encoder, r);
152 SkBitmap bm; 152 SkBitmap bm;
153 if (GetResourceAsBitmap("mandrill_64.png", &bm)) { 153 if (GetResourceAsBitmap("mandrill_64.png", &bm)) {
154 // Lossy encoding works better on photographs. 154 // Lossy encoding works better on photographs.
155 REPORTER_ASSERT(r, count_bytes(bm, true) < count_bytes(bm, false)); 155 REPORTER_ASSERT(r, count_bytes(bm, true) < count_bytes(bm, false));
156 } 156 }
157 } 157 }
158 158
159 DEF_TEST(document_skbug_4734, r) { 159 DEF_TEST(document_skbug_4734, r) {
160 REQUIRE_PDF_DOCUMENT(document_skbug_4734, r); 160 REQUIRE_PDF_DOCUMENT(document_skbug_4734, r);
161 SkDynamicMemoryWStream stream; 161 SkDynamicMemoryWStream stream;
162 SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(&stream)); 162 sk_sp<SkDocument> doc(SkDocument::MakePDF(&stream));
163 SkCanvas* canvas = doc->beginPage(64, 64); 163 SkCanvas* canvas = doc->beginPage(64, 64);
164 canvas->scale(10000.0f, 10000.0f); 164 canvas->scale(10000.0f, 10000.0f);
165 canvas->translate(20.0f, 10.0f); 165 canvas->translate(20.0f, 10.0f);
166 canvas->rotate(30.0f); 166 canvas->rotate(30.0f);
167 const char text[] = "HELLO"; 167 const char text[] = "HELLO";
168 canvas->drawText(text, strlen(text), 0, 0, SkPaint()); 168 canvas->drawText(text, strlen(text), 0, 0, SkPaint());
169 } 169 }
OLDNEW
« no previous file with comments | « tests/CanvasTest.cpp ('k') | tests/PDFInvalidBitmapTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698