AnnotationTest.cpp revision 1cad898916c9e4a21429b5325c834a51fa6f361e
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 "Test.h" 9#include "SkAnnotation.h" 10#include "SkData.h" 11#include "SkCanvas.h" 12#include "SkPDFDevice.h" 13#include "SkPDFDocument.h" 14 15/** 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 27static void test_nodraw(skiatest::Reporter* reporter) { 28 SkBitmap bm; 29 bm.setConfig(SkBitmap::kARGB_8888_Config, 10, 10); 30 bm.allocPixels(); 31 bm.eraseColor(SK_ColorTRANSPARENT); 32 33 SkCanvas canvas(bm); 34 SkRect r = SkRect::MakeWH(SkIntToScalar(10), SkIntToScalar(10)); 35 36 SkAutoDataUnref data(SkData::NewWithCString("http://www.gooogle.com")); 37 38 REPORTER_ASSERT(reporter, 0 == *bm.getAddr32(0, 0)); 39 SkAnnotateRectWithURL(&canvas, r, data.get()); 40 REPORTER_ASSERT(reporter, 0 == *bm.getAddr32(0, 0)); 41} 42 43struct testCase { 44 SkPDFDocument::Flags flags; 45 bool expectAnnotations; 46}; 47 48static void test_pdf_link_annotations(skiatest::Reporter* reporter) { 49 SkISize size = SkISize::Make(612, 792); 50 SkMatrix initialTransform; 51 initialTransform.reset(); 52 SkPDFDevice device(size, size, initialTransform); 53 SkCanvas canvas(&device); 54 55 SkRect r = SkRect::MakeXYWH(SkIntToScalar(72), SkIntToScalar(72), 56 SkIntToScalar(288), SkIntToScalar(72)); 57 SkAutoDataUnref data(SkData::NewWithCString("http://www.gooogle.com")); 58 SkAnnotateRectWithURL(&canvas, r, data.get()); 59 60 testCase tests[] = {{(SkPDFDocument::Flags)0, true}, 61 {SkPDFDocument::kNoLinks_Flags, false}}; 62 for (size_t testNum = 0; testNum < SK_ARRAY_COUNT(tests); testNum++) { 63 SkPDFDocument doc(tests[testNum].flags); 64 doc.appendPage(&device); 65 SkDynamicMemoryWStream outStream; 66 doc.emitPDF(&outStream); 67 SkAutoDataUnref out(outStream.copyToData()); 68 const char* rawOutput = (const char*)out->data(); 69 70 REPORTER_ASSERT(reporter, 71 ContainsString(rawOutput, out->size(), "/Annots ") 72 == tests[testNum].expectAnnotations); 73 } 74} 75 76static void test_named_destination_annotations(skiatest::Reporter* reporter) { 77 SkISize size = SkISize::Make(612, 792); 78 SkMatrix initialTransform; 79 initialTransform.reset(); 80 SkPDFDevice device(size, size, initialTransform); 81 SkCanvas canvas(&device); 82 83 SkPoint p = SkPoint::Make(SkIntToScalar(72), SkIntToScalar(72)); 84 SkAutoDataUnref data(SkData::NewWithCString("example")); 85 SkAnnotateNamedDestination(&canvas, p, data.get()); 86 87 SkPDFDocument doc; 88 doc.appendPage(&device); 89 SkDynamicMemoryWStream outStream; 90 doc.emitPDF(&outStream); 91 SkAutoDataUnref out(outStream.copyToData()); 92 const char* rawOutput = (const char*)out->data(); 93 94 REPORTER_ASSERT(reporter, 95 ContainsString(rawOutput, out->size(), "/example ")); 96} 97 98static void TestAnnotation(skiatest::Reporter* reporter) { 99 test_nodraw(reporter); 100 test_pdf_link_annotations(reporter); 101 test_named_destination_annotations(reporter); 102} 103 104#include "TestClassDef.h" 105DEFINE_TESTCLASS("Annotation", AnnotationClass, TestAnnotation) 106