Index: tests/SerializeDrawableTest.cpp |
diff --git a/tests/SerializeDrawableTest.cpp b/tests/SerializeDrawableTest.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..1de42fb370f34c19641a240aee3e03cb53bb7c71 |
--- /dev/null |
+++ b/tests/SerializeDrawableTest.cpp |
@@ -0,0 +1,235 @@ |
+/* |
+ * Copyright 2016 Google Inc. |
+ * |
+ * Use of this source code is governed by a BSD-style license that can be |
+ * found in the LICENSE file. |
+ */ |
+ |
+#include "SkCanvas.h" |
+#include "SkDrawable.h" |
+#include "SkReadBuffer.h" |
+#include "SkRect.h" |
+#include "SkSerializeDrawable.h" |
+#include "SkStream.h" |
+#include "SkWriteBuffer.h" |
+#include "Test.h" |
+ |
+class SkIntDrawable : public SkDrawable { |
+public: |
+ SkIntDrawable(uint32_t a, uint32_t b, uint32_t c, uint32_t d) |
+ : fA(a) |
+ , fB(b) |
+ , fC(c) |
+ , fD(d) |
+ {} |
+ |
+ void flatten(SkWriteBuffer& buffer) const override { |
+ buffer.writeUInt(fA); |
+ buffer.writeUInt(fB); |
+ buffer.writeUInt(fC); |
+ buffer.writeUInt(fD); |
+ } |
+ |
+ static SkFlattenable* CreateProc(SkReadBuffer& buffer) { |
+ uint32_t a = buffer.readUInt(); |
+ uint32_t b = buffer.readUInt(); |
+ uint32_t c = buffer.readUInt(); |
+ uint32_t d = buffer.readUInt(); |
+ return new SkIntDrawable(a, b, c, d); |
+ } |
+ |
+ Factory getFactory() const override { return CreateProc; } |
+ |
+ uint32_t a() const { return fA; } |
+ uint32_t b() const { return fB; } |
+ uint32_t c() const { return fC; } |
+ uint32_t d() const { return fD; } |
+ |
+protected: |
+ SkRect onGetBounds() override { return SkRect::MakeEmpty(); } |
+ void onDraw(SkCanvas*) override {} |
+ |
+private: |
+ uint32_t fA; |
+ uint32_t fB; |
+ uint32_t fC; |
+ uint32_t fD; |
+}; |
+ |
+class SkPaintDrawable : public SkDrawable { |
+public: |
+ SkPaintDrawable(const SkPaint& paint) |
+ : fPaint(paint) |
+ {} |
+ |
+ void flatten(SkWriteBuffer& buffer) const override { |
+ buffer.writePaint(fPaint); |
+ } |
+ |
+ static SkFlattenable* CreateProc(SkReadBuffer& buffer) { |
+ SkPaint paint; |
+ buffer.readPaint(&paint); |
+ return new SkPaintDrawable(paint); |
+ } |
+ |
+ Factory getFactory() const override { return CreateProc; } |
+ |
+ const SkPaint& paint() const { return fPaint; } |
+ |
+protected: |
+ SkRect onGetBounds() override { return SkRect::MakeEmpty(); } |
+ void onDraw(SkCanvas*) override {} |
+ |
+private: |
+ SkPaint fPaint; |
+}; |
+ |
+class SkCompoundDrawable : public SkDrawable { |
+public: |
+ SkCompoundDrawable(uint32_t a, uint32_t b, uint32_t c, uint32_t d, const SkPaint& paint) |
+ : fIntDrawable(new SkIntDrawable(a, b, c, d)) |
+ , fPaintDrawable(new SkPaintDrawable(paint)) |
+ {} |
+ |
+ SkCompoundDrawable(SkIntDrawable* intDrawable, SkPaintDrawable* paintDrawable) |
+ : fIntDrawable(SkRef(intDrawable)) |
+ , fPaintDrawable(SkRef(paintDrawable)) |
+ {} |
+ |
+ void flatten(SkWriteBuffer& buffer) const override { |
+ buffer.writeFlattenable(fIntDrawable); |
+ buffer.writeFlattenable(fPaintDrawable); |
+ } |
+ |
+ static SkFlattenable* CreateProc(SkReadBuffer& buffer) { |
+ SkAutoTUnref<SkFlattenable> intDrawable( |
+ buffer.readFlattenable(SkFlattenable::kSkDrawable_Type)); |
+ SkASSERT(intDrawable); |
+ SkASSERT(!strcmp("SkIntDrawable", intDrawable->getTypeName())); |
+ |
+ SkAutoTUnref<SkFlattenable> paintDrawable( |
+ buffer.readFlattenable(SkFlattenable::kSkDrawable_Type)); |
+ SkASSERT(paintDrawable); |
+ SkASSERT(!strcmp("SkPaintDrawable", paintDrawable->getTypeName())); |
+ |
+ return new SkCompoundDrawable((SkIntDrawable*) intDrawable.get(), |
+ (SkPaintDrawable*) paintDrawable.get()); |
+ } |
+ |
+ Factory getFactory() const override { return CreateProc; } |
+ |
+ SkIntDrawable* intDrawable() const { return fIntDrawable; } |
+ SkPaintDrawable* paintDrawable() const { return fPaintDrawable; } |
+ |
+protected: |
+ SkRect onGetBounds() override { return SkRect::MakeEmpty(); } |
+ void onDraw(SkCanvas*) override {} |
+ |
+private: |
+ SkAutoTUnref<SkIntDrawable> fIntDrawable; |
+ SkAutoTUnref<SkPaintDrawable> fPaintDrawable; |
+}; |
+ |
+class SkRootDrawable : public SkDrawable { |
+public: |
+ SkRootDrawable(uint32_t a, uint32_t b, uint32_t c, uint32_t d, const SkPaint& paint, |
+ uint32_t e, uint32_t f, uint32_t g, uint32_t h, SkDrawable* drawable) |
+ : fCompoundDrawable(new SkCompoundDrawable(a, b, c, d, paint)) |
+ , fIntDrawable(new SkIntDrawable(e, f, g, h)) |
+ , fDrawable(SkRef(drawable)) |
+ {} |
+ |
+ SkRootDrawable(SkCompoundDrawable* compoundDrawable, SkIntDrawable* intDrawable, |
+ SkDrawable* drawable) |
+ : fCompoundDrawable(SkRef(compoundDrawable)) |
+ , fIntDrawable(SkRef(intDrawable)) |
+ , fDrawable(SkRef(drawable)) |
+ {} |
+ |
+ void flatten(SkWriteBuffer& buffer) const override { |
+ buffer.writeFlattenable(fCompoundDrawable); |
+ buffer.writeFlattenable(fIntDrawable); |
+ buffer.writeFlattenable(fDrawable); |
+ } |
+ |
+ static SkFlattenable* CreateProc(SkReadBuffer& buffer) { |
+ SkAutoTUnref<SkFlattenable> compoundDrawable( |
+ buffer.readFlattenable(SkFlattenable::kSkDrawable_Type)); |
+ SkASSERT(compoundDrawable); |
+ SkASSERT(!strcmp("SkCompoundDrawable", compoundDrawable->getTypeName())); |
+ |
+ SkAutoTUnref<SkFlattenable> intDrawable( |
+ buffer.readFlattenable(SkFlattenable::kSkDrawable_Type)); |
+ SkASSERT(intDrawable); |
+ SkASSERT(!strcmp("SkIntDrawable", intDrawable->getTypeName())); |
+ |
+ SkAutoTUnref<SkFlattenable> drawable( |
+ buffer.readFlattenable(SkFlattenable::kSkDrawable_Type)); |
+ SkASSERT(drawable); |
+ |
+ return new SkRootDrawable((SkCompoundDrawable*) compoundDrawable.get(), |
+ (SkIntDrawable*) intDrawable.get(), |
+ (SkDrawable*) drawable.get()); |
+ } |
+ |
+ Factory getFactory() const override { return CreateProc; } |
+ |
+ SkCompoundDrawable* compoundDrawable() const { return fCompoundDrawable; } |
+ SkIntDrawable* intDrawable() const { return fIntDrawable; } |
+ SkDrawable* drawable() const { return fDrawable; } |
+ |
+protected: |
+ SkRect onGetBounds() override { return SkRect::MakeEmpty(); } |
+ void onDraw(SkCanvas*) override {} |
+ |
+private: |
+ SkAutoTUnref<SkCompoundDrawable> fCompoundDrawable; |
+ SkAutoTUnref<SkIntDrawable> fIntDrawable; |
+ SkAutoTUnref<SkDrawable> fDrawable; |
+}; |
+ |
+DEF_TEST(SerializeDrawable, r) { |
+ SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkIntDrawable) |
msarett
2016/03/29 15:29:18
Still need to think about how to register drawable
|
+ SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkPaintDrawable) |
+ SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkCompoundDrawable) |
+ SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkRootDrawable) |
+ |
+ // Create and serialize the test drawable |
+ SkAutoTUnref<SkDrawable> drawable(new SkIntDrawable(1, 2, 3, 4)); |
+ SkPaint paint; |
+ paint.setColor(SK_ColorBLUE); |
+ SkAutoTUnref<SkRootDrawable> rootDrawable(new SkRootDrawable(5, 6, 7, 8, paint, 9, 10, 11, 12, |
+ drawable)); |
+ SkAutoTDelete<SkWriteBuffer> writeBuffer(SkSerializeDrawable::Serialize(*rootDrawable)); |
+ |
+ // Copy the contents of the write buffer into a read buffer |
+ sk_sp<SkData> data = SkData::MakeUninitialized(writeBuffer->bytesWritten()); |
+ writeBuffer->writeToMemory(data->writable_data()); |
+ SkReadBuffer readBuffer(data->data(), data->size()); |
+ |
+ // Deserialize and verify the drawable |
+ SkAutoTUnref<SkDrawable> out(SkSerializeDrawable::NewFromSerialized(readBuffer)); |
+ REPORTER_ASSERT(r, out); |
+ REPORTER_ASSERT(r, !strcmp("SkRootDrawable", out->getTypeName())); |
+ |
+ SkRootDrawable* root = (SkRootDrawable*) out.get(); |
+ REPORTER_ASSERT(r, 5 == root->compoundDrawable()->intDrawable()->a()); |
+ REPORTER_ASSERT(r, 6 == root->compoundDrawable()->intDrawable()->b()); |
+ REPORTER_ASSERT(r, 7 == root->compoundDrawable()->intDrawable()->c()); |
+ REPORTER_ASSERT(r, 8 == root->compoundDrawable()->intDrawable()->d()); |
+ REPORTER_ASSERT(r, SK_ColorBLUE == |
+ root->compoundDrawable()->paintDrawable()->paint().getColor()); |
+ REPORTER_ASSERT(r, 9 == root->intDrawable()->a()); |
+ REPORTER_ASSERT(r, 10 == root->intDrawable()->b()); |
+ REPORTER_ASSERT(r, 11 == root->intDrawable()->c()); |
+ REPORTER_ASSERT(r, 12 == root->intDrawable()->d()); |
+ |
+ // Note that we can still recognize the generic drawable as int drawable |
+ SkDrawable* generic = root->drawable(); |
+ REPORTER_ASSERT(r, !strcmp("SkIntDrawable", generic->getTypeName())); |
+ SkIntDrawable* integer = (SkIntDrawable*) generic; |
+ REPORTER_ASSERT(r, 1 == integer->a()); |
+ REPORTER_ASSERT(r, 2 == integer->b()); |
+ REPORTER_ASSERT(r, 3 == integer->c()); |
+ REPORTER_ASSERT(r, 4 == integer->d()); |
+} |