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