Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(340)

Side by Side Diff: net/base/network_change_notifier_chromeos_unittest.cc

Issue 11469044: Implement new network change notifier that uses NetworkStateHandler (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Move to net/base Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 "net/base/network_change_notifier_chromeos.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 "net/base/network_change_notifier_factory_chromeos.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "third_party/cros_system_api/dbus/service_constants.h"
15
16 namespace net {
17
18 TEST(NetworkChangeNotifierChromeosTest, ConnectionTypeFromShill) {
19 struct TypeMapping {
20 const char* shill_type;
21 const char* technology;
22 NetworkChangeNotifier::ConnectionType connection_type;
23 } type_mappings[] = {
stevenjb 2012/12/13 19:28:22 nit: define named struct separately
gauravsh 2012/12/15 00:42:45 Done.
24 {flimflam::kTypeEthernet, "", NetworkChangeNotifier::CONNECTION_ETHERNET},
stevenjb 2012/12/13 19:28:22 ' ' after { and before }
gauravsh 2012/12/15 00:42:45 Done.
25 {flimflam::kTypeWifi, "", NetworkChangeNotifier::CONNECTION_WIFI},
26 {flimflam::kTypeWimax, "", NetworkChangeNotifier::CONNECTION_4G},
27 {"unknown type", "unknown technology",
28 NetworkChangeNotifier::CONNECTION_UNKNOWN},
29 {flimflam::kTypeCellular, flimflam::kNetworkTechnology1Xrtt,
30 NetworkChangeNotifier::CONNECTION_2G},
31 {flimflam::kTypeCellular, flimflam::kNetworkTechnologyGprs,
32 NetworkChangeNotifier::CONNECTION_2G},
33 {flimflam::kTypeCellular, flimflam::kNetworkTechnologyEdge,
34 NetworkChangeNotifier::CONNECTION_2G},
35 {flimflam::kTypeCellular, flimflam::kNetworkTechnologyEvdo,
36 NetworkChangeNotifier::CONNECTION_3G},
37 {flimflam::kTypeCellular, flimflam::kNetworkTechnologyGsm,
38 NetworkChangeNotifier::CONNECTION_3G},
39 {flimflam::kTypeCellular, flimflam::kNetworkTechnologyUmts,
40 NetworkChangeNotifier::CONNECTION_3G},
41 {flimflam::kTypeCellular, flimflam::kNetworkTechnologyHspa,
42 NetworkChangeNotifier::CONNECTION_3G},
43 {flimflam::kTypeCellular, flimflam::kNetworkTechnologyHspaPlus,
44 NetworkChangeNotifier::CONNECTION_4G},
45 {flimflam::kTypeCellular, flimflam::kNetworkTechnologyLte,
46 NetworkChangeNotifier::CONNECTION_4G},
47 {flimflam::kTypeCellular, flimflam::kNetworkTechnologyLteAdvanced,
48 NetworkChangeNotifier::CONNECTION_4G},
49 {flimflam::kTypeCellular, "unknown technology",
50 NetworkChangeNotifier::CONNECTION_2G}
51 };
52
53 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(type_mappings); ++i) {
54 NetworkChangeNotifier::ConnectionType type =
55 NetworkChangeNotifierChromeos::ConnectionTypeFromShill(
56 type_mappings[i].shill_type, type_mappings[i].technology);
57 EXPECT_EQ(type_mappings[i].connection_type, type);
58 }
59 }
60
61 class NetworkChangeNotifierChromeosUpdateTest : public testing::Test {
62 protected:
63 NetworkChangeNotifierChromeosUpdateTest() : active_network_("") {
64 }
65 virtual ~NetworkChangeNotifierChromeosUpdateTest() {}
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.
stevenjb 2012/12/13 19:28:22 Update comment (no returned pointer)
gauravsh 2012/12/15 00:42:45 Done.
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 chromeos::NetworkState active_network_;
110 NetworkChangeNotifier::DisableForTest disable_for_test;
111 NetworkChangeNotifierChromeos notifier_;
112 };
113
114 TEST_F(NetworkChangeNotifierChromeosUpdateTest, UpdateActiveNetworkOffline) {
115 // Test that Online to Offline transitions are correctly handled.
116 SetNotifierState(NetworkChangeNotifier::CONNECTION_ETHERNET, "/service/1",
117 "192.168.1.1");
118 SetActiveNetworkState(false, // offline.
119 flimflam::kTypeEthernet, "", "/service/1", "");
120 bool type_changed = false, ip_changed = false, dns_changed = false;
121 ProcessActiveNetworkUpdate(&type_changed, &ip_changed, &dns_changed);
122 VerifyNotifierState(NetworkChangeNotifier::CONNECTION_NONE, "", "");
123 EXPECT_TRUE(type_changed);
124 EXPECT_TRUE(ip_changed);
125 EXPECT_TRUE(dns_changed);
126 }
127
128 TEST_F(NetworkChangeNotifierChromeosUpdateTest, UpdateActiveNetworkOnline) {
129 // Test that Offline to Online transitions are correctly handled.
130 SetNotifierState(NetworkChangeNotifier::CONNECTION_NONE, "", "");
131
132 SetActiveNetworkState(false, // offline.
133 flimflam::kTypeEthernet, "",
134 "192.168.0.1", "/service/1");
135 bool type_changed = false, ip_changed = false, dns_changed = false;
136 ProcessActiveNetworkUpdate(&type_changed, &ip_changed, &dns_changed);
137 // If the new active network is still offline, nothing should have changed.
138 VerifyNotifierState(NetworkChangeNotifier::CONNECTION_NONE, "", "");
139 EXPECT_FALSE(type_changed);
140 EXPECT_FALSE(ip_changed);
141 EXPECT_FALSE(dns_changed);
142
143 SetActiveNetworkState(true, // online.
144 flimflam::kTypeEthernet, "", "/service/1",
145 "192.168.0.1");
146 ProcessActiveNetworkUpdate(&type_changed, &ip_changed, &dns_changed);
147 // Now the new active network is online, so this should trigger a notifier
148 // state change.
149 VerifyNotifierState(NetworkChangeNotifier::CONNECTION_ETHERNET, "/service/1",
150 "192.168.0.1");
151 EXPECT_TRUE(type_changed);
152 EXPECT_TRUE(ip_changed);
153 EXPECT_TRUE(dns_changed);
154 }
155
156 TEST_F(NetworkChangeNotifierChromeosUpdateTest, UpdateActiveNetworkChanged) {
157 // Test that Online to Online transisions (active network changes) are
158 // correctly handled.
159 SetNotifierState(NetworkChangeNotifier::CONNECTION_ETHERNET, "/service/1",
160 "192.168.1.1");
161
162 SetActiveNetworkState(true, // online.
163 flimflam::kTypeWifi, "", "/service/2", "192.168.1.2");
164 bool type_changed = false, ip_changed = false, dns_changed = false;
165 ProcessActiveNetworkUpdate(&type_changed, &ip_changed, &dns_changed);
166 VerifyNotifierState(NetworkChangeNotifier::CONNECTION_WIFI, "/service/2",
167 "192.168.1.2" );
168 EXPECT_TRUE(type_changed);
169 EXPECT_TRUE(ip_changed);
170 EXPECT_TRUE(dns_changed);
171
172 SetActiveNetworkState(true, // online.
173 flimflam::kTypeWifi, "", "/service/3", "192.168.1.2");
174 ProcessActiveNetworkUpdate(&type_changed, &ip_changed, &dns_changed);
175 VerifyNotifierState(NetworkChangeNotifier::CONNECTION_WIFI, "/service/3",
176 "192.168.1.2" );
177 EXPECT_FALSE(type_changed);
178 // A service path change (even with a corresponding IP change) should still
179 // trigger an IP address update to observers.
180 EXPECT_TRUE(ip_changed);
181 EXPECT_TRUE(dns_changed);
182 }
183
184 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698