Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(772)

Unified Diff: tests/FlattenableCustomFactory.cpp

Issue 1858323002: Enable flattening/unflattening with custom unflatten procs (Closed) Base URL: https://skia.googlesource.com/skia.git@flattenable
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« src/core/SkReadBuffer.cpp ('K') | « src/core/SkWriteBuffer.cpp ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tests/FlattenableCustomFactory.cpp
diff --git a/tests/FlattenableCustomFactory.cpp b/tests/FlattenableCustomFactory.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..57612d1f55a44a4f43490a2103055e05540ef898
--- /dev/null
+++ b/tests/FlattenableCustomFactory.cpp
@@ -0,0 +1,75 @@
+/*
+ * 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 "SkFlattenable.h"
+#include "SkReadBuffer.h"
+#include "SkWriteBuffer.h"
+#include "Test.h"
+
+class IntFlattenable : public SkFlattenable {
+public:
+ IntFlattenable(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);
+ }
+
+ Factory getFactory() const override { return nullptr; }
+
+ uint32_t a() const { return fA; }
+ uint32_t b() const { return fB; }
+ uint32_t c() const { return fC; }
+ uint32_t d() const { return fD; }
+
+ const char* getTypeName() const override { return "IntFlattenable"; }
+
+private:
+ uint32_t fA;
+ uint32_t fB;
+ uint32_t fC;
+ uint32_t fD;
+};
+
+static sk_sp<SkFlattenable> custom_create_proc(SkReadBuffer& buffer) {
+ uint32_t a = buffer.readUInt();
+ uint32_t b = buffer.readUInt();
+ uint32_t c = buffer.readUInt();
+ uint32_t d = buffer.readUInt();
+ return sk_sp<SkFlattenable>(new IntFlattenable(a + 1, b + 1, c + 1, d + 1));
+}
+
+DEF_TEST(UnflattenWithCustomFactory, r) {
+ // Create and flatten the test flattenable
+ SkAutoTUnref<SkFlattenable> flattenable(new IntFlattenable(1, 2, 3, 4));
+ SkWriteBuffer writeBuffer;
+ writeBuffer.writeFlattenable(flattenable);
+
+ // 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());
+
+ // Register a custom factory with the read buffer
+ readBuffer.setCustomFactory("IntFlattenable", &custom_create_proc);
+
+ // Unflatten and verify the flattenable
+ SkAutoTUnref<IntFlattenable> out((IntFlattenable*) readBuffer.readFlattenable(
+ SkFlattenable::kSkUnused_Type));
+ REPORTER_ASSERT(r, out);
+ REPORTER_ASSERT(r, 2 == out->a());
+ REPORTER_ASSERT(r, 3 == out->b());
+ REPORTER_ASSERT(r, 4 == out->c());
+ REPORTER_ASSERT(r, 5 == out->d());
+}
« src/core/SkReadBuffer.cpp ('K') | « src/core/SkWriteBuffer.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698