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 "SkData.h" | |
9 #include "SkMatrix.h" | |
10 #include "SkToFromValue.h" | |
11 #include "Test.h" | |
12 | |
13 static const SkValue::Type example_type = | |
14 SkValue::Type(SkValue::kMaxPublicObject + 1); | |
15 | |
16 enum { kExampleS32, kExampleF32, kExampleU32, kExampleObject, kExampleBytes, | |
17 kExampleF32s, kExampleU32s, kExampleU16s, kExampleArray }; | |
18 | |
19 static const uint16_t aU16[] = { 1, 2, 3, 4 }; | |
20 static const uint32_t aU32[] = { 5, 6, 7, 8 }; | |
21 static const float aF32[] = { 9.0f, 9.125f, 9.25f }; | |
22 static const char hello[] = "HELLO"; | |
23 | |
24 static SkValue make_example(skiatest::Reporter* r, int level = 4) { | |
25 auto value = SkValue::Object(example_type); | |
26 value.set(kExampleU32, SkValue::FromU32(1000)); | |
27 value.set(kExampleS32, SkValue::FromS32(-123)); | |
28 value.set(kExampleF32, SkValue::FromF32(0.5f)); | |
29 value.set(kExampleU32, SkValue::FromU32(1234)); | |
30 if (level > 0) { | |
31 value.set(kExampleObject, make_example(r, 0)); | |
32 value.set(kExampleObject, make_example(r, level - 1)); // replace | |
33 } | |
34 SkAutoTUnref<SkData> data(SkData::NewWithCString(hello)); | |
35 value.set(kExampleBytes, SkValue::FromBytes(data)); | |
36 | |
37 SkAutoTUnref<SkData> dataU16(SkData::NewWithCopy(aU16, sizeof(aU16))); | |
38 SkAutoTUnref<SkData> dataU32(SkData::NewWithCopy(aU32, sizeof(aU32))); | |
39 SkAutoTUnref<SkData> dataF32(SkData::NewWithCopy(aF32, sizeof(aF32))); | |
40 value.set(kExampleU16s, SkValue::FromU16s(dataU16)); | |
41 value.set(kExampleU32s, SkValue::FromU32s(dataU32)); | |
42 value.set(kExampleF32s, SkValue::FromF32s(dataF32)); | |
43 | |
44 auto varray = SkValue::ValueArray(); | |
45 varray.append(SkValue::FromU32(99)); | |
46 varray.append(SkValue::FromS32(-99)); | |
47 value.set(kExampleArray, std::move(varray)); | |
48 return value; | |
49 } | |
50 | |
51 DEF_TEST(Value, r) { | |
52 SkValue val = make_example(r); | |
53 REPORTER_ASSERT(r, example_type == val.type()); | |
54 SkValue valCopy = val; | |
55 REPORTER_ASSERT(r, example_type == valCopy.type()); | |
56 valCopy.set(4321, SkValue()); | |
57 auto fn = [&](SkValue::Key k, const SkValue& v){ | |
58 int count; | |
59 switch (k) { | |
60 case kExampleS32: | |
61 REPORTER_ASSERT(r, -123 == v.s32()); | |
62 break; | |
63 case kExampleF32: | |
64 REPORTER_ASSERT(r, 0.5f == v.f32()); | |
65 break; | |
66 case kExampleU32: | |
67 REPORTER_ASSERT(r, 1234 == v.u32()); | |
68 break; | |
69 case kExampleObject: | |
70 REPORTER_ASSERT(r, example_type == v.type()); | |
71 break; | |
72 case kExampleBytes: | |
73 REPORTER_ASSERT(r, v.type() == SkValue::Bytes && v.bytes() | |
74 && v.bytes()->size() == sizeof(hello)); | |
75 break; | |
76 case kExampleF32s: | |
77 REPORTER_ASSERT(r, v.type() == SkValue::F32s && v.bytes() | |
78 && v.bytes()->size() == sizeof(aF32) | |
79 && v.f32s(&count) | |
80 && count == SK_ARRAY_COUNT(aF32)); | |
81 break; | |
82 case kExampleU32s: | |
83 REPORTER_ASSERT(r, v.type() == SkValue::U32s && v.bytes() | |
84 && v.bytes()->size() == sizeof(aU32) | |
85 && v.u32s(&count) | |
86 && count == SK_ARRAY_COUNT(aU32)); | |
87 break; | |
88 case kExampleU16s: | |
89 REPORTER_ASSERT(r, v.type() == SkValue::U16s && v.bytes() | |
90 && v.bytes()->size() == sizeof(aU16) | |
91 && v.u16s(&count) | |
92 && count == SK_ARRAY_COUNT(aU16)); | |
93 break; | |
94 case kExampleArray: | |
95 REPORTER_ASSERT(r, v.type() == SkValue::Array | |
96 && v.length() == 2); | |
97 break; | |
98 default: | |
99 ERRORF(r, "unexpected key"); | |
100 } | |
101 }; | |
102 val.foreach(fn); | |
103 } | |
104 | |
105 DEF_TEST(Value_Matrix, r) { | |
106 auto m = SkMatrix::MakeTrans(900.0f, 1000.0f); | |
107 auto val = SkToValue(m); | |
108 SkMatrix dst; | |
109 REPORTER_ASSERT(r, SkFromValue(val, &dst)); | |
110 REPORTER_ASSERT(r, dst == m); | |
111 } | |
OLD | NEW |