msarett | a3b3b23 | 2016-04-22 12:43:07 -0700 | [diff] [blame] | 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; } |
Mike Reed | 58a3fcd | 2017-12-11 13:23:33 -0500 | [diff] [blame] | 30 | Type getFlattenableType() const override { return SkFlattenable::kSkUnused_Type; } |
msarett | a3b3b23 | 2016-04-22 12:43:07 -0700 | [diff] [blame] | 31 | |
| 32 | uint32_t a() const { return fA; } |
| 33 | uint32_t b() const { return fB; } |
| 34 | uint32_t c() const { return fC; } |
| 35 | uint32_t d() const { return fD; } |
| 36 | |
| 37 | const char* getTypeName() const override { return "IntFlattenable"; } |
| 38 | |
| 39 | private: |
| 40 | uint32_t fA; |
| 41 | uint32_t fB; |
| 42 | uint32_t fC; |
| 43 | uint32_t fD; |
| 44 | }; |
| 45 | |
| 46 | static sk_sp<SkFlattenable> custom_create_proc(SkReadBuffer& buffer) { |
| 47 | uint32_t a = buffer.readUInt(); |
| 48 | uint32_t b = buffer.readUInt(); |
| 49 | uint32_t c = buffer.readUInt(); |
| 50 | uint32_t d = buffer.readUInt(); |
| 51 | return sk_sp<SkFlattenable>(new IntFlattenable(a + 1, b + 1, c + 1, d + 1)); |
| 52 | } |
| 53 | |
| 54 | DEF_TEST(UnflattenWithCustomFactory, r) { |
| 55 | // Create and flatten the test flattenable |
brianosman | fad9856 | 2016-05-04 11:06:28 -0700 | [diff] [blame] | 56 | SkBinaryWriteBuffer writeBuffer; |
Hal Canary | 342b7ac | 2016-11-04 11:49:42 -0400 | [diff] [blame] | 57 | sk_sp<SkFlattenable> flattenable1(new IntFlattenable(1, 2, 3, 4)); |
| 58 | writeBuffer.writeFlattenable(flattenable1.get()); |
| 59 | sk_sp<SkFlattenable> flattenable2(new IntFlattenable(2, 3, 4, 5)); |
| 60 | writeBuffer.writeFlattenable(flattenable2.get()); |
| 61 | sk_sp<SkFlattenable> flattenable3(new IntFlattenable(3, 4, 5, 6)); |
| 62 | writeBuffer.writeFlattenable(flattenable3.get()); |
msarett | a3b3b23 | 2016-04-22 12:43:07 -0700 | [diff] [blame] | 63 | |
| 64 | // Copy the contents of the write buffer into a read buffer |
| 65 | sk_sp<SkData> data = SkData::MakeUninitialized(writeBuffer.bytesWritten()); |
| 66 | writeBuffer.writeToMemory(data->writable_data()); |
| 67 | SkReadBuffer readBuffer(data->data(), data->size()); |
| 68 | |
| 69 | // Register a custom factory with the read buffer |
| 70 | readBuffer.setCustomFactory(SkString("IntFlattenable"), &custom_create_proc); |
| 71 | |
| 72 | // Unflatten and verify the flattenables |
Hal Canary | 342b7ac | 2016-11-04 11:49:42 -0400 | [diff] [blame] | 73 | sk_sp<IntFlattenable> out1((IntFlattenable*) readBuffer.readFlattenable( |
msarett | a3b3b23 | 2016-04-22 12:43:07 -0700 | [diff] [blame] | 74 | SkFlattenable::kSkUnused_Type)); |
| 75 | REPORTER_ASSERT(r, out1); |
| 76 | REPORTER_ASSERT(r, 2 == out1->a()); |
| 77 | REPORTER_ASSERT(r, 3 == out1->b()); |
| 78 | REPORTER_ASSERT(r, 4 == out1->c()); |
| 79 | REPORTER_ASSERT(r, 5 == out1->d()); |
| 80 | |
Hal Canary | 342b7ac | 2016-11-04 11:49:42 -0400 | [diff] [blame] | 81 | sk_sp<IntFlattenable> out2((IntFlattenable*) readBuffer.readFlattenable( |
msarett | a3b3b23 | 2016-04-22 12:43:07 -0700 | [diff] [blame] | 82 | SkFlattenable::kSkUnused_Type)); |
| 83 | REPORTER_ASSERT(r, out2); |
| 84 | REPORTER_ASSERT(r, 3 == out2->a()); |
| 85 | REPORTER_ASSERT(r, 4 == out2->b()); |
| 86 | REPORTER_ASSERT(r, 5 == out2->c()); |
| 87 | REPORTER_ASSERT(r, 6 == out2->d()); |
| 88 | |
Hal Canary | 342b7ac | 2016-11-04 11:49:42 -0400 | [diff] [blame] | 89 | sk_sp<IntFlattenable> out3((IntFlattenable*) readBuffer.readFlattenable( |
msarett | a3b3b23 | 2016-04-22 12:43:07 -0700 | [diff] [blame] | 90 | SkFlattenable::kSkUnused_Type)); |
| 91 | REPORTER_ASSERT(r, out3); |
| 92 | REPORTER_ASSERT(r, 4 == out3->a()); |
| 93 | REPORTER_ASSERT(r, 5 == out3->b()); |
| 94 | REPORTER_ASSERT(r, 6 == out3->c()); |
| 95 | REPORTER_ASSERT(r, 7 == out3->d()); |
| 96 | } |