blob: 54ddeacebd4fd0cc1cdf2d15face1c45b4eb9e62 [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
7#include <string>
8#include <vector>
9
10#include "base/basictypes.h"
11#include "base/bind.h"
avi@chromium.orga29af562013-07-18 08:00:30 +090012#include "base/message_loop/message_loop.h"
earthdok64401d72014-09-03 19:32:36 +090013#include "base/run_loop.h"
keybuk@chromium.org09715012013-03-26 03:20:08 +090014#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.orgf0b7eac2013-06-13 15:37:19 +090023namespace dbus {
24
keybuk@chromium.org09715012013-03-26 03:20:08 +090025// The object manager test exercises the asynchronous APIs in ObjectManager,
26// and by extension PropertySet and Property<>.
27class ObjectManagerTest
28 : public testing::Test,
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090029 public ObjectManager::Interface {
keybuk@chromium.org09715012013-03-26 03:20:08 +090030 public:
armansitof4364642014-09-06 02:49:34 +090031 ObjectManagerTest() : timeout_expired_(false) {
keybuk@chromium.org09715012013-03-26 03:20:08 +090032 }
33
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090034 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.org09715012013-03-26 03:20:08 +090039
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090040 Properties(ObjectProxy* object_proxy,
keybuk@chromium.org09715012013-03-26 03:20:08 +090041 const std::string& interface_name,
42 PropertyChangedCallback property_changed_callback)
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090043 : PropertySet(object_proxy, interface_name, property_changed_callback) {
keybuk@chromium.org09715012013-03-26 03:20:08 +090044 RegisterProperty("Name", &name);
45 RegisterProperty("Version", &version);
46 RegisterProperty("Methods", &methods);
47 RegisterProperty("Objects", &objects);
48 }
49 };
50
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090051 virtual PropertySet* CreateProperties(
52 ObjectProxy* object_proxy,
53 const ObjectPath& object_path,
keybuk@chromium.org09715012013-03-26 03:20:08 +090054 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.orgf0b7eac2013-06-13 15:37:19 +090059 return static_cast<PropertySet*>(properties);
keybuk@chromium.org09715012013-03-26 03:20:08 +090060 }
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.orgdff6b132013-05-02 01:10:30 +090069 thread_options.message_loop_type = base::MessageLoop::TYPE_IO;
keybuk@chromium.org09715012013-03-26 03:20:08 +090070 ASSERT_TRUE(dbus_thread_->StartWithOptions(thread_options));
71
72 // Start the test service, using the D-Bus thread.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090073 TestService::Options options;
keybuk@chromium.org09715012013-03-26 03:20:08 +090074 options.dbus_task_runner = dbus_thread_->message_loop_proxy();
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090075 test_service_.reset(new TestService(options));
keybuk@chromium.org09715012013-03-26 03:20:08 +090076 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.orgf0b7eac2013-06-13 15:37:19 +090081 Bus::Options bus_options;
82 bus_options.bus_type = Bus::SESSION;
83 bus_options.connection_type = Bus::PRIVATE;
keybuk@chromium.org09715012013-03-26 03:20:08 +090084 bus_options.dbus_task_runner = dbus_thread_->message_loop_proxy();
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090085 bus_ = new Bus(bus_options);
keybuk@chromium.org09715012013-03-26 03:20:08 +090086 ASSERT_TRUE(bus_->HasDBusThread());
87
88 object_manager_ = bus_->GetObjectManager(
89 "org.chromium.TestService",
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090090 ObjectPath("/org/chromium/TestService"));
keybuk@chromium.org09715012013-03-26 03:20:08 +090091 object_manager_->RegisterInterface("org.chromium.TestInterface", this);
92
keybuk@chromium.org09715012013-03-26 03:20:08 +090093 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();
earthdok64401d72014-09-03 19:32:36 +0900108
109 base::RunLoop().RunUntilIdle();
keybuk@chromium.org09715012013-03-26 03:20:08 +0900110 }
111
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900112 void MethodCallback(Response* response) {
keybuk@chromium.org09715012013-03-26 03:20:08 +0900113 method_callback_called_ = true;
earthdok64401d72014-09-03 19:32:36 +0900114 run_loop_->Quit();
keybuk@chromium.org09715012013-03-26 03:20:08 +0900115 }
116
armansitof4364642014-09-06 02:49:34 +0900117 // 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.orgf0b7eac2013-06-13 15:37:19 +0900127 protected:
keybuk@chromium.org09715012013-03-26 03:20:08 +0900128 // Called when an object is added.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900129 virtual void ObjectAdded(const ObjectPath& object_path,
130 const std::string& interface_name) OVERRIDE {
keybuk@chromium.org09715012013-03-26 03:20:08 +0900131 added_objects_.push_back(std::make_pair(object_path, interface_name));
earthdok64401d72014-09-03 19:32:36 +0900132 run_loop_->Quit();
keybuk@chromium.org09715012013-03-26 03:20:08 +0900133 }
134
135 // Called when an object is removed.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900136 virtual void ObjectRemoved(const ObjectPath& object_path,
rsleevi@chromium.org0a2b25e2013-05-02 11:24:12 +0900137 const std::string& interface_name) OVERRIDE {
keybuk@chromium.org09715012013-03-26 03:20:08 +0900138 removed_objects_.push_back(std::make_pair(object_path, interface_name));
earthdok64401d72014-09-03 19:32:36 +0900139 run_loop_->Quit();
keybuk@chromium.org09715012013-03-26 03:20:08 +0900140 }
141
142 // Called when a property value is updated.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900143 void OnPropertyChanged(const ObjectPath& object_path,
keybuk@chromium.org09715012013-03-26 03:20:08 +0900144 const std::string& name) {
armansitof4364642014-09-06 02:49:34 +0900145 // 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.org09715012013-03-26 03:20:08 +0900155 updated_properties_.push_back(name);
earthdok64401d72014-09-03 19:32:36 +0900156 run_loop_->Quit();
keybuk@chromium.org09715012013-03-26 03:20:08 +0900157 }
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 ||
earthdok64401d72014-09-03 19:32:36 +0900164 updated_properties_.size() < kExpectedProperties) {
165 run_loop_.reset(new base::RunLoop);
166 run_loop_->Run();
167 }
keybuk@chromium.org09715012013-03-26 03:20:08 +0900168 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() {
earthdok64401d72014-09-03 19:32:36 +0900175 while (removed_objects_.size() < kExpectedObjects) {
176 run_loop_.reset(new base::RunLoop);
177 run_loop_->Run();
178 }
keybuk@chromium.org09715012013-03-26 03:20:08 +0900179 for (size_t i = 0; i < kExpectedObjects; ++i)
180 removed_objects_.erase(removed_objects_.begin());
181 }
182
183 void WaitForMethodCallback() {
earthdok64401d72014-09-03 19:32:36 +0900184 run_loop_.reset(new base::RunLoop);
185 run_loop_->Run();
keybuk@chromium.org09715012013-03-26 03:20:08 +0900186 method_callback_called_ = false;
187 }
188
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900189 void PerformAction(const std::string& action, const ObjectPath& object_path) {
190 ObjectProxy* object_proxy = bus_->GetObjectProxy(
keybuk@chromium.org09715012013-03-26 03:20:08 +0900191 "org.chromium.TestService",
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900192 ObjectPath("/org/chromium/TestObject"));
keybuk@chromium.org09715012013-03-26 03:20:08 +0900193
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900194 MethodCall method_call("org.chromium.TestInterface", "PerformAction");
195 MessageWriter writer(&method_call);
keybuk@chromium.org09715012013-03-26 03:20:08 +0900196 writer.AppendString(action);
197 writer.AppendObjectPath(object_path);
198
199 object_proxy->CallMethod(&method_call,
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900200 ObjectProxy::TIMEOUT_USE_DEFAULT,
keybuk@chromium.org09715012013-03-26 03:20:08 +0900201 base::Bind(&ObjectManagerTest::MethodCallback,
202 base::Unretained(this)));
203 WaitForMethodCallback();
204 }
205
xhwang@chromium.orgdff6b132013-05-02 01:10:30 +0900206 base::MessageLoop message_loop_;
earthdok64401d72014-09-03 19:32:36 +0900207 scoped_ptr<base::RunLoop> run_loop_;
keybuk@chromium.org09715012013-03-26 03:20:08 +0900208 scoped_ptr<base::Thread> dbus_thread_;
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900209 scoped_refptr<Bus> bus_;
210 ObjectManager* object_manager_;
211 scoped_ptr<TestService> test_service_;
keybuk@chromium.org09715012013-03-26 03:20:08 +0900212
armansitof4364642014-09-06 02:49:34 +0900213 std::string last_name_value_;
214 bool timeout_expired_;
215
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900216 std::vector<std::pair<ObjectPath, std::string> > added_objects_;
217 std::vector<std::pair<ObjectPath, std::string> > removed_objects_;
keybuk@chromium.org09715012013-03-26 03:20:08 +0900218 std::vector<std::string> updated_properties_;
219
220 bool method_callback_called_;
221};
222
223
224TEST_F(ObjectManagerTest, InitialObject) {
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900225 ObjectProxy* object_proxy = object_manager_->GetObjectProxy(
226 ObjectPath("/org/chromium/TestObject"));
keybuk@chromium.org09715012013-03-26 03:20:08 +0900227 EXPECT_TRUE(object_proxy != NULL);
228
229 Properties* properties = static_cast<Properties*>(
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900230 object_manager_->GetProperties(ObjectPath("/org/chromium/TestObject"),
231 "org.chromium.TestInterface"));
keybuk@chromium.org09715012013-03-26 03:20:08 +0900232 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.orgf0b7eac2013-06-13 15:37:19 +0900244 std::vector<ObjectPath> objects = properties->objects.value();
keybuk@chromium.org09715012013-03-26 03:20:08 +0900245 ASSERT_EQ(1U, objects.size());
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900246 EXPECT_EQ(ObjectPath("/TestObjectPath"), objects[0]);
keybuk@chromium.org09715012013-03-26 03:20:08 +0900247}
248
249TEST_F(ObjectManagerTest, UnknownObjectProxy) {
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900250 ObjectProxy* object_proxy = object_manager_->GetObjectProxy(
251 ObjectPath("/org/chromium/UnknownObject"));
keybuk@chromium.org09715012013-03-26 03:20:08 +0900252 EXPECT_TRUE(object_proxy == NULL);
253}
254
255TEST_F(ObjectManagerTest, UnknownObjectProperties) {
256 Properties* properties = static_cast<Properties*>(
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900257 object_manager_->GetProperties(ObjectPath("/org/chromium/UnknownObject"),
258 "org.chromium.TestInterface"));
keybuk@chromium.org09715012013-03-26 03:20:08 +0900259 EXPECT_TRUE(properties == NULL);
260}
261
262TEST_F(ObjectManagerTest, UnknownInterfaceProperties) {
263 Properties* properties = static_cast<Properties*>(
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900264 object_manager_->GetProperties(ObjectPath("/org/chromium/TestObject"),
265 "org.chromium.UnknownService"));
keybuk@chromium.org09715012013-03-26 03:20:08 +0900266 EXPECT_TRUE(properties == NULL);
267}
268
269TEST_F(ObjectManagerTest, GetObjects) {
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900270 std::vector<ObjectPath> object_paths = object_manager_->GetObjects();
keybuk@chromium.org09715012013-03-26 03:20:08 +0900271 ASSERT_EQ(1U, object_paths.size());
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900272 EXPECT_EQ(ObjectPath("/org/chromium/TestObject"), object_paths[0]);
keybuk@chromium.org09715012013-03-26 03:20:08 +0900273}
274
275TEST_F(ObjectManagerTest, GetObjectsWithInterface) {
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900276 std::vector<ObjectPath> object_paths =
keybuk@chromium.org09715012013-03-26 03:20:08 +0900277 object_manager_->GetObjectsWithInterface("org.chromium.TestInterface");
278 ASSERT_EQ(1U, object_paths.size());
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900279 EXPECT_EQ(ObjectPath("/org/chromium/TestObject"), object_paths[0]);
keybuk@chromium.org09715012013-03-26 03:20:08 +0900280}
281
282TEST_F(ObjectManagerTest, GetObjectsWithUnknownInterface) {
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900283 std::vector<ObjectPath> object_paths =
keybuk@chromium.org09715012013-03-26 03:20:08 +0900284 object_manager_->GetObjectsWithInterface("org.chromium.UnknownService");
285 EXPECT_EQ(0U, object_paths.size());
286}
287
288TEST_F(ObjectManagerTest, SameObject) {
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900289 ObjectManager* object_manager = bus_->GetObjectManager(
keybuk@chromium.org09715012013-03-26 03:20:08 +0900290 "org.chromium.TestService",
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900291 ObjectPath("/org/chromium/TestService"));
keybuk@chromium.org09715012013-03-26 03:20:08 +0900292 EXPECT_EQ(object_manager_, object_manager);
293}
294
295TEST_F(ObjectManagerTest, DifferentObjectForService) {
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900296 ObjectManager* object_manager = bus_->GetObjectManager(
keybuk@chromium.org09715012013-03-26 03:20:08 +0900297 "org.chromium.DifferentService",
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900298 ObjectPath("/org/chromium/TestService"));
keybuk@chromium.org09715012013-03-26 03:20:08 +0900299 EXPECT_NE(object_manager_, object_manager);
300}
301
302TEST_F(ObjectManagerTest, DifferentObjectForPath) {
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900303 ObjectManager* object_manager = bus_->GetObjectManager(
keybuk@chromium.org09715012013-03-26 03:20:08 +0900304 "org.chromium.TestService",
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900305 ObjectPath("/org/chromium/DifferentService"));
keybuk@chromium.org09715012013-03-26 03:20:08 +0900306 EXPECT_NE(object_manager_, object_manager);
307}
308
309TEST_F(ObjectManagerTest, SecondObject) {
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900310 PerformAction("AddObject", ObjectPath("/org/chromium/SecondObject"));
keybuk@chromium.org09715012013-03-26 03:20:08 +0900311 WaitForObject();
312
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900313 ObjectProxy* object_proxy = object_manager_->GetObjectProxy(
314 ObjectPath("/org/chromium/SecondObject"));
keybuk@chromium.org09715012013-03-26 03:20:08 +0900315 EXPECT_TRUE(object_proxy != NULL);
316
317 Properties* properties = static_cast<Properties*>(
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900318 object_manager_->GetProperties(ObjectPath("/org/chromium/SecondObject"),
319 "org.chromium.TestInterface"));
keybuk@chromium.org09715012013-03-26 03:20:08 +0900320 EXPECT_TRUE(properties != NULL);
321
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900322 std::vector<ObjectPath> object_paths = object_manager_->GetObjects();
keybuk@chromium.org09715012013-03-26 03:20:08 +0900323 ASSERT_EQ(2U, object_paths.size());
324
325 std::sort(object_paths.begin(), object_paths.end());
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900326 EXPECT_EQ(ObjectPath("/org/chromium/SecondObject"), object_paths[0]);
327 EXPECT_EQ(ObjectPath("/org/chromium/TestObject"), object_paths[1]);
keybuk@chromium.org09715012013-03-26 03:20:08 +0900328
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.orgf0b7eac2013-06-13 15:37:19 +0900334 EXPECT_EQ(ObjectPath("/org/chromium/SecondObject"), object_paths[0]);
335 EXPECT_EQ(ObjectPath("/org/chromium/TestObject"), object_paths[1]);
keybuk@chromium.org09715012013-03-26 03:20:08 +0900336}
337
338TEST_F(ObjectManagerTest, RemoveSecondObject) {
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900339 PerformAction("AddObject", ObjectPath("/org/chromium/SecondObject"));
keybuk@chromium.org09715012013-03-26 03:20:08 +0900340 WaitForObject();
341
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900342 std::vector<ObjectPath> object_paths = object_manager_->GetObjects();
keybuk@chromium.org09715012013-03-26 03:20:08 +0900343 ASSERT_EQ(2U, object_paths.size());
344
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900345 PerformAction("RemoveObject", ObjectPath("/org/chromium/SecondObject"));
keybuk@chromium.org09715012013-03-26 03:20:08 +0900346 WaitForRemoveObject();
347
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900348 ObjectProxy* object_proxy = object_manager_->GetObjectProxy(
349 ObjectPath("/org/chromium/SecondObject"));
keybuk@chromium.org09715012013-03-26 03:20:08 +0900350 EXPECT_TRUE(object_proxy == NULL);
351
352 Properties* properties = static_cast<Properties*>(
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900353 object_manager_->GetProperties(ObjectPath("/org/chromium/SecondObject"),
354 "org.chromium.TestInterface"));
keybuk@chromium.org09715012013-03-26 03:20:08 +0900355 EXPECT_TRUE(properties == NULL);
356
357 object_paths = object_manager_->GetObjects();
358 ASSERT_EQ(1U, object_paths.size());
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900359 EXPECT_EQ(ObjectPath("/org/chromium/TestObject"), object_paths[0]);
keybuk@chromium.org09715012013-03-26 03:20:08 +0900360
361 object_paths =
362 object_manager_->GetObjectsWithInterface("org.chromium.TestInterface");
363 ASSERT_EQ(1U, object_paths.size());
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900364 EXPECT_EQ(ObjectPath("/org/chromium/TestObject"), object_paths[0]);
keybuk@chromium.org09715012013-03-26 03:20:08 +0900365}
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900366
keybuk@chromium.org40b05ba2014-03-07 11:24:33 +0900367TEST_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
375TEST_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
armansitof4364642014-09-06 02:49:34 +0900384TEST_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.orgf0b7eac2013-06-13 15:37:19 +0900418} // namespace dbus