Adding Peter Thatcher to the owners file.
[chromium-blink-merge.git] / chromeos / dbus / shill_client_unittest_base.cc
blob184851a96bab7e6cf1ffd003e6978866f1912468
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"
7 #include "base/bind.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"
17 using ::testing::_;
18 using ::testing::Invoke;
19 using ::testing::Return;
21 namespace chromeos {
23 namespace {
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))
30 return NULL;
31 scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue);
32 while (array_reader.HasMoreData()) {
33 dbus::MessageReader entry_reader(NULL);
34 std::string key;
35 std::string value;
36 if (!array_reader.PopDictEntry(&entry_reader) ||
37 !entry_reader.PopString(&key) ||
38 !entry_reader.PopString(&value))
39 return NULL;
40 result->SetWithoutPathExpansion(key, new base::StringValue(value));
42 return result.release();
45 } // namespace
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_.get(),
58 base::JSONWriter::OPTIONS_PRETTY_PRINT,
59 &expected_value_str);
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_.get(),
66 base::JSONWriter::OPTIONS_PRETTY_PRINT,
67 &expected_value_str);
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),
112 response_(NULL) {
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(
126 mock_bus_.get(),
127 shill::kFlimflamServiceName,
128 object_path_);
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.
143 EXPECT_CALL(
144 *mock_proxy_.get(),
145 ConnectToSignal(interface_name_, shill::kMonitorPropertyChanged, _, _))
146 .WillRepeatedly(
147 Invoke(this, &ShillClientUnittestBase::OnConnectToPropertyChanged));
149 EXPECT_CALL(
150 *mock_proxy_.get(),
151 ConnectToSignal(interface_name_, shill::kOnPlatformMessageFunction, _, _))
152 .WillRepeatedly(
153 Invoke(this, &ShillClientUnittestBase::OnConnectToPlatformMessage));
155 EXPECT_CALL(
156 *mock_proxy_.get(),
157 ConnectToSignal(interface_name_, shill::kOnPacketReceivedFunction, _, _))
158 .WillRepeatedly(
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
168 // task runner.
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);
207 // static
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));
217 // static
218 void ShillClientUnittestBase::ExpectNoArgument(dbus::MessageReader* reader) {
219 EXPECT_FALSE(reader->HasMoreData());
222 // static
223 void ShillClientUnittestBase::ExpectUint32Argument(
224 uint32_t expected_value,
225 dbus::MessageReader* reader) {
226 uint32_t value;
227 ASSERT_TRUE(reader->PopUint32(&value));
228 EXPECT_EQ(expected_value, value);
229 EXPECT_FALSE(reader->HasMoreData());
232 // static
233 void ShillClientUnittestBase::ExpectArrayOfBytesArgument(
234 const std::string& expected_bytes,
235 dbus::MessageReader* reader) {
236 const uint8_t* bytes = nullptr;
237 size_t size = 0;
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());
246 // static
247 void ShillClientUnittestBase::ExpectStringArgument(
248 const std::string& expected_string,
249 dbus::MessageReader* reader) {
250 std::string str;
251 ASSERT_TRUE(reader->PopString(&str));
252 EXPECT_EQ(expected_string, str);
253 EXPECT_FALSE(reader->HasMoreData());
256 // static
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());
266 // static
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());
276 // static
277 void ShillClientUnittestBase::ExpectStringAndValueArguments(
278 const std::string& expected_string,
279 const base::Value* expected_value,
280 dbus::MessageReader* reader) {
281 std::string str;
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());
290 // static
291 void ShillClientUnittestBase::ExpectDictionaryValueArgument(
292 const base::DictionaryValue* expected_dictionary,
293 bool string_valued,
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));
300 std::string key;
301 ASSERT_TRUE(entry_reader.PopString(&key));
302 if (string_valued) {
303 std::string value;
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);
309 continue;
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
315 // dictinoary.
316 switch (variant_reader.GetDataType()) {
317 case dbus::Message::ARRAY:
318 value.reset(PopStringToStringDictionary(&variant_reader));
319 break;
320 case dbus::Message::BOOL:
321 case dbus::Message::INT32:
322 case dbus::Message::STRING:
323 value.reset(dbus::PopDataAsValue(&variant_reader));
324 break;
325 default:
326 NOTREACHED();
328 ASSERT_TRUE(value.get());
329 const base::Value* expected_value = NULL;
330 EXPECT_TRUE(expected_dictionary->GetWithoutPathExpansion(key,
331 &expected_value));
332 EXPECT_TRUE(value->Equals(expected_value));
336 // static
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));
351 return properties;
355 // static
356 void ShillClientUnittestBase::ExpectNoResultValue(
357 DBusMethodCallStatus call_status) {
358 EXPECT_EQ(DBUS_METHOD_CALL_SUCCESS, call_status);
361 // static
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);
370 // static
371 void ShillClientUnittestBase::ExpectObjectPathResultWithoutStatus(
372 const dbus::ObjectPath& expected_result,
373 const dbus::ObjectPath& result) {
374 EXPECT_EQ(expected_result, result);
377 // static
378 void ShillClientUnittestBase::ExpectBoolResultWithoutStatus(
379 bool expected_result,
380 bool result) {
381 EXPECT_EQ(expected_result, result);
384 // static
385 void ShillClientUnittestBase::ExpectStringResultWithoutStatus(
386 const std::string& expected_result,
387 const std::string& result) {
388 EXPECT_EQ(expected_result, result);
391 // static
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);
402 // static
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,
420 interface_name,
421 signal_name,
422 success));
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,
434 interface_name,
435 signal_name,
436 success));
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,
448 interface_name,
449 signal_name,
450 success));
453 void ShillClientUnittestBase::OnCallMethod(
454 dbus::MethodCall* method_call,
455 int timeout_ms,
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,
467 int timeout_ms,
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