forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnetwork_connection_unittest.cc
105 lines (84 loc) · 4 KB
/
network_connection_unittest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/quic/network_connection.h"
#include "base/run_loop.h"
#include "base/test/scoped_task_environment.h"
#include "net/base/mock_network_change_notifier.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace net {
namespace test {
constexpr auto CONNECTION_3G = NetworkChangeNotifier::CONNECTION_3G;
constexpr auto CONNECTION_2G = NetworkChangeNotifier::CONNECTION_2G;
constexpr auto CONNECTION_ETHERNET = NetworkChangeNotifier::CONNECTION_ETHERNET;
constexpr auto CONNECTION_WIFI = NetworkChangeNotifier::CONNECTION_WIFI;
class NetworkConnectionTest : public testing::Test {
protected:
NetworkConnectionTest()
: notifier_(scoped_notifier_.mock_network_change_notifier()) {}
ScopedMockNetworkChangeNotifier scoped_notifier_;
MockNetworkChangeNotifier* notifier_;
};
TEST_F(NetworkConnectionTest, Connection2G) {
notifier_->SetConnectionType(CONNECTION_2G);
NetworkConnection network_connection;
EXPECT_EQ(CONNECTION_2G, network_connection.connection_type());
const char* description = network_connection.connection_description();
EXPECT_EQ(NetworkChangeNotifier::ConnectionTypeToString(CONNECTION_2G),
description);
}
TEST_F(NetworkConnectionTest, Connection3G) {
notifier_->SetConnectionType(CONNECTION_3G);
NetworkConnection network_connection;
EXPECT_EQ(CONNECTION_3G, network_connection.connection_type());
const char* description = network_connection.connection_description();
EXPECT_EQ(NetworkChangeNotifier::ConnectionTypeToString(CONNECTION_3G),
description);
}
TEST_F(NetworkConnectionTest, ConnectionEthnernet) {
notifier_->SetConnectionType(CONNECTION_ETHERNET);
NetworkConnection network_connection;
EXPECT_EQ(CONNECTION_ETHERNET, network_connection.connection_type());
const char* description = network_connection.connection_description();
EXPECT_EQ(NetworkChangeNotifier::ConnectionTypeToString(CONNECTION_ETHERNET),
description);
}
TEST_F(NetworkConnectionTest, ConnectionWifi) {
notifier_->SetConnectionType(CONNECTION_WIFI);
NetworkConnection network_connection;
EXPECT_EQ(CONNECTION_WIFI, network_connection.connection_type());
const char* description = network_connection.connection_description();
// On some platforms, the description for wifi will be more detailed
// than what is returned by NetworkChangeNotifier::ConnectionTypeToString.
EXPECT_NE(nullptr, description);
}
TEST_F(NetworkConnectionTest, ConnectionChange) {
base::test::ScopedTaskEnvironment scoped_task_environment;
notifier_->SetConnectionType(CONNECTION_2G);
NetworkConnection network_connection;
const char* description_2g = network_connection.connection_description();
notifier_->SetConnectionType(CONNECTION_3G);
NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
// Spin the message loop so the notification is delivered.
base::RunLoop().RunUntilIdle();
EXPECT_EQ(CONNECTION_3G, network_connection.connection_type());
const char* description_3g = network_connection.connection_description();
NetworkChangeNotifier::NotifyObserversOfConnectionTypeChangeForTests(
CONNECTION_ETHERNET);
// Spin the message loop so the notification is delivered.
base::RunLoop().RunUntilIdle();
EXPECT_EQ(CONNECTION_ETHERNET, network_connection.connection_type());
const char* description_ethernet =
network_connection.connection_description();
NetworkChangeNotifier::NotifyObserversOfConnectionTypeChangeForTests(
CONNECTION_WIFI);
EXPECT_NE(nullptr, network_connection.connection_description());
EXPECT_EQ(NetworkChangeNotifier::ConnectionTypeToString(CONNECTION_2G),
description_2g);
EXPECT_EQ(NetworkChangeNotifier::ConnectionTypeToString(CONNECTION_3G),
description_3g);
EXPECT_EQ(NetworkChangeNotifier::ConnectionTypeToString(CONNECTION_ETHERNET),
description_ethernet);
}
} // namespace test
} // namespace net