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