blob: 7a1ca97a5543471bdef74c97541c250fb3ec7c45 [file] [log] [blame]
reed@google.comb0a34d82012-07-11 19:57:55 +00001
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.comb0a34d82012-07-11 19:57:55 +00008#include "SkAnnotation.h"
reed@google.comb0a34d82012-07-11 19:57:55 +00009#include "SkCanvas.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000010#include "SkData.h"
halcanary3d32d502015-03-01 06:55:20 -080011#include "SkDocument.h"
12#include "SkStream.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000013#include "Test.h"
reed@google.comb0a34d82012-07-11 19:57:55 +000014
epoger@google.comb58772f2013-03-08 09:09:10 +000015/** Returns true if data (may contain null characters) contains needle (null
16 * terminated). */
17static 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.orge4fafb12013-12-12 21:11:12 +000027DEF_TEST(Annotation_NoDraw, reporter) {
reed@google.comb0a34d82012-07-11 19:57:55 +000028 SkBitmap bm;
commit-bot@chromium.orgfa9e5fa2014-02-13 22:00:04 +000029 bm.allocN32Pixels(10, 10);
junov@google.comdbfac8a2012-12-06 21:47:40 +000030 bm.eraseColor(SK_ColorTRANSPARENT);
reed@google.comb0a34d82012-07-11 19:57:55 +000031
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.orge4fafb12013-12-12 21:11:12 +000042DEF_TEST(Annotation_PdfLink, reporter) {
halcanary3d32d502015-03-01 06:55:20 -080043 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.org238be8c2012-07-13 20:06:02 +000047
48 SkRect r = SkRect::MakeXYWH(SkIntToScalar(72), SkIntToScalar(72),
49 SkIntToScalar(288), SkIntToScalar(72));
50 SkAutoDataUnref data(SkData::NewWithCString("http://www.gooogle.com"));
halcanary3d32d502015-03-01 06:55:20 -080051 SkAnnotateRectWithURL(canvas, r, data.get());
vandebo@chromium.org238be8c2012-07-13 20:06:02 +000052
halcanary3d32d502015-03-01 06:55:20 -080053 REPORTER_ASSERT(reporter, doc->close());
mtkleincabc08c2015-02-19 08:29:24 -080054 SkAutoDataUnref out(outStream.copyToData());
55 const char* rawOutput = (const char*)out->data();
vandebo@chromium.org238be8c2012-07-13 20:06:02 +000056
mtkleincabc08c2015-02-19 08:29:24 -080057 REPORTER_ASSERT(reporter, ContainsString(rawOutput, out->size(), "/Annots "));
vandebo@chromium.org238be8c2012-07-13 20:06:02 +000058}
59
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000060DEF_TEST(Annotation_NamedDestination, reporter) {
halcanary3d32d502015-03-01 06:55:20 -080061 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.comb58772f2013-03-08 09:09:10 +000065
66 SkPoint p = SkPoint::Make(SkIntToScalar(72), SkIntToScalar(72));
67 SkAutoDataUnref data(SkData::NewWithCString("example"));
halcanary3d32d502015-03-01 06:55:20 -080068 SkAnnotateNamedDestination(canvas, p, data.get());
epoger@google.comb58772f2013-03-08 09:09:10 +000069
halcanary3d32d502015-03-01 06:55:20 -080070 REPORTER_ASSERT(reporter, doc->close());
epoger@google.comb58772f2013-03-08 09:09:10 +000071 SkAutoDataUnref out(outStream.copyToData());
72 const char* rawOutput = (const char*)out->data();
73
74 REPORTER_ASSERT(reporter,
75 ContainsString(rawOutput, out->size(), "/example "));
76}