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 | */ |
halcanary | f12a167 | 2015-09-23 12:45:49 -0700 | [diff] [blame] | 7 | #include "SkData.h" |
Hal Canary | 23564b9 | 2018-09-07 14:33:14 -0400 | [diff] [blame] | 8 | #include "SkPDFDocument.h" |
| 9 | #include "SkStream.h" |
halcanary | f12a167 | 2015-09-23 12:45:49 -0700 | [diff] [blame] | 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); |
Hal Canary | 23564b9 | 2018-09-07 14:33:14 -0400 | [diff] [blame] | 16 | SkPDF::Metadata metadata; |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 17 | metadata.fTitle = "A1"; |
| 18 | metadata.fAuthor = "A2"; |
| 19 | metadata.fSubject = "A3"; |
| 20 | metadata.fKeywords = "A4"; |
| 21 | metadata.fCreator = "A5"; |
Hal Canary | 23564b9 | 2018-09-07 14:33:14 -0400 | [diff] [blame] | 22 | metadata.fCreation = now; |
| 23 | metadata.fModified = now; |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 24 | |
| 25 | SkDynamicMemoryWStream pdf; |
Hal Canary | 3026d4b | 2019-01-07 10:00:48 -0500 | [diff] [blame^] | 26 | auto doc = SkPDF::MakeDocument(&pdf, metadata); |
halcanary | f12a167 | 2015-09-23 12:45:49 -0700 | [diff] [blame] | 27 | doc->beginPage(612.0f, 792.0f); |
| 28 | doc->close(); |
reed | 42943c8 | 2016-09-12 12:01:44 -0700 | [diff] [blame] | 29 | sk_sp<SkData> data = pdf.detachAsData(); |
halcanary | f12a167 | 2015-09-23 12:45:49 -0700 | [diff] [blame] | 30 | static const char* expectations[] = { |
| 31 | "/Title (A1)", |
| 32 | "/Author (A2)", |
| 33 | "/Subject (A3)", |
| 34 | "/Keywords (A4)", |
| 35 | "/Creator (A5)", |
halcanary | ffe5400 | 2016-03-29 09:09:29 -0700 | [diff] [blame] | 36 | "/Producer (Skia/PDF ", |
halcanary | f12a167 | 2015-09-23 12:45:49 -0700 | [diff] [blame] | 37 | "/CreationDate (D:", |
| 38 | "/ModDate (D:" |
| 39 | }; |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 40 | const uint8_t* bytes = data->bytes(); |
halcanary | f12a167 | 2015-09-23 12:45:49 -0700 | [diff] [blame] | 41 | for (const char* expectation : expectations) { |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 42 | size_t len = strlen(expectation); |
halcanary | f12a167 | 2015-09-23 12:45:49 -0700 | [diff] [blame] | 43 | bool found = false; |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 44 | size_t N = 1 + data->size() - len; |
halcanary | f12a167 | 2015-09-23 12:45:49 -0700 | [diff] [blame] | 45 | for (size_t i = 0; i < N; ++i) { |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 46 | if (0 == memcmp(bytes + i, expectation, len)) { |
halcanary | f12a167 | 2015-09-23 12:45:49 -0700 | [diff] [blame] | 47 | found = true; |
| 48 | break; |
| 49 | } |
| 50 | } |
| 51 | if (!found) { |
| 52 | ERRORF(r, "expectation missing: '%s'.", expectation); |
| 53 | } |
| 54 | } |
| 55 | } |