blob: ae03776a51df59a8a7e9903aa92aecb20ead67b4 [file] [log] [blame]
keybuk@chromium.org09715012013-03-26 03:20:08 +09001// 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
avi0ad0ce02015-12-23 03:12:45 +09007#include <stddef.h>
8#include <stdint.h>
9
keybuk@chromium.org09715012013-03-26 03:20:08 +090010#include <string>
11#include <vector>
12
keybuk@chromium.org09715012013-03-26 03:20:08 +090013#include "base/bind.h"
avi@chromium.orga29af562013-07-18 08:00:30 +090014#include "base/message_loop/message_loop.h"
earthdok64401d72014-09-03 19:32:36 +090015#include "base/run_loop.h"
keybuk@chromium.org09715012013-03-26 03:20:08 +090016#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.orgf0b7eac2013-06-13 15:37:19 +090025namespace dbus {
26
keybuk@chromium.org09715012013-03-26 03:20:08 +090027// The object manager test exercises the asynchronous APIs in ObjectManager,
28// and by extension PropertySet and Property<>.
29class ObjectManagerTest
30 : public testing::Test,
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090031 public ObjectManager::Interface {
keybuk@chromium.org09715012013-03-26 03:20:08 +090032 public:
armansitof4364642014-09-06 02:49:34 +090033 ObjectManagerTest() : timeout_expired_(false) {
keybuk@chromium.org09715012013-03-26 03:20:08 +090034 }
35
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090036 struct Properties : public PropertySet {
37 Property<std::string> name;
avi0ad0ce02015-12-23 03:12:45 +090038 Property<int16_t> version;
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090039 Property<std::vector<std::string> > methods;
40 Property<std::vector<ObjectPath> > objects;
keybuk@chromium.org09715012013-03-26 03:20:08 +090041
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090042 Properties(ObjectProxy* object_proxy,
keybuk@chromium.org09715012013-03-26 03:20:08 +090043 const std::string& interface_name,
44 PropertyChangedCallback property_changed_callback)
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090045 : PropertySet(object_proxy, interface_name, property_changed_callback) {
keybuk@chromium.org09715012013-03-26 03:20:08 +090046 RegisterProperty("Name", &name);
47 RegisterProperty("Version", &version);
48 RegisterProperty("Methods", &methods);
49 RegisterProperty("Objects", &objects);
50 }
51 };
52
dcheng7f5750d2014-12-30 03:30:17 +090053 PropertySet* CreateProperties(ObjectProxy* object_proxy,
54 const ObjectPath& object_path,
55 const std::string& interface_name) override {
keybuk@chromium.org09715012013-03-26 03:20:08 +090056 Properties* properties = new Properties(
57 object_proxy, interface_name,
58 base::Bind(&ObjectManagerTest::OnPropertyChanged,
59 base::Unretained(this), object_path));
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090060 return static_cast<PropertySet*>(properties);
keybuk@chromium.org09715012013-03-26 03:20:08 +090061 }
62
dcheng7f5750d2014-12-30 03:30:17 +090063 void SetUp() override {
keybuk@chromium.org09715012013-03-26 03:20:08 +090064 // 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.orgdff6b132013-05-02 01:10:30 +090070 thread_options.message_loop_type = base::MessageLoop::TYPE_IO;
keybuk@chromium.org09715012013-03-26 03:20:08 +090071 ASSERT_TRUE(dbus_thread_->StartWithOptions(thread_options));
72
73 // Start the test service, using the D-Bus thread.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090074 TestService::Options options;
skyostile5a8dc42015-06-18 00:46:04 +090075 options.dbus_task_runner = dbus_thread_->task_runner();
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090076 test_service_.reset(new TestService(options));
keybuk@chromium.org09715012013-03-26 03:20:08 +090077 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.orgf0b7eac2013-06-13 15:37:19 +090082 Bus::Options bus_options;
83 bus_options.bus_type = Bus::SESSION;
84 bus_options.connection_type = Bus::PRIVATE;
skyostile5a8dc42015-06-18 00:46:04 +090085 bus_options.dbus_task_runner = dbus_thread_->task_runner();
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090086 bus_ = new Bus(bus_options);
keybuk@chromium.org09715012013-03-26 03:20:08 +090087 ASSERT_TRUE(bus_->HasDBusThread());
88
89 object_manager_ = bus_->GetObjectManager(
hashimotof4a4c0d2016-01-05 17:48:03 +090090 test_service_->service_name(),
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090091 ObjectPath("/org/chromium/TestService"));
keybuk@chromium.org09715012013-03-26 03:20:08 +090092 object_manager_->RegisterInterface("org.chromium.TestInterface", this);
93
keybuk@chromium.org09715012013-03-26 03:20:08 +090094 WaitForObject();
95 }
96
dcheng7f5750d2014-12-30 03:30:17 +090097 void TearDown() override {
keybuk@chromium.org09715012013-03-26 03:20:08 +090098 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();
earthdok64401d72014-09-03 19:32:36 +0900109
110 base::RunLoop().RunUntilIdle();
keybuk@chromium.org09715012013-03-26 03:20:08 +0900111 }
112
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900113 void MethodCallback(Response* response) {
keybuk@chromium.org09715012013-03-26 03:20:08 +0900114 method_callback_called_ = true;
earthdok64401d72014-09-03 19:32:36 +0900115 run_loop_->Quit();
keybuk@chromium.org09715012013-03-26 03:20:08 +0900116 }
117
armansitof4364642014-09-06 02:49:34 +0900118 // 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.orgf0b7eac2013-06-13 15:37:19 +0900128 protected:
keybuk@chromium.org09715012013-03-26 03:20:08 +0900129 // Called when an object is added.
dcheng7f5750d2014-12-30 03:30:17 +0900130 void ObjectAdded(const ObjectPath& object_path,
131 const std::string& interface_name) override {
keybuk@chromium.org09715012013-03-26 03:20:08 +0900132 added_objects_.push_back(std::make_pair(object_path, interface_name));
earthdok64401d72014-09-03 19:32:36 +0900133 run_loop_->Quit();
keybuk@chromium.org09715012013-03-26 03:20:08 +0900134 }
135
136 // Called when an object is removed.
dcheng7f5750d2014-12-30 03:30:17 +0900137 void ObjectRemoved(const ObjectPath& object_path,
138 const std::string& interface_name) override {
keybuk@chromium.org09715012013-03-26 03:20:08 +0900139 removed_objects_.push_back(std::make_pair(object_path, interface_name));
earthdok64401d72014-09-03 19:32:36 +0900140 run_loop_->Quit();
keybuk@chromium.org09715012013-03-26 03:20:08 +0900141 }
142
143 // Called when a property value is updated.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900144 void OnPropertyChanged(const ObjectPath& object_path,
keybuk@chromium.org09715012013-03-26 03:20:08 +0900145 const std::string& name) {
armansitof4364642014-09-06 02:49:34 +0900146 // 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.org09715012013-03-26 03:20:08 +0900156 updated_properties_.push_back(name);
earthdok64401d72014-09-03 19:32:36 +0900157 run_loop_->Quit();
keybuk@chromium.org09715012013-03-26 03:20:08 +0900158 }
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 ||
earthdok64401d72014-09-03 19:32:36 +0900165 updated_properties_.size() < kExpectedProperties) {
166 run_loop_.reset(new base::RunLoop);
167 run_loop_->Run();
168 }
keybuk@chromium.org09715012013-03-26 03:20:08 +0900169 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() {
earthdok64401d72014-09-03 19:32:36 +0900176 while (removed_objects_.size() < kExpectedObjects) {
177 run_loop_.reset(new base::RunLoop);
178 run_loop_->Run();
179 }
keybuk@chromium.org09715012013-03-26 03:20:08 +0900180 for (size_t i = 0; i < kExpectedObjects; ++i)
181 removed_objects_.erase(removed_objects_.begin());
182 }
183
184 void WaitForMethodCallback() {
earthdok64401d72014-09-03 19:32:36 +0900185 run_loop_.reset(new base::RunLoop);
186 run_loop_->Run();
keybuk@chromium.org09715012013-03-26 03:20:08 +0900187 method_callback_called_ = false;
188 }
189
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900190 void PerformAction(const std::string& action, const ObjectPath& object_path) {
191 ObjectProxy* object_proxy = bus_->GetObjectProxy(
hashimotof4a4c0d2016-01-05 17:48:03 +0900192 test_service_->service_name(),
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900193 ObjectPath("/org/chromium/TestObject"));
keybuk@chromium.org09715012013-03-26 03:20:08 +0900194
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900195 MethodCall method_call("org.chromium.TestInterface", "PerformAction");
196 MessageWriter writer(&method_call);
keybuk@chromium.org09715012013-03-26 03:20:08 +0900197 writer.AppendString(action);
198 writer.AppendObjectPath(object_path);
199
200 object_proxy->CallMethod(&method_call,
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900201 ObjectProxy::TIMEOUT_USE_DEFAULT,
keybuk@chromium.org09715012013-03-26 03:20:08 +0900202 base::Bind(&ObjectManagerTest::MethodCallback,
203 base::Unretained(this)));
204 WaitForMethodCallback();
205 }
206
xhwang@chromium.orgdff6b132013-05-02 01:10:30 +0900207 base::MessageLoop message_loop_;
dcheng30c5a172016-04-09 07:55:04 +0900208 std::unique_ptr<base::RunLoop> run_loop_;
209 std::unique_ptr<base::Thread> dbus_thread_;
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900210 scoped_refptr<Bus> bus_;
211 ObjectManager* object_manager_;
dcheng30c5a172016-04-09 07:55:04 +0900212 std::unique_ptr<TestService> test_service_;
keybuk@chromium.org09715012013-03-26 03:20:08 +0900213
armansitof4364642014-09-06 02:49:34 +0900214 std::string last_name_value_;
215 bool timeout_expired_;
216
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900217 std::vector<std::pair<ObjectPath, std::string> > added_objects_;
218 std::vector<std::pair<ObjectPath, std::string> > removed_objects_;
keybuk@chromium.org09715012013-03-26 03:20:08 +0900219 std::vector<std::string> updated_properties_;
220
221 bool method_callback_called_;
222};
223
224
225TEST_F(ObjectManagerTest, InitialObject) {
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900226 ObjectProxy* object_proxy = object_manager_->GetObjectProxy(
227 ObjectPath("/org/chromium/TestObject"));
keybuk@chromium.org09715012013-03-26 03:20:08 +0900228 EXPECT_TRUE(object_proxy != NULL);
229
230 Properties* properties = static_cast<Properties*>(
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900231 object_manager_->GetProperties(ObjectPath("/org/chromium/TestObject"),
232 "org.chromium.TestInterface"));
keybuk@chromium.org09715012013-03-26 03:20:08 +0900233 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.orgf0b7eac2013-06-13 15:37:19 +0900245 std::vector<ObjectPath> objects = properties->objects.value();
keybuk@chromium.org09715012013-03-26 03:20:08 +0900246 ASSERT_EQ(1U, objects.size());
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900247 EXPECT_EQ(ObjectPath("/TestObjectPath"), objects[0]);
keybuk@chromium.org09715012013-03-26 03:20:08 +0900248}
249
250TEST_F(ObjectManagerTest, UnknownObjectProxy) {
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900251 ObjectProxy* object_proxy = object_manager_->GetObjectProxy(
252 ObjectPath("/org/chromium/UnknownObject"));
keybuk@chromium.org09715012013-03-26 03:20:08 +0900253 EXPECT_TRUE(object_proxy == NULL);
254}
255
256TEST_F(ObjectManagerTest, UnknownObjectProperties) {
257 Properties* properties = static_cast<Properties*>(
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900258 object_manager_->GetProperties(ObjectPath("/org/chromium/UnknownObject"),
259 "org.chromium.TestInterface"));
keybuk@chromium.org09715012013-03-26 03:20:08 +0900260 EXPECT_TRUE(properties == NULL);
261}
262
263TEST_F(ObjectManagerTest, UnknownInterfaceProperties) {
264 Properties* properties = static_cast<Properties*>(
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900265 object_manager_->GetProperties(ObjectPath("/org/chromium/TestObject"),
266 "org.chromium.UnknownService"));
keybuk@chromium.org09715012013-03-26 03:20:08 +0900267 EXPECT_TRUE(properties == NULL);
268}
269
270TEST_F(ObjectManagerTest, GetObjects) {
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900271 std::vector<ObjectPath> object_paths = object_manager_->GetObjects();
keybuk@chromium.org09715012013-03-26 03:20:08 +0900272 ASSERT_EQ(1U, object_paths.size());
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900273 EXPECT_EQ(ObjectPath("/org/chromium/TestObject"), object_paths[0]);
keybuk@chromium.org09715012013-03-26 03:20:08 +0900274}
275
276TEST_F(ObjectManagerTest, GetObjectsWithInterface) {
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900277 std::vector<ObjectPath> object_paths =
keybuk@chromium.org09715012013-03-26 03:20:08 +0900278 object_manager_->GetObjectsWithInterface("org.chromium.TestInterface");
279 ASSERT_EQ(1U, object_paths.size());
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900280 EXPECT_EQ(ObjectPath("/org/chromium/TestObject"), object_paths[0]);
keybuk@chromium.org09715012013-03-26 03:20:08 +0900281}
282
283TEST_F(ObjectManagerTest, GetObjectsWithUnknownInterface) {
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900284 std::vector<ObjectPath> object_paths =
keybuk@chromium.org09715012013-03-26 03:20:08 +0900285 object_manager_->GetObjectsWithInterface("org.chromium.UnknownService");
286 EXPECT_EQ(0U, object_paths.size());
287}
288
289TEST_F(ObjectManagerTest, SameObject) {
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900290 ObjectManager* object_manager = bus_->GetObjectManager(
hashimotof4a4c0d2016-01-05 17:48:03 +0900291 test_service_->service_name(),
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900292 ObjectPath("/org/chromium/TestService"));
keybuk@chromium.org09715012013-03-26 03:20:08 +0900293 EXPECT_EQ(object_manager_, object_manager);
294}
295
296TEST_F(ObjectManagerTest, DifferentObjectForService) {
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900297 ObjectManager* object_manager = bus_->GetObjectManager(
keybuk@chromium.org09715012013-03-26 03:20:08 +0900298 "org.chromium.DifferentService",
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900299 ObjectPath("/org/chromium/TestService"));
keybuk@chromium.org09715012013-03-26 03:20:08 +0900300 EXPECT_NE(object_manager_, object_manager);
301}
302
303TEST_F(ObjectManagerTest, DifferentObjectForPath) {
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900304 ObjectManager* object_manager = bus_->GetObjectManager(
hashimotof4a4c0d2016-01-05 17:48:03 +0900305 test_service_->service_name(),
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900306 ObjectPath("/org/chromium/DifferentService"));
keybuk@chromium.org09715012013-03-26 03:20:08 +0900307 EXPECT_NE(object_manager_, object_manager);
308}
309
310TEST_F(ObjectManagerTest, SecondObject) {
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900311 PerformAction("AddObject", ObjectPath("/org/chromium/SecondObject"));
keybuk@chromium.org09715012013-03-26 03:20:08 +0900312 WaitForObject();
313
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900314 ObjectProxy* object_proxy = object_manager_->GetObjectProxy(
315 ObjectPath("/org/chromium/SecondObject"));
keybuk@chromium.org09715012013-03-26 03:20:08 +0900316 EXPECT_TRUE(object_proxy != NULL);
317
318 Properties* properties = static_cast<Properties*>(
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900319 object_manager_->GetProperties(ObjectPath("/org/chromium/SecondObject"),
320 "org.chromium.TestInterface"));
keybuk@chromium.org09715012013-03-26 03:20:08 +0900321 EXPECT_TRUE(properties != NULL);
322
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900323 std::vector<ObjectPath> object_paths = object_manager_->GetObjects();
keybuk@chromium.org09715012013-03-26 03:20:08 +0900324 ASSERT_EQ(2U, object_paths.size());
325
326 std::sort(object_paths.begin(), object_paths.end());
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900327 EXPECT_EQ(ObjectPath("/org/chromium/SecondObject"), object_paths[0]);
328 EXPECT_EQ(ObjectPath("/org/chromium/TestObject"), object_paths[1]);
keybuk@chromium.org09715012013-03-26 03:20:08 +0900329
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.orgf0b7eac2013-06-13 15:37:19 +0900335 EXPECT_EQ(ObjectPath("/org/chromium/SecondObject"), object_paths[0]);
336 EXPECT_EQ(ObjectPath("/org/chromium/TestObject"), object_paths[1]);
keybuk@chromium.org09715012013-03-26 03:20:08 +0900337}
338
339TEST_F(ObjectManagerTest, RemoveSecondObject) {
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900340 PerformAction("AddObject", ObjectPath("/org/chromium/SecondObject"));
keybuk@chromium.org09715012013-03-26 03:20:08 +0900341 WaitForObject();
342
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900343 std::vector<ObjectPath> object_paths = object_manager_->GetObjects();
keybuk@chromium.org09715012013-03-26 03:20:08 +0900344 ASSERT_EQ(2U, object_paths.size());
345
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900346 PerformAction("RemoveObject", ObjectPath("/org/chromium/SecondObject"));
keybuk@chromium.org09715012013-03-26 03:20:08 +0900347 WaitForRemoveObject();
348
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900349 ObjectProxy* object_proxy = object_manager_->GetObjectProxy(
350 ObjectPath("/org/chromium/SecondObject"));
keybuk@chromium.org09715012013-03-26 03:20:08 +0900351 EXPECT_TRUE(object_proxy == NULL);
352
353 Properties* properties = static_cast<Properties*>(
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900354 object_manager_->GetProperties(ObjectPath("/org/chromium/SecondObject"),
355 "org.chromium.TestInterface"));
keybuk@chromium.org09715012013-03-26 03:20:08 +0900356 EXPECT_TRUE(properties == NULL);
357
358 object_paths = object_manager_->GetObjects();
359 ASSERT_EQ(1U, object_paths.size());
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900360 EXPECT_EQ(ObjectPath("/org/chromium/TestObject"), object_paths[0]);
keybuk@chromium.org09715012013-03-26 03:20:08 +0900361
362 object_paths =
363 object_manager_->GetObjectsWithInterface("org.chromium.TestInterface");
364 ASSERT_EQ(1U, object_paths.size());
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900365 EXPECT_EQ(ObjectPath("/org/chromium/TestObject"), object_paths[0]);
keybuk@chromium.org09715012013-03-26 03:20:08 +0900366}
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900367
keybuk@chromium.org40b05ba2014-03-07 11:24:33 +0900368TEST_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
376TEST_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
armansitof4364642014-09-06 02:49:34 +0900385TEST_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(
hashimotof4a4c0d2016-01-05 17:48:03 +0900390 test_service_->service_name(),
armansitof4364642014-09-06 02:49:34 +0900391 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(
hashimotof4a4c0d2016-01-05 17:48:03 +0900399 test_service_->service_name(),
armansitof4364642014-09-06 02:49:34 +0900400 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.orgf0b7eac2013-06-13 15:37:19 +0900419} // namespace dbus