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

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

Issue 2519583002: Add gfx::ImageSkia and icon support to aura::PropertyConverter. (Closed)
Patch Set: Fix ConvertPropertyForTransport; add 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/views/mus/native_widget_mus.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/aura/mus/property_converter.h" 5 #include "ui/aura/mus/property_converter.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 #include <string> 8 #include <string>
9 9
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/macros.h" 11 #include "base/macros.h"
12 #include "base/strings/utf_string_conversions.h" 12 #include "base/strings/utf_string_conversions.h"
13 #include "services/ui/public/cpp/property_type_converters.h" 13 #include "services/ui/public/cpp/property_type_converters.h"
14 #include "third_party/skia/include/core/SkBitmap.h"
14 #include "ui/aura/client/aura_constants.h" 15 #include "ui/aura/client/aura_constants.h"
15 #include "ui/aura/test/aura_test_base.h" 16 #include "ui/aura/test/aura_test_base.h"
16 #include "ui/aura/window.h" 17 #include "ui/aura/window.h"
17 #include "ui/aura/window_property.h" 18 #include "ui/aura/window_property.h"
18 #include "ui/gfx/geometry/rect.h" 19 #include "ui/gfx/geometry/rect.h"
20 #include "ui/gfx/image/image_skia.h"
21 #include "ui/gfx/skia_util.h"
19 22
20 // See aura_constants.cc for bool, int32_t, int64_t, std::string, gfx::Rect, 23 // See aura_constants.cc for bool, int32_t, int64_t, std::string, gfx::Rect,
21 // and base::string16. It also declares the uint32_t type via SkColor. 24 // base::string16, uint32_t (via SkColor), and gfx::ImageSkia.
22 DECLARE_WINDOW_PROPERTY_TYPE(uint8_t) 25 DECLARE_WINDOW_PROPERTY_TYPE(uint8_t)
23 DECLARE_WINDOW_PROPERTY_TYPE(uint16_t) 26 DECLARE_WINDOW_PROPERTY_TYPE(uint16_t)
24 DECLARE_WINDOW_PROPERTY_TYPE(uint64_t) 27 DECLARE_WINDOW_PROPERTY_TYPE(uint64_t)
25 DECLARE_WINDOW_PROPERTY_TYPE(int8_t) 28 DECLARE_WINDOW_PROPERTY_TYPE(int8_t)
26 DECLARE_WINDOW_PROPERTY_TYPE(int16_t) 29 DECLARE_WINDOW_PROPERTY_TYPE(int16_t)
27 30
28 namespace aura { 31 namespace aura {
29 32
30 namespace { 33 namespace {
31 34
32 DEFINE_WINDOW_PROPERTY_KEY(bool, kTestPropertyKey0, false); 35 DEFINE_WINDOW_PROPERTY_KEY(bool, kTestPropertyKey0, false);
33 DEFINE_WINDOW_PROPERTY_KEY(uint8_t, kTestPropertyKey1, 0); 36 DEFINE_WINDOW_PROPERTY_KEY(uint8_t, kTestPropertyKey1, 0);
34 DEFINE_WINDOW_PROPERTY_KEY(uint16_t, kTestPropertyKey2, 0); 37 DEFINE_WINDOW_PROPERTY_KEY(uint16_t, kTestPropertyKey2, 0);
35 DEFINE_WINDOW_PROPERTY_KEY(uint32_t, kTestPropertyKey3, 0); 38 DEFINE_WINDOW_PROPERTY_KEY(uint32_t, kTestPropertyKey3, 0);
36 DEFINE_WINDOW_PROPERTY_KEY(uint64_t, kTestPropertyKey4, 0); 39 DEFINE_WINDOW_PROPERTY_KEY(uint64_t, kTestPropertyKey4, 0);
37 DEFINE_WINDOW_PROPERTY_KEY(int8_t, kTestPropertyKey5, 0); 40 DEFINE_WINDOW_PROPERTY_KEY(int8_t, kTestPropertyKey5, 0);
38 DEFINE_WINDOW_PROPERTY_KEY(int16_t, kTestPropertyKey6, 0); 41 DEFINE_WINDOW_PROPERTY_KEY(int16_t, kTestPropertyKey6, 0);
39 DEFINE_WINDOW_PROPERTY_KEY(int32_t, kTestPropertyKey7, 0); 42 DEFINE_WINDOW_PROPERTY_KEY(int32_t, kTestPropertyKey7, 0);
40 DEFINE_WINDOW_PROPERTY_KEY(int64_t, kTestPropertyKey8, 0); 43 DEFINE_WINDOW_PROPERTY_KEY(int64_t, kTestPropertyKey8, 0);
41 44
45 DEFINE_OWNED_WINDOW_PROPERTY_KEY(gfx::ImageSkia, kTestImagePropertyKey,
46 nullptr);
42 DEFINE_OWNED_WINDOW_PROPERTY_KEY(gfx::Rect, kTestRectPropertyKey, nullptr); 47 DEFINE_OWNED_WINDOW_PROPERTY_KEY(gfx::Rect, kTestRectPropertyKey, nullptr);
43 DEFINE_OWNED_WINDOW_PROPERTY_KEY(std::string, kTestStringPropertyKey, nullptr); 48 DEFINE_OWNED_WINDOW_PROPERTY_KEY(std::string, kTestStringPropertyKey, nullptr);
44 DEFINE_OWNED_WINDOW_PROPERTY_KEY(base::string16, kTestString16PropertyKey, 49 DEFINE_OWNED_WINDOW_PROPERTY_KEY(base::string16, kTestString16PropertyKey,
45 nullptr); 50 nullptr);
46 51
47 const char kTestPropertyServerKey0[] = "test-property-server0"; 52 const char kTestPropertyServerKey0[] = "test-property-server0";
48 const char kTestPropertyServerKey1[] = "test-property-server1"; 53 const char kTestPropertyServerKey1[] = "test-property-server1";
49 const char kTestPropertyServerKey2[] = "test-property-server2"; 54 const char kTestPropertyServerKey2[] = "test-property-server2";
50 const char kTestPropertyServerKey3[] = "test-property-server3"; 55 const char kTestPropertyServerKey3[] = "test-property-server3";
51 const char kTestPropertyServerKey4[] = "test-property-server4"; 56 const char kTestPropertyServerKey4[] = "test-property-server4";
52 const char kTestPropertyServerKey5[] = "test-property-server5"; 57 const char kTestPropertyServerKey5[] = "test-property-server5";
53 const char kTestPropertyServerKey6[] = "test-property-server6"; 58 const char kTestPropertyServerKey6[] = "test-property-server6";
54 const char kTestPropertyServerKey7[] = "test-property-server7"; 59 const char kTestPropertyServerKey7[] = "test-property-server7";
55 const char kTestPropertyServerKey8[] = "test-property-server8"; 60 const char kTestPropertyServerKey8[] = "test-property-server8";
56 61
62 const char kTestImagePropertyServerKey[] = "test-image-property-server";
57 const char kTestRectPropertyServerKey[] = "test-rect-property-server"; 63 const char kTestRectPropertyServerKey[] = "test-rect-property-server";
58 const char kTestStringPropertyServerKey[] = "test-string-property-server"; 64 const char kTestStringPropertyServerKey[] = "test-string-property-server";
59 const char kTestString16PropertyServerKey[] = "test-string16-property-server"; 65 const char kTestString16PropertyServerKey[] = "test-string16-property-server";
60 66
61 // Test registration, naming and value conversion for primitive property types. 67 // Test registration, naming and value conversion for primitive property types.
62 template <typename T> 68 template <typename T>
63 void TestPrimitiveProperty(PropertyConverter* property_converter, 69 void TestPrimitiveProperty(PropertyConverter* property_converter,
64 Window* window, 70 Window* window,
65 const WindowProperty<T>* key, 71 const WindowProperty<T>* key,
66 const char* transport_name, 72 const char* transport_name,
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
130 136
131 const int32_t value_7a = INT32_MIN / 4, value_7b = INT32_MIN / 5; 137 const int32_t value_7a = INT32_MIN / 4, value_7b = INT32_MIN / 5;
132 TestPrimitiveProperty(&property_converter, window.get(), kTestPropertyKey7, 138 TestPrimitiveProperty(&property_converter, window.get(), kTestPropertyKey7,
133 kTestPropertyServerKey7, value_7a, value_7b); 139 kTestPropertyServerKey7, value_7a, value_7b);
134 140
135 const int64_t value_8a = INT64_MIN / 5, value_8b = INT64_MIN / 6; 141 const int64_t value_8a = INT64_MIN / 5, value_8b = INT64_MIN / 6;
136 TestPrimitiveProperty(&property_converter, window.get(), kTestPropertyKey8, 142 TestPrimitiveProperty(&property_converter, window.get(), kTestPropertyKey8,
137 kTestPropertyServerKey8, value_8a, value_8b); 143 kTestPropertyServerKey8, value_8a, value_8b);
138 } 144 }
139 145
146 // Verifies property setting behavior for a gfx::ImageSkia* property.
147 TEST_F(PropertyConverterTest, ImageSkiaProperty) {
148 PropertyConverter property_converter;
149 property_converter.RegisterProperty(kTestImagePropertyKey,
150 kTestImagePropertyServerKey);
151 EXPECT_EQ(
152 kTestImagePropertyServerKey,
153 property_converter.GetTransportNameForPropertyKey(kTestImagePropertyKey));
154
155 SkBitmap bitmap_1;
156 bitmap_1.allocN32Pixels(16, 32);
157 bitmap_1.eraseARGB(255, 11, 22, 33);
158 gfx::ImageSkia value_1 = gfx::ImageSkia::CreateFrom1xBitmap(bitmap_1);
159 std::unique_ptr<Window> window(CreateNormalWindow(1, root_window(), nullptr));
160 window->SetProperty(kTestImagePropertyKey, new gfx::ImageSkia(value_1));
161 gfx::ImageSkia* image_out_1 = window->GetProperty(kTestImagePropertyKey);
162 EXPECT_TRUE(gfx::BitmapsAreEqual(bitmap_1, *image_out_1->bitmap()));
163
164 std::string transport_name_out;
165 std::unique_ptr<std::vector<uint8_t>> transport_value_out;
166 EXPECT_TRUE(property_converter.ConvertPropertyForTransport(
167 window.get(), kTestImagePropertyKey, &transport_name_out,
168 &transport_value_out));
169 EXPECT_EQ(kTestImagePropertyServerKey, transport_name_out);
170 EXPECT_EQ(mojo::ConvertTo<std::vector<uint8_t>>(bitmap_1),
171 *transport_value_out.get());
172
173 SkBitmap bitmap_2;
174 bitmap_2.allocN32Pixels(16, 16);
175 bitmap_2.eraseARGB(255, 33, 22, 11);
176 EXPECT_FALSE(gfx::BitmapsAreEqual(bitmap_1, bitmap_2));
177 gfx::ImageSkia value_2 = gfx::ImageSkia::CreateFrom1xBitmap(bitmap_2);
178 std::vector<uint8_t> transport_value =
179 mojo::ConvertTo<std::vector<uint8_t>>(bitmap_2);
180 property_converter.SetPropertyFromTransportValue(
181 window.get(), kTestImagePropertyServerKey, &transport_value);
182 gfx::ImageSkia* image_out_2 = window->GetProperty(kTestImagePropertyKey);
183 EXPECT_TRUE(gfx::BitmapsAreEqual(bitmap_2, *image_out_2->bitmap()));
184 }
185
140 // Verifies property setting behavior for a gfx::Rect* property. 186 // Verifies property setting behavior for a gfx::Rect* property.
141 TEST_F(PropertyConverterTest, RectProperty) { 187 TEST_F(PropertyConverterTest, RectProperty) {
142 PropertyConverter property_converter; 188 PropertyConverter property_converter;
143 property_converter.RegisterProperty(kTestRectPropertyKey, 189 property_converter.RegisterProperty(kTestRectPropertyKey,
144 kTestRectPropertyServerKey); 190 kTestRectPropertyServerKey);
145 EXPECT_EQ( 191 EXPECT_EQ(
146 kTestRectPropertyServerKey, 192 kTestRectPropertyServerKey,
147 property_converter.GetTransportNameForPropertyKey(kTestRectPropertyKey)); 193 property_converter.GetTransportNameForPropertyKey(kTestRectPropertyKey));
148 194
149 gfx::Rect value_1(1, 2, 3, 4); 195 gfx::Rect value_1(1, 2, 3, 4);
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
224 270
225 base::string16 value_2 = base::ASCIIToUTF16("another test value"); 271 base::string16 value_2 = base::ASCIIToUTF16("another test value");
226 std::vector<uint8_t> transport_value = 272 std::vector<uint8_t> transport_value =
227 mojo::ConvertTo<std::vector<uint8_t>>(value_2); 273 mojo::ConvertTo<std::vector<uint8_t>>(value_2);
228 property_converter.SetPropertyFromTransportValue( 274 property_converter.SetPropertyFromTransportValue(
229 window.get(), kTestString16PropertyServerKey, &transport_value); 275 window.get(), kTestString16PropertyServerKey, &transport_value);
230 EXPECT_EQ(value_2, *window->GetProperty(kTestString16PropertyKey)); 276 EXPECT_EQ(value_2, *window->GetProperty(kTestString16PropertyKey));
231 } 277 }
232 278
233 } // namespace aura 279 } // namespace aura
OLDNEW
« no previous file with comments | « ui/aura/mus/property_converter.cc ('k') | ui/views/mus/native_widget_mus.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698