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" |
(...skipping 10 matching lines...) Expand all Loading... |
21 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
22 | 22 |
23 namespace dbus { | 23 namespace dbus { |
24 | 24 |
25 // The object manager test exercises the asynchronous APIs in ObjectManager, | 25 // The object manager test exercises the asynchronous APIs in ObjectManager, |
26 // and by extension PropertySet and Property<>. | 26 // and by extension PropertySet and Property<>. |
27 class ObjectManagerTest | 27 class ObjectManagerTest |
28 : public testing::Test, | 28 : public testing::Test, |
29 public ObjectManager::Interface { | 29 public ObjectManager::Interface { |
30 public: | 30 public: |
31 ObjectManagerTest() { | 31 ObjectManagerTest() : timeout_expired_(false) { |
32 } | 32 } |
33 | 33 |
34 struct Properties : public PropertySet { | 34 struct Properties : public PropertySet { |
35 Property<std::string> name; | 35 Property<std::string> name; |
36 Property<int16> version; | 36 Property<int16> version; |
37 Property<std::vector<std::string> > methods; | 37 Property<std::vector<std::string> > methods; |
38 Property<std::vector<ObjectPath> > objects; | 38 Property<std::vector<ObjectPath> > objects; |
39 | 39 |
40 Properties(ObjectProxy* object_proxy, | 40 Properties(ObjectProxy* object_proxy, |
41 const std::string& interface_name, | 41 const std::string& interface_name, |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 bus_options.connection_type = Bus::PRIVATE; | 83 bus_options.connection_type = Bus::PRIVATE; |
84 bus_options.dbus_task_runner = dbus_thread_->message_loop_proxy(); | 84 bus_options.dbus_task_runner = dbus_thread_->message_loop_proxy(); |
85 bus_ = new Bus(bus_options); | 85 bus_ = new Bus(bus_options); |
86 ASSERT_TRUE(bus_->HasDBusThread()); | 86 ASSERT_TRUE(bus_->HasDBusThread()); |
87 | 87 |
88 object_manager_ = bus_->GetObjectManager( | 88 object_manager_ = bus_->GetObjectManager( |
89 "org.chromium.TestService", | 89 "org.chromium.TestService", |
90 ObjectPath("/org/chromium/TestService")); | 90 ObjectPath("/org/chromium/TestService")); |
91 object_manager_->RegisterInterface("org.chromium.TestInterface", this); | 91 object_manager_->RegisterInterface("org.chromium.TestInterface", this); |
92 | 92 |
93 object_manager_->GetManagedObjects(); | |
94 WaitForObject(); | 93 WaitForObject(); |
95 } | 94 } |
96 | 95 |
97 virtual void TearDown() { | 96 virtual void TearDown() { |
98 bus_->ShutdownOnDBusThreadAndBlock(); | 97 bus_->ShutdownOnDBusThreadAndBlock(); |
99 | 98 |
100 // Shut down the service. | 99 // Shut down the service. |
101 test_service_->ShutdownAndBlock(); | 100 test_service_->ShutdownAndBlock(); |
102 | 101 |
103 // Reset to the default. | 102 // Reset to the default. |
104 base::ThreadRestrictions::SetIOAllowed(true); | 103 base::ThreadRestrictions::SetIOAllowed(true); |
105 | 104 |
106 // 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 |
107 // allowing IO. | 106 // allowing IO. |
108 test_service_->Stop(); | 107 test_service_->Stop(); |
109 | 108 |
110 base::RunLoop().RunUntilIdle(); | 109 base::RunLoop().RunUntilIdle(); |
111 } | 110 } |
112 | 111 |
113 void MethodCallback(Response* response) { | 112 void MethodCallback(Response* response) { |
114 method_callback_called_ = true; | 113 method_callback_called_ = true; |
115 run_loop_->Quit(); | 114 run_loop_->Quit(); |
116 } | 115 } |
117 | 116 |
| 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 |
118 protected: | 127 protected: |
119 // Called when an object is added. | 128 // Called when an object is added. |
120 virtual void ObjectAdded(const ObjectPath& object_path, | 129 virtual void ObjectAdded(const ObjectPath& object_path, |
121 const std::string& interface_name) OVERRIDE { | 130 const std::string& interface_name) OVERRIDE { |
122 added_objects_.push_back(std::make_pair(object_path, interface_name)); | 131 added_objects_.push_back(std::make_pair(object_path, interface_name)); |
123 run_loop_->Quit(); | 132 run_loop_->Quit(); |
124 } | 133 } |
125 | 134 |
126 // Called when an object is removed. | 135 // Called when an object is removed. |
127 virtual void ObjectRemoved(const ObjectPath& object_path, | 136 virtual void ObjectRemoved(const ObjectPath& object_path, |
128 const std::string& interface_name) OVERRIDE { | 137 const std::string& interface_name) OVERRIDE { |
129 removed_objects_.push_back(std::make_pair(object_path, interface_name)); | 138 removed_objects_.push_back(std::make_pair(object_path, interface_name)); |
130 run_loop_->Quit(); | 139 run_loop_->Quit(); |
131 } | 140 } |
132 | 141 |
133 // Called when a property value is updated. | 142 // Called when a property value is updated. |
134 void OnPropertyChanged(const ObjectPath& object_path, | 143 void OnPropertyChanged(const ObjectPath& object_path, |
135 const std::string& name) { | 144 const std::string& name) { |
| 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. |
136 updated_properties_.push_back(name); | 155 updated_properties_.push_back(name); |
137 run_loop_->Quit(); | 156 run_loop_->Quit(); |
138 } | 157 } |
139 | 158 |
140 static const size_t kExpectedObjects = 1; | 159 static const size_t kExpectedObjects = 1; |
141 static const size_t kExpectedProperties = 4; | 160 static const size_t kExpectedProperties = 4; |
142 | 161 |
143 void WaitForObject() { | 162 void WaitForObject() { |
144 while (added_objects_.size() < kExpectedObjects || | 163 while (added_objects_.size() < kExpectedObjects || |
145 updated_properties_.size() < kExpectedProperties) { | 164 updated_properties_.size() < kExpectedProperties) { |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
184 WaitForMethodCallback(); | 203 WaitForMethodCallback(); |
185 } | 204 } |
186 | 205 |
187 base::MessageLoop message_loop_; | 206 base::MessageLoop message_loop_; |
188 scoped_ptr<base::RunLoop> run_loop_; | 207 scoped_ptr<base::RunLoop> run_loop_; |
189 scoped_ptr<base::Thread> dbus_thread_; | 208 scoped_ptr<base::Thread> dbus_thread_; |
190 scoped_refptr<Bus> bus_; | 209 scoped_refptr<Bus> bus_; |
191 ObjectManager* object_manager_; | 210 ObjectManager* object_manager_; |
192 scoped_ptr<TestService> test_service_; | 211 scoped_ptr<TestService> test_service_; |
193 | 212 |
| 213 std::string last_name_value_; |
| 214 bool timeout_expired_; |
| 215 |
194 std::vector<std::pair<ObjectPath, std::string> > added_objects_; | 216 std::vector<std::pair<ObjectPath, std::string> > added_objects_; |
195 std::vector<std::pair<ObjectPath, std::string> > removed_objects_; | 217 std::vector<std::pair<ObjectPath, std::string> > removed_objects_; |
196 std::vector<std::string> updated_properties_; | 218 std::vector<std::string> updated_properties_; |
197 | 219 |
198 bool method_callback_called_; | 220 bool method_callback_called_; |
199 }; | 221 }; |
200 | 222 |
201 | 223 |
202 TEST_F(ObjectManagerTest, InitialObject) { | 224 TEST_F(ObjectManagerTest, InitialObject) { |
203 ObjectProxy* object_proxy = object_manager_->GetObjectProxy( | 225 ObjectProxy* object_proxy = object_manager_->GetObjectProxy( |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
352 | 374 |
353 TEST_F(ObjectManagerTest, OwnershipLostAndRegained) { | 375 TEST_F(ObjectManagerTest, OwnershipLostAndRegained) { |
354 PerformAction("Ownership", ObjectPath("/org/chromium/TestService")); | 376 PerformAction("Ownership", ObjectPath("/org/chromium/TestService")); |
355 WaitForRemoveObject(); | 377 WaitForRemoveObject(); |
356 WaitForObject(); | 378 WaitForObject(); |
357 | 379 |
358 std::vector<ObjectPath> object_paths = object_manager_->GetObjects(); | 380 std::vector<ObjectPath> object_paths = object_manager_->GetObjects(); |
359 ASSERT_EQ(1U, object_paths.size()); | 381 ASSERT_EQ(1U, object_paths.size()); |
360 } | 382 } |
361 | 383 |
| 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 |
362 } // namespace dbus | 418 } // namespace dbus |
OLD | NEW |