OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "dbus/property.h" | 5 #include "dbus/property.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
14 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
| 15 #include "base/strings/string_number_conversions.h" |
15 #include "base/threading/thread.h" | 16 #include "base/threading/thread.h" |
16 #include "base/threading/thread_restrictions.h" | 17 #include "base/threading/thread_restrictions.h" |
17 #include "dbus/bus.h" | 18 #include "dbus/bus.h" |
18 #include "dbus/object_path.h" | 19 #include "dbus/object_path.h" |
19 #include "dbus/object_proxy.h" | 20 #include "dbus/object_proxy.h" |
20 #include "dbus/test_service.h" | 21 #include "dbus/test_service.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
22 | 23 |
23 namespace dbus { | 24 namespace dbus { |
24 | 25 |
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
270 base::Unretained(this), | 271 base::Unretained(this), |
271 "Set")); | 272 "Set")); |
272 WaitForCallback("Set"); | 273 WaitForCallback("Set"); |
273 | 274 |
274 // TestService sends a property update. | 275 // TestService sends a property update. |
275 WaitForUpdates(1); | 276 WaitForUpdates(1); |
276 | 277 |
277 EXPECT_EQ("NewService", properties_->name.value()); | 278 EXPECT_EQ("NewService", properties_->name.value()); |
278 } | 279 } |
279 | 280 |
| 281 TEST(PropertyTestStatic, ReadWriteStringMap) { |
| 282 scoped_ptr<Response> message(Response::CreateEmpty()); |
| 283 MessageWriter writer(message.get()); |
| 284 MessageWriter variant_writer(NULL); |
| 285 MessageWriter variant_array_writer(NULL); |
| 286 MessageWriter struct_entry_writer(NULL); |
| 287 |
| 288 writer.OpenVariant("a{ss}", &variant_writer); |
| 289 variant_writer.OpenArray("{ss}", &variant_array_writer); |
| 290 const char* items[] = {"One", "Two", "Three", "Four"}; |
| 291 for (unsigned i = 0; i < arraysize(items); ++i) { |
| 292 variant_array_writer.OpenDictEntry(&struct_entry_writer); |
| 293 struct_entry_writer.AppendString(items[i]); |
| 294 struct_entry_writer.AppendString(base::UintToString(i + 1)); |
| 295 variant_array_writer.CloseContainer(&struct_entry_writer); |
| 296 } |
| 297 variant_writer.CloseContainer(&variant_array_writer); |
| 298 writer.CloseContainer(&variant_writer); |
| 299 |
| 300 MessageReader reader(message.get()); |
| 301 Property<std::map<std::string, std::string>> string_map; |
| 302 EXPECT_TRUE(string_map.PopValueFromReader(&reader)); |
| 303 ASSERT_EQ(4U, string_map.value().size()); |
| 304 EXPECT_EQ("1", string_map.value().at("One")); |
| 305 EXPECT_EQ("2", string_map.value().at("Two")); |
| 306 EXPECT_EQ("3", string_map.value().at("Three")); |
| 307 EXPECT_EQ("4", string_map.value().at("Four")); |
| 308 } |
| 309 |
| 310 TEST(PropertyTestStatic, SerializeStringMap) { |
| 311 std::map<std::string, std::string> test_map; |
| 312 test_map["Hi"] = "There"; |
| 313 test_map["Map"] = "Test"; |
| 314 test_map["Random"] = "Text"; |
| 315 |
| 316 scoped_ptr<Response> message(Response::CreateEmpty()); |
| 317 MessageWriter writer(message.get()); |
| 318 |
| 319 Property<std::map<std::string, std::string>> string_map; |
| 320 string_map.ReplaceSetValueForTesting(test_map); |
| 321 string_map.AppendSetValueToWriter(&writer); |
| 322 |
| 323 MessageReader reader(message.get()); |
| 324 EXPECT_TRUE(string_map.PopValueFromReader(&reader)); |
| 325 EXPECT_EQ(test_map, string_map.value()); |
| 326 } |
| 327 |
| 328 TEST(PropertyTestStatic, ReadWriteNetAddressArray) { |
| 329 scoped_ptr<Response> message(Response::CreateEmpty()); |
| 330 MessageWriter writer(message.get()); |
| 331 MessageWriter variant_writer(NULL); |
| 332 MessageWriter variant_array_writer(NULL); |
| 333 MessageWriter struct_entry_writer(NULL); |
| 334 |
| 335 writer.OpenVariant("a(ayq)", &variant_writer); |
| 336 variant_writer.OpenArray("(ayq)", &variant_array_writer); |
| 337 uint8 ip_bytes[] = {0x54, 0x65, 0x73, 0x74, 0x30}; |
| 338 for (uint16 i = 0; i < 5; ++i) { |
| 339 variant_array_writer.OpenStruct(&struct_entry_writer); |
| 340 ip_bytes[4] = 0x30 + i; |
| 341 struct_entry_writer.AppendArrayOfBytes(ip_bytes, arraysize(ip_bytes)); |
| 342 struct_entry_writer.AppendUint16(i); |
| 343 variant_array_writer.CloseContainer(&struct_entry_writer); |
| 344 } |
| 345 variant_writer.CloseContainer(&variant_array_writer); |
| 346 writer.CloseContainer(&variant_writer); |
| 347 |
| 348 MessageReader reader(message.get()); |
| 349 Property<std::vector<std::pair<std::vector<uint8>, uint16>>> ip_list; |
| 350 EXPECT_TRUE(ip_list.PopValueFromReader(&reader)); |
| 351 |
| 352 ASSERT_EQ(5U, ip_list.value().size()); |
| 353 size_t item_index = 0; |
| 354 for (auto& item : ip_list.value()) { |
| 355 ASSERT_EQ(5U, item.first.size()); |
| 356 ip_bytes[4] = 0x30 + item_index; |
| 357 EXPECT_EQ(0, memcmp(ip_bytes, item.first.data(), 5U)); |
| 358 EXPECT_EQ(item_index, item.second); |
| 359 ++item_index; |
| 360 } |
| 361 } |
| 362 |
| 363 TEST(PropertyTestStatic, SerializeNetAddressArray) { |
| 364 std::vector<std::pair<std::vector<uint8>, uint16>> test_list; |
| 365 |
| 366 uint8 ip_bytes[] = {0x54, 0x65, 0x73, 0x74, 0x30}; |
| 367 for (uint16 i = 0; i < 5; ++i) { |
| 368 ip_bytes[4] = 0x30 + i; |
| 369 std::vector<uint8> bytes(ip_bytes, ip_bytes + arraysize(ip_bytes)); |
| 370 test_list.push_back(make_pair(bytes, 16)); |
| 371 } |
| 372 |
| 373 scoped_ptr<Response> message(Response::CreateEmpty()); |
| 374 MessageWriter writer(message.get()); |
| 375 |
| 376 Property<std::vector<std::pair<std::vector<uint8>, uint16>>> ip_list; |
| 377 ip_list.ReplaceSetValueForTesting(test_list); |
| 378 ip_list.AppendSetValueToWriter(&writer); |
| 379 |
| 380 MessageReader reader(message.get()); |
| 381 EXPECT_TRUE(ip_list.PopValueFromReader(&reader)); |
| 382 EXPECT_EQ(test_list, ip_list.value()); |
| 383 } |
| 384 |
280 } // namespace dbus | 385 } // namespace dbus |
OLD | NEW |