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