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; |
Mike Reed | a4daf19 | 2017-12-14 13:25:04 -0500 | [diff] [blame] | 28 | sk_sp<SkDocument> doc = SkDocument::MakePDF(&pdf, metadata); |
halcanary | f12a167 | 2015-09-23 12:45:49 -0700 | [diff] [blame] | 29 | doc->beginPage(612.0f, 792.0f); |
| 30 | doc->close(); |
reed | 42943c8 | 2016-09-12 12:01:44 -0700 | [diff] [blame] | 31 | sk_sp<SkData> data = pdf.detachAsData(); |
halcanary | f12a167 | 2015-09-23 12:45:49 -0700 | [diff] [blame] | 32 | static const char* expectations[] = { |
| 33 | "/Title (A1)", |
| 34 | "/Author (A2)", |
| 35 | "/Subject (A3)", |
| 36 | "/Keywords (A4)", |
| 37 | "/Creator (A5)", |
halcanary | ffe5400 | 2016-03-29 09:09:29 -0700 | [diff] [blame] | 38 | "/Producer (Skia/PDF ", |
halcanary | f12a167 | 2015-09-23 12:45:49 -0700 | [diff] [blame] | 39 | "/CreationDate (D:", |
| 40 | "/ModDate (D:" |
| 41 | }; |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 42 | const uint8_t* bytes = data->bytes(); |
halcanary | f12a167 | 2015-09-23 12:45:49 -0700 | [diff] [blame] | 43 | for (const char* expectation : expectations) { |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 44 | size_t len = strlen(expectation); |
halcanary | f12a167 | 2015-09-23 12:45:49 -0700 | [diff] [blame] | 45 | bool found = false; |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 46 | size_t N = 1 + data->size() - len; |
halcanary | f12a167 | 2015-09-23 12:45:49 -0700 | [diff] [blame] | 47 | for (size_t i = 0; i < N; ++i) { |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 48 | if (0 == memcmp(bytes + i, expectation, len)) { |
halcanary | f12a167 | 2015-09-23 12:45:49 -0700 | [diff] [blame] | 49 | found = true; |
| 50 | break; |
| 51 | } |
| 52 | } |
| 53 | if (!found) { |
| 54 | ERRORF(r, "expectation missing: '%s'.", expectation); |
| 55 | } |
| 56 | } |
| 57 | } |