OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2013 Google Inc. | |
3 * | |
4 * Use of this source code is governed by a BSD-style license that can be | |
5 * found in the LICENSE file. | |
6 */ | |
7 #include "Test.h" | |
8 | |
9 #include "SkCanvas.h" | |
10 #include "SkDocument.h" | |
11 #include "SkOSFile.h" | |
12 #include "SkStream.h" | |
13 #if SK_SUPPORT_PDF | |
14 | |
15 static void test_empty(skiatest::Reporter* reporter) { | |
16 SkDynamicMemoryWStream stream; | |
17 | |
18 SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(&stream)); | |
19 | |
20 doc->close(); | |
21 | |
22 REPORTER_ASSERT(reporter, stream.bytesWritten() == 0); | |
23 } | |
24 | |
25 static void test_abort(skiatest::Reporter* reporter) { | |
26 SkDynamicMemoryWStream stream; | |
27 SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(&stream)); | |
28 | |
29 SkCanvas* canvas = doc->beginPage(100, 100); | |
30 canvas->drawColor(SK_ColorRED); | |
31 doc->endPage(); | |
32 | |
33 doc->abort(); | |
34 | |
35 REPORTER_ASSERT(reporter, stream.bytesWritten() == 0); | |
36 } | |
37 | |
38 static void test_abortWithFile(skiatest::Reporter* reporter) { | |
39 SkString tmpDir = skiatest::GetTmpDir(); | |
40 | |
41 if (tmpDir.isEmpty()) { | |
42 return; // TODO(edisonn): unfortunatelly this pattern is used in other | |
43 // tests, but if GetTmpDir() starts returning and empty dir | |
44 // allways, then all these tests will be disabled. | |
45 } | |
46 | |
47 SkString path = SkOSPath::Join(tmpDir.c_str(), "aborted.pdf"); | |
48 | |
49 // Make sure doc's destructor is called to flush. | |
50 { | |
51 SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(path.c_str())); | |
52 | |
53 SkCanvas* canvas = doc->beginPage(100, 100); | |
54 canvas->drawColor(SK_ColorRED); | |
55 doc->endPage(); | |
56 | |
57 doc->abort(); | |
58 } | |
59 | |
60 FILE* file = fopen(path.c_str(), "r"); | |
61 // The created file should be empty. | |
62 char buffer[100]; | |
63 REPORTER_ASSERT(reporter, fread(buffer, 1, 1, file) == 0); | |
64 fclose(file); | |
65 } | |
66 | |
67 static void test_file(skiatest::Reporter* reporter) { | |
68 SkString tmpDir = skiatest::GetTmpDir(); | |
69 if (tmpDir.isEmpty()) { | |
70 return; // TODO(edisonn): unfortunatelly this pattern is used in other | |
71 // tests, but if GetTmpDir() starts returning and empty dir | |
72 // allways, then all these tests will be disabled. | |
73 } | |
74 | |
75 SkString path = SkOSPath::Join(tmpDir.c_str(), "file.pdf"); | |
76 | |
77 SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(path.c_str())); | |
78 | |
79 SkCanvas* canvas = doc->beginPage(100, 100); | |
80 | |
81 canvas->drawColor(SK_ColorRED); | |
82 doc->endPage(); | |
83 doc->close(); | |
84 | |
85 FILE* file = fopen(path.c_str(), "r"); | |
86 REPORTER_ASSERT(reporter, file != NULL); | |
87 char header[100]; | |
88 REPORTER_ASSERT(reporter, fread(header, 4, 1, file) != 0); | |
89 REPORTER_ASSERT(reporter, strncmp(header, "%PDF", 4) == 0); | |
90 fclose(file); | |
91 } | |
92 | |
93 static void test_close(skiatest::Reporter* reporter) { | |
94 SkDynamicMemoryWStream stream; | |
95 SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(&stream)); | |
96 | |
97 SkCanvas* canvas = doc->beginPage(100, 100); | |
98 canvas->drawColor(SK_ColorRED); | |
99 doc->endPage(); | |
100 | |
101 doc->close(); | |
102 | |
103 REPORTER_ASSERT(reporter, stream.bytesWritten() != 0); | |
104 } | |
105 | |
106 DEF_TEST(document_tests, reporter) { | |
107 test_empty(reporter); | |
108 test_abort(reporter); | |
109 test_abortWithFile(reporter); | |
110 test_file(reporter); | |
111 test_close(reporter); | |
112 } | |
113 #endif // SK_SUPPORT_PDF | |
OLD | NEW |