blob: a73c95ff8c1dd08146d23f90c0331755176d3740 [file] [log] [blame]
rtenneti041b2992015-02-23 23:03:281// 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 Hamiltona3ee93a72018-08-01 22:03:085#include "net/quic/network_connection.h"
rtenneti041b2992015-02-23 23:03:286
rchd6163f32017-01-30 23:50:387#include "base/run_loop.h"
Bence Béky98447b12018-05-08 03:14:018#include "base/test/scoped_task_environment.h"
mgershaf9a9232017-04-13 20:19:039#include "net/base/mock_network_change_notifier.h"
rtenneti041b2992015-02-23 23:03:2810#include "testing/gtest/include/gtest/gtest.h"
11
12namespace net {
13namespace test {
14
rchd6163f32017-01-30 23:50:3815constexpr auto CONNECTION_3G = NetworkChangeNotifier::CONNECTION_3G;
16constexpr auto CONNECTION_2G = NetworkChangeNotifier::CONNECTION_2G;
17constexpr auto CONNECTION_ETHERNET = NetworkChangeNotifier::CONNECTION_ETHERNET;
18constexpr auto CONNECTION_WIFI = NetworkChangeNotifier::CONNECTION_WIFI;
rtenneti041b2992015-02-23 23:03:2819
rtenneti041b2992015-02-23 23:03:2820class NetworkConnectionTest : public testing::Test {
21 protected:
rchd6163f32017-01-30 23:50:3822 NetworkConnectionTest()
23 : notifier_(scoped_notifier_.mock_network_change_notifier()) {}
rtenneti041b2992015-02-23 23:03:2824
rchd6163f32017-01-30 23:50:3825 ScopedMockNetworkChangeNotifier scoped_notifier_;
26 MockNetworkChangeNotifier* notifier_;
rtenneti041b2992015-02-23 23:03:2827};
28
rchd6163f32017-01-30 23:50:3829TEST_F(NetworkConnectionTest, Connection2G) {
30 notifier_->SetConnectionType(CONNECTION_2G);
rtenneti041b2992015-02-23 23:03:2831
rchd6163f32017-01-30 23:50:3832 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);
rtenneti041b2992015-02-23 23:03:2837}
38
rchd6163f32017-01-30 23:50:3839TEST_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
49TEST_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
59TEST_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
70TEST_F(NetworkConnectionTest, ConnectionChange) {
Gabriel Charette694c3c332019-08-19 14:53:0571 base::test::TaskEnvironment task_environment;
Bence Béky98447b12018-05-08 03:14:0172
rchd6163f32017-01-30 23:50:3873 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);
rtenneti041b2992015-02-23 23:03:28102}
103
104} // namespace test
105} // namespace net