aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests
diff options
context:
space:
mode:
authorGravatar reed <reed@google.com>2016-03-04 16:36:20 -0800
committerGravatar Commit bot <commit-bot@chromium.org>2016-03-04 16:36:20 -0800
commitf70b531daaf47db1ee95c70da9843f1dd1f418d3 (patch)
tree85965584497549d7729c301075ff2ec9d1dc2c81 /tests
parente77875aa425d51cc8db3463343a6308f9d2aadcc (diff)
Move annotations to canvas virtual (patchset #8 id:140001 of https://codereview.chromium.org/1744103002/ )"
Diffstat (limited to 'tests')
-rw-r--r--tests/SerializationTest.cpp83
-rw-r--r--tests/Writer32Test.cpp41
2 files changed, 124 insertions, 0 deletions
diff --git a/tests/SerializationTest.cpp b/tests/SerializationTest.cpp
index a4e40c54ee..eb84b3c957 100644
--- a/tests/SerializationTest.cpp
+++ b/tests/SerializationTest.cpp
@@ -5,6 +5,7 @@
* found in the LICENSE file.
*/
+#include "SkAnnotationKeys.h"
#include "Resources.h"
#include "SkCanvas.h"
#include "SkFixed.h"
@@ -546,3 +547,85 @@ DEF_TEST(Serialization, reporter) {
TestPictureTypefaceSerialization(reporter);
}
+
+///////////////////////////////////////////////////////////////////////////////////////////////////
+#include "SkAnnotation.h"
+
+static SkPicture* copy_picture_via_serialization(SkPicture* src) {
+ SkDynamicMemoryWStream wstream;
+ src->serialize(&wstream);
+ SkAutoTDelete<SkStreamAsset> rstream(wstream.detachAsStream());
+ return SkPicture::CreateFromStream(rstream);
+}
+
+struct AnnotationRec {
+ const SkRect fRect;
+ const char* fKey;
+ SkData* fValue;
+};
+
+class TestAnnotationCanvas : public SkCanvas {
+ skiatest::Reporter* fReporter;
+ const AnnotationRec* fRec;
+ int fCount;
+ int fCurrIndex;
+
+public:
+ TestAnnotationCanvas(skiatest::Reporter* reporter, const AnnotationRec rec[], int count)
+ : SkCanvas(100, 100)
+ , fReporter(reporter)
+ , fRec(rec)
+ , fCount(count)
+ , fCurrIndex(0)
+ {}
+
+ ~TestAnnotationCanvas() {
+ REPORTER_ASSERT(fReporter, fCount == fCurrIndex);
+ }
+
+protected:
+ void onDrawAnnotation(const SkRect& rect, const char key[], SkData* value) {
+ REPORTER_ASSERT(fReporter, fCurrIndex < fCount);
+ REPORTER_ASSERT(fReporter, rect == fRec[fCurrIndex].fRect);
+ REPORTER_ASSERT(fReporter, !strcmp(key, fRec[fCurrIndex].fKey));
+ REPORTER_ASSERT(fReporter, value->equals(fRec[fCurrIndex].fValue));
+ fCurrIndex += 1;
+ }
+};
+
+/*
+ * Test the 3 annotation types by recording them into a picture, serializing, and then playing
+ * them back into another canvas.
+ */
+DEF_TEST(Annotations, reporter) {
+ SkPictureRecorder recorder;
+ SkCanvas* recordingCanvas = recorder.beginRecording(SkRect::MakeWH(100, 100));
+
+ const char* str0 = "rect-with-url";
+ const SkRect r0 = SkRect::MakeWH(10, 10);
+ SkAutoTUnref<SkData> d0(SkData::NewWithCString(str0));
+ SkAnnotateRectWithURL(recordingCanvas, r0, d0);
+
+ const char* str1 = "named-destination";
+ const SkRect r1 = SkRect::MakeXYWH(5, 5, 0, 0); // collapsed to a point
+ SkAutoTUnref<SkData> d1(SkData::NewWithCString(str1));
+ SkAnnotateNamedDestination(recordingCanvas, {r1.x(), r1.y()}, d1);
+
+ const char* str2 = "link-to-destination";
+ const SkRect r2 = SkRect::MakeXYWH(20, 20, 5, 6);
+ SkAutoTUnref<SkData> d2(SkData::NewWithCString(str2));
+ SkAnnotateLinkToDestination(recordingCanvas, r2, d2);
+
+ const AnnotationRec recs[] = {
+ { r0, SkAnnotationKeys::URL_Key(), d0 },
+ { r1, SkAnnotationKeys::Define_Named_Dest_Key(), d1 },
+ { r2, SkAnnotationKeys::Link_Named_Dest_Key(), d2 },
+ };
+
+ SkAutoTUnref<SkPicture> pict0(recorder.endRecording());
+ SkAutoTUnref<SkPicture> pict1(copy_picture_via_serialization(pict0));
+
+ TestAnnotationCanvas canvas(reporter, recs, SK_ARRAY_COUNT(recs));
+ canvas.drawPicture(pict1);
+}
+
diff --git a/tests/Writer32Test.cpp b/tests/Writer32Test.cpp
index 39ae79cff6..e03a9163aa 100644
--- a/tests/Writer32Test.cpp
+++ b/tests/Writer32Test.cpp
@@ -278,3 +278,44 @@ DEF_TEST(Writer32_misc, reporter) {
test_rewind(reporter);
}
+DEF_TEST(Writer32_data, reporter) {
+ const char* str = "0123456789";
+ SkAutoTUnref<SkData> data0(SkData::NewWithCString(str));
+ SkAutoTUnref<SkData> data1(SkData::NewEmpty());
+
+ const size_t sizes[] = {
+ SkWriter32::WriteDataSize(nullptr),
+ SkWriter32::WriteDataSize(data0),
+ SkWriter32::WriteDataSize(data1),
+ };
+
+ SkSWriter32<1000> writer;
+ size_t sizeWritten = 0;
+
+ writer.writeData(nullptr);
+ sizeWritten += sizes[0];
+ REPORTER_ASSERT(reporter, sizeWritten == writer.bytesWritten());
+
+ writer.writeData(data0);
+ sizeWritten += sizes[1];
+ REPORTER_ASSERT(reporter, sizeWritten == writer.bytesWritten());
+
+ writer.writeData(data1);
+ sizeWritten += sizes[2];
+ REPORTER_ASSERT(reporter, sizeWritten == writer.bytesWritten());
+
+ SkAutoTUnref<SkData> result(writer.snapshotAsData());
+
+ SkReader32 reader(result->data(), result->size());
+ SkAutoTUnref<SkData> d0(reader.readData()),
+ d1(reader.readData()),
+ d2(reader.readData());
+
+ REPORTER_ASSERT(reporter, 0 == d0->size());
+ REPORTER_ASSERT(reporter, strlen(str)+1 == d1->size());
+ REPORTER_ASSERT(reporter, !memcmp(str, d1->data(), strlen(str)+1));
+ REPORTER_ASSERT(reporter, 0 == d2->size());
+
+ REPORTER_ASSERT(reporter, reader.offset() == sizeWritten);
+ REPORTER_ASSERT(reporter, reader.eof());
+}