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 "SkAnnotation.h"
10 #include "SkData.h"
11 #include "SkCanvas.h"
12 #include "SkPDFDevice.h"
13 #include "SkPDFDocument.h"
14
test_nodraw(skiatest::Reporter * reporter)15 static void test_nodraw(skiatest::Reporter* reporter) {
16 SkBitmap bm;
17 bm.setConfig(SkBitmap::kARGB_8888_Config, 10, 10);
18 bm.allocPixels();
19 bm.eraseColor(SK_ColorTRANSPARENT);
20
21 SkCanvas canvas(bm);
22 SkRect r = SkRect::MakeWH(SkIntToScalar(10), SkIntToScalar(10));
23
24 SkAutoDataUnref data(SkData::NewWithCString("http://www.gooogle.com"));
25
26 REPORTER_ASSERT(reporter, 0 == *bm.getAddr32(0, 0));
27 SkAnnotateRectWithURL(&canvas, r, data.get());
28 REPORTER_ASSERT(reporter, 0 == *bm.getAddr32(0, 0));
29 }
30
31 struct testCase {
32 SkPDFDocument::Flags flags;
33 bool expectAnnotations;
34 };
35
test_pdf_link_annotations(skiatest::Reporter * reporter)36 static void test_pdf_link_annotations(skiatest::Reporter* reporter) {
37 SkISize size = SkISize::Make(612, 792);
38 SkMatrix initialTransform;
39 initialTransform.reset();
40 SkPDFDevice device(size, size, initialTransform);
41 SkCanvas canvas(&device);
42
43 SkRect r = SkRect::MakeXYWH(SkIntToScalar(72), SkIntToScalar(72),
44 SkIntToScalar(288), SkIntToScalar(72));
45 SkAutoDataUnref data(SkData::NewWithCString("http://www.gooogle.com"));
46 SkAnnotateRectWithURL(&canvas, r, data.get());
47
48 testCase tests[] = {{(SkPDFDocument::Flags)0, true},
49 {SkPDFDocument::kNoLinks_Flags, false}};
50 for (size_t testNum = 0; testNum < SK_ARRAY_COUNT(tests); testNum++) {
51 SkPDFDocument doc(tests[testNum].flags);
52 doc.appendPage(&device);
53 SkDynamicMemoryWStream outStream;
54 doc.emitPDF(&outStream);
55 SkAutoDataUnref out(outStream.copyToData());
56 const char* rawOutput = (const char*)out->data();
57
58 bool found = false;
59 for (size_t i = 0; i < out->size() - 8; i++) {
60 if (rawOutput[i + 0] == '/' &&
61 rawOutput[i + 1] == 'A' &&
62 rawOutput[i + 2] == 'n' &&
63 rawOutput[i + 3] == 'n' &&
64 rawOutput[i + 4] == 'o' &&
65 rawOutput[i + 5] == 't' &&
66 rawOutput[i + 6] == 's' &&
67 rawOutput[i + 7] == ' ') {
68 found = true;
69 break;
70 }
71 }
72 REPORTER_ASSERT(reporter, found == tests[testNum].expectAnnotations);
73 }
74 }
75
TestAnnotation(skiatest::Reporter * reporter)76 static void TestAnnotation(skiatest::Reporter* reporter) {
77 test_nodraw(reporter);
78 test_pdf_link_annotations(reporter);
79 }
80
81 #include "TestClassDef.h"
82 DEFINE_TESTCLASS("Annotation", AnnotationClass, TestAnnotation)
83