reed@google.com | b0a34d8 | 2012-07-11 19:57:55 +0000 | [diff] [blame] | 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 | */ |
reed@google.com | b0a34d8 | 2012-07-11 19:57:55 +0000 | [diff] [blame] | 7 | #include "SkAnnotation.h" |
reed@google.com | b0a34d8 | 2012-07-11 19:57:55 +0000 | [diff] [blame] | 8 | #include "SkCanvas.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 9 | #include "SkData.h" |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 10 | #include "SkDocument.h" |
| 11 | #include "SkStream.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 12 | #include "Test.h" |
reed@google.com | b0a34d8 | 2012-07-11 19:57:55 +0000 | [diff] [blame] | 13 | |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 14 | /** Returns true if data (may contain null characters) contains needle (null |
| 15 | * terminated). */ |
| 16 | static 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 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 26 | DEF_TEST(Annotation_NoDraw, reporter) { |
reed@google.com | b0a34d8 | 2012-07-11 19:57:55 +0000 | [diff] [blame] | 27 | SkBitmap bm; |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 28 | bm.allocN32Pixels(10, 10); |
junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 29 | bm.eraseColor(SK_ColorTRANSPARENT); |
reed@google.com | b0a34d8 | 2012-07-11 19:57:55 +0000 | [diff] [blame] | 30 | |
| 31 | SkCanvas canvas(bm); |
| 32 | SkRect r = SkRect::MakeWH(SkIntToScalar(10), SkIntToScalar(10)); |
| 33 | |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 34 | sk_sp<SkData> data(SkData::MakeWithCString("http://www.gooogle.com")); |
reed@google.com | b0a34d8 | 2012-07-11 19:57:55 +0000 | [diff] [blame] | 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 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 41 | DEF_TEST(Annotation_PdfLink, reporter) { |
halcanary | 2ccdb63 | 2015-08-11 13:35:12 -0700 | [diff] [blame] | 42 | REQUIRE_PDF_DOCUMENT(Annotation_PdfLink, reporter); |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 43 | SkDynamicMemoryWStream outStream; |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 44 | sk_sp<SkDocument> doc(SkDocument::MakePDF(&outStream)); |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 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)); |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 50 | sk_sp<SkData> data(SkData::MakeWithCString("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 | |
reed | d14df7c | 2016-09-22 14:12:46 -0700 | [diff] [blame] | 53 | doc->close(); |
reed | 42943c8 | 2016-09-12 12:01:44 -0700 | [diff] [blame] | 54 | sk_sp<SkData> out = outStream.detachAsData(); |
mtklein | cabc08c | 2015-02-19 08:29:24 -0800 | [diff] [blame] | 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 | 2ccdb63 | 2015-08-11 13:35:12 -0700 | [diff] [blame] | 61 | REQUIRE_PDF_DOCUMENT(Annotation_NamedDestination, reporter); |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 62 | SkDynamicMemoryWStream outStream; |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 63 | sk_sp<SkDocument> doc(SkDocument::MakePDF(&outStream)); |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 64 | SkCanvas* canvas = doc->beginPage(612.0f, 792.0f); |
| 65 | REPORTER_ASSERT(reporter, canvas); |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 66 | |
| 67 | SkPoint p = SkPoint::Make(SkIntToScalar(72), SkIntToScalar(72)); |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 68 | sk_sp<SkData> data(SkData::MakeWithCString("example")); |
halcanary | 3d32d50 | 2015-03-01 06:55:20 -0800 | [diff] [blame] | 69 | SkAnnotateNamedDestination(canvas, p, data.get()); |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 70 | |
reed | d14df7c | 2016-09-22 14:12:46 -0700 | [diff] [blame] | 71 | doc->close(); |
reed | 42943c8 | 2016-09-12 12:01:44 -0700 | [diff] [blame] | 72 | sk_sp<SkData> out = outStream.detachAsData(); |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 73 | const char* rawOutput = (const char*)out->data(); |
| 74 | |
| 75 | REPORTER_ASSERT(reporter, |
| 76 | ContainsString(rawOutput, out->size(), "/example ")); |
| 77 | } |