reed@google.com | b0a34d8 | 2012-07-11 19:57:55 +0000 | [diff] [blame] | 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 | */ |
reed@google.com | b0a34d8 | 2012-07-11 19:57:55 +0000 | [diff] [blame] | 8 | #include "SkAnnotation.h" |
reed@google.com | b0a34d8 | 2012-07-11 19:57:55 +0000 | [diff] [blame] | 9 | #include "SkCanvas.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 10 | #include "SkData.h" |
vandebo@chromium.org | 238be8c | 2012-07-13 20:06:02 +0000 | [diff] [blame] | 11 | #include "SkPDFDevice.h" |
| 12 | #include "SkPDFDocument.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 13 | #include "Test.h" |
reed@google.com | b0a34d8 | 2012-07-11 19:57:55 +0000 | [diff] [blame] | 14 | |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 15 | /** Returns true if data (may contain null characters) contains needle (null |
| 16 | * terminated). */ |
| 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 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 27 | DEF_TEST(Annotation_NoDraw, reporter) { |
reed@google.com | b0a34d8 | 2012-07-11 19:57:55 +0000 | [diff] [blame] | 28 | SkBitmap bm; |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 29 | bm.allocN32Pixels(10, 10); |
junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 30 | bm.eraseColor(SK_ColorTRANSPARENT); |
reed@google.com | b0a34d8 | 2012-07-11 19:57:55 +0000 | [diff] [blame] | 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 | |
vandebo@chromium.org | 238be8c | 2012-07-13 20:06:02 +0000 | [diff] [blame] | 42 | struct testCase { |
| 43 | SkPDFDocument::Flags flags; |
| 44 | bool expectAnnotations; |
| 45 | }; |
| 46 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 47 | DEF_TEST(Annotation_PdfLink, reporter) { |
vandebo@chromium.org | 238be8c | 2012-07-13 20:06:02 +0000 | [diff] [blame] | 48 | SkISize size = SkISize::Make(612, 792); |
| 49 | SkMatrix initialTransform; |
| 50 | initialTransform.reset(); |
| 51 | SkPDFDevice device(size, size, initialTransform); |
| 52 | SkCanvas canvas(&device); |
| 53 | |
| 54 | SkRect r = SkRect::MakeXYWH(SkIntToScalar(72), SkIntToScalar(72), |
| 55 | SkIntToScalar(288), SkIntToScalar(72)); |
| 56 | SkAutoDataUnref data(SkData::NewWithCString("http://www.gooogle.com")); |
| 57 | SkAnnotateRectWithURL(&canvas, r, data.get()); |
| 58 | |
| 59 | testCase tests[] = {{(SkPDFDocument::Flags)0, true}, |
| 60 | {SkPDFDocument::kNoLinks_Flags, false}}; |
| 61 | for (size_t testNum = 0; testNum < SK_ARRAY_COUNT(tests); testNum++) { |
| 62 | SkPDFDocument doc(tests[testNum].flags); |
| 63 | doc.appendPage(&device); |
| 64 | SkDynamicMemoryWStream outStream; |
| 65 | doc.emitPDF(&outStream); |
| 66 | SkAutoDataUnref out(outStream.copyToData()); |
| 67 | const char* rawOutput = (const char*)out->data(); |
| 68 | |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 69 | REPORTER_ASSERT(reporter, |
| 70 | ContainsString(rawOutput, out->size(), "/Annots ") |
| 71 | == tests[testNum].expectAnnotations); |
vandebo@chromium.org | 238be8c | 2012-07-13 20:06:02 +0000 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 75 | DEF_TEST(Annotation_NamedDestination, reporter) { |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 76 | SkISize size = SkISize::Make(612, 792); |
| 77 | SkMatrix initialTransform; |
| 78 | initialTransform.reset(); |
| 79 | SkPDFDevice device(size, size, initialTransform); |
| 80 | SkCanvas canvas(&device); |
| 81 | |
| 82 | SkPoint p = SkPoint::Make(SkIntToScalar(72), SkIntToScalar(72)); |
| 83 | SkAutoDataUnref data(SkData::NewWithCString("example")); |
| 84 | SkAnnotateNamedDestination(&canvas, p, data.get()); |
| 85 | |
| 86 | SkPDFDocument doc; |
| 87 | doc.appendPage(&device); |
| 88 | SkDynamicMemoryWStream outStream; |
| 89 | doc.emitPDF(&outStream); |
| 90 | SkAutoDataUnref out(outStream.copyToData()); |
| 91 | const char* rawOutput = (const char*)out->data(); |
| 92 | |
| 93 | REPORTER_ASSERT(reporter, |
| 94 | ContainsString(rawOutput, out->size(), "/example ")); |
| 95 | } |