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

Side by Side Diff: ui/aura/mus/property_converter_unittest.cc

Issue 2499933003: Expand aura::PropertyConverter support. (Closed)
Patch Set: Revert to std::string. Created 4 years, 1 month 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "ui/aura/mus/property_converter.h"
6
7 #include <stdint.h>
8 #include <string>
9
10 #include "base/logging.h"
11 #include "base/macros.h"
12 #include "services/ui/public/cpp/property_type_converters.h"
13 #include "ui/aura/test/aura_test_base.h"
14 #include "ui/aura/window.h"
15 #include "ui/aura/window_property.h"
16 #include "ui/gfx/geometry/rect.h"
17
18 DECLARE_WINDOW_PROPERTY_TYPE(uint8_t)
19 DECLARE_WINDOW_PROPERTY_TYPE(uint16_t)
20 DECLARE_WINDOW_PROPERTY_TYPE(uint32_t)
21 DECLARE_WINDOW_PROPERTY_TYPE(uint64_t)
22 DECLARE_WINDOW_PROPERTY_TYPE(int8_t)
23 DECLARE_WINDOW_PROPERTY_TYPE(int16_t)
24 DECLARE_WINDOW_PROPERTY_TYPE(int32_t)
25 DECLARE_WINDOW_PROPERTY_TYPE(int64_t)
26
27 namespace aura {
28
29 namespace {
30
31 DEFINE_WINDOW_PROPERTY_KEY(bool, kTestPropertyKey0, false);
32 DEFINE_WINDOW_PROPERTY_KEY(uint8_t, kTestPropertyKey1, 0);
33 DEFINE_WINDOW_PROPERTY_KEY(uint16_t, kTestPropertyKey2, 0);
34 DEFINE_WINDOW_PROPERTY_KEY(uint32_t, kTestPropertyKey3, 0);
35 DEFINE_WINDOW_PROPERTY_KEY(uint64_t, kTestPropertyKey4, 0);
36 DEFINE_WINDOW_PROPERTY_KEY(int8_t, kTestPropertyKey5, 0);
37 DEFINE_WINDOW_PROPERTY_KEY(int16_t, kTestPropertyKey6, 0);
38 DEFINE_WINDOW_PROPERTY_KEY(int32_t, kTestPropertyKey7, 0);
39 DEFINE_WINDOW_PROPERTY_KEY(int64_t, kTestPropertyKey8, 0);
40
41 DEFINE_WINDOW_PROPERTY_KEY(gfx::Rect*, kTestRectPropertyKey, nullptr);
42 DEFINE_WINDOW_PROPERTY_KEY(std::string*, kTestStringPropertyKey, nullptr);
43
44 const char kTestPropertyServerKey0[] = "test-property-server0";
45 const char kTestPropertyServerKey1[] = "test-property-server1";
46 const char kTestPropertyServerKey2[] = "test-property-server2";
47 const char kTestPropertyServerKey3[] = "test-property-server3";
48 const char kTestPropertyServerKey4[] = "test-property-server4";
49 const char kTestPropertyServerKey5[] = "test-property-server5";
50 const char kTestPropertyServerKey6[] = "test-property-server6";
51 const char kTestPropertyServerKey7[] = "test-property-server7";
52 const char kTestPropertyServerKey8[] = "test-property-server8";
53
54 const char kTestRectPropertyServerKey[] = "test-rect-property-server";
55 const char kTestStringPropertyServerKey[] = "test-string-property-server";
56
57 mojo::Array<uint8_t> GetPropertyTransportValue(int64_t value) {
58 mojo::Array<uint8_t> transport_value(8);
59 transport_value[7] = (value >> 56) & 0xFF;
60 transport_value[6] = (value >> 48) & 0xFF;
61 transport_value[5] = (value >> 40) & 0xFF;
62 transport_value[4] = (value >> 32) & 0xFF;
63 transport_value[3] = (value >> 24) & 0xFF;
64 transport_value[2] = (value >> 16) & 0xFF;
65 transport_value[1] = (value >> 8) & 0xFF;
66 transport_value[0] = value & 0xFF;
67 return transport_value;
68 }
69
70 // Test registration, naming and value conversion for primitive property types.
71 template <typename T>
72 void TestPrimitiveProperty(PropertyConverter* property_converter,
73 Window* window,
74 const WindowProperty<T>* key,
75 const char* transport_name,
76 T value_1,
77 T value_2) {
78 property_converter->RegisterPrimitiveProperty(key, key->name, transport_name);
79 EXPECT_EQ(transport_name,
80 property_converter->GetTransportNameForPropertyKey(key));
81
82 window->SetProperty(key, value_1);
83 EXPECT_EQ(value_1, window->GetProperty(key));
84
85 std::string transport_name_out;
86 std::unique_ptr<std::vector<uint8_t>> transport_value_out;
87 EXPECT_TRUE(property_converter->ConvertPropertyForTransport(
88 window, key, &transport_name_out, &transport_value_out));
89 EXPECT_EQ(transport_name, transport_name_out);
90 std::vector<uint8_t> transport_value1 = GetPropertyTransportValue(value_1);
91 EXPECT_EQ(transport_value1, *transport_value_out.get());
92
93 std::vector<uint8_t> transport_value2 = GetPropertyTransportValue(value_2);
94 property_converter->SetPropertyFromTransportValue(window, transport_name,
95 &transport_value2);
96 EXPECT_EQ(value_2, window->GetProperty(key));
97 }
98
99 } // namespace
100
101 using PropertyConverterTest = test::AuraTestBase;
102
103 // Verifies property setting behavior for a std::string* property.
104 TEST_F(PropertyConverterTest, PrimitiveProperties) {
105 PropertyConverter property_converter;
106 std::unique_ptr<Window> window(CreateNormalWindow(1, root_window(), nullptr));
107
108 const bool value_0a = true, value_0b = false;
109 TestPrimitiveProperty(&property_converter, window.get(), kTestPropertyKey0,
110 kTestPropertyServerKey0, value_0a, value_0b);
111
112 const uint8_t value_1a = UINT8_MAX / 2, value_1b = UINT8_MAX / 3;
113 TestPrimitiveProperty(&property_converter, window.get(), kTestPropertyKey1,
114 kTestPropertyServerKey1, value_1a, value_1b);
115
116 const uint16_t value_2a = UINT16_MAX / 3, value_2b = UINT16_MAX / 4;
117 TestPrimitiveProperty(&property_converter, window.get(), kTestPropertyKey2,
118 kTestPropertyServerKey2, value_2a, value_2b);
119
120 const uint32_t value_3a = UINT32_MAX / 4, value_3b = UINT32_MAX / 5;
121 TestPrimitiveProperty(&property_converter, window.get(), kTestPropertyKey3,
122 kTestPropertyServerKey3, value_3a, value_3b);
123
124 const uint64_t value_4a = UINT64_MAX / 5, value_4b = UINT64_MAX / 6;
125 TestPrimitiveProperty(&property_converter, window.get(), kTestPropertyKey4,
126 kTestPropertyServerKey4, value_4a, value_4b);
127
128 const int8_t value_5a = INT8_MIN / 2, value_5b = INT8_MIN / 3;
129 TestPrimitiveProperty(&property_converter, window.get(), kTestPropertyKey5,
130 kTestPropertyServerKey5, value_5a, value_5b);
131
132 const int16_t value_6a = INT16_MIN / 3, value_6b = INT16_MIN / 4;
133 TestPrimitiveProperty(&property_converter, window.get(), kTestPropertyKey6,
134 kTestPropertyServerKey6, value_6a, value_6b);
135
136 const int32_t value_7a = INT32_MIN / 4, value_7b = INT32_MIN / 5;
137 TestPrimitiveProperty(&property_converter, window.get(), kTestPropertyKey7,
138 kTestPropertyServerKey7, value_7a, value_7b);
139
140 const int64_t value_8a = INT64_MIN / 5, value_8b = INT64_MIN / 6;
141 TestPrimitiveProperty(&property_converter, window.get(), kTestPropertyKey8,
142 kTestPropertyServerKey8, value_8a, value_8b);
143 }
144
145 // Verifies property setting behavior for a gfx::Rect* property.
146 TEST_F(PropertyConverterTest, RectProperty) {
147 PropertyConverter property_converter;
148 property_converter.RegisterRectProperty(kTestRectPropertyKey,
149 kTestRectPropertyServerKey);
150 EXPECT_EQ(
151 kTestRectPropertyServerKey,
152 property_converter.GetTransportNameForPropertyKey(kTestRectPropertyKey));
153
154 gfx::Rect value_1(1, 2, 3, 4);
155 std::unique_ptr<Window> window(CreateNormalWindow(1, root_window(), nullptr));
156 window->SetProperty(kTestRectPropertyKey, new gfx::Rect(value_1));
157 EXPECT_EQ(value_1, *window->GetProperty(kTestRectPropertyKey));
158
159 std::string transport_name_out;
160 std::unique_ptr<std::vector<uint8_t>> transport_value_out;
161 EXPECT_TRUE(property_converter.ConvertPropertyForTransport(
162 window.get(), kTestRectPropertyKey, &transport_name_out,
163 &transport_value_out));
164 EXPECT_EQ(kTestRectPropertyServerKey, transport_name_out);
165 EXPECT_EQ(mojo::ConvertTo<std::vector<uint8_t>>(value_1),
166 *transport_value_out.get());
167
168 gfx::Rect value_2(1, 3, 5, 7);
169 std::vector<uint8_t> transport_value =
170 mojo::ConvertTo<std::vector<uint8_t>>(value_2);
171 property_converter.SetPropertyFromTransportValue(
172 window.get(), kTestRectPropertyServerKey, &transport_value);
173 EXPECT_EQ(value_2, *window->GetProperty(kTestRectPropertyKey));
174 }
175
176 // Verifies property setting behavior for a std::string* property.
177 TEST_F(PropertyConverterTest, StringProperty) {
178 PropertyConverter property_converter;
179 property_converter.RegisterStringProperty(kTestStringPropertyKey,
180 kTestStringPropertyServerKey);
181 EXPECT_EQ(kTestStringPropertyServerKey,
182 property_converter.GetTransportNameForPropertyKey(
183 kTestStringPropertyKey));
184
185 std::string value_1 = "test value";
186 std::unique_ptr<Window> window(CreateNormalWindow(1, root_window(), nullptr));
187 window->SetProperty(kTestStringPropertyKey, new std::string(value_1));
188 EXPECT_EQ(value_1, *window->GetProperty(kTestStringPropertyKey));
189
190 std::string transport_name_out;
191 std::unique_ptr<std::vector<uint8_t>> transport_value_out;
192 EXPECT_TRUE(property_converter.ConvertPropertyForTransport(
193 window.get(), kTestStringPropertyKey, &transport_name_out,
194 &transport_value_out));
195 EXPECT_EQ(kTestStringPropertyServerKey, transport_name_out);
196 EXPECT_EQ(mojo::ConvertTo<std::vector<uint8_t>>(value_1),
197 *transport_value_out.get());
198
199 std::string value_2 = "another test value";
200 std::vector<uint8_t> transport_value =
201 mojo::ConvertTo<std::vector<uint8_t>>(value_2);
202 property_converter.SetPropertyFromTransportValue(
203 window.get(), kTestStringPropertyServerKey, &transport_value);
204 EXPECT_EQ(value_2, *window->GetProperty(kTestStringPropertyKey));
205 }
206
207 } // namespace aura
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698