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