shill_client_unittest_base.cc revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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. 4 5#include "chromeos/dbus/shill_client_unittest_base.h" 6 7#include "base/bind.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" 16 17using ::testing::_; 18using ::testing::Invoke; 19using ::testing::Return; 20 21namespace chromeos { 22 23namespace { 24 25// Runs the given task. This function is used to implement the mock bus. 26void RunTask(const tracked_objects::Location& from_here, 27 const base::Closure& task) { 28 task.Run(); 29} 30 31} // namespace 32 33ValueMatcher::ValueMatcher(const base::Value& value) 34 : expected_value_(value.DeepCopy()) {} 35 36bool ValueMatcher::MatchAndExplain(const base::Value& value, 37 MatchResultListener* listener) const { 38 return expected_value_->Equals(&value); 39} 40 41void 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, 45 &expected_value_str); 46 *os << "value equals " << expected_value_str; 47} 48 49void 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, 53 &expected_value_str); 54 *os << "value does not equal " << expected_value_str; 55} 56 57 58ShillClientUnittestBase::MockClosure::MockClosure() {} 59 60ShillClientUnittestBase::MockClosure::~MockClosure() {} 61 62base::Closure ShillClientUnittestBase::MockClosure::GetCallback() { 63 return base::Bind(&MockClosure::Run, base::Unretained(this)); 64} 65 66 67ShillClientUnittestBase::MockListValueCallback::MockListValueCallback() {} 68 69ShillClientUnittestBase::MockListValueCallback::~MockListValueCallback() {} 70 71ShillClientHelper::ListValueCallback 72ShillClientUnittestBase::MockListValueCallback::GetCallback() { 73 return base::Bind(&MockListValueCallback::Run, base::Unretained(this)); 74} 75 76 77ShillClientUnittestBase::MockErrorCallback::MockErrorCallback() {} 78 79ShillClientUnittestBase::MockErrorCallback::~MockErrorCallback() {} 80 81ShillClientUnittestBase::MockPropertyChangeObserver:: 82 MockPropertyChangeObserver() {} 83 84ShillClientUnittestBase::MockPropertyChangeObserver:: 85 ~MockPropertyChangeObserver() {} 86 87ShillClientHelper::ErrorCallback 88ShillClientUnittestBase::MockErrorCallback::GetCallback() { 89 return base::Bind(&MockErrorCallback::Run, base::Unretained(this)); 90} 91 92 93ShillClientUnittestBase::ShillClientUnittestBase( 94 const std::string& interface_name, 95 const dbus::ObjectPath& object_path) 96 : interface_name_(interface_name), 97 object_path_(object_path), 98 response_(NULL) { 99} 100 101ShillClientUnittestBase::~ShillClientUnittestBase() { 102} 103 104void 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); 109 110 // Create a mock proxy. 111 mock_proxy_ = new dbus::MockObjectProxy( 112 mock_bus_.get(), 113 flimflam::kFlimflamServiceName, 114 object_path_); 115 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)); 121 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)); 126 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)); 132 133 // Set an expectation so mock_proxy's ConnectToSignal() will use 134 // OnConnectToSignal() to run the callback. 135 EXPECT_CALL(*mock_proxy_, ConnectToSignal( 136 interface_name_, 137 flimflam::kMonitorPropertyChanged, _, _)) 138 .WillRepeatedly(Invoke(this, 139 &ShillClientUnittestBase::OnConnectToSignal)); 140 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, 144 object_path_)) 145 .WillOnce(Return(mock_proxy_.get())); 146 147 // Set an expectation so mock_bus's PostTaskToDBusThread() will run the 148 // given task. 149 EXPECT_CALL(*mock_bus_, PostTaskToDBusThread(_, _)) 150 .WillRepeatedly(Invoke(&RunTask)); 151 152 // ShutdownAndBlock() will be called in TearDown(). 153 EXPECT_CALL(*mock_bus_, ShutdownAndBlock()).WillOnce(Return()); 154} 155 156void ShillClientUnittestBase::TearDown() { 157 mock_bus_->ShutdownAndBlock(); 158} 159 160void 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; 167} 168 169void ShillClientUnittestBase::SendPropertyChangedSignal( 170 dbus::Signal* signal) { 171 ASSERT_FALSE(property_changed_handler_.is_null()); 172 property_changed_handler_.Run(signal); 173} 174 175// static 176void 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)); 183} 184 185// static 186void ShillClientUnittestBase::ExpectNoArgument(dbus::MessageReader* reader) { 187 EXPECT_FALSE(reader->HasMoreData()); 188} 189 190// static 191void ShillClientUnittestBase::ExpectStringArgument( 192 const std::string& expected_string, 193 dbus::MessageReader* reader) { 194 std::string str; 195 ASSERT_TRUE(reader->PopString(&str)); 196 EXPECT_EQ(expected_string, str); 197 EXPECT_FALSE(reader->HasMoreData()); 198} 199 200// static 201void 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()); 208} 209 210// static 211void 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()); 218} 219 220// static 221void ShillClientUnittestBase::ExpectStringAndValueArguments( 222 const std::string& expected_string, 223 const base::Value* expected_value, 224 dbus::MessageReader* reader) { 225 std::string str; 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()); 232} 233 234// static 235void ShillClientUnittestBase::ExpectNoResultValue( 236 DBusMethodCallStatus call_status) { 237 EXPECT_EQ(DBUS_METHOD_CALL_SUCCESS, call_status); 238} 239 240// static 241void 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); 247} 248 249// static 250void ShillClientUnittestBase::ExpectObjectPathResultWithoutStatus( 251 const dbus::ObjectPath& expected_result, 252 const dbus::ObjectPath& result) { 253 EXPECT_EQ(expected_result, result); 254} 255 256// static 257void ShillClientUnittestBase::ExpectBoolResultWithoutStatus( 258 bool expected_result, 259 bool result) { 260 EXPECT_EQ(expected_result, result); 261} 262 263// static 264void ShillClientUnittestBase::ExpectStringResultWithoutStatus( 265 const std::string& expected_result, 266 const std::string& result) { 267 EXPECT_EQ(expected_result, result); 268} 269 270// static 271void 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); 279} 280 281// static 282void 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); 288} 289 290void 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, 299 interface_name, 300 signal_name, 301 success)); 302} 303 304void ShillClientUnittestBase::OnCallMethod( 305 dbus::MethodCall* method_call, 306 int timeout_ms, 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_)); 314} 315 316void ShillClientUnittestBase::OnCallMethodWithErrorCallback( 317 dbus::MethodCall* method_call, 318 int timeout_ms, 319 const dbus::ObjectProxy::ResponseCallback& response_callback, 320 const dbus::ObjectProxy::ErrorCallback& error_callback) { 321 OnCallMethod(method_call, timeout_ms, response_callback); 322} 323 324dbus::Response* ShillClientUnittestBase::OnCallMethodAndBlock( 325 dbus::MethodCall* method_call, 326 int timeout_ms) { 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(); 333} 334 335} // namespace chromeos 336