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
3 // found in the LICENSE file.
5 #include "chromeos/dbus/shill_client_unittest_base.h"
8 #include "base/json/json_writer.h"
9 #include "base/values.h"
10 #include "dbus/message.h"
11 #include "dbus/object_path.h"
12 #include "dbus/values_util.h"
13 #include "testing/gmock/include/gmock/gmock.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "third_party/cros_system_api/dbus/service_constants.h"
18 using ::testing::Invoke
;
19 using ::testing::Return
;
25 // Runs the given task. This function is used to implement the mock bus.
26 void RunTask(const tracked_objects::Location
& from_here
,
27 const base::Closure
& task
) {
33 ValueMatcher::ValueMatcher(const base::Value
& value
)
34 : expected_value_(value
.DeepCopy()) {}
36 bool ValueMatcher::MatchAndExplain(const base::Value
& value
,
37 MatchResultListener
* listener
) const {
38 return expected_value_
->Equals(&value
);
41 void ValueMatcher::DescribeTo(::std::ostream
* os
) const {
42 std::string expected_value_str
;
43 base::JSONWriter::WriteWithOptions(expected_value_
.get(),
44 base::JSONWriter::OPTIONS_PRETTY_PRINT
,
46 *os
<< "value equals " << expected_value_str
;
49 void ValueMatcher::DescribeNegationTo(::std::ostream
* os
) const {
50 std::string expected_value_str
;
51 base::JSONWriter::WriteWithOptions(expected_value_
.get(),
52 base::JSONWriter::OPTIONS_PRETTY_PRINT
,
54 *os
<< "value does not equal " << expected_value_str
;
58 ShillClientUnittestBase::MockClosure::MockClosure() {}
60 ShillClientUnittestBase::MockClosure::~MockClosure() {}
62 base::Closure
ShillClientUnittestBase::MockClosure::GetCallback() {
63 return base::Bind(&MockClosure::Run
, base::Unretained(this));
67 ShillClientUnittestBase::MockListValueCallback::MockListValueCallback() {}
69 ShillClientUnittestBase::MockListValueCallback::~MockListValueCallback() {}
71 ShillClientHelper::ListValueCallback
72 ShillClientUnittestBase::MockListValueCallback::GetCallback() {
73 return base::Bind(&MockListValueCallback::Run
, base::Unretained(this));
77 ShillClientUnittestBase::MockErrorCallback::MockErrorCallback() {}
79 ShillClientUnittestBase::MockErrorCallback::~MockErrorCallback() {}
81 ShillClientUnittestBase::MockPropertyChangeObserver::
82 MockPropertyChangeObserver() {}
84 ShillClientUnittestBase::MockPropertyChangeObserver::
85 ~MockPropertyChangeObserver() {}
87 ShillClientHelper::ErrorCallback
88 ShillClientUnittestBase::MockErrorCallback::GetCallback() {
89 return base::Bind(&MockErrorCallback::Run
, base::Unretained(this));
93 ShillClientUnittestBase::ShillClientUnittestBase(
94 const std::string
& interface_name
,
95 const dbus::ObjectPath
& object_path
)
96 : interface_name_(interface_name
),
97 object_path_(object_path
),
101 ShillClientUnittestBase::~ShillClientUnittestBase() {
104 void ShillClientUnittestBase::SetUp() {
105 // Create a mock bus.
106 dbus::Bus::Options options
;
107 options
.bus_type
= dbus::Bus::SYSTEM
;
108 mock_bus_
= new dbus::MockBus(options
);
110 // Create a mock proxy.
111 mock_proxy_
= new dbus::MockObjectProxy(
113 flimflam::kFlimflamServiceName
,
116 // Set an expectation so mock_proxy's CallMethodAndBlock() will use
117 // OnCallMethodAndBlock() to return responses.
118 EXPECT_CALL(*mock_proxy_
, MockCallMethodAndBlock(_
, _
))
119 .WillRepeatedly(Invoke(
120 this, &ShillClientUnittestBase::OnCallMethodAndBlock
));
122 // Set an expectation so mock_proxy's CallMethod() will use OnCallMethod()
123 // to return responses.
124 EXPECT_CALL(*mock_proxy_
, CallMethod(_
, _
, _
))
125 .WillRepeatedly(Invoke(this, &ShillClientUnittestBase::OnCallMethod
));
127 // Set an expectation so mock_proxy's CallMethodWithErrorCallback() will use
128 // OnCallMethodWithErrorCallback() to return responses.
129 EXPECT_CALL(*mock_proxy_
, CallMethodWithErrorCallback(_
, _
, _
, _
))
130 .WillRepeatedly(Invoke(
131 this, &ShillClientUnittestBase::OnCallMethodWithErrorCallback
));
133 // Set an expectation so mock_proxy's ConnectToSignal() will use
134 // OnConnectToSignal() to run the callback.
135 EXPECT_CALL(*mock_proxy_
, ConnectToSignal(
137 flimflam::kMonitorPropertyChanged
, _
, _
))
138 .WillRepeatedly(Invoke(this,
139 &ShillClientUnittestBase::OnConnectToSignal
));
141 // Set an expectation so mock_bus's GetObjectProxy() for the given
142 // service name and the object path will return mock_proxy_.
143 EXPECT_CALL(*mock_bus_
, GetObjectProxy(flimflam::kFlimflamServiceName
,
145 .WillOnce(Return(mock_proxy_
.get()));
147 // Set an expectation so mock_bus's PostTaskToDBusThread() will run the
149 EXPECT_CALL(*mock_bus_
, PostTaskToDBusThread(_
, _
))
150 .WillRepeatedly(Invoke(&RunTask
));
152 // ShutdownAndBlock() will be called in TearDown().
153 EXPECT_CALL(*mock_bus_
, ShutdownAndBlock()).WillOnce(Return());
156 void ShillClientUnittestBase::TearDown() {
157 mock_bus_
->ShutdownAndBlock();
160 void ShillClientUnittestBase::PrepareForMethodCall(
161 const std::string
& method_name
,
162 const ArgumentCheckCallback
& argument_checker
,
163 dbus::Response
* response
) {
164 expected_method_name_
= method_name
;
165 argument_checker_
= argument_checker
;
166 response_
= response
;
169 void ShillClientUnittestBase::SendPropertyChangedSignal(
170 dbus::Signal
* signal
) {
171 ASSERT_FALSE(property_changed_handler_
.is_null());
172 property_changed_handler_
.Run(signal
);
176 void ShillClientUnittestBase::ExpectPropertyChanged(
177 const std::string
& expected_name
,
178 const base::Value
* expected_value
,
179 const std::string
& name
,
180 const base::Value
& value
) {
181 EXPECT_EQ(expected_name
, name
);
182 EXPECT_TRUE(expected_value
->Equals(&value
));
186 void ShillClientUnittestBase::ExpectNoArgument(dbus::MessageReader
* reader
) {
187 EXPECT_FALSE(reader
->HasMoreData());
191 void ShillClientUnittestBase::ExpectStringArgument(
192 const std::string
& expected_string
,
193 dbus::MessageReader
* reader
) {
195 ASSERT_TRUE(reader
->PopString(&str
));
196 EXPECT_EQ(expected_string
, str
);
197 EXPECT_FALSE(reader
->HasMoreData());
201 void ShillClientUnittestBase::ExpectArrayOfStringsArgument(
202 const std::vector
<std::string
>& expected_strings
,
203 dbus::MessageReader
* reader
) {
204 std::vector
<std::string
> strs
;
205 ASSERT_TRUE(reader
->PopArrayOfStrings(&strs
));
206 EXPECT_EQ(expected_strings
, strs
);
207 EXPECT_FALSE(reader
->HasMoreData());
211 void ShillClientUnittestBase::ExpectValueArgument(
212 const base::Value
* expected_value
,
213 dbus::MessageReader
* reader
) {
214 scoped_ptr
<base::Value
> value(dbus::PopDataAsValue(reader
));
215 ASSERT_TRUE(value
.get());
216 EXPECT_TRUE(value
->Equals(expected_value
));
217 EXPECT_FALSE(reader
->HasMoreData());
221 void ShillClientUnittestBase::ExpectStringAndValueArguments(
222 const std::string
& expected_string
,
223 const base::Value
* expected_value
,
224 dbus::MessageReader
* reader
) {
226 ASSERT_TRUE(reader
->PopString(&str
));
227 EXPECT_EQ(expected_string
, str
);
228 scoped_ptr
<base::Value
> value(dbus::PopDataAsValue(reader
));
229 ASSERT_TRUE(value
.get());
230 EXPECT_TRUE(value
->Equals(expected_value
));
231 EXPECT_FALSE(reader
->HasMoreData());
235 void ShillClientUnittestBase::ExpectNoResultValue(
236 DBusMethodCallStatus call_status
) {
237 EXPECT_EQ(DBUS_METHOD_CALL_SUCCESS
, call_status
);
241 void ShillClientUnittestBase::ExpectObjectPathResult(
242 const dbus::ObjectPath
& expected_result
,
243 DBusMethodCallStatus call_status
,
244 const dbus::ObjectPath
& result
) {
245 EXPECT_EQ(DBUS_METHOD_CALL_SUCCESS
, call_status
);
246 EXPECT_EQ(expected_result
, result
);
250 void ShillClientUnittestBase::ExpectObjectPathResultWithoutStatus(
251 const dbus::ObjectPath
& expected_result
,
252 const dbus::ObjectPath
& result
) {
253 EXPECT_EQ(expected_result
, result
);
257 void ShillClientUnittestBase::ExpectBoolResultWithoutStatus(
258 bool expected_result
,
260 EXPECT_EQ(expected_result
, result
);
264 void ShillClientUnittestBase::ExpectStringResultWithoutStatus(
265 const std::string
& expected_result
,
266 const std::string
& result
) {
267 EXPECT_EQ(expected_result
, result
);
271 void ShillClientUnittestBase::ExpectDictionaryValueResultWithoutStatus(
272 const base::DictionaryValue
* expected_result
,
273 const base::DictionaryValue
& result
) {
274 std::string expected_result_string
;
275 base::JSONWriter::Write(expected_result
, &expected_result_string
);
276 std::string result_string
;
277 base::JSONWriter::Write(&result
, &result_string
);
278 EXPECT_EQ(expected_result_string
, result_string
);
282 void ShillClientUnittestBase::ExpectDictionaryValueResult(
283 const base::DictionaryValue
* expected_result
,
284 DBusMethodCallStatus call_status
,
285 const base::DictionaryValue
& result
) {
286 EXPECT_EQ(DBUS_METHOD_CALL_SUCCESS
, call_status
);
287 ExpectDictionaryValueResultWithoutStatus(expected_result
, result
);
290 void ShillClientUnittestBase::OnConnectToSignal(
291 const std::string
& interface_name
,
292 const std::string
& signal_name
,
293 const dbus::ObjectProxy::SignalCallback
& signal_callback
,
294 const dbus::ObjectProxy::OnConnectedCallback
& on_connected_callback
) {
295 property_changed_handler_
= signal_callback
;
296 const bool success
= true;
297 message_loop_
.PostTask(FROM_HERE
,
298 base::Bind(on_connected_callback
,
304 void ShillClientUnittestBase::OnCallMethod(
305 dbus::MethodCall
* method_call
,
307 const dbus::ObjectProxy::ResponseCallback
& response_callback
) {
308 EXPECT_EQ(interface_name_
, method_call
->GetInterface());
309 EXPECT_EQ(expected_method_name_
, method_call
->GetMember());
310 dbus::MessageReader
reader(method_call
);
311 argument_checker_
.Run(&reader
);
312 message_loop_
.PostTask(FROM_HERE
,
313 base::Bind(response_callback
, response_
));
316 void ShillClientUnittestBase::OnCallMethodWithErrorCallback(
317 dbus::MethodCall
* method_call
,
319 const dbus::ObjectProxy::ResponseCallback
& response_callback
,
320 const dbus::ObjectProxy::ErrorCallback
& error_callback
) {
321 OnCallMethod(method_call
, timeout_ms
, response_callback
);
324 dbus::Response
* ShillClientUnittestBase::OnCallMethodAndBlock(
325 dbus::MethodCall
* method_call
,
327 EXPECT_EQ(interface_name_
, method_call
->GetInterface());
328 EXPECT_EQ(expected_method_name_
, method_call
->GetMember());
329 dbus::MessageReader
reader(method_call
);
330 argument_checker_
.Run(&reader
);
331 return dbus::Response::FromRawMessage(
332 dbus_message_copy(response_
->raw_message())).release();
335 } // namespace chromeos