OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chromeos/dbus/services/service_provider_test_helper.h" | 5 #include "chromeos/dbus/services/service_provider_test_helper.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "dbus/message.h" | 8 #include "dbus/message.h" |
9 #include "dbus/mock_bus.h" | 9 #include "dbus/mock_bus.h" |
10 #include "third_party/cros_system_api/dbus/service_constants.h" | 10 #include "third_party/cros_system_api/dbus/service_constants.h" |
11 | 11 |
12 using ::testing::_; | 12 using ::testing::_; |
13 using ::testing::AllOf; | 13 using ::testing::AllOf; |
14 using ::testing::Invoke; | 14 using ::testing::Invoke; |
15 using ::testing::ResultOf; | 15 using ::testing::ResultOf; |
16 using ::testing::Return; | 16 using ::testing::Return; |
17 using ::testing::Unused; | 17 using ::testing::Unused; |
18 | 18 |
19 namespace chromeos { | 19 namespace chromeos { |
20 | 20 |
21 ServiceProviderTestHelper::ServiceProviderTestHelper() | 21 ServiceProviderTestHelper::ServiceProviderTestHelper() |
22 : response_received_(false) { | 22 : response_received_(false) { |
| 23 if (!base::MessageLoop::current()) |
| 24 message_loop_.reset(new base::MessageLoop()); |
23 } | 25 } |
24 | 26 |
25 ServiceProviderTestHelper::~ServiceProviderTestHelper() { | 27 ServiceProviderTestHelper::~ServiceProviderTestHelper() { |
26 } | 28 } |
27 | 29 |
28 void ServiceProviderTestHelper::SetUp( | 30 void ServiceProviderTestHelper::SetUp( |
29 const std::string& exported_method_name, | 31 const std::string& exported_method_name, |
30 CrosDBusService::ServiceProviderInterface* service_provider) { | 32 CrosDBusService::ServiceProviderInterface* service_provider) { |
31 // Create a mock bus. | 33 // Create a mock bus. |
32 dbus::Bus::Options options; | 34 dbus::Bus::Options options; |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
122 // dbus_message_new_method_return() won't emit a warning. | 124 // dbus_message_new_method_return() won't emit a warning. |
123 method_call->SetSerial(1); | 125 method_call->SetSerial(1); |
124 // Run the callback captured in MockExportMethod(). In addition to returning | 126 // Run the callback captured in MockExportMethod(). In addition to returning |
125 // a response that the caller will ignore, this will send a signal, which | 127 // a response that the caller will ignore, this will send a signal, which |
126 // will be received by |on_signal_callback_|. | 128 // will be received by |on_signal_callback_|. |
127 method_callback_.Run(method_call, | 129 method_callback_.Run(method_call, |
128 base::Bind(&ServiceProviderTestHelper::OnResponse, | 130 base::Bind(&ServiceProviderTestHelper::OnResponse, |
129 base::Unretained(this))); | 131 base::Unretained(this))); |
130 // Check for a response. | 132 // Check for a response. |
131 if (!response_received_) | 133 if (!response_received_) |
132 message_loop_.Run(); | 134 base::MessageLoop::current()->Run(); |
133 // Return response. | 135 // Return response. |
134 return response_.release(); | 136 return response_.release(); |
135 } | 137 } |
136 | 138 |
137 void ServiceProviderTestHelper::MockConnectToSignal( | 139 void ServiceProviderTestHelper::MockConnectToSignal( |
138 const std::string& interface_name, | 140 const std::string& interface_name, |
139 const std::string& signal_name, | 141 const std::string& signal_name, |
140 dbus::ObjectProxy::SignalCallback signal_callback, | 142 dbus::ObjectProxy::SignalCallback signal_callback, |
141 dbus::ObjectProxy::OnConnectedCallback connected_callback) { | 143 dbus::ObjectProxy::OnConnectedCallback connected_callback) { |
142 // Tell the callback that the object proxy is connected to the signal. | 144 // Tell the callback that the object proxy is connected to the signal. |
143 connected_callback.Run(interface_name, signal_name, true); | 145 connected_callback.Run(interface_name, signal_name, true); |
144 // Capture the callback, so we can run this at a later time. | 146 // Capture the callback, so we can run this at a later time. |
145 on_signal_callback_ = signal_callback; | 147 on_signal_callback_ = signal_callback; |
146 } | 148 } |
147 | 149 |
148 void ServiceProviderTestHelper::MockSendSignal(dbus::Signal* signal) { | 150 void ServiceProviderTestHelper::MockSendSignal(dbus::Signal* signal) { |
149 // Run the callback captured in MockConnectToSignal(). This will call | 151 // Run the callback captured in MockConnectToSignal(). This will call |
150 // OnSignalReceived(). | 152 // OnSignalReceived(). |
151 on_signal_callback_.Run(signal); | 153 on_signal_callback_.Run(signal); |
152 } | 154 } |
153 | 155 |
154 void ServiceProviderTestHelper::OnResponse( | 156 void ServiceProviderTestHelper::OnResponse( |
155 scoped_ptr<dbus::Response> response) { | 157 scoped_ptr<dbus::Response> response) { |
156 response_ = response.Pass(); | 158 response_ = response.Pass(); |
157 response_received_ = true; | 159 response_received_ = true; |
158 if (message_loop_.is_running()) | 160 if (base::MessageLoop::current()->is_running()) |
159 message_loop_.Quit(); | 161 base::MessageLoop::current()->Quit(); |
160 } | 162 } |
161 | 163 |
162 } // namespace chromeos | 164 } // namespace chromeos |
OLD | NEW |