blob: 595489fbd9939d88f0bf15c3d9b57bd3f1b7a142 [file] [log] [blame]
[email protected]9cc40cb2013-03-25 18:20:081// 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"
[email protected]2a9ec0e2013-07-17 23:00:3012#include "base/message_loop/message_loop.h"
[email protected]9cc40cb2013-03-25 18:20:0813#include "base/threading/thread.h"
14#include "base/threading/thread_restrictions.h"
15#include "dbus/bus.h"
16#include "dbus/object_path.h"
17#include "dbus/object_proxy.h"
18#include "dbus/property.h"
19#include "dbus/test_service.h"
20#include "testing/gtest/include/gtest/gtest.h"
21
[email protected]2a57ca642013-06-13 06:37:1922namespace dbus {
23
[email protected]9cc40cb2013-03-25 18:20:0824// The object manager test exercises the asynchronous APIs in ObjectManager,
25// and by extension PropertySet and Property<>.
26class ObjectManagerTest
27 : public testing::Test,
[email protected]2a57ca642013-06-13 06:37:1928 public ObjectManager::Interface {
[email protected]9cc40cb2013-03-25 18:20:0829 public:
30 ObjectManagerTest() {
31 }
32
[email protected]2a57ca642013-06-13 06:37:1933 struct Properties : public PropertySet {
34 Property<std::string> name;
35 Property<int16> version;
36 Property<std::vector<std::string> > methods;
37 Property<std::vector<ObjectPath> > objects;
[email protected]9cc40cb2013-03-25 18:20:0838
[email protected]2a57ca642013-06-13 06:37:1939 Properties(ObjectProxy* object_proxy,
[email protected]9cc40cb2013-03-25 18:20:0840 const std::string& interface_name,
41 PropertyChangedCallback property_changed_callback)
[email protected]2a57ca642013-06-13 06:37:1942 : PropertySet(object_proxy, interface_name, property_changed_callback) {
[email protected]9cc40cb2013-03-25 18:20:0843 RegisterProperty("Name", &name);
44 RegisterProperty("Version", &version);
45 RegisterProperty("Methods", &methods);
46 RegisterProperty("Objects", &objects);
47 }
48 };
49
[email protected]2a57ca642013-06-13 06:37:1950 virtual PropertySet* CreateProperties(
51 ObjectProxy* object_proxy,
52 const ObjectPath& object_path,
[email protected]9cc40cb2013-03-25 18:20:0853 const std::string& interface_name) OVERRIDE {
54 Properties* properties = new Properties(
55 object_proxy, interface_name,
56 base::Bind(&ObjectManagerTest::OnPropertyChanged,
57 base::Unretained(this), object_path));
[email protected]2a57ca642013-06-13 06:37:1958 return static_cast<PropertySet*>(properties);
[email protected]9cc40cb2013-03-25 18:20:0859 }
60
61 virtual void SetUp() {
62 // Make the main thread not to allow IO.
63 base::ThreadRestrictions::SetIOAllowed(false);
64
65 // Start the D-Bus thread.
66 dbus_thread_.reset(new base::Thread("D-Bus Thread"));
67 base::Thread::Options thread_options;
[email protected]ff33b18e2013-05-01 16:10:3068 thread_options.message_loop_type = base::MessageLoop::TYPE_IO;
[email protected]9cc40cb2013-03-25 18:20:0869 ASSERT_TRUE(dbus_thread_->StartWithOptions(thread_options));
70
71 // Start the test service, using the D-Bus thread.
[email protected]2a57ca642013-06-13 06:37:1972 TestService::Options options;
[email protected]9cc40cb2013-03-25 18:20:0873 options.dbus_task_runner = dbus_thread_->message_loop_proxy();
[email protected]2a57ca642013-06-13 06:37:1974 test_service_.reset(new TestService(options));
[email protected]9cc40cb2013-03-25 18:20:0875 ASSERT_TRUE(test_service_->StartService());
76 ASSERT_TRUE(test_service_->WaitUntilServiceIsStarted());
77 ASSERT_TRUE(test_service_->HasDBusThread());
78
79 // Create the client, using the D-Bus thread.
[email protected]2a57ca642013-06-13 06:37:1980 Bus::Options bus_options;
81 bus_options.bus_type = Bus::SESSION;
82 bus_options.connection_type = Bus::PRIVATE;
[email protected]9cc40cb2013-03-25 18:20:0883 bus_options.dbus_task_runner = dbus_thread_->message_loop_proxy();
[email protected]2a57ca642013-06-13 06:37:1984 bus_ = new Bus(bus_options);
[email protected]9cc40cb2013-03-25 18:20:0885 ASSERT_TRUE(bus_->HasDBusThread());
86
87 object_manager_ = bus_->GetObjectManager(
88 "org.chromium.TestService",
[email protected]2a57ca642013-06-13 06:37:1989 ObjectPath("/org/chromium/TestService"));
[email protected]9cc40cb2013-03-25 18:20:0890 object_manager_->RegisterInterface("org.chromium.TestInterface", this);
91
92 object_manager_->GetManagedObjects();
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();
108 }
109
[email protected]2a57ca642013-06-13 06:37:19110 void MethodCallback(Response* response) {
[email protected]9cc40cb2013-03-25 18:20:08111 method_callback_called_ = true;
112 message_loop_.Quit();
113 }
114
[email protected]2a57ca642013-06-13 06:37:19115 protected:
[email protected]9cc40cb2013-03-25 18:20:08116 // Called when an object is added.
[email protected]2a57ca642013-06-13 06:37:19117 virtual void ObjectAdded(const ObjectPath& object_path,
118 const std::string& interface_name) OVERRIDE {
[email protected]9cc40cb2013-03-25 18:20:08119 added_objects_.push_back(std::make_pair(object_path, interface_name));
120 message_loop_.Quit();
121 }
122
123 // Called when an object is removed.
[email protected]2a57ca642013-06-13 06:37:19124 virtual void ObjectRemoved(const ObjectPath& object_path,
[email protected]42091902013-05-02 02:24:12125 const std::string& interface_name) OVERRIDE {
[email protected]9cc40cb2013-03-25 18:20:08126 removed_objects_.push_back(std::make_pair(object_path, interface_name));
127 message_loop_.Quit();
128 }
129
130 // Called when a property value is updated.
[email protected]2a57ca642013-06-13 06:37:19131 void OnPropertyChanged(const ObjectPath& object_path,
[email protected]9cc40cb2013-03-25 18:20:08132 const std::string& name) {
133 updated_properties_.push_back(name);
134 message_loop_.Quit();
135 }
136
137 static const size_t kExpectedObjects = 1;
138 static const size_t kExpectedProperties = 4;
139
140 void WaitForObject() {
141 while (added_objects_.size() < kExpectedObjects ||
142 updated_properties_.size() < kExpectedProperties)
143 message_loop_.Run();
144 for (size_t i = 0; i < kExpectedObjects; ++i)
145 added_objects_.erase(added_objects_.begin());
146 for (size_t i = 0; i < kExpectedProperties; ++i)
147 updated_properties_.erase(updated_properties_.begin());
148 }
149
150 void WaitForRemoveObject() {
151 while (removed_objects_.size() < kExpectedObjects)
152 message_loop_.Run();
153 for (size_t i = 0; i < kExpectedObjects; ++i)
154 removed_objects_.erase(removed_objects_.begin());
155 }
156
157 void WaitForMethodCallback() {
158 message_loop_.Run();
159 method_callback_called_ = false;
160 }
161
[email protected]2a57ca642013-06-13 06:37:19162 void PerformAction(const std::string& action, const ObjectPath& object_path) {
163 ObjectProxy* object_proxy = bus_->GetObjectProxy(
[email protected]9cc40cb2013-03-25 18:20:08164 "org.chromium.TestService",
[email protected]2a57ca642013-06-13 06:37:19165 ObjectPath("/org/chromium/TestObject"));
[email protected]9cc40cb2013-03-25 18:20:08166
[email protected]2a57ca642013-06-13 06:37:19167 MethodCall method_call("org.chromium.TestInterface", "PerformAction");
168 MessageWriter writer(&method_call);
[email protected]9cc40cb2013-03-25 18:20:08169 writer.AppendString(action);
170 writer.AppendObjectPath(object_path);
171
172 object_proxy->CallMethod(&method_call,
[email protected]2a57ca642013-06-13 06:37:19173 ObjectProxy::TIMEOUT_USE_DEFAULT,
[email protected]9cc40cb2013-03-25 18:20:08174 base::Bind(&ObjectManagerTest::MethodCallback,
175 base::Unretained(this)));
176 WaitForMethodCallback();
177 }
178
[email protected]ff33b18e2013-05-01 16:10:30179 base::MessageLoop message_loop_;
[email protected]9cc40cb2013-03-25 18:20:08180 scoped_ptr<base::Thread> dbus_thread_;
[email protected]2a57ca642013-06-13 06:37:19181 scoped_refptr<Bus> bus_;
182 ObjectManager* object_manager_;
183 scoped_ptr<TestService> test_service_;
[email protected]9cc40cb2013-03-25 18:20:08184
[email protected]2a57ca642013-06-13 06:37:19185 std::vector<std::pair<ObjectPath, std::string> > added_objects_;
186 std::vector<std::pair<ObjectPath, std::string> > removed_objects_;
[email protected]9cc40cb2013-03-25 18:20:08187 std::vector<std::string> updated_properties_;
188
189 bool method_callback_called_;
190};
191
192
193TEST_F(ObjectManagerTest, InitialObject) {
[email protected]2a57ca642013-06-13 06:37:19194 ObjectProxy* object_proxy = object_manager_->GetObjectProxy(
195 ObjectPath("/org/chromium/TestObject"));
[email protected]9cc40cb2013-03-25 18:20:08196 EXPECT_TRUE(object_proxy != NULL);
197
198 Properties* properties = static_cast<Properties*>(
[email protected]2a57ca642013-06-13 06:37:19199 object_manager_->GetProperties(ObjectPath("/org/chromium/TestObject"),
200 "org.chromium.TestInterface"));
[email protected]9cc40cb2013-03-25 18:20:08201 EXPECT_TRUE(properties != NULL);
202
203 EXPECT_EQ("TestService", properties->name.value());
204 EXPECT_EQ(10, properties->version.value());
205
206 std::vector<std::string> methods = properties->methods.value();
207 ASSERT_EQ(4U, methods.size());
208 EXPECT_EQ("Echo", methods[0]);
209 EXPECT_EQ("SlowEcho", methods[1]);
210 EXPECT_EQ("AsyncEcho", methods[2]);
211 EXPECT_EQ("BrokenMethod", methods[3]);
212
[email protected]2a57ca642013-06-13 06:37:19213 std::vector<ObjectPath> objects = properties->objects.value();
[email protected]9cc40cb2013-03-25 18:20:08214 ASSERT_EQ(1U, objects.size());
[email protected]2a57ca642013-06-13 06:37:19215 EXPECT_EQ(ObjectPath("/TestObjectPath"), objects[0]);
[email protected]9cc40cb2013-03-25 18:20:08216}
217
218TEST_F(ObjectManagerTest, UnknownObjectProxy) {
[email protected]2a57ca642013-06-13 06:37:19219 ObjectProxy* object_proxy = object_manager_->GetObjectProxy(
220 ObjectPath("/org/chromium/UnknownObject"));
[email protected]9cc40cb2013-03-25 18:20:08221 EXPECT_TRUE(object_proxy == NULL);
222}
223
224TEST_F(ObjectManagerTest, UnknownObjectProperties) {
225 Properties* properties = static_cast<Properties*>(
[email protected]2a57ca642013-06-13 06:37:19226 object_manager_->GetProperties(ObjectPath("/org/chromium/UnknownObject"),
227 "org.chromium.TestInterface"));
[email protected]9cc40cb2013-03-25 18:20:08228 EXPECT_TRUE(properties == NULL);
229}
230
231TEST_F(ObjectManagerTest, UnknownInterfaceProperties) {
232 Properties* properties = static_cast<Properties*>(
[email protected]2a57ca642013-06-13 06:37:19233 object_manager_->GetProperties(ObjectPath("/org/chromium/TestObject"),
234 "org.chromium.UnknownService"));
[email protected]9cc40cb2013-03-25 18:20:08235 EXPECT_TRUE(properties == NULL);
236}
237
238TEST_F(ObjectManagerTest, GetObjects) {
[email protected]2a57ca642013-06-13 06:37:19239 std::vector<ObjectPath> object_paths = object_manager_->GetObjects();
[email protected]9cc40cb2013-03-25 18:20:08240 ASSERT_EQ(1U, object_paths.size());
[email protected]2a57ca642013-06-13 06:37:19241 EXPECT_EQ(ObjectPath("/org/chromium/TestObject"), object_paths[0]);
[email protected]9cc40cb2013-03-25 18:20:08242}
243
244TEST_F(ObjectManagerTest, GetObjectsWithInterface) {
[email protected]2a57ca642013-06-13 06:37:19245 std::vector<ObjectPath> object_paths =
[email protected]9cc40cb2013-03-25 18:20:08246 object_manager_->GetObjectsWithInterface("org.chromium.TestInterface");
247 ASSERT_EQ(1U, object_paths.size());
[email protected]2a57ca642013-06-13 06:37:19248 EXPECT_EQ(ObjectPath("/org/chromium/TestObject"), object_paths[0]);
[email protected]9cc40cb2013-03-25 18:20:08249}
250
251TEST_F(ObjectManagerTest, GetObjectsWithUnknownInterface) {
[email protected]2a57ca642013-06-13 06:37:19252 std::vector<ObjectPath> object_paths =
[email protected]9cc40cb2013-03-25 18:20:08253 object_manager_->GetObjectsWithInterface("org.chromium.UnknownService");
254 EXPECT_EQ(0U, object_paths.size());
255}
256
257TEST_F(ObjectManagerTest, SameObject) {
[email protected]2a57ca642013-06-13 06:37:19258 ObjectManager* object_manager = bus_->GetObjectManager(
[email protected]9cc40cb2013-03-25 18:20:08259 "org.chromium.TestService",
[email protected]2a57ca642013-06-13 06:37:19260 ObjectPath("/org/chromium/TestService"));
[email protected]9cc40cb2013-03-25 18:20:08261 EXPECT_EQ(object_manager_, object_manager);
262}
263
264TEST_F(ObjectManagerTest, DifferentObjectForService) {
[email protected]2a57ca642013-06-13 06:37:19265 ObjectManager* object_manager = bus_->GetObjectManager(
[email protected]9cc40cb2013-03-25 18:20:08266 "org.chromium.DifferentService",
[email protected]2a57ca642013-06-13 06:37:19267 ObjectPath("/org/chromium/TestService"));
[email protected]9cc40cb2013-03-25 18:20:08268 EXPECT_NE(object_manager_, object_manager);
269}
270
271TEST_F(ObjectManagerTest, DifferentObjectForPath) {
[email protected]2a57ca642013-06-13 06:37:19272 ObjectManager* object_manager = bus_->GetObjectManager(
[email protected]9cc40cb2013-03-25 18:20:08273 "org.chromium.TestService",
[email protected]2a57ca642013-06-13 06:37:19274 ObjectPath("/org/chromium/DifferentService"));
[email protected]9cc40cb2013-03-25 18:20:08275 EXPECT_NE(object_manager_, object_manager);
276}
277
278TEST_F(ObjectManagerTest, SecondObject) {
[email protected]2a57ca642013-06-13 06:37:19279 PerformAction("AddObject", ObjectPath("/org/chromium/SecondObject"));
[email protected]9cc40cb2013-03-25 18:20:08280 WaitForObject();
281
[email protected]2a57ca642013-06-13 06:37:19282 ObjectProxy* object_proxy = object_manager_->GetObjectProxy(
283 ObjectPath("/org/chromium/SecondObject"));
[email protected]9cc40cb2013-03-25 18:20:08284 EXPECT_TRUE(object_proxy != NULL);
285
286 Properties* properties = static_cast<Properties*>(
[email protected]2a57ca642013-06-13 06:37:19287 object_manager_->GetProperties(ObjectPath("/org/chromium/SecondObject"),
288 "org.chromium.TestInterface"));
[email protected]9cc40cb2013-03-25 18:20:08289 EXPECT_TRUE(properties != NULL);
290
[email protected]2a57ca642013-06-13 06:37:19291 std::vector<ObjectPath> object_paths = object_manager_->GetObjects();
[email protected]9cc40cb2013-03-25 18:20:08292 ASSERT_EQ(2U, object_paths.size());
293
294 std::sort(object_paths.begin(), object_paths.end());
[email protected]2a57ca642013-06-13 06:37:19295 EXPECT_EQ(ObjectPath("/org/chromium/SecondObject"), object_paths[0]);
296 EXPECT_EQ(ObjectPath("/org/chromium/TestObject"), object_paths[1]);
[email protected]9cc40cb2013-03-25 18:20:08297
298 object_paths =
299 object_manager_->GetObjectsWithInterface("org.chromium.TestInterface");
300 ASSERT_EQ(2U, object_paths.size());
301
302 std::sort(object_paths.begin(), object_paths.end());
[email protected]2a57ca642013-06-13 06:37:19303 EXPECT_EQ(ObjectPath("/org/chromium/SecondObject"), object_paths[0]);
304 EXPECT_EQ(ObjectPath("/org/chromium/TestObject"), object_paths[1]);
[email protected]9cc40cb2013-03-25 18:20:08305}
306
307TEST_F(ObjectManagerTest, RemoveSecondObject) {
[email protected]2a57ca642013-06-13 06:37:19308 PerformAction("AddObject", ObjectPath("/org/chromium/SecondObject"));
[email protected]9cc40cb2013-03-25 18:20:08309 WaitForObject();
310
[email protected]2a57ca642013-06-13 06:37:19311 std::vector<ObjectPath> object_paths = object_manager_->GetObjects();
[email protected]9cc40cb2013-03-25 18:20:08312 ASSERT_EQ(2U, object_paths.size());
313
[email protected]2a57ca642013-06-13 06:37:19314 PerformAction("RemoveObject", ObjectPath("/org/chromium/SecondObject"));
[email protected]9cc40cb2013-03-25 18:20:08315 WaitForRemoveObject();
316
[email protected]2a57ca642013-06-13 06:37:19317 ObjectProxy* object_proxy = object_manager_->GetObjectProxy(
318 ObjectPath("/org/chromium/SecondObject"));
[email protected]9cc40cb2013-03-25 18:20:08319 EXPECT_TRUE(object_proxy == NULL);
320
321 Properties* properties = static_cast<Properties*>(
[email protected]2a57ca642013-06-13 06:37:19322 object_manager_->GetProperties(ObjectPath("/org/chromium/SecondObject"),
323 "org.chromium.TestInterface"));
[email protected]9cc40cb2013-03-25 18:20:08324 EXPECT_TRUE(properties == NULL);
325
326 object_paths = object_manager_->GetObjects();
327 ASSERT_EQ(1U, object_paths.size());
[email protected]2a57ca642013-06-13 06:37:19328 EXPECT_EQ(ObjectPath("/org/chromium/TestObject"), object_paths[0]);
[email protected]9cc40cb2013-03-25 18:20:08329
330 object_paths =
331 object_manager_->GetObjectsWithInterface("org.chromium.TestInterface");
332 ASSERT_EQ(1U, object_paths.size());
[email protected]2a57ca642013-06-13 06:37:19333 EXPECT_EQ(ObjectPath("/org/chromium/TestObject"), object_paths[0]);
[email protected]9cc40cb2013-03-25 18:20:08334}
[email protected]2a57ca642013-06-13 06:37:19335
336} // namespace dbus