| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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/components/tether/tether_connector.h" | 5 #include "chromeos/components/tether/tether_connector.h" |
| 6 | 6 |
| 7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
| 8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "chromeos/components/tether/connect_tethering_operation.h" | 9 #include "chromeos/components/tether/connect_tethering_operation.h" |
| 10 #include "chromeos/components/tether/device_id_tether_network_guid_map.h" | 10 #include "chromeos/components/tether/device_id_tether_network_guid_map.h" |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 "TetherNetworkName2", "TetherNetworkCarrier2", | 174 "TetherNetworkName2", "TetherNetworkCarrier2", |
| 175 90 /* battery_percentage */, 50 /* signal_strength */, | 175 90 /* battery_percentage */, 50 /* signal_strength */, |
| 176 true /* has_connected_to_host */); | 176 true /* has_connected_to_host */); |
| 177 } | 177 } |
| 178 | 178 |
| 179 void SuccessfullyJoinWifiNetwork() { | 179 void SuccessfullyJoinWifiNetwork() { |
| 180 ConfigureService(CreateWifiConfigurationJsonString()); | 180 ConfigureService(CreateWifiConfigurationJsonString()); |
| 181 fake_wifi_hotspot_connector_->CallMostRecentCallback(kWifiNetworkGuid); | 181 fake_wifi_hotspot_connector_->CallMostRecentCallback(kWifiNetworkGuid); |
| 182 } | 182 } |
| 183 | 183 |
| 184 void VerifyTetherAndWifiNetworkAssociation( | |
| 185 const std::string& tether_network_guid) { | |
| 186 const NetworkState* tether_network_state = | |
| 187 network_state_handler()->GetNetworkStateFromGuid(tether_network_guid); | |
| 188 EXPECT_TRUE(tether_network_state); | |
| 189 EXPECT_EQ(kWifiNetworkGuid, tether_network_state->tether_guid()); | |
| 190 | |
| 191 const NetworkState* wifi_network_state = | |
| 192 network_state_handler()->GetNetworkStateFromGuid(kWifiNetworkGuid); | |
| 193 EXPECT_TRUE(wifi_network_state); | |
| 194 EXPECT_EQ(tether_network_guid, wifi_network_state->tether_guid()); | |
| 195 } | |
| 196 | |
| 197 void SuccessCallback() { result_ = kSuccessResult; } | 184 void SuccessCallback() { result_ = kSuccessResult; } |
| 198 | 185 |
| 199 void ErrorCallback(const std::string& error_name) { result_ = error_name; } | 186 void ErrorCallback(const std::string& error_name) { result_ = error_name; } |
| 200 | 187 |
| 201 void CallConnect(const std::string& tether_network_guid) { | 188 void CallConnect(const std::string& tether_network_guid) { |
| 202 tether_connector_->ConnectToNetwork( | 189 tether_connector_->ConnectToNetwork( |
| 203 tether_network_guid, | 190 tether_network_guid, |
| 204 base::Bind(&TetherConnectorTest::SuccessCallback, | 191 base::Bind(&TetherConnectorTest::SuccessCallback, |
| 205 base::Unretained(this)), | 192 base::Unretained(this)), |
| 206 base::Bind(&TetherConnectorTest::ErrorCallback, | 193 base::Bind(&TetherConnectorTest::ErrorCallback, |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 322 fake_operation_factory_->created_operations()[0]->SendSuccessfulResponse( | 309 fake_operation_factory_->created_operations()[0]->SendSuccessfulResponse( |
| 323 kSsid, kPassword); | 310 kSsid, kPassword); |
| 324 EXPECT_EQ(ActiveHost::ActiveHostStatus::CONNECTING, | 311 EXPECT_EQ(ActiveHost::ActiveHostStatus::CONNECTING, |
| 325 fake_active_host_->GetActiveHostStatus()); | 312 fake_active_host_->GetActiveHostStatus()); |
| 326 | 313 |
| 327 // |fake_wifi_hotspot_connector_| should have received the SSID and password | 314 // |fake_wifi_hotspot_connector_| should have received the SSID and password |
| 328 // above. Verify this, then return an empty string, signaling a failure to | 315 // above. Verify this, then return an empty string, signaling a failure to |
| 329 // connect. | 316 // connect. |
| 330 EXPECT_EQ(kSsid, fake_wifi_hotspot_connector_->most_recent_ssid()); | 317 EXPECT_EQ(kSsid, fake_wifi_hotspot_connector_->most_recent_ssid()); |
| 331 EXPECT_EQ(kPassword, fake_wifi_hotspot_connector_->most_recent_password()); | 318 EXPECT_EQ(kPassword, fake_wifi_hotspot_connector_->most_recent_password()); |
| 319 EXPECT_EQ(fake_active_host_->GetTetherNetworkGuid(), |
| 320 fake_wifi_hotspot_connector_->most_recent_tether_network_guid()); |
| 332 fake_wifi_hotspot_connector_->CallMostRecentCallback(""); | 321 fake_wifi_hotspot_connector_->CallMostRecentCallback(""); |
| 333 | 322 |
| 334 // The failure should have resulted in the host being disconnected. | 323 // The failure should have resulted in the host being disconnected. |
| 335 EXPECT_EQ(ActiveHost::ActiveHostStatus::DISCONNECTED, | 324 EXPECT_EQ(ActiveHost::ActiveHostStatus::DISCONNECTED, |
| 336 fake_active_host_->GetActiveHostStatus()); | 325 fake_active_host_->GetActiveHostStatus()); |
| 337 EXPECT_EQ(NetworkConnectionHandler::kErrorConnectFailed, GetResultAndReset()); | 326 EXPECT_EQ(NetworkConnectionHandler::kErrorConnectFailed, GetResultAndReset()); |
| 338 } | 327 } |
| 339 | 328 |
| 340 TEST_F(TetherConnectorTest, TestCancelWhileConnectingToWifi) { | 329 TEST_F(TetherConnectorTest, TestCancelWhileConnectingToWifi) { |
| 341 CallConnect(GetTetherNetworkGuid(test_devices_[0].GetDeviceId())); | 330 CallConnect(GetTetherNetworkGuid(test_devices_[0].GetDeviceId())); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 382 fake_operation_factory_->created_operations()[0]->SendSuccessfulResponse( | 371 fake_operation_factory_->created_operations()[0]->SendSuccessfulResponse( |
| 383 kSsid, kPassword); | 372 kSsid, kPassword); |
| 384 EXPECT_EQ(ActiveHost::ActiveHostStatus::CONNECTING, | 373 EXPECT_EQ(ActiveHost::ActiveHostStatus::CONNECTING, |
| 385 fake_active_host_->GetActiveHostStatus()); | 374 fake_active_host_->GetActiveHostStatus()); |
| 386 | 375 |
| 387 // |fake_wifi_hotspot_connector_| should have received the SSID and password | 376 // |fake_wifi_hotspot_connector_| should have received the SSID and password |
| 388 // above. Verify this, then return the GUID corresponding to the connected | 377 // above. Verify this, then return the GUID corresponding to the connected |
| 389 // Wi-Fi network. | 378 // Wi-Fi network. |
| 390 EXPECT_EQ(kSsid, fake_wifi_hotspot_connector_->most_recent_ssid()); | 379 EXPECT_EQ(kSsid, fake_wifi_hotspot_connector_->most_recent_ssid()); |
| 391 EXPECT_EQ(kPassword, fake_wifi_hotspot_connector_->most_recent_password()); | 380 EXPECT_EQ(kPassword, fake_wifi_hotspot_connector_->most_recent_password()); |
| 381 EXPECT_EQ(fake_active_host_->GetTetherNetworkGuid(), |
| 382 fake_wifi_hotspot_connector_->most_recent_tether_network_guid()); |
| 392 SuccessfullyJoinWifiNetwork(); | 383 SuccessfullyJoinWifiNetwork(); |
| 393 | 384 |
| 394 // The active host should now be connected, and the tether and Wi-Fi networks | 385 // The active host should now be connected. |
| 395 // should be associated. | |
| 396 EXPECT_EQ(ActiveHost::ActiveHostStatus::CONNECTED, | 386 EXPECT_EQ(ActiveHost::ActiveHostStatus::CONNECTED, |
| 397 fake_active_host_->GetActiveHostStatus()); | 387 fake_active_host_->GetActiveHostStatus()); |
| 398 EXPECT_EQ(test_devices_[0].GetDeviceId(), | 388 EXPECT_EQ(test_devices_[0].GetDeviceId(), |
| 399 fake_active_host_->GetActiveHostDeviceId()); | 389 fake_active_host_->GetActiveHostDeviceId()); |
| 400 EXPECT_EQ(GetTetherNetworkGuid(test_devices_[0].GetDeviceId()), | 390 EXPECT_EQ(GetTetherNetworkGuid(test_devices_[0].GetDeviceId()), |
| 401 fake_active_host_->GetTetherNetworkGuid()); | 391 fake_active_host_->GetTetherNetworkGuid()); |
| 402 EXPECT_EQ(kWifiNetworkGuid, fake_active_host_->GetWifiNetworkGuid()); | 392 EXPECT_EQ(kWifiNetworkGuid, fake_active_host_->GetWifiNetworkGuid()); |
| 403 VerifyTetherAndWifiNetworkAssociation( | 393 |
| 404 GetTetherNetworkGuid(test_devices_[0].GetDeviceId())); | |
| 405 EXPECT_EQ(kSuccessResult, GetResultAndReset()); | 394 EXPECT_EQ(kSuccessResult, GetResultAndReset()); |
| 406 } | 395 } |
| 407 | 396 |
| 408 TEST_F(TetherConnectorTest, | 397 TEST_F(TetherConnectorTest, |
| 409 TestNewConnectionAttemptDuringFetch_DifferentDevice) { | 398 TestNewConnectionAttemptDuringFetch_DifferentDevice) { |
| 410 CallConnect(GetTetherNetworkGuid(test_devices_[0].GetDeviceId())); | 399 CallConnect(GetTetherNetworkGuid(test_devices_[0].GetDeviceId())); |
| 411 | 400 |
| 412 // Instead of invoking the pending callbacks on |fake_tether_host_fetcher_|, | 401 // Instead of invoking the pending callbacks on |fake_tether_host_fetcher_|, |
| 413 // attempt another connection attempt, this time to another device. | 402 // attempt another connection attempt, this time to another device. |
| 414 CallConnect(GetTetherNetworkGuid(test_devices_[1].GetDeviceId())); | 403 CallConnect(GetTetherNetworkGuid(test_devices_[1].GetDeviceId())); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 458 EXPECT_EQ(GetTetherNetworkGuid(test_devices_[1].GetDeviceId()), | 447 EXPECT_EQ(GetTetherNetworkGuid(test_devices_[1].GetDeviceId()), |
| 459 fake_active_host_->GetTetherNetworkGuid()); | 448 fake_active_host_->GetTetherNetworkGuid()); |
| 460 EXPECT_TRUE(fake_active_host_->GetWifiNetworkGuid().empty()); | 449 EXPECT_TRUE(fake_active_host_->GetWifiNetworkGuid().empty()); |
| 461 | 450 |
| 462 // A second operation should have been created. | 451 // A second operation should have been created. |
| 463 EXPECT_EQ(2u, fake_operation_factory_->created_operations().size()); | 452 EXPECT_EQ(2u, fake_operation_factory_->created_operations().size()); |
| 464 | 453 |
| 465 // No connection should have been started. | 454 // No connection should have been started. |
| 466 EXPECT_TRUE(fake_wifi_hotspot_connector_->most_recent_ssid().empty()); | 455 EXPECT_TRUE(fake_wifi_hotspot_connector_->most_recent_ssid().empty()); |
| 467 EXPECT_TRUE(fake_wifi_hotspot_connector_->most_recent_password().empty()); | 456 EXPECT_TRUE(fake_wifi_hotspot_connector_->most_recent_password().empty()); |
| 457 EXPECT_TRUE( |
| 458 fake_wifi_hotspot_connector_->most_recent_tether_network_guid().empty()); |
| 468 | 459 |
| 469 // The second operation replies successfully, and this response should | 460 // The second operation replies successfully, and this response should |
| 470 // result in a Wi-Fi connection attempt. | 461 // result in a Wi-Fi connection attempt. |
| 471 fake_operation_factory_->created_operations()[1]->SendSuccessfulResponse( | 462 fake_operation_factory_->created_operations()[1]->SendSuccessfulResponse( |
| 472 kSsid, kPassword); | 463 kSsid, kPassword); |
| 473 EXPECT_EQ(kSsid, fake_wifi_hotspot_connector_->most_recent_ssid()); | 464 EXPECT_EQ(kSsid, fake_wifi_hotspot_connector_->most_recent_ssid()); |
| 474 EXPECT_EQ(kPassword, fake_wifi_hotspot_connector_->most_recent_password()); | 465 EXPECT_EQ(kPassword, fake_wifi_hotspot_connector_->most_recent_password()); |
| 466 EXPECT_EQ(fake_active_host_->GetTetherNetworkGuid(), |
| 467 fake_wifi_hotspot_connector_->most_recent_tether_network_guid()); |
| 475 } | 468 } |
| 476 | 469 |
| 477 TEST_F(TetherConnectorTest, | 470 TEST_F(TetherConnectorTest, |
| 478 TestNewConnectionAttemptDuringWifiConnection_DifferentDevice) { | 471 TestNewConnectionAttemptDuringWifiConnection_DifferentDevice) { |
| 479 CallConnect(GetTetherNetworkGuid(test_devices_[0].GetDeviceId())); | 472 CallConnect(GetTetherNetworkGuid(test_devices_[0].GetDeviceId())); |
| 480 EXPECT_EQ(ActiveHost::ActiveHostStatus::CONNECTING, | 473 EXPECT_EQ(ActiveHost::ActiveHostStatus::CONNECTING, |
| 481 fake_active_host_->GetActiveHostStatus()); | 474 fake_active_host_->GetActiveHostStatus()); |
| 482 EXPECT_EQ(test_devices_[0].GetDeviceId(), | 475 EXPECT_EQ(test_devices_[0].GetDeviceId(), |
| 483 fake_active_host_->GetActiveHostDeviceId()); | 476 fake_active_host_->GetActiveHostDeviceId()); |
| 484 | 477 |
| 485 fake_tether_host_fetcher_->InvokePendingCallbacks(); | 478 fake_tether_host_fetcher_->InvokePendingCallbacks(); |
| 486 | 479 |
| 487 EXPECT_EQ(1u, fake_operation_factory_->created_operations().size()); | 480 EXPECT_EQ(1u, fake_operation_factory_->created_operations().size()); |
| 488 fake_operation_factory_->created_operations()[0]->SendSuccessfulResponse( | 481 fake_operation_factory_->created_operations()[0]->SendSuccessfulResponse( |
| 489 kSsid, kPassword); | 482 kSsid, kPassword); |
| 490 EXPECT_EQ(ActiveHost::ActiveHostStatus::CONNECTING, | 483 EXPECT_EQ(ActiveHost::ActiveHostStatus::CONNECTING, |
| 491 fake_active_host_->GetActiveHostStatus()); | 484 fake_active_host_->GetActiveHostStatus()); |
| 492 EXPECT_EQ(kSsid, fake_wifi_hotspot_connector_->most_recent_ssid()); | 485 EXPECT_EQ(kSsid, fake_wifi_hotspot_connector_->most_recent_ssid()); |
| 493 EXPECT_EQ(kPassword, fake_wifi_hotspot_connector_->most_recent_password()); | 486 EXPECT_EQ(kPassword, fake_wifi_hotspot_connector_->most_recent_password()); |
| 487 EXPECT_EQ(fake_active_host_->GetTetherNetworkGuid(), |
| 488 fake_wifi_hotspot_connector_->most_recent_tether_network_guid()); |
| 494 | 489 |
| 495 // While the connection to the Wi-Fi network is in progress, start a new | 490 // While the connection to the Wi-Fi network is in progress, start a new |
| 496 // connection attempt. | 491 // connection attempt. |
| 497 CallConnect(GetTetherNetworkGuid(test_devices_[1].GetDeviceId())); | 492 CallConnect(GetTetherNetworkGuid(test_devices_[1].GetDeviceId())); |
| 498 // The first connection attempt should have resulted in a connect canceled | 493 // The first connection attempt should have resulted in a connect canceled |
| 499 // error. | 494 // error. |
| 500 EXPECT_EQ(NetworkConnectionHandler::kErrorConnectCanceled, | 495 EXPECT_EQ(NetworkConnectionHandler::kErrorConnectCanceled, |
| 501 GetResultAndReset()); | 496 GetResultAndReset()); |
| 502 fake_tether_host_fetcher_->InvokePendingCallbacks(); | 497 fake_tether_host_fetcher_->InvokePendingCallbacks(); |
| 503 | 498 |
| 504 // Connect successfully to the first Wi-Fi network. Even though a temporary | 499 // Connect successfully to the first Wi-Fi network. Even though a temporary |
| 505 // connection has succeeded, the active host should be CONNECTING to device 1. | 500 // connection has succeeded, the active host should be CONNECTING to device 1. |
| 506 SuccessfullyJoinWifiNetwork(); | 501 SuccessfullyJoinWifiNetwork(); |
| 507 EXPECT_EQ(ActiveHost::ActiveHostStatus::CONNECTING, | 502 EXPECT_EQ(ActiveHost::ActiveHostStatus::CONNECTING, |
| 508 fake_active_host_->GetActiveHostStatus()); | 503 fake_active_host_->GetActiveHostStatus()); |
| 509 EXPECT_EQ(test_devices_[1].GetDeviceId(), | 504 EXPECT_EQ(test_devices_[1].GetDeviceId(), |
| 510 fake_active_host_->GetActiveHostDeviceId()); | 505 fake_active_host_->GetActiveHostDeviceId()); |
| 511 EXPECT_EQ(GetTetherNetworkGuid(test_devices_[1].GetDeviceId()), | 506 EXPECT_EQ(GetTetherNetworkGuid(test_devices_[1].GetDeviceId()), |
| 512 fake_active_host_->GetTetherNetworkGuid()); | 507 fake_active_host_->GetTetherNetworkGuid()); |
| 513 EXPECT_TRUE(fake_active_host_->GetWifiNetworkGuid().empty()); | 508 EXPECT_TRUE(fake_active_host_->GetWifiNetworkGuid().empty()); |
| 514 } | 509 } |
| 515 | 510 |
| 516 } // namespace tether | 511 } // namespace tether |
| 517 | 512 |
| 518 } // namespace chromeos | 513 } // namespace chromeos |
| OLD | NEW |