OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2016 Google Inc. |
| 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. |
| 6 */ |
| 7 |
| 8 #include "SkFlattenable.h" |
| 9 #include "SkReadBuffer.h" |
| 10 #include "SkWriteBuffer.h" |
| 11 #include "Test.h" |
| 12 |
| 13 class IntFlattenable : public SkFlattenable { |
| 14 public: |
| 15 IntFlattenable(uint32_t a, uint32_t b, uint32_t c, uint32_t d) |
| 16 : fA(a) |
| 17 , fB(b) |
| 18 , fC(c) |
| 19 , fD(d) |
| 20 {} |
| 21 |
| 22 void flatten(SkWriteBuffer& buffer) const override { |
| 23 buffer.writeUInt(fA); |
| 24 buffer.writeUInt(fB); |
| 25 buffer.writeUInt(fC); |
| 26 buffer.writeUInt(fD); |
| 27 } |
| 28 |
| 29 Factory getFactory() const override { return nullptr; } |
| 30 |
| 31 uint32_t a() const { return fA; } |
| 32 uint32_t b() const { return fB; } |
| 33 uint32_t c() const { return fC; } |
| 34 uint32_t d() const { return fD; } |
| 35 |
| 36 const char* getTypeName() const override { return "IntFlattenable"; } |
| 37 |
| 38 private: |
| 39 uint32_t fA; |
| 40 uint32_t fB; |
| 41 uint32_t fC; |
| 42 uint32_t fD; |
| 43 }; |
| 44 |
| 45 static sk_sp<SkFlattenable> custom_create_proc(SkReadBuffer& buffer) { |
| 46 uint32_t a = buffer.readUInt(); |
| 47 uint32_t b = buffer.readUInt(); |
| 48 uint32_t c = buffer.readUInt(); |
| 49 uint32_t d = buffer.readUInt(); |
| 50 return sk_sp<SkFlattenable>(new IntFlattenable(a + 1, b + 1, c + 1, d + 1)); |
| 51 } |
| 52 |
| 53 DEF_TEST(UnflattenWithCustomFactory, r) { |
| 54 // Create and flatten the test flattenable |
| 55 SkAutoTUnref<SkFlattenable> flattenable(new IntFlattenable(1, 2, 3, 4)); |
| 56 SkWriteBuffer writeBuffer; |
| 57 writeBuffer.writeFlattenable(flattenable); |
| 58 |
| 59 // Copy the contents of the write buffer into a read buffer |
| 60 sk_sp<SkData> data = SkData::MakeUninitialized(writeBuffer.bytesWritten()); |
| 61 writeBuffer.writeToMemory(data->writable_data()); |
| 62 SkReadBuffer readBuffer(data->data(), data->size()); |
| 63 |
| 64 // Register a custom factory with the read buffer |
| 65 readBuffer.setCustomFactory("IntFlattenable", &custom_create_proc); |
| 66 |
| 67 // Unflatten and verify the flattenable |
| 68 SkAutoTUnref<IntFlattenable> out((IntFlattenable*) readBuffer.readFlattenabl
e( |
| 69 SkFlattenable::kSkUnused_Type)); |
| 70 REPORTER_ASSERT(r, out); |
| 71 REPORTER_ASSERT(r, 2 == out->a()); |
| 72 REPORTER_ASSERT(r, 3 == out->b()); |
| 73 REPORTER_ASSERT(r, 4 == out->c()); |
| 74 REPORTER_ASSERT(r, 5 == out->d()); |
| 75 } |
OLD | NEW |