commit-bot@chromium.org | 8c90827 | 2013-10-22 14:49:03 +0000 | [diff] [blame] | 1 | #include "Test.h" |
commit-bot@chromium.org | 8c90827 | 2013-10-22 14:49:03 +0000 | [diff] [blame] | 2 | |
| 3 | #include "SkCanvas.h" |
| 4 | #include "SkDocument.h" |
| 5 | #include "SkOSFile.h" |
| 6 | #include "SkStream.h" |
| 7 | |
| 8 | static void test_empty(skiatest::Reporter* reporter) { |
| 9 | SkDynamicMemoryWStream stream; |
| 10 | |
robertphillips@google.com | 701b405 | 2013-11-18 16:26:25 +0000 | [diff] [blame] | 11 | SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(&stream)); |
commit-bot@chromium.org | 8c90827 | 2013-10-22 14:49:03 +0000 | [diff] [blame] | 12 | |
| 13 | doc->close(); |
| 14 | |
| 15 | REPORTER_ASSERT(reporter, stream.bytesWritten() == 0); |
| 16 | } |
| 17 | |
| 18 | static 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 | |
| 31 | static void test_abortWithFile(skiatest::Reporter* reporter) { |
| 32 | SkString tmpDir = skiatest::Test::GetTmpDir(); |
| 33 | |
| 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 | |
tfarina | a8e2e15 | 2014-07-28 19:26:58 -0700 | [diff] [blame] | 40 | SkString path = SkOSPath::Join(tmpDir.c_str(), "aborted.pdf"); |
commit-bot@chromium.org | 8c90827 | 2013-10-22 14:49:03 +0000 | [diff] [blame] | 41 | |
| 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 | |
| 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. |
| 66 | } |
| 67 | |
tfarina | a8e2e15 | 2014-07-28 19:26:58 -0700 | [diff] [blame] | 68 | SkString path = SkOSPath::Join(tmpDir.c_str(), "file.pdf"); |
commit-bot@chromium.org | 8c90827 | 2013-10-22 14:49:03 +0000 | [diff] [blame] | 69 | |
| 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.com | 5237b7f | 2013-10-22 18:33:21 +0000 | [diff] [blame] | 81 | REPORTER_ASSERT(reporter, fread(header, 4, 1, file) != 0); |
commit-bot@chromium.org | 8c90827 | 2013-10-22 14:49:03 +0000 | [diff] [blame] | 82 | REPORTER_ASSERT(reporter, strncmp(header, "%PDF", 4) == 0); |
| 83 | fclose(file); |
| 84 | } |
| 85 | |
| 86 | static 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 | |
| 99 | DEF_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 | } |