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 "chromeos/network/shill_property_util.h"
11 #include "dbus/message.h"
12 #include "dbus/object_path.h"
13 #include "dbus/values_util.h"
14 #include "testing/gmock/include/gmock/gmock.h"
15 #include "third_party/cros_system_api/dbus/service_constants.h"
18 using ::testing::Invoke
;
19 using ::testing::Return
;
25 // Pops a string-to-string dictionary from the reader.
26 base::DictionaryValue
* PopStringToStringDictionary(
27 dbus::MessageReader
* reader
) {
28 dbus::MessageReader
array_reader(NULL
);
29 if (!reader
->PopArray(&array_reader
))
31 scoped_ptr
<base::DictionaryValue
> result(new base::DictionaryValue
);
32 while (array_reader
.HasMoreData()) {
33 dbus::MessageReader
entry_reader(NULL
);
36 if (!array_reader
.PopDictEntry(&entry_reader
) ||
37 !entry_reader
.PopString(&key
) ||
38 !entry_reader
.PopString(&value
))
40 result
->SetWithoutPathExpansion(key
, new base::StringValue(value
));
42 return result
.release();
47 ValueMatcher::ValueMatcher(const base::Value
& value
)
48 : expected_value_(value
.DeepCopy()) {}
50 bool ValueMatcher::MatchAndExplain(const base::Value
& value
,
51 MatchResultListener
* listener
) const {
52 return expected_value_
->Equals(&value
);
55 void ValueMatcher::DescribeTo(::std::ostream
* os
) const {
56 std::string expected_value_str
;
57 base::JSONWriter::WriteWithOptions(*expected_value_
,
58 base::JSONWriter::OPTIONS_PRETTY_PRINT
,
60 *os
<< "value equals " << expected_value_str
;
63 void ValueMatcher::DescribeNegationTo(::std::ostream
* os
) const {
64 std::string expected_value_str
;
65 base::JSONWriter::WriteWithOptions(*expected_value_
,
66 base::JSONWriter::OPTIONS_PRETTY_PRINT
,
68 *os
<< "value does not equal " << expected_value_str
;
72 ShillClientUnittestBase::MockClosure::MockClosure() {}
74 ShillClientUnittestBase::MockClosure::~MockClosure() {}
76 base::Closure
ShillClientUnittestBase::MockClosure::GetCallback() {
77 return base::Bind(&MockClosure::Run
, base::Unretained(this));
81 ShillClientUnittestBase::MockListValueCallback::MockListValueCallback() {}
83 ShillClientUnittestBase::MockListValueCallback::~MockListValueCallback() {}
85 ShillClientHelper::ListValueCallback
86 ShillClientUnittestBase::MockListValueCallback::GetCallback() {
87 return base::Bind(&MockListValueCallback::Run
, base::Unretained(this));
91 ShillClientUnittestBase::MockErrorCallback::MockErrorCallback() {}
93 ShillClientUnittestBase::MockErrorCallback::~MockErrorCallback() {}
95 ShillClientUnittestBase::MockPropertyChangeObserver::
96 MockPropertyChangeObserver() {}
98 ShillClientUnittestBase::MockPropertyChangeObserver::
99 ~MockPropertyChangeObserver() {}
101 ShillClientHelper::ErrorCallback
102 ShillClientUnittestBase::MockErrorCallback::GetCallback() {
103 return base::Bind(&MockErrorCallback::Run
, base::Unretained(this));
107 ShillClientUnittestBase::ShillClientUnittestBase(
108 const std::string
& interface_name
,
109 const dbus::ObjectPath
& object_path
)
110 : interface_name_(interface_name
),
111 object_path_(object_path
),
115 ShillClientUnittestBase::~ShillClientUnittestBase() {
118 void ShillClientUnittestBase::SetUp() {
119 // Create a mock bus.
120 dbus::Bus::Options options
;
121 options
.bus_type
= dbus::Bus::SYSTEM
;
122 mock_bus_
= new dbus::MockBus(options
);
124 // Create a mock proxy.
125 mock_proxy_
= new dbus::MockObjectProxy(
127 shill::kFlimflamServiceName
,
130 // Set an expectation so mock_proxy's CallMethod() will use OnCallMethod()
131 // to return responses.
132 EXPECT_CALL(*mock_proxy_
.get(), CallMethod(_
, _
, _
))
133 .WillRepeatedly(Invoke(this, &ShillClientUnittestBase::OnCallMethod
));
135 // Set an expectation so mock_proxy's CallMethodWithErrorCallback() will use
136 // OnCallMethodWithErrorCallback() to return responses.
137 EXPECT_CALL(*mock_proxy_
.get(), CallMethodWithErrorCallback(_
, _
, _
, _
))
138 .WillRepeatedly(Invoke(
139 this, &ShillClientUnittestBase::OnCallMethodWithErrorCallback
));
141 // Set an expectation so mock_proxy's ConnectToSignal() will use
142 // OnConnectToPropertyChanged() to run the callback.
145 ConnectToSignal(interface_name_
, shill::kMonitorPropertyChanged
, _
, _
))
147 Invoke(this, &ShillClientUnittestBase::OnConnectToPropertyChanged
));
151 ConnectToSignal(interface_name_
, shill::kOnPlatformMessageFunction
, _
, _
))
153 Invoke(this, &ShillClientUnittestBase::OnConnectToPlatformMessage
));
157 ConnectToSignal(interface_name_
, shill::kOnPacketReceivedFunction
, _
, _
))
159 Invoke(this, &ShillClientUnittestBase::OnConnectToPacketReceived
));
161 // Set an expectation so mock_bus's GetObjectProxy() for the given
162 // service name and the object path will return mock_proxy_.
163 EXPECT_CALL(*mock_bus_
.get(),
164 GetObjectProxy(shill::kFlimflamServiceName
, object_path_
))
165 .WillOnce(Return(mock_proxy_
.get()));
167 // Set an expectation so mock_bus's GetDBusTaskRunner will return the current
169 EXPECT_CALL(*mock_bus_
.get(), GetDBusTaskRunner())
170 .WillRepeatedly(Return(message_loop_
.message_loop_proxy().get()));
172 // ShutdownAndBlock() will be called in TearDown().
173 EXPECT_CALL(*mock_bus_
.get(), ShutdownAndBlock()).WillOnce(Return());
176 void ShillClientUnittestBase::TearDown() {
177 mock_bus_
->ShutdownAndBlock();
180 void ShillClientUnittestBase::PrepareForMethodCall(
181 const std::string
& method_name
,
182 const ArgumentCheckCallback
& argument_checker
,
183 dbus::Response
* response
) {
184 expected_method_name_
= method_name
;
185 argument_checker_
= argument_checker
;
186 response_
= response
;
189 void ShillClientUnittestBase::SendPlatformMessageSignal(
190 dbus::Signal
* signal
) {
191 ASSERT_FALSE(platform_message_handler_
.is_null());
192 platform_message_handler_
.Run(signal
);
195 void ShillClientUnittestBase::SendPacketReceievedSignal(
196 dbus::Signal
* signal
) {
197 ASSERT_FALSE(packet_receieved__handler_
.is_null());
198 packet_receieved__handler_
.Run(signal
);
201 void ShillClientUnittestBase::SendPropertyChangedSignal(
202 dbus::Signal
* signal
) {
203 ASSERT_FALSE(property_changed_handler_
.is_null());
204 property_changed_handler_
.Run(signal
);
208 void ShillClientUnittestBase::ExpectPropertyChanged(
209 const std::string
& expected_name
,
210 const base::Value
* expected_value
,
211 const std::string
& name
,
212 const base::Value
& value
) {
213 EXPECT_EQ(expected_name
, name
);
214 EXPECT_TRUE(expected_value
->Equals(&value
));
218 void ShillClientUnittestBase::ExpectNoArgument(dbus::MessageReader
* reader
) {
219 EXPECT_FALSE(reader
->HasMoreData());
223 void ShillClientUnittestBase::ExpectUint32Argument(
224 uint32_t expected_value
,
225 dbus::MessageReader
* reader
) {
227 ASSERT_TRUE(reader
->PopUint32(&value
));
228 EXPECT_EQ(expected_value
, value
);
229 EXPECT_FALSE(reader
->HasMoreData());
233 void ShillClientUnittestBase::ExpectArrayOfBytesArgument(
234 const std::string
& expected_bytes
,
235 dbus::MessageReader
* reader
) {
236 const uint8_t* bytes
= nullptr;
238 ASSERT_TRUE(reader
->PopArrayOfBytes(&bytes
, &size
));
239 EXPECT_EQ(expected_bytes
.size(), size
);
240 for (size_t i
= 0; i
< size
; ++i
) {
241 EXPECT_EQ(expected_bytes
[i
], bytes
[i
]);
243 EXPECT_FALSE(reader
->HasMoreData());
247 void ShillClientUnittestBase::ExpectStringArgument(
248 const std::string
& expected_string
,
249 dbus::MessageReader
* reader
) {
251 ASSERT_TRUE(reader
->PopString(&str
));
252 EXPECT_EQ(expected_string
, str
);
253 EXPECT_FALSE(reader
->HasMoreData());
257 void ShillClientUnittestBase::ExpectArrayOfStringsArgument(
258 const std::vector
<std::string
>& expected_strings
,
259 dbus::MessageReader
* reader
) {
260 std::vector
<std::string
> strs
;
261 ASSERT_TRUE(reader
->PopArrayOfStrings(&strs
));
262 EXPECT_EQ(expected_strings
, strs
);
263 EXPECT_FALSE(reader
->HasMoreData());
267 void ShillClientUnittestBase::ExpectValueArgument(
268 const base::Value
* expected_value
,
269 dbus::MessageReader
* reader
) {
270 scoped_ptr
<base::Value
> value(dbus::PopDataAsValue(reader
));
271 ASSERT_TRUE(value
.get());
272 EXPECT_TRUE(value
->Equals(expected_value
));
273 EXPECT_FALSE(reader
->HasMoreData());
277 void ShillClientUnittestBase::ExpectStringAndValueArguments(
278 const std::string
& expected_string
,
279 const base::Value
* expected_value
,
280 dbus::MessageReader
* reader
) {
282 ASSERT_TRUE(reader
->PopString(&str
));
283 EXPECT_EQ(expected_string
, str
);
284 scoped_ptr
<base::Value
> value(dbus::PopDataAsValue(reader
));
285 ASSERT_TRUE(value
.get());
286 EXPECT_TRUE(value
->Equals(expected_value
));
287 EXPECT_FALSE(reader
->HasMoreData());
291 void ShillClientUnittestBase::ExpectDictionaryValueArgument(
292 const base::DictionaryValue
* expected_dictionary
,
294 dbus::MessageReader
* reader
) {
295 dbus::MessageReader
array_reader(NULL
);
296 ASSERT_TRUE(reader
->PopArray(&array_reader
));
297 while (array_reader
.HasMoreData()) {
298 dbus::MessageReader
entry_reader(NULL
);
299 ASSERT_TRUE(array_reader
.PopDictEntry(&entry_reader
));
301 ASSERT_TRUE(entry_reader
.PopString(&key
));
304 std::string expected_value
;
305 ASSERT_TRUE(entry_reader
.PopString(&value
));
306 EXPECT_TRUE(expected_dictionary
->GetStringWithoutPathExpansion(
307 key
, &expected_value
));
308 EXPECT_EQ(expected_value
, value
);
311 dbus::MessageReader
variant_reader(NULL
);
312 ASSERT_TRUE(entry_reader
.PopVariant(&variant_reader
));
313 scoped_ptr
<base::Value
> value
;
314 // Variants in the dictionary can be basic types or string-to-string
316 switch (variant_reader
.GetDataType()) {
317 case dbus::Message::ARRAY
:
318 value
.reset(PopStringToStringDictionary(&variant_reader
));
320 case dbus::Message::BOOL
:
321 case dbus::Message::INT32
:
322 case dbus::Message::STRING
:
323 value
.reset(dbus::PopDataAsValue(&variant_reader
));
328 ASSERT_TRUE(value
.get());
329 const base::Value
* expected_value
= NULL
;
330 EXPECT_TRUE(expected_dictionary
->GetWithoutPathExpansion(key
,
332 EXPECT_TRUE(value
->Equals(expected_value
));
337 base::DictionaryValue
*
338 ShillClientUnittestBase::CreateExampleServiceProperties() {
339 base::DictionaryValue
* properties
= new base::DictionaryValue
;
340 properties
->SetWithoutPathExpansion(
341 shill::kGuidProperty
,
342 new base::StringValue("00000000-0000-0000-0000-000000000000"));
343 properties
->SetWithoutPathExpansion(
344 shill::kModeProperty
, new base::StringValue(shill::kModeManaged
));
345 properties
->SetWithoutPathExpansion(shill::kTypeProperty
,
346 new base::StringValue(shill::kTypeWifi
));
347 shill_property_util::SetSSID("testssid", properties
);
348 properties
->SetWithoutPathExpansion(
349 shill::kSecurityClassProperty
,
350 new base::StringValue(shill::kSecurityPsk
));
356 void ShillClientUnittestBase::ExpectNoResultValue(
357 DBusMethodCallStatus call_status
) {
358 EXPECT_EQ(DBUS_METHOD_CALL_SUCCESS
, call_status
);
362 void ShillClientUnittestBase::ExpectObjectPathResult(
363 const dbus::ObjectPath
& expected_result
,
364 DBusMethodCallStatus call_status
,
365 const dbus::ObjectPath
& result
) {
366 EXPECT_EQ(DBUS_METHOD_CALL_SUCCESS
, call_status
);
367 EXPECT_EQ(expected_result
, result
);
371 void ShillClientUnittestBase::ExpectObjectPathResultWithoutStatus(
372 const dbus::ObjectPath
& expected_result
,
373 const dbus::ObjectPath
& result
) {
374 EXPECT_EQ(expected_result
, result
);
378 void ShillClientUnittestBase::ExpectBoolResultWithoutStatus(
379 bool expected_result
,
381 EXPECT_EQ(expected_result
, result
);
385 void ShillClientUnittestBase::ExpectStringResultWithoutStatus(
386 const std::string
& expected_result
,
387 const std::string
& result
) {
388 EXPECT_EQ(expected_result
, result
);
392 void ShillClientUnittestBase::ExpectDictionaryValueResultWithoutStatus(
393 const base::DictionaryValue
* expected_result
,
394 const base::DictionaryValue
& result
) {
395 std::string expected_result_string
;
396 base::JSONWriter::Write(*expected_result
, &expected_result_string
);
397 std::string result_string
;
398 base::JSONWriter::Write(result
, &result_string
);
399 EXPECT_EQ(expected_result_string
, result_string
);
403 void ShillClientUnittestBase::ExpectDictionaryValueResult(
404 const base::DictionaryValue
* expected_result
,
405 DBusMethodCallStatus call_status
,
406 const base::DictionaryValue
& result
) {
407 EXPECT_EQ(DBUS_METHOD_CALL_SUCCESS
, call_status
);
408 ExpectDictionaryValueResultWithoutStatus(expected_result
, result
);
411 void ShillClientUnittestBase::OnConnectToPlatformMessage(
412 const std::string
& interface_name
,
413 const std::string
& signal_name
,
414 const dbus::ObjectProxy::SignalCallback
& signal_callback
,
415 const dbus::ObjectProxy::OnConnectedCallback
& on_connected_callback
) {
416 platform_message_handler_
= signal_callback
;
417 const bool success
= true;
418 message_loop_
.PostTask(FROM_HERE
,
419 base::Bind(on_connected_callback
,
425 void ShillClientUnittestBase::OnConnectToPacketReceived(
426 const std::string
& interface_name
,
427 const std::string
& signal_name
,
428 const dbus::ObjectProxy::SignalCallback
& signal_callback
,
429 const dbus::ObjectProxy::OnConnectedCallback
& on_connected_callback
) {
430 packet_receieved__handler_
= signal_callback
;
431 const bool success
= true;
432 message_loop_
.PostTask(FROM_HERE
,
433 base::Bind(on_connected_callback
,
439 void ShillClientUnittestBase::OnConnectToPropertyChanged(
440 const std::string
& interface_name
,
441 const std::string
& signal_name
,
442 const dbus::ObjectProxy::SignalCallback
& signal_callback
,
443 const dbus::ObjectProxy::OnConnectedCallback
& on_connected_callback
) {
444 property_changed_handler_
= signal_callback
;
445 const bool success
= true;
446 message_loop_
.PostTask(FROM_HERE
,
447 base::Bind(on_connected_callback
,
453 void ShillClientUnittestBase::OnCallMethod(
454 dbus::MethodCall
* method_call
,
456 const dbus::ObjectProxy::ResponseCallback
& response_callback
) {
457 EXPECT_EQ(interface_name_
, method_call
->GetInterface());
458 EXPECT_EQ(expected_method_name_
, method_call
->GetMember());
459 dbus::MessageReader
reader(method_call
);
460 argument_checker_
.Run(&reader
);
461 message_loop_
.PostTask(FROM_HERE
,
462 base::Bind(response_callback
, response_
));
465 void ShillClientUnittestBase::OnCallMethodWithErrorCallback(
466 dbus::MethodCall
* method_call
,
468 const dbus::ObjectProxy::ResponseCallback
& response_callback
,
469 const dbus::ObjectProxy::ErrorCallback
& error_callback
) {
470 OnCallMethod(method_call
, timeout_ms
, response_callback
);
473 } // namespace chromeos