skia2/tests/PDFMetadataAttributeTest.cpp
halcanary f12a1673f0 SkPDF: add basic metadata support
Motivation: I want too finalize this API before working on the more
complex problem of adding XMP metadata for PDF/A.

BUG=skia:3110

Review URL: https://codereview.chromium.org/1359943003
2015-09-23 12:45:49 -07:00

53 lines
1.6 KiB
C++

/*
* Copyright 2015 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkDocument.h"
#include "SkStream.h"
#include "SkData.h"
#include "Test.h"
DEF_TEST(SkPDF_MetadataAttribute, r) {
REQUIRE_PDF_DOCUMENT(SkPDF_MetadataAttribute, r);
SkDynamicMemoryWStream pdf;
SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(&pdf));
SkTArray<SkDocument::Attribute> info;
info.emplace_back(SkString("Title"), SkString("A1"));
info.emplace_back(SkString("Author"), SkString("A2"));
info.emplace_back(SkString("Subject"), SkString("A3"));
info.emplace_back(SkString("Keywords"), SkString("A4"));
info.emplace_back(SkString("Creator"), SkString("A5"));
SkTime::DateTime now;
SkTime::GetDateTime(&now);
doc->setMetadata(info, &now, &now);
doc->beginPage(612.0f, 792.0f);
doc->close();
SkAutoTUnref<SkData> data(pdf.copyToData());
static const char* expectations[] = {
"/Title (A1)",
"/Author (A2)",
"/Subject (A3)",
"/Keywords (A4)",
"/Creator (A5)",
"/Producer (Skia/PDF)",
"/CreationDate (D:",
"/ModDate (D:"
};
for (const char* expectation : expectations) {
bool found = false;
size_t N = 1 + data->size() - strlen(expectation);
for (size_t i = 0; i < N; ++i) {
if (0 == memcmp(data->bytes() + i,
expectation, strlen(expectation))) {
found = true;
break;
}
}
if (!found) {
ERRORF(r, "expectation missing: '%s'.", expectation);
}
}
}