blob: f9fcdff6a2b8d1e906cd59c6c6482351539c4a41 [file] [log] [blame]
halcanary8ee06f22015-08-11 10:30:12 -07001/*
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 */
commit-bot@chromium.org8c908272013-10-22 14:49:03 +00007#include "Test.h"
commit-bot@chromium.org8c908272013-10-22 14:49:03 +00008
9#include "SkCanvas.h"
10#include "SkDocument.h"
11#include "SkOSFile.h"
12#include "SkStream.h"
13
14static void test_empty(skiatest::Reporter* reporter) {
15 SkDynamicMemoryWStream stream;
16
robertphillips@google.com701b4052013-11-18 16:26:25 +000017 SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(&stream));
commit-bot@chromium.org8c908272013-10-22 14:49:03 +000018
19 doc->close();
20
21 REPORTER_ASSERT(reporter, stream.bytesWritten() == 0);
22}
23
24static void test_abort(skiatest::Reporter* reporter) {
25 SkDynamicMemoryWStream stream;
26 SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(&stream));
27
28 SkCanvas* canvas = doc->beginPage(100, 100);
29 canvas->drawColor(SK_ColorRED);
30 doc->endPage();
31
32 doc->abort();
33
34 REPORTER_ASSERT(reporter, stream.bytesWritten() == 0);
35}
36
37static void test_abortWithFile(skiatest::Reporter* reporter) {
halcanary87f3ba42015-01-20 09:30:20 -080038 SkString tmpDir = skiatest::GetTmpDir();
commit-bot@chromium.org8c908272013-10-22 14:49:03 +000039
40 if (tmpDir.isEmpty()) {
41 return; // TODO(edisonn): unfortunatelly this pattern is used in other
42 // tests, but if GetTmpDir() starts returning and empty dir
43 // allways, then all these tests will be disabled.
44 }
45
tfarinaa8e2e152014-07-28 19:26:58 -070046 SkString path = SkOSPath::Join(tmpDir.c_str(), "aborted.pdf");
commit-bot@chromium.org8c908272013-10-22 14:49:03 +000047
48 // Make sure doc's destructor is called to flush.
49 {
50 SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(path.c_str()));
51
52 SkCanvas* canvas = doc->beginPage(100, 100);
53 canvas->drawColor(SK_ColorRED);
54 doc->endPage();
55
56 doc->abort();
57 }
58
59 FILE* file = fopen(path.c_str(), "r");
60 // The created file should be empty.
61 char buffer[100];
62 REPORTER_ASSERT(reporter, fread(buffer, 1, 1, file) == 0);
63 fclose(file);
64}
65
66static void test_file(skiatest::Reporter* reporter) {
halcanary87f3ba42015-01-20 09:30:20 -080067 SkString tmpDir = skiatest::GetTmpDir();
commit-bot@chromium.org8c908272013-10-22 14:49:03 +000068 if (tmpDir.isEmpty()) {
69 return; // TODO(edisonn): unfortunatelly this pattern is used in other
70 // tests, but if GetTmpDir() starts returning and empty dir
71 // allways, then all these tests will be disabled.
72 }
73
tfarinaa8e2e152014-07-28 19:26:58 -070074 SkString path = SkOSPath::Join(tmpDir.c_str(), "file.pdf");
commit-bot@chromium.org8c908272013-10-22 14:49:03 +000075
76 SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(path.c_str()));
77
78 SkCanvas* canvas = doc->beginPage(100, 100);
79
80 canvas->drawColor(SK_ColorRED);
81 doc->endPage();
82 doc->close();
83
84 FILE* file = fopen(path.c_str(), "r");
85 REPORTER_ASSERT(reporter, file != NULL);
86 char header[100];
edisonn@google.com5237b7f2013-10-22 18:33:21 +000087 REPORTER_ASSERT(reporter, fread(header, 4, 1, file) != 0);
commit-bot@chromium.org8c908272013-10-22 14:49:03 +000088 REPORTER_ASSERT(reporter, strncmp(header, "%PDF", 4) == 0);
89 fclose(file);
90}
91
92static void test_close(skiatest::Reporter* reporter) {
93 SkDynamicMemoryWStream stream;
94 SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(&stream));
95
96 SkCanvas* canvas = doc->beginPage(100, 100);
97 canvas->drawColor(SK_ColorRED);
98 doc->endPage();
99
100 doc->close();
101
102 REPORTER_ASSERT(reporter, stream.bytesWritten() != 0);
103}
104
105DEF_TEST(document_tests, reporter) {
halcanary2ccdb632015-08-11 13:35:12 -0700106 REQUIRE_PDF_DOCUMENT(document_tests, reporter);
commit-bot@chromium.org8c908272013-10-22 14:49:03 +0000107 test_empty(reporter);
108 test_abort(reporter);
109 test_abortWithFile(reporter);
110 test_file(reporter);
111 test_close(reporter);
112}