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> |
| 8 #include <stdint.h> |
| 9 |
7 #include <string> | 10 #include <string> |
8 #include <vector> | 11 #include <vector> |
9 | 12 |
10 #include "base/basictypes.h" | |
11 #include "base/bind.h" | 13 #include "base/bind.h" |
12 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/macros.h" |
13 #include "base/message_loop/message_loop.h" | 16 #include "base/message_loop/message_loop.h" |
14 #include "base/run_loop.h" | 17 #include "base/run_loop.h" |
15 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
16 #include "base/threading/thread.h" | 19 #include "base/threading/thread.h" |
17 #include "base/threading/thread_restrictions.h" | 20 #include "base/threading/thread_restrictions.h" |
18 #include "dbus/bus.h" | 21 #include "dbus/bus.h" |
19 #include "dbus/object_path.h" | 22 #include "dbus/object_path.h" |
20 #include "dbus/object_proxy.h" | 23 #include "dbus/object_proxy.h" |
21 #include "dbus/test_service.h" | 24 #include "dbus/test_service.h" |
22 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
23 | 26 |
24 namespace dbus { | 27 namespace dbus { |
25 | 28 |
26 // The property test exerises the asynchronous APIs in PropertySet and | 29 // The property test exerises the asynchronous APIs in PropertySet and |
27 // Property<>. | 30 // Property<>. |
28 class PropertyTest : public testing::Test { | 31 class PropertyTest : public testing::Test { |
29 public: | 32 public: |
30 PropertyTest() {} | 33 PropertyTest() {} |
31 | 34 |
32 struct Properties : public PropertySet { | 35 struct Properties : public PropertySet { |
33 Property<std::string> name; | 36 Property<std::string> name; |
34 Property<int16> version; | 37 Property<int16_t> version; |
35 Property<std::vector<std::string> > methods; | 38 Property<std::vector<std::string> > methods; |
36 Property<std::vector<ObjectPath> > objects; | 39 Property<std::vector<ObjectPath> > objects; |
37 Property<std::vector<uint8> > bytes; | 40 Property<std::vector<uint8_t>> bytes; |
38 | 41 |
39 Properties(ObjectProxy* object_proxy, | 42 Properties(ObjectProxy* object_proxy, |
40 PropertyChangedCallback property_changed_callback) | 43 PropertyChangedCallback property_changed_callback) |
41 : PropertySet(object_proxy, | 44 : PropertySet(object_proxy, |
42 "org.chromium.TestInterface", | 45 "org.chromium.TestInterface", |
43 property_changed_callback) { | 46 property_changed_callback) { |
44 RegisterProperty("Name", &name); | 47 RegisterProperty("Name", &name); |
45 RegisterProperty("Version", &version); | 48 RegisterProperty("Version", &version); |
46 RegisterProperty("Methods", &methods); | 49 RegisterProperty("Methods", &methods); |
47 RegisterProperty("Objects", &objects); | 50 RegisterProperty("Objects", &objects); |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 ASSERT_EQ(4U, methods.size()); | 185 ASSERT_EQ(4U, methods.size()); |
183 EXPECT_EQ("Echo", methods[0]); | 186 EXPECT_EQ("Echo", methods[0]); |
184 EXPECT_EQ("SlowEcho", methods[1]); | 187 EXPECT_EQ("SlowEcho", methods[1]); |
185 EXPECT_EQ("AsyncEcho", methods[2]); | 188 EXPECT_EQ("AsyncEcho", methods[2]); |
186 EXPECT_EQ("BrokenMethod", methods[3]); | 189 EXPECT_EQ("BrokenMethod", methods[3]); |
187 | 190 |
188 std::vector<ObjectPath> objects = properties_->objects.value(); | 191 std::vector<ObjectPath> objects = properties_->objects.value(); |
189 ASSERT_EQ(1U, objects.size()); | 192 ASSERT_EQ(1U, objects.size()); |
190 EXPECT_EQ(ObjectPath("/TestObjectPath"), objects[0]); | 193 EXPECT_EQ(ObjectPath("/TestObjectPath"), objects[0]); |
191 | 194 |
192 std::vector<uint8> bytes = properties_->bytes.value(); | 195 std::vector<uint8_t> bytes = properties_->bytes.value(); |
193 ASSERT_EQ(4U, bytes.size()); | 196 ASSERT_EQ(4U, bytes.size()); |
194 EXPECT_EQ('T', bytes[0]); | 197 EXPECT_EQ('T', bytes[0]); |
195 EXPECT_EQ('e', bytes[1]); | 198 EXPECT_EQ('e', bytes[1]); |
196 EXPECT_EQ('s', bytes[2]); | 199 EXPECT_EQ('s', bytes[2]); |
197 EXPECT_EQ('t', bytes[3]); | 200 EXPECT_EQ('t', bytes[3]); |
198 } | 201 } |
199 | 202 |
200 TEST_F(PropertyTest, UpdatedValues) { | 203 TEST_F(PropertyTest, UpdatedValues) { |
201 WaitForGetAll(); | 204 WaitForGetAll(); |
202 | 205 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
246 EXPECT_EQ(ObjectPath("/TestObjectPath"), objects[0]); | 249 EXPECT_EQ(ObjectPath("/TestObjectPath"), objects[0]); |
247 | 250 |
248 // Update the value of the "Bytes" property, this value should not change | 251 // Update the value of the "Bytes" property, this value should not change |
249 // and should not grow to contain duplicate entries. | 252 // and should not grow to contain duplicate entries. |
250 properties_->bytes.Get(base::Bind(&PropertyTest::PropertyCallback, | 253 properties_->bytes.Get(base::Bind(&PropertyTest::PropertyCallback, |
251 base::Unretained(this), | 254 base::Unretained(this), |
252 "Bytes")); | 255 "Bytes")); |
253 WaitForCallback("Bytes"); | 256 WaitForCallback("Bytes"); |
254 WaitForUpdates(1); | 257 WaitForUpdates(1); |
255 | 258 |
256 std::vector<uint8> bytes = properties_->bytes.value(); | 259 std::vector<uint8_t> bytes = properties_->bytes.value(); |
257 ASSERT_EQ(4U, bytes.size()); | 260 ASSERT_EQ(4U, bytes.size()); |
258 EXPECT_EQ('T', bytes[0]); | 261 EXPECT_EQ('T', bytes[0]); |
259 EXPECT_EQ('e', bytes[1]); | 262 EXPECT_EQ('e', bytes[1]); |
260 EXPECT_EQ('s', bytes[2]); | 263 EXPECT_EQ('s', bytes[2]); |
261 EXPECT_EQ('t', bytes[3]); | 264 EXPECT_EQ('t', bytes[3]); |
262 } | 265 } |
263 | 266 |
264 TEST_F(PropertyTest, Get) { | 267 TEST_F(PropertyTest, Get) { |
265 WaitForGetAll(); | 268 WaitForGetAll(); |
266 | 269 |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
373 | 376 |
374 TEST(PropertyTestStatic, ReadWriteNetAddressArray) { | 377 TEST(PropertyTestStatic, ReadWriteNetAddressArray) { |
375 scoped_ptr<Response> message(Response::CreateEmpty()); | 378 scoped_ptr<Response> message(Response::CreateEmpty()); |
376 MessageWriter writer(message.get()); | 379 MessageWriter writer(message.get()); |
377 MessageWriter variant_writer(NULL); | 380 MessageWriter variant_writer(NULL); |
378 MessageWriter variant_array_writer(NULL); | 381 MessageWriter variant_array_writer(NULL); |
379 MessageWriter struct_entry_writer(NULL); | 382 MessageWriter struct_entry_writer(NULL); |
380 | 383 |
381 writer.OpenVariant("a(ayq)", &variant_writer); | 384 writer.OpenVariant("a(ayq)", &variant_writer); |
382 variant_writer.OpenArray("(ayq)", &variant_array_writer); | 385 variant_writer.OpenArray("(ayq)", &variant_array_writer); |
383 uint8 ip_bytes[] = {0x54, 0x65, 0x73, 0x74, 0x30}; | 386 uint8_t ip_bytes[] = {0x54, 0x65, 0x73, 0x74, 0x30}; |
384 for (uint16 i = 0; i < 5; ++i) { | 387 for (uint16_t i = 0; i < 5; ++i) { |
385 variant_array_writer.OpenStruct(&struct_entry_writer); | 388 variant_array_writer.OpenStruct(&struct_entry_writer); |
386 ip_bytes[4] = 0x30 + i; | 389 ip_bytes[4] = 0x30 + i; |
387 struct_entry_writer.AppendArrayOfBytes(ip_bytes, arraysize(ip_bytes)); | 390 struct_entry_writer.AppendArrayOfBytes(ip_bytes, arraysize(ip_bytes)); |
388 struct_entry_writer.AppendUint16(i); | 391 struct_entry_writer.AppendUint16(i); |
389 variant_array_writer.CloseContainer(&struct_entry_writer); | 392 variant_array_writer.CloseContainer(&struct_entry_writer); |
390 } | 393 } |
391 variant_writer.CloseContainer(&variant_array_writer); | 394 variant_writer.CloseContainer(&variant_array_writer); |
392 writer.CloseContainer(&variant_writer); | 395 writer.CloseContainer(&variant_writer); |
393 | 396 |
394 MessageReader reader(message.get()); | 397 MessageReader reader(message.get()); |
395 Property<std::vector<std::pair<std::vector<uint8>, uint16>>> ip_list; | 398 Property<std::vector<std::pair<std::vector<uint8_t>, uint16_t>>> ip_list; |
396 EXPECT_TRUE(ip_list.PopValueFromReader(&reader)); | 399 EXPECT_TRUE(ip_list.PopValueFromReader(&reader)); |
397 | 400 |
398 ASSERT_EQ(5U, ip_list.value().size()); | 401 ASSERT_EQ(5U, ip_list.value().size()); |
399 size_t item_index = 0; | 402 size_t item_index = 0; |
400 for (auto& item : ip_list.value()) { | 403 for (auto& item : ip_list.value()) { |
401 ASSERT_EQ(5U, item.first.size()); | 404 ASSERT_EQ(5U, item.first.size()); |
402 ip_bytes[4] = 0x30 + item_index; | 405 ip_bytes[4] = 0x30 + item_index; |
403 EXPECT_EQ(0, memcmp(ip_bytes, item.first.data(), 5U)); | 406 EXPECT_EQ(0, memcmp(ip_bytes, item.first.data(), 5U)); |
404 EXPECT_EQ(item_index, item.second); | 407 EXPECT_EQ(item_index, item.second); |
405 ++item_index; | 408 ++item_index; |
406 } | 409 } |
407 } | 410 } |
408 | 411 |
409 TEST(PropertyTestStatic, SerializeNetAddressArray) { | 412 TEST(PropertyTestStatic, SerializeNetAddressArray) { |
410 std::vector<std::pair<std::vector<uint8>, uint16>> test_list; | 413 std::vector<std::pair<std::vector<uint8_t>, uint16_t>> test_list; |
411 | 414 |
412 uint8 ip_bytes[] = {0x54, 0x65, 0x73, 0x74, 0x30}; | 415 uint8_t ip_bytes[] = {0x54, 0x65, 0x73, 0x74, 0x30}; |
413 for (uint16 i = 0; i < 5; ++i) { | 416 for (uint16_t i = 0; i < 5; ++i) { |
414 ip_bytes[4] = 0x30 + i; | 417 ip_bytes[4] = 0x30 + i; |
415 std::vector<uint8> bytes(ip_bytes, ip_bytes + arraysize(ip_bytes)); | 418 std::vector<uint8_t> bytes(ip_bytes, ip_bytes + arraysize(ip_bytes)); |
416 test_list.push_back(make_pair(bytes, 16)); | 419 test_list.push_back(make_pair(bytes, 16)); |
417 } | 420 } |
418 | 421 |
419 scoped_ptr<Response> message(Response::CreateEmpty()); | 422 scoped_ptr<Response> message(Response::CreateEmpty()); |
420 MessageWriter writer(message.get()); | 423 MessageWriter writer(message.get()); |
421 | 424 |
422 Property<std::vector<std::pair<std::vector<uint8>, uint16>>> ip_list; | 425 Property<std::vector<std::pair<std::vector<uint8_t>, uint16_t>>> ip_list; |
423 ip_list.ReplaceSetValueForTesting(test_list); | 426 ip_list.ReplaceSetValueForTesting(test_list); |
424 ip_list.AppendSetValueToWriter(&writer); | 427 ip_list.AppendSetValueToWriter(&writer); |
425 | 428 |
426 MessageReader reader(message.get()); | 429 MessageReader reader(message.get()); |
427 EXPECT_TRUE(ip_list.PopValueFromReader(&reader)); | 430 EXPECT_TRUE(ip_list.PopValueFromReader(&reader)); |
428 EXPECT_EQ(test_list, ip_list.value()); | 431 EXPECT_EQ(test_list, ip_list.value()); |
429 } | 432 } |
430 | 433 |
431 } // namespace dbus | 434 } // namespace dbus |
OLD | NEW |