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/wifi_hotspot_connector.h" | 5 #include "chromeos/components/tether/wifi_hotspot_connector.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <sstream> | 8 #include <sstream> |
9 | 9 |
10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
11 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
12 #include "base/test/scoped_task_environment.h" | 12 #include "base/test/scoped_task_environment.h" |
13 #include "base/timer/mock_timer.h" | 13 #include "base/timer/mock_timer.h" |
14 #include "base/values.h" | 14 #include "base/values.h" |
| 15 #include "chromeos/components/tether/mock_host_connection_metrics_logger.h" |
15 #include "chromeos/dbus/dbus_thread_manager.h" | 16 #include "chromeos/dbus/dbus_thread_manager.h" |
16 #include "chromeos/dbus/shill_device_client.h" | 17 #include "chromeos/dbus/shill_device_client.h" |
17 #include "chromeos/dbus/shill_service_client.h" | 18 #include "chromeos/dbus/shill_service_client.h" |
18 #include "chromeos/network/network_connect.h" | 19 #include "chromeos/network/network_connect.h" |
19 #include "chromeos/network/network_state.h" | 20 #include "chromeos/network/network_state.h" |
20 #include "chromeos/network/network_state_handler.h" | 21 #include "chromeos/network/network_state_handler.h" |
21 #include "chromeos/network/network_state_test.h" | 22 #include "chromeos/network/network_state_test.h" |
22 #include "chromeos/network/shill_property_util.h" | 23 #include "chromeos/network/shill_property_util.h" |
23 #include "testing/gmock/include/gmock/gmock.h" | 24 #include "testing/gmock/include/gmock/gmock.h" |
24 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 network_state_handler()->AddTetherNetworkState( | 133 network_state_handler()->AddTetherNetworkState( |
133 kTetherNetworkGuid, "tetherNetworkName" /* name */, | 134 kTetherNetworkGuid, "tetherNetworkName" /* name */, |
134 "tetherNetworkCarrier" /* carrier */, 100 /* full battery */, | 135 "tetherNetworkCarrier" /* carrier */, 100 /* full battery */, |
135 100 /* full signal strength */, false /* has_connected_to_host */); | 136 100 /* full signal strength */, false /* has_connected_to_host */); |
136 | 137 |
137 network_state_handler()->AddTetherNetworkState( | 138 network_state_handler()->AddTetherNetworkState( |
138 kTetherNetworkGuid2, "tetherNetworkName2" /* name */, | 139 kTetherNetworkGuid2, "tetherNetworkName2" /* name */, |
139 "tetherNetworkCarrier2" /* carrier */, 100 /* full battery */, | 140 "tetherNetworkCarrier2" /* carrier */, 100 /* full battery */, |
140 100 /* full signal strength */, false /* has_connected_to_host */); | 141 100 /* full signal strength */, false /* has_connected_to_host */); |
141 | 142 |
| 143 mock_host_connection_metrics_logger_ = |
| 144 base::MakeUnique<MockHostConnectionMetricsLogger>(); |
| 145 |
142 wifi_hotspot_connector_ = base::WrapUnique(new WifiHotspotConnector( | 146 wifi_hotspot_connector_ = base::WrapUnique(new WifiHotspotConnector( |
143 network_state_handler(), test_network_connect_.get())); | 147 network_state_handler(), test_network_connect_.get(), |
| 148 mock_host_connection_metrics_logger_.get())); |
144 | 149 |
145 mock_timer_ = new base::MockTimer(true /* retain_user_task */, | 150 mock_timer_ = new base::MockTimer(true /* retain_user_task */, |
146 false /* is_repeating */); | 151 false /* is_repeating */); |
147 wifi_hotspot_connector_->SetTimerForTest(base::WrapUnique(mock_timer_)); | 152 wifi_hotspot_connector_->SetTimerForTest(base::WrapUnique(mock_timer_)); |
148 } | 153 } |
149 | 154 |
150 void SetUpShillState() { | 155 void SetUpShillState() { |
151 // Add a Wi-Fi service unrelated to the one under test. In some tests, a | 156 // Add a Wi-Fi service unrelated to the one under test. In some tests, a |
152 // connection from another device is tested. | 157 // connection from another device is tested. |
153 other_wifi_service_path_ = ConfigureService( | 158 other_wifi_service_path_ = ConfigureService( |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
249 connection_callback_responses_.push_back(wifi_guid); | 254 connection_callback_responses_.push_back(wifi_guid); |
250 } | 255 } |
251 | 256 |
252 base::test::ScopedTaskEnvironment scoped_task_environment_; | 257 base::test::ScopedTaskEnvironment scoped_task_environment_; |
253 | 258 |
254 std::string other_wifi_service_path_; | 259 std::string other_wifi_service_path_; |
255 std::vector<std::string> connection_callback_responses_; | 260 std::vector<std::string> connection_callback_responses_; |
256 | 261 |
257 base::MockTimer* mock_timer_; | 262 base::MockTimer* mock_timer_; |
258 std::unique_ptr<TestNetworkConnect> test_network_connect_; | 263 std::unique_ptr<TestNetworkConnect> test_network_connect_; |
| 264 std::unique_ptr<MockHostConnectionMetricsLogger> |
| 265 mock_host_connection_metrics_logger_; |
259 | 266 |
260 std::unique_ptr<WifiHotspotConnector> wifi_hotspot_connector_; | 267 std::unique_ptr<WifiHotspotConnector> wifi_hotspot_connector_; |
261 | 268 |
262 private: | 269 private: |
263 DISALLOW_COPY_AND_ASSIGN(WifiHotspotConnectorTest); | 270 DISALLOW_COPY_AND_ASSIGN(WifiHotspotConnectorTest); |
264 }; | 271 }; |
265 | 272 |
266 TEST_F(WifiHotspotConnectorTest, TestConnect_NetworkDoesNotBecomeConnectable) { | 273 TEST_F(WifiHotspotConnectorTest, TestConnect_NetworkDoesNotBecomeConnectable) { |
| 274 EXPECT_CALL(*mock_host_connection_metrics_logger_, |
| 275 RecordConnectionToHostResult( |
| 276 HostConnectionMetricsLogger::ConnectionToHostResult:: |
| 277 CONNECTION_RESULT_FAILURE_CLIENT_CONNECTION_TIMEOUT)); |
| 278 |
267 wifi_hotspot_connector_->ConnectToWifiHotspot( | 279 wifi_hotspot_connector_->ConnectToWifiHotspot( |
268 std::string(kSsid), std::string(kPassword), kTetherNetworkGuid, | 280 std::string(kSsid), std::string(kPassword), kTetherNetworkGuid, |
269 base::Bind(&WifiHotspotConnectorTest::WifiConnectionCallback, | 281 base::Bind(&WifiHotspotConnectorTest::WifiConnectionCallback, |
270 base::Unretained(this))); | 282 base::Unretained(this))); |
271 | 283 |
272 std::string wifi_guid = | 284 std::string wifi_guid = |
273 VerifyLastConfiguration(std::string(kSsid), std::string(kPassword)); | 285 VerifyLastConfiguration(std::string(kSsid), std::string(kPassword)); |
274 EXPECT_FALSE(wifi_guid.empty()); | 286 EXPECT_FALSE(wifi_guid.empty()); |
275 | 287 |
276 // Network does not become connectable. | 288 // Network does not become connectable. |
277 EXPECT_EQ("", test_network_connect_->network_id_to_connect()); | 289 EXPECT_EQ("", test_network_connect_->network_id_to_connect()); |
278 | 290 |
279 // Timeout timer fires. | 291 // Timeout timer fires. |
280 EXPECT_EQ(0u, connection_callback_responses_.size()); | 292 EXPECT_EQ(0u, connection_callback_responses_.size()); |
281 InvokeTimerTask(); | 293 InvokeTimerTask(); |
282 EXPECT_EQ(1u, connection_callback_responses_.size()); | 294 EXPECT_EQ(1u, connection_callback_responses_.size()); |
283 EXPECT_EQ("", connection_callback_responses_[0]); | 295 EXPECT_EQ("", connection_callback_responses_[0]); |
284 } | 296 } |
285 | 297 |
286 TEST_F(WifiHotspotConnectorTest, TestConnect_AnotherNetworkBecomesConnectable) { | 298 TEST_F(WifiHotspotConnectorTest, TestConnect_AnotherNetworkBecomesConnectable) { |
| 299 EXPECT_CALL(*mock_host_connection_metrics_logger_, |
| 300 RecordConnectionToHostResult( |
| 301 HostConnectionMetricsLogger::ConnectionToHostResult:: |
| 302 CONNECTION_RESULT_FAILURE_CLIENT_CONNECTION_TIMEOUT)); |
| 303 |
287 wifi_hotspot_connector_->ConnectToWifiHotspot( | 304 wifi_hotspot_connector_->ConnectToWifiHotspot( |
288 std::string(kSsid), std::string(kPassword), kTetherNetworkGuid, | 305 std::string(kSsid), std::string(kPassword), kTetherNetworkGuid, |
289 base::Bind(&WifiHotspotConnectorTest::WifiConnectionCallback, | 306 base::Bind(&WifiHotspotConnectorTest::WifiConnectionCallback, |
290 base::Unretained(this))); | 307 base::Unretained(this))); |
291 | 308 |
292 std::string wifi_guid = | 309 std::string wifi_guid = |
293 VerifyLastConfiguration(std::string(kSsid), std::string(kPassword)); | 310 VerifyLastConfiguration(std::string(kSsid), std::string(kPassword)); |
294 EXPECT_FALSE(wifi_guid.empty()); | 311 EXPECT_FALSE(wifi_guid.empty()); |
295 | 312 |
296 // Another network becomes connectable. This should not cause the connection | 313 // Another network becomes connectable. This should not cause the connection |
297 // to start. | 314 // to start. |
298 NotifyConnectable(other_wifi_service_path_); | 315 NotifyConnectable(other_wifi_service_path_); |
299 VerifyNetworkNotAssociated(wifi_guid); | 316 VerifyNetworkNotAssociated(wifi_guid); |
300 std::string other_wifi_guid = network_state_handler() | 317 std::string other_wifi_guid = network_state_handler() |
301 ->GetNetworkState(other_wifi_service_path_) | 318 ->GetNetworkState(other_wifi_service_path_) |
302 ->guid(); | 319 ->guid(); |
303 VerifyNetworkNotAssociated(other_wifi_guid); | 320 VerifyNetworkNotAssociated(other_wifi_guid); |
304 EXPECT_EQ("", test_network_connect_->network_id_to_connect()); | 321 EXPECT_EQ("", test_network_connect_->network_id_to_connect()); |
305 | 322 |
306 // Timeout timer fires. | 323 // Timeout timer fires. |
307 EXPECT_EQ(0u, connection_callback_responses_.size()); | 324 EXPECT_EQ(0u, connection_callback_responses_.size()); |
308 InvokeTimerTask(); | 325 InvokeTimerTask(); |
309 EXPECT_EQ(1u, connection_callback_responses_.size()); | 326 EXPECT_EQ(1u, connection_callback_responses_.size()); |
310 EXPECT_EQ("", connection_callback_responses_[0]); | 327 EXPECT_EQ("", connection_callback_responses_[0]); |
311 } | 328 } |
312 | 329 |
313 TEST_F(WifiHotspotConnectorTest, TestConnect_CannotConnectToNetwork) { | 330 TEST_F(WifiHotspotConnectorTest, TestConnect_CannotConnectToNetwork) { |
| 331 EXPECT_CALL(*mock_host_connection_metrics_logger_, |
| 332 RecordConnectionToHostResult( |
| 333 HostConnectionMetricsLogger::ConnectionToHostResult:: |
| 334 CONNECTION_RESULT_FAILURE_CLIENT_CONNECTION_TIMEOUT)); |
| 335 |
314 wifi_hotspot_connector_->ConnectToWifiHotspot( | 336 wifi_hotspot_connector_->ConnectToWifiHotspot( |
315 std::string(kSsid), std::string(kPassword), kTetherNetworkGuid, | 337 std::string(kSsid), std::string(kPassword), kTetherNetworkGuid, |
316 base::Bind(&WifiHotspotConnectorTest::WifiConnectionCallback, | 338 base::Bind(&WifiHotspotConnectorTest::WifiConnectionCallback, |
317 base::Unretained(this))); | 339 base::Unretained(this))); |
318 | 340 |
319 std::string wifi_guid = | 341 std::string wifi_guid = |
320 VerifyLastConfiguration(std::string(kSsid), std::string(kPassword)); | 342 VerifyLastConfiguration(std::string(kSsid), std::string(kPassword)); |
321 EXPECT_FALSE(wifi_guid.empty()); | 343 EXPECT_FALSE(wifi_guid.empty()); |
322 | 344 |
323 // Network becomes connectable. | 345 // Network becomes connectable. |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
377 | 399 |
378 // Connection to network successful. | 400 // Connection to network successful. |
379 NotifyConnected(test_network_connect_->last_service_path_created()); | 401 NotifyConnected(test_network_connect_->last_service_path_created()); |
380 EXPECT_EQ(1u, connection_callback_responses_.size()); | 402 EXPECT_EQ(1u, connection_callback_responses_.size()); |
381 EXPECT_EQ(wifi_guid, connection_callback_responses_[0]); | 403 EXPECT_EQ(wifi_guid, connection_callback_responses_[0]); |
382 VerifyTimerStopped(); | 404 VerifyTimerStopped(); |
383 } | 405 } |
384 | 406 |
385 TEST_F(WifiHotspotConnectorTest, | 407 TEST_F(WifiHotspotConnectorTest, |
386 TestConnect_SecondConnectionWhileWaitingForFirstToBecomeConnectable) { | 408 TestConnect_SecondConnectionWhileWaitingForFirstToBecomeConnectable) { |
| 409 EXPECT_CALL( |
| 410 *mock_host_connection_metrics_logger_, |
| 411 RecordConnectionToHostResult( |
| 412 HostConnectionMetricsLogger::ConnectionToHostResult:: |
| 413 CONNECTION_RESULT_FAILURE_CLIENT_CONNECTION_CANCELED_BY_NEW_ATTEMP
T)); |
| 414 |
387 wifi_hotspot_connector_->ConnectToWifiHotspot( | 415 wifi_hotspot_connector_->ConnectToWifiHotspot( |
388 "ssid1", "password1", "tetherNetworkGuid1", | 416 "ssid1", "password1", "tetherNetworkGuid1", |
389 base::Bind(&WifiHotspotConnectorTest::WifiConnectionCallback, | 417 base::Bind(&WifiHotspotConnectorTest::WifiConnectionCallback, |
390 base::Unretained(this))); | 418 base::Unretained(this))); |
391 | 419 |
392 std::string wifi_guid1 = VerifyLastConfiguration("ssid1", "password1"); | 420 std::string wifi_guid1 = VerifyLastConfiguration("ssid1", "password1"); |
393 EXPECT_FALSE(wifi_guid1.empty()); | 421 EXPECT_FALSE(wifi_guid1.empty()); |
394 std::string service_path1 = | 422 std::string service_path1 = |
395 test_network_connect_->last_service_path_created(); | 423 test_network_connect_->last_service_path_created(); |
396 EXPECT_FALSE(service_path1.empty()); | 424 EXPECT_FALSE(service_path1.empty()); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
432 | 460 |
433 // Connection to network successful. | 461 // Connection to network successful. |
434 NotifyConnected(service_path2); | 462 NotifyConnected(service_path2); |
435 EXPECT_EQ(2u, connection_callback_responses_.size()); | 463 EXPECT_EQ(2u, connection_callback_responses_.size()); |
436 EXPECT_EQ(wifi_guid2, connection_callback_responses_[1]); | 464 EXPECT_EQ(wifi_guid2, connection_callback_responses_[1]); |
437 VerifyTimerStopped(); | 465 VerifyTimerStopped(); |
438 } | 466 } |
439 | 467 |
440 TEST_F(WifiHotspotConnectorTest, | 468 TEST_F(WifiHotspotConnectorTest, |
441 TestConnect_SecondConnectionWhileWaitingForFirstToConnect) { | 469 TestConnect_SecondConnectionWhileWaitingForFirstToConnect) { |
| 470 EXPECT_CALL( |
| 471 *mock_host_connection_metrics_logger_, |
| 472 RecordConnectionToHostResult( |
| 473 HostConnectionMetricsLogger::ConnectionToHostResult:: |
| 474 CONNECTION_RESULT_FAILURE_CLIENT_CONNECTION_CANCELED_BY_NEW_ATTEMP
T)); |
| 475 |
442 wifi_hotspot_connector_->ConnectToWifiHotspot( | 476 wifi_hotspot_connector_->ConnectToWifiHotspot( |
443 "ssid1", "password1", kTetherNetworkGuid, | 477 "ssid1", "password1", kTetherNetworkGuid, |
444 base::Bind(&WifiHotspotConnectorTest::WifiConnectionCallback, | 478 base::Bind(&WifiHotspotConnectorTest::WifiConnectionCallback, |
445 base::Unretained(this))); | 479 base::Unretained(this))); |
446 | 480 |
447 std::string wifi_guid1 = VerifyLastConfiguration("ssid1", "password1"); | 481 std::string wifi_guid1 = VerifyLastConfiguration("ssid1", "password1"); |
448 EXPECT_FALSE(wifi_guid1.empty()); | 482 EXPECT_FALSE(wifi_guid1.empty()); |
449 std::string service_path1 = | 483 std::string service_path1 = |
450 test_network_connect_->last_service_path_created(); | 484 test_network_connect_->last_service_path_created(); |
451 EXPECT_FALSE(service_path1.empty()); | 485 EXPECT_FALSE(service_path1.empty()); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
489 // Connection to network successful. | 523 // Connection to network successful. |
490 NotifyConnected(service_path2); | 524 NotifyConnected(service_path2); |
491 EXPECT_EQ(2u, connection_callback_responses_.size()); | 525 EXPECT_EQ(2u, connection_callback_responses_.size()); |
492 EXPECT_EQ(wifi_guid2, connection_callback_responses_[1]); | 526 EXPECT_EQ(wifi_guid2, connection_callback_responses_[1]); |
493 VerifyTimerStopped(); | 527 VerifyTimerStopped(); |
494 } | 528 } |
495 | 529 |
496 } // namespace tether | 530 } // namespace tether |
497 | 531 |
498 } // namespace chromeos | 532 } // namespace chromeos |
OLD | NEW |