| OLD | NEW |
| (Empty) |
| 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 "base/bind.h" | |
| 6 #include "base/values.h" | |
| 7 #include "chromeos/dbus/flimflam_client_unittest_base.h" | |
| 8 #include "chromeos/dbus/flimflam_device_client.h" | |
| 9 #include "dbus/message.h" | |
| 10 #include "dbus/object_path.h" | |
| 11 #include "dbus/values_util.h" | |
| 12 #include "testing/gtest/include/gtest/gtest.h" | |
| 13 #include "third_party/cros_system_api/dbus/service_constants.h" | |
| 14 | |
| 15 namespace chromeos { | |
| 16 | |
| 17 namespace { | |
| 18 | |
| 19 const char kExampleDevicePath[] = "/foo/bar"; | |
| 20 | |
| 21 // Expects the reader to have a string and a bool. | |
| 22 void ExpectStringAndBoolArguments(const std::string& expected_string, | |
| 23 bool expected_bool, | |
| 24 dbus::MessageReader* reader) { | |
| 25 std::string arg1; | |
| 26 ASSERT_TRUE(reader->PopString(&arg1)); | |
| 27 EXPECT_EQ(expected_string, arg1); | |
| 28 bool arg2 = false; | |
| 29 ASSERT_TRUE(reader->PopBool(&arg2)); | |
| 30 EXPECT_EQ(expected_bool, arg2); | |
| 31 EXPECT_FALSE(reader->HasMoreData()); | |
| 32 } | |
| 33 | |
| 34 // Expects the reader to have two strings. | |
| 35 void ExpectTwoStringArguments(const std::string& expected_string1, | |
| 36 const std::string& expected_string2, | |
| 37 dbus::MessageReader* reader) { | |
| 38 std::string arg1; | |
| 39 ASSERT_TRUE(reader->PopString(&arg1)); | |
| 40 EXPECT_EQ(expected_string1, arg1); | |
| 41 std::string arg2; | |
| 42 ASSERT_TRUE(reader->PopString(&arg2)); | |
| 43 EXPECT_EQ(expected_string2, arg2); | |
| 44 EXPECT_FALSE(reader->HasMoreData()); | |
| 45 } | |
| 46 | |
| 47 } // namespace | |
| 48 | |
| 49 class FlimflamDeviceClientTest : public FlimflamClientUnittestBase { | |
| 50 public: | |
| 51 FlimflamDeviceClientTest() | |
| 52 : FlimflamClientUnittestBase(flimflam::kFlimflamDeviceInterface, | |
| 53 dbus::ObjectPath(kExampleDevicePath)) { | |
| 54 } | |
| 55 | |
| 56 virtual void SetUp() { | |
| 57 FlimflamClientUnittestBase::SetUp(); | |
| 58 // Create a client with the mock bus. | |
| 59 client_.reset(FlimflamDeviceClient::Create(REAL_DBUS_CLIENT_IMPLEMENTATION, | |
| 60 mock_bus_)); | |
| 61 // Run the message loop to run the signal connection result callback. | |
| 62 message_loop_.RunAllPending(); | |
| 63 } | |
| 64 | |
| 65 virtual void TearDown() { | |
| 66 FlimflamClientUnittestBase::TearDown(); | |
| 67 } | |
| 68 | |
| 69 protected: | |
| 70 scoped_ptr<FlimflamDeviceClient> client_; | |
| 71 }; | |
| 72 | |
| 73 TEST_F(FlimflamDeviceClientTest, PropertyChanged) { | |
| 74 const bool kValue = true; | |
| 75 // Create a signal. | |
| 76 dbus::Signal signal(flimflam::kFlimflamDeviceInterface, | |
| 77 flimflam::kMonitorPropertyChanged); | |
| 78 dbus::MessageWriter writer(&signal); | |
| 79 writer.AppendString(flimflam::kCellularAllowRoamingProperty); | |
| 80 writer.AppendVariantOfBool(kValue); | |
| 81 | |
| 82 // Set expectations. | |
| 83 const base::FundamentalValue value(kValue); | |
| 84 client_->SetPropertyChangedHandler( | |
| 85 dbus::ObjectPath(kExampleDevicePath), | |
| 86 base::Bind(&ExpectPropertyChanged, | |
| 87 flimflam::kCellularAllowRoamingProperty, | |
| 88 &value)); | |
| 89 // Run the signal callback. | |
| 90 SendPropertyChangedSignal(&signal); | |
| 91 | |
| 92 // Reset the handler. | |
| 93 client_->ResetPropertyChangedHandler(dbus::ObjectPath(kExampleDevicePath)); | |
| 94 } | |
| 95 | |
| 96 TEST_F(FlimflamDeviceClientTest, GetProperties) { | |
| 97 const bool kValue = true; | |
| 98 // Create response. | |
| 99 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); | |
| 100 dbus::MessageWriter writer(response.get()); | |
| 101 dbus::MessageWriter array_writer(NULL); | |
| 102 writer.OpenArray("{sv}", &array_writer); | |
| 103 dbus::MessageWriter entry_writer(NULL); | |
| 104 array_writer.OpenDictEntry(&entry_writer); | |
| 105 entry_writer.AppendString(flimflam::kCellularAllowRoamingProperty); | |
| 106 entry_writer.AppendVariantOfBool(kValue); | |
| 107 array_writer.CloseContainer(&entry_writer); | |
| 108 writer.CloseContainer(&array_writer); | |
| 109 | |
| 110 // Set expectations. | |
| 111 base::DictionaryValue value; | |
| 112 value.SetWithoutPathExpansion(flimflam::kCellularAllowRoamingProperty, | |
| 113 base::Value::CreateBooleanValue(kValue)); | |
| 114 PrepareForMethodCall(flimflam::kGetPropertiesFunction, | |
| 115 base::Bind(&ExpectNoArgument), | |
| 116 response.get()); | |
| 117 // Call method. | |
| 118 client_->GetProperties(dbus::ObjectPath(kExampleDevicePath), | |
| 119 base::Bind(&ExpectDictionaryValueResult, &value)); | |
| 120 // Run the message loop. | |
| 121 message_loop_.RunAllPending(); | |
| 122 } | |
| 123 | |
| 124 TEST_F(FlimflamDeviceClientTest, CallGetPropertiesAndBlock) { | |
| 125 const bool kValue = true; | |
| 126 // Create response. | |
| 127 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); | |
| 128 dbus::MessageWriter writer(response.get()); | |
| 129 dbus::MessageWriter array_writer(NULL); | |
| 130 writer.OpenArray("{sv}", &array_writer); | |
| 131 dbus::MessageWriter entry_writer(NULL); | |
| 132 array_writer.OpenDictEntry(&entry_writer); | |
| 133 entry_writer.AppendString(flimflam::kCellularAllowRoamingProperty); | |
| 134 entry_writer.AppendVariantOfBool(kValue); | |
| 135 array_writer.CloseContainer(&entry_writer); | |
| 136 writer.CloseContainer(&array_writer); | |
| 137 | |
| 138 // Set expectations. | |
| 139 base::DictionaryValue value; | |
| 140 value.SetWithoutPathExpansion(flimflam::kCellularAllowRoamingProperty, | |
| 141 base::Value::CreateBooleanValue(kValue)); | |
| 142 PrepareForMethodCall(flimflam::kGetPropertiesFunction, | |
| 143 base::Bind(&ExpectNoArgument), | |
| 144 response.get()); | |
| 145 // Call method. | |
| 146 scoped_ptr<base::DictionaryValue> result( | |
| 147 client_->CallGetPropertiesAndBlock(dbus::ObjectPath(kExampleDevicePath))); | |
| 148 ASSERT_TRUE(result.get()); | |
| 149 EXPECT_TRUE(result->Equals(&value)); | |
| 150 } | |
| 151 | |
| 152 TEST_F(FlimflamDeviceClientTest, ProposeScan) { | |
| 153 // Create response. | |
| 154 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); | |
| 155 | |
| 156 // Set expectations. | |
| 157 PrepareForMethodCall(flimflam::kProposeScanFunction, | |
| 158 base::Bind(&ExpectNoArgument), | |
| 159 response.get()); | |
| 160 // Call method. | |
| 161 client_->ProposeScan(dbus::ObjectPath(kExampleDevicePath), | |
| 162 base::Bind(&ExpectNoResultValue)); | |
| 163 // Run the message loop. | |
| 164 message_loop_.RunAllPending(); | |
| 165 } | |
| 166 | |
| 167 TEST_F(FlimflamDeviceClientTest, SetProperty) { | |
| 168 const bool kValue = true; | |
| 169 // Create response. | |
| 170 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); | |
| 171 | |
| 172 // Set expectations. | |
| 173 const base::FundamentalValue value(kValue); | |
| 174 PrepareForMethodCall(flimflam::kSetPropertyFunction, | |
| 175 base::Bind(&ExpectStringAndValueArguments, | |
| 176 flimflam::kCellularAllowRoamingProperty, | |
| 177 &value), | |
| 178 response.get()); | |
| 179 // Call method. | |
| 180 client_->SetProperty(dbus::ObjectPath(kExampleDevicePath), | |
| 181 flimflam::kCellularAllowRoamingProperty, | |
| 182 value, | |
| 183 base::Bind(&ExpectNoResultValue)); | |
| 184 // Run the message loop. | |
| 185 message_loop_.RunAllPending(); | |
| 186 } | |
| 187 | |
| 188 TEST_F(FlimflamDeviceClientTest, ClearProperty) { | |
| 189 // Create response. | |
| 190 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); | |
| 191 | |
| 192 // Set expectations. | |
| 193 PrepareForMethodCall(flimflam::kClearPropertyFunction, | |
| 194 base::Bind(&ExpectStringArgument, | |
| 195 flimflam::kCellularAllowRoamingProperty), | |
| 196 response.get()); | |
| 197 // Call method. | |
| 198 client_->ClearProperty(dbus::ObjectPath(kExampleDevicePath), | |
| 199 flimflam::kCellularAllowRoamingProperty, | |
| 200 base::Bind(&ExpectNoResultValue)); | |
| 201 // Run the message loop. | |
| 202 message_loop_.RunAllPending(); | |
| 203 } | |
| 204 | |
| 205 TEST_F(FlimflamDeviceClientTest, AddIPConfig) { | |
| 206 const dbus::ObjectPath expected_result("/result/path"); | |
| 207 // Create response. | |
| 208 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); | |
| 209 dbus::MessageWriter writer(response.get()); | |
| 210 writer.AppendObjectPath(expected_result); | |
| 211 | |
| 212 // Set expectations. | |
| 213 PrepareForMethodCall(flimflam::kAddIPConfigFunction, | |
| 214 base::Bind(&ExpectStringArgument, flimflam::kTypeDHCP), | |
| 215 response.get()); | |
| 216 // Call method. | |
| 217 client_->AddIPConfig(dbus::ObjectPath(kExampleDevicePath), | |
| 218 flimflam::kTypeDHCP, | |
| 219 base::Bind(&ExpectObjectPathResult, expected_result)); | |
| 220 // Run the message loop. | |
| 221 message_loop_.RunAllPending(); | |
| 222 } | |
| 223 | |
| 224 TEST_F(FlimflamDeviceClientTest, CallAddIPConfigAndBlock) { | |
| 225 const dbus::ObjectPath expected_result("/result/path"); | |
| 226 // Create response. | |
| 227 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); | |
| 228 dbus::MessageWriter writer(response.get()); | |
| 229 writer.AppendObjectPath(expected_result); | |
| 230 | |
| 231 // Set expectations. | |
| 232 PrepareForMethodCall(flimflam::kAddIPConfigFunction, | |
| 233 base::Bind(&ExpectStringArgument, flimflam::kTypeDHCP), | |
| 234 response.get()); | |
| 235 // Call method. | |
| 236 const dbus::ObjectPath result = client_->CallAddIPConfigAndBlock( | |
| 237 dbus::ObjectPath(kExampleDevicePath), flimflam::kTypeDHCP); | |
| 238 EXPECT_EQ(expected_result, result); | |
| 239 } | |
| 240 | |
| 241 TEST_F(FlimflamDeviceClientTest, RequirePin) { | |
| 242 const char kPin[] = "123456"; | |
| 243 const bool kRequired = true; | |
| 244 // Create response. | |
| 245 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); | |
| 246 | |
| 247 // Set expectations. | |
| 248 MockClosure mock_closure; | |
| 249 MockErrorCallback mock_error_callback; | |
| 250 PrepareForMethodCall(flimflam::kRequirePinFunction, | |
| 251 base::Bind(&ExpectStringAndBoolArguments, | |
| 252 kPin, | |
| 253 kRequired), | |
| 254 response.get()); | |
| 255 EXPECT_CALL(mock_closure, Run()).Times(1); | |
| 256 // Call method. | |
| 257 client_->RequirePin(dbus::ObjectPath(kExampleDevicePath), | |
| 258 kPin, | |
| 259 kRequired, | |
| 260 mock_closure.GetCallback(), | |
| 261 mock_error_callback.GetCallback()); | |
| 262 // Run the message loop. | |
| 263 message_loop_.RunAllPending(); | |
| 264 } | |
| 265 | |
| 266 TEST_F(FlimflamDeviceClientTest, EnterPin) { | |
| 267 const char kPin[] = "123456"; | |
| 268 // Create response. | |
| 269 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); | |
| 270 | |
| 271 // Set expectations. | |
| 272 MockClosure mock_closure; | |
| 273 MockErrorCallback mock_error_callback; | |
| 274 PrepareForMethodCall(flimflam::kEnterPinFunction, | |
| 275 base::Bind(&ExpectStringArgument, | |
| 276 kPin), | |
| 277 response.get()); | |
| 278 EXPECT_CALL(mock_closure, Run()).Times(1); | |
| 279 // Call method. | |
| 280 client_->EnterPin(dbus::ObjectPath(kExampleDevicePath), | |
| 281 kPin, | |
| 282 mock_closure.GetCallback(), | |
| 283 mock_error_callback.GetCallback()); | |
| 284 // Run the message loop. | |
| 285 message_loop_.RunAllPending(); | |
| 286 } | |
| 287 | |
| 288 TEST_F(FlimflamDeviceClientTest, UnblockPin) { | |
| 289 const char kPuk[] = "987654"; | |
| 290 const char kPin[] = "123456"; | |
| 291 // Create response. | |
| 292 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); | |
| 293 | |
| 294 // Set expectations. | |
| 295 MockClosure mock_closure; | |
| 296 MockErrorCallback mock_error_callback; | |
| 297 PrepareForMethodCall(flimflam::kUnblockPinFunction, | |
| 298 base::Bind(&ExpectTwoStringArguments, kPuk, kPin), | |
| 299 response.get()); | |
| 300 EXPECT_CALL(mock_closure, Run()).Times(1); | |
| 301 // Call method. | |
| 302 client_->UnblockPin(dbus::ObjectPath(kExampleDevicePath), | |
| 303 kPuk, | |
| 304 kPin, | |
| 305 mock_closure.GetCallback(), | |
| 306 mock_error_callback.GetCallback()); | |
| 307 // Run the message loop. | |
| 308 message_loop_.RunAllPending(); | |
| 309 } | |
| 310 | |
| 311 TEST_F(FlimflamDeviceClientTest, ChangePin) { | |
| 312 const char kOldPin[] = "123456"; | |
| 313 const char kNewPin[] = "234567"; | |
| 314 // Create response. | |
| 315 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); | |
| 316 | |
| 317 // Set expectations. | |
| 318 MockClosure mock_closure; | |
| 319 MockErrorCallback mock_error_callback; | |
| 320 PrepareForMethodCall(flimflam::kChangePinFunction, | |
| 321 base::Bind(&ExpectTwoStringArguments, | |
| 322 kOldPin, | |
| 323 kNewPin), | |
| 324 response.get()); | |
| 325 EXPECT_CALL(mock_closure, Run()).Times(1); | |
| 326 // Call method. | |
| 327 client_->ChangePin(dbus::ObjectPath(kExampleDevicePath), | |
| 328 kOldPin, | |
| 329 kNewPin, | |
| 330 mock_closure.GetCallback(), | |
| 331 mock_error_callback.GetCallback()); | |
| 332 // Run the message loop. | |
| 333 message_loop_.RunAllPending(); | |
| 334 } | |
| 335 | |
| 336 TEST_F(FlimflamDeviceClientTest, Register) { | |
| 337 const char kNetworkId[] = "networkid"; | |
| 338 // Create response. | |
| 339 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); | |
| 340 | |
| 341 // Set expectations. | |
| 342 MockClosure mock_closure; | |
| 343 MockErrorCallback mock_error_callback; | |
| 344 PrepareForMethodCall(flimflam::kRegisterFunction, | |
| 345 base::Bind(&ExpectStringArgument, kNetworkId), | |
| 346 response.get()); | |
| 347 EXPECT_CALL(mock_closure, Run()).Times(1); | |
| 348 // Call method. | |
| 349 client_->Register(dbus::ObjectPath(kExampleDevicePath), | |
| 350 kNetworkId, | |
| 351 mock_closure.GetCallback(), | |
| 352 mock_error_callback.GetCallback()); | |
| 353 // Run the message loop. | |
| 354 message_loop_.RunAllPending(); | |
| 355 } | |
| 356 | |
| 357 } // namespace chromeos | |
| OLD | NEW |