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

Side by Side Diff: chromeos/network/network_state_handler_unittest.cc

Issue 11614035: Improve NetworkStateHandler API (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address feedback, state -> connection state 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 | Annotate | Revision Log
OLDNEW
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_state_handler.h" 5 #include "chromeos/network/network_state_handler.h"
6 6
7 #include <map> 7 #include <map>
8 #include <set> 8 #include <set>
9 #include <string> 9 #include <string>
10 10
(...skipping 11 matching lines...) Expand all
22 #include "third_party/cros_system_api/dbus/service_constants.h" 22 #include "third_party/cros_system_api/dbus/service_constants.h"
23 23
24 namespace { 24 namespace {
25 25
26 void ErrorCallbackFunction(const std::string& error_name, 26 void ErrorCallbackFunction(const std::string& error_name,
27 const std::string& error_message) { 27 const std::string& error_message) {
28 LOG(ERROR) << "Shill Error: " << error_name << " : " << error_message; 28 LOG(ERROR) << "Shill Error: " << error_name << " : " << error_message;
29 } 29 }
30 30
31 using chromeos::NetworkState; 31 using chromeos::NetworkState;
32 using chromeos::NetworkStateHandler;
32 33
33 class TestObserver : public chromeos::NetworkStateHandlerObserver { 34 class TestObserver : public chromeos::NetworkStateHandlerObserver {
34 public: 35 public:
35 TestObserver() 36 explicit TestObserver(NetworkStateHandler* handler)
36 : manager_changed_count_(0), 37 : handler_(handler),
38 manager_changed_count_(0),
37 network_count_(0) { 39 network_count_(0) {
38 } 40 }
39 41
40 virtual ~TestObserver() { 42 virtual ~TestObserver() {
41 } 43 }
42 44
43 virtual void NetworkManagerChanged() { 45 virtual void NetworkManagerChanged() OVERRIDE {
44 ++manager_changed_count_; 46 ++manager_changed_count_;
45 } 47 }
46 48
47 virtual void NetworkListChanged( 49 virtual void NetworkListChanged(
48 const chromeos::NetworkStateHandler::NetworkStateList& networks) { 50 const NetworkStateHandler::NetworkStateList& networks) OVERRIDE {
49 network_count_ = networks.size(); 51 network_count_ = networks.size();
52 if (network_count_ == 0) {
53 default_network_ = "";
54 default_network_connection_state_ = "";
55 }
50 } 56 }
51 57
52 virtual void ActiveNetworkChanged(const NetworkState* network) { 58 virtual void DefaultNetworkChanged(const NetworkState* network) OVERRIDE {
53 active_network_ = network ? network->path() : ""; 59 default_network_ = network ? network->path() : "";
54 active_network_state_ = network ? network->state() : ""; 60 default_network_connection_state_ =
61 network ? network->connection_state() : "";
55 } 62 }
56 63
57 virtual void ActiveNetworkStateChanged(const NetworkState* network) { 64 virtual void NetworkStateChanged(const NetworkState* network) OVERRIDE {
58 active_network_state_ = network ? network->state() : ""; 65 network_connection_state_[network->path()] = network->connection_state();
66 connection_state_changes_[network->path()]++;
59 } 67 }
60 68
61 virtual void NetworkServiceChanged(const NetworkState* network) { 69 virtual void NetworkPropertyChanged(const NetworkState* network) OVERRIDE {
62 DCHECK(network); 70 DCHECK(network);
63 std::map<std::string, int>::iterator iter = 71 property_changes_[network->path()]++;
64 property_changes_.find(network->path());
65 if (iter == property_changes_.end())
66 property_changes_[network->path()] = 1;
67 else
68 iter->second++;
69 } 72 }
70 73
71 size_t manager_changed_count() { return manager_changed_count_; } 74 size_t manager_changed_count() { return manager_changed_count_; }
72 size_t network_count() { return network_count_; } 75 size_t network_count() { return network_count_; }
73 std::string active_network() { return active_network_; } 76 std::string default_network() { return default_network_; }
74 std::string active_network_state() { return active_network_state_; } 77 std::string default_network_connection_state() {
78 return default_network_connection_state_;
79 }
75 80
76 int PropertyChangesForService(const std::string& service_path) { 81 int PropertyChangesForService(const std::string& service_path) {
77 std::map<std::string, int>::iterator iter = 82 return property_changes_[service_path];
78 property_changes_.find(service_path); 83 }
79 if (iter == property_changes_.end()) 84
80 return 0; 85 int ConnectionStateChangesForService(const std::string& service_path) {
81 return iter->second; 86 return connection_state_changes_[service_path];
87 }
88
89 std::string NetworkConnectionStateForService(
90 const std::string& service_path) {
91 return network_connection_state_[service_path];
82 } 92 }
83 93
84 private: 94 private:
95 NetworkStateHandler* handler_;
85 size_t manager_changed_count_; 96 size_t manager_changed_count_;
86 size_t network_count_; 97 size_t network_count_;
87 std::string active_network_; 98 std::string default_network_;
88 std::string active_network_state_; 99 std::string default_network_connection_state_;
89 std::map<std::string, int> property_changes_; 100 std::map<std::string, int> property_changes_;
101 std::map<std::string, int> connection_state_changes_;
102 std::map<std::string, std::string> network_connection_state_;
90 103
91 DISALLOW_COPY_AND_ASSIGN(TestObserver); 104 DISALLOW_COPY_AND_ASSIGN(TestObserver);
92 }; 105 };
93 106
94 } // namespace 107 } // namespace
95 108
96 namespace chromeos { 109 namespace chromeos {
97 110
98 class NetworkStateHandlerTest : public testing::Test { 111 class NetworkStateHandlerTest : public testing::Test {
99 public: 112 public:
100 NetworkStateHandlerTest() {} 113 NetworkStateHandlerTest() {}
101 virtual ~NetworkStateHandlerTest() {} 114 virtual ~NetworkStateHandlerTest() {}
102 115
103 virtual void SetUp() OVERRIDE { 116 virtual void SetUp() OVERRIDE {
104 // Initialize DBusThreadManager with a stub implementation. 117 // Initialize DBusThreadManager with a stub implementation.
105 DBusThreadManager::InitializeWithStub(); 118 DBusThreadManager::InitializeWithStub();
106 } 119 }
107 120
108 virtual void TearDown() OVERRIDE { 121 virtual void TearDown() OVERRIDE {
109 network_state_handler_.reset(); 122 network_state_handler_.reset();
110 test_observer_.reset(); 123 test_observer_.reset();
111 DBusThreadManager::Shutdown(); 124 DBusThreadManager::Shutdown();
112 } 125 }
113 126
114 void SetupNetworkStateHandler() { 127 void SetupNetworkStateHandler() {
115 test_observer_.reset(new TestObserver);
116 network_state_handler_.reset(new NetworkStateHandler); 128 network_state_handler_.reset(new NetworkStateHandler);
129 test_observer_.reset(new TestObserver(network_state_handler_.get()));
117 network_state_handler_->AddObserver(test_observer_.get()); 130 network_state_handler_->AddObserver(test_observer_.get());
118 network_state_handler_->InitShillPropertyHandler(); 131 network_state_handler_->InitShillPropertyHandler();
119 } 132 }
120 133
121 protected: 134 protected:
122 MessageLoopForUI message_loop_; 135 MessageLoopForUI message_loop_;
123 scoped_ptr<NetworkStateHandler> network_state_handler_; 136 scoped_ptr<NetworkStateHandler> network_state_handler_;
124 scoped_ptr<TestObserver> test_observer_; 137 scoped_ptr<TestObserver> test_observer_;
125 138
126 private: 139 private:
127 DISALLOW_COPY_AND_ASSIGN(NetworkStateHandlerTest); 140 DISALLOW_COPY_AND_ASSIGN(NetworkStateHandlerTest);
128 }; 141 };
129 142
130 TEST_F(NetworkStateHandlerTest, NetworkStateHandlerStub) { 143 TEST_F(NetworkStateHandlerTest, NetworkStateHandlerStub) {
131 // This relies on the stub dbus implementations for ShillManagerClient, 144 // This relies on the stub dbus implementations for ShillManagerClient,
132 SetupNetworkStateHandler(); 145 SetupNetworkStateHandler();
133 message_loop_.RunUntilIdle(); 146 message_loop_.RunUntilIdle();
134 EXPECT_EQ(1u, test_observer_->manager_changed_count()); 147 EXPECT_EQ(1u, test_observer_->manager_changed_count());
135 // Ensure that the network list is the expected size. 148 // Ensure that the network list is the expected size.
136 const size_t kNumShillManagerClientStubImplServices = 4; 149 const size_t kNumShillManagerClientStubImplServices = 4;
137 EXPECT_EQ(kNumShillManagerClientStubImplServices, 150 EXPECT_EQ(kNumShillManagerClientStubImplServices,
138 test_observer_->network_count()); 151 test_observer_->network_count());
139 // Ensure that the first stub network is the active network. 152 // Ensure that the first stub network is the default network.
140 const std::string kShillManagerClientStubActiveService = "stub_ethernet"; 153 const std::string kShillManagerClientStubDefaultService = "stub_ethernet";
141 EXPECT_EQ(kShillManagerClientStubActiveService, 154 EXPECT_EQ(kShillManagerClientStubDefaultService,
142 test_observer_->active_network()); 155 test_observer_->default_network());
143 EXPECT_EQ(kShillManagerClientStubActiveService, 156 EXPECT_EQ(kShillManagerClientStubDefaultService,
144 network_state_handler_->ActiveNetwork()->path()); 157 network_state_handler_->ConnectedNetworkByType(
145 EXPECT_EQ(kShillManagerClientStubActiveService, 158 NetworkState::kMatchTypeDefault)->path());
159 EXPECT_EQ(kShillManagerClientStubDefaultService,
146 network_state_handler_->ConnectedNetworkByType( 160 network_state_handler_->ConnectedNetworkByType(
147 flimflam::kTypeEthernet)->path()); 161 flimflam::kTypeEthernet)->path());
148 EXPECT_EQ(flimflam::kStateOnline, test_observer_->active_network_state()); 162 const std::string kShillManagerClientStubDefaultWireless = "stub_wifi1";
163 EXPECT_EQ(kShillManagerClientStubDefaultWireless,
164 network_state_handler_->ConnectedNetworkByType(
165 NetworkState::kMatchTypeWireless)->path());
166 EXPECT_EQ(flimflam::kStateOnline,
167 test_observer_->default_network_connection_state());
149 } 168 }
150 169
151 TEST_F(NetworkStateHandlerTest, NetworkStateHandlerTechnologyChanged) { 170 TEST_F(NetworkStateHandlerTest, TechnologyChanged) {
152 // This relies on the stub dbus implementations for ShillManagerClient, 171 // This relies on the stub dbus implementations for ShillManagerClient,
153 SetupNetworkStateHandler(); 172 SetupNetworkStateHandler();
154 message_loop_.RunUntilIdle(); 173 message_loop_.RunUntilIdle();
155 EXPECT_EQ(1u, test_observer_->manager_changed_count()); 174 EXPECT_EQ(1u, test_observer_->manager_changed_count());
156 // Enable a technology. 175 // Enable a technology.
157 EXPECT_FALSE(network_state_handler_->TechnologyEnabled(flimflam::kTypeWimax)); 176 EXPECT_FALSE(network_state_handler_->TechnologyEnabled(flimflam::kTypeWimax));
158 network_state_handler_->SetTechnologyEnabled( 177 network_state_handler_->SetTechnologyEnabled(
159 flimflam::kTypeWimax, true, network_handler::ErrorCallback()); 178 flimflam::kTypeWimax, true, network_handler::ErrorCallback());
160 message_loop_.RunUntilIdle(); 179 message_loop_.RunUntilIdle();
161 // Ensure we get a manager changed callback when we change a property. 180 // Ensure we get a manager changed callback when we change a property.
162 EXPECT_EQ(2u, test_observer_->manager_changed_count()); 181 EXPECT_EQ(2u, test_observer_->manager_changed_count());
163 EXPECT_TRUE(network_state_handler_->TechnologyEnabled(flimflam::kTypeWimax)); 182 EXPECT_TRUE(network_state_handler_->TechnologyEnabled(flimflam::kTypeWimax));
164 } 183 }
165 184
166 TEST_F(NetworkStateHandlerTest, NetworkStateHandlerServicePropertyChanged) { 185 TEST_F(NetworkStateHandlerTest, ServicePropertyChanged) {
167 // This relies on the stub dbus implementations for ShillManagerClient, 186 // This relies on the stub dbus implementations for ShillManagerClient,
168 SetupNetworkStateHandler(); 187 SetupNetworkStateHandler();
169 message_loop_.RunUntilIdle(); 188 message_loop_.RunUntilIdle();
170 // Set a service property. 189 // Set a service property.
171 const std::string eth0 = "stub_ethernet"; 190 const std::string eth0 = "stub_ethernet";
172 EXPECT_EQ("", network_state_handler_->GetNetworkState(eth0)->security()); 191 EXPECT_EQ("", network_state_handler_->GetNetworkState(eth0)->security());
173 EXPECT_EQ(1, test_observer_->PropertyChangesForService(eth0)); 192 EXPECT_EQ(1, test_observer_->PropertyChangesForService(eth0));
174 base::StringValue security_value("TestSecurity"); 193 base::StringValue security_value("TestSecurity");
175 DBusThreadManager::Get()->GetShillServiceClient()->SetProperty( 194 DBusThreadManager::Get()->GetShillServiceClient()->SetProperty(
176 dbus::ObjectPath(eth0), 195 dbus::ObjectPath(eth0),
177 flimflam::kSecurityProperty, security_value, 196 flimflam::kSecurityProperty, security_value,
178 base::Bind(&base::DoNothing), base::Bind(&ErrorCallbackFunction)); 197 base::Bind(&base::DoNothing), base::Bind(&ErrorCallbackFunction));
179 message_loop_.RunUntilIdle(); 198 message_loop_.RunUntilIdle();
180 EXPECT_EQ("TestSecurity", 199 EXPECT_EQ("TestSecurity",
181 network_state_handler_->GetNetworkState(eth0)->security()); 200 network_state_handler_->GetNetworkState(eth0)->security());
182 EXPECT_EQ(2, test_observer_->PropertyChangesForService(eth0)); 201 EXPECT_EQ(2, test_observer_->PropertyChangesForService(eth0));
202 // Confirm that changing a property to the same value still signals the
203 // observer.
204 DBusThreadManager::Get()->GetShillServiceClient()->SetProperty(
pneubeck (no reviews) 2012/12/19 19:40:06 Is that really a requirement about the NetworkStat
stevenjb 2012/12/19 21:36:50 Good point. I added that to confirm that the behav
205 dbus::ObjectPath(eth0),
206 flimflam::kSecurityProperty, security_value,
207 base::Bind(&base::DoNothing), base::Bind(&ErrorCallbackFunction));
208 message_loop_.RunUntilIdle();
209 EXPECT_EQ(3, test_observer_->PropertyChangesForService(eth0));
183 } 210 }
184 211
185 TEST_F(NetworkStateHandlerTest, NetworkStateHandlerActiveServiceChanged) { 212 TEST_F(NetworkStateHandlerTest, NetworkStateChanged) {
213 // This relies on the stub dbus implementations for ShillManagerClient,
214 SetupNetworkStateHandler();
215 message_loop_.RunUntilIdle();
216 // Change a network state.
217 ShillServiceClient::TestInterface* service_test =
218 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface();
219 const std::string eth0 = "stub_ethernet";
220 base::StringValue connection_state_idle_value(flimflam::kStateIdle);
221 service_test->SetServiceProperty(eth0, flimflam::kStateProperty,
222 connection_state_idle_value);
223 message_loop_.RunUntilIdle();
224 EXPECT_EQ(flimflam::kStateIdle,
225 test_observer_->NetworkConnectionStateForService(eth0));
226 EXPECT_EQ(2, test_observer_->ConnectionStateChangesForService(eth0));
227 // Confirm that changing the connection state to the same value does *not*
228 // signal the observer.
229 service_test->SetServiceProperty(eth0, flimflam::kStateProperty,
230 connection_state_idle_value);
231 message_loop_.RunUntilIdle();
232 EXPECT_EQ(2, test_observer_->ConnectionStateChangesForService(eth0));
233 }
234
235 TEST_F(NetworkStateHandlerTest, DefaultServiceChanged) {
186 // This relies on the stub dbus implementations for ShillManagerClient, 236 // This relies on the stub dbus implementations for ShillManagerClient,
187 SetupNetworkStateHandler(); 237 SetupNetworkStateHandler();
188 message_loop_.RunUntilIdle(); 238 message_loop_.RunUntilIdle();
189 239
190 // Change the active network by inserting wifi1 at the front of the list.
191 ShillManagerClient::TestInterface* manager_test = 240 ShillManagerClient::TestInterface* manager_test =
192 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface(); 241 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface();
193 ASSERT_TRUE(manager_test); 242 ASSERT_TRUE(manager_test);
194 const std::string wifi1 = "stub_wifi1";
195 manager_test->AddServiceAtIndex(wifi1, 0, true);
196 message_loop_.RunUntilIdle();
197 EXPECT_EQ(wifi1, test_observer_->active_network());
198 EXPECT_EQ(flimflam::kStateOnline, test_observer_->active_network_state());
199
200 // Change the state of wifi1, ensure that triggers the active state changed
201 // observer.
202 ShillServiceClient::TestInterface* service_test = 243 ShillServiceClient::TestInterface* service_test =
203 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface(); 244 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface();
204 ASSERT_TRUE(service_test); 245 ASSERT_TRUE(service_test);
205 base::StringValue state_value(flimflam::kStateConfiguration); 246
206 service_test->SetServiceProperty(wifi1, flimflam::kStateProperty, 247 // Change the default network by inserting wifi1 at the front of the list
207 state_value); 248 // and changing the state of stub_ethernet to Idle.
249 const std::string wifi1 = "stub_wifi1";
250 manager_test->AddServiceAtIndex(wifi1, 0, true);
251 const std::string eth0 = "stub_ethernet";
252 base::StringValue connection_state_idle_value(flimflam::kStateIdle);
253 service_test->SetServiceProperty(eth0, flimflam::kStateProperty,
254 connection_state_idle_value);
208 message_loop_.RunUntilIdle(); 255 message_loop_.RunUntilIdle();
209 EXPECT_EQ(wifi1, test_observer_->active_network()); 256 EXPECT_EQ(wifi1, test_observer_->default_network());
210 EXPECT_EQ(flimflam::kStateConfiguration, 257 EXPECT_EQ(flimflam::kStateOnline,
211 test_observer_->active_network_state()); 258 test_observer_->default_network_connection_state());
212 } 259 }
213 260
214 } // namespace chromeos 261 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698