From ebbfffa2f19d6d4727586710dd49be63fcd8e1e2 Mon Sep 17 00:00:00 2001 From: "armansito@chromium.org" Date: Sat, 15 Mar 2014 07:40:49 +0000 Subject: dbus: Add template specialization for Property >. BUG=351229 TEST=dbus_unittests Review URL: https://codereview.chromium.org/199573003 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@257315 0039d316-1c4b-4281-b951-d872f2087c98 --- dbus/property.cc | 28 ++++++++++++++++++++++++++++ dbus/property.h | 5 +++++ dbus/property_unittest.cc | 26 +++++++++++++++++++++++++- dbus/test_service.cc | 23 +++++++++++++++++++++++ 4 files changed, 81 insertions(+), 1 deletion(-) (limited to 'dbus') diff --git a/dbus/property.cc b/dbus/property.cc index 9289c86..774719f 100644 --- a/dbus/property.cc +++ b/dbus/property.cc @@ -449,4 +449,32 @@ void Property >::AppendSetValueToWriter( writer->CloseContainer(&variant_writer); } +// +// Property > specialization. +// + +template <> +bool Property >::PopValueFromReader(MessageReader* reader) { + MessageReader variant_reader(NULL); + if (!reader->PopVariant(&variant_reader)) + return false; + + value_.clear(); + const uint8* bytes = NULL; + size_t length = 0; + if (!variant_reader.PopArrayOfBytes(&bytes, &length)) + return false; + value_.assign(bytes, bytes + length); + return true; +} + +template <> +void Property >::AppendSetValueToWriter( + MessageWriter* writer) { + MessageWriter variant_writer(NULL); + writer->OpenVariant("ay", &variant_writer); + variant_writer.AppendArrayOfBytes(set_value_.data(), set_value_.size()); + writer->CloseContainer(&variant_writer); +} + } // namespace dbus diff --git a/dbus/property.h b/dbus/property.h index 9f5af48..f6baffc 100644 --- a/dbus/property.h +++ b/dbus/property.h @@ -466,6 +466,11 @@ template <> bool Property >::PopValueFromReader( template <> void Property >::AppendSetValueToWriter( MessageWriter* writer); +template <> bool Property >::PopValueFromReader( + MessageReader* reader); +template <> void Property >::AppendSetValueToWriter( + MessageWriter* writer); + } // namespace dbus #endif // DBUS_PROPERTY_H_ diff --git a/dbus/property_unittest.cc b/dbus/property_unittest.cc index cbff1dc..e078c00 100644 --- a/dbus/property_unittest.cc +++ b/dbus/property_unittest.cc @@ -33,6 +33,7 @@ class PropertyTest : public testing::Test { Property version; Property > methods; Property > objects; + Property > bytes; Properties(ObjectProxy* object_proxy, PropertyChangedCallback property_changed_callback) @@ -43,6 +44,7 @@ class PropertyTest : public testing::Test { RegisterProperty("Version", &version); RegisterProperty("Methods", &methods); RegisterProperty("Objects", &objects); + RegisterProperty("Bytes", &bytes); } }; @@ -122,7 +124,7 @@ class PropertyTest : public testing::Test { } // Name, Version, Methods, Objects - static const int kExpectedSignalUpdates = 4; + static const int kExpectedSignalUpdates = 5; // Waits for initial values to be set. void WaitForGetAll() { @@ -166,6 +168,13 @@ TEST_F(PropertyTest, InitialValues) { std::vector objects = properties_->objects.value(); ASSERT_EQ(1U, objects.size()); EXPECT_EQ(ObjectPath("/TestObjectPath"), objects[0]); + + std::vector bytes = properties_->bytes.value(); + ASSERT_EQ(4U, bytes.size()); + EXPECT_EQ('T', bytes[0]); + EXPECT_EQ('e', bytes[1]); + EXPECT_EQ('s', bytes[2]); + EXPECT_EQ('t', bytes[3]); } TEST_F(PropertyTest, UpdatedValues) { @@ -215,6 +224,21 @@ TEST_F(PropertyTest, UpdatedValues) { std::vector objects = properties_->objects.value(); ASSERT_EQ(1U, objects.size()); EXPECT_EQ(ObjectPath("/TestObjectPath"), objects[0]); + + // Update the value of the "Bytes" property, this value should not change + // and should not grow to contain duplicate entries. + properties_->bytes.Get(base::Bind(&PropertyTest::PropertyCallback, + base::Unretained(this), + "Bytes")); + WaitForCallback("Bytes"); + WaitForUpdates(1); + + std::vector bytes = properties_->bytes.value(); + ASSERT_EQ(4U, bytes.size()); + EXPECT_EQ('T', bytes[0]); + EXPECT_EQ('e', bytes[1]); + EXPECT_EQ('s', bytes[2]); + EXPECT_EQ('t', bytes[3]); } TEST_F(PropertyTest, Get) { diff --git a/dbus/test_service.cc b/dbus/test_service.cc index 064b91e..96fa8bc 100644 --- a/dbus/test_service.cc +++ b/dbus/test_service.cc @@ -408,6 +408,20 @@ void TestService::GetProperty(MethodCall* method_call, writer.CloseContainer(&variant_writer); response_sender.Run(response.Pass()); + } else if (name == "Bytes") { + // Return the previous value for the "Bytes" property: + // Variant<[0x54, 0x65, 0x73, 0x74]> + scoped_ptr response = Response::FromMethodCall(method_call); + MessageWriter writer(response.get()); + MessageWriter variant_writer(NULL); + MessageWriter variant_array_writer(NULL); + + writer.OpenVariant("ay", &variant_writer); + const uint8 bytes[] = { 0x54, 0x65, 0x73, 0x74 }; + variant_writer.AppendArrayOfBytes(bytes, sizeof(bytes)); + writer.CloseContainer(&variant_writer); + + response_sender.Run(response.Pass()); } else { // Return error. response_sender.Run(scoped_ptr()); @@ -554,6 +568,7 @@ void TestService::AddPropertiesToWriter(MessageWriter* writer) { // "Version": Variant<10>, // "Methods": Variant<["Echo", "SlowEcho", "AsyncEcho", "BrokenMethod"]>, // "Objects": Variant<[objectpath:"/TestObjectPath"]> + // "Bytes": Variant<[0x54, 0x65, 0x73, 0x74]> // } MessageWriter array_writer(NULL); @@ -594,6 +609,14 @@ void TestService::AddPropertiesToWriter(MessageWriter* writer) { dict_entry_writer.CloseContainer(&variant_writer); array_writer.CloseContainer(&dict_entry_writer); + array_writer.OpenDictEntry(&dict_entry_writer); + dict_entry_writer.AppendString("Bytes"); + dict_entry_writer.OpenVariant("ay", &variant_writer); + const uint8 bytes[] = { 0x54, 0x65, 0x73, 0x74 }; + variant_writer.AppendArrayOfBytes(bytes, sizeof(bytes)); + dict_entry_writer.CloseContainer(&variant_writer); + array_writer.CloseContainer(&dict_entry_writer); + writer->CloseContainer(&array_writer); } -- cgit v1.1