OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 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 "chromeos/network/connection_change_notifier.h" | |
6 | |
7 #include <string> | |
8 | |
9 #include "base/basictypes.h" | |
10 #include "chromeos/network/network_state.h" | |
11 #include "net/base/network_change_notifier.h" | |
12 #include "testing/gtest/include/gtest/gtest.h" | |
13 #include "third_party/cros_system_api/dbus/service_constants.h" | |
14 | |
15 namespace chromeos { | |
16 | |
17 using net::NetworkChangeNotifier; | |
18 | |
19 TEST(ConnectionChangeNotifierTest, ConnectionTypeFromShill) { | |
20 struct TypeMapping { | |
21 const char* shill_type; | |
22 const char* technology; | |
23 NetworkChangeNotifier::ConnectionType connection_type; | |
24 } type_mappings[] = { | |
25 {flimflam::kTypeEthernet, "", NetworkChangeNotifier::CONNECTION_ETHERNET}, | |
26 {flimflam::kTypeWifi, "", NetworkChangeNotifier::CONNECTION_WIFI}, | |
27 {flimflam::kTypeWimax, "", NetworkChangeNotifier::CONNECTION_4G}, | |
28 {"unknown type", "unknown technology", | |
29 NetworkChangeNotifier::CONNECTION_UNKNOWN}, | |
30 {flimflam::kTypeCellular, flimflam::kNetworkTechnology1Xrtt, | |
31 NetworkChangeNotifier::CONNECTION_2G}, | |
32 {flimflam::kTypeCellular, flimflam::kNetworkTechnologyGprs, | |
33 NetworkChangeNotifier::CONNECTION_2G}, | |
34 {flimflam::kTypeCellular, flimflam::kNetworkTechnologyEdge, | |
35 NetworkChangeNotifier::CONNECTION_2G}, | |
36 {flimflam::kTypeCellular, flimflam::kNetworkTechnologyEvdo, | |
37 NetworkChangeNotifier::CONNECTION_3G}, | |
38 {flimflam::kTypeCellular, flimflam::kNetworkTechnologyGsm, | |
39 NetworkChangeNotifier::CONNECTION_3G}, | |
40 {flimflam::kTypeCellular, flimflam::kNetworkTechnologyUmts, | |
41 NetworkChangeNotifier::CONNECTION_3G}, | |
42 {flimflam::kTypeCellular, flimflam::kNetworkTechnologyHspa, | |
43 NetworkChangeNotifier::CONNECTION_3G}, | |
44 {flimflam::kTypeCellular, flimflam::kNetworkTechnologyHspaPlus, | |
45 NetworkChangeNotifier::CONNECTION_4G}, | |
46 {flimflam::kTypeCellular, flimflam::kNetworkTechnologyLte, | |
47 NetworkChangeNotifier::CONNECTION_4G}, | |
48 {flimflam::kTypeCellular, flimflam::kNetworkTechnologyLteAdvanced, | |
49 NetworkChangeNotifier::CONNECTION_4G}, | |
50 {flimflam::kTypeCellular, "unknown technology", | |
51 NetworkChangeNotifier::CONNECTION_2G} | |
52 }; | |
53 | |
54 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(type_mappings); ++i) { | |
Greg Spencer (Chromium)
2012/12/10 22:54:55
Why can't you use arraysize() here?
gauravsh
2012/12/11 00:39:18
arraysize() doesn't work on anonymous/structs defi
| |
55 NetworkChangeNotifier::ConnectionType type = | |
56 ConnectionChangeNotifier::ConnectionTypeFromShill( | |
57 type_mappings[i].shill_type, type_mappings[i].technology); | |
58 EXPECT_EQ(type_mappings[i].connection_type, type); | |
59 } | |
60 } | |
61 | |
62 class ConnectionChangeNotifierUpdateTest : public testing::Test { | |
63 protected: | |
64 ConnectionChangeNotifierUpdateTest() : active_network_("") {} | |
65 virtual ~ConnectionChangeNotifierUpdateTest() {} | |
66 | |
67 void SetNotifierState(NetworkChangeNotifier::ConnectionType type, | |
68 std::string service_path, | |
69 std::string ip_address) { | |
70 notifier_.ip_address_ = ip_address; | |
71 notifier_.service_path_ = service_path; | |
72 notifier_.connection_type_ = type; | |
73 } | |
74 | |
75 void VerifyNotifierState(NetworkChangeNotifier::ConnectionType expected_type, | |
76 std::string expected_service_path, | |
77 std::string expected_ip_address) { | |
78 EXPECT_EQ(expected_type, notifier_.connection_type_); | |
79 EXPECT_EQ(expected_ip_address, notifier_.ip_address_); | |
80 EXPECT_EQ(expected_service_path, notifier_.service_path_); | |
81 } | |
82 | |
83 // A helper for initializing an network state with given values. Caller | |
84 // owns the returned pointer and must delete it. | |
85 void SetActiveNetworkState(bool is_connected, | |
86 std::string type, | |
87 std::string technology, | |
88 std::string service_path, | |
89 std::string ip_address) { | |
90 if (is_connected) | |
91 active_network_.state_ = flimflam::kStateOnline; | |
92 else | |
93 active_network_.state_ = flimflam::kStateConfiguration; | |
94 active_network_.type_ = type; | |
95 active_network_.technology_ = technology; | |
96 active_network_.path_ = service_path; | |
97 active_network_.ip_address_ = ip_address; | |
98 } | |
99 | |
100 // Process an active network update based on the state of |active_network_|. | |
101 void ProcessActiveNetworkUpdate(bool* type_changed, | |
102 bool* ip_changed, | |
103 bool* dns_changed) { | |
104 notifier_.UpdateActiveNetwork(&active_network_, type_changed, ip_changed, | |
105 dns_changed); | |
106 } | |
107 | |
108 private: | |
109 NetworkState active_network_; | |
110 ConnectionChangeNotifier notifier_; | |
111 }; | |
112 | |
113 TEST_F(ConnectionChangeNotifierUpdateTest, UpdateActiveNetworkOffline) { | |
114 // Test that Online to Offline transitions are correctly handled. | |
115 SetNotifierState(NetworkChangeNotifier::CONNECTION_ETHERNET, "/service/1", | |
116 "192.168.1.1"); | |
117 SetActiveNetworkState(false, // offline. | |
118 flimflam::kTypeEthernet, "", "/service/1", ""); | |
119 bool type_changed = false, ip_changed = false, dns_changed = false; | |
120 ProcessActiveNetworkUpdate(&type_changed, &ip_changed, &dns_changed); | |
121 VerifyNotifierState(NetworkChangeNotifier::CONNECTION_NONE, "", ""); | |
122 EXPECT_TRUE(type_changed); | |
123 EXPECT_TRUE(ip_changed); | |
124 EXPECT_TRUE(dns_changed); | |
125 } | |
126 | |
127 TEST_F(ConnectionChangeNotifierUpdateTest, UpdateActiveNetworkOnline) { | |
128 // Test that Offline to Online transitions are correctly handled. | |
129 SetNotifierState(NetworkChangeNotifier::CONNECTION_NONE, "", ""); | |
130 | |
131 SetActiveNetworkState(false, // offline. | |
132 flimflam::kTypeEthernet, "", | |
133 "192.168.0.1", "/service/1"); | |
134 bool type_changed = false, ip_changed = false, dns_changed = false; | |
135 ProcessActiveNetworkUpdate(&type_changed, &ip_changed, &dns_changed); | |
136 // If the new active network is still offline, nothing should have changed. | |
137 VerifyNotifierState(NetworkChangeNotifier::CONNECTION_NONE, "", ""); | |
138 EXPECT_FALSE(type_changed); | |
139 EXPECT_FALSE(ip_changed); | |
140 EXPECT_FALSE(dns_changed); | |
141 | |
142 SetActiveNetworkState(true, // online. | |
143 flimflam::kTypeEthernet, "", "/service/1", | |
144 "192.168.0.1"); | |
145 ProcessActiveNetworkUpdate(&type_changed, &ip_changed, &dns_changed); | |
146 // Now the new active network is online, so this should trigger a notifier | |
147 // state change. | |
148 VerifyNotifierState(NetworkChangeNotifier::CONNECTION_ETHERNET, "/service/1", | |
149 "192.168.0.1"); | |
150 EXPECT_TRUE(type_changed); | |
151 EXPECT_TRUE(ip_changed); | |
152 EXPECT_TRUE(dns_changed); | |
153 } | |
154 | |
155 TEST_F(ConnectionChangeNotifierUpdateTest, UpdateActiveNetworkChanged) { | |
156 // Test that Online to Online transisions (active network changes) are | |
157 // correctly handled. | |
158 SetNotifierState(NetworkChangeNotifier::CONNECTION_ETHERNET, "/service/1", | |
159 "192.168.1.1"); | |
160 | |
161 SetActiveNetworkState(true, // online. | |
162 flimflam::kTypeWifi, "", "/service/2", "192.168.1.2"); | |
163 bool type_changed = false, ip_changed = false, dns_changed = false; | |
164 ProcessActiveNetworkUpdate(&type_changed, &ip_changed, &dns_changed); | |
165 VerifyNotifierState(NetworkChangeNotifier::CONNECTION_WIFI, "/service/2", | |
166 "192.168.1.2" ); | |
167 EXPECT_TRUE(type_changed); | |
168 EXPECT_TRUE(ip_changed); | |
169 EXPECT_TRUE(dns_changed); | |
170 | |
171 SetActiveNetworkState(true, // online. | |
172 flimflam::kTypeWifi, "", "/service/3", "192.168.1.2"); | |
173 ProcessActiveNetworkUpdate(&type_changed, &ip_changed, &dns_changed); | |
174 VerifyNotifierState(NetworkChangeNotifier::CONNECTION_WIFI, "/service/3", | |
175 "192.168.1.2" ); | |
176 EXPECT_FALSE(type_changed); | |
177 // A service path change (even with a corresponding IP change) should still | |
178 // trigger an IP address update to observers. | |
179 EXPECT_TRUE(ip_changed); | |
180 EXPECT_TRUE(dns_changed); | |
181 } | |
182 | |
183 } // namespace chromeos | |
OLD | NEW |