rtenneti | 041b299 | 2015-02-23 23:03:28 | [diff] [blame] | 1 | // Copyright 2015 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 | |
Ryan Hamilton | a3ee93a7 | 2018-08-01 22:03:08 | [diff] [blame] | 5 | #include "net/quic/network_connection.h" |
rtenneti | 041b299 | 2015-02-23 23:03:28 | [diff] [blame] | 6 | |
rch | d6163f3 | 2017-01-30 23:50:38 | [diff] [blame] | 7 | #include "base/run_loop.h" |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 8 | #include "base/test/scoped_task_environment.h" |
mgersh | af9a923 | 2017-04-13 20:19:03 | [diff] [blame] | 9 | #include "net/base/mock_network_change_notifier.h" |
rtenneti | 041b299 | 2015-02-23 23:03:28 | [diff] [blame] | 10 | #include "testing/gtest/include/gtest/gtest.h" |
| 11 | |
| 12 | namespace net { |
| 13 | namespace test { |
| 14 | |
rch | d6163f3 | 2017-01-30 23:50:38 | [diff] [blame] | 15 | constexpr auto CONNECTION_3G = NetworkChangeNotifier::CONNECTION_3G; |
| 16 | constexpr auto CONNECTION_2G = NetworkChangeNotifier::CONNECTION_2G; |
| 17 | constexpr auto CONNECTION_ETHERNET = NetworkChangeNotifier::CONNECTION_ETHERNET; |
| 18 | constexpr auto CONNECTION_WIFI = NetworkChangeNotifier::CONNECTION_WIFI; |
rtenneti | 041b299 | 2015-02-23 23:03:28 | [diff] [blame] | 19 | |
rtenneti | 041b299 | 2015-02-23 23:03:28 | [diff] [blame] | 20 | class NetworkConnectionTest : public testing::Test { |
| 21 | protected: |
rch | d6163f3 | 2017-01-30 23:50:38 | [diff] [blame] | 22 | NetworkConnectionTest() |
| 23 | : notifier_(scoped_notifier_.mock_network_change_notifier()) {} |
rtenneti | 041b299 | 2015-02-23 23:03:28 | [diff] [blame] | 24 | |
rch | d6163f3 | 2017-01-30 23:50:38 | [diff] [blame] | 25 | ScopedMockNetworkChangeNotifier scoped_notifier_; |
| 26 | MockNetworkChangeNotifier* notifier_; |
rtenneti | 041b299 | 2015-02-23 23:03:28 | [diff] [blame] | 27 | }; |
| 28 | |
rch | d6163f3 | 2017-01-30 23:50:38 | [diff] [blame] | 29 | TEST_F(NetworkConnectionTest, Connection2G) { |
| 30 | notifier_->SetConnectionType(CONNECTION_2G); |
rtenneti | 041b299 | 2015-02-23 23:03:28 | [diff] [blame] | 31 | |
rch | d6163f3 | 2017-01-30 23:50:38 | [diff] [blame] | 32 | NetworkConnection network_connection; |
| 33 | EXPECT_EQ(CONNECTION_2G, network_connection.connection_type()); |
| 34 | const char* description = network_connection.connection_description(); |
| 35 | EXPECT_EQ(NetworkChangeNotifier::ConnectionTypeToString(CONNECTION_2G), |
| 36 | description); |
rtenneti | 041b299 | 2015-02-23 23:03:28 | [diff] [blame] | 37 | } |
| 38 | |
rch | d6163f3 | 2017-01-30 23:50:38 | [diff] [blame] | 39 | TEST_F(NetworkConnectionTest, Connection3G) { |
| 40 | notifier_->SetConnectionType(CONNECTION_3G); |
| 41 | |
| 42 | NetworkConnection network_connection; |
| 43 | EXPECT_EQ(CONNECTION_3G, network_connection.connection_type()); |
| 44 | const char* description = network_connection.connection_description(); |
| 45 | EXPECT_EQ(NetworkChangeNotifier::ConnectionTypeToString(CONNECTION_3G), |
| 46 | description); |
| 47 | } |
| 48 | |
| 49 | TEST_F(NetworkConnectionTest, ConnectionEthnernet) { |
| 50 | notifier_->SetConnectionType(CONNECTION_ETHERNET); |
| 51 | |
| 52 | NetworkConnection network_connection; |
| 53 | EXPECT_EQ(CONNECTION_ETHERNET, network_connection.connection_type()); |
| 54 | const char* description = network_connection.connection_description(); |
| 55 | EXPECT_EQ(NetworkChangeNotifier::ConnectionTypeToString(CONNECTION_ETHERNET), |
| 56 | description); |
| 57 | } |
| 58 | |
| 59 | TEST_F(NetworkConnectionTest, ConnectionWifi) { |
| 60 | notifier_->SetConnectionType(CONNECTION_WIFI); |
| 61 | |
| 62 | NetworkConnection network_connection; |
| 63 | EXPECT_EQ(CONNECTION_WIFI, network_connection.connection_type()); |
| 64 | const char* description = network_connection.connection_description(); |
| 65 | // On some platforms, the description for wifi will be more detailed |
| 66 | // than what is returned by NetworkChangeNotifier::ConnectionTypeToString. |
| 67 | EXPECT_NE(nullptr, description); |
| 68 | } |
| 69 | |
| 70 | TEST_F(NetworkConnectionTest, ConnectionChange) { |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame^] | 71 | base::test::TaskEnvironment task_environment; |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 72 | |
rch | d6163f3 | 2017-01-30 23:50:38 | [diff] [blame] | 73 | notifier_->SetConnectionType(CONNECTION_2G); |
| 74 | |
| 75 | NetworkConnection network_connection; |
| 76 | const char* description_2g = network_connection.connection_description(); |
| 77 | |
| 78 | notifier_->SetConnectionType(CONNECTION_3G); |
| 79 | NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); |
| 80 | // Spin the message loop so the notification is delivered. |
| 81 | base::RunLoop().RunUntilIdle(); |
| 82 | EXPECT_EQ(CONNECTION_3G, network_connection.connection_type()); |
| 83 | const char* description_3g = network_connection.connection_description(); |
| 84 | |
| 85 | NetworkChangeNotifier::NotifyObserversOfConnectionTypeChangeForTests( |
| 86 | CONNECTION_ETHERNET); |
| 87 | // Spin the message loop so the notification is delivered. |
| 88 | base::RunLoop().RunUntilIdle(); |
| 89 | EXPECT_EQ(CONNECTION_ETHERNET, network_connection.connection_type()); |
| 90 | const char* description_ethernet = |
| 91 | network_connection.connection_description(); |
| 92 | |
| 93 | NetworkChangeNotifier::NotifyObserversOfConnectionTypeChangeForTests( |
| 94 | CONNECTION_WIFI); |
| 95 | EXPECT_NE(nullptr, network_connection.connection_description()); |
| 96 | EXPECT_EQ(NetworkChangeNotifier::ConnectionTypeToString(CONNECTION_2G), |
| 97 | description_2g); |
| 98 | EXPECT_EQ(NetworkChangeNotifier::ConnectionTypeToString(CONNECTION_3G), |
| 99 | description_3g); |
| 100 | EXPECT_EQ(NetworkChangeNotifier::ConnectionTypeToString(CONNECTION_ETHERNET), |
| 101 | description_ethernet); |
rtenneti | 041b299 | 2015-02-23 23:03:28 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | } // namespace test |
| 105 | } // namespace net |