Flutter Engine
The Flutter Engine
AnnotationTest.cpp
Go to the documentation of this file.
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 */
7
12#include "include/core/SkData.h"
15#include "include/core/SkRect.h"
21#include "tests/Test.h"
22
23#include <cstddef>
24#include <cstring>
25
26/** Returns true if data (may contain null characters) contains needle (null
27 * terminated). */
28static bool ContainsString(const char* data, size_t dataSize, const char* needle) {
29 size_t nSize = strlen(needle);
30 for (size_t i = 0; i < dataSize - nSize; i++) {
31 if (strncmp(&data[i], needle, nSize) == 0) {
32 return true;
33 }
34 }
35 return false;
36}
37
38DEF_TEST(Annotation_NoDraw, reporter) {
39 SkBitmap bm;
40 bm.allocN32Pixels(10, 10);
42
43 SkCanvas canvas(bm);
45
46 sk_sp<SkData> data(SkData::MakeWithCString("http://www.gooogle.com"));
47
48 REPORTER_ASSERT(reporter, 0 == *bm.getAddr32(0, 0));
49 SkAnnotateRectWithURL(&canvas, r, data.get());
50 REPORTER_ASSERT(reporter, 0 == *bm.getAddr32(0, 0));
51}
52
53DEF_TEST(Annotation_PdfLink, reporter) {
54 REQUIRE_PDF_DOCUMENT(Annotation_PdfLink, reporter);
55 SkDynamicMemoryWStream outStream;
56 auto doc = SkPDF::MakeDocument(&outStream);
57 SkCanvas* canvas = doc->beginPage(612.0f, 792.0f);
59
62 sk_sp<SkData> data(SkData::MakeWithCString("http://www.gooogle.com"));
63 SkAnnotateRectWithURL(canvas, r, data.get());
64
65 doc->close();
66 sk_sp<SkData> out = outStream.detachAsData();
67 const char* rawOutput = (const char*)out->data();
68
69 REPORTER_ASSERT(reporter, ContainsString(rawOutput, out->size(), "/Annots "));
70}
71
72DEF_TEST(Annotation_PdfDefineNamedDestination, reporter) {
73 REQUIRE_PDF_DOCUMENT(Annotation_PdfNamedDestination, reporter);
74 SkDynamicMemoryWStream outStream;
75 auto doc = SkPDF::MakeDocument(&outStream);
76 SkCanvas* canvas = doc->beginPage(612.0f, 792.0f);
78
81 SkAnnotateNamedDestination(canvas, p, data.get());
82
83 doc->close();
84 sk_sp<SkData> out = outStream.detachAsData();
85 const char* rawOutput = (const char*)out->data();
86
88 ContainsString(rawOutput, out->size(), "/example "));
89}
90
91#if defined(SK_XML)
93
94 DEF_TEST(Annotation_SvgLink, reporter) {
95 SkDynamicMemoryWStream outStream;
96 SkRect bounds = SkRect::MakeIWH(400, 400);
97 std::unique_ptr<SkCanvas> canvas = SkSVGCanvas::Make(bounds, &outStream);
98
100 SkIntToScalar(72));
101 sk_sp<SkData> data(SkData::MakeWithCString("http://www.gooogle.com"));
102 SkAnnotateRectWithURL(canvas.get(), r, data.get());
103
104 sk_sp<SkData> out = outStream.detachAsData();
105 const char* rawOutput = (const char*)out->data();
106
108 ContainsString(rawOutput, out->size(), "a xlink:href=\"http://www.gooogle.com\""));
109 }
110
111 DEF_TEST(Annotation_SvgLinkNamedDestination, reporter) {
112 SkDynamicMemoryWStream outStream;
113 SkRect bounds = SkRect::MakeIWH(400, 400);
114 std::unique_ptr<SkCanvas> canvas = SkSVGCanvas::Make(bounds, &outStream);
115
117 SkIntToScalar(72));
118 sk_sp<SkData> data(SkData::MakeWithCString("http://www.gooogle.com/#NamedDestination"));
119 SkAnnotateLinkToDestination(canvas.get(), r, data.get());
120
121 sk_sp<SkData> out = outStream.detachAsData();
122 const char* rawOutput = (const char*)out->data();
123
125 ContainsString(rawOutput, out->size(),
126 "a xlink:href=\"http://www.gooogle.com/#NamedDestination\""));
127 }
128#endif
static bool ContainsString(const char *data, size_t dataSize, const char *needle)
DEF_TEST(Annotation_NoDraw, reporter)
reporter
Definition: FontMgrTest.cpp:39
SK_API void SkAnnotateRectWithURL(SkCanvas *, const SkRect &, SkData *)
SK_API void SkAnnotateNamedDestination(SkCanvas *, const SkPoint &, SkData *)
SK_API void SkAnnotateLinkToDestination(SkCanvas *, const SkRect &, SkData *)
constexpr SkColor SK_ColorTRANSPARENT
Definition: SkColor.h:99
#define SkIntToScalar(x)
Definition: SkScalar.h:57
#define REPORTER_ASSERT(r, cond,...)
Definition: Test.h:286
#define REQUIRE_PDF_DOCUMENT(TEST_NAME, REPORTER)
Definition: Test.h:485
void allocN32Pixels(int width, int height, bool isOpaque=false)
Definition: SkBitmap.cpp:232
uint32_t * getAddr32(int x, int y) const
Definition: SkBitmap.h:1260
void eraseColor(SkColor4f) const
Definition: SkBitmap.cpp:442
static sk_sp< SkData > MakeWithCString(const char cstr[])
Definition: SkData.cpp:195
sk_sp< SkData > detachAsData()
Definition: SkStream.cpp:707
static std::unique_ptr< SkCanvas > Make(const SkRect &bounds, SkWStream *, uint32_t flags=0)
Definition: SkSVGCanvas.cpp:19
SK_API sk_sp< SkDocument > MakeDocument(SkWStream *stream, const Metadata &metadata)
Optional< SkRect > bounds
Definition: SkRecords.h:189
static constexpr SkPoint Make(float x, float y)
Definition: SkPoint_impl.h:173
static SkRect MakeIWH(int w, int h)
Definition: SkRect.h:623
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition: SkRect.h:659
static constexpr SkRect MakeWH(float w, float h)
Definition: SkRect.h:609
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63