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" |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 11 | #include "SkDocument.h" |
| 12 | #include "SkStream.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 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 42 | DEF_TEST(Annotation_PdfLink, reporter) { |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 43 | SkDynamicMemoryWStream outStream; |
| 44 | SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(&outStream)); |
| 45 | SkCanvas* canvas = doc->beginPage(612.0f, 792.0f); |
| 46 | REPORTER_ASSERT(reporter, canvas); |
vandebo@chromium.org | 238be8c | 2012-07-13 20:06:02 +0000 | [diff] [blame] | 47 | |
| 48 | SkRect r = SkRect::MakeXYWH(SkIntToScalar(72), SkIntToScalar(72), |
| 49 | SkIntToScalar(288), SkIntToScalar(72)); |
| 50 | SkAutoDataUnref data(SkData::NewWithCString("http://www.gooogle.com")); |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 51 | SkAnnotateRectWithURL(canvas, r, data.get()); |
vandebo@chromium.org | 238be8c | 2012-07-13 20:06:02 +0000 | [diff] [blame] | 52 | |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 53 | REPORTER_ASSERT(reporter, doc->close()); |
mtklein | cabc08c | 2015-02-19 08:29:24 -0800 | [diff] [blame] | 54 | SkAutoDataUnref out(outStream.copyToData()); |
| 55 | const char* rawOutput = (const char*)out->data(); |
vandebo@chromium.org | 238be8c | 2012-07-13 20:06:02 +0000 | [diff] [blame] | 56 | |
mtklein | cabc08c | 2015-02-19 08:29:24 -0800 | [diff] [blame] | 57 | REPORTER_ASSERT(reporter, ContainsString(rawOutput, out->size(), "/Annots ")); |
vandebo@chromium.org | 238be8c | 2012-07-13 20:06:02 +0000 | [diff] [blame] | 58 | } |
| 59 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 60 | DEF_TEST(Annotation_NamedDestination, reporter) { |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 61 | SkDynamicMemoryWStream outStream; |
| 62 | SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(&outStream)); |
| 63 | SkCanvas* canvas = doc->beginPage(612.0f, 792.0f); |
| 64 | REPORTER_ASSERT(reporter, canvas); |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 65 | |
| 66 | SkPoint p = SkPoint::Make(SkIntToScalar(72), SkIntToScalar(72)); |
| 67 | SkAutoDataUnref data(SkData::NewWithCString("example")); |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 68 | SkAnnotateNamedDestination(canvas, p, data.get()); |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 69 | |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 70 | REPORTER_ASSERT(reporter, doc->close()); |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 71 | SkAutoDataUnref out(outStream.copyToData()); |
| 72 | const char* rawOutput = (const char*)out->data(); |
| 73 | |
| 74 | REPORTER_ASSERT(reporter, |
| 75 | ContainsString(rawOutput, out->size(), "/example ")); |
| 76 | } |