| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chromeos/dbus/shill_client_unittest_base.h" | 5 #include "chromeos/dbus/shill_client_unittest_base.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
| 9 #include "base/values.h" | 9 #include "base/values.h" |
| 10 #include "chromeos/network/shill_property_util.h" | 10 #include "chromeos/network/shill_property_util.h" |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 EXPECT_CALL(*mock_proxy_.get(), CallMethod(_, _, _)) | 133 EXPECT_CALL(*mock_proxy_.get(), CallMethod(_, _, _)) |
| 134 .WillRepeatedly(Invoke(this, &ShillClientUnittestBase::OnCallMethod)); | 134 .WillRepeatedly(Invoke(this, &ShillClientUnittestBase::OnCallMethod)); |
| 135 | 135 |
| 136 // Set an expectation so mock_proxy's CallMethodWithErrorCallback() will use | 136 // Set an expectation so mock_proxy's CallMethodWithErrorCallback() will use |
| 137 // OnCallMethodWithErrorCallback() to return responses. | 137 // OnCallMethodWithErrorCallback() to return responses. |
| 138 EXPECT_CALL(*mock_proxy_.get(), CallMethodWithErrorCallback(_, _, _, _)) | 138 EXPECT_CALL(*mock_proxy_.get(), CallMethodWithErrorCallback(_, _, _, _)) |
| 139 .WillRepeatedly(Invoke( | 139 .WillRepeatedly(Invoke( |
| 140 this, &ShillClientUnittestBase::OnCallMethodWithErrorCallback)); | 140 this, &ShillClientUnittestBase::OnCallMethodWithErrorCallback)); |
| 141 | 141 |
| 142 // Set an expectation so mock_proxy's ConnectToSignal() will use | 142 // Set an expectation so mock_proxy's ConnectToSignal() will use |
| 143 // OnConnectToSignal() to run the callback. | 143 // OnConnectToPropertyChanged() to run the callback. |
| 144 EXPECT_CALL( | 144 EXPECT_CALL( |
| 145 *mock_proxy_.get(), | 145 *mock_proxy_.get(), |
| 146 ConnectToSignal(interface_name_, shill::kMonitorPropertyChanged, _, _)) | 146 ConnectToSignal(interface_name_, shill::kMonitorPropertyChanged, _, _)) |
| 147 .WillRepeatedly( | 147 .WillRepeatedly( |
| 148 Invoke(this, &ShillClientUnittestBase::OnConnectToSignal)); | 148 Invoke(this, &ShillClientUnittestBase::OnConnectToPropertyChanged)); |
| 149 |
| 150 EXPECT_CALL( |
| 151 *mock_proxy_.get(), |
| 152 ConnectToSignal(interface_name_, shill::kOnPlatformMessageFunction, _, _)) |
| 153 .WillRepeatedly( |
| 154 Invoke(this, &ShillClientUnittestBase::OnConnectToPlatformMessage)); |
| 155 |
| 156 EXPECT_CALL( |
| 157 *mock_proxy_.get(), |
| 158 ConnectToSignal(interface_name_, shill::kOnPacketReceivedFunction, _, _)) |
| 159 .WillRepeatedly( |
| 160 Invoke(this, &ShillClientUnittestBase::OnConnectToPacketReceived)); |
| 149 | 161 |
| 150 // Set an expectation so mock_bus's GetObjectProxy() for the given | 162 // Set an expectation so mock_bus's GetObjectProxy() for the given |
| 151 // service name and the object path will return mock_proxy_. | 163 // service name and the object path will return mock_proxy_. |
| 152 EXPECT_CALL(*mock_bus_.get(), | 164 EXPECT_CALL(*mock_bus_.get(), |
| 153 GetObjectProxy(shill::kFlimflamServiceName, object_path_)) | 165 GetObjectProxy(shill::kFlimflamServiceName, object_path_)) |
| 154 .WillOnce(Return(mock_proxy_.get())); | 166 .WillOnce(Return(mock_proxy_.get())); |
| 155 | 167 |
| 156 // Set an expectation so mock_bus's GetDBusTaskRunner will return the current | 168 // Set an expectation so mock_bus's GetDBusTaskRunner will return the current |
| 157 // task runner. | 169 // task runner. |
| 158 EXPECT_CALL(*mock_bus_.get(), GetDBusTaskRunner()) | 170 EXPECT_CALL(*mock_bus_.get(), GetDBusTaskRunner()) |
| 159 .WillRepeatedly(Return(message_loop_.message_loop_proxy().get())); | 171 .WillRepeatedly(Return(message_loop_.message_loop_proxy().get())); |
| 160 | 172 |
| 161 // ShutdownAndBlock() will be called in TearDown(). | 173 // ShutdownAndBlock() will be called in TearDown(). |
| 162 EXPECT_CALL(*mock_bus_.get(), ShutdownAndBlock()).WillOnce(Return()); | 174 EXPECT_CALL(*mock_bus_.get(), ShutdownAndBlock()).WillOnce(Return()); |
| 163 } | 175 } |
| 164 | 176 |
| 165 void ShillClientUnittestBase::TearDown() { | 177 void ShillClientUnittestBase::TearDown() { |
| 166 mock_bus_->ShutdownAndBlock(); | 178 mock_bus_->ShutdownAndBlock(); |
| 167 } | 179 } |
| 168 | 180 |
| 169 void ShillClientUnittestBase::PrepareForMethodCall( | 181 void ShillClientUnittestBase::PrepareForMethodCall( |
| 170 const std::string& method_name, | 182 const std::string& method_name, |
| 171 const ArgumentCheckCallback& argument_checker, | 183 const ArgumentCheckCallback& argument_checker, |
| 172 dbus::Response* response) { | 184 dbus::Response* response) { |
| 173 expected_method_name_ = method_name; | 185 expected_method_name_ = method_name; |
| 174 argument_checker_ = argument_checker; | 186 argument_checker_ = argument_checker; |
| 175 response_ = response; | 187 response_ = response; |
| 176 } | 188 } |
| 177 | 189 |
| 190 void ShillClientUnittestBase::SendPlatformMessageSignal( |
| 191 dbus::Signal* signal) { |
| 192 ASSERT_FALSE(platform_message_handler_.is_null()); |
| 193 platform_message_handler_.Run(signal); |
| 194 } |
| 195 |
| 196 void ShillClientUnittestBase::SendPacketReceievedSignal( |
| 197 dbus::Signal* signal) { |
| 198 ASSERT_FALSE(packet_receieved__handler_.is_null()); |
| 199 packet_receieved__handler_.Run(signal); |
| 200 } |
| 201 |
| 178 void ShillClientUnittestBase::SendPropertyChangedSignal( | 202 void ShillClientUnittestBase::SendPropertyChangedSignal( |
| 179 dbus::Signal* signal) { | 203 dbus::Signal* signal) { |
| 180 ASSERT_FALSE(property_changed_handler_.is_null()); | 204 ASSERT_FALSE(property_changed_handler_.is_null()); |
| 181 property_changed_handler_.Run(signal); | 205 property_changed_handler_.Run(signal); |
| 182 } | 206 } |
| 183 | 207 |
| 184 // static | 208 // static |
| 185 void ShillClientUnittestBase::ExpectPropertyChanged( | 209 void ShillClientUnittestBase::ExpectPropertyChanged( |
| 186 const std::string& expected_name, | 210 const std::string& expected_name, |
| 187 const base::Value* expected_value, | 211 const base::Value* expected_value, |
| 188 const std::string& name, | 212 const std::string& name, |
| 189 const base::Value& value) { | 213 const base::Value& value) { |
| 190 EXPECT_EQ(expected_name, name); | 214 EXPECT_EQ(expected_name, name); |
| 191 EXPECT_TRUE(expected_value->Equals(&value)); | 215 EXPECT_TRUE(expected_value->Equals(&value)); |
| 192 } | 216 } |
| 193 | 217 |
| 194 // static | 218 // static |
| 195 void ShillClientUnittestBase::ExpectNoArgument(dbus::MessageReader* reader) { | 219 void ShillClientUnittestBase::ExpectNoArgument(dbus::MessageReader* reader) { |
| 196 EXPECT_FALSE(reader->HasMoreData()); | 220 EXPECT_FALSE(reader->HasMoreData()); |
| 197 } | 221 } |
| 198 | 222 |
| 199 // static | 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()); |
| 231 } |
| 232 |
| 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]); |
| 243 } |
| 244 EXPECT_FALSE(reader->HasMoreData()); |
| 245 } |
| 246 |
| 247 // static |
| 200 void ShillClientUnittestBase::ExpectStringArgument( | 248 void ShillClientUnittestBase::ExpectStringArgument( |
| 201 const std::string& expected_string, | 249 const std::string& expected_string, |
| 202 dbus::MessageReader* reader) { | 250 dbus::MessageReader* reader) { |
| 203 std::string str; | 251 std::string str; |
| 204 ASSERT_TRUE(reader->PopString(&str)); | 252 ASSERT_TRUE(reader->PopString(&str)); |
| 205 EXPECT_EQ(expected_string, str); | 253 EXPECT_EQ(expected_string, str); |
| 206 EXPECT_FALSE(reader->HasMoreData()); | 254 EXPECT_FALSE(reader->HasMoreData()); |
| 207 } | 255 } |
| 208 | 256 |
| 209 // static | 257 // static |
| (...skipping 26 matching lines...) Expand all Loading... |
| 236 EXPECT_EQ(expected_string, str); | 284 EXPECT_EQ(expected_string, str); |
| 237 scoped_ptr<base::Value> value(dbus::PopDataAsValue(reader)); | 285 scoped_ptr<base::Value> value(dbus::PopDataAsValue(reader)); |
| 238 ASSERT_TRUE(value.get()); | 286 ASSERT_TRUE(value.get()); |
| 239 EXPECT_TRUE(value->Equals(expected_value)); | 287 EXPECT_TRUE(value->Equals(expected_value)); |
| 240 EXPECT_FALSE(reader->HasMoreData()); | 288 EXPECT_FALSE(reader->HasMoreData()); |
| 241 } | 289 } |
| 242 | 290 |
| 243 // static | 291 // static |
| 244 void ShillClientUnittestBase::ExpectDictionaryValueArgument( | 292 void ShillClientUnittestBase::ExpectDictionaryValueArgument( |
| 245 const base::DictionaryValue* expected_dictionary, | 293 const base::DictionaryValue* expected_dictionary, |
| 294 bool string_valued, |
| 246 dbus::MessageReader* reader) { | 295 dbus::MessageReader* reader) { |
| 247 dbus::MessageReader array_reader(NULL); | 296 dbus::MessageReader array_reader(NULL); |
| 248 ASSERT_TRUE(reader->PopArray(&array_reader)); | 297 ASSERT_TRUE(reader->PopArray(&array_reader)); |
| 249 while (array_reader.HasMoreData()) { | 298 while (array_reader.HasMoreData()) { |
| 250 dbus::MessageReader entry_reader(NULL); | 299 dbus::MessageReader entry_reader(NULL); |
| 251 ASSERT_TRUE(array_reader.PopDictEntry(&entry_reader)); | 300 ASSERT_TRUE(array_reader.PopDictEntry(&entry_reader)); |
| 252 std::string key; | 301 std::string key; |
| 253 ASSERT_TRUE(entry_reader.PopString(&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; |
| 311 } |
| 254 dbus::MessageReader variant_reader(NULL); | 312 dbus::MessageReader variant_reader(NULL); |
| 255 ASSERT_TRUE(entry_reader.PopVariant(&variant_reader)); | 313 ASSERT_TRUE(entry_reader.PopVariant(&variant_reader)); |
| 256 scoped_ptr<base::Value> value; | 314 scoped_ptr<base::Value> value; |
| 257 // Variants in the dictionary can be basic types or string-to-string | 315 // Variants in the dictionary can be basic types or string-to-string |
| 258 // dictinoary. | 316 // dictinoary. |
| 259 switch (variant_reader.GetDataType()) { | 317 switch (variant_reader.GetDataType()) { |
| 260 case dbus::Message::ARRAY: | 318 case dbus::Message::ARRAY: |
| 261 value.reset(PopStringToStringDictionary(&variant_reader)); | 319 value.reset(PopStringToStringDictionary(&variant_reader)); |
| 262 break; | 320 break; |
| 263 case dbus::Message::BOOL: | 321 case dbus::Message::BOOL: |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 343 | 401 |
| 344 // static | 402 // static |
| 345 void ShillClientUnittestBase::ExpectDictionaryValueResult( | 403 void ShillClientUnittestBase::ExpectDictionaryValueResult( |
| 346 const base::DictionaryValue* expected_result, | 404 const base::DictionaryValue* expected_result, |
| 347 DBusMethodCallStatus call_status, | 405 DBusMethodCallStatus call_status, |
| 348 const base::DictionaryValue& result) { | 406 const base::DictionaryValue& result) { |
| 349 EXPECT_EQ(DBUS_METHOD_CALL_SUCCESS, call_status); | 407 EXPECT_EQ(DBUS_METHOD_CALL_SUCCESS, call_status); |
| 350 ExpectDictionaryValueResultWithoutStatus(expected_result, result); | 408 ExpectDictionaryValueResultWithoutStatus(expected_result, result); |
| 351 } | 409 } |
| 352 | 410 |
| 353 void ShillClientUnittestBase::OnConnectToSignal( | 411 void ShillClientUnittestBase::OnConnectToPlatformMessage( |
| 354 const std::string& interface_name, | 412 const std::string& interface_name, |
| 355 const std::string& signal_name, | 413 const std::string& signal_name, |
| 356 const dbus::ObjectProxy::SignalCallback& signal_callback, | 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)); |
| 423 } |
| 424 |
| 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)); |
| 437 } |
| 438 |
| 439 void ShillClientUnittestBase::OnConnectToPropertyChanged( |
| 440 const std::string& interface_name, |
| 441 const std::string& signal_name, |
| 442 const dbus::ObjectProxy::SignalCallback& signal_callback, |
| 357 const dbus::ObjectProxy::OnConnectedCallback& on_connected_callback) { | 443 const dbus::ObjectProxy::OnConnectedCallback& on_connected_callback) { |
| 358 property_changed_handler_ = signal_callback; | 444 property_changed_handler_ = signal_callback; |
| 359 const bool success = true; | 445 const bool success = true; |
| 360 message_loop_.PostTask(FROM_HERE, | 446 message_loop_.PostTask(FROM_HERE, |
| 361 base::Bind(on_connected_callback, | 447 base::Bind(on_connected_callback, |
| 362 interface_name, | 448 interface_name, |
| 363 signal_name, | 449 signal_name, |
| 364 success)); | 450 success)); |
| 365 } | 451 } |
| 366 | 452 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 378 | 464 |
| 379 void ShillClientUnittestBase::OnCallMethodWithErrorCallback( | 465 void ShillClientUnittestBase::OnCallMethodWithErrorCallback( |
| 380 dbus::MethodCall* method_call, | 466 dbus::MethodCall* method_call, |
| 381 int timeout_ms, | 467 int timeout_ms, |
| 382 const dbus::ObjectProxy::ResponseCallback& response_callback, | 468 const dbus::ObjectProxy::ResponseCallback& response_callback, |
| 383 const dbus::ObjectProxy::ErrorCallback& error_callback) { | 469 const dbus::ObjectProxy::ErrorCallback& error_callback) { |
| 384 OnCallMethod(method_call, timeout_ms, response_callback); | 470 OnCallMethod(method_call, timeout_ms, response_callback); |
| 385 } | 471 } |
| 386 | 472 |
| 387 } // namespace chromeos | 473 } // namespace chromeos |
| OLD | NEW |