Index: tests/DocumentTest.cpp |
diff --git a/tests/DocumentTest.cpp b/tests/DocumentTest.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..5e1d1c2f80cf47a2503d28b2b3b9eb83d0bf6954 |
--- /dev/null |
+++ b/tests/DocumentTest.cpp |
@@ -0,0 +1,113 @@ |
+#include "Test.h" |
+#include "TestClassDef.h" |
+ |
+#include "SkCanvas.h" |
+#include "SkDocument.h" |
+#include "SkOSFile.h" |
+#include "SkStream.h" |
+ |
+void test_empty(skiatest::Reporter* reporter) { |
+ SkDynamicMemoryWStream stream; |
+ |
+ SkDocument* doc = SkDocument::CreatePDF(&stream); |
+ |
+ doc->close(); |
+ |
+ REPORTER_ASSERT(reporter, stream.bytesWritten() == 0); |
+} |
+ |
+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); |
+} |
+ |
+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"); |
+ if (file != NULL) { |
+ // If a file is created, it should be at least empty. |
vandebo (ex-Chrome)
2013/10/21 19:02:43
It may be better to define the behavior we desire
edisonn
2013/10/21 19:14:55
Done.
|
+ char buffer[100]; |
+ REPORTER_ASSERT(reporter, fread(buffer, 1, 1, file) == 0); |
+ fclose(file); |
+ } |
+} |
+ |
+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); |
+} |
+ |
+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); |
+} |
+ |
+void test_PdfDpi(skiatest::Reporter* reporter) { |
vandebo (ex-Chrome)
2013/10/21 19:02:43
nit: omit until it has something in it.
edisonn
2013/10/21 19:14:55
Done.
|
+ // TODO(edisonn): will be checked in with a feature that supports DPI. |
+} |
+ |
+DEF_TEST(document_tests, reporter) { |
+ test_empty(reporter); |
+ test_abort(reporter); |
+ test_abortWithFile(reporter); |
+ test_file(reporter); |
+ test_close(reporter); |
+ test_PdfDpi(reporter); |
+} |