[email protected] | cf910da2 | 2012-02-15 04:21:08 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "dbus/property.h" |
| 6 | |
| 7 | #include <string> |
| 8 | #include <vector> |
| 9 | |
| 10 | #include "base/basictypes.h" |
| 11 | #include "base/bind.h" |
| 12 | #include "base/logging.h" |
[email protected] | 2a9ec0e | 2013-07-17 23:00:30 | [diff] [blame] | 13 | #include "base/message_loop/message_loop.h" |
earthdok | 9562caf | 2014-09-03 10:32:36 | [diff] [blame] | 14 | #include "base/run_loop.h" |
dtapuska | 32d2545 | 2015-02-09 16:02:55 | [diff] [blame] | 15 | #include "base/strings/string_number_conversions.h" |
[email protected] | cf910da2 | 2012-02-15 04:21:08 | [diff] [blame] | 16 | #include "base/threading/thread.h" |
| 17 | #include "base/threading/thread_restrictions.h" |
| 18 | #include "dbus/bus.h" |
| 19 | #include "dbus/object_path.h" |
| 20 | #include "dbus/object_proxy.h" |
[email protected] | cf910da2 | 2012-02-15 04:21:08 | [diff] [blame] | 21 | #include "dbus/test_service.h" |
| 22 | #include "testing/gtest/include/gtest/gtest.h" |
| 23 | |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 24 | namespace dbus { |
| 25 | |
[email protected] | cf910da2 | 2012-02-15 04:21:08 | [diff] [blame] | 26 | // The property test exerises the asynchronous APIs in PropertySet and |
| 27 | // Property<>. |
| 28 | class PropertyTest : public testing::Test { |
| 29 | public: |
jpawlowski | ed27654 | 2015-05-11 11:07:04 | [diff] [blame] | 30 | PropertyTest() {} |
[email protected] | cf910da2 | 2012-02-15 04:21:08 | [diff] [blame] | 31 | |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 32 | struct Properties : public PropertySet { |
| 33 | Property<std::string> name; |
| 34 | Property<int16> version; |
| 35 | Property<std::vector<std::string> > methods; |
| 36 | Property<std::vector<ObjectPath> > objects; |
[email protected] | ebbfffa2 | 2014-03-15 07:40:49 | [diff] [blame] | 37 | Property<std::vector<uint8> > bytes; |
[email protected] | cf910da2 | 2012-02-15 04:21:08 | [diff] [blame] | 38 | |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 39 | Properties(ObjectProxy* object_proxy, |
[email protected] | cf910da2 | 2012-02-15 04:21:08 | [diff] [blame] | 40 | PropertyChangedCallback property_changed_callback) |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 41 | : PropertySet(object_proxy, |
| 42 | "org.chromium.TestInterface", |
| 43 | property_changed_callback) { |
[email protected] | cf910da2 | 2012-02-15 04:21:08 | [diff] [blame] | 44 | RegisterProperty("Name", &name); |
| 45 | RegisterProperty("Version", &version); |
| 46 | RegisterProperty("Methods", &methods); |
| 47 | RegisterProperty("Objects", &objects); |
[email protected] | ebbfffa2 | 2014-03-15 07:40:49 | [diff] [blame] | 48 | RegisterProperty("Bytes", &bytes); |
[email protected] | cf910da2 | 2012-02-15 04:21:08 | [diff] [blame] | 49 | } |
| 50 | }; |
| 51 | |
dcheng | 3bb7119c | 2014-12-29 18:30:17 | [diff] [blame] | 52 | void SetUp() override { |
[email protected] | cf910da2 | 2012-02-15 04:21:08 | [diff] [blame] | 53 | // Make the main thread not to allow IO. |
| 54 | base::ThreadRestrictions::SetIOAllowed(false); |
| 55 | |
| 56 | // Start the D-Bus thread. |
| 57 | dbus_thread_.reset(new base::Thread("D-Bus Thread")); |
| 58 | base::Thread::Options thread_options; |
[email protected] | ff33b18e | 2013-05-01 16:10:30 | [diff] [blame] | 59 | thread_options.message_loop_type = base::MessageLoop::TYPE_IO; |
[email protected] | cf910da2 | 2012-02-15 04:21:08 | [diff] [blame] | 60 | ASSERT_TRUE(dbus_thread_->StartWithOptions(thread_options)); |
| 61 | |
| 62 | // Start the test service, using the D-Bus thread. |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 63 | TestService::Options options; |
skyostil | 8a033aa | 2015-06-17 15:46:04 | [diff] [blame] | 64 | options.dbus_task_runner = dbus_thread_->task_runner(); |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 65 | test_service_.reset(new TestService(options)); |
[email protected] | cf910da2 | 2012-02-15 04:21:08 | [diff] [blame] | 66 | ASSERT_TRUE(test_service_->StartService()); |
| 67 | ASSERT_TRUE(test_service_->WaitUntilServiceIsStarted()); |
| 68 | ASSERT_TRUE(test_service_->HasDBusThread()); |
| 69 | |
| 70 | // Create the client, using the D-Bus thread. |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 71 | Bus::Options bus_options; |
| 72 | bus_options.bus_type = Bus::SESSION; |
| 73 | bus_options.connection_type = Bus::PRIVATE; |
skyostil | 8a033aa | 2015-06-17 15:46:04 | [diff] [blame] | 74 | bus_options.dbus_task_runner = dbus_thread_->task_runner(); |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 75 | bus_ = new Bus(bus_options); |
[email protected] | cf910da2 | 2012-02-15 04:21:08 | [diff] [blame] | 76 | object_proxy_ = bus_->GetObjectProxy( |
| 77 | "org.chromium.TestService", |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 78 | ObjectPath("/org/chromium/TestObject")); |
[email protected] | cf910da2 | 2012-02-15 04:21:08 | [diff] [blame] | 79 | ASSERT_TRUE(bus_->HasDBusThread()); |
| 80 | |
| 81 | // Create the properties structure |
[email protected] | ba4ce1c | 2012-05-31 06:55:53 | [diff] [blame] | 82 | properties_.reset(new Properties( |
| 83 | object_proxy_, |
| 84 | base::Bind(&PropertyTest::OnPropertyChanged, |
| 85 | base::Unretained(this)))); |
[email protected] | cf910da2 | 2012-02-15 04:21:08 | [diff] [blame] | 86 | properties_->ConnectSignals(); |
| 87 | properties_->GetAll(); |
| 88 | } |
| 89 | |
dcheng | 3bb7119c | 2014-12-29 18:30:17 | [diff] [blame] | 90 | void TearDown() override { |
[email protected] | cf910da2 | 2012-02-15 04:21:08 | [diff] [blame] | 91 | bus_->ShutdownOnDBusThreadAndBlock(); |
| 92 | |
| 93 | // Shut down the service. |
| 94 | test_service_->ShutdownAndBlock(); |
| 95 | |
| 96 | // Reset to the default. |
| 97 | base::ThreadRestrictions::SetIOAllowed(true); |
| 98 | |
| 99 | // Stopping a thread is considered an IO operation, so do this after |
| 100 | // allowing IO. |
| 101 | test_service_->Stop(); |
| 102 | } |
| 103 | |
| 104 | // Generic callback, bind with a string |id| for passing to |
| 105 | // WaitForCallback() to ensure the callback for the right method is |
| 106 | // waited for. |
| 107 | void PropertyCallback(const std::string& id, bool success) { |
| 108 | last_callback_ = id; |
earthdok | 9562caf | 2014-09-03 10:32:36 | [diff] [blame] | 109 | run_loop_->Quit(); |
[email protected] | cf910da2 | 2012-02-15 04:21:08 | [diff] [blame] | 110 | } |
| 111 | |
jpawlowski | ed27654 | 2015-05-11 11:07:04 | [diff] [blame] | 112 | // Generic method callback, that might be used together with |
| 113 | // WaitForMethodCallback to test wether method was succesfully called. |
| 114 | void MethodCallback(Response* response) { run_loop_->Quit(); } |
| 115 | |
[email protected] | cf910da2 | 2012-02-15 04:21:08 | [diff] [blame] | 116 | protected: |
| 117 | // Called when a property value is updated. |
| 118 | void OnPropertyChanged(const std::string& name) { |
| 119 | updated_properties_.push_back(name); |
earthdok | 9562caf | 2014-09-03 10:32:36 | [diff] [blame] | 120 | run_loop_->Quit(); |
[email protected] | cf910da2 | 2012-02-15 04:21:08 | [diff] [blame] | 121 | } |
| 122 | |
[email protected] | 8a3eaffa | 2012-03-22 20:34:05 | [diff] [blame] | 123 | // Waits for the given number of updates. |
[email protected] | cf910da2 | 2012-02-15 04:21:08 | [diff] [blame] | 124 | void WaitForUpdates(size_t num_updates) { |
earthdok | 9562caf | 2014-09-03 10:32:36 | [diff] [blame] | 125 | while (updated_properties_.size() < num_updates) { |
| 126 | run_loop_.reset(new base::RunLoop); |
| 127 | run_loop_->Run(); |
| 128 | } |
[email protected] | cf910da2 | 2012-02-15 04:21:08 | [diff] [blame] | 129 | for (size_t i = 0; i < num_updates; ++i) |
| 130 | updated_properties_.erase(updated_properties_.begin()); |
| 131 | } |
| 132 | |
| 133 | // Name, Version, Methods, Objects |
[email protected] | ebbfffa2 | 2014-03-15 07:40:49 | [diff] [blame] | 134 | static const int kExpectedSignalUpdates = 5; |
[email protected] | cf910da2 | 2012-02-15 04:21:08 | [diff] [blame] | 135 | |
[email protected] | 8a3eaffa | 2012-03-22 20:34:05 | [diff] [blame] | 136 | // Waits for initial values to be set. |
[email protected] | cf910da2 | 2012-02-15 04:21:08 | [diff] [blame] | 137 | void WaitForGetAll() { |
| 138 | WaitForUpdates(kExpectedSignalUpdates); |
| 139 | } |
| 140 | |
jpawlowski | ed27654 | 2015-05-11 11:07:04 | [diff] [blame] | 141 | // Waits until MethodCallback is called. |
| 142 | void WaitForMethodCallback() { |
| 143 | run_loop_.reset(new base::RunLoop); |
| 144 | run_loop_->Run(); |
| 145 | } |
| 146 | |
[email protected] | 8a3eaffa | 2012-03-22 20:34:05 | [diff] [blame] | 147 | // Waits for the callback. |id| is the string bound to the callback when |
[email protected] | cf910da2 | 2012-02-15 04:21:08 | [diff] [blame] | 148 | // the method call is made that identifies it and distinguishes from any |
| 149 | // other; you can set this to whatever you wish. |
| 150 | void WaitForCallback(const std::string& id) { |
| 151 | while (last_callback_ != id) { |
earthdok | 9562caf | 2014-09-03 10:32:36 | [diff] [blame] | 152 | run_loop_.reset(new base::RunLoop); |
| 153 | run_loop_->Run(); |
[email protected] | cf910da2 | 2012-02-15 04:21:08 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | |
[email protected] | ff33b18e | 2013-05-01 16:10:30 | [diff] [blame] | 157 | base::MessageLoop message_loop_; |
earthdok | 9562caf | 2014-09-03 10:32:36 | [diff] [blame] | 158 | scoped_ptr<base::RunLoop> run_loop_; |
[email protected] | cf910da2 | 2012-02-15 04:21:08 | [diff] [blame] | 159 | scoped_ptr<base::Thread> dbus_thread_; |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 160 | scoped_refptr<Bus> bus_; |
| 161 | ObjectProxy* object_proxy_; |
[email protected] | ba4ce1c | 2012-05-31 06:55:53 | [diff] [blame] | 162 | scoped_ptr<Properties> properties_; |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 163 | scoped_ptr<TestService> test_service_; |
[email protected] | cf910da2 | 2012-02-15 04:21:08 | [diff] [blame] | 164 | // Properties updated. |
| 165 | std::vector<std::string> updated_properties_; |
| 166 | // Last callback received. |
| 167 | std::string last_callback_; |
| 168 | }; |
| 169 | |
| 170 | TEST_F(PropertyTest, InitialValues) { |
jpawlowski | ed27654 | 2015-05-11 11:07:04 | [diff] [blame] | 171 | EXPECT_FALSE(properties_->name.is_valid()); |
| 172 | EXPECT_FALSE(properties_->version.is_valid()); |
| 173 | |
[email protected] | cf910da2 | 2012-02-15 04:21:08 | [diff] [blame] | 174 | WaitForGetAll(); |
| 175 | |
jpawlowski | ed27654 | 2015-05-11 11:07:04 | [diff] [blame] | 176 | EXPECT_TRUE(properties_->name.is_valid()); |
[email protected] | 8a3eaffa | 2012-03-22 20:34:05 | [diff] [blame] | 177 | EXPECT_EQ("TestService", properties_->name.value()); |
jpawlowski | ed27654 | 2015-05-11 11:07:04 | [diff] [blame] | 178 | EXPECT_TRUE(properties_->version.is_valid()); |
[email protected] | 8a3eaffa | 2012-03-22 20:34:05 | [diff] [blame] | 179 | EXPECT_EQ(10, properties_->version.value()); |
[email protected] | cf910da2 | 2012-02-15 04:21:08 | [diff] [blame] | 180 | |
| 181 | std::vector<std::string> methods = properties_->methods.value(); |
| 182 | ASSERT_EQ(4U, methods.size()); |
| 183 | EXPECT_EQ("Echo", methods[0]); |
| 184 | EXPECT_EQ("SlowEcho", methods[1]); |
| 185 | EXPECT_EQ("AsyncEcho", methods[2]); |
| 186 | EXPECT_EQ("BrokenMethod", methods[3]); |
| 187 | |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 188 | std::vector<ObjectPath> objects = properties_->objects.value(); |
[email protected] | cf910da2 | 2012-02-15 04:21:08 | [diff] [blame] | 189 | ASSERT_EQ(1U, objects.size()); |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 190 | EXPECT_EQ(ObjectPath("/TestObjectPath"), objects[0]); |
[email protected] | ebbfffa2 | 2014-03-15 07:40:49 | [diff] [blame] | 191 | |
| 192 | std::vector<uint8> bytes = properties_->bytes.value(); |
| 193 | ASSERT_EQ(4U, bytes.size()); |
| 194 | EXPECT_EQ('T', bytes[0]); |
| 195 | EXPECT_EQ('e', bytes[1]); |
| 196 | EXPECT_EQ('s', bytes[2]); |
| 197 | EXPECT_EQ('t', bytes[3]); |
[email protected] | cf910da2 | 2012-02-15 04:21:08 | [diff] [blame] | 198 | } |
| 199 | |
[email protected] | 72bbacc | 2012-03-21 23:43:45 | [diff] [blame] | 200 | TEST_F(PropertyTest, UpdatedValues) { |
| 201 | WaitForGetAll(); |
| 202 | |
| 203 | // Update the value of the "Name" property, this value should not change. |
| 204 | properties_->name.Get(base::Bind(&PropertyTest::PropertyCallback, |
| 205 | base::Unretained(this), |
| 206 | "Name")); |
| 207 | WaitForCallback("Name"); |
| 208 | WaitForUpdates(1); |
| 209 | |
[email protected] | 8a3eaffa | 2012-03-22 20:34:05 | [diff] [blame] | 210 | EXPECT_EQ("TestService", properties_->name.value()); |
[email protected] | 72bbacc | 2012-03-21 23:43:45 | [diff] [blame] | 211 | |
| 212 | // Update the value of the "Version" property, this value should be changed. |
| 213 | properties_->version.Get(base::Bind(&PropertyTest::PropertyCallback, |
| 214 | base::Unretained(this), |
| 215 | "Version")); |
| 216 | WaitForCallback("Version"); |
| 217 | WaitForUpdates(1); |
| 218 | |
[email protected] | 8a3eaffa | 2012-03-22 20:34:05 | [diff] [blame] | 219 | EXPECT_EQ(20, properties_->version.value()); |
[email protected] | 72bbacc | 2012-03-21 23:43:45 | [diff] [blame] | 220 | |
| 221 | // Update the value of the "Methods" property, this value should not change |
| 222 | // and should not grow to contain duplicate entries. |
| 223 | properties_->methods.Get(base::Bind(&PropertyTest::PropertyCallback, |
| 224 | base::Unretained(this), |
| 225 | "Methods")); |
| 226 | WaitForCallback("Methods"); |
| 227 | WaitForUpdates(1); |
| 228 | |
| 229 | std::vector<std::string> methods = properties_->methods.value(); |
| 230 | ASSERT_EQ(4U, methods.size()); |
| 231 | EXPECT_EQ("Echo", methods[0]); |
| 232 | EXPECT_EQ("SlowEcho", methods[1]); |
| 233 | EXPECT_EQ("AsyncEcho", methods[2]); |
| 234 | EXPECT_EQ("BrokenMethod", methods[3]); |
| 235 | |
| 236 | // Update the value of the "Objects" property, this value should not change |
| 237 | // and should not grow to contain duplicate entries. |
| 238 | properties_->objects.Get(base::Bind(&PropertyTest::PropertyCallback, |
| 239 | base::Unretained(this), |
| 240 | "Objects")); |
| 241 | WaitForCallback("Objects"); |
| 242 | WaitForUpdates(1); |
| 243 | |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 244 | std::vector<ObjectPath> objects = properties_->objects.value(); |
[email protected] | 72bbacc | 2012-03-21 23:43:45 | [diff] [blame] | 245 | ASSERT_EQ(1U, objects.size()); |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 246 | EXPECT_EQ(ObjectPath("/TestObjectPath"), objects[0]); |
[email protected] | ebbfffa2 | 2014-03-15 07:40:49 | [diff] [blame] | 247 | |
| 248 | // Update the value of the "Bytes" property, this value should not change |
| 249 | // and should not grow to contain duplicate entries. |
| 250 | properties_->bytes.Get(base::Bind(&PropertyTest::PropertyCallback, |
| 251 | base::Unretained(this), |
| 252 | "Bytes")); |
| 253 | WaitForCallback("Bytes"); |
| 254 | WaitForUpdates(1); |
| 255 | |
| 256 | std::vector<uint8> bytes = properties_->bytes.value(); |
| 257 | ASSERT_EQ(4U, bytes.size()); |
| 258 | EXPECT_EQ('T', bytes[0]); |
| 259 | EXPECT_EQ('e', bytes[1]); |
| 260 | EXPECT_EQ('s', bytes[2]); |
| 261 | EXPECT_EQ('t', bytes[3]); |
[email protected] | 72bbacc | 2012-03-21 23:43:45 | [diff] [blame] | 262 | } |
| 263 | |
[email protected] | cf910da2 | 2012-02-15 04:21:08 | [diff] [blame] | 264 | TEST_F(PropertyTest, Get) { |
| 265 | WaitForGetAll(); |
| 266 | |
| 267 | // Ask for the new Version property. |
| 268 | properties_->version.Get(base::Bind(&PropertyTest::PropertyCallback, |
| 269 | base::Unretained(this), |
| 270 | "Get")); |
| 271 | WaitForCallback("Get"); |
| 272 | |
| 273 | // Make sure we got a property update too. |
| 274 | WaitForUpdates(1); |
| 275 | |
[email protected] | 8a3eaffa | 2012-03-22 20:34:05 | [diff] [blame] | 276 | EXPECT_EQ(20, properties_->version.value()); |
[email protected] | cf910da2 | 2012-02-15 04:21:08 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | TEST_F(PropertyTest, Set) { |
| 280 | WaitForGetAll(); |
| 281 | |
| 282 | // Set a new name. |
| 283 | properties_->name.Set("NewService", |
| 284 | base::Bind(&PropertyTest::PropertyCallback, |
| 285 | base::Unretained(this), |
| 286 | "Set")); |
| 287 | WaitForCallback("Set"); |
| 288 | |
| 289 | // TestService sends a property update. |
| 290 | WaitForUpdates(1); |
| 291 | |
[email protected] | 8a3eaffa | 2012-03-22 20:34:05 | [diff] [blame] | 292 | EXPECT_EQ("NewService", properties_->name.value()); |
[email protected] | cf910da2 | 2012-02-15 04:21:08 | [diff] [blame] | 293 | } |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 294 | |
jpawlowski | ed27654 | 2015-05-11 11:07:04 | [diff] [blame] | 295 | TEST_F(PropertyTest, Invalidate) { |
| 296 | WaitForGetAll(); |
| 297 | |
| 298 | EXPECT_TRUE(properties_->name.is_valid()); |
| 299 | |
| 300 | // Invalidate name. |
| 301 | MethodCall method_call("org.chromium.TestInterface", "PerformAction"); |
| 302 | MessageWriter writer(&method_call); |
| 303 | writer.AppendString("InvalidateProperty"); |
| 304 | writer.AppendObjectPath(ObjectPath("/org/chromium/TestService")); |
| 305 | object_proxy_->CallMethod( |
| 306 | &method_call, ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 307 | base::Bind(&PropertyTest::MethodCallback, base::Unretained(this))); |
| 308 | WaitForMethodCallback(); |
| 309 | |
| 310 | // TestService sends a property update. |
| 311 | WaitForUpdates(1); |
| 312 | |
| 313 | EXPECT_FALSE(properties_->name.is_valid()); |
| 314 | |
| 315 | // Set name to something valid. |
| 316 | properties_->name.Set("NewService", |
| 317 | base::Bind(&PropertyTest::PropertyCallback, |
| 318 | base::Unretained(this), "Set")); |
| 319 | WaitForCallback("Set"); |
| 320 | |
| 321 | // TestService sends a property update. |
| 322 | WaitForUpdates(1); |
| 323 | |
| 324 | EXPECT_TRUE(properties_->name.is_valid()); |
| 325 | } |
| 326 | |
dtapuska | 32d2545 | 2015-02-09 16:02:55 | [diff] [blame] | 327 | TEST(PropertyTestStatic, ReadWriteStringMap) { |
| 328 | scoped_ptr<Response> message(Response::CreateEmpty()); |
| 329 | MessageWriter writer(message.get()); |
| 330 | MessageWriter variant_writer(NULL); |
| 331 | MessageWriter variant_array_writer(NULL); |
| 332 | MessageWriter struct_entry_writer(NULL); |
| 333 | |
| 334 | writer.OpenVariant("a{ss}", &variant_writer); |
| 335 | variant_writer.OpenArray("{ss}", &variant_array_writer); |
| 336 | const char* items[] = {"One", "Two", "Three", "Four"}; |
| 337 | for (unsigned i = 0; i < arraysize(items); ++i) { |
| 338 | variant_array_writer.OpenDictEntry(&struct_entry_writer); |
| 339 | struct_entry_writer.AppendString(items[i]); |
| 340 | struct_entry_writer.AppendString(base::UintToString(i + 1)); |
| 341 | variant_array_writer.CloseContainer(&struct_entry_writer); |
| 342 | } |
| 343 | variant_writer.CloseContainer(&variant_array_writer); |
| 344 | writer.CloseContainer(&variant_writer); |
| 345 | |
| 346 | MessageReader reader(message.get()); |
| 347 | Property<std::map<std::string, std::string>> string_map; |
| 348 | EXPECT_TRUE(string_map.PopValueFromReader(&reader)); |
| 349 | ASSERT_EQ(4U, string_map.value().size()); |
| 350 | EXPECT_EQ("1", string_map.value().at("One")); |
| 351 | EXPECT_EQ("2", string_map.value().at("Two")); |
| 352 | EXPECT_EQ("3", string_map.value().at("Three")); |
| 353 | EXPECT_EQ("4", string_map.value().at("Four")); |
| 354 | } |
| 355 | |
| 356 | TEST(PropertyTestStatic, SerializeStringMap) { |
| 357 | std::map<std::string, std::string> test_map; |
| 358 | test_map["Hi"] = "There"; |
| 359 | test_map["Map"] = "Test"; |
| 360 | test_map["Random"] = "Text"; |
| 361 | |
| 362 | scoped_ptr<Response> message(Response::CreateEmpty()); |
| 363 | MessageWriter writer(message.get()); |
| 364 | |
| 365 | Property<std::map<std::string, std::string>> string_map; |
| 366 | string_map.ReplaceSetValueForTesting(test_map); |
| 367 | string_map.AppendSetValueToWriter(&writer); |
| 368 | |
| 369 | MessageReader reader(message.get()); |
| 370 | EXPECT_TRUE(string_map.PopValueFromReader(&reader)); |
| 371 | EXPECT_EQ(test_map, string_map.value()); |
| 372 | } |
| 373 | |
| 374 | TEST(PropertyTestStatic, ReadWriteNetAddressArray) { |
| 375 | scoped_ptr<Response> message(Response::CreateEmpty()); |
| 376 | MessageWriter writer(message.get()); |
| 377 | MessageWriter variant_writer(NULL); |
| 378 | MessageWriter variant_array_writer(NULL); |
| 379 | MessageWriter struct_entry_writer(NULL); |
| 380 | |
| 381 | writer.OpenVariant("a(ayq)", &variant_writer); |
| 382 | variant_writer.OpenArray("(ayq)", &variant_array_writer); |
| 383 | uint8 ip_bytes[] = {0x54, 0x65, 0x73, 0x74, 0x30}; |
| 384 | for (uint16 i = 0; i < 5; ++i) { |
| 385 | variant_array_writer.OpenStruct(&struct_entry_writer); |
| 386 | ip_bytes[4] = 0x30 + i; |
| 387 | struct_entry_writer.AppendArrayOfBytes(ip_bytes, arraysize(ip_bytes)); |
| 388 | struct_entry_writer.AppendUint16(i); |
| 389 | variant_array_writer.CloseContainer(&struct_entry_writer); |
| 390 | } |
| 391 | variant_writer.CloseContainer(&variant_array_writer); |
| 392 | writer.CloseContainer(&variant_writer); |
| 393 | |
| 394 | MessageReader reader(message.get()); |
| 395 | Property<std::vector<std::pair<std::vector<uint8>, uint16>>> ip_list; |
| 396 | EXPECT_TRUE(ip_list.PopValueFromReader(&reader)); |
| 397 | |
| 398 | ASSERT_EQ(5U, ip_list.value().size()); |
| 399 | size_t item_index = 0; |
| 400 | for (auto& item : ip_list.value()) { |
| 401 | ASSERT_EQ(5U, item.first.size()); |
| 402 | ip_bytes[4] = 0x30 + item_index; |
| 403 | EXPECT_EQ(0, memcmp(ip_bytes, item.first.data(), 5U)); |
| 404 | EXPECT_EQ(item_index, item.second); |
| 405 | ++item_index; |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | TEST(PropertyTestStatic, SerializeNetAddressArray) { |
| 410 | std::vector<std::pair<std::vector<uint8>, uint16>> test_list; |
| 411 | |
| 412 | uint8 ip_bytes[] = {0x54, 0x65, 0x73, 0x74, 0x30}; |
| 413 | for (uint16 i = 0; i < 5; ++i) { |
| 414 | ip_bytes[4] = 0x30 + i; |
| 415 | std::vector<uint8> bytes(ip_bytes, ip_bytes + arraysize(ip_bytes)); |
| 416 | test_list.push_back(make_pair(bytes, 16)); |
| 417 | } |
| 418 | |
| 419 | scoped_ptr<Response> message(Response::CreateEmpty()); |
| 420 | MessageWriter writer(message.get()); |
| 421 | |
| 422 | Property<std::vector<std::pair<std::vector<uint8>, uint16>>> ip_list; |
| 423 | ip_list.ReplaceSetValueForTesting(test_list); |
| 424 | ip_list.AppendSetValueToWriter(&writer); |
| 425 | |
| 426 | MessageReader reader(message.get()); |
| 427 | EXPECT_TRUE(ip_list.PopValueFromReader(&reader)); |
| 428 | EXPECT_EQ(test_list, ip_list.value()); |
| 429 | } |
| 430 | |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 431 | } // namespace dbus |