Home | History | Annotate | Download | only in tests
      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 
      8 #include "SkAnnotation.h"
      9 #include "SkBitmap.h"
     10 #include "SkCanvas.h"
     11 #include "SkColor.h"
     12 #include "SkData.h"
     13 #include "SkPDFDocument.h"
     14 #include "SkPoint.h"
     15 #include "SkRect.h"
     16 #include "SkRefCnt.h"
     17 #include "SkScalar.h"
     18 #include "SkStream.h"
     19 #include "SkTypes.h"
     20 #include "Test.h"
     21 
     22 #include <string.h>
     23 #include <memory>
     24 
     25 /** Returns true if data (may contain null characters) contains needle (null
     26  *  terminated). */
     27 static bool ContainsString(const char* data, size_t dataSize, const char* needle) {
     28     size_t nSize = strlen(needle);
     29     for (size_t i = 0; i < dataSize - nSize; i++) {
     30         if (strncmp(&data[i], needle, nSize) == 0) {
     31             return true;
     32         }
     33     }
     34     return false;
     35 }
     36 
     37 DEF_TEST(Annotation_NoDraw, reporter) {
     38     SkBitmap bm;
     39     bm.allocN32Pixels(10, 10);
     40     bm.eraseColor(SK_ColorTRANSPARENT);
     41 
     42     SkCanvas canvas(bm);
     43     SkRect r = SkRect::MakeWH(SkIntToScalar(10), SkIntToScalar(10));
     44 
     45     sk_sp<SkData> data(SkData::MakeWithCString("http://www.gooogle.com"));
     46 
     47     REPORTER_ASSERT(reporter, 0 == *bm.getAddr32(0, 0));
     48     SkAnnotateRectWithURL(&canvas, r, data.get());
     49     REPORTER_ASSERT(reporter, 0 == *bm.getAddr32(0, 0));
     50 }
     51 
     52 DEF_TEST(Annotation_PdfLink, reporter) {
     53     REQUIRE_PDF_DOCUMENT(Annotation_PdfLink, reporter);
     54     SkDynamicMemoryWStream outStream;
     55     auto doc = SkPDF::MakeDocument(&outStream);
     56     SkCanvas* canvas = doc->beginPage(612.0f, 792.0f);
     57     REPORTER_ASSERT(reporter, canvas);
     58 
     59     SkRect r = SkRect::MakeXYWH(SkIntToScalar(72), SkIntToScalar(72),
     60                                 SkIntToScalar(288), SkIntToScalar(72));
     61     sk_sp<SkData> data(SkData::MakeWithCString("http://www.gooogle.com"));
     62     SkAnnotateRectWithURL(canvas, r, data.get());
     63 
     64     doc->close();
     65     sk_sp<SkData> out = outStream.detachAsData();
     66     const char* rawOutput = (const char*)out->data();
     67 
     68     REPORTER_ASSERT(reporter, ContainsString(rawOutput, out->size(), "/Annots "));
     69 }
     70 
     71 DEF_TEST(Annotation_PdfDefineNamedDestination, reporter) {
     72     REQUIRE_PDF_DOCUMENT(Annotation_PdfNamedDestination, reporter);
     73     SkDynamicMemoryWStream outStream;
     74     auto doc = SkPDF::MakeDocument(&outStream);
     75     SkCanvas* canvas = doc->beginPage(612.0f, 792.0f);
     76     REPORTER_ASSERT(reporter, canvas);
     77 
     78     SkPoint p = SkPoint::Make(SkIntToScalar(72), SkIntToScalar(72));
     79     sk_sp<SkData> data(SkData::MakeWithCString("example"));
     80     SkAnnotateNamedDestination(canvas, p, data.get());
     81 
     82     doc->close();
     83     sk_sp<SkData> out = outStream.detachAsData();
     84     const char* rawOutput = (const char*)out->data();
     85 
     86     REPORTER_ASSERT(reporter,
     87         ContainsString(rawOutput, out->size(), "/example "));
     88 }
     89 
     90 #if defined(SK_XML)
     91     #include "SkSVGCanvas.h"
     92     #include "SkXMLWriter.h"
     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 
     99         SkRect r = SkRect::MakeXYWH(SkIntToScalar(72), SkIntToScalar(72), SkIntToScalar(288),
    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 
    107         REPORTER_ASSERT(reporter,
    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 
    116         SkRect r = SkRect::MakeXYWH(SkIntToScalar(72), SkIntToScalar(72), SkIntToScalar(288),
    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 
    124         REPORTER_ASSERT(reporter,
    125             ContainsString(rawOutput, out->size(),
    126                            "a xlink:href=\"http://www.gooogle.com/#NamedDestination\""));
    127     }
    128 #endif
    129