Home | History | Annotate | Download | only in tests
      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  */
      8 #include "SkAnnotation.h"
      9 #include "SkCanvas.h"
     10 #include "SkData.h"
     11 #include "SkPDFDevice.h"
     12 #include "SkPDFDocument.h"
     13 #include "Test.h"
     14 
     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 
     27 DEF_TEST(Annotation_NoDraw, reporter) {
     28     SkBitmap bm;
     29     bm.allocN32Pixels(10, 10);
     30     bm.eraseColor(SK_ColorTRANSPARENT);
     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 
     42 struct testCase {
     43     SkPDFDocument::Flags flags;
     44     bool expectAnnotations;
     45 };
     46 
     47 DEF_TEST(Annotation_PdfLink, reporter) {
     48     SkISize size = SkISize::Make(612, 792);
     49     SkMatrix initialTransform;
     50     initialTransform.reset();
     51     SkPDFDevice device(size, size, initialTransform);
     52     SkCanvas canvas(&device);
     53 
     54     SkRect r = SkRect::MakeXYWH(SkIntToScalar(72), SkIntToScalar(72),
     55                                 SkIntToScalar(288), SkIntToScalar(72));
     56     SkAutoDataUnref data(SkData::NewWithCString("http://www.gooogle.com"));
     57     SkAnnotateRectWithURL(&canvas, r, data.get());
     58 
     59     testCase tests[] = {{(SkPDFDocument::Flags)0, true},
     60                         {SkPDFDocument::kNoLinks_Flags, false}};
     61     for (size_t testNum = 0; testNum < SK_ARRAY_COUNT(tests); testNum++) {
     62         SkPDFDocument doc(tests[testNum].flags);
     63         doc.appendPage(&device);
     64         SkDynamicMemoryWStream outStream;
     65         doc.emitPDF(&outStream);
     66         SkAutoDataUnref out(outStream.copyToData());
     67         const char* rawOutput = (const char*)out->data();
     68 
     69         REPORTER_ASSERT(reporter,
     70             ContainsString(rawOutput, out->size(), "/Annots ")
     71             == tests[testNum].expectAnnotations);
     72     }
     73 }
     74 
     75 DEF_TEST(Annotation_NamedDestination, reporter) {
     76     SkISize size = SkISize::Make(612, 792);
     77     SkMatrix initialTransform;
     78     initialTransform.reset();
     79     SkPDFDevice device(size, size, initialTransform);
     80     SkCanvas canvas(&device);
     81 
     82     SkPoint p = SkPoint::Make(SkIntToScalar(72), SkIntToScalar(72));
     83     SkAutoDataUnref data(SkData::NewWithCString("example"));
     84     SkAnnotateNamedDestination(&canvas, p, data.get());
     85 
     86     SkPDFDocument doc;
     87     doc.appendPage(&device);
     88     SkDynamicMemoryWStream outStream;
     89     doc.emitPDF(&outStream);
     90     SkAutoDataUnref out(outStream.copyToData());
     91     const char* rawOutput = (const char*)out->data();
     92 
     93     REPORTER_ASSERT(reporter,
     94         ContainsString(rawOutput, out->size(), "/example "));
     95 }
     96