OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chromeos/network/network_change_notifier_chromeos.h" | 5 #include "chromeos/network/network_change_notifier_chromeos.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "chromeos/network/network_change_notifier_factory_chromeos.h" | 10 #include "chromeos/network/network_change_notifier_factory_chromeos.h" |
11 #include "chromeos/network/network_state.h" | 11 #include "chromeos/network/network_state.h" |
12 #include "net/base/network_change_notifier.h" | 12 #include "net/base/network_change_notifier.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
14 #include "third_party/cros_system_api/dbus/service_constants.h" | 14 #include "third_party/cros_system_api/dbus/service_constants.h" |
15 | 15 |
16 namespace chromeos { | 16 namespace chromeos { |
pneubeck (no reviews)
2013/03/28 10:37:54
Can we please take one step back and think about w
gauravsh
2013/03/28 18:48:54
The # of lines for the test compared to the lines
stevenjb
2013/03/28 19:27:13
I tend to agree that lines of test code is not a g
| |
17 | 17 |
18 namespace { | |
19 | |
20 const char kDnsServers1[] = "192.168.0.1,192.168.0.2"; | |
21 const char kDnsServers2[] = "192.168.3.1,192.168.3.2"; | |
22 const char kIpAddress1[] = "192.168.1.1"; | |
23 const char kIpAddress2[] = "192.168.1.2"; | |
24 const char kService1[] = "/service/1"; | |
25 const char kService2[] = "/service/2"; | |
26 const char kService3[] = "/service/3"; | |
27 | |
28 } | |
29 | |
18 using net::NetworkChangeNotifier; | 30 using net::NetworkChangeNotifier; |
19 | 31 |
20 TEST(NetworkChangeNotifierChromeosTest, ConnectionTypeFromShill) { | 32 TEST(NetworkChangeNotifierChromeosTest, ConnectionTypeFromShill) { |
21 struct TypeMapping { | 33 struct TypeMapping { |
22 const char* shill_type; | 34 const char* shill_type; |
23 const char* technology; | 35 const char* technology; |
24 NetworkChangeNotifier::ConnectionType connection_type; | 36 NetworkChangeNotifier::ConnectionType connection_type; |
25 }; | 37 }; |
26 TypeMapping type_mappings[] = { | 38 TypeMapping type_mappings[] = { |
27 { flimflam::kTypeEthernet, "", NetworkChangeNotifier::CONNECTION_ETHERNET }, | 39 { flimflam::kTypeEthernet, "", NetworkChangeNotifier::CONNECTION_ETHERNET }, |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
62 } | 74 } |
63 | 75 |
64 class NetworkChangeNotifierChromeosUpdateTest : public testing::Test { | 76 class NetworkChangeNotifierChromeosUpdateTest : public testing::Test { |
65 protected: | 77 protected: |
66 NetworkChangeNotifierChromeosUpdateTest() : default_network_("") { | 78 NetworkChangeNotifierChromeosUpdateTest() : default_network_("") { |
67 } | 79 } |
68 virtual ~NetworkChangeNotifierChromeosUpdateTest() {} | 80 virtual ~NetworkChangeNotifierChromeosUpdateTest() {} |
69 | 81 |
70 void SetNotifierState(NetworkChangeNotifier::ConnectionType type, | 82 void SetNotifierState(NetworkChangeNotifier::ConnectionType type, |
71 std::string service_path, | 83 std::string service_path, |
72 std::string ip_address) { | 84 std::string ip_address, |
85 std::string dns_servers) { | |
86 notifier_.connection_type_ = type; | |
87 notifier_.service_path_ = service_path; | |
73 notifier_.ip_address_ = ip_address; | 88 notifier_.ip_address_ = ip_address; |
74 notifier_.service_path_ = service_path; | 89 notifier_.dns_servers_ = dns_servers; |
75 notifier_.connection_type_ = type; | |
76 } | 90 } |
77 | 91 |
78 void VerifyNotifierState(NetworkChangeNotifier::ConnectionType expected_type, | 92 void VerifyNotifierState(NetworkChangeNotifier::ConnectionType expected_type, |
79 std::string expected_service_path, | 93 std::string expected_service_path, |
80 std::string expected_ip_address) { | 94 std::string expected_ip_address, |
95 std::string expected_dns_servers) { | |
81 EXPECT_EQ(expected_type, notifier_.connection_type_); | 96 EXPECT_EQ(expected_type, notifier_.connection_type_); |
97 EXPECT_EQ(expected_service_path, notifier_.service_path_); | |
82 EXPECT_EQ(expected_ip_address, notifier_.ip_address_); | 98 EXPECT_EQ(expected_ip_address, notifier_.ip_address_); |
83 EXPECT_EQ(expected_service_path, notifier_.service_path_); | 99 EXPECT_EQ(expected_dns_servers, notifier_.dns_servers_); |
84 } | 100 } |
85 | 101 |
86 // Sets the default network state used for notifier updates. | 102 // Sets the default network state used for notifier updates. |
87 void SetDefaultNetworkState(bool is_connected, | 103 void SetDefaultNetworkState(bool is_connected, |
88 std::string type, | 104 std::string type, |
89 std::string technology, | 105 std::string technology, |
90 std::string service_path, | 106 std::string service_path, |
91 std::string ip_address) { | 107 std::string ip_address, |
108 std::string dns_servers) { | |
92 if (is_connected) | 109 if (is_connected) |
93 default_network_.connection_state_ = flimflam::kStateOnline; | 110 default_network_.connection_state_ = flimflam::kStateOnline; |
94 else | 111 else |
95 default_network_.connection_state_ = flimflam::kStateConfiguration; | 112 default_network_.connection_state_ = flimflam::kStateConfiguration; |
96 default_network_.type_ = type; | 113 default_network_.type_ = type; |
97 default_network_.technology_ = technology; | 114 default_network_.technology_ = technology; |
98 default_network_.path_ = service_path; | 115 default_network_.path_ = service_path; |
99 default_network_.ip_address_ = ip_address; | 116 default_network_.ip_address_ = ip_address; |
117 default_network_.dns_servers_ = dns_servers; | |
100 } | 118 } |
101 | 119 |
102 // Process an default network update based on the state of |default_network_|. | 120 // Process an default network update based on the state of |default_network_|. |
103 void ProcessDefaultNetworkUpdate(bool* type_changed, | 121 void ProcessDefaultNetworkUpdate(bool* type_changed, |
104 bool* ip_changed, | 122 bool* ip_changed, |
105 bool* dns_changed) { | 123 bool* dns_changed) { |
106 notifier_.UpdateState(&default_network_, type_changed, ip_changed, | 124 notifier_.UpdateState(&default_network_, type_changed, ip_changed, |
107 dns_changed); | 125 dns_changed); |
108 } | 126 } |
109 | 127 |
110 private: | 128 private: |
111 NetworkState default_network_; | 129 NetworkState default_network_; |
112 NetworkChangeNotifierChromeos notifier_; | 130 NetworkChangeNotifierChromeos notifier_; |
113 }; | 131 }; |
114 | 132 |
115 TEST_F(NetworkChangeNotifierChromeosUpdateTest, UpdateDefaultNetworkOffline) { | 133 TEST_F(NetworkChangeNotifierChromeosUpdateTest, UpdateDefaultNetworkOffline) { |
116 // Test that Online to Offline transitions are correctly handled. | 134 // Test that Online to Offline transitions are correctly handled. |
117 SetNotifierState(NetworkChangeNotifier::CONNECTION_ETHERNET, "/service/1", | 135 SetNotifierState(NetworkChangeNotifier::CONNECTION_ETHERNET, kService1, |
118 "192.168.1.1"); | 136 kIpAddress1, kDnsServers1); |
119 SetDefaultNetworkState(false, // offline. | 137 SetDefaultNetworkState(false, // offline. |
120 flimflam::kTypeEthernet, "", "/service/1", ""); | 138 flimflam::kTypeEthernet, "", kService1, "", ""); |
121 bool type_changed = false, ip_changed = false, dns_changed = false; | 139 bool type_changed = false, ip_changed = false, dns_changed = false; |
122 ProcessDefaultNetworkUpdate(&type_changed, &ip_changed, &dns_changed); | 140 ProcessDefaultNetworkUpdate(&type_changed, &ip_changed, &dns_changed); |
123 VerifyNotifierState(NetworkChangeNotifier::CONNECTION_NONE, "", ""); | 141 VerifyNotifierState(NetworkChangeNotifier::CONNECTION_NONE, "", "", ""); |
124 EXPECT_TRUE(type_changed); | 142 EXPECT_TRUE(type_changed); |
125 EXPECT_TRUE(ip_changed); | 143 EXPECT_TRUE(ip_changed); |
126 EXPECT_TRUE(dns_changed); | 144 EXPECT_TRUE(dns_changed); |
127 } | 145 } |
128 | 146 |
129 TEST_F(NetworkChangeNotifierChromeosUpdateTest, UpdateDefaultNetworkOnline) { | 147 TEST_F(NetworkChangeNotifierChromeosUpdateTest, UpdateDefaultNetworkOnline) { |
130 // Test that Offline to Online transitions are correctly handled. | 148 // Test that Offline to Online transitions are correctly handled. |
131 SetNotifierState(NetworkChangeNotifier::CONNECTION_NONE, "", ""); | 149 SetNotifierState(NetworkChangeNotifier::CONNECTION_NONE, "", "", ""); |
132 | 150 |
133 SetDefaultNetworkState(false, // offline. | 151 SetDefaultNetworkState(false, // offline. |
134 flimflam::kTypeEthernet, "", | 152 flimflam::kTypeEthernet, "", |
135 "192.168.0.1", "/service/1"); | 153 kService1, kIpAddress1, |
154 kDnsServers1); | |
136 bool type_changed = false, ip_changed = false, dns_changed = false; | 155 bool type_changed = false, ip_changed = false, dns_changed = false; |
137 ProcessDefaultNetworkUpdate(&type_changed, &ip_changed, &dns_changed); | 156 ProcessDefaultNetworkUpdate(&type_changed, &ip_changed, &dns_changed); |
138 // If the new default network is still offline, nothing should have changed. | 157 // If the new default network is still offline, nothing should have changed. |
139 VerifyNotifierState(NetworkChangeNotifier::CONNECTION_NONE, "", ""); | 158 VerifyNotifierState(NetworkChangeNotifier::CONNECTION_NONE, "", "", ""); |
140 EXPECT_FALSE(type_changed); | 159 EXPECT_FALSE(type_changed); |
141 EXPECT_FALSE(ip_changed); | 160 EXPECT_FALSE(ip_changed); |
142 EXPECT_FALSE(dns_changed); | 161 EXPECT_FALSE(dns_changed); |
143 | 162 |
144 SetDefaultNetworkState(true, // online. | 163 SetDefaultNetworkState(true, // online. |
145 flimflam::kTypeEthernet, "", "/service/1", | 164 flimflam::kTypeEthernet, "", |
146 "192.168.0.1"); | 165 kService1, kIpAddress1, kDnsServers1); |
147 ProcessDefaultNetworkUpdate(&type_changed, &ip_changed, &dns_changed); | 166 ProcessDefaultNetworkUpdate(&type_changed, &ip_changed, &dns_changed); |
148 // Now the new default network is online, so this should trigger a notifier | 167 // Now the new default network is online, so this should trigger a notifier |
149 // state change. | 168 // state change. |
150 VerifyNotifierState(NetworkChangeNotifier::CONNECTION_ETHERNET, "/service/1", | 169 VerifyNotifierState(NetworkChangeNotifier::CONNECTION_ETHERNET, kService1, |
151 "192.168.0.1"); | 170 kIpAddress1, kDnsServers1); |
152 EXPECT_TRUE(type_changed); | 171 EXPECT_TRUE(type_changed); |
153 EXPECT_TRUE(ip_changed); | 172 EXPECT_TRUE(ip_changed); |
154 EXPECT_TRUE(dns_changed); | 173 EXPECT_TRUE(dns_changed); |
155 } | 174 } |
156 | 175 |
157 TEST_F(NetworkChangeNotifierChromeosUpdateTest, UpdateDefaultNetworkChanged) { | 176 TEST_F(NetworkChangeNotifierChromeosUpdateTest, UpdateDefaultNetworkChanged) { |
158 // Test that Online to Online transitions (default network changes) are | 177 // Test that Online to Online transitions (default network changes) are |
159 // correctly handled. | 178 // correctly handled. |
160 SetNotifierState(NetworkChangeNotifier::CONNECTION_ETHERNET, "/service/1", | 179 SetNotifierState(NetworkChangeNotifier::CONNECTION_ETHERNET, |
161 "192.168.1.1"); | 180 kService1, |
181 kIpAddress1, kDnsServers1); | |
162 | 182 |
183 // New default service, different connection type. | |
163 SetDefaultNetworkState(true, // online. | 184 SetDefaultNetworkState(true, // online. |
164 flimflam::kTypeWifi, "", "/service/2", "192.168.1.2"); | 185 flimflam::kTypeWifi, "", kService2, kIpAddress1, |
186 kDnsServers1); | |
165 bool type_changed = false, ip_changed = false, dns_changed = false; | 187 bool type_changed = false, ip_changed = false, dns_changed = false; |
166 ProcessDefaultNetworkUpdate(&type_changed, &ip_changed, &dns_changed); | 188 ProcessDefaultNetworkUpdate(&type_changed, &ip_changed, &dns_changed); |
167 VerifyNotifierState(NetworkChangeNotifier::CONNECTION_WIFI, "/service/2", | 189 VerifyNotifierState(NetworkChangeNotifier::CONNECTION_WIFI, |
168 "192.168.1.2" ); | 190 kService2, |
191 kIpAddress1, kDnsServers1); | |
169 EXPECT_TRUE(type_changed); | 192 EXPECT_TRUE(type_changed); |
170 EXPECT_TRUE(ip_changed); | 193 EXPECT_TRUE(ip_changed); |
171 EXPECT_TRUE(dns_changed); | 194 EXPECT_TRUE(dns_changed); |
172 | 195 |
196 // New default service, same connection type. | |
173 SetDefaultNetworkState(true, // online. | 197 SetDefaultNetworkState(true, // online. |
174 flimflam::kTypeWifi, "", "/service/3", "192.168.1.2"); | 198 flimflam::kTypeWifi, "", kService3, |
199 kIpAddress1, kDnsServers1); | |
175 ProcessDefaultNetworkUpdate(&type_changed, &ip_changed, &dns_changed); | 200 ProcessDefaultNetworkUpdate(&type_changed, &ip_changed, &dns_changed); |
176 VerifyNotifierState(NetworkChangeNotifier::CONNECTION_WIFI, "/service/3", | 201 VerifyNotifierState(NetworkChangeNotifier::CONNECTION_WIFI, kService3, |
177 "192.168.1.2" ); | 202 kIpAddress1, kDnsServers1); |
178 EXPECT_FALSE(type_changed); | 203 EXPECT_FALSE(type_changed); |
179 // A service path change (even with a corresponding IP change) should still | |
180 // trigger an IP address update to observers. | |
181 EXPECT_TRUE(ip_changed); | 204 EXPECT_TRUE(ip_changed); |
182 EXPECT_TRUE(dns_changed); | 205 EXPECT_TRUE(dns_changed); |
206 | |
207 // Same default service, new IP address, same DNS. | |
208 SetDefaultNetworkState(true, // online. | |
209 flimflam::kTypeWifi, "", kService3, | |
210 kIpAddress2, kDnsServers1); | |
211 ProcessDefaultNetworkUpdate(&type_changed, &ip_changed, &dns_changed); | |
212 VerifyNotifierState(NetworkChangeNotifier::CONNECTION_WIFI, kService3, | |
213 kIpAddress2, kDnsServers1); | |
214 EXPECT_FALSE(type_changed); | |
215 EXPECT_TRUE(ip_changed); | |
216 EXPECT_FALSE(dns_changed); | |
217 | |
218 // Same default service, same IP address, new DNS. | |
219 SetDefaultNetworkState(true, // online. | |
220 flimflam::kTypeWifi, "", kService3, kIpAddress2, | |
221 kDnsServers2); | |
222 ProcessDefaultNetworkUpdate(&type_changed, &ip_changed, &dns_changed); | |
223 VerifyNotifierState(NetworkChangeNotifier::CONNECTION_WIFI, kService3, | |
224 kIpAddress2, kDnsServers2); | |
225 EXPECT_FALSE(type_changed); | |
226 EXPECT_FALSE(ip_changed); | |
227 EXPECT_TRUE(dns_changed); | |
183 } | 228 } |
184 | 229 |
185 } // namespace chromeos | 230 } // namespace chromeos |
OLD | NEW |