| OLD | NEW |
| 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" |
| (...skipping 27 matching lines...) Expand all Loading... |
| 38 DEFINE_WINDOW_PROPERTY_KEY(uint32_t, kTestPropertyKey3, 0); | 38 DEFINE_WINDOW_PROPERTY_KEY(uint32_t, kTestPropertyKey3, 0); |
| 39 DEFINE_WINDOW_PROPERTY_KEY(uint64_t, kTestPropertyKey4, 0); | 39 DEFINE_WINDOW_PROPERTY_KEY(uint64_t, kTestPropertyKey4, 0); |
| 40 DEFINE_WINDOW_PROPERTY_KEY(int8_t, kTestPropertyKey5, 0); | 40 DEFINE_WINDOW_PROPERTY_KEY(int8_t, kTestPropertyKey5, 0); |
| 41 DEFINE_WINDOW_PROPERTY_KEY(int16_t, kTestPropertyKey6, 0); | 41 DEFINE_WINDOW_PROPERTY_KEY(int16_t, kTestPropertyKey6, 0); |
| 42 DEFINE_WINDOW_PROPERTY_KEY(int32_t, kTestPropertyKey7, 0); | 42 DEFINE_WINDOW_PROPERTY_KEY(int32_t, kTestPropertyKey7, 0); |
| 43 DEFINE_WINDOW_PROPERTY_KEY(int64_t, kTestPropertyKey8, 0); | 43 DEFINE_WINDOW_PROPERTY_KEY(int64_t, kTestPropertyKey8, 0); |
| 44 | 44 |
| 45 DEFINE_OWNED_WINDOW_PROPERTY_KEY(gfx::ImageSkia, kTestImagePropertyKey, | 45 DEFINE_OWNED_WINDOW_PROPERTY_KEY(gfx::ImageSkia, kTestImagePropertyKey, |
| 46 nullptr); | 46 nullptr); |
| 47 DEFINE_OWNED_WINDOW_PROPERTY_KEY(gfx::Rect, kTestRectPropertyKey, nullptr); | 47 DEFINE_OWNED_WINDOW_PROPERTY_KEY(gfx::Rect, kTestRectPropertyKey, nullptr); |
| 48 DEFINE_OWNED_WINDOW_PROPERTY_KEY(gfx::Size, kTestSizePropertyKey, nullptr); |
| 48 DEFINE_OWNED_WINDOW_PROPERTY_KEY(std::string, kTestStringPropertyKey, nullptr); | 49 DEFINE_OWNED_WINDOW_PROPERTY_KEY(std::string, kTestStringPropertyKey, nullptr); |
| 49 DEFINE_OWNED_WINDOW_PROPERTY_KEY(base::string16, kTestString16PropertyKey, | 50 DEFINE_OWNED_WINDOW_PROPERTY_KEY(base::string16, kTestString16PropertyKey, |
| 50 nullptr); | 51 nullptr); |
| 51 | 52 |
| 52 const char kTestPropertyServerKey0[] = "test-property-server0"; | 53 const char kTestPropertyServerKey0[] = "test-property-server0"; |
| 53 const char kTestPropertyServerKey1[] = "test-property-server1"; | 54 const char kTestPropertyServerKey1[] = "test-property-server1"; |
| 54 const char kTestPropertyServerKey2[] = "test-property-server2"; | 55 const char kTestPropertyServerKey2[] = "test-property-server2"; |
| 55 const char kTestPropertyServerKey3[] = "test-property-server3"; | 56 const char kTestPropertyServerKey3[] = "test-property-server3"; |
| 56 const char kTestPropertyServerKey4[] = "test-property-server4"; | 57 const char kTestPropertyServerKey4[] = "test-property-server4"; |
| 57 const char kTestPropertyServerKey5[] = "test-property-server5"; | 58 const char kTestPropertyServerKey5[] = "test-property-server5"; |
| 58 const char kTestPropertyServerKey6[] = "test-property-server6"; | 59 const char kTestPropertyServerKey6[] = "test-property-server6"; |
| 59 const char kTestPropertyServerKey7[] = "test-property-server7"; | 60 const char kTestPropertyServerKey7[] = "test-property-server7"; |
| 60 const char kTestPropertyServerKey8[] = "test-property-server8"; | 61 const char kTestPropertyServerKey8[] = "test-property-server8"; |
| 61 | 62 |
| 62 const char kTestImagePropertyServerKey[] = "test-image-property-server"; | 63 const char kTestImagePropertyServerKey[] = "test-image-property-server"; |
| 63 const char kTestRectPropertyServerKey[] = "test-rect-property-server"; | 64 const char kTestRectPropertyServerKey[] = "test-rect-property-server"; |
| 65 const char kTestSizePropertyServerKey[] = "test-size-property-server"; |
| 64 const char kTestStringPropertyServerKey[] = "test-string-property-server"; | 66 const char kTestStringPropertyServerKey[] = "test-string-property-server"; |
| 65 const char kTestString16PropertyServerKey[] = "test-string16-property-server"; | 67 const char kTestString16PropertyServerKey[] = "test-string16-property-server"; |
| 66 | 68 |
| 67 // Test registration, naming and value conversion for primitive property types. | 69 // Test registration, naming and value conversion for primitive property types. |
| 68 template <typename T> | 70 template <typename T> |
| 69 void TestPrimitiveProperty(PropertyConverter* property_converter, | 71 void TestPrimitiveProperty(PropertyConverter* property_converter, |
| 70 Window* window, | 72 Window* window, |
| 71 const WindowProperty<T>* key, | 73 const WindowProperty<T>* key, |
| 72 const char* transport_name, | 74 const char* transport_name, |
| 73 T value_1, | 75 T value_1, |
| 74 T value_2) { | 76 T value_2) { |
| 75 property_converter->RegisterProperty(key, transport_name); | 77 property_converter->RegisterProperty(key, transport_name); |
| 76 EXPECT_EQ(transport_name, | 78 EXPECT_EQ(transport_name, |
| 77 property_converter->GetTransportNameForPropertyKey(key)); | 79 property_converter->GetTransportNameForPropertyKey(key)); |
| 78 | 80 |
| 79 window->SetProperty(key, value_1); | 81 window->SetProperty(key, value_1); |
| 80 EXPECT_EQ(value_1, window->GetProperty(key)); | 82 EXPECT_EQ(value_1, window->GetProperty(key)); |
| 81 | 83 |
| 82 std::string transport_name_out; | 84 std::string transport_name_out; |
| 83 std::unique_ptr<std::vector<uint8_t>> transport_value_out; | 85 std::unique_ptr<std::vector<uint8_t>> transport_value_out; |
| 84 EXPECT_TRUE(property_converter->ConvertPropertyForTransport( | 86 EXPECT_TRUE(property_converter->ConvertPropertyForTransport( |
| 85 window, key, &transport_name_out, &transport_value_out)); | 87 window, key, &transport_name_out, &transport_value_out)); |
| 86 EXPECT_EQ(transport_name, transport_name_out); | 88 EXPECT_EQ(transport_name, transport_name_out); |
| 87 const int64_t storage_value_1 = static_cast<int64_t>(value_1); | 89 const int64_t storage_value_1 = static_cast<int64_t>(value_1); |
| 88 std::vector<uint8_t> transport_value1 = | 90 std::vector<uint8_t> transport_value1 = |
| 89 mojo::ConvertTo<std::vector<uint8_t>>(storage_value_1); | 91 mojo::ConvertTo<std::vector<uint8_t>>(storage_value_1); |
| 90 EXPECT_EQ(transport_value1, *transport_value_out.get()); | 92 EXPECT_EQ(transport_value1, *transport_value_out.get()); |
| 91 | 93 |
| 94 int64_t decoded_value_1 = 0; |
| 95 EXPECT_TRUE(property_converter->GetPropertyValueFromTransportValue( |
| 96 transport_name, *transport_value_out, &decoded_value_1)); |
| 97 EXPECT_EQ(value_1, static_cast<T>(decoded_value_1)); |
| 98 |
| 92 const int64_t storage_value_2 = static_cast<int64_t>(value_2); | 99 const int64_t storage_value_2 = static_cast<int64_t>(value_2); |
| 93 std::vector<uint8_t> transport_value2 = | 100 std::vector<uint8_t> transport_value2 = |
| 94 mojo::ConvertTo<std::vector<uint8_t>>(storage_value_2); | 101 mojo::ConvertTo<std::vector<uint8_t>>(storage_value_2); |
| 95 property_converter->SetPropertyFromTransportValue(window, transport_name, | 102 property_converter->SetPropertyFromTransportValue(window, transport_name, |
| 96 &transport_value2); | 103 &transport_value2); |
| 97 EXPECT_EQ(value_2, window->GetProperty(key)); | 104 EXPECT_EQ(value_2, window->GetProperty(key)); |
| 105 int64_t decoded_value_2 = 0; |
| 106 EXPECT_TRUE(property_converter->GetPropertyValueFromTransportValue( |
| 107 transport_name, transport_value2, &decoded_value_2)); |
| 108 EXPECT_EQ(value_2, static_cast<T>(decoded_value_2)); |
| 98 } | 109 } |
| 99 | 110 |
| 100 } // namespace | 111 } // namespace |
| 101 | 112 |
| 102 using PropertyConverterTest = test::AuraTestBase; | 113 using PropertyConverterTest = test::AuraTestBase; |
| 103 | 114 |
| 104 // Verifies property setting behavior for a std::string* property. | 115 // Verifies property setting behavior for a std::string* property. |
| 105 TEST_F(PropertyConverterTest, PrimitiveProperties) { | 116 TEST_F(PropertyConverterTest, PrimitiveProperties) { |
| 106 PropertyConverter property_converter; | 117 PropertyConverter property_converter; |
| 107 std::unique_ptr<Window> window(CreateNormalWindow(1, root_window(), nullptr)); | 118 std::unique_ptr<Window> window(CreateNormalWindow(1, root_window(), nullptr)); |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 207 *transport_value_out.get()); | 218 *transport_value_out.get()); |
| 208 | 219 |
| 209 gfx::Rect value_2(1, 3, 5, 7); | 220 gfx::Rect value_2(1, 3, 5, 7); |
| 210 std::vector<uint8_t> transport_value = | 221 std::vector<uint8_t> transport_value = |
| 211 mojo::ConvertTo<std::vector<uint8_t>>(value_2); | 222 mojo::ConvertTo<std::vector<uint8_t>>(value_2); |
| 212 property_converter.SetPropertyFromTransportValue( | 223 property_converter.SetPropertyFromTransportValue( |
| 213 window.get(), kTestRectPropertyServerKey, &transport_value); | 224 window.get(), kTestRectPropertyServerKey, &transport_value); |
| 214 EXPECT_EQ(value_2, *window->GetProperty(kTestRectPropertyKey)); | 225 EXPECT_EQ(value_2, *window->GetProperty(kTestRectPropertyKey)); |
| 215 } | 226 } |
| 216 | 227 |
| 228 // Verifies property setting behavior for a gfx::Size* property. |
| 229 TEST_F(PropertyConverterTest, SizeProperty) { |
| 230 PropertyConverter property_converter; |
| 231 property_converter.RegisterProperty(kTestSizePropertyKey, |
| 232 kTestSizePropertyServerKey); |
| 233 EXPECT_EQ( |
| 234 kTestSizePropertyServerKey, |
| 235 property_converter.GetTransportNameForPropertyKey(kTestSizePropertyKey)); |
| 236 |
| 237 gfx::Size value_1(1, 2); |
| 238 std::unique_ptr<Window> window(CreateNormalWindow(1, root_window(), nullptr)); |
| 239 window->SetProperty(kTestSizePropertyKey, new gfx::Size(value_1)); |
| 240 EXPECT_EQ(value_1, *window->GetProperty(kTestSizePropertyKey)); |
| 241 |
| 242 std::string transport_name_out; |
| 243 std::unique_ptr<std::vector<uint8_t>> transport_value_out; |
| 244 EXPECT_TRUE(property_converter.ConvertPropertyForTransport( |
| 245 window.get(), kTestSizePropertyKey, &transport_name_out, |
| 246 &transport_value_out)); |
| 247 EXPECT_EQ(kTestSizePropertyServerKey, transport_name_out); |
| 248 EXPECT_EQ(mojo::ConvertTo<std::vector<uint8_t>>(value_1), |
| 249 *transport_value_out.get()); |
| 250 |
| 251 gfx::Size value_2(1, 3); |
| 252 std::vector<uint8_t> transport_value = |
| 253 mojo::ConvertTo<std::vector<uint8_t>>(value_2); |
| 254 property_converter.SetPropertyFromTransportValue( |
| 255 window.get(), kTestSizePropertyServerKey, &transport_value); |
| 256 EXPECT_EQ(value_2, *window->GetProperty(kTestSizePropertyKey)); |
| 257 } |
| 258 |
| 217 // Verifies property setting behavior for a std::string* property. | 259 // Verifies property setting behavior for a std::string* property. |
| 218 TEST_F(PropertyConverterTest, StringProperty) { | 260 TEST_F(PropertyConverterTest, StringProperty) { |
| 219 PropertyConverter property_converter; | 261 PropertyConverter property_converter; |
| 220 property_converter.RegisterProperty(kTestStringPropertyKey, | 262 property_converter.RegisterProperty(kTestStringPropertyKey, |
| 221 kTestStringPropertyServerKey); | 263 kTestStringPropertyServerKey); |
| 222 EXPECT_EQ(kTestStringPropertyServerKey, | 264 EXPECT_EQ(kTestStringPropertyServerKey, |
| 223 property_converter.GetTransportNameForPropertyKey( | 265 property_converter.GetTransportNameForPropertyKey( |
| 224 kTestStringPropertyKey)); | 266 kTestStringPropertyKey)); |
| 225 | 267 |
| 226 std::string value_1 = "test value"; | 268 std::string value_1 = "test value"; |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 270 | 312 |
| 271 base::string16 value_2 = base::ASCIIToUTF16("another test value"); | 313 base::string16 value_2 = base::ASCIIToUTF16("another test value"); |
| 272 std::vector<uint8_t> transport_value = | 314 std::vector<uint8_t> transport_value = |
| 273 mojo::ConvertTo<std::vector<uint8_t>>(value_2); | 315 mojo::ConvertTo<std::vector<uint8_t>>(value_2); |
| 274 property_converter.SetPropertyFromTransportValue( | 316 property_converter.SetPropertyFromTransportValue( |
| 275 window.get(), kTestString16PropertyServerKey, &transport_value); | 317 window.get(), kTestString16PropertyServerKey, &transport_value); |
| 276 EXPECT_EQ(value_2, *window->GetProperty(kTestString16PropertyKey)); | 318 EXPECT_EQ(value_2, *window->GetProperty(kTestString16PropertyKey)); |
| 277 } | 319 } |
| 278 | 320 |
| 279 } // namespace aura | 321 } // namespace aura |
| OLD | NEW |