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

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

Issue 2499933003: Expand aura::PropertyConverter support. (Closed)
Patch Set: Fix the other new unit test. 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
« no previous file with comments | « ui/aura/mus/property_converter.cc ('k') | ui/aura/mus/window_tree_client_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 // See aura_constants.cc for bool, int32_t, int64_t, std::string, and gfx::Rect.
19 // That same file also declares the uint32_t type via SkColor.
20 DECLARE_WINDOW_PROPERTY_TYPE(uint8_t)
21 DECLARE_WINDOW_PROPERTY_TYPE(uint16_t)
22 DECLARE_WINDOW_PROPERTY_TYPE(uint64_t)
23 DECLARE_WINDOW_PROPERTY_TYPE(int8_t)
24 DECLARE_WINDOW_PROPERTY_TYPE(int16_t)
25
26 namespace aura {
27
28 namespace {
29
30 DEFINE_WINDOW_PROPERTY_KEY(bool, kTestPropertyKey0, false);
31 DEFINE_WINDOW_PROPERTY_KEY(uint8_t, kTestPropertyKey1, 0);
32 DEFINE_WINDOW_PROPERTY_KEY(uint16_t, kTestPropertyKey2, 0);
33 DEFINE_WINDOW_PROPERTY_KEY(uint32_t, kTestPropertyKey3, 0);
34 DEFINE_WINDOW_PROPERTY_KEY(uint64_t, kTestPropertyKey4, 0);
35 DEFINE_WINDOW_PROPERTY_KEY(int8_t, kTestPropertyKey5, 0);
36 DEFINE_WINDOW_PROPERTY_KEY(int16_t, kTestPropertyKey6, 0);
37 DEFINE_WINDOW_PROPERTY_KEY(int32_t, kTestPropertyKey7, 0);
38 DEFINE_WINDOW_PROPERTY_KEY(int64_t, kTestPropertyKey8, 0);
39
40 DEFINE_WINDOW_PROPERTY_KEY(gfx::Rect*, kTestRectPropertyKey, nullptr);
41 DEFINE_WINDOW_PROPERTY_KEY(std::string*, kTestStringPropertyKey, nullptr);
42
43 const char kTestPropertyServerKey0[] = "test-property-server0";
44 const char kTestPropertyServerKey1[] = "test-property-server1";
45 const char kTestPropertyServerKey2[] = "test-property-server2";
46 const char kTestPropertyServerKey3[] = "test-property-server3";
47 const char kTestPropertyServerKey4[] = "test-property-server4";
48 const char kTestPropertyServerKey5[] = "test-property-server5";
49 const char kTestPropertyServerKey6[] = "test-property-server6";
50 const char kTestPropertyServerKey7[] = "test-property-server7";
51 const char kTestPropertyServerKey8[] = "test-property-server8";
52
53 const char kTestRectPropertyServerKey[] = "test-rect-property-server";
54 const char kTestStringPropertyServerKey[] = "test-string-property-server";
55
56 // Test registration, naming and value conversion for primitive property types.
57 template <typename T>
58 void TestPrimitiveProperty(PropertyConverter* property_converter,
59 Window* window,
60 const WindowProperty<T>* key,
61 const char* transport_name,
62 T value_1,
63 T value_2) {
64 property_converter->RegisterProperty(key, transport_name);
65 EXPECT_EQ(transport_name,
66 property_converter->GetTransportNameForPropertyKey(key));
67
68 window->SetProperty(key, value_1);
69 EXPECT_EQ(value_1, window->GetProperty(key));
70
71 std::string transport_name_out;
72 std::unique_ptr<std::vector<uint8_t>> transport_value_out;
73 EXPECT_TRUE(property_converter->ConvertPropertyForTransport(
74 window, key, &transport_name_out, &transport_value_out));
75 EXPECT_EQ(transport_name, transport_name_out);
76 const int64_t storage_value_1 = static_cast<int64_t>(value_1);
77 std::vector<uint8_t> transport_value1 =
78 mojo::ConvertTo<std::vector<uint8_t>>(storage_value_1);
79 EXPECT_EQ(transport_value1, *transport_value_out.get());
80
81 const int64_t storage_value_2 = static_cast<int64_t>(value_2);
82 std::vector<uint8_t> transport_value2 =
83 mojo::ConvertTo<std::vector<uint8_t>>(storage_value_2);
84 property_converter->SetPropertyFromTransportValue(window, transport_name,
85 &transport_value2);
86 EXPECT_EQ(value_2, window->GetProperty(key));
87 }
88
89 } // namespace
90
91 using PropertyConverterTest = test::AuraTestBase;
92
93 // Verifies property setting behavior for a std::string* property.
94 TEST_F(PropertyConverterTest, PrimitiveProperties) {
95 PropertyConverter property_converter;
96 std::unique_ptr<Window> window(CreateNormalWindow(1, root_window(), nullptr));
97
98 const bool value_0a = true, value_0b = false;
99 TestPrimitiveProperty(&property_converter, window.get(), kTestPropertyKey0,
100 kTestPropertyServerKey0, value_0a, value_0b);
101
102 const uint8_t value_1a = UINT8_MAX / 2, value_1b = UINT8_MAX / 3;
103 TestPrimitiveProperty(&property_converter, window.get(), kTestPropertyKey1,
104 kTestPropertyServerKey1, value_1a, value_1b);
105
106 const uint16_t value_2a = UINT16_MAX / 3, value_2b = UINT16_MAX / 4;
107 TestPrimitiveProperty(&property_converter, window.get(), kTestPropertyKey2,
108 kTestPropertyServerKey2, value_2a, value_2b);
109
110 const uint32_t value_3a = UINT32_MAX / 4, value_3b = UINT32_MAX / 5;
111 TestPrimitiveProperty(&property_converter, window.get(), kTestPropertyKey3,
112 kTestPropertyServerKey3, value_3a, value_3b);
113
114 const uint64_t value_4a = UINT64_MAX / 5, value_4b = UINT64_MAX / 6;
115 TestPrimitiveProperty(&property_converter, window.get(), kTestPropertyKey4,
116 kTestPropertyServerKey4, value_4a, value_4b);
117
118 const int8_t value_5a = INT8_MIN / 2, value_5b = INT8_MIN / 3;
119 TestPrimitiveProperty(&property_converter, window.get(), kTestPropertyKey5,
120 kTestPropertyServerKey5, value_5a, value_5b);
121
122 const int16_t value_6a = INT16_MIN / 3, value_6b = INT16_MIN / 4;
123 TestPrimitiveProperty(&property_converter, window.get(), kTestPropertyKey6,
124 kTestPropertyServerKey6, value_6a, value_6b);
125
126 const int32_t value_7a = INT32_MIN / 4, value_7b = INT32_MIN / 5;
127 TestPrimitiveProperty(&property_converter, window.get(), kTestPropertyKey7,
128 kTestPropertyServerKey7, value_7a, value_7b);
129
130 const int64_t value_8a = INT64_MIN / 5, value_8b = INT64_MIN / 6;
131 TestPrimitiveProperty(&property_converter, window.get(), kTestPropertyKey8,
132 kTestPropertyServerKey8, value_8a, value_8b);
133 }
134
135 // Verifies property setting behavior for a gfx::Rect* property.
136 TEST_F(PropertyConverterTest, RectProperty) {
137 PropertyConverter property_converter;
138 property_converter.RegisterProperty(kTestRectPropertyKey,
139 kTestRectPropertyServerKey);
140 EXPECT_EQ(
141 kTestRectPropertyServerKey,
142 property_converter.GetTransportNameForPropertyKey(kTestRectPropertyKey));
143
144 gfx::Rect value_1(1, 2, 3, 4);
145 std::unique_ptr<Window> window(CreateNormalWindow(1, root_window(), nullptr));
146 window->SetProperty(kTestRectPropertyKey, new gfx::Rect(value_1));
147 EXPECT_EQ(value_1, *window->GetProperty(kTestRectPropertyKey));
148
149 std::string transport_name_out;
150 std::unique_ptr<std::vector<uint8_t>> transport_value_out;
151 EXPECT_TRUE(property_converter.ConvertPropertyForTransport(
152 window.get(), kTestRectPropertyKey, &transport_name_out,
153 &transport_value_out));
154 EXPECT_EQ(kTestRectPropertyServerKey, transport_name_out);
155 EXPECT_EQ(mojo::ConvertTo<std::vector<uint8_t>>(value_1),
156 *transport_value_out.get());
157
158 gfx::Rect value_2(1, 3, 5, 7);
159 std::vector<uint8_t> transport_value =
160 mojo::ConvertTo<std::vector<uint8_t>>(value_2);
161 property_converter.SetPropertyFromTransportValue(
162 window.get(), kTestRectPropertyServerKey, &transport_value);
163 EXPECT_EQ(value_2, *window->GetProperty(kTestRectPropertyKey));
164 }
165
166 // Verifies property setting behavior for a std::string* property.
167 TEST_F(PropertyConverterTest, StringProperty) {
168 PropertyConverter property_converter;
169 property_converter.RegisterProperty(kTestStringPropertyKey,
170 kTestStringPropertyServerKey);
171 EXPECT_EQ(kTestStringPropertyServerKey,
172 property_converter.GetTransportNameForPropertyKey(
173 kTestStringPropertyKey));
174
175 std::string value_1 = "test value";
176 std::unique_ptr<Window> window(CreateNormalWindow(1, root_window(), nullptr));
177 window->SetProperty(kTestStringPropertyKey, new std::string(value_1));
178 EXPECT_EQ(value_1, *window->GetProperty(kTestStringPropertyKey));
179
180 std::string transport_name_out;
181 std::unique_ptr<std::vector<uint8_t>> transport_value_out;
182 EXPECT_TRUE(property_converter.ConvertPropertyForTransport(
183 window.get(), kTestStringPropertyKey, &transport_name_out,
184 &transport_value_out));
185 EXPECT_EQ(kTestStringPropertyServerKey, transport_name_out);
186 EXPECT_EQ(mojo::ConvertTo<std::vector<uint8_t>>(value_1),
187 *transport_value_out.get());
188
189 std::string value_2 = "another test value";
190 std::vector<uint8_t> transport_value =
191 mojo::ConvertTo<std::vector<uint8_t>>(value_2);
192 property_converter.SetPropertyFromTransportValue(
193 window.get(), kTestStringPropertyServerKey, &transport_value);
194 EXPECT_EQ(value_2, *window->GetProperty(kTestStringPropertyKey));
195 }
196
197 } // namespace aura
OLDNEW
« no previous file with comments | « ui/aura/mus/property_converter.cc ('k') | ui/aura/mus/window_tree_client_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698