[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 1 | // Copyright (c) 2013 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/object_manager.h" |
| 6 | |
avi | 22437c69 | 2015-12-22 18:12:45 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | #include <stdint.h> |
| 9 | |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 10 | #include <string> |
| 11 | #include <vector> |
| 12 | |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 13 | #include "base/bind.h" |
[email protected] | 2a9ec0e | 2013-07-17 23:00:30 | [diff] [blame] | 14 | #include "base/message_loop/message_loop.h" |
earthdok | 9562caf | 2014-09-03 10:32:36 | [diff] [blame] | 15 | #include "base/run_loop.h" |
fdoray | 6ef45cf | 2016-08-25 15:36:37 | [diff] [blame] | 16 | #include "base/single_thread_task_runner.h" |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 17 | #include "base/threading/thread.h" |
| 18 | #include "base/threading/thread_restrictions.h" |
| 19 | #include "dbus/bus.h" |
| 20 | #include "dbus/object_path.h" |
| 21 | #include "dbus/object_proxy.h" |
| 22 | #include "dbus/property.h" |
| 23 | #include "dbus/test_service.h" |
| 24 | #include "testing/gtest/include/gtest/gtest.h" |
| 25 | |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 26 | namespace dbus { |
| 27 | |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 28 | // The object manager test exercises the asynchronous APIs in ObjectManager, |
| 29 | // and by extension PropertySet and Property<>. |
| 30 | class ObjectManagerTest |
| 31 | : public testing::Test, |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 32 | public ObjectManager::Interface { |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 33 | public: |
armansito | ebff093d | 2014-09-05 17:49:34 | [diff] [blame] | 34 | ObjectManagerTest() : timeout_expired_(false) { |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 35 | } |
| 36 | |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 37 | struct Properties : public PropertySet { |
| 38 | Property<std::string> name; |
avi | 22437c69 | 2015-12-22 18:12:45 | [diff] [blame] | 39 | Property<int16_t> version; |
Ben Chan | c7c9c76 | 2017-11-08 01:50:21 | [diff] [blame^] | 40 | Property<std::vector<std::string>> methods; |
| 41 | Property<std::vector<ObjectPath>> objects; |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 42 | |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 43 | Properties(ObjectProxy* object_proxy, |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 44 | const std::string& interface_name, |
| 45 | PropertyChangedCallback property_changed_callback) |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 46 | : PropertySet(object_proxy, interface_name, property_changed_callback) { |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 47 | RegisterProperty("Name", &name); |
| 48 | RegisterProperty("Version", &version); |
| 49 | RegisterProperty("Methods", &methods); |
| 50 | RegisterProperty("Objects", &objects); |
| 51 | } |
| 52 | }; |
| 53 | |
dcheng | 3bb7119c | 2014-12-29 18:30:17 | [diff] [blame] | 54 | PropertySet* CreateProperties(ObjectProxy* object_proxy, |
| 55 | const ObjectPath& object_path, |
| 56 | const std::string& interface_name) override { |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 57 | Properties* properties = new Properties( |
| 58 | object_proxy, interface_name, |
| 59 | base::Bind(&ObjectManagerTest::OnPropertyChanged, |
| 60 | base::Unretained(this), object_path)); |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 61 | return static_cast<PropertySet*>(properties); |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 62 | } |
| 63 | |
dcheng | 3bb7119c | 2014-12-29 18:30:17 | [diff] [blame] | 64 | void SetUp() override { |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 65 | // Make the main thread not to allow IO. |
| 66 | base::ThreadRestrictions::SetIOAllowed(false); |
| 67 | |
| 68 | // Start the D-Bus thread. |
| 69 | dbus_thread_.reset(new base::Thread("D-Bus Thread")); |
| 70 | base::Thread::Options thread_options; |
[email protected] | ff33b18e | 2013-05-01 16:10:30 | [diff] [blame] | 71 | thread_options.message_loop_type = base::MessageLoop::TYPE_IO; |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 72 | ASSERT_TRUE(dbus_thread_->StartWithOptions(thread_options)); |
| 73 | |
| 74 | // Start the test service, using the D-Bus thread. |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 75 | TestService::Options options; |
skyostil | 8a033aa | 2015-06-17 15:46:04 | [diff] [blame] | 76 | options.dbus_task_runner = dbus_thread_->task_runner(); |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 77 | test_service_.reset(new TestService(options)); |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 78 | ASSERT_TRUE(test_service_->StartService()); |
| 79 | ASSERT_TRUE(test_service_->WaitUntilServiceIsStarted()); |
| 80 | ASSERT_TRUE(test_service_->HasDBusThread()); |
| 81 | |
| 82 | // Create the client, using the D-Bus thread. |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 83 | Bus::Options bus_options; |
| 84 | bus_options.bus_type = Bus::SESSION; |
| 85 | bus_options.connection_type = Bus::PRIVATE; |
skyostil | 8a033aa | 2015-06-17 15:46:04 | [diff] [blame] | 86 | bus_options.dbus_task_runner = dbus_thread_->task_runner(); |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 87 | bus_ = new Bus(bus_options); |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 88 | ASSERT_TRUE(bus_->HasDBusThread()); |
| 89 | |
| 90 | object_manager_ = bus_->GetObjectManager( |
hashimoto | 067d84f52 | 2016-01-05 08:48:03 | [diff] [blame] | 91 | test_service_->service_name(), |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 92 | ObjectPath("/org/chromium/TestService")); |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 93 | object_manager_->RegisterInterface("org.chromium.TestInterface", this); |
| 94 | |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 95 | WaitForObject(); |
| 96 | } |
| 97 | |
dcheng | 3bb7119c | 2014-12-29 18:30:17 | [diff] [blame] | 98 | void TearDown() override { |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 99 | bus_->ShutdownOnDBusThreadAndBlock(); |
| 100 | |
| 101 | // Shut down the service. |
| 102 | test_service_->ShutdownAndBlock(); |
| 103 | |
| 104 | // Reset to the default. |
| 105 | base::ThreadRestrictions::SetIOAllowed(true); |
| 106 | |
| 107 | // Stopping a thread is considered an IO operation, so do this after |
| 108 | // allowing IO. |
| 109 | test_service_->Stop(); |
earthdok | 9562caf | 2014-09-03 10:32:36 | [diff] [blame] | 110 | |
| 111 | base::RunLoop().RunUntilIdle(); |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 112 | } |
| 113 | |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 114 | void MethodCallback(Response* response) { |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 115 | method_callback_called_ = true; |
earthdok | 9562caf | 2014-09-03 10:32:36 | [diff] [blame] | 116 | run_loop_->Quit(); |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 117 | } |
| 118 | |
armansito | ebff093d | 2014-09-05 17:49:34 | [diff] [blame] | 119 | // Called from the PropertiesChangedAsObjectsReceived test case. The test will |
| 120 | // not run the message loop if it receives the expected PropertiesChanged |
| 121 | // signal before the timeout. This method immediately fails the test. |
| 122 | void PropertiesChangedTestTimeout() { |
| 123 | timeout_expired_ = true; |
| 124 | run_loop_->Quit(); |
| 125 | |
| 126 | FAIL() << "Never received PropertiesChanged"; |
| 127 | } |
| 128 | |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 129 | protected: |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 130 | // Called when an object is added. |
dcheng | 3bb7119c | 2014-12-29 18:30:17 | [diff] [blame] | 131 | void ObjectAdded(const ObjectPath& object_path, |
| 132 | const std::string& interface_name) override { |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 133 | added_objects_.push_back(std::make_pair(object_path, interface_name)); |
earthdok | 9562caf | 2014-09-03 10:32:36 | [diff] [blame] | 134 | run_loop_->Quit(); |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | // Called when an object is removed. |
dcheng | 3bb7119c | 2014-12-29 18:30:17 | [diff] [blame] | 138 | void ObjectRemoved(const ObjectPath& object_path, |
| 139 | const std::string& interface_name) override { |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 140 | removed_objects_.push_back(std::make_pair(object_path, interface_name)); |
earthdok | 9562caf | 2014-09-03 10:32:36 | [diff] [blame] | 141 | run_loop_->Quit(); |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | // Called when a property value is updated. |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 145 | void OnPropertyChanged(const ObjectPath& object_path, |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 146 | const std::string& name) { |
armansito | ebff093d | 2014-09-05 17:49:34 | [diff] [blame] | 147 | // Store the value of the "Name" property if that's the one that |
| 148 | // changed. |
| 149 | Properties* properties = static_cast<Properties*>( |
| 150 | object_manager_->GetProperties( |
| 151 | object_path, |
| 152 | "org.chromium.TestInterface")); |
| 153 | if (name == properties->name.name()) |
| 154 | last_name_value_ = properties->name.value(); |
| 155 | |
| 156 | // Store the updated property. |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 157 | updated_properties_.push_back(name); |
earthdok | 9562caf | 2014-09-03 10:32:36 | [diff] [blame] | 158 | run_loop_->Quit(); |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | static const size_t kExpectedObjects = 1; |
| 162 | static const size_t kExpectedProperties = 4; |
| 163 | |
| 164 | void WaitForObject() { |
| 165 | while (added_objects_.size() < kExpectedObjects || |
earthdok | 9562caf | 2014-09-03 10:32:36 | [diff] [blame] | 166 | updated_properties_.size() < kExpectedProperties) { |
| 167 | run_loop_.reset(new base::RunLoop); |
| 168 | run_loop_->Run(); |
| 169 | } |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 170 | for (size_t i = 0; i < kExpectedObjects; ++i) |
| 171 | added_objects_.erase(added_objects_.begin()); |
| 172 | for (size_t i = 0; i < kExpectedProperties; ++i) |
| 173 | updated_properties_.erase(updated_properties_.begin()); |
| 174 | } |
| 175 | |
| 176 | void WaitForRemoveObject() { |
earthdok | 9562caf | 2014-09-03 10:32:36 | [diff] [blame] | 177 | while (removed_objects_.size() < kExpectedObjects) { |
| 178 | run_loop_.reset(new base::RunLoop); |
| 179 | run_loop_->Run(); |
| 180 | } |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 181 | for (size_t i = 0; i < kExpectedObjects; ++i) |
| 182 | removed_objects_.erase(removed_objects_.begin()); |
| 183 | } |
| 184 | |
| 185 | void WaitForMethodCallback() { |
earthdok | 9562caf | 2014-09-03 10:32:36 | [diff] [blame] | 186 | run_loop_.reset(new base::RunLoop); |
| 187 | run_loop_->Run(); |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 188 | method_callback_called_ = false; |
| 189 | } |
| 190 | |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 191 | void PerformAction(const std::string& action, const ObjectPath& object_path) { |
| 192 | ObjectProxy* object_proxy = bus_->GetObjectProxy( |
hashimoto | 067d84f52 | 2016-01-05 08:48:03 | [diff] [blame] | 193 | test_service_->service_name(), |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 194 | ObjectPath("/org/chromium/TestObject")); |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 195 | |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 196 | MethodCall method_call("org.chromium.TestInterface", "PerformAction"); |
| 197 | MessageWriter writer(&method_call); |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 198 | writer.AppendString(action); |
| 199 | writer.AppendObjectPath(object_path); |
| 200 | |
| 201 | object_proxy->CallMethod(&method_call, |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 202 | ObjectProxy::TIMEOUT_USE_DEFAULT, |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 203 | base::Bind(&ObjectManagerTest::MethodCallback, |
| 204 | base::Unretained(this))); |
| 205 | WaitForMethodCallback(); |
| 206 | } |
| 207 | |
[email protected] | ff33b18e | 2013-05-01 16:10:30 | [diff] [blame] | 208 | base::MessageLoop message_loop_; |
dcheng | 2a19328 | 2016-04-08 22:55:04 | [diff] [blame] | 209 | std::unique_ptr<base::RunLoop> run_loop_; |
| 210 | std::unique_ptr<base::Thread> dbus_thread_; |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 211 | scoped_refptr<Bus> bus_; |
| 212 | ObjectManager* object_manager_; |
dcheng | 2a19328 | 2016-04-08 22:55:04 | [diff] [blame] | 213 | std::unique_ptr<TestService> test_service_; |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 214 | |
armansito | ebff093d | 2014-09-05 17:49:34 | [diff] [blame] | 215 | std::string last_name_value_; |
| 216 | bool timeout_expired_; |
| 217 | |
Ben Chan | c7c9c76 | 2017-11-08 01:50:21 | [diff] [blame^] | 218 | std::vector<std::pair<ObjectPath, std::string>> added_objects_; |
| 219 | std::vector<std::pair<ObjectPath, std::string>> removed_objects_; |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 220 | std::vector<std::string> updated_properties_; |
| 221 | |
| 222 | bool method_callback_called_; |
| 223 | }; |
| 224 | |
| 225 | |
| 226 | TEST_F(ObjectManagerTest, InitialObject) { |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 227 | ObjectProxy* object_proxy = object_manager_->GetObjectProxy( |
| 228 | ObjectPath("/org/chromium/TestObject")); |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 229 | EXPECT_TRUE(object_proxy != NULL); |
| 230 | |
| 231 | Properties* properties = static_cast<Properties*>( |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 232 | object_manager_->GetProperties(ObjectPath("/org/chromium/TestObject"), |
| 233 | "org.chromium.TestInterface")); |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 234 | EXPECT_TRUE(properties != NULL); |
| 235 | |
| 236 | EXPECT_EQ("TestService", properties->name.value()); |
| 237 | EXPECT_EQ(10, properties->version.value()); |
| 238 | |
| 239 | std::vector<std::string> methods = properties->methods.value(); |
| 240 | ASSERT_EQ(4U, methods.size()); |
| 241 | EXPECT_EQ("Echo", methods[0]); |
| 242 | EXPECT_EQ("SlowEcho", methods[1]); |
| 243 | EXPECT_EQ("AsyncEcho", methods[2]); |
| 244 | EXPECT_EQ("BrokenMethod", methods[3]); |
| 245 | |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 246 | std::vector<ObjectPath> objects = properties->objects.value(); |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 247 | ASSERT_EQ(1U, objects.size()); |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 248 | EXPECT_EQ(ObjectPath("/TestObjectPath"), objects[0]); |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | TEST_F(ObjectManagerTest, UnknownObjectProxy) { |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 252 | ObjectProxy* object_proxy = object_manager_->GetObjectProxy( |
| 253 | ObjectPath("/org/chromium/UnknownObject")); |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 254 | EXPECT_TRUE(object_proxy == NULL); |
| 255 | } |
| 256 | |
| 257 | TEST_F(ObjectManagerTest, UnknownObjectProperties) { |
| 258 | Properties* properties = static_cast<Properties*>( |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 259 | object_manager_->GetProperties(ObjectPath("/org/chromium/UnknownObject"), |
| 260 | "org.chromium.TestInterface")); |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 261 | EXPECT_TRUE(properties == NULL); |
| 262 | } |
| 263 | |
| 264 | TEST_F(ObjectManagerTest, UnknownInterfaceProperties) { |
| 265 | Properties* properties = static_cast<Properties*>( |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 266 | object_manager_->GetProperties(ObjectPath("/org/chromium/TestObject"), |
| 267 | "org.chromium.UnknownService")); |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 268 | EXPECT_TRUE(properties == NULL); |
| 269 | } |
| 270 | |
| 271 | TEST_F(ObjectManagerTest, GetObjects) { |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 272 | std::vector<ObjectPath> object_paths = object_manager_->GetObjects(); |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 273 | ASSERT_EQ(1U, object_paths.size()); |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 274 | EXPECT_EQ(ObjectPath("/org/chromium/TestObject"), object_paths[0]); |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | TEST_F(ObjectManagerTest, GetObjectsWithInterface) { |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 278 | std::vector<ObjectPath> object_paths = |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 279 | object_manager_->GetObjectsWithInterface("org.chromium.TestInterface"); |
| 280 | ASSERT_EQ(1U, object_paths.size()); |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 281 | EXPECT_EQ(ObjectPath("/org/chromium/TestObject"), object_paths[0]); |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | TEST_F(ObjectManagerTest, GetObjectsWithUnknownInterface) { |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 285 | std::vector<ObjectPath> object_paths = |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 286 | object_manager_->GetObjectsWithInterface("org.chromium.UnknownService"); |
| 287 | EXPECT_EQ(0U, object_paths.size()); |
| 288 | } |
| 289 | |
| 290 | TEST_F(ObjectManagerTest, SameObject) { |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 291 | ObjectManager* object_manager = bus_->GetObjectManager( |
hashimoto | 067d84f52 | 2016-01-05 08:48:03 | [diff] [blame] | 292 | test_service_->service_name(), |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 293 | ObjectPath("/org/chromium/TestService")); |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 294 | EXPECT_EQ(object_manager_, object_manager); |
| 295 | } |
| 296 | |
| 297 | TEST_F(ObjectManagerTest, DifferentObjectForService) { |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 298 | ObjectManager* object_manager = bus_->GetObjectManager( |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 299 | "org.chromium.DifferentService", |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 300 | ObjectPath("/org/chromium/TestService")); |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 301 | EXPECT_NE(object_manager_, object_manager); |
| 302 | } |
| 303 | |
| 304 | TEST_F(ObjectManagerTest, DifferentObjectForPath) { |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 305 | ObjectManager* object_manager = bus_->GetObjectManager( |
hashimoto | 067d84f52 | 2016-01-05 08:48:03 | [diff] [blame] | 306 | test_service_->service_name(), |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 307 | ObjectPath("/org/chromium/DifferentService")); |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 308 | EXPECT_NE(object_manager_, object_manager); |
| 309 | } |
| 310 | |
| 311 | TEST_F(ObjectManagerTest, SecondObject) { |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 312 | PerformAction("AddObject", ObjectPath("/org/chromium/SecondObject")); |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 313 | WaitForObject(); |
| 314 | |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 315 | ObjectProxy* object_proxy = object_manager_->GetObjectProxy( |
| 316 | ObjectPath("/org/chromium/SecondObject")); |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 317 | EXPECT_TRUE(object_proxy != NULL); |
| 318 | |
| 319 | Properties* properties = static_cast<Properties*>( |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 320 | object_manager_->GetProperties(ObjectPath("/org/chromium/SecondObject"), |
| 321 | "org.chromium.TestInterface")); |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 322 | EXPECT_TRUE(properties != NULL); |
| 323 | |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 324 | std::vector<ObjectPath> object_paths = object_manager_->GetObjects(); |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 325 | ASSERT_EQ(2U, object_paths.size()); |
| 326 | |
| 327 | std::sort(object_paths.begin(), object_paths.end()); |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 328 | EXPECT_EQ(ObjectPath("/org/chromium/SecondObject"), object_paths[0]); |
| 329 | EXPECT_EQ(ObjectPath("/org/chromium/TestObject"), object_paths[1]); |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 330 | |
| 331 | object_paths = |
| 332 | object_manager_->GetObjectsWithInterface("org.chromium.TestInterface"); |
| 333 | ASSERT_EQ(2U, object_paths.size()); |
| 334 | |
| 335 | std::sort(object_paths.begin(), object_paths.end()); |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 336 | EXPECT_EQ(ObjectPath("/org/chromium/SecondObject"), object_paths[0]); |
| 337 | EXPECT_EQ(ObjectPath("/org/chromium/TestObject"), object_paths[1]); |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | TEST_F(ObjectManagerTest, RemoveSecondObject) { |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 341 | PerformAction("AddObject", ObjectPath("/org/chromium/SecondObject")); |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 342 | WaitForObject(); |
| 343 | |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 344 | std::vector<ObjectPath> object_paths = object_manager_->GetObjects(); |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 345 | ASSERT_EQ(2U, object_paths.size()); |
| 346 | |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 347 | PerformAction("RemoveObject", ObjectPath("/org/chromium/SecondObject")); |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 348 | WaitForRemoveObject(); |
| 349 | |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 350 | ObjectProxy* object_proxy = object_manager_->GetObjectProxy( |
| 351 | ObjectPath("/org/chromium/SecondObject")); |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 352 | EXPECT_TRUE(object_proxy == NULL); |
| 353 | |
| 354 | Properties* properties = static_cast<Properties*>( |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 355 | object_manager_->GetProperties(ObjectPath("/org/chromium/SecondObject"), |
| 356 | "org.chromium.TestInterface")); |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 357 | EXPECT_TRUE(properties == NULL); |
| 358 | |
| 359 | object_paths = object_manager_->GetObjects(); |
| 360 | ASSERT_EQ(1U, object_paths.size()); |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 361 | EXPECT_EQ(ObjectPath("/org/chromium/TestObject"), object_paths[0]); |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 362 | |
| 363 | object_paths = |
| 364 | object_manager_->GetObjectsWithInterface("org.chromium.TestInterface"); |
| 365 | ASSERT_EQ(1U, object_paths.size()); |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 366 | EXPECT_EQ(ObjectPath("/org/chromium/TestObject"), object_paths[0]); |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 367 | } |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 368 | |
[email protected] | 043fb8c | 2014-03-07 02:24:33 | [diff] [blame] | 369 | TEST_F(ObjectManagerTest, OwnershipLost) { |
| 370 | PerformAction("ReleaseOwnership", ObjectPath("/org/chromium/TestService")); |
| 371 | WaitForRemoveObject(); |
| 372 | |
| 373 | std::vector<ObjectPath> object_paths = object_manager_->GetObjects(); |
| 374 | ASSERT_EQ(0U, object_paths.size()); |
| 375 | } |
| 376 | |
| 377 | TEST_F(ObjectManagerTest, OwnershipLostAndRegained) { |
| 378 | PerformAction("Ownership", ObjectPath("/org/chromium/TestService")); |
| 379 | WaitForRemoveObject(); |
| 380 | WaitForObject(); |
| 381 | |
| 382 | std::vector<ObjectPath> object_paths = object_manager_->GetObjects(); |
| 383 | ASSERT_EQ(1U, object_paths.size()); |
| 384 | } |
| 385 | |
armansito | ebff093d | 2014-09-05 17:49:34 | [diff] [blame] | 386 | TEST_F(ObjectManagerTest, PropertiesChangedAsObjectsReceived) { |
| 387 | // Remove the existing object manager. |
| 388 | object_manager_->UnregisterInterface("org.chromium.TestInterface"); |
| 389 | run_loop_.reset(new base::RunLoop); |
| 390 | EXPECT_TRUE(bus_->RemoveObjectManager( |
hashimoto | 067d84f52 | 2016-01-05 08:48:03 | [diff] [blame] | 391 | test_service_->service_name(), |
armansito | ebff093d | 2014-09-05 17:49:34 | [diff] [blame] | 392 | ObjectPath("/org/chromium/TestService"), |
| 393 | run_loop_->QuitClosure())); |
| 394 | run_loop_->Run(); |
| 395 | |
| 396 | PerformAction("SetSendImmediatePropertiesChanged", |
| 397 | ObjectPath("/org/chromium/TestService")); |
| 398 | |
| 399 | object_manager_ = bus_->GetObjectManager( |
hashimoto | 067d84f52 | 2016-01-05 08:48:03 | [diff] [blame] | 400 | test_service_->service_name(), |
armansito | ebff093d | 2014-09-05 17:49:34 | [diff] [blame] | 401 | ObjectPath("/org/chromium/TestService")); |
| 402 | object_manager_->RegisterInterface("org.chromium.TestInterface", this); |
| 403 | |
| 404 | // The newly created object manager should call GetManagedObjects immediately |
| 405 | // after setting up the match rule for PropertiesChanged. We should process |
| 406 | // the PropertiesChanged event right after that. If we don't receive it within |
| 407 | // 2 seconds, then fail the test. |
fdoray | 6ef45cf | 2016-08-25 15:36:37 | [diff] [blame] | 408 | message_loop_.task_runner()->PostDelayedTask( |
| 409 | FROM_HERE, base::Bind(&ObjectManagerTest::PropertiesChangedTestTimeout, |
| 410 | base::Unretained(this)), |
armansito | ebff093d | 2014-09-05 17:49:34 | [diff] [blame] | 411 | base::TimeDelta::FromSeconds(2)); |
| 412 | |
| 413 | while (last_name_value_ != "ChangedTestServiceName" && !timeout_expired_) { |
| 414 | run_loop_.reset(new base::RunLoop); |
| 415 | run_loop_->Run(); |
| 416 | } |
| 417 | } |
| 418 | |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 419 | } // namespace dbus |