4 #include "SkDocument.h"
8 static void test_empty(skiatest::Reporter* reporter) {
9 SkDynamicMemoryWStream stream;
11 SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(&stream));
15 REPORTER_ASSERT(reporter, stream.bytesWritten() == 0);
18 static void test_abort(skiatest::Reporter* reporter) {
19 SkDynamicMemoryWStream stream;
20 SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(&stream));
22 SkCanvas* canvas = doc->beginPage(100, 100);
23 canvas->drawColor(SK_ColorRED);
28 REPORTER_ASSERT(reporter, stream.bytesWritten() == 0);
31 static void test_abortWithFile(skiatest::Reporter* reporter) {
32 SkString tmpDir = skiatest::Test::GetTmpDir();
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.
40 SkString path = SkOSPath::Join(tmpDir.c_str(), "aborted.pdf");
42 // Make sure doc's destructor is called to flush.
44 SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(path.c_str()));
46 SkCanvas* canvas = doc->beginPage(100, 100);
47 canvas->drawColor(SK_ColorRED);
53 FILE* file = fopen(path.c_str(), "r");
54 // The created file should be empty.
56 REPORTER_ASSERT(reporter, fread(buffer, 1, 1, file) == 0);
60 static void test_file(skiatest::Reporter* reporter) {
61 SkString tmpDir = skiatest::Test::GetTmpDir();
62 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.
68 SkString path = SkOSPath::Join(tmpDir.c_str(), "file.pdf");
70 SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(path.c_str()));
72 SkCanvas* canvas = doc->beginPage(100, 100);
74 canvas->drawColor(SK_ColorRED);
78 FILE* file = fopen(path.c_str(), "r");
79 REPORTER_ASSERT(reporter, file != NULL);
81 REPORTER_ASSERT(reporter, fread(header, 4, 1, file) != 0);
82 REPORTER_ASSERT(reporter, strncmp(header, "%PDF", 4) == 0);
86 static void test_close(skiatest::Reporter* reporter) {
87 SkDynamicMemoryWStream stream;
88 SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(&stream));
90 SkCanvas* canvas = doc->beginPage(100, 100);
91 canvas->drawColor(SK_ColorRED);
96 REPORTER_ASSERT(reporter, stream.bytesWritten() != 0);
99 DEF_TEST(document_tests, reporter) {
100 test_empty(reporter);
101 test_abort(reporter);
102 test_abortWithFile(reporter);
104 test_close(reporter);