| OLD | NEW |
| 1 | 1 |
| 2 /* | 2 /* |
| 3 * Copyright 2011 Google Inc. | 3 * Copyright 2011 Google Inc. |
| 4 * | 4 * |
| 5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
| 6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
| 7 */ | 7 */ |
| 8 #include "Test.h" | 8 #include "Test.h" |
| 9 #include "SkAnnotation.h" | 9 #include "SkAnnotation.h" |
| 10 #include "SkData.h" | 10 #include "SkData.h" |
| 11 #include "SkCanvas.h" | 11 #include "SkCanvas.h" |
| 12 #include "SkPDFDevice.h" | 12 #include "SkPDFDevice.h" |
| 13 #include "SkPDFDocument.h" | 13 #include "SkPDFDocument.h" |
| 14 | 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 |
| 15 static void test_nodraw(skiatest::Reporter* reporter) { | 27 static void test_nodraw(skiatest::Reporter* reporter) { |
| 16 SkBitmap bm; | 28 SkBitmap bm; |
| 17 bm.setConfig(SkBitmap::kARGB_8888_Config, 10, 10); | 29 bm.setConfig(SkBitmap::kARGB_8888_Config, 10, 10); |
| 18 bm.allocPixels(); | 30 bm.allocPixels(); |
| 19 bm.eraseColor(SK_ColorTRANSPARENT); | 31 bm.eraseColor(SK_ColorTRANSPARENT); |
| 20 | 32 |
| 21 SkCanvas canvas(bm); | 33 SkCanvas canvas(bm); |
| 22 SkRect r = SkRect::MakeWH(SkIntToScalar(10), SkIntToScalar(10)); | 34 SkRect r = SkRect::MakeWH(SkIntToScalar(10), SkIntToScalar(10)); |
| 23 | 35 |
| 24 SkAutoDataUnref data(SkData::NewWithCString("http://www.gooogle.com")); | 36 SkAutoDataUnref data(SkData::NewWithCString("http://www.gooogle.com")); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 48 testCase tests[] = {{(SkPDFDocument::Flags)0, true}, | 60 testCase tests[] = {{(SkPDFDocument::Flags)0, true}, |
| 49 {SkPDFDocument::kNoLinks_Flags, false}}; | 61 {SkPDFDocument::kNoLinks_Flags, false}}; |
| 50 for (size_t testNum = 0; testNum < SK_ARRAY_COUNT(tests); testNum++) { | 62 for (size_t testNum = 0; testNum < SK_ARRAY_COUNT(tests); testNum++) { |
| 51 SkPDFDocument doc(tests[testNum].flags); | 63 SkPDFDocument doc(tests[testNum].flags); |
| 52 doc.appendPage(&device); | 64 doc.appendPage(&device); |
| 53 SkDynamicMemoryWStream outStream; | 65 SkDynamicMemoryWStream outStream; |
| 54 doc.emitPDF(&outStream); | 66 doc.emitPDF(&outStream); |
| 55 SkAutoDataUnref out(outStream.copyToData()); | 67 SkAutoDataUnref out(outStream.copyToData()); |
| 56 const char* rawOutput = (const char*)out->data(); | 68 const char* rawOutput = (const char*)out->data(); |
| 57 | 69 |
| 58 bool found = false; | 70 REPORTER_ASSERT(reporter, |
| 59 for (size_t i = 0; i < out->size() - 8; i++) { | 71 ContainsString(rawOutput, out->size(), "/Annots ") |
| 60 if (rawOutput[i + 0] == '/' && | 72 == tests[testNum].expectAnnotations); |
| 61 rawOutput[i + 1] == 'A' && | |
| 62 rawOutput[i + 2] == 'n' && | |
| 63 rawOutput[i + 3] == 'n' && | |
| 64 rawOutput[i + 4] == 'o' && | |
| 65 rawOutput[i + 5] == 't' && | |
| 66 rawOutput[i + 6] == 's' && | |
| 67 rawOutput[i + 7] == ' ') { | |
| 68 found = true; | |
| 69 break; | |
| 70 } | |
| 71 } | |
| 72 REPORTER_ASSERT(reporter, found == tests[testNum].expectAnnotations); | |
| 73 } | 73 } |
| 74 } | 74 } |
| 75 | 75 |
| 76 static 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 |
| 76 static void TestAnnotation(skiatest::Reporter* reporter) { | 98 static void TestAnnotation(skiatest::Reporter* reporter) { |
| 77 test_nodraw(reporter); | 99 test_nodraw(reporter); |
| 78 test_pdf_link_annotations(reporter); | 100 test_pdf_link_annotations(reporter); |
| 101 test_named_destination_annotations(reporter); |
| 79 } | 102 } |
| 80 | 103 |
| 81 #include "TestClassDef.h" | 104 #include "TestClassDef.h" |
| 82 DEFINE_TESTCLASS("Annotation", AnnotationClass, TestAnnotation) | 105 DEFINE_TESTCLASS("Annotation", AnnotationClass, TestAnnotation) |
| OLD | NEW |