1/*
2 * Copyright 2011 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 "SkAnnotation.h"
8#include "SkCanvas.h"
9#include "SkData.h"
10#include "SkDocument.h"
11#include "SkStream.h"
12#include "Test.h"
13
14/** Returns true if data (may contain null characters) contains needle (null
15 *  terminated). */
16static bool ContainsString(const char* data, size_t dataSize, const char* needle) {
17    size_t nSize = strlen(needle);
18    for (size_t i = 0; i < dataSize - nSize; i++) {
19        if (strncmp(&data[i], needle, nSize) == 0) {
20            return true;
21        }
22    }
23    return false;
24}
25
26DEF_TEST(Annotation_NoDraw, reporter) {
27    SkBitmap bm;
28    bm.allocN32Pixels(10, 10);
29    bm.eraseColor(SK_ColorTRANSPARENT);
30
31    SkCanvas canvas(bm);
32    SkRect r = SkRect::MakeWH(SkIntToScalar(10), SkIntToScalar(10));
33
34    sk_sp<SkData> data(SkData::MakeWithCString("http://www.gooogle.com"));
35
36    REPORTER_ASSERT(reporter, 0 == *bm.getAddr32(0, 0));
37    SkAnnotateRectWithURL(&canvas, r, data.get());
38    REPORTER_ASSERT(reporter, 0 == *bm.getAddr32(0, 0));
39}
40
41DEF_TEST(Annotation_PdfLink, reporter) {
42    REQUIRE_PDF_DOCUMENT(Annotation_PdfLink, reporter);
43    SkDynamicMemoryWStream outStream;
44    sk_sp<SkDocument> doc(SkDocument::MakePDF(&outStream));
45    SkCanvas* canvas = doc->beginPage(612.0f, 792.0f);
46    REPORTER_ASSERT(reporter, canvas);
47
48    SkRect r = SkRect::MakeXYWH(SkIntToScalar(72), SkIntToScalar(72),
49                                SkIntToScalar(288), SkIntToScalar(72));
50    sk_sp<SkData> data(SkData::MakeWithCString("http://www.gooogle.com"));
51    SkAnnotateRectWithURL(canvas, r, data.get());
52
53    doc->close();
54    sk_sp<SkData> out = outStream.detachAsData();
55    const char* rawOutput = (const char*)out->data();
56
57    REPORTER_ASSERT(reporter, ContainsString(rawOutput, out->size(), "/Annots "));
58}
59
60DEF_TEST(Annotation_NamedDestination, reporter) {
61    REQUIRE_PDF_DOCUMENT(Annotation_NamedDestination, reporter);
62    SkDynamicMemoryWStream outStream;
63    sk_sp<SkDocument> doc(SkDocument::MakePDF(&outStream));
64    SkCanvas* canvas = doc->beginPage(612.0f, 792.0f);
65    REPORTER_ASSERT(reporter, canvas);
66
67    SkPoint p = SkPoint::Make(SkIntToScalar(72), SkIntToScalar(72));
68    sk_sp<SkData> data(SkData::MakeWithCString("example"));
69    SkAnnotateNamedDestination(canvas, p, data.get());
70
71    doc->close();
72    sk_sp<SkData> out = outStream.detachAsData();
73    const char* rawOutput = (const char*)out->data();
74
75    REPORTER_ASSERT(reporter,
76        ContainsString(rawOutput, out->size(), "/example "));
77}
78