halcanary | 8ee06f2 | 2015-08-11 10:30:12 -0700 | [diff] [blame] | 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 | */ |
commit-bot@chromium.org | 8c90827 | 2013-10-22 14:49:03 +0000 | [diff] [blame] | 7 | #include "Test.h" |
commit-bot@chromium.org | 8c90827 | 2013-10-22 14:49:03 +0000 | [diff] [blame] | 8 | |
| 9 | #include "SkCanvas.h" |
| 10 | #include "SkDocument.h" |
| 11 | #include "SkOSFile.h" |
| 12 | #include "SkStream.h" |
| 13 | |
| 14 | static void test_empty(skiatest::Reporter* reporter) { |
| 15 | SkDynamicMemoryWStream stream; |
| 16 | |
robertphillips@google.com | 701b405 | 2013-11-18 16:26:25 +0000 | [diff] [blame] | 17 | SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(&stream)); |
commit-bot@chromium.org | 8c90827 | 2013-10-22 14:49:03 +0000 | [diff] [blame] | 18 | |
| 19 | doc->close(); |
| 20 | |
| 21 | REPORTER_ASSERT(reporter, stream.bytesWritten() == 0); |
| 22 | } |
| 23 | |
| 24 | static 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 | |
| 37 | static void test_abortWithFile(skiatest::Reporter* reporter) { |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 38 | SkString tmpDir = skiatest::GetTmpDir(); |
commit-bot@chromium.org | 8c90827 | 2013-10-22 14:49:03 +0000 | [diff] [blame] | 39 | |
| 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 | |
tfarina | a8e2e15 | 2014-07-28 19:26:58 -0700 | [diff] [blame] | 46 | SkString path = SkOSPath::Join(tmpDir.c_str(), "aborted.pdf"); |
commit-bot@chromium.org | 8c90827 | 2013-10-22 14:49:03 +0000 | [diff] [blame] | 47 | |
| 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 | |
| 66 | static void test_file(skiatest::Reporter* reporter) { |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 67 | SkString tmpDir = skiatest::GetTmpDir(); |
commit-bot@chromium.org | 8c90827 | 2013-10-22 14:49:03 +0000 | [diff] [blame] | 68 | 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 | |
tfarina | a8e2e15 | 2014-07-28 19:26:58 -0700 | [diff] [blame] | 74 | SkString path = SkOSPath::Join(tmpDir.c_str(), "file.pdf"); |
commit-bot@chromium.org | 8c90827 | 2013-10-22 14:49:03 +0000 | [diff] [blame] | 75 | |
| 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"); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame^] | 85 | REPORTER_ASSERT(reporter, file != nullptr); |
commit-bot@chromium.org | 8c90827 | 2013-10-22 14:49:03 +0000 | [diff] [blame] | 86 | char header[100]; |
edisonn@google.com | 5237b7f | 2013-10-22 18:33:21 +0000 | [diff] [blame] | 87 | REPORTER_ASSERT(reporter, fread(header, 4, 1, file) != 0); |
commit-bot@chromium.org | 8c90827 | 2013-10-22 14:49:03 +0000 | [diff] [blame] | 88 | REPORTER_ASSERT(reporter, strncmp(header, "%PDF", 4) == 0); |
| 89 | fclose(file); |
| 90 | } |
| 91 | |
| 92 | static 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 | |
| 105 | DEF_TEST(document_tests, reporter) { |
halcanary | 2ccdb63 | 2015-08-11 13:35:12 -0700 | [diff] [blame] | 106 | REQUIRE_PDF_DOCUMENT(document_tests, reporter); |
commit-bot@chromium.org | 8c90827 | 2013-10-22 14:49:03 +0000 | [diff] [blame] | 107 | test_empty(reporter); |
| 108 | test_abort(reporter); |
| 109 | test_abortWithFile(reporter); |
| 110 | test_file(reporter); |
| 111 | test_close(reporter); |
| 112 | } |