halcanary | f12a167 | 2015-09-23 12:45:49 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | #include "SkDocument.h" |
| 8 | #include "SkStream.h" |
| 9 | #include "SkData.h" |
| 10 | #include "Test.h" |
| 11 | |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 12 | DEF_TEST(SkPDF_Metadata, r) { |
| 13 | REQUIRE_PDF_DOCUMENT(SkPDF_Metadata, r); |
halcanary | f12a167 | 2015-09-23 12:45:49 -0700 | [diff] [blame] | 14 | SkTime::DateTime now; |
| 15 | SkTime::GetDateTime(&now); |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 16 | SkDocument::PDFMetadata metadata; |
| 17 | metadata.fTitle = "A1"; |
| 18 | metadata.fAuthor = "A2"; |
| 19 | metadata.fSubject = "A3"; |
| 20 | metadata.fKeywords = "A4"; |
| 21 | metadata.fCreator = "A5"; |
| 22 | metadata.fCreation.fEnabled = true; |
| 23 | metadata.fCreation.fDateTime = now; |
| 24 | metadata.fModified.fEnabled = true; |
| 25 | metadata.fModified.fDateTime = now; |
| 26 | |
| 27 | SkDynamicMemoryWStream pdf; |
| 28 | sk_sp<SkDocument> doc = SkDocument::MakePDF(&pdf, SK_ScalarDefaultRasterDPI, |
| 29 | metadata, nullptr, false); |
halcanary | f12a167 | 2015-09-23 12:45:49 -0700 | [diff] [blame] | 30 | doc->beginPage(612.0f, 792.0f); |
| 31 | doc->close(); |
reed | 42943c8 | 2016-09-12 12:01:44 -0700 | [diff] [blame] | 32 | sk_sp<SkData> data = pdf.detachAsData(); |
halcanary | f12a167 | 2015-09-23 12:45:49 -0700 | [diff] [blame] | 33 | static const char* expectations[] = { |
| 34 | "/Title (A1)", |
| 35 | "/Author (A2)", |
| 36 | "/Subject (A3)", |
| 37 | "/Keywords (A4)", |
| 38 | "/Creator (A5)", |
halcanary | ffe5400 | 2016-03-29 09:09:29 -0700 | [diff] [blame] | 39 | "/Producer (Skia/PDF ", |
halcanary | f12a167 | 2015-09-23 12:45:49 -0700 | [diff] [blame] | 40 | "/CreationDate (D:", |
| 41 | "/ModDate (D:" |
| 42 | }; |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 43 | const uint8_t* bytes = data->bytes(); |
halcanary | f12a167 | 2015-09-23 12:45:49 -0700 | [diff] [blame] | 44 | for (const char* expectation : expectations) { |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 45 | size_t len = strlen(expectation); |
halcanary | f12a167 | 2015-09-23 12:45:49 -0700 | [diff] [blame] | 46 | bool found = false; |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 47 | size_t N = 1 + data->size() - len; |
halcanary | f12a167 | 2015-09-23 12:45:49 -0700 | [diff] [blame] | 48 | for (size_t i = 0; i < N; ++i) { |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 49 | if (0 == memcmp(bytes + i, expectation, len)) { |
halcanary | f12a167 | 2015-09-23 12:45:49 -0700 | [diff] [blame] | 50 | found = true; |
| 51 | break; |
| 52 | } |
| 53 | } |
| 54 | if (!found) { |
| 55 | ERRORF(r, "expectation missing: '%s'.", expectation); |
| 56 | } |
| 57 | } |
| 58 | } |