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 <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
| 10 #include <memory> |
10 #include <string> | 11 #include <string> |
11 #include <vector> | 12 #include <vector> |
12 | 13 |
13 #include "base/bind.h" | 14 #include "base/bind.h" |
14 #include "base/logging.h" | 15 #include "base/logging.h" |
15 #include "base/macros.h" | 16 #include "base/macros.h" |
16 #include "base/message_loop/message_loop.h" | 17 #include "base/message_loop/message_loop.h" |
17 #include "base/run_loop.h" | 18 #include "base/run_loop.h" |
18 #include "base/strings/string_number_conversions.h" | 19 #include "base/strings/string_number_conversions.h" |
19 #include "base/threading/thread.h" | 20 #include "base/threading/thread.h" |
(...skipping 404 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
424 | 425 |
425 Property<std::vector<std::pair<std::vector<uint8_t>, uint16_t>>> ip_list; | 426 Property<std::vector<std::pair<std::vector<uint8_t>, uint16_t>>> ip_list; |
426 ip_list.ReplaceSetValueForTesting(test_list); | 427 ip_list.ReplaceSetValueForTesting(test_list); |
427 ip_list.AppendSetValueToWriter(&writer); | 428 ip_list.AppendSetValueToWriter(&writer); |
428 | 429 |
429 MessageReader reader(message.get()); | 430 MessageReader reader(message.get()); |
430 EXPECT_TRUE(ip_list.PopValueFromReader(&reader)); | 431 EXPECT_TRUE(ip_list.PopValueFromReader(&reader)); |
431 EXPECT_EQ(test_list, ip_list.value()); | 432 EXPECT_EQ(test_list, ip_list.value()); |
432 } | 433 } |
433 | 434 |
| 435 TEST(PropertyTestStatic, ReadWriteStringToByteVectorMap) { |
| 436 std::unique_ptr<Response> message(Response::CreateEmpty()); |
| 437 MessageWriter writer(message.get()); |
| 438 MessageWriter variant_writer(nullptr); |
| 439 MessageWriter dict_writer(nullptr); |
| 440 |
| 441 writer.OpenVariant("a{sv}", &variant_writer); |
| 442 variant_writer.OpenArray("{sv}", &dict_writer); |
| 443 |
| 444 const char* keys[] = {"One", "Two", "Three", "Four"}; |
| 445 const std::vector<uint8_t> values[] = {{1}, {1, 2}, {1, 2, 3}, {1, 2, 3, 4}}; |
| 446 for (unsigned i = 0; i < arraysize(keys); ++i) { |
| 447 MessageWriter entry_writer(nullptr); |
| 448 dict_writer.OpenDictEntry(&entry_writer); |
| 449 |
| 450 entry_writer.AppendString(keys[i]); |
| 451 |
| 452 MessageWriter value_varient_writer(nullptr); |
| 453 entry_writer.OpenVariant("ay", &value_varient_writer); |
| 454 value_varient_writer.AppendArrayOfBytes(values[i].data(), values[i].size()); |
| 455 entry_writer.CloseContainer(&value_varient_writer); |
| 456 |
| 457 dict_writer.CloseContainer(&entry_writer); |
| 458 } |
| 459 |
| 460 variant_writer.CloseContainer(&dict_writer); |
| 461 writer.CloseContainer(&variant_writer); |
| 462 |
| 463 MessageReader reader(message.get()); |
| 464 Property<std::unordered_map<std::string, std::vector<uint8_t>>> test_property; |
| 465 EXPECT_TRUE(test_property.PopValueFromReader(&reader)); |
| 466 |
| 467 ASSERT_EQ(arraysize(keys), test_property.value().size()); |
| 468 for (unsigned i = 0; i < arraysize(keys); ++i) |
| 469 EXPECT_EQ(values[i], test_property.value().at(keys[i])); |
| 470 } |
| 471 |
| 472 TEST(PropertyTestStatic, SerializeStringToByteVectorMap) { |
| 473 std::unordered_map<std::string, std::vector<uint8_t>> test_map; |
| 474 test_map["Hi"] = {1, 2, 3}; |
| 475 test_map["Map"] = {0xab, 0xcd}; |
| 476 test_map["Random"] = {0x0}; |
| 477 |
| 478 std::unique_ptr<Response> message(Response::CreateEmpty()); |
| 479 MessageWriter writer(message.get()); |
| 480 |
| 481 Property<std::unordered_map<std::string, std::vector<uint8_t>>> test_property; |
| 482 test_property.ReplaceSetValueForTesting(test_map); |
| 483 test_property.AppendSetValueToWriter(&writer); |
| 484 |
| 485 MessageReader reader(message.get()); |
| 486 EXPECT_TRUE(test_property.PopValueFromReader(&reader)); |
| 487 EXPECT_EQ(test_map, test_property.value()); |
| 488 } |
| 489 |
434 } // namespace dbus | 490 } // namespace dbus |
OLD | NEW |