1
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8#include "Test.h"
9#include "TestClassDef.h"
10#include "SkAnnotation.h"
11#include "SkData.h"
12#include "SkCanvas.h"
13#include "SkPDFDevice.h"
14#include "SkPDFDocument.h"
15
16/** Returns true if data (may contain null characters) contains needle (null
17 *  terminated). */
18static bool ContainsString(const char* data, size_t dataSize, const char* needle) {
19    size_t nSize = strlen(needle);
20    for (size_t i = 0; i < dataSize - nSize; i++) {
21        if (strncmp(&data[i], needle, nSize) == 0) {
22            return true;
23        }
24    }
25    return false;
26}
27
28DEF_TEST(Annotation_NoDraw, reporter) {
29    SkBitmap bm;
30    bm.setConfig(SkBitmap::kARGB_8888_Config, 10, 10);
31    bm.allocPixels();
32    bm.eraseColor(SK_ColorTRANSPARENT);
33
34    SkCanvas canvas(bm);
35    SkRect r = SkRect::MakeWH(SkIntToScalar(10), SkIntToScalar(10));
36
37    SkAutoDataUnref data(SkData::NewWithCString("http://www.gooogle.com"));
38
39    REPORTER_ASSERT(reporter, 0 == *bm.getAddr32(0, 0));
40    SkAnnotateRectWithURL(&canvas, r, data.get());
41    REPORTER_ASSERT(reporter, 0 == *bm.getAddr32(0, 0));
42}
43
44struct testCase {
45    SkPDFDocument::Flags flags;
46    bool expectAnnotations;
47};
48
49DEF_TEST(Annotation_PdfLink, reporter) {
50    SkISize size = SkISize::Make(612, 792);
51    SkMatrix initialTransform;
52    initialTransform.reset();
53    SkPDFDevice device(size, size, initialTransform);
54    SkCanvas canvas(&device);
55
56    SkRect r = SkRect::MakeXYWH(SkIntToScalar(72), SkIntToScalar(72),
57                                SkIntToScalar(288), SkIntToScalar(72));
58    SkAutoDataUnref data(SkData::NewWithCString("http://www.gooogle.com"));
59    SkAnnotateRectWithURL(&canvas, r, data.get());
60
61    testCase tests[] = {{(SkPDFDocument::Flags)0, true},
62                        {SkPDFDocument::kNoLinks_Flags, false}};
63    for (size_t testNum = 0; testNum < SK_ARRAY_COUNT(tests); testNum++) {
64        SkPDFDocument doc(tests[testNum].flags);
65        doc.appendPage(&device);
66        SkDynamicMemoryWStream outStream;
67        doc.emitPDF(&outStream);
68        SkAutoDataUnref out(outStream.copyToData());
69        const char* rawOutput = (const char*)out->data();
70
71        REPORTER_ASSERT(reporter,
72            ContainsString(rawOutput, out->size(), "/Annots ")
73            == tests[testNum].expectAnnotations);
74    }
75}
76
77DEF_TEST(Annotation_NamedDestination, reporter) {
78    SkISize size = SkISize::Make(612, 792);
79    SkMatrix initialTransform;
80    initialTransform.reset();
81    SkPDFDevice device(size, size, initialTransform);
82    SkCanvas canvas(&device);
83
84    SkPoint p = SkPoint::Make(SkIntToScalar(72), SkIntToScalar(72));
85    SkAutoDataUnref data(SkData::NewWithCString("example"));
86    SkAnnotateNamedDestination(&canvas, p, data.get());
87
88    SkPDFDocument doc;
89    doc.appendPage(&device);
90    SkDynamicMemoryWStream outStream;
91    doc.emitPDF(&outStream);
92    SkAutoDataUnref out(outStream.copyToData());
93    const char* rawOutput = (const char*)out->data();
94
95    REPORTER_ASSERT(reporter,
96        ContainsString(rawOutput, out->size(), "/example "));
97}
98