18c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org#include "Test.h"
28c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org
38c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org#include "SkCanvas.h"
48c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org#include "SkDocument.h"
58c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org#include "SkOSFile.h"
68c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org#include "SkStream.h"
78c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org
88c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.orgstatic void test_empty(skiatest::Reporter* reporter) {
98c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org    SkDynamicMemoryWStream stream;
108c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org
11701b40543d5d124dfa1e59b051cba9d2aaf61670robertphillips@google.com    SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(&stream));
128c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org
138c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org    doc->close();
148c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org
158c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org    REPORTER_ASSERT(reporter, stream.bytesWritten() == 0);
168c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org}
178c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org
188c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.orgstatic void test_abort(skiatest::Reporter* reporter) {
198c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org    SkDynamicMemoryWStream stream;
208c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org    SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(&stream));
218c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org
228c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org    SkCanvas* canvas = doc->beginPage(100, 100);
238c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org    canvas->drawColor(SK_ColorRED);
248c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org    doc->endPage();
258c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org
268c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org    doc->abort();
278c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org
288c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org    REPORTER_ASSERT(reporter, stream.bytesWritten() == 0);
298c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org}
308c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org
318c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.orgstatic void test_abortWithFile(skiatest::Reporter* reporter) {
328c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org    SkString tmpDir = skiatest::Test::GetTmpDir();
338c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org
348c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org    if (tmpDir.isEmpty()) {
358c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org        return;  // TODO(edisonn): unfortunatelly this pattern is used in other
368c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org                 // tests, but if GetTmpDir() starts returning and empty dir
378c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org                 // allways, then all these tests will be disabled.
388c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org    }
398c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org
40a8e2e1504b9af6ba791637f228debaa23953064atfarina    SkString path = SkOSPath::Join(tmpDir.c_str(), "aborted.pdf");
418c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org
428c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org    // Make sure doc's destructor is called to flush.
438c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org    {
448c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org        SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(path.c_str()));
458c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org
468c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org        SkCanvas* canvas = doc->beginPage(100, 100);
478c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org        canvas->drawColor(SK_ColorRED);
488c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org        doc->endPage();
498c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org
508c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org        doc->abort();
518c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org    }
528c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org
538c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org    FILE* file = fopen(path.c_str(), "r");
548c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org    // The created file should be empty.
558c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org    char buffer[100];
568c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org    REPORTER_ASSERT(reporter, fread(buffer, 1, 1, file) == 0);
578c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org    fclose(file);
588c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org}
598c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org
608c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.orgstatic void test_file(skiatest::Reporter* reporter) {
618c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org    SkString tmpDir = skiatest::Test::GetTmpDir();
628c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org    if (tmpDir.isEmpty()) {
638c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org        return;  // TODO(edisonn): unfortunatelly this pattern is used in other
648c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org                 // tests, but if GetTmpDir() starts returning and empty dir
658c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org                 // allways, then all these tests will be disabled.
668c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org    }
678c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org
68a8e2e1504b9af6ba791637f228debaa23953064atfarina    SkString path = SkOSPath::Join(tmpDir.c_str(), "file.pdf");
698c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org
708c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org    SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(path.c_str()));
718c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org
728c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org    SkCanvas* canvas = doc->beginPage(100, 100);
738c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org
748c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org    canvas->drawColor(SK_ColorRED);
758c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org    doc->endPage();
768c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org    doc->close();
778c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org
788c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org    FILE* file = fopen(path.c_str(), "r");
798c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org    REPORTER_ASSERT(reporter, file != NULL);
808c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org    char header[100];
815237b7fb0e3535d284ca18849d532f298a5e85ededisonn@google.com    REPORTER_ASSERT(reporter, fread(header, 4, 1, file) != 0);
828c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org    REPORTER_ASSERT(reporter, strncmp(header, "%PDF", 4) == 0);
838c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org    fclose(file);
848c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org}
858c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org
868c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.orgstatic void test_close(skiatest::Reporter* reporter) {
878c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org    SkDynamicMemoryWStream stream;
888c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org    SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(&stream));
898c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org
908c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org    SkCanvas* canvas = doc->beginPage(100, 100);
918c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org    canvas->drawColor(SK_ColorRED);
928c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org    doc->endPage();
938c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org
948c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org    doc->close();
958c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org
968c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org    REPORTER_ASSERT(reporter, stream.bytesWritten() != 0);
978c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org}
988c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org
998c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.orgDEF_TEST(document_tests, reporter) {
1008c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org    test_empty(reporter);
1018c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org    test_abort(reporter);
1028c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org    test_abortWithFile(reporter);
1038c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org    test_file(reporter);
1048c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org    test_close(reporter);
1058c908273bb21e3f4141c86312f8a3ef5f90ebe9fcommit-bot@chromium.org}
106