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

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

Issue 289383004: Merge FavoriteState into NetworkState (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase Created 6 years, 6 months 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
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/scoped_ptr.h"
13 #include "base/message_loop/message_loop.h" 13 #include "base/message_loop/message_loop.h"
14 #include "base/values.h" 14 #include "base/values.h"
15 #include "chromeos/dbus/dbus_thread_manager.h" 15 #include "chromeos/dbus/dbus_thread_manager.h"
16 #include "chromeos/dbus/shill_device_client.h" 16 #include "chromeos/dbus/shill_device_client.h"
17 #include "chromeos/dbus/shill_manager_client.h" 17 #include "chromeos/dbus/shill_manager_client.h"
18 #include "chromeos/dbus/shill_profile_client.h" 18 #include "chromeos/dbus/shill_profile_client.h"
19 #include "chromeos/dbus/shill_service_client.h" 19 #include "chromeos/dbus/shill_service_client.h"
20 #include "chromeos/network/favorite_state.h"
21 #include "chromeos/network/network_state.h" 20 #include "chromeos/network/network_state.h"
22 #include "chromeos/network/network_state_handler_observer.h" 21 #include "chromeos/network/network_state_handler_observer.h"
23 #include "dbus/object_path.h" 22 #include "dbus/object_path.h"
24 #include "testing/gtest/include/gtest/gtest.h" 23 #include "testing/gtest/include/gtest/gtest.h"
25 #include "third_party/cros_system_api/dbus/service_constants.h" 24 #include "third_party/cros_system_api/dbus/service_constants.h"
26 25
27 namespace { 26 namespace {
28 27
29 void ErrorCallbackFunction(const std::string& error_name, 28 void ErrorCallbackFunction(const std::string& error_name,
30 const std::string& error_message) { 29 const std::string& error_message) {
31 LOG(ERROR) << "Shill Error: " << error_name << " : " << error_message; 30 LOG(ERROR) << "Shill Error: " << error_name << " : " << error_message;
32 } 31 }
33 32
34 const std::string kShillManagerClientStubDefaultService = "eth1"; 33 const std::string kShillManagerClientStubDefaultService = "/service/eth1";
35 const std::string kShillManagerClientStubDefaultWifi = "wifi1"; 34 const std::string kShillManagerClientStubDefaultWifi = "/service/wifi1";
36 const std::string kShillManagerClientStubWifi2 = "wifi2"; 35 const std::string kShillManagerClientStubWifi2 = "/service/wifi2";
37 const std::string kShillManagerClientStubCellular = "cellular1"; 36 const std::string kShillManagerClientStubCellular = "/service/cellular1";
38 37
39 using chromeos::NetworkState; 38 using chromeos::NetworkState;
40 using chromeos::NetworkStateHandler; 39 using chromeos::NetworkStateHandler;
41 40
42 class TestObserver : public chromeos::NetworkStateHandlerObserver { 41 class TestObserver : public chromeos::NetworkStateHandlerObserver {
43 public: 42 public:
44 explicit TestObserver(NetworkStateHandler* handler) 43 explicit TestObserver(NetworkStateHandler* handler)
45 : handler_(handler), 44 : handler_(handler),
46 device_list_changed_count_(0), 45 device_list_changed_count_(0),
46 device_count_(0),
47 network_count_(0), 47 network_count_(0),
48 default_network_change_count_(0), 48 default_network_change_count_(0) {
49 favorite_count_(0) {
50 } 49 }
51 50
52 virtual ~TestObserver() { 51 virtual ~TestObserver() {
53 } 52 }
54 53
55 virtual void DeviceListChanged() OVERRIDE { 54 virtual void DeviceListChanged() OVERRIDE {
55 NetworkStateHandler::DeviceStateList devices;
56 handler_->GetDeviceList(&devices);
57 device_count_ = devices.size();
56 ++device_list_changed_count_; 58 ++device_list_changed_count_;
57 } 59 }
58 60
59 virtual void NetworkListChanged() OVERRIDE { 61 virtual void NetworkListChanged() OVERRIDE {
60 NetworkStateHandler::NetworkStateList networks; 62 NetworkStateHandler::NetworkStateList networks;
61 handler_->GetNetworkList(&networks); 63 handler_->GetVisibleNetworkList(&networks);
62 network_count_ = networks.size(); 64 network_count_ = networks.size();
63 if (network_count_ == 0) { 65 if (network_count_ == 0) {
64 default_network_ = ""; 66 default_network_ = "";
65 default_network_connection_state_ = ""; 67 default_network_connection_state_ = "";
66 } 68 }
67 NetworkStateHandler::FavoriteStateList favorites;
68 handler_->GetFavoriteList(&favorites);
69 favorite_count_ = favorites.size();
70 } 69 }
71 70
72 virtual void DefaultNetworkChanged(const NetworkState* network) OVERRIDE { 71 virtual void DefaultNetworkChanged(const NetworkState* network) OVERRIDE {
73 ++default_network_change_count_; 72 ++default_network_change_count_;
74 default_network_ = network ? network->path() : ""; 73 default_network_ = network ? network->path() : "";
75 default_network_connection_state_ = 74 default_network_connection_state_ =
76 network ? network->connection_state() : ""; 75 network ? network->connection_state() : "";
77 DVLOG(1) << "DefaultNetworkChanged: " << default_network_ 76 DVLOG(1) << "DefaultNetworkChanged: " << default_network_
78 << " State: " << default_network_connection_state_; 77 << " State: " << default_network_connection_state_;
79 } 78 }
80 79
81 virtual void NetworkConnectionStateChanged( 80 virtual void NetworkConnectionStateChanged(
82 const NetworkState* network) OVERRIDE { 81 const NetworkState* network) OVERRIDE {
83 network_connection_state_[network->path()] = network->connection_state(); 82 network_connection_state_[network->path()] = network->connection_state();
84 connection_state_changes_[network->path()]++; 83 connection_state_changes_[network->path()]++;
85 } 84 }
86 85
87 virtual void NetworkPropertiesUpdated(const NetworkState* network) OVERRIDE { 86 virtual void NetworkPropertiesUpdated(const NetworkState* network) OVERRIDE {
88 DCHECK(network); 87 DCHECK(network);
89 property_updates_[network->path()]++; 88 property_updates_[network->path()]++;
90 } 89 }
91 90
92 size_t device_list_changed_count() { return device_list_changed_count_; } 91 size_t device_list_changed_count() { return device_list_changed_count_; }
92 size_t device_count() { return device_count_; }
93 size_t network_count() { return network_count_; } 93 size_t network_count() { return network_count_; }
94 size_t default_network_change_count() { 94 size_t default_network_change_count() {
95 return default_network_change_count_; 95 return default_network_change_count_;
96 } 96 }
97 void reset_network_change_count() { 97 void reset_change_counts() {
98 DVLOG(1) << "ResetNetworkChangeCount"; 98 DVLOG(1) << "=== RESET CHANGE COUNTS ===";
99 default_network_change_count_ = 0; 99 default_network_change_count_ = 0;
100 device_list_changed_count_ = 0;
100 } 101 }
101 std::string default_network() { return default_network_; } 102 std::string default_network() { return default_network_; }
102 std::string default_network_connection_state() { 103 std::string default_network_connection_state() {
103 return default_network_connection_state_; 104 return default_network_connection_state_;
104 } 105 }
105 size_t favorite_count() { return favorite_count_; }
106 106
107 int PropertyUpdatesForService(const std::string& service_path) { 107 int PropertyUpdatesForService(const std::string& service_path) {
108 return property_updates_[service_path]; 108 return property_updates_[service_path];
109 } 109 }
110 110
111 int ConnectionStateChangesForService(const std::string& service_path) { 111 int ConnectionStateChangesForService(const std::string& service_path) {
112 return connection_state_changes_[service_path]; 112 return connection_state_changes_[service_path];
113 } 113 }
114 114
115 std::string NetworkConnectionStateForService( 115 std::string NetworkConnectionStateForService(
116 const std::string& service_path) { 116 const std::string& service_path) {
117 return network_connection_state_[service_path]; 117 return network_connection_state_[service_path];
118 } 118 }
119 119
120 private: 120 private:
121 NetworkStateHandler* handler_; 121 NetworkStateHandler* handler_;
122 size_t device_list_changed_count_; 122 size_t device_list_changed_count_;
123 size_t device_count_;
123 size_t network_count_; 124 size_t network_count_;
124 size_t default_network_change_count_; 125 size_t default_network_change_count_;
125 std::string default_network_; 126 std::string default_network_;
126 std::string default_network_connection_state_; 127 std::string default_network_connection_state_;
127 size_t favorite_count_;
128 std::map<std::string, int> property_updates_; 128 std::map<std::string, int> property_updates_;
129 std::map<std::string, int> connection_state_changes_; 129 std::map<std::string, int> connection_state_changes_;
130 std::map<std::string, std::string> network_connection_state_; 130 std::map<std::string, std::string> network_connection_state_;
131 131
132 DISALLOW_COPY_AND_ASSIGN(TestObserver); 132 DISALLOW_COPY_AND_ASSIGN(TestObserver);
133 }; 133 };
134 134
135 } // namespace 135 } // namespace
136 136
137 namespace chromeos { 137 namespace chromeos {
138 138
139 class NetworkStateHandlerTest : public testing::Test { 139 class NetworkStateHandlerTest : public testing::Test {
140 public: 140 public:
141 NetworkStateHandlerTest() 141 NetworkStateHandlerTest()
142 : device_test_(NULL), 142 : device_test_(NULL),
143 manager_test_(NULL), 143 manager_test_(NULL),
144 profile_test_(NULL), 144 profile_test_(NULL),
145 service_test_(NULL) {} 145 service_test_(NULL) {}
146 virtual ~NetworkStateHandlerTest() {} 146 virtual ~NetworkStateHandlerTest() {}
147 147
148 virtual void SetUp() OVERRIDE { 148 virtual void SetUp() OVERRIDE {
149 // Initialize DBusThreadManager with a stub implementation. 149 // Initialize DBusThreadManager with a stub implementation.
150 DBusThreadManager::InitializeWithStub(); 150 DBusThreadManager::InitializeWithStub();
151 SetupNetworkStateHandler(); 151 SetupDefaultShillState();
152 network_state_handler_.reset(new NetworkStateHandler);
153 test_observer_.reset(new TestObserver(network_state_handler_.get()));
154 network_state_handler_->AddObserver(test_observer_.get(), FROM_HERE);
155 network_state_handler_->InitShillPropertyHandler();
152 message_loop_.RunUntilIdle(); 156 message_loop_.RunUntilIdle();
153 } 157 }
154 158
155 virtual void TearDown() OVERRIDE { 159 virtual void TearDown() OVERRIDE {
156 network_state_handler_->RemoveObserver(test_observer_.get(), FROM_HERE); 160 network_state_handler_->RemoveObserver(test_observer_.get(), FROM_HERE);
157 test_observer_.reset(); 161 test_observer_.reset();
158 network_state_handler_.reset(); 162 network_state_handler_.reset();
159 DBusThreadManager::Shutdown(); 163 DBusThreadManager::Shutdown();
160 } 164 }
161 165
162 void SetupNetworkStateHandler() {
163 SetupDefaultShillState();
164 network_state_handler_.reset(new NetworkStateHandler);
165 test_observer_.reset(new TestObserver(network_state_handler_.get()));
166 network_state_handler_->AddObserver(test_observer_.get(), FROM_HERE);
167 network_state_handler_->InitShillPropertyHandler();
168 }
169
170 protected: 166 protected:
171 void AddService(const std::string& service_path, 167 void AddService(const std::string& service_path,
172 const std::string& name, 168 const std::string& name,
173 const std::string& type, 169 const std::string& type,
174 const std::string& state) { 170 const std::string& state) {
175 service_test_->AddService(service_path, name, type, state, 171 service_test_->AddService(service_path, name, type, state,
176 true /* add_to_visible */, 172 true /* add_to_visible */,
177 true /* add_to_watchlist */); 173 true /* add_to_watchlist */);
178 } 174 }
179 175
(...skipping 16 matching lines...) Expand all
196 profile_test_ = 192 profile_test_ =
197 DBusThreadManager::Get()->GetShillProfileClient()->GetTestInterface(); 193 DBusThreadManager::Get()->GetShillProfileClient()->GetTestInterface();
198 ASSERT_TRUE(profile_test_); 194 ASSERT_TRUE(profile_test_);
199 profile_test_->ClearProfiles(); 195 profile_test_->ClearProfiles();
200 196
201 service_test_ = 197 service_test_ =
202 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface(); 198 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface();
203 ASSERT_TRUE(service_test_); 199 ASSERT_TRUE(service_test_);
204 service_test_->ClearServices(); 200 service_test_->ClearServices();
205 AddService(kShillManagerClientStubDefaultService, 201 AddService(kShillManagerClientStubDefaultService,
206 kShillManagerClientStubDefaultService, 202 "eth1",
207 shill::kTypeEthernet, 203 shill::kTypeEthernet,
208 shill::kStateOnline); 204 shill::kStateOnline);
209 AddService(kShillManagerClientStubDefaultWifi, 205 AddService(kShillManagerClientStubDefaultWifi,
210 kShillManagerClientStubDefaultWifi, 206 "wifi1",
211 shill::kTypeWifi, 207 shill::kTypeWifi,
212 shill::kStateOnline); 208 shill::kStateOnline);
213 AddService(kShillManagerClientStubWifi2, 209 AddService(kShillManagerClientStubWifi2,
214 kShillManagerClientStubWifi2, 210 "wifi2",
215 shill::kTypeWifi, 211 shill::kTypeWifi,
216 shill::kStateIdle); 212 shill::kStateIdle);
217 AddService(kShillManagerClientStubCellular, 213 AddService(kShillManagerClientStubCellular,
218 kShillManagerClientStubCellular, 214 "cellular1",
219 shill::kTypeCellular, 215 shill::kTypeCellular,
220 shill::kStateIdle); 216 shill::kStateIdle);
221 } 217 }
222 218
223 void UpdateManagerProperties() { 219 void UpdateManagerProperties() {
224 message_loop_.RunUntilIdle(); 220 message_loop_.RunUntilIdle();
225 network_state_handler_->UpdateManagerProperties(); 221 network_state_handler_->UpdateManagerProperties();
226 message_loop_.RunUntilIdle(); 222 message_loop_.RunUntilIdle();
227 } 223 }
228 224
229 base::MessageLoopForUI message_loop_; 225 base::MessageLoopForUI message_loop_;
230 scoped_ptr<NetworkStateHandler> network_state_handler_; 226 scoped_ptr<NetworkStateHandler> network_state_handler_;
231 scoped_ptr<TestObserver> test_observer_; 227 scoped_ptr<TestObserver> test_observer_;
232 ShillDeviceClient::TestInterface* device_test_; 228 ShillDeviceClient::TestInterface* device_test_;
233 ShillManagerClient::TestInterface* manager_test_; 229 ShillManagerClient::TestInterface* manager_test_;
234 ShillProfileClient::TestInterface* profile_test_; 230 ShillProfileClient::TestInterface* profile_test_;
235 ShillServiceClient::TestInterface* service_test_; 231 ShillServiceClient::TestInterface* service_test_;
236 232
237 private: 233 private:
238 DISALLOW_COPY_AND_ASSIGN(NetworkStateHandlerTest); 234 DISALLOW_COPY_AND_ASSIGN(NetworkStateHandlerTest);
239 }; 235 };
240 236
241 TEST_F(NetworkStateHandlerTest, NetworkStateHandlerStub) { 237 TEST_F(NetworkStateHandlerTest, NetworkStateHandlerStub) {
242 // Ensure that the network list is the expected size. 238 // Ensure that the device and network list are the expected size.
239 const size_t kNumShillManagerClientStubImplDevices = 2;
240 EXPECT_EQ(kNumShillManagerClientStubImplDevices,
241 test_observer_->device_count());
243 const size_t kNumShillManagerClientStubImplServices = 4; 242 const size_t kNumShillManagerClientStubImplServices = 4;
244 EXPECT_EQ(kNumShillManagerClientStubImplServices, 243 EXPECT_EQ(kNumShillManagerClientStubImplServices,
245 test_observer_->network_count()); 244 test_observer_->network_count());
246 // Ensure that the first stub network is the default network. 245 // Ensure that the first stub network is the default network.
247 EXPECT_EQ(kShillManagerClientStubDefaultService, 246 EXPECT_EQ(kShillManagerClientStubDefaultService,
248 test_observer_->default_network()); 247 test_observer_->default_network());
248 ASSERT_TRUE(network_state_handler_->DefaultNetwork());
249 EXPECT_EQ(kShillManagerClientStubDefaultService, 249 EXPECT_EQ(kShillManagerClientStubDefaultService,
250 network_state_handler_->ConnectedNetworkByType( 250 network_state_handler_->DefaultNetwork()->path());
251 NetworkTypePattern::Default())->path());
252 EXPECT_EQ(kShillManagerClientStubDefaultService, 251 EXPECT_EQ(kShillManagerClientStubDefaultService,
253 network_state_handler_->ConnectedNetworkByType( 252 network_state_handler_->ConnectedNetworkByType(
254 NetworkTypePattern::Ethernet())->path()); 253 NetworkTypePattern::Ethernet())->path());
255 EXPECT_EQ(kShillManagerClientStubDefaultWifi, 254 EXPECT_EQ(kShillManagerClientStubDefaultWifi,
256 network_state_handler_->ConnectedNetworkByType( 255 network_state_handler_->ConnectedNetworkByType(
257 NetworkTypePattern::WiFi())->path()); 256 NetworkTypePattern::WiFi())->path());
258 EXPECT_EQ(kShillManagerClientStubCellular, 257 EXPECT_EQ(kShillManagerClientStubCellular,
259 network_state_handler_->FirstNetworkByType( 258 network_state_handler_->FirstNetworkByType(
260 NetworkTypePattern::Mobile())->path()); 259 NetworkTypePattern::Mobile())->path());
261 EXPECT_EQ( 260 EXPECT_EQ(
262 kShillManagerClientStubCellular, 261 kShillManagerClientStubCellular,
263 network_state_handler_->FirstNetworkByType(NetworkTypePattern::Cellular()) 262 network_state_handler_->FirstNetworkByType(NetworkTypePattern::Cellular())
264 ->path()); 263 ->path());
265 EXPECT_EQ(shill::kStateOnline, 264 EXPECT_EQ(shill::kStateOnline,
266 test_observer_->default_network_connection_state()); 265 test_observer_->default_network_connection_state());
267 } 266 }
268 267
269 TEST_F(NetworkStateHandlerTest, TechnologyChanged) { 268 TEST_F(NetworkStateHandlerTest, TechnologyChanged) {
270 // There may be several manager changes during initialization. 269 // Disable a technology. Will immediately set the state to AVAILABLE and
271 size_t initial_changed_count = test_observer_->device_list_changed_count(); 270 // notify observers.
272 // Disable a technology. 271 test_observer_->reset_change_counts();
273 network_state_handler_->SetTechnologyEnabled( 272 network_state_handler_->SetTechnologyEnabled(
274 NetworkTypePattern::Wimax(), false, network_handler::ErrorCallback()); 273 NetworkTypePattern::WiFi(), false, network_handler::ErrorCallback());
275 EXPECT_NE( 274 EXPECT_EQ(1u, test_observer_->device_list_changed_count());
276 NetworkStateHandler::TECHNOLOGY_ENABLED, 275 EXPECT_EQ(
277 network_state_handler_->GetTechnologyState(NetworkTypePattern::Wimax())); 276 NetworkStateHandler::TECHNOLOGY_AVAILABLE,
278 EXPECT_EQ(initial_changed_count + 1, 277 network_state_handler_->GetTechnologyState(NetworkTypePattern::WiFi()));
279 test_observer_->device_list_changed_count()); 278
280 // Enable a technology. 279 // Run the message loop. An additional notification will be received when
280 // Shill updates the enabled technologies. The state should remain AVAILABLE.
281 // Additionally, this will remove Servies, which triggers a Manager update,
282 // which will update the Enabled and Available technologies lists, as well as
283 // the device list.
284 test_observer_->reset_change_counts();
285 message_loop_.RunUntilIdle();
286 EXPECT_EQ(4u, test_observer_->device_list_changed_count());
287 EXPECT_EQ(
288 NetworkStateHandler::TECHNOLOGY_AVAILABLE,
289 network_state_handler_->GetTechnologyState(NetworkTypePattern::WiFi()));
290
291 // Enable a technology. Will immediately set the state to ENABLING and
292 // notify observers.
293 test_observer_->reset_change_counts();
281 network_state_handler_->SetTechnologyEnabled( 294 network_state_handler_->SetTechnologyEnabled(
282 NetworkTypePattern::Wimax(), true, network_handler::ErrorCallback()); 295 NetworkTypePattern::WiFi(), true, network_handler::ErrorCallback());
283 // The technology state should immediately change to ENABLING and we should 296 EXPECT_EQ(1u, test_observer_->device_list_changed_count());
284 // receive a manager changed callback.
285 EXPECT_EQ(initial_changed_count + 2,
286 test_observer_->device_list_changed_count());
287 EXPECT_EQ( 297 EXPECT_EQ(
288 NetworkStateHandler::TECHNOLOGY_ENABLING, 298 NetworkStateHandler::TECHNOLOGY_ENABLING,
289 network_state_handler_->GetTechnologyState(NetworkTypePattern::Wimax())); 299 network_state_handler_->GetTechnologyState(NetworkTypePattern::WiFi()));
300
301 // Run the message loop. State should change to ENABLED. See note above
302 // about notifications.
303 test_observer_->reset_change_counts();
290 message_loop_.RunUntilIdle(); 304 message_loop_.RunUntilIdle();
291 // Ensure we receive 2 manager changed callbacks when the technology becomes 305 EXPECT_EQ(4u, test_observer_->device_list_changed_count());
292 // avalable and enabled.
293 EXPECT_EQ(initial_changed_count + 4,
294 test_observer_->device_list_changed_count());
295 EXPECT_EQ( 306 EXPECT_EQ(
296 NetworkStateHandler::TECHNOLOGY_ENABLED, 307 NetworkStateHandler::TECHNOLOGY_ENABLED,
297 network_state_handler_->GetTechnologyState(NetworkTypePattern::Wimax())); 308 network_state_handler_->GetTechnologyState(NetworkTypePattern::WiFi()));
298 } 309 }
299 310
300 TEST_F(NetworkStateHandlerTest, TechnologyState) { 311 TEST_F(NetworkStateHandlerTest, TechnologyState) {
301 manager_test_->RemoveTechnology(shill::kTypeWimax); 312 manager_test_->RemoveTechnology(shill::kTypeWimax);
302 message_loop_.RunUntilIdle(); 313 message_loop_.RunUntilIdle();
303 EXPECT_EQ( 314 EXPECT_EQ(
304 NetworkStateHandler::TECHNOLOGY_UNAVAILABLE, 315 NetworkStateHandler::TECHNOLOGY_UNAVAILABLE,
305 network_state_handler_->GetTechnologyState(NetworkTypePattern::Wimax())); 316 network_state_handler_->GetTechnologyState(NetworkTypePattern::Wimax()));
306 317
307 manager_test_->AddTechnology(shill::kTypeWimax, false); 318 manager_test_->AddTechnology(shill::kTypeWimax, false);
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
359 370
360 TEST_F(NetworkStateHandlerTest, GetState) { 371 TEST_F(NetworkStateHandlerTest, GetState) {
361 const std::string profile = "/profile/profile1"; 372 const std::string profile = "/profile/profile1";
362 const std::string wifi_path = kShillManagerClientStubDefaultWifi; 373 const std::string wifi_path = kShillManagerClientStubDefaultWifi;
363 374
364 // Add a wifi service to a Profile. 375 // Add a wifi service to a Profile.
365 profile_test_->AddProfile(profile, "" /* userhash */); 376 profile_test_->AddProfile(profile, "" /* userhash */);
366 EXPECT_TRUE(profile_test_->AddService(profile, wifi_path)); 377 EXPECT_TRUE(profile_test_->AddService(profile, wifi_path));
367 UpdateManagerProperties(); 378 UpdateManagerProperties();
368 379
369 // Ensure that a NetworkState and corresponding FavoriteState exist. 380 // Ensure that a NetworkState exists.
370 const NetworkState* wifi_network = 381 const NetworkState* wifi_network =
371 network_state_handler_->GetNetworkState(wifi_path); 382 network_state_handler_->GetNetworkStateFromServicePath(
383 wifi_path, true /* configured_only */);
372 ASSERT_TRUE(wifi_network); 384 ASSERT_TRUE(wifi_network);
373 const FavoriteState* wifi_favorite =
374 network_state_handler_->GetFavoriteStateFromServicePath(
375 wifi_path, true /* configured_only */);
376 ASSERT_TRUE(wifi_favorite);
377 EXPECT_EQ(wifi_network->path(), wifi_favorite->path());
378
379 // Ensure that we are notified that a Favorite was added.
380 EXPECT_EQ(1u, test_observer_->favorite_count());
381 385
382 // Test looking up by GUID. 386 // Test looking up by GUID.
383 ASSERT_FALSE(wifi_favorite->guid().empty()); 387 ASSERT_FALSE(wifi_network->guid().empty());
384 const FavoriteState* wifi_favorite_guid = 388 const NetworkState* wifi_network_guid =
385 network_state_handler_->GetFavoriteStateFromGuid(wifi_favorite->guid()); 389 network_state_handler_->GetNetworkStateFromGuid(wifi_network->guid());
386 EXPECT_EQ(wifi_favorite, wifi_favorite_guid); 390 EXPECT_EQ(wifi_network, wifi_network_guid);
387 391
388 // Remove the service, verify that there is no longer a NetworkState for it. 392 // Remove the service, verify that there is no longer a NetworkState for it.
389 service_test_->RemoveService(wifi_path); 393 service_test_->RemoveService(wifi_path);
390 UpdateManagerProperties(); 394 UpdateManagerProperties();
391 EXPECT_FALSE(network_state_handler_->GetNetworkState(wifi_path)); 395 EXPECT_FALSE(network_state_handler_->GetNetworkState(wifi_path));
392 } 396 }
393 397
394 TEST_F(NetworkStateHandlerTest, NetworkConnectionStateChanged) { 398 TEST_F(NetworkStateHandlerTest, NetworkConnectionStateChanged) {
395 // Change a network state. 399 // Change a network state.
396 const std::string eth1 = kShillManagerClientStubDefaultService; 400 const std::string eth1 = kShillManagerClientStubDefaultService;
(...skipping 10 matching lines...) Expand all
407 connection_state_idle_value); 411 connection_state_idle_value);
408 message_loop_.RunUntilIdle(); 412 message_loop_.RunUntilIdle();
409 EXPECT_EQ(2, test_observer_->ConnectionStateChangesForService(eth1)); 413 EXPECT_EQ(2, test_observer_->ConnectionStateChangesForService(eth1));
410 } 414 }
411 415
412 TEST_F(NetworkStateHandlerTest, DefaultServiceDisconnected) { 416 TEST_F(NetworkStateHandlerTest, DefaultServiceDisconnected) {
413 const std::string eth1 = kShillManagerClientStubDefaultService; 417 const std::string eth1 = kShillManagerClientStubDefaultService;
414 const std::string wifi1 = kShillManagerClientStubDefaultWifi; 418 const std::string wifi1 = kShillManagerClientStubDefaultWifi;
415 419
416 // Disconnect ethernet. 420 // Disconnect ethernet.
417 test_observer_->reset_network_change_count(); 421 test_observer_->reset_change_counts();
418 base::StringValue connection_state_idle_value(shill::kStateIdle); 422 base::StringValue connection_state_idle_value(shill::kStateIdle);
419 service_test_->SetServiceProperty(eth1, shill::kStateProperty, 423 service_test_->SetServiceProperty(eth1, shill::kStateProperty,
420 connection_state_idle_value); 424 connection_state_idle_value);
421 message_loop_.RunUntilIdle(); 425 message_loop_.RunUntilIdle();
422 // Expect two changes: first when eth1 becomes disconnected, second when 426 // Expect two changes: first when eth1 becomes disconnected, second when
423 // wifi1 becomes the default. 427 // wifi1 becomes the default.
424 EXPECT_EQ(2u, test_observer_->default_network_change_count()); 428 EXPECT_EQ(2u, test_observer_->default_network_change_count());
425 EXPECT_EQ(wifi1, test_observer_->default_network()); 429 EXPECT_EQ(wifi1, test_observer_->default_network());
426 430
427 // Disconnect wifi. 431 // Disconnect wifi.
428 test_observer_->reset_network_change_count(); 432 test_observer_->reset_change_counts();
429 service_test_->SetServiceProperty(wifi1, shill::kStateProperty, 433 service_test_->SetServiceProperty(wifi1, shill::kStateProperty,
430 connection_state_idle_value); 434 connection_state_idle_value);
431 message_loop_.RunUntilIdle(); 435 message_loop_.RunUntilIdle();
432 EXPECT_EQ(1u, test_observer_->default_network_change_count()); 436 EXPECT_EQ(1u, test_observer_->default_network_change_count());
433 EXPECT_EQ("", test_observer_->default_network()); 437 EXPECT_EQ("", test_observer_->default_network());
434 } 438 }
435 439
436 TEST_F(NetworkStateHandlerTest, DefaultServiceConnected) { 440 TEST_F(NetworkStateHandlerTest, DefaultServiceConnected) {
437 const std::string eth1 = kShillManagerClientStubDefaultService; 441 const std::string eth1 = kShillManagerClientStubDefaultService;
438 const std::string wifi1 = kShillManagerClientStubDefaultWifi; 442 const std::string wifi1 = kShillManagerClientStubDefaultWifi;
439 443
440 // Disconnect ethernet and wifi. 444 // Disconnect ethernet and wifi.
441 base::StringValue connection_state_idle_value(shill::kStateIdle); 445 base::StringValue connection_state_idle_value(shill::kStateIdle);
442 service_test_->SetServiceProperty(eth1, shill::kStateProperty, 446 service_test_->SetServiceProperty(eth1, shill::kStateProperty,
443 connection_state_idle_value); 447 connection_state_idle_value);
444 service_test_->SetServiceProperty(wifi1, shill::kStateProperty, 448 service_test_->SetServiceProperty(wifi1, shill::kStateProperty,
445 connection_state_idle_value); 449 connection_state_idle_value);
446 message_loop_.RunUntilIdle(); 450 message_loop_.RunUntilIdle();
447 EXPECT_EQ(std::string(), test_observer_->default_network()); 451 EXPECT_EQ(std::string(), test_observer_->default_network());
448 452
449 // Connect ethernet, should become the default network. 453 // Connect ethernet, should become the default network.
450 test_observer_->reset_network_change_count(); 454 test_observer_->reset_change_counts();
451 base::StringValue connection_state_ready_value(shill::kStateReady); 455 base::StringValue connection_state_ready_value(shill::kStateReady);
452 service_test_->SetServiceProperty(eth1, shill::kStateProperty, 456 service_test_->SetServiceProperty(eth1, shill::kStateProperty,
453 connection_state_ready_value); 457 connection_state_ready_value);
454 message_loop_.RunUntilIdle(); 458 message_loop_.RunUntilIdle();
455 EXPECT_EQ(eth1, test_observer_->default_network()); 459 EXPECT_EQ(eth1, test_observer_->default_network());
456 EXPECT_EQ(shill::kStateReady, 460 EXPECT_EQ(shill::kStateReady,
457 test_observer_->default_network_connection_state()); 461 test_observer_->default_network_connection_state());
458 EXPECT_EQ(1u, test_observer_->default_network_change_count()); 462 EXPECT_EQ(1u, test_observer_->default_network_change_count());
459 } 463 }
460 464
461 TEST_F(NetworkStateHandlerTest, DefaultServiceChanged) { 465 TEST_F(NetworkStateHandlerTest, DefaultServiceChanged) {
462 const std::string eth1 = kShillManagerClientStubDefaultService; 466 const std::string eth1 = kShillManagerClientStubDefaultService;
463 // The default service should be eth1. 467 // The default service should be eth1.
464 EXPECT_EQ(eth1, test_observer_->default_network()); 468 EXPECT_EQ(eth1, test_observer_->default_network());
465 469
466 // Change the default network by changing Manager.DefaultService. 470 // Change the default network by changing Manager.DefaultService.
467 test_observer_->reset_network_change_count(); 471 test_observer_->reset_change_counts();
468 const std::string wifi1 = kShillManagerClientStubDefaultWifi; 472 const std::string wifi1 = kShillManagerClientStubDefaultWifi;
469 base::StringValue wifi1_value(wifi1); 473 base::StringValue wifi1_value(wifi1);
470 manager_test_->SetManagerProperty( 474 manager_test_->SetManagerProperty(
471 shill::kDefaultServiceProperty, wifi1_value); 475 shill::kDefaultServiceProperty, wifi1_value);
472 message_loop_.RunUntilIdle(); 476 message_loop_.RunUntilIdle();
473 EXPECT_EQ(wifi1, test_observer_->default_network()); 477 EXPECT_EQ(wifi1, test_observer_->default_network());
474 EXPECT_EQ(1u, test_observer_->default_network_change_count()); 478 EXPECT_EQ(1u, test_observer_->default_network_change_count());
475 479
476 // Change the state of the default network. 480 // Change the state of the default network.
477 test_observer_->reset_network_change_count(); 481 test_observer_->reset_change_counts();
478 base::StringValue connection_state_ready_value(shill::kStateReady); 482 base::StringValue connection_state_ready_value(shill::kStateReady);
479 service_test_->SetServiceProperty(wifi1, shill::kStateProperty, 483 service_test_->SetServiceProperty(wifi1, shill::kStateProperty,
480 connection_state_ready_value); 484 connection_state_ready_value);
481 message_loop_.RunUntilIdle(); 485 message_loop_.RunUntilIdle();
482 EXPECT_EQ(shill::kStateReady, 486 EXPECT_EQ(shill::kStateReady,
483 test_observer_->default_network_connection_state()); 487 test_observer_->default_network_connection_state());
484 EXPECT_EQ(1u, test_observer_->default_network_change_count()); 488 EXPECT_EQ(1u, test_observer_->default_network_change_count());
485 489
486 // Updating a property on the default network should trigger 490 // Updating a property on the default network should trigger
487 // a default network change. 491 // a default network change.
488 test_observer_->reset_network_change_count(); 492 test_observer_->reset_change_counts();
489 DBusThreadManager::Get()->GetShillServiceClient()->SetProperty( 493 DBusThreadManager::Get()->GetShillServiceClient()->SetProperty(
490 dbus::ObjectPath(wifi1), 494 dbus::ObjectPath(wifi1),
491 shill::kSecurityProperty, base::StringValue("TestSecurity"), 495 shill::kSecurityProperty, base::StringValue("TestSecurity"),
492 base::Bind(&base::DoNothing), base::Bind(&ErrorCallbackFunction)); 496 base::Bind(&base::DoNothing), base::Bind(&ErrorCallbackFunction));
493 message_loop_.RunUntilIdle(); 497 message_loop_.RunUntilIdle();
494 EXPECT_EQ(1u, test_observer_->default_network_change_count()); 498 EXPECT_EQ(1u, test_observer_->default_network_change_count());
495 499
496 // No default network updates for signal strength changes. 500 // No default network updates for signal strength changes.
497 test_observer_->reset_network_change_count(); 501 test_observer_->reset_change_counts();
498 DBusThreadManager::Get()->GetShillServiceClient()->SetProperty( 502 DBusThreadManager::Get()->GetShillServiceClient()->SetProperty(
499 dbus::ObjectPath(wifi1), 503 dbus::ObjectPath(wifi1),
500 shill::kSignalStrengthProperty, base::FundamentalValue(32), 504 shill::kSignalStrengthProperty, base::FundamentalValue(32),
501 base::Bind(&base::DoNothing), base::Bind(&ErrorCallbackFunction)); 505 base::Bind(&base::DoNothing), base::Bind(&ErrorCallbackFunction));
502 message_loop_.RunUntilIdle(); 506 message_loop_.RunUntilIdle();
503 EXPECT_EQ(0u, test_observer_->default_network_change_count()); 507 EXPECT_EQ(0u, test_observer_->default_network_change_count());
504 } 508 }
505 509
506 TEST_F(NetworkStateHandlerTest, RequestUpdate) { 510 TEST_F(NetworkStateHandlerTest, RequestUpdate) {
507 // Request an update for kShillManagerClientStubDefaultWifi. 511 // Request an update for kShillManagerClientStubDefaultWifi.
508 EXPECT_EQ(1, test_observer_->PropertyUpdatesForService( 512 EXPECT_EQ(1, test_observer_->PropertyUpdatesForService(
509 kShillManagerClientStubDefaultWifi)); 513 kShillManagerClientStubDefaultWifi));
510 network_state_handler_->RequestUpdateForNetwork( 514 network_state_handler_->RequestUpdateForNetwork(
511 kShillManagerClientStubDefaultWifi); 515 kShillManagerClientStubDefaultWifi);
512 message_loop_.RunUntilIdle(); 516 message_loop_.RunUntilIdle();
513 EXPECT_EQ(2, test_observer_->PropertyUpdatesForService( 517 EXPECT_EQ(2, test_observer_->PropertyUpdatesForService(
514 kShillManagerClientStubDefaultWifi)); 518 kShillManagerClientStubDefaultWifi));
515 } 519 }
516 520
517 TEST_F(NetworkStateHandlerTest, NetworkGuidInProfile) { 521 TEST_F(NetworkStateHandlerTest, NetworkGuidInProfile) {
518 const std::string profile = "/profile/profile1"; 522 const std::string profile = "/profile/profile1";
519 const std::string wifi_path = "wifi_with_guid"; 523 const std::string wifi_path = "/service/wifi_with_guid";
520 const std::string wifi_guid = "WIFI_GUID"; 524 const std::string wifi_guid = "WIFI_GUID";
521 const bool is_service_configured = true; 525 const bool is_service_configured = true;
522 526
523 // Add a network to the default Profile with a specified GUID. 527 // Add a network to the default Profile with a specified GUID.
524 service_test_->AddServiceWithIPConfig( 528 service_test_->AddServiceWithIPConfig(
525 wifi_path, 529 wifi_path,
526 wifi_guid, 530 wifi_guid,
527 wifi_path /* name */, 531 wifi_path /* name */,
528 shill::kTypeWifi, 532 shill::kTypeWifi,
529 shill::kStateOnline, 533 shill::kStateOnline,
530 "" /* ipconfig_path */, 534 "" /* ipconfig_path */,
531 true /* add_to_visible */, 535 true /* add_to_visible */,
532 true /* add_to_watchlist */); 536 true /* add_to_watchlist */);
533 profile_test_->AddProfile(profile, "" /* userhash */); 537 profile_test_->AddProfile(profile, "" /* userhash */);
534 EXPECT_TRUE(profile_test_->AddService(profile, wifi_path)); 538 EXPECT_TRUE(profile_test_->AddService(profile, wifi_path));
535 UpdateManagerProperties(); 539 UpdateManagerProperties();
536 540
537 // Verify that a FavoriteState exists with a matching GUID. 541 // Verify that a NetworkState exists with a matching GUID.
538 const FavoriteState* favorite = 542 const NetworkState* network =
539 network_state_handler_->GetFavoriteStateFromServicePath( 543 network_state_handler_->GetNetworkStateFromServicePath(
540 wifi_path, is_service_configured); 544 wifi_path, is_service_configured);
541 ASSERT_TRUE(favorite);
542 EXPECT_EQ(wifi_guid, favorite->guid());
543
544 // Verify that a NetworkState exists with the same GUID.
545 const NetworkState* network =
546 network_state_handler_->GetNetworkState(wifi_path);
547 ASSERT_TRUE(network); 545 ASSERT_TRUE(network);
548 EXPECT_EQ(wifi_guid, network->guid()); 546 EXPECT_EQ(wifi_guid, network->guid());
549 547
550 // Remove the service (simulating a network going out of range). 548 // Remove the service (simulating a network going out of range).
551 service_test_->RemoveService(wifi_path); 549 service_test_->RemoveService(wifi_path);
552 UpdateManagerProperties(); 550 UpdateManagerProperties();
553 EXPECT_FALSE(network_state_handler_->GetNetworkState(wifi_path)); 551 EXPECT_FALSE(network_state_handler_->GetNetworkState(wifi_path));
554 552
555 // Add the service (simulating a network coming back in range) and verify that 553 // Add the service (simulating a network coming back in range) and verify that
556 // the NetworkState was created with the same GUID. 554 // the NetworkState was created with the same GUID.
557 AddService(wifi_path, wifi_path, shill::kTypeWifi, shill::kStateOnline); 555 AddService(wifi_path, wifi_path, shill::kTypeWifi, shill::kStateOnline);
558 UpdateManagerProperties(); 556 UpdateManagerProperties();
559 network = network_state_handler_->GetNetworkState(wifi_path); 557 network = network_state_handler_->GetNetworkStateFromServicePath(
558 wifi_path, is_service_configured);
560 ASSERT_TRUE(network); 559 ASSERT_TRUE(network);
561 EXPECT_EQ(wifi_guid, network->guid()); 560 EXPECT_EQ(wifi_guid, network->guid());
562
563 // Also verify FavoriteState (mostly to test the stub behavior).
564 favorite = network_state_handler_->GetFavoriteStateFromServicePath(
565 wifi_path, is_service_configured);
566 ASSERT_TRUE(favorite);
567 EXPECT_EQ(wifi_guid, favorite->guid());
568 } 561 }
569 562
570 TEST_F(NetworkStateHandlerTest, NetworkGuidNotInProfile) { 563 TEST_F(NetworkStateHandlerTest, NetworkGuidNotInProfile) {
571 const std::string wifi_path = "wifi_with_guid"; 564 const std::string wifi_path = "/service/wifi_with_guid";
572 const bool is_service_configured = false; 565 const bool is_service_configured = false;
573 566
574 // Add a network without adding it to a profile. 567 // Add a network without adding it to a profile.
575 AddService(wifi_path, wifi_path, shill::kTypeWifi, shill::kStateOnline); 568 AddService(wifi_path, wifi_path, shill::kTypeWifi, shill::kStateOnline);
576 UpdateManagerProperties(); 569 UpdateManagerProperties();
577 570
578 // Verify that a FavoriteState exists with an assigned GUID. 571 // Verify that a NetworkState exists with an assigned GUID.
579 const FavoriteState* favorite = 572 const NetworkState* network =
580 network_state_handler_->GetFavoriteStateFromServicePath( 573 network_state_handler_->GetNetworkStateFromServicePath(
581 wifi_path, is_service_configured); 574 wifi_path, is_service_configured);
582 ASSERT_TRUE(favorite); 575 ASSERT_TRUE(network);
583 std::string wifi_guid = favorite->guid(); 576 std::string wifi_guid = network->guid();
584 EXPECT_FALSE(wifi_guid.empty()); 577 EXPECT_FALSE(wifi_guid.empty());
585 578
586 // Verify that a NetworkState exists with the same GUID.
587 const NetworkState* network =
588 network_state_handler_->GetNetworkState(wifi_path);
589 ASSERT_TRUE(network);
590 EXPECT_EQ(wifi_guid, network->guid());
591
592 // Remove the service (simulating a network going out of range). 579 // Remove the service (simulating a network going out of range).
593 service_test_->RemoveService(wifi_path); 580 service_test_->RemoveService(wifi_path);
594 UpdateManagerProperties(); 581 UpdateManagerProperties();
595 EXPECT_FALSE(network_state_handler_->GetNetworkState(wifi_path)); 582 EXPECT_FALSE(network_state_handler_->GetNetworkState(wifi_path));
596 583
597 // Add the service (simulating a network coming back in range) and verify that 584 // Add the service (simulating a network coming back in range) and verify that
598 // the NetworkState was created with the same GUID. 585 // the NetworkState was created with the same GUID.
599 AddService(wifi_path, wifi_path, shill::kTypeWifi, shill::kStateOnline); 586 AddService(wifi_path, wifi_path, shill::kTypeWifi, shill::kStateOnline);
600 UpdateManagerProperties(); 587 UpdateManagerProperties();
601 network = network_state_handler_->GetNetworkState(wifi_path); 588 network = network_state_handler_->GetNetworkStateFromServicePath(
589 wifi_path, is_service_configured);
602 ASSERT_TRUE(network); 590 ASSERT_TRUE(network);
603 EXPECT_EQ(wifi_guid, network->guid()); 591 EXPECT_EQ(wifi_guid, network->guid());
604
605 // Also verify FavoriteState (mostly to test the stub behavior).
606 favorite = network_state_handler_->GetFavoriteStateFromServicePath(
607 wifi_path, is_service_configured);
608 ASSERT_TRUE(favorite);
609 EXPECT_EQ(wifi_guid, favorite->guid());
610 } 592 }
611 593
612 } // namespace chromeos 594 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698