add tests for SkDocument
authorcommit-bot@chromium.org <commit-bot@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81>
Tue, 22 Oct 2013 14:49:03 +0000 (14:49 +0000)
committercommit-bot@chromium.org <commit-bot@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81>
Tue, 22 Oct 2013 14:49:03 +0000 (14:49 +0000)
R=reed@google.com, vandebo@chromium.org

Author: edisonn@google.com

Review URL: https://codereview.chromium.org/33423002

git-svn-id: http://skia.googlecode.com/svn/trunk@11907 2bbb7eff-a529-9590-31e7-b0007b416f81

gyp/tests.gyp
tests/DocumentTest.cpp [new file with mode: 0644]

index f0dc472c0fa8d7c5ad8ec3ce7fd5dc29eaa861f8..4c61a5c96493f317bae902dcea018223eefb6855 100644 (file)
@@ -48,6 +48,7 @@
         '../tests/DeferredCanvasTest.cpp',
         '../tests/DequeTest.cpp',
         '../tests/DeviceLooperTest.cpp',
+        '../tests/DocumentTest.cpp',
         '../tests/DrawBitmapRectTest.cpp',
         '../tests/DrawPathTest.cpp',
         '../tests/DrawTextTest.cpp',
diff --git a/tests/DocumentTest.cpp b/tests/DocumentTest.cpp
new file mode 100644 (file)
index 0000000..9ca7881
--- /dev/null
@@ -0,0 +1,106 @@
+#include "Test.h"
+#include "TestClassDef.h"
+
+#include "SkCanvas.h"
+#include "SkDocument.h"
+#include "SkOSFile.h"
+#include "SkStream.h"
+
+static void test_empty(skiatest::Reporter* reporter) {
+    SkDynamicMemoryWStream stream;
+
+    SkDocument* doc = SkDocument::CreatePDF(&stream);
+
+    doc->close();
+
+    REPORTER_ASSERT(reporter, stream.bytesWritten() == 0);
+}
+
+static void test_abort(skiatest::Reporter* reporter) {
+    SkDynamicMemoryWStream stream;
+    SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(&stream));
+
+    SkCanvas* canvas = doc->beginPage(100, 100);
+    canvas->drawColor(SK_ColorRED);
+    doc->endPage();
+
+    doc->abort();
+
+    REPORTER_ASSERT(reporter, stream.bytesWritten() == 0);
+}
+
+static void test_abortWithFile(skiatest::Reporter* reporter) {
+    SkString tmpDir = skiatest::Test::GetTmpDir();
+
+    if (tmpDir.isEmpty()) {
+        return;  // TODO(edisonn): unfortunatelly this pattern is used in other
+                 // tests, but if GetTmpDir() starts returning and empty dir
+                 // allways, then all these tests will be disabled.
+    }
+
+    SkString path = SkOSPath::SkPathJoin(tmpDir.c_str(), "aborted.pdf");
+
+    // Make sure doc's destructor is called to flush.
+    {
+        SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(path.c_str()));
+
+        SkCanvas* canvas = doc->beginPage(100, 100);
+        canvas->drawColor(SK_ColorRED);
+        doc->endPage();
+
+        doc->abort();
+    }
+
+    FILE* file = fopen(path.c_str(), "r");
+    // The created file should be empty.
+    char buffer[100];
+    REPORTER_ASSERT(reporter, fread(buffer, 1, 1, file) == 0);
+    fclose(file);
+}
+
+static void test_file(skiatest::Reporter* reporter) {
+    SkString tmpDir = skiatest::Test::GetTmpDir();
+    if (tmpDir.isEmpty()) {
+        return;  // TODO(edisonn): unfortunatelly this pattern is used in other
+                 // tests, but if GetTmpDir() starts returning and empty dir
+                 // allways, then all these tests will be disabled.
+    }
+
+    SkString path = SkOSPath::SkPathJoin(tmpDir.c_str(), "file.pdf");
+
+    SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(path.c_str()));
+
+    SkCanvas* canvas = doc->beginPage(100, 100);
+
+    canvas->drawColor(SK_ColorRED);
+    doc->endPage();
+    doc->close();
+
+    FILE* file = fopen(path.c_str(), "r");
+    REPORTER_ASSERT(reporter, file != NULL);
+    char header[100];
+    fread(header, 4, 1, file);
+    REPORTER_ASSERT(reporter, strncmp(header, "%PDF", 4) == 0);
+    fclose(file);
+}
+
+static void test_close(skiatest::Reporter* reporter) {
+    SkDynamicMemoryWStream stream;
+    SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(&stream));
+
+    SkCanvas* canvas = doc->beginPage(100, 100);
+    canvas->drawColor(SK_ColorRED);
+    doc->endPage();
+
+    doc->close();
+
+    REPORTER_ASSERT(reporter, stream.bytesWritten() != 0);
+}
+
+DEF_TEST(document_tests, reporter) {
+    test_empty(reporter);
+    test_abort(reporter);
+    test_abortWithFile(reporter);
+    test_file(reporter);
+    test_close(reporter);
+}