Always send enabled state in extension update checks
[chromium-blink-merge.git] / chromeos / dbus / shill_client_unittest_base.cc
blobbba6f958d7ca6ece72c3e48c9ce702dc44108cbc
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 "testing/gtest/include/gtest/gtest.h"
16 #include "third_party/cros_system_api/dbus/service_constants.h"
18 using ::testing::_;
19 using ::testing::Invoke;
20 using ::testing::Return;
22 namespace chromeos {
24 namespace {
26 // Pops a string-to-string dictionary from the reader.
27 base::DictionaryValue* PopStringToStringDictionary(
28 dbus::MessageReader* reader) {
29 dbus::MessageReader array_reader(NULL);
30 if (!reader->PopArray(&array_reader))
31 return NULL;
32 scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue);
33 while (array_reader.HasMoreData()) {
34 dbus::MessageReader entry_reader(NULL);
35 std::string key;
36 std::string value;
37 if (!array_reader.PopDictEntry(&entry_reader) ||
38 !entry_reader.PopString(&key) ||
39 !entry_reader.PopString(&value))
40 return NULL;
41 result->SetWithoutPathExpansion(key, new base::StringValue(value));
43 return result.release();
46 } // namespace
48 ValueMatcher::ValueMatcher(const base::Value& value)
49 : expected_value_(value.DeepCopy()) {}
51 bool ValueMatcher::MatchAndExplain(const base::Value& value,
52 MatchResultListener* listener) const {
53 return expected_value_->Equals(&value);
56 void ValueMatcher::DescribeTo(::std::ostream* os) const {
57 std::string expected_value_str;
58 base::JSONWriter::WriteWithOptions(expected_value_.get(),
59 base::JSONWriter::OPTIONS_PRETTY_PRINT,
60 &expected_value_str);
61 *os << "value equals " << expected_value_str;
64 void ValueMatcher::DescribeNegationTo(::std::ostream* os) const {
65 std::string expected_value_str;
66 base::JSONWriter::WriteWithOptions(expected_value_.get(),
67 base::JSONWriter::OPTIONS_PRETTY_PRINT,
68 &expected_value_str);
69 *os << "value does not equal " << expected_value_str;
73 ShillClientUnittestBase::MockClosure::MockClosure() {}
75 ShillClientUnittestBase::MockClosure::~MockClosure() {}
77 base::Closure ShillClientUnittestBase::MockClosure::GetCallback() {
78 return base::Bind(&MockClosure::Run, base::Unretained(this));
82 ShillClientUnittestBase::MockListValueCallback::MockListValueCallback() {}
84 ShillClientUnittestBase::MockListValueCallback::~MockListValueCallback() {}
86 ShillClientHelper::ListValueCallback
87 ShillClientUnittestBase::MockListValueCallback::GetCallback() {
88 return base::Bind(&MockListValueCallback::Run, base::Unretained(this));
92 ShillClientUnittestBase::MockErrorCallback::MockErrorCallback() {}
94 ShillClientUnittestBase::MockErrorCallback::~MockErrorCallback() {}
96 ShillClientUnittestBase::MockPropertyChangeObserver::
97 MockPropertyChangeObserver() {}
99 ShillClientUnittestBase::MockPropertyChangeObserver::
100 ~MockPropertyChangeObserver() {}
102 ShillClientHelper::ErrorCallback
103 ShillClientUnittestBase::MockErrorCallback::GetCallback() {
104 return base::Bind(&MockErrorCallback::Run, base::Unretained(this));
108 ShillClientUnittestBase::ShillClientUnittestBase(
109 const std::string& interface_name,
110 const dbus::ObjectPath& object_path)
111 : interface_name_(interface_name),
112 object_path_(object_path),
113 response_(NULL) {
116 ShillClientUnittestBase::~ShillClientUnittestBase() {
119 void ShillClientUnittestBase::SetUp() {
120 // Create a mock bus.
121 dbus::Bus::Options options;
122 options.bus_type = dbus::Bus::SYSTEM;
123 mock_bus_ = new dbus::MockBus(options);
125 // Create a mock proxy.
126 mock_proxy_ = new dbus::MockObjectProxy(
127 mock_bus_.get(),
128 shill::kFlimflamServiceName,
129 object_path_);
131 // Set an expectation so mock_proxy's CallMethod() will use OnCallMethod()
132 // to return responses.
133 EXPECT_CALL(*mock_proxy_.get(), CallMethod(_, _, _))
134 .WillRepeatedly(Invoke(this, &ShillClientUnittestBase::OnCallMethod));
136 // Set an expectation so mock_proxy's CallMethodWithErrorCallback() will use
137 // OnCallMethodWithErrorCallback() to return responses.
138 EXPECT_CALL(*mock_proxy_.get(), CallMethodWithErrorCallback(_, _, _, _))
139 .WillRepeatedly(Invoke(
140 this, &ShillClientUnittestBase::OnCallMethodWithErrorCallback));
142 // Set an expectation so mock_proxy's ConnectToSignal() will use
143 // OnConnectToPropertyChanged() to run the callback.
144 EXPECT_CALL(
145 *mock_proxy_.get(),
146 ConnectToSignal(interface_name_, shill::kMonitorPropertyChanged, _, _))
147 .WillRepeatedly(
148 Invoke(this, &ShillClientUnittestBase::OnConnectToPropertyChanged));
150 EXPECT_CALL(
151 *mock_proxy_.get(),
152 ConnectToSignal(interface_name_, shill::kOnPlatformMessageFunction, _, _))
153 .WillRepeatedly(
154 Invoke(this, &ShillClientUnittestBase::OnConnectToPlatformMessage));
156 EXPECT_CALL(
157 *mock_proxy_.get(),
158 ConnectToSignal(interface_name_, shill::kOnPacketReceivedFunction, _, _))
159 .WillRepeatedly(
160 Invoke(this, &ShillClientUnittestBase::OnConnectToPacketReceived));
162 // Set an expectation so mock_bus's GetObjectProxy() for the given
163 // service name and the object path will return mock_proxy_.
164 EXPECT_CALL(*mock_bus_.get(),
165 GetObjectProxy(shill::kFlimflamServiceName, object_path_))
166 .WillOnce(Return(mock_proxy_.get()));
168 // Set an expectation so mock_bus's GetDBusTaskRunner will return the current
169 // task runner.
170 EXPECT_CALL(*mock_bus_.get(), GetDBusTaskRunner())
171 .WillRepeatedly(Return(message_loop_.message_loop_proxy().get()));
173 // ShutdownAndBlock() will be called in TearDown().
174 EXPECT_CALL(*mock_bus_.get(), ShutdownAndBlock()).WillOnce(Return());
177 void ShillClientUnittestBase::TearDown() {
178 mock_bus_->ShutdownAndBlock();
181 void ShillClientUnittestBase::PrepareForMethodCall(
182 const std::string& method_name,
183 const ArgumentCheckCallback& argument_checker,
184 dbus::Response* response) {
185 expected_method_name_ = method_name;
186 argument_checker_ = argument_checker;
187 response_ = response;
190 void ShillClientUnittestBase::SendPlatformMessageSignal(
191 dbus::Signal* signal) {
192 ASSERT_FALSE(platform_message_handler_.is_null());
193 platform_message_handler_.Run(signal);
196 void ShillClientUnittestBase::SendPacketReceievedSignal(
197 dbus::Signal* signal) {
198 ASSERT_FALSE(packet_receieved__handler_.is_null());
199 packet_receieved__handler_.Run(signal);
202 void ShillClientUnittestBase::SendPropertyChangedSignal(
203 dbus::Signal* signal) {
204 ASSERT_FALSE(property_changed_handler_.is_null());
205 property_changed_handler_.Run(signal);
208 // static
209 void ShillClientUnittestBase::ExpectPropertyChanged(
210 const std::string& expected_name,
211 const base::Value* expected_value,
212 const std::string& name,
213 const base::Value& value) {
214 EXPECT_EQ(expected_name, name);
215 EXPECT_TRUE(expected_value->Equals(&value));
218 // static
219 void ShillClientUnittestBase::ExpectNoArgument(dbus::MessageReader* reader) {
220 EXPECT_FALSE(reader->HasMoreData());
223 // static
224 void ShillClientUnittestBase::ExpectUint32Argument(
225 uint32_t expected_value,
226 dbus::MessageReader* reader) {
227 uint32_t value;
228 ASSERT_TRUE(reader->PopUint32(&value));
229 EXPECT_EQ(expected_value, value);
230 EXPECT_FALSE(reader->HasMoreData());
233 // static
234 void ShillClientUnittestBase::ExpectArrayOfBytesArgument(
235 const std::string& expected_bytes,
236 dbus::MessageReader* reader) {
237 const uint8_t* bytes = nullptr;
238 size_t size = 0;
239 ASSERT_TRUE(reader->PopArrayOfBytes(&bytes, &size));
240 EXPECT_EQ(expected_bytes.size(), size);
241 for (size_t i = 0; i < size; ++i) {
242 EXPECT_EQ(expected_bytes[i], bytes[i]);
244 EXPECT_FALSE(reader->HasMoreData());
247 // static
248 void ShillClientUnittestBase::ExpectStringArgument(
249 const std::string& expected_string,
250 dbus::MessageReader* reader) {
251 std::string str;
252 ASSERT_TRUE(reader->PopString(&str));
253 EXPECT_EQ(expected_string, str);
254 EXPECT_FALSE(reader->HasMoreData());
257 // static
258 void ShillClientUnittestBase::ExpectArrayOfStringsArgument(
259 const std::vector<std::string>& expected_strings,
260 dbus::MessageReader* reader) {
261 std::vector<std::string> strs;
262 ASSERT_TRUE(reader->PopArrayOfStrings(&strs));
263 EXPECT_EQ(expected_strings, strs);
264 EXPECT_FALSE(reader->HasMoreData());
267 // static
268 void ShillClientUnittestBase::ExpectValueArgument(
269 const base::Value* expected_value,
270 dbus::MessageReader* reader) {
271 scoped_ptr<base::Value> value(dbus::PopDataAsValue(reader));
272 ASSERT_TRUE(value.get());
273 EXPECT_TRUE(value->Equals(expected_value));
274 EXPECT_FALSE(reader->HasMoreData());
277 // static
278 void ShillClientUnittestBase::ExpectStringAndValueArguments(
279 const std::string& expected_string,
280 const base::Value* expected_value,
281 dbus::MessageReader* reader) {
282 std::string str;
283 ASSERT_TRUE(reader->PopString(&str));
284 EXPECT_EQ(expected_string, str);
285 scoped_ptr<base::Value> value(dbus::PopDataAsValue(reader));
286 ASSERT_TRUE(value.get());
287 EXPECT_TRUE(value->Equals(expected_value));
288 EXPECT_FALSE(reader->HasMoreData());
291 // static
292 void ShillClientUnittestBase::ExpectDictionaryValueArgument(
293 const base::DictionaryValue* expected_dictionary,
294 bool string_valued,
295 dbus::MessageReader* reader) {
296 dbus::MessageReader array_reader(NULL);
297 ASSERT_TRUE(reader->PopArray(&array_reader));
298 while (array_reader.HasMoreData()) {
299 dbus::MessageReader entry_reader(NULL);
300 ASSERT_TRUE(array_reader.PopDictEntry(&entry_reader));
301 std::string key;
302 ASSERT_TRUE(entry_reader.PopString(&key));
303 if (string_valued) {
304 std::string value;
305 std::string expected_value;
306 ASSERT_TRUE(entry_reader.PopString(&value));
307 EXPECT_TRUE(expected_dictionary->GetStringWithoutPathExpansion(
308 key, &expected_value));
309 EXPECT_EQ(expected_value, value);
310 continue;
312 dbus::MessageReader variant_reader(NULL);
313 ASSERT_TRUE(entry_reader.PopVariant(&variant_reader));
314 scoped_ptr<base::Value> value;
315 // Variants in the dictionary can be basic types or string-to-string
316 // dictinoary.
317 switch (variant_reader.GetDataType()) {
318 case dbus::Message::ARRAY:
319 value.reset(PopStringToStringDictionary(&variant_reader));
320 break;
321 case dbus::Message::BOOL:
322 case dbus::Message::INT32:
323 case dbus::Message::STRING:
324 value.reset(dbus::PopDataAsValue(&variant_reader));
325 break;
326 default:
327 NOTREACHED();
329 ASSERT_TRUE(value.get());
330 const base::Value* expected_value = NULL;
331 EXPECT_TRUE(expected_dictionary->GetWithoutPathExpansion(key,
332 &expected_value));
333 EXPECT_TRUE(value->Equals(expected_value));
337 // static
338 base::DictionaryValue*
339 ShillClientUnittestBase::CreateExampleServiceProperties() {
340 base::DictionaryValue* properties = new base::DictionaryValue;
341 properties->SetWithoutPathExpansion(
342 shill::kGuidProperty,
343 new base::StringValue("00000000-0000-0000-0000-000000000000"));
344 properties->SetWithoutPathExpansion(
345 shill::kModeProperty, new base::StringValue(shill::kModeManaged));
346 properties->SetWithoutPathExpansion(shill::kTypeProperty,
347 new base::StringValue(shill::kTypeWifi));
348 shill_property_util::SetSSID("testssid", properties);
349 properties->SetWithoutPathExpansion(
350 shill::kSecurityProperty, 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