| 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/run_loop.h" |
| 13 #include "base/threading/thread.h" | 14 #include "base/threading/thread.h" |
| 14 #include "base/threading/thread_restrictions.h" | 15 #include "base/threading/thread_restrictions.h" |
| 15 #include "dbus/bus.h" | 16 #include "dbus/bus.h" |
| 16 #include "dbus/object_path.h" | 17 #include "dbus/object_path.h" |
| 17 #include "dbus/object_proxy.h" | 18 #include "dbus/object_proxy.h" |
| 18 #include "dbus/property.h" | 19 #include "dbus/property.h" |
| 19 #include "dbus/test_service.h" | 20 #include "dbus/test_service.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 21 | 22 |
| 22 namespace dbus { | 23 namespace dbus { |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 | 99 |
| 99 // Shut down the service. | 100 // Shut down the service. |
| 100 test_service_->ShutdownAndBlock(); | 101 test_service_->ShutdownAndBlock(); |
| 101 | 102 |
| 102 // Reset to the default. | 103 // Reset to the default. |
| 103 base::ThreadRestrictions::SetIOAllowed(true); | 104 base::ThreadRestrictions::SetIOAllowed(true); |
| 104 | 105 |
| 105 // Stopping a thread is considered an IO operation, so do this after | 106 // Stopping a thread is considered an IO operation, so do this after |
| 106 // allowing IO. | 107 // allowing IO. |
| 107 test_service_->Stop(); | 108 test_service_->Stop(); |
| 109 |
| 110 base::RunLoop().RunUntilIdle(); |
| 108 } | 111 } |
| 109 | 112 |
| 110 void MethodCallback(Response* response) { | 113 void MethodCallback(Response* response) { |
| 111 method_callback_called_ = true; | 114 method_callback_called_ = true; |
| 112 message_loop_.Quit(); | 115 run_loop_->Quit(); |
| 113 } | 116 } |
| 114 | 117 |
| 115 protected: | 118 protected: |
| 116 // Called when an object is added. | 119 // Called when an object is added. |
| 117 virtual void ObjectAdded(const ObjectPath& object_path, | 120 virtual void ObjectAdded(const ObjectPath& object_path, |
| 118 const std::string& interface_name) OVERRIDE { | 121 const std::string& interface_name) OVERRIDE { |
| 119 added_objects_.push_back(std::make_pair(object_path, interface_name)); | 122 added_objects_.push_back(std::make_pair(object_path, interface_name)); |
| 120 message_loop_.Quit(); | 123 run_loop_->Quit(); |
| 121 } | 124 } |
| 122 | 125 |
| 123 // Called when an object is removed. | 126 // Called when an object is removed. |
| 124 virtual void ObjectRemoved(const ObjectPath& object_path, | 127 virtual void ObjectRemoved(const ObjectPath& object_path, |
| 125 const std::string& interface_name) OVERRIDE { | 128 const std::string& interface_name) OVERRIDE { |
| 126 removed_objects_.push_back(std::make_pair(object_path, interface_name)); | 129 removed_objects_.push_back(std::make_pair(object_path, interface_name)); |
| 127 message_loop_.Quit(); | 130 run_loop_->Quit(); |
| 128 } | 131 } |
| 129 | 132 |
| 130 // Called when a property value is updated. | 133 // Called when a property value is updated. |
| 131 void OnPropertyChanged(const ObjectPath& object_path, | 134 void OnPropertyChanged(const ObjectPath& object_path, |
| 132 const std::string& name) { | 135 const std::string& name) { |
| 133 updated_properties_.push_back(name); | 136 updated_properties_.push_back(name); |
| 134 message_loop_.Quit(); | 137 run_loop_->Quit(); |
| 135 } | 138 } |
| 136 | 139 |
| 137 static const size_t kExpectedObjects = 1; | 140 static const size_t kExpectedObjects = 1; |
| 138 static const size_t kExpectedProperties = 4; | 141 static const size_t kExpectedProperties = 4; |
| 139 | 142 |
| 140 void WaitForObject() { | 143 void WaitForObject() { |
| 141 while (added_objects_.size() < kExpectedObjects || | 144 while (added_objects_.size() < kExpectedObjects || |
| 142 updated_properties_.size() < kExpectedProperties) | 145 updated_properties_.size() < kExpectedProperties) { |
| 143 message_loop_.Run(); | 146 run_loop_.reset(new base::RunLoop); |
| 147 run_loop_->Run(); |
| 148 } |
| 144 for (size_t i = 0; i < kExpectedObjects; ++i) | 149 for (size_t i = 0; i < kExpectedObjects; ++i) |
| 145 added_objects_.erase(added_objects_.begin()); | 150 added_objects_.erase(added_objects_.begin()); |
| 146 for (size_t i = 0; i < kExpectedProperties; ++i) | 151 for (size_t i = 0; i < kExpectedProperties; ++i) |
| 147 updated_properties_.erase(updated_properties_.begin()); | 152 updated_properties_.erase(updated_properties_.begin()); |
| 148 } | 153 } |
| 149 | 154 |
| 150 void WaitForRemoveObject() { | 155 void WaitForRemoveObject() { |
| 151 while (removed_objects_.size() < kExpectedObjects) | 156 while (removed_objects_.size() < kExpectedObjects) { |
| 152 message_loop_.Run(); | 157 run_loop_.reset(new base::RunLoop); |
| 158 run_loop_->Run(); |
| 159 } |
| 153 for (size_t i = 0; i < kExpectedObjects; ++i) | 160 for (size_t i = 0; i < kExpectedObjects; ++i) |
| 154 removed_objects_.erase(removed_objects_.begin()); | 161 removed_objects_.erase(removed_objects_.begin()); |
| 155 } | 162 } |
| 156 | 163 |
| 157 void WaitForMethodCallback() { | 164 void WaitForMethodCallback() { |
| 158 message_loop_.Run(); | 165 run_loop_.reset(new base::RunLoop); |
| 166 run_loop_->Run(); |
| 159 method_callback_called_ = false; | 167 method_callback_called_ = false; |
| 160 } | 168 } |
| 161 | 169 |
| 162 void PerformAction(const std::string& action, const ObjectPath& object_path) { | 170 void PerformAction(const std::string& action, const ObjectPath& object_path) { |
| 163 ObjectProxy* object_proxy = bus_->GetObjectProxy( | 171 ObjectProxy* object_proxy = bus_->GetObjectProxy( |
| 164 "org.chromium.TestService", | 172 "org.chromium.TestService", |
| 165 ObjectPath("/org/chromium/TestObject")); | 173 ObjectPath("/org/chromium/TestObject")); |
| 166 | 174 |
| 167 MethodCall method_call("org.chromium.TestInterface", "PerformAction"); | 175 MethodCall method_call("org.chromium.TestInterface", "PerformAction"); |
| 168 MessageWriter writer(&method_call); | 176 MessageWriter writer(&method_call); |
| 169 writer.AppendString(action); | 177 writer.AppendString(action); |
| 170 writer.AppendObjectPath(object_path); | 178 writer.AppendObjectPath(object_path); |
| 171 | 179 |
| 172 object_proxy->CallMethod(&method_call, | 180 object_proxy->CallMethod(&method_call, |
| 173 ObjectProxy::TIMEOUT_USE_DEFAULT, | 181 ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 174 base::Bind(&ObjectManagerTest::MethodCallback, | 182 base::Bind(&ObjectManagerTest::MethodCallback, |
| 175 base::Unretained(this))); | 183 base::Unretained(this))); |
| 176 WaitForMethodCallback(); | 184 WaitForMethodCallback(); |
| 177 } | 185 } |
| 178 | 186 |
| 179 base::MessageLoop message_loop_; | 187 base::MessageLoop message_loop_; |
| 188 scoped_ptr<base::RunLoop> run_loop_; |
| 180 scoped_ptr<base::Thread> dbus_thread_; | 189 scoped_ptr<base::Thread> dbus_thread_; |
| 181 scoped_refptr<Bus> bus_; | 190 scoped_refptr<Bus> bus_; |
| 182 ObjectManager* object_manager_; | 191 ObjectManager* object_manager_; |
| 183 scoped_ptr<TestService> test_service_; | 192 scoped_ptr<TestService> test_service_; |
| 184 | 193 |
| 185 std::vector<std::pair<ObjectPath, std::string> > added_objects_; | 194 std::vector<std::pair<ObjectPath, std::string> > added_objects_; |
| 186 std::vector<std::pair<ObjectPath, std::string> > removed_objects_; | 195 std::vector<std::pair<ObjectPath, std::string> > removed_objects_; |
| 187 std::vector<std::string> updated_properties_; | 196 std::vector<std::string> updated_properties_; |
| 188 | 197 |
| 189 bool method_callback_called_; | 198 bool method_callback_called_; |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 344 TEST_F(ObjectManagerTest, OwnershipLostAndRegained) { | 353 TEST_F(ObjectManagerTest, OwnershipLostAndRegained) { |
| 345 PerformAction("Ownership", ObjectPath("/org/chromium/TestService")); | 354 PerformAction("Ownership", ObjectPath("/org/chromium/TestService")); |
| 346 WaitForRemoveObject(); | 355 WaitForRemoveObject(); |
| 347 WaitForObject(); | 356 WaitForObject(); |
| 348 | 357 |
| 349 std::vector<ObjectPath> object_paths = object_manager_->GetObjects(); | 358 std::vector<ObjectPath> object_paths = object_manager_->GetObjects(); |
| 350 ASSERT_EQ(1U, object_paths.size()); | 359 ASSERT_EQ(1U, object_paths.size()); |
| 351 } | 360 } |
| 352 | 361 |
| 353 } // namespace dbus | 362 } // namespace dbus |
| OLD | NEW |