xref: /aosp_15_r20/external/skia/tests/PDFMetadataAttributeTest.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 "include/core/SkTypes.h"
8 
9 #ifdef SK_SUPPORT_PDF
10 #include "include/core/SkData.h"
11 #include "include/core/SkDocument.h"
12 #include "include/core/SkRefCnt.h"
13 #include "include/core/SkStream.h"
14 #include "include/core/SkString.h"
15 #include "include/docs/SkPDFDocument.h"
16 #include "src/pdf/SkPDFUtils.h"
17 #include "tests/Test.h"
18 
19 #include <cstdint>
20 #include <cstring>
21 
DEF_TEST(SkPDF_Metadata,r)22 DEF_TEST(SkPDF_Metadata, r) {
23     REQUIRE_PDF_DOCUMENT(SkPDF_Metadata, r);
24     SkPDF::DateTime now;
25     SkPDFUtils::GetDateTime(&now);
26     SkPDF::Metadata metadata;
27     metadata.fTitle = "A1";
28     metadata.fAuthor = "A2";
29     metadata.fSubject = "A3";
30     metadata.fKeywords = "A4";
31     metadata.fCreator = "A5";
32     metadata.fCreation = now;
33     metadata.fModified = now;
34 
35     SkDynamicMemoryWStream pdf;
36     auto doc = SkPDF::MakeDocument(&pdf, metadata);
37     doc->beginPage(612.0f, 792.0f);
38     doc->close();
39     sk_sp<SkData> data = pdf.detachAsData();
40     static const char* expectations[] = {
41         "/Title (A1)",
42         "/Author (A2)",
43         "/Subject (A3)",
44         "/Keywords (A4)",
45         "/Creator (A5)",
46         "/Producer (Skia/PDF ",
47         "/CreationDate (D:",
48         "/ModDate (D:"
49     };
50     const uint8_t* bytes = data->bytes();
51     for (const char* expectation : expectations) {
52         size_t len = strlen(expectation);
53         bool found = false;
54         size_t N = 1 + data->size() - len;
55         for (size_t i = 0; i < N; ++i) {
56             if (0 == memcmp(bytes + i, expectation, len)) {
57                 found = true;
58                 break;
59             }
60         }
61         if (!found) {
62             ERRORF(r, "expectation missing: '%s'.", expectation);
63         }
64     }
65 }
66 #endif
67