OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "dbus/object_manager.h" | 5 #include "dbus/object_manager.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
13 #include "base/threading/thread.h" | 13 #include "base/threading/thread.h" |
14 #include "base/threading/thread_restrictions.h" | 14 #include "base/threading/thread_restrictions.h" |
15 #include "dbus/bus.h" | 15 #include "dbus/bus.h" |
16 #include "dbus/object_path.h" | 16 #include "dbus/object_path.h" |
17 #include "dbus/object_proxy.h" | 17 #include "dbus/object_proxy.h" |
18 #include "dbus/property.h" | 18 #include "dbus/property.h" |
19 #include "dbus/test_service.h" | 19 #include "dbus/test_service.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
21 | 21 |
22 namespace dbus { | 22 namespace dbus { |
23 | 23 |
24 // The object manager test exercises the asynchronous APIs in ObjectManager, | 24 // The object manager test exercises the asynchronous APIs in ObjectManager, |
25 // and by extension PropertySet and Property<>. | 25 // and by extension PropertySet and Property<>. |
26 class ObjectManagerTest | 26 class ObjectManagerTest |
27 : public testing::Test, | 27 : public testing::Test, |
28 public ObjectManager::Interface { | 28 public ObjectManager::Interface { |
29 public: | 29 public: |
30 ObjectManagerTest() { | 30 ObjectManagerTest() : timeout_expired_(false) { |
31 } | 31 } |
32 | 32 |
33 struct Properties : public PropertySet { | 33 struct Properties : public PropertySet { |
34 Property<std::string> name; | 34 Property<std::string> name; |
35 Property<int16> version; | 35 Property<int16> version; |
36 Property<std::vector<std::string> > methods; | 36 Property<std::vector<std::string> > methods; |
37 Property<std::vector<ObjectPath> > objects; | 37 Property<std::vector<ObjectPath> > objects; |
38 | 38 |
39 Properties(ObjectProxy* object_proxy, | 39 Properties(ObjectProxy* object_proxy, |
40 const std::string& interface_name, | 40 const std::string& interface_name, |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 // Stopping a thread is considered an IO operation, so do this after | 105 // Stopping a thread is considered an IO operation, so do this after |
106 // allowing IO. | 106 // allowing IO. |
107 test_service_->Stop(); | 107 test_service_->Stop(); |
108 } | 108 } |
109 | 109 |
110 void MethodCallback(Response* response) { | 110 void MethodCallback(Response* response) { |
111 method_callback_called_ = true; | 111 method_callback_called_ = true; |
112 message_loop_.Quit(); | 112 message_loop_.Quit(); |
113 } | 113 } |
114 | 114 |
| 115 // Called when an object manager has been removed from the bus. |
| 116 void OnObjectManagerRemoved() { |
| 117 message_loop_.Quit(); |
| 118 } |
| 119 |
| 120 // Called from the PropertiesChangedAsObjectsReceived test case. The test will |
| 121 // not run the message loop if it receives the expected PropertiesChanged |
| 122 // signal before the timeout. This method immediately fails the test. |
| 123 void PropertiesChangedTestTimeout() { |
| 124 timeout_expired_ = true; |
| 125 message_loop_.Quit(); |
| 126 |
| 127 FAIL() << "Never received PropertiesChanged"; |
| 128 } |
| 129 |
115 protected: | 130 protected: |
116 // Called when an object is added. | 131 // Called when an object is added. |
117 virtual void ObjectAdded(const ObjectPath& object_path, | 132 virtual void ObjectAdded(const ObjectPath& object_path, |
118 const std::string& interface_name) OVERRIDE { | 133 const std::string& interface_name) OVERRIDE { |
119 added_objects_.push_back(std::make_pair(object_path, interface_name)); | 134 added_objects_.push_back(std::make_pair(object_path, interface_name)); |
120 message_loop_.Quit(); | 135 message_loop_.Quit(); |
121 } | 136 } |
122 | 137 |
123 // Called when an object is removed. | 138 // Called when an object is removed. |
124 virtual void ObjectRemoved(const ObjectPath& object_path, | 139 virtual void ObjectRemoved(const ObjectPath& object_path, |
125 const std::string& interface_name) OVERRIDE { | 140 const std::string& interface_name) OVERRIDE { |
126 removed_objects_.push_back(std::make_pair(object_path, interface_name)); | 141 removed_objects_.push_back(std::make_pair(object_path, interface_name)); |
127 message_loop_.Quit(); | 142 message_loop_.Quit(); |
128 } | 143 } |
129 | 144 |
130 // Called when a property value is updated. | 145 // Called when a property value is updated. |
131 void OnPropertyChanged(const ObjectPath& object_path, | 146 void OnPropertyChanged(const ObjectPath& object_path, |
132 const std::string& name) { | 147 const std::string& name) { |
| 148 // Store the value of the "Name" property if that's the one that |
| 149 // changed. |
| 150 Properties* properties = static_cast<Properties*>( |
| 151 object_manager_->GetProperties( |
| 152 object_path, |
| 153 "org.chromium.TestInterface")); |
| 154 if (name == properties->name.name()) |
| 155 last_name_value_ = properties->name.value(); |
| 156 |
| 157 // Store the updated property. |
133 updated_properties_.push_back(name); | 158 updated_properties_.push_back(name); |
134 message_loop_.Quit(); | 159 message_loop_.Quit(); |
135 } | 160 } |
136 | 161 |
137 static const size_t kExpectedObjects = 1; | 162 static const size_t kExpectedObjects = 1; |
138 static const size_t kExpectedProperties = 4; | 163 static const size_t kExpectedProperties = 4; |
139 | 164 |
140 void WaitForObject() { | 165 void WaitForObject() { |
141 while (added_objects_.size() < kExpectedObjects || | 166 while (added_objects_.size() < kExpectedObjects || |
142 updated_properties_.size() < kExpectedProperties) | 167 updated_properties_.size() < kExpectedProperties) |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 base::Unretained(this))); | 200 base::Unretained(this))); |
176 WaitForMethodCallback(); | 201 WaitForMethodCallback(); |
177 } | 202 } |
178 | 203 |
179 base::MessageLoop message_loop_; | 204 base::MessageLoop message_loop_; |
180 scoped_ptr<base::Thread> dbus_thread_; | 205 scoped_ptr<base::Thread> dbus_thread_; |
181 scoped_refptr<Bus> bus_; | 206 scoped_refptr<Bus> bus_; |
182 ObjectManager* object_manager_; | 207 ObjectManager* object_manager_; |
183 scoped_ptr<TestService> test_service_; | 208 scoped_ptr<TestService> test_service_; |
184 | 209 |
| 210 std::string last_name_value_; |
| 211 bool timeout_expired_; |
| 212 |
185 std::vector<std::pair<ObjectPath, std::string> > added_objects_; | 213 std::vector<std::pair<ObjectPath, std::string> > added_objects_; |
186 std::vector<std::pair<ObjectPath, std::string> > removed_objects_; | 214 std::vector<std::pair<ObjectPath, std::string> > removed_objects_; |
187 std::vector<std::string> updated_properties_; | 215 std::vector<std::string> updated_properties_; |
188 | 216 |
189 bool method_callback_called_; | 217 bool method_callback_called_; |
190 }; | 218 }; |
191 | 219 |
192 | 220 |
193 TEST_F(ObjectManagerTest, InitialObject) { | 221 TEST_F(ObjectManagerTest, InitialObject) { |
194 ObjectProxy* object_proxy = object_manager_->GetObjectProxy( | 222 ObjectProxy* object_proxy = object_manager_->GetObjectProxy( |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
343 | 371 |
344 TEST_F(ObjectManagerTest, OwnershipLostAndRegained) { | 372 TEST_F(ObjectManagerTest, OwnershipLostAndRegained) { |
345 PerformAction("Ownership", ObjectPath("/org/chromium/TestService")); | 373 PerformAction("Ownership", ObjectPath("/org/chromium/TestService")); |
346 WaitForRemoveObject(); | 374 WaitForRemoveObject(); |
347 WaitForObject(); | 375 WaitForObject(); |
348 | 376 |
349 std::vector<ObjectPath> object_paths = object_manager_->GetObjects(); | 377 std::vector<ObjectPath> object_paths = object_manager_->GetObjects(); |
350 ASSERT_EQ(1U, object_paths.size()); | 378 ASSERT_EQ(1U, object_paths.size()); |
351 } | 379 } |
352 | 380 |
| 381 TEST_F(ObjectManagerTest, PropertiesChangedAsObjectsReceived) { |
| 382 // Remove the existing object manager. |
| 383 object_manager_->UnregisterInterface("org.chromium.TestInterface"); |
| 384 EXPECT_TRUE(bus_->RemoveObjectManager( |
| 385 "org.chromium.TestService", |
| 386 ObjectPath("/org/chromium/TestService"), |
| 387 base::Bind(&ObjectManagerTest::OnObjectManagerRemoved, |
| 388 base::Unretained(this)))); |
| 389 message_loop_.Run(); |
| 390 |
| 391 PerformAction("SetSendImmediatePropertiesChanged", |
| 392 ObjectPath("/org/chromium/TestService")); |
| 393 |
| 394 object_manager_ = bus_->GetObjectManager( |
| 395 "org.chromium.TestService", |
| 396 ObjectPath("/org/chromium/TestService")); |
| 397 object_manager_->RegisterInterface("org.chromium.TestInterface", this); |
| 398 |
| 399 // The newly created object manager should call GetManagedObjects immediately |
| 400 // after setting up the match rule for PropertiesChanged. We should process |
| 401 // the PropertiesChanged event right after that. If we don't receive it within |
| 402 // 2 seconds, then fail the test. |
| 403 message_loop_.PostDelayedTask( |
| 404 FROM_HERE, |
| 405 base::Bind(&ObjectManagerTest::PropertiesChangedTestTimeout, |
| 406 base::Unretained(this)), |
| 407 base::TimeDelta::FromSeconds(2)); |
| 408 |
| 409 while (last_name_value_ != "ChangedTestServiceName" && !timeout_expired_) |
| 410 message_loop_.Run(); |
| 411 } |
| 412 |
353 } // namespace dbus | 413 } // namespace dbus |
OLD | NEW |