Home | History | Annotate | Download | only in tests
      1 #include "Test.h"
      2 #include "TestClassDef.h"
      3 
      4 #include "SkCanvas.h"
      5 #include "SkDocument.h"
      6 #include "SkOSFile.h"
      7 #include "SkStream.h"
      8 
      9 static void test_empty(skiatest::Reporter* reporter) {
     10     SkDynamicMemoryWStream stream;
     11 
     12     SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(&stream));
     13 
     14     doc->close();
     15 
     16     REPORTER_ASSERT(reporter, stream.bytesWritten() == 0);
     17 }
     18 
     19 static void test_abort(skiatest::Reporter* reporter) {
     20     SkDynamicMemoryWStream stream;
     21     SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(&stream));
     22 
     23     SkCanvas* canvas = doc->beginPage(100, 100);
     24     canvas->drawColor(SK_ColorRED);
     25     doc->endPage();
     26 
     27     doc->abort();
     28 
     29     REPORTER_ASSERT(reporter, stream.bytesWritten() == 0);
     30 }
     31 
     32 static void test_abortWithFile(skiatest::Reporter* reporter) {
     33     SkString tmpDir = skiatest::Test::GetTmpDir();
     34 
     35     if (tmpDir.isEmpty()) {
     36         return;  // TODO(edisonn): unfortunatelly this pattern is used in other
     37                  // tests, but if GetTmpDir() starts returning and empty dir
     38                  // allways, then all these tests will be disabled.
     39     }
     40 
     41     SkString path = SkOSPath::SkPathJoin(tmpDir.c_str(), "aborted.pdf");
     42 
     43     // Make sure doc's destructor is called to flush.
     44     {
     45         SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(path.c_str()));
     46 
     47         SkCanvas* canvas = doc->beginPage(100, 100);
     48         canvas->drawColor(SK_ColorRED);
     49         doc->endPage();
     50 
     51         doc->abort();
     52     }
     53 
     54     FILE* file = fopen(path.c_str(), "r");
     55     // The created file should be empty.
     56     char buffer[100];
     57     REPORTER_ASSERT(reporter, fread(buffer, 1, 1, file) == 0);
     58     fclose(file);
     59 }
     60 
     61 static void test_file(skiatest::Reporter* reporter) {
     62     SkString tmpDir = skiatest::Test::GetTmpDir();
     63     if (tmpDir.isEmpty()) {
     64         return;  // TODO(edisonn): unfortunatelly this pattern is used in other
     65                  // tests, but if GetTmpDir() starts returning and empty dir
     66                  // allways, then all these tests will be disabled.
     67     }
     68 
     69     SkString path = SkOSPath::SkPathJoin(tmpDir.c_str(), "file.pdf");
     70 
     71     SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(path.c_str()));
     72 
     73     SkCanvas* canvas = doc->beginPage(100, 100);
     74 
     75     canvas->drawColor(SK_ColorRED);
     76     doc->endPage();
     77     doc->close();
     78 
     79     FILE* file = fopen(path.c_str(), "r");
     80     REPORTER_ASSERT(reporter, file != NULL);
     81     char header[100];
     82     REPORTER_ASSERT(reporter, fread(header, 4, 1, file) != 0);
     83     REPORTER_ASSERT(reporter, strncmp(header, "%PDF", 4) == 0);
     84     fclose(file);
     85 }
     86 
     87 static void test_close(skiatest::Reporter* reporter) {
     88     SkDynamicMemoryWStream stream;
     89     SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(&stream));
     90 
     91     SkCanvas* canvas = doc->beginPage(100, 100);
     92     canvas->drawColor(SK_ColorRED);
     93     doc->endPage();
     94 
     95     doc->close();
     96 
     97     REPORTER_ASSERT(reporter, stream.bytesWritten() != 0);
     98 }
     99 
    100 DEF_TEST(document_tests, reporter) {
    101     test_empty(reporter);
    102     test_abort(reporter);
    103     test_abortWithFile(reporter);
    104     test_file(reporter);
    105     test_close(reporter);
    106 }
    107