[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" |
Carlos Caballero | dd8bf7b04 | 2019-07-30 14:14:15 | [diff] [blame] | 14 | #include "base/message_loop/message_pump_type.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" |
Gabriel Charette | c710874 | 2019-08-23 03:31:40 | [diff] [blame] | 17 | #include "base/test/task_environment.h" |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 18 | #include "base/threading/thread.h" |
| 19 | #include "base/threading/thread_restrictions.h" |
| 20 | #include "dbus/bus.h" |
| 21 | #include "dbus/object_path.h" |
| 22 | #include "dbus/object_proxy.h" |
| 23 | #include "dbus/property.h" |
| 24 | #include "dbus/test_service.h" |
| 25 | #include "testing/gtest/include/gtest/gtest.h" |
| 26 | |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 27 | namespace dbus { |
| 28 | |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 29 | // The object manager test exercises the asynchronous APIs in ObjectManager, |
| 30 | // and by extension PropertySet and Property<>. |
| 31 | class ObjectManagerTest |
| 32 | : public testing::Test, |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 33 | public ObjectManager::Interface { |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 34 | public: |
armansito | ebff093d | 2014-09-05 17:49:34 | [diff] [blame] | 35 | ObjectManagerTest() : timeout_expired_(false) { |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 36 | } |
| 37 | |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 38 | struct Properties : public PropertySet { |
| 39 | Property<std::string> name; |
avi | 22437c69 | 2015-12-22 18:12:45 | [diff] [blame] | 40 | Property<int16_t> version; |
Ben Chan | c7c9c76 | 2017-11-08 01:50:21 | [diff] [blame] | 41 | Property<std::vector<std::string>> methods; |
| 42 | Property<std::vector<ObjectPath>> objects; |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 43 | |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 44 | Properties(ObjectProxy* object_proxy, |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 45 | const std::string& interface_name, |
| 46 | PropertyChangedCallback property_changed_callback) |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 47 | : PropertySet(object_proxy, interface_name, property_changed_callback) { |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 48 | RegisterProperty("Name", &name); |
| 49 | RegisterProperty("Version", &version); |
| 50 | RegisterProperty("Methods", &methods); |
| 51 | RegisterProperty("Objects", &objects); |
| 52 | } |
| 53 | }; |
| 54 | |
dcheng | 3bb7119c | 2014-12-29 18:30:17 | [diff] [blame] | 55 | PropertySet* CreateProperties(ObjectProxy* object_proxy, |
| 56 | const ObjectPath& object_path, |
| 57 | const std::string& interface_name) override { |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 58 | Properties* properties = new Properties( |
| 59 | object_proxy, interface_name, |
Reilly Grant | d4e6613 | 2019-11-22 17:14:50 | [diff] [blame^] | 60 | base::BindRepeating(&ObjectManagerTest::OnPropertyChanged, |
| 61 | base::Unretained(this), object_path)); |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 62 | return static_cast<PropertySet*>(properties); |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 63 | } |
| 64 | |
dcheng | 3bb7119c | 2014-12-29 18:30:17 | [diff] [blame] | 65 | void SetUp() override { |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 66 | // Make the main thread not to allow IO. |
| 67 | base::ThreadRestrictions::SetIOAllowed(false); |
| 68 | |
| 69 | // Start the D-Bus thread. |
| 70 | dbus_thread_.reset(new base::Thread("D-Bus Thread")); |
| 71 | base::Thread::Options thread_options; |
Carlos Caballero | dd8bf7b04 | 2019-07-30 14:14:15 | [diff] [blame] | 72 | thread_options.message_pump_type = base::MessagePumpType::IO; |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 73 | ASSERT_TRUE(dbus_thread_->StartWithOptions(thread_options)); |
| 74 | |
| 75 | // Start the test service, using the D-Bus thread. |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 76 | TestService::Options options; |
skyostil | 8a033aa | 2015-06-17 15:46:04 | [diff] [blame] | 77 | options.dbus_task_runner = dbus_thread_->task_runner(); |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 78 | test_service_.reset(new TestService(options)); |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 79 | ASSERT_TRUE(test_service_->StartService()); |
Ryo Hashimoto | 3bbeb5c | 2018-08-13 04:58:39 | [diff] [blame] | 80 | test_service_->WaitUntilServiceIsStarted(); |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 81 | ASSERT_TRUE(test_service_->HasDBusThread()); |
| 82 | |
| 83 | // Create the client, using the D-Bus thread. |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 84 | Bus::Options bus_options; |
| 85 | bus_options.bus_type = Bus::SESSION; |
| 86 | bus_options.connection_type = Bus::PRIVATE; |
skyostil | 8a033aa | 2015-06-17 15:46:04 | [diff] [blame] | 87 | bus_options.dbus_task_runner = dbus_thread_->task_runner(); |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 88 | bus_ = new Bus(bus_options); |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 89 | ASSERT_TRUE(bus_->HasDBusThread()); |
| 90 | |
| 91 | object_manager_ = bus_->GetObjectManager( |
hashimoto | 067d84f52 | 2016-01-05 08:48:03 | [diff] [blame] | 92 | test_service_->service_name(), |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 93 | ObjectPath("/org/chromium/TestService")); |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 94 | object_manager_->RegisterInterface("org.chromium.TestInterface", this); |
| 95 | |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 96 | WaitForObject(); |
| 97 | } |
| 98 | |
dcheng | 3bb7119c | 2014-12-29 18:30:17 | [diff] [blame] | 99 | void TearDown() override { |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 100 | bus_->ShutdownOnDBusThreadAndBlock(); |
| 101 | |
| 102 | // Shut down the service. |
| 103 | test_service_->ShutdownAndBlock(); |
| 104 | |
| 105 | // Reset to the default. |
| 106 | base::ThreadRestrictions::SetIOAllowed(true); |
| 107 | |
| 108 | // Stopping a thread is considered an IO operation, so do this after |
| 109 | // allowing IO. |
| 110 | test_service_->Stop(); |
earthdok | 9562caf | 2014-09-03 10:32:36 | [diff] [blame] | 111 | |
| 112 | base::RunLoop().RunUntilIdle(); |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 113 | } |
| 114 | |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 115 | void MethodCallback(Response* response) { |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 116 | method_callback_called_ = true; |
earthdok | 9562caf | 2014-09-03 10:32:36 | [diff] [blame] | 117 | run_loop_->Quit(); |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 118 | } |
| 119 | |
armansito | ebff093d | 2014-09-05 17:49:34 | [diff] [blame] | 120 | // Called from the PropertiesChangedAsObjectsReceived test case. The test will |
| 121 | // not run the message loop if it receives the expected PropertiesChanged |
| 122 | // signal before the timeout. This method immediately fails the test. |
| 123 | void PropertiesChangedTestTimeout() { |
| 124 | timeout_expired_ = true; |
| 125 | run_loop_->Quit(); |
| 126 | |
| 127 | FAIL() << "Never received PropertiesChanged"; |
| 128 | } |
| 129 | |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 130 | protected: |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 131 | // Called when an object is added. |
dcheng | 3bb7119c | 2014-12-29 18:30:17 | [diff] [blame] | 132 | void ObjectAdded(const ObjectPath& object_path, |
| 133 | const std::string& interface_name) override { |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 134 | added_objects_.push_back(std::make_pair(object_path, interface_name)); |
earthdok | 9562caf | 2014-09-03 10:32:36 | [diff] [blame] | 135 | run_loop_->Quit(); |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | // Called when an object is removed. |
dcheng | 3bb7119c | 2014-12-29 18:30:17 | [diff] [blame] | 139 | void ObjectRemoved(const ObjectPath& object_path, |
| 140 | const std::string& interface_name) override { |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 141 | removed_objects_.push_back(std::make_pair(object_path, interface_name)); |
earthdok | 9562caf | 2014-09-03 10:32:36 | [diff] [blame] | 142 | run_loop_->Quit(); |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | // Called when a property value is updated. |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 146 | void OnPropertyChanged(const ObjectPath& object_path, |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 147 | const std::string& name) { |
armansito | ebff093d | 2014-09-05 17:49:34 | [diff] [blame] | 148 | // Store the value of the "Name" property if that's the one that |
| 149 | // changed. |
| 150 | Properties* properties = static_cast<Properties*>( |
| 151 | object_manager_->GetProperties( |
| 152 | object_path, |
| 153 | "org.chromium.TestInterface")); |
| 154 | if (name == properties->name.name()) |
| 155 | last_name_value_ = properties->name.value(); |
| 156 | |
| 157 | // Store the updated property. |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 158 | updated_properties_.push_back(name); |
earthdok | 9562caf | 2014-09-03 10:32:36 | [diff] [blame] | 159 | run_loop_->Quit(); |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | static const size_t kExpectedObjects = 1; |
| 163 | static const size_t kExpectedProperties = 4; |
| 164 | |
| 165 | void WaitForObject() { |
| 166 | while (added_objects_.size() < kExpectedObjects || |
earthdok | 9562caf | 2014-09-03 10:32:36 | [diff] [blame] | 167 | updated_properties_.size() < kExpectedProperties) { |
| 168 | run_loop_.reset(new base::RunLoop); |
| 169 | run_loop_->Run(); |
| 170 | } |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 171 | for (size_t i = 0; i < kExpectedObjects; ++i) |
| 172 | added_objects_.erase(added_objects_.begin()); |
| 173 | for (size_t i = 0; i < kExpectedProperties; ++i) |
| 174 | updated_properties_.erase(updated_properties_.begin()); |
| 175 | } |
| 176 | |
| 177 | void WaitForRemoveObject() { |
earthdok | 9562caf | 2014-09-03 10:32:36 | [diff] [blame] | 178 | while (removed_objects_.size() < kExpectedObjects) { |
| 179 | run_loop_.reset(new base::RunLoop); |
| 180 | run_loop_->Run(); |
| 181 | } |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 182 | for (size_t i = 0; i < kExpectedObjects; ++i) |
| 183 | removed_objects_.erase(removed_objects_.begin()); |
| 184 | } |
| 185 | |
| 186 | void WaitForMethodCallback() { |
earthdok | 9562caf | 2014-09-03 10:32:36 | [diff] [blame] | 187 | run_loop_.reset(new base::RunLoop); |
| 188 | run_loop_->Run(); |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 189 | method_callback_called_ = false; |
| 190 | } |
| 191 | |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 192 | void PerformAction(const std::string& action, const ObjectPath& object_path) { |
| 193 | ObjectProxy* object_proxy = bus_->GetObjectProxy( |
hashimoto | 067d84f52 | 2016-01-05 08:48:03 | [diff] [blame] | 194 | test_service_->service_name(), |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 195 | ObjectPath("/org/chromium/TestObject")); |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 196 | |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 197 | MethodCall method_call("org.chromium.TestInterface", "PerformAction"); |
| 198 | MessageWriter writer(&method_call); |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 199 | writer.AppendString(action); |
| 200 | writer.AppendObjectPath(object_path); |
| 201 | |
Reilly Grant | d4e6613 | 2019-11-22 17:14:50 | [diff] [blame^] | 202 | object_proxy->CallMethod(&method_call, ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 203 | base::BindOnce(&ObjectManagerTest::MethodCallback, |
| 204 | base::Unretained(this))); |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 205 | WaitForMethodCallback(); |
| 206 | } |
| 207 | |
Gabriel Charette | 1858a23 | 2019-09-09 07:50:49 | [diff] [blame] | 208 | base::test::SingleThreadTaskEnvironment task_environment_; |
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")); |
Ben Chan | 14d5003 | 2017-11-09 20:20:16 | [diff] [blame] | 229 | EXPECT_NE(nullptr, object_proxy); |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 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")); |
Ben Chan | 14d5003 | 2017-11-09 20:20:16 | [diff] [blame] | 234 | EXPECT_NE(nullptr, properties); |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 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")); |
Ben Chan | 14d5003 | 2017-11-09 20:20:16 | [diff] [blame] | 254 | EXPECT_EQ(nullptr, object_proxy); |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 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")); |
Ben Chan | 14d5003 | 2017-11-09 20:20:16 | [diff] [blame] | 261 | EXPECT_EQ(nullptr, properties); |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 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")); |
Ben Chan | 14d5003 | 2017-11-09 20:20:16 | [diff] [blame] | 268 | EXPECT_EQ(nullptr, properties); |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 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")); |
Ben Chan | 14d5003 | 2017-11-09 20:20:16 | [diff] [blame] | 317 | EXPECT_NE(nullptr, object_proxy); |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 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")); |
Ben Chan | 14d5003 | 2017-11-09 20:20:16 | [diff] [blame] | 322 | EXPECT_NE(nullptr, properties); |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 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")); |
Ben Chan | 14d5003 | 2017-11-09 20:20:16 | [diff] [blame] | 352 | EXPECT_EQ(nullptr, object_proxy); |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 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")); |
Ben Chan | 14d5003 | 2017-11-09 20:20:16 | [diff] [blame] | 357 | EXPECT_EQ(nullptr, properties); |
[email protected] | 9cc40cb | 2013-03-25 18:20:08 | [diff] [blame] | 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. |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 408 | task_environment_.GetMainThreadTaskRunner()->PostDelayedTask( |
kylechar | 39c3928 | 2019-02-19 19:04:04 | [diff] [blame] | 409 | FROM_HERE, |
| 410 | base::BindOnce(&ObjectManagerTest::PropertiesChangedTestTimeout, |
| 411 | base::Unretained(this)), |
armansito | ebff093d | 2014-09-05 17:49:34 | [diff] [blame] | 412 | base::TimeDelta::FromSeconds(2)); |
| 413 | |
| 414 | while (last_name_value_ != "ChangedTestServiceName" && !timeout_expired_) { |
| 415 | run_loop_.reset(new base::RunLoop); |
| 416 | run_loop_->Run(); |
| 417 | } |
| 418 | } |
| 419 | |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 420 | } // namespace dbus |