blob: 8bfb502700f0126ebd2b5f544f657caf5251051c [file] [log] [blame]
commit-bot@chromium.org8c908272013-10-22 14:49:03 +00001#include "Test.h"
commit-bot@chromium.org8c908272013-10-22 14:49:03 +00002
3#include "SkCanvas.h"
4#include "SkDocument.h"
5#include "SkOSFile.h"
6#include "SkStream.h"
7
8static void test_empty(skiatest::Reporter* reporter) {
9 SkDynamicMemoryWStream stream;
10
robertphillips@google.com701b4052013-11-18 16:26:25 +000011 SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(&stream));
commit-bot@chromium.org8c908272013-10-22 14:49:03 +000012
13 doc->close();
14
15 REPORTER_ASSERT(reporter, stream.bytesWritten() == 0);
16}
17
18static void test_abort(skiatest::Reporter* reporter) {
19 SkDynamicMemoryWStream stream;
20 SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(&stream));
21
22 SkCanvas* canvas = doc->beginPage(100, 100);
23 canvas->drawColor(SK_ColorRED);
24 doc->endPage();
25
26 doc->abort();
27
28 REPORTER_ASSERT(reporter, stream.bytesWritten() == 0);
29}
30
31static void test_abortWithFile(skiatest::Reporter* reporter) {
halcanary87f3ba42015-01-20 09:30:20 -080032 SkString tmpDir = skiatest::GetTmpDir();
commit-bot@chromium.org8c908272013-10-22 14:49:03 +000033
34 if (tmpDir.isEmpty()) {
35 return; // TODO(edisonn): unfortunatelly this pattern is used in other
36 // tests, but if GetTmpDir() starts returning and empty dir
37 // allways, then all these tests will be disabled.
38 }
39
tfarinaa8e2e152014-07-28 19:26:58 -070040 SkString path = SkOSPath::Join(tmpDir.c_str(), "aborted.pdf");
commit-bot@chromium.org8c908272013-10-22 14:49:03 +000041
42 // Make sure doc's destructor is called to flush.
43 {
44 SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(path.c_str()));
45
46 SkCanvas* canvas = doc->beginPage(100, 100);
47 canvas->drawColor(SK_ColorRED);
48 doc->endPage();
49
50 doc->abort();
51 }
52
53 FILE* file = fopen(path.c_str(), "r");
54 // The created file should be empty.
55 char buffer[100];
56 REPORTER_ASSERT(reporter, fread(buffer, 1, 1, file) == 0);
57 fclose(file);
58}
59
60static void test_file(skiatest::Reporter* reporter) {
halcanary87f3ba42015-01-20 09:30:20 -080061 SkString tmpDir = skiatest::GetTmpDir();
commit-bot@chromium.org8c908272013-10-22 14:49:03 +000062 if (tmpDir.isEmpty()) {
63 return; // TODO(edisonn): unfortunatelly this pattern is used in other
64 // tests, but if GetTmpDir() starts returning and empty dir
65 // allways, then all these tests will be disabled.
66 }
67
tfarinaa8e2e152014-07-28 19:26:58 -070068 SkString path = SkOSPath::Join(tmpDir.c_str(), "file.pdf");
commit-bot@chromium.org8c908272013-10-22 14:49:03 +000069
70 SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(path.c_str()));
71
72 SkCanvas* canvas = doc->beginPage(100, 100);
73
74 canvas->drawColor(SK_ColorRED);
75 doc->endPage();
76 doc->close();
77
78 FILE* file = fopen(path.c_str(), "r");
79 REPORTER_ASSERT(reporter, file != NULL);
80 char header[100];
edisonn@google.com5237b7f2013-10-22 18:33:21 +000081 REPORTER_ASSERT(reporter, fread(header, 4, 1, file) != 0);
commit-bot@chromium.org8c908272013-10-22 14:49:03 +000082 REPORTER_ASSERT(reporter, strncmp(header, "%PDF", 4) == 0);
83 fclose(file);
84}
85
86static void test_close(skiatest::Reporter* reporter) {
87 SkDynamicMemoryWStream stream;
88 SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(&stream));
89
90 SkCanvas* canvas = doc->beginPage(100, 100);
91 canvas->drawColor(SK_ColorRED);
92 doc->endPage();
93
94 doc->close();
95
96 REPORTER_ASSERT(reporter, stream.bytesWritten() != 0);
97}
98
99DEF_TEST(document_tests, reporter) {
100 test_empty(reporter);
101 test_abort(reporter);
102 test_abortWithFile(reporter);
103 test_file(reporter);
104 test_close(reporter);
105}