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