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 
5 #include "net/quic/network_connection.h"
6 
7 #include "base/run_loop.h"
8 #include "net/base/mock_network_change_notifier.h"
9 #include "net/test/test_with_task_environment.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 
12 namespace net {
13 namespace test {
14 
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;
19 
20 // TestWithTaskEnvironment needed to instantiate a
21 // net::NetworkChangeNotifier::NetworkChangeNotifier via
22 // ScopedMockNetworkChangeNotifier.
23 class NetworkConnectionTest : public TestWithTaskEnvironment {
24  protected:
NetworkConnectionTest()25   NetworkConnectionTest()
26       : notifier_(scoped_notifier_.mock_network_change_notifier()) {}
27 
28   ScopedMockNetworkChangeNotifier scoped_notifier_;
29   MockNetworkChangeNotifier* notifier_;
30 };
31 
TEST_F(NetworkConnectionTest,Connection2G)32 TEST_F(NetworkConnectionTest, Connection2G) {
33   notifier_->SetConnectionType(CONNECTION_2G);
34 
35   NetworkConnection network_connection;
36   EXPECT_EQ(CONNECTION_2G, network_connection.connection_type());
37   const char* description = network_connection.connection_description();
38   EXPECT_EQ(NetworkChangeNotifier::ConnectionTypeToString(CONNECTION_2G),
39             description);
40 }
41 
TEST_F(NetworkConnectionTest,Connection3G)42 TEST_F(NetworkConnectionTest, Connection3G) {
43   notifier_->SetConnectionType(CONNECTION_3G);
44 
45   NetworkConnection network_connection;
46   EXPECT_EQ(CONNECTION_3G, network_connection.connection_type());
47   const char* description = network_connection.connection_description();
48   EXPECT_EQ(NetworkChangeNotifier::ConnectionTypeToString(CONNECTION_3G),
49             description);
50 }
51 
TEST_F(NetworkConnectionTest,ConnectionEthnernet)52 TEST_F(NetworkConnectionTest, ConnectionEthnernet) {
53   notifier_->SetConnectionType(CONNECTION_ETHERNET);
54 
55   NetworkConnection network_connection;
56   EXPECT_EQ(CONNECTION_ETHERNET, network_connection.connection_type());
57   const char* description = network_connection.connection_description();
58   EXPECT_EQ(NetworkChangeNotifier::ConnectionTypeToString(CONNECTION_ETHERNET),
59             description);
60 }
61 
TEST_F(NetworkConnectionTest,ConnectionWifi)62 TEST_F(NetworkConnectionTest, ConnectionWifi) {
63   notifier_->SetConnectionType(CONNECTION_WIFI);
64 
65   NetworkConnection network_connection;
66   EXPECT_EQ(CONNECTION_WIFI, network_connection.connection_type());
67   const char* description = network_connection.connection_description();
68   // On some platforms, the description for wifi will be more detailed
69   // than what is returned by NetworkChangeNotifier::ConnectionTypeToString.
70   EXPECT_NE(nullptr, description);
71 }
72 
TEST_F(NetworkConnectionTest,ConnectionChange)73 TEST_F(NetworkConnectionTest, ConnectionChange) {
74   notifier_->SetConnectionType(CONNECTION_2G);
75 
76   NetworkConnection network_connection;
77   const char* description_2g = network_connection.connection_description();
78 
79   notifier_->SetConnectionType(CONNECTION_3G);
80   NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
81   // Spin the message loop so the notification is delivered.
82   base::RunLoop().RunUntilIdle();
83   EXPECT_EQ(CONNECTION_3G, network_connection.connection_type());
84   const char* description_3g = network_connection.connection_description();
85 
86   NetworkChangeNotifier::NotifyObserversOfConnectionTypeChangeForTests(
87       CONNECTION_ETHERNET);
88   // Spin the message loop so the notification is delivered.
89   base::RunLoop().RunUntilIdle();
90   EXPECT_EQ(CONNECTION_ETHERNET, network_connection.connection_type());
91   const char* description_ethernet =
92       network_connection.connection_description();
93 
94   NetworkChangeNotifier::NotifyObserversOfConnectionTypeChangeForTests(
95       CONNECTION_WIFI);
96   EXPECT_NE(nullptr, network_connection.connection_description());
97   EXPECT_EQ(NetworkChangeNotifier::ConnectionTypeToString(CONNECTION_2G),
98             description_2g);
99   EXPECT_EQ(NetworkChangeNotifier::ConnectionTypeToString(CONNECTION_3G),
100             description_3g);
101   EXPECT_EQ(NetworkChangeNotifier::ConnectionTypeToString(CONNECTION_ETHERNET),
102             description_ethernet);
103 }
104 
105 }  // namespace test
106 }  // namespace net
107