Instrumenting context getter and predictor to find jank.
[chromium-blink-merge.git] / chromeos / network / network_state_handler_unittest.cc
blobc118e1911137194081850094d86d43a5788a82fb
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.
5 #include "chromeos/network/network_state_handler.h"
7 #include <map>
8 #include <set>
9 #include <string>
11 #include "base/bind.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/message_loop/message_loop.h"
14 #include "base/values.h"
15 #include "chromeos/dbus/dbus_thread_manager.h"
16 #include "chromeos/dbus/shill_device_client.h"
17 #include "chromeos/dbus/shill_ipconfig_client.h"
18 #include "chromeos/dbus/shill_manager_client.h"
19 #include "chromeos/dbus/shill_profile_client.h"
20 #include "chromeos/dbus/shill_service_client.h"
21 #include "chromeos/network/device_state.h"
22 #include "chromeos/network/network_state.h"
23 #include "chromeos/network/network_state_handler.h"
24 #include "chromeos/network/network_state_handler_observer.h"
25 #include "dbus/object_path.h"
26 #include "testing/gtest/include/gtest/gtest.h"
27 #include "third_party/cros_system_api/dbus/service_constants.h"
29 namespace {
31 void ErrorCallbackFunction(const std::string& error_name,
32 const std::string& error_message) {
33 LOG(ERROR) << "Shill Error: " << error_name << " : " << error_message;
36 const std::string kShillManagerClientStubWifiDevice =
37 "/device/stub_wifi_device1";
38 const std::string kShillManagerClientStubCellularDevice =
39 "/device/stub_cellular_device1";
40 const std::string kShillManagerClientStubDefaultService = "/service/eth1";
41 const std::string kShillManagerClientStubDefaultWifi = "/service/wifi1";
42 const std::string kShillManagerClientStubWifi2 = "/service/wifi2";
43 const std::string kShillManagerClientStubCellular = "/service/cellular1";
45 using chromeos::DeviceState;
46 using chromeos::NetworkState;
47 using chromeos::NetworkStateHandler;
49 class TestObserver : public chromeos::NetworkStateHandlerObserver {
50 public:
51 explicit TestObserver(NetworkStateHandler* handler)
52 : handler_(handler),
53 device_list_changed_count_(0),
54 device_count_(0),
55 network_list_changed_count_(0),
56 network_count_(0),
57 default_network_change_count_(0) {
60 virtual ~TestObserver() {
63 virtual void DeviceListChanged() override {
64 NetworkStateHandler::DeviceStateList devices;
65 handler_->GetDeviceList(&devices);
66 device_count_ = devices.size();
67 ++device_list_changed_count_;
70 virtual void NetworkListChanged() override {
71 NetworkStateHandler::NetworkStateList networks;
72 handler_->GetNetworkListByType(chromeos::NetworkTypePattern::Default(),
73 false /* configured_only */,
74 false /* visible_only */,
75 0 /* no limit */,
76 &networks);
77 network_count_ = networks.size();
78 if (network_count_ == 0) {
79 default_network_ = "";
80 default_network_connection_state_ = "";
82 ++network_list_changed_count_;
85 virtual void DefaultNetworkChanged(const NetworkState* network) override {
86 ++default_network_change_count_;
87 default_network_ = network ? network->path() : "";
88 default_network_connection_state_ =
89 network ? network->connection_state() : "";
90 DVLOG(1) << "DefaultNetworkChanged: " << default_network_
91 << " State: " << default_network_connection_state_;
94 virtual void NetworkConnectionStateChanged(
95 const NetworkState* network) override {
96 network_connection_state_[network->path()] = network->connection_state();
97 connection_state_changes_[network->path()]++;
100 virtual void NetworkPropertiesUpdated(const NetworkState* network) override {
101 DCHECK(network);
102 property_updates_[network->path()]++;
105 virtual void DevicePropertiesUpdated(const DeviceState* device) override {
106 DCHECK(device);
107 device_property_updates_[device->path()]++;
110 size_t device_list_changed_count() { return device_list_changed_count_; }
111 size_t device_count() { return device_count_; }
112 size_t network_list_changed_count() { return network_list_changed_count_; }
113 size_t network_count() { return network_count_; }
114 size_t default_network_change_count() {
115 return default_network_change_count_;
117 void reset_change_counts() {
118 DVLOG(1) << "=== RESET CHANGE COUNTS ===";
119 default_network_change_count_ = 0;
120 device_list_changed_count_ = 0;
121 network_list_changed_count_ = 0;
122 connection_state_changes_.clear();
124 void reset_updates() {
125 property_updates_.clear();
126 device_property_updates_.clear();
128 std::string default_network() { return default_network_; }
129 std::string default_network_connection_state() {
130 return default_network_connection_state_;
133 int PropertyUpdatesForService(const std::string& service_path) {
134 return property_updates_[service_path];
137 int PropertyUpdatesForDevice(const std::string& device_path) {
138 return device_property_updates_[device_path];
141 int ConnectionStateChangesForService(const std::string& service_path) {
142 return connection_state_changes_[service_path];
145 std::string NetworkConnectionStateForService(
146 const std::string& service_path) {
147 return network_connection_state_[service_path];
150 private:
151 NetworkStateHandler* handler_;
152 size_t device_list_changed_count_;
153 size_t device_count_;
154 size_t network_list_changed_count_;
155 size_t network_count_;
156 size_t default_network_change_count_;
157 std::string default_network_;
158 std::string default_network_connection_state_;
159 std::map<std::string, int> property_updates_;
160 std::map<std::string, int> device_property_updates_;
161 std::map<std::string, int> connection_state_changes_;
162 std::map<std::string, std::string> network_connection_state_;
164 DISALLOW_COPY_AND_ASSIGN(TestObserver);
167 } // namespace
169 namespace chromeos {
171 class NetworkStateHandlerTest : public testing::Test {
172 public:
173 NetworkStateHandlerTest()
174 : device_test_(NULL),
175 manager_test_(NULL),
176 profile_test_(NULL),
177 service_test_(NULL) {}
178 virtual ~NetworkStateHandlerTest() {}
180 virtual void SetUp() override {
181 // Initialize DBusThreadManager with a stub implementation.
182 DBusThreadManager::Initialize();
183 SetupDefaultShillState();
184 network_state_handler_.reset(new NetworkStateHandler);
185 test_observer_.reset(new TestObserver(network_state_handler_.get()));
186 network_state_handler_->AddObserver(test_observer_.get(), FROM_HERE);
187 network_state_handler_->InitShillPropertyHandler();
188 message_loop_.RunUntilIdle();
189 test_observer_->reset_change_counts();
192 virtual void TearDown() override {
193 network_state_handler_->RemoveObserver(test_observer_.get(), FROM_HERE);
194 test_observer_.reset();
195 network_state_handler_.reset();
196 DBusThreadManager::Shutdown();
199 protected:
200 void AddService(const std::string& service_path,
201 const std::string& guid,
202 const std::string& name,
203 const std::string& type,
204 const std::string& state) {
205 service_test_->AddService(service_path, guid, name, type, state,
206 true /* add_to_visible */);
209 void SetupDefaultShillState() {
210 message_loop_.RunUntilIdle(); // Process any pending updates
211 device_test_ =
212 DBusThreadManager::Get()->GetShillDeviceClient()->GetTestInterface();
213 ASSERT_TRUE(device_test_);
214 device_test_->ClearDevices();
215 device_test_->AddDevice(kShillManagerClientStubWifiDevice,
216 shill::kTypeWifi, "stub_wifi_device1");
217 device_test_->AddDevice(kShillManagerClientStubCellularDevice,
218 shill::kTypeCellular, "stub_cellular_device1");
220 manager_test_ =
221 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface();
222 ASSERT_TRUE(manager_test_);
224 profile_test_ =
225 DBusThreadManager::Get()->GetShillProfileClient()->GetTestInterface();
226 ASSERT_TRUE(profile_test_);
227 profile_test_->ClearProfiles();
229 service_test_ =
230 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface();
231 ASSERT_TRUE(service_test_);
232 service_test_->ClearServices();
233 AddService(kShillManagerClientStubDefaultService,
234 "eth1_guid",
235 "eth1",
236 shill::kTypeEthernet,
237 shill::kStateOnline);
238 AddService(kShillManagerClientStubDefaultWifi,
239 "wifi1_guid",
240 "wifi1",
241 shill::kTypeWifi,
242 shill::kStateOnline);
243 AddService(kShillManagerClientStubWifi2,
244 "wifi2_guid",
245 "wifi2",
246 shill::kTypeWifi,
247 shill::kStateIdle);
248 AddService(kShillManagerClientStubCellular,
249 "cellular1_guid",
250 "cellular1",
251 shill::kTypeCellular,
252 shill::kStateIdle);
255 void UpdateManagerProperties() {
256 message_loop_.RunUntilIdle();
259 base::MessageLoopForUI message_loop_;
260 scoped_ptr<NetworkStateHandler> network_state_handler_;
261 scoped_ptr<TestObserver> test_observer_;
262 ShillDeviceClient::TestInterface* device_test_;
263 ShillManagerClient::TestInterface* manager_test_;
264 ShillProfileClient::TestInterface* profile_test_;
265 ShillServiceClient::TestInterface* service_test_;
267 private:
268 DISALLOW_COPY_AND_ASSIGN(NetworkStateHandlerTest);
271 TEST_F(NetworkStateHandlerTest, NetworkStateHandlerStub) {
272 // Ensure that the device and network list are the expected size.
273 const size_t kNumShillManagerClientStubImplDevices = 2;
274 EXPECT_EQ(kNumShillManagerClientStubImplDevices,
275 test_observer_->device_count());
276 const size_t kNumShillManagerClientStubImplServices = 4;
277 EXPECT_EQ(kNumShillManagerClientStubImplServices,
278 test_observer_->network_count());
279 // Ensure that the first stub network is the default network.
280 EXPECT_EQ(kShillManagerClientStubDefaultService,
281 test_observer_->default_network());
282 ASSERT_TRUE(network_state_handler_->DefaultNetwork());
283 EXPECT_EQ(kShillManagerClientStubDefaultService,
284 network_state_handler_->DefaultNetwork()->path());
285 EXPECT_EQ(kShillManagerClientStubDefaultService,
286 network_state_handler_->ConnectedNetworkByType(
287 NetworkTypePattern::Ethernet())->path());
288 EXPECT_EQ(kShillManagerClientStubDefaultWifi,
289 network_state_handler_->ConnectedNetworkByType(
290 NetworkTypePattern::WiFi())->path());
291 EXPECT_EQ(kShillManagerClientStubCellular,
292 network_state_handler_->FirstNetworkByType(
293 NetworkTypePattern::Mobile())->path());
294 EXPECT_EQ(
295 kShillManagerClientStubCellular,
296 network_state_handler_->FirstNetworkByType(NetworkTypePattern::Cellular())
297 ->path());
298 EXPECT_EQ(shill::kStateOnline,
299 test_observer_->default_network_connection_state());
302 TEST_F(NetworkStateHandlerTest, GetNetworkList) {
303 // Ensure that the network list is the expected size.
304 const size_t kNumShillManagerClientStubImplServices = 4;
305 EXPECT_EQ(kNumShillManagerClientStubImplServices,
306 test_observer_->network_count());
307 // Add a non-visible network to the profile.
308 const std::string profile = "/profile/profile1";
309 const std::string wifi_favorite_path = "/service/wifi_faviorite";
310 service_test_->AddService(wifi_favorite_path,
311 "wifi_faviorite_guid",
312 "wifi_faviorite",
313 shill::kTypeWifi,
314 shill::kStateIdle,
315 false /* add_to_visible */);
316 profile_test_->AddProfile(profile, "" /* userhash */);
317 EXPECT_TRUE(profile_test_->AddService(profile, wifi_favorite_path));
318 UpdateManagerProperties();
319 message_loop_.RunUntilIdle();
320 EXPECT_EQ(kNumShillManagerClientStubImplServices + 1,
321 test_observer_->network_count());
323 // Get all networks.
324 NetworkStateHandler::NetworkStateList networks;
325 network_state_handler_->GetNetworkListByType(NetworkTypePattern::Default(),
326 false /* configured_only */,
327 false /* visible_only */,
328 0 /* no limit */,
329 &networks);
330 EXPECT_EQ(kNumShillManagerClientStubImplServices + 1, networks.size());
331 // Limit number of results.
332 network_state_handler_->GetNetworkListByType(NetworkTypePattern::Default(),
333 false /* configured_only */,
334 false /* visible_only */,
335 2 /* limit */,
336 &networks);
337 EXPECT_EQ(2u, networks.size());
338 // Get all wifi networks.
339 network_state_handler_->GetNetworkListByType(NetworkTypePattern::WiFi(),
340 false /* configured_only */,
341 false /* visible_only */,
342 0 /* no limit */,
343 &networks);
344 EXPECT_EQ(3u, networks.size());
345 // Get visible networks.
346 network_state_handler_->GetNetworkListByType(NetworkTypePattern::Default(),
347 false /* configured_only */,
348 true /* visible_only */,
349 0 /* no limit */,
350 &networks);
351 EXPECT_EQ(kNumShillManagerClientStubImplServices, networks.size());
352 network_state_handler_->GetVisibleNetworkList(&networks);
353 EXPECT_EQ(kNumShillManagerClientStubImplServices, networks.size());
354 // Get configured (profile) networks.
355 network_state_handler_->GetNetworkListByType(NetworkTypePattern::Default(),
356 true /* configured_only */,
357 false /* visible_only */,
358 0 /* no limit */,
359 &networks);
360 EXPECT_EQ(1u, networks.size());
363 TEST_F(NetworkStateHandlerTest, NetworkListChanged) {
364 size_t stub_network_count = test_observer_->network_count();
365 // Set up two additional visible networks.
366 const std::string wifi3 = "/service/wifi3";
367 const std::string wifi4 = "/service/wifi4";
368 service_test_->SetServiceProperties(
369 wifi3, "wifi3_guid", "wifi3",
370 shill::kTypeWifi, shill::kStateIdle, true /* visible */);
371 service_test_->SetServiceProperties(
372 wifi4, "wifi4_guid", "wifi4",
373 shill::kTypeWifi, shill::kStateIdle, true /* visible */);
374 // Add the services to the Manager. Only notify when the second service is
375 // added.
376 manager_test_->AddManagerService(wifi3, false);
377 manager_test_->AddManagerService(wifi4, true);
378 UpdateManagerProperties();
379 // Expect two service updates and one list update.
380 EXPECT_EQ(stub_network_count + 2, test_observer_->network_count());
381 EXPECT_EQ(1, test_observer_->PropertyUpdatesForService(wifi3));
382 EXPECT_EQ(1, test_observer_->PropertyUpdatesForService(wifi4));
383 EXPECT_EQ(1u, test_observer_->network_list_changed_count());
386 TEST_F(NetworkStateHandlerTest, GetVisibleNetworks) {
387 // Ensure that the network list is the expected size.
388 const size_t kNumShillManagerClientStubImplServices = 4;
389 EXPECT_EQ(kNumShillManagerClientStubImplServices,
390 test_observer_->network_count());
391 // Add a non-visible network to the profile.
392 const std::string profile = "/profile/profile1";
393 const std::string wifi_favorite_path = "/service/wifi_faviorite";
394 service_test_->AddService(wifi_favorite_path,
395 "wifi_faviorite_guid",
396 "wifi_faviorite",
397 shill::kTypeWifi,
398 shill::kStateIdle,
399 false /* add_to_visible */);
400 message_loop_.RunUntilIdle();
401 EXPECT_EQ(kNumShillManagerClientStubImplServices + 1,
402 test_observer_->network_count());
404 // Get visible networks.
405 NetworkStateHandler::NetworkStateList networks;
406 network_state_handler_->GetVisibleNetworkList(&networks);
407 EXPECT_EQ(kNumShillManagerClientStubImplServices, networks.size());
409 // Change the visible state of a network.
410 DBusThreadManager::Get()->GetShillServiceClient()->SetProperty(
411 dbus::ObjectPath(kShillManagerClientStubWifi2),
412 shill::kVisibleProperty, base::FundamentalValue(false),
413 base::Bind(&base::DoNothing), base::Bind(&ErrorCallbackFunction));
414 message_loop_.RunUntilIdle();
415 network_state_handler_->GetVisibleNetworkList(&networks);
416 EXPECT_EQ(kNumShillManagerClientStubImplServices - 1, networks.size());
419 TEST_F(NetworkStateHandlerTest, TechnologyChanged) {
420 // Disable a technology. Will immediately set the state to AVAILABLE and
421 // notify observers.
422 network_state_handler_->SetTechnologyEnabled(
423 NetworkTypePattern::WiFi(), false, network_handler::ErrorCallback());
424 EXPECT_EQ(1u, test_observer_->device_list_changed_count());
425 EXPECT_EQ(
426 NetworkStateHandler::TECHNOLOGY_AVAILABLE,
427 network_state_handler_->GetTechnologyState(NetworkTypePattern::WiFi()));
429 // Run the message loop. An additional notification will be received when
430 // Shill updates the enabled technologies. The state should remain AVAILABLE.
431 test_observer_->reset_change_counts();
432 message_loop_.RunUntilIdle();
433 EXPECT_EQ(1u, test_observer_->device_list_changed_count());
434 EXPECT_EQ(
435 NetworkStateHandler::TECHNOLOGY_AVAILABLE,
436 network_state_handler_->GetTechnologyState(NetworkTypePattern::WiFi()));
438 // Enable a technology. Will immediately set the state to ENABLING and
439 // notify observers.
440 test_observer_->reset_change_counts();
441 network_state_handler_->SetTechnologyEnabled(
442 NetworkTypePattern::WiFi(), true, network_handler::ErrorCallback());
443 EXPECT_EQ(1u, test_observer_->device_list_changed_count());
444 EXPECT_EQ(
445 NetworkStateHandler::TECHNOLOGY_ENABLING,
446 network_state_handler_->GetTechnologyState(NetworkTypePattern::WiFi()));
448 // Run the message loop. State should change to ENABLED.
449 test_observer_->reset_change_counts();
450 message_loop_.RunUntilIdle();
451 EXPECT_EQ(1u, test_observer_->device_list_changed_count());
452 EXPECT_EQ(
453 NetworkStateHandler::TECHNOLOGY_ENABLED,
454 network_state_handler_->GetTechnologyState(NetworkTypePattern::WiFi()));
457 TEST_F(NetworkStateHandlerTest, TechnologyState) {
458 manager_test_->RemoveTechnology(shill::kTypeWimax);
459 message_loop_.RunUntilIdle();
460 EXPECT_EQ(
461 NetworkStateHandler::TECHNOLOGY_UNAVAILABLE,
462 network_state_handler_->GetTechnologyState(NetworkTypePattern::Wimax()));
464 manager_test_->AddTechnology(shill::kTypeWimax, false);
465 message_loop_.RunUntilIdle();
466 EXPECT_EQ(
467 NetworkStateHandler::TECHNOLOGY_AVAILABLE,
468 network_state_handler_->GetTechnologyState(NetworkTypePattern::Wimax()));
470 manager_test_->SetTechnologyInitializing(shill::kTypeWimax, true);
471 message_loop_.RunUntilIdle();
472 EXPECT_EQ(
473 NetworkStateHandler::TECHNOLOGY_UNINITIALIZED,
474 network_state_handler_->GetTechnologyState(NetworkTypePattern::Wimax()));
476 manager_test_->SetTechnologyInitializing(shill::kTypeWimax, false);
477 network_state_handler_->SetTechnologyEnabled(
478 NetworkTypePattern::Wimax(), true, network_handler::ErrorCallback());
479 message_loop_.RunUntilIdle();
480 EXPECT_EQ(
481 NetworkStateHandler::TECHNOLOGY_ENABLED,
482 network_state_handler_->GetTechnologyState(NetworkTypePattern::Wimax()));
484 manager_test_->RemoveTechnology(shill::kTypeWimax);
485 message_loop_.RunUntilIdle();
486 EXPECT_EQ(
487 NetworkStateHandler::TECHNOLOGY_UNAVAILABLE,
488 network_state_handler_->GetTechnologyState(NetworkTypePattern::Wimax()));
491 TEST_F(NetworkStateHandlerTest, ServicePropertyChanged) {
492 // Set a service property.
493 const std::string eth1 = kShillManagerClientStubDefaultService;
494 const NetworkState* ethernet = network_state_handler_->GetNetworkState(eth1);
495 ASSERT_TRUE(ethernet);
496 EXPECT_EQ("", ethernet->security());
497 EXPECT_EQ(1, test_observer_->PropertyUpdatesForService(eth1));
498 base::StringValue security_value("TestSecurity");
499 DBusThreadManager::Get()->GetShillServiceClient()->SetProperty(
500 dbus::ObjectPath(eth1),
501 shill::kSecurityProperty, security_value,
502 base::Bind(&base::DoNothing), base::Bind(&ErrorCallbackFunction));
503 message_loop_.RunUntilIdle();
504 ethernet = network_state_handler_->GetNetworkState(eth1);
505 EXPECT_EQ("TestSecurity", ethernet->security());
506 EXPECT_EQ(2, test_observer_->PropertyUpdatesForService(eth1));
508 // Changing a service to the existing value should not trigger an update.
509 DBusThreadManager::Get()->GetShillServiceClient()->SetProperty(
510 dbus::ObjectPath(eth1),
511 shill::kSecurityProperty, security_value,
512 base::Bind(&base::DoNothing), base::Bind(&ErrorCallbackFunction));
513 message_loop_.RunUntilIdle();
514 EXPECT_EQ(2, test_observer_->PropertyUpdatesForService(eth1));
517 TEST_F(NetworkStateHandlerTest, GetState) {
518 const std::string profile = "/profile/profile1";
519 const std::string wifi_path = kShillManagerClientStubDefaultWifi;
521 // Add a wifi service to a Profile.
522 profile_test_->AddProfile(profile, "" /* userhash */);
523 EXPECT_TRUE(profile_test_->AddService(profile, wifi_path));
524 UpdateManagerProperties();
526 // Ensure that a NetworkState exists.
527 const NetworkState* wifi_network =
528 network_state_handler_->GetNetworkStateFromServicePath(
529 wifi_path, true /* configured_only */);
530 ASSERT_TRUE(wifi_network);
532 // Test looking up by GUID.
533 ASSERT_FALSE(wifi_network->guid().empty());
534 const NetworkState* wifi_network_guid =
535 network_state_handler_->GetNetworkStateFromGuid(wifi_network->guid());
536 EXPECT_EQ(wifi_network, wifi_network_guid);
538 // Remove the service, verify that there is no longer a NetworkState for it.
539 service_test_->RemoveService(wifi_path);
540 UpdateManagerProperties();
541 EXPECT_FALSE(network_state_handler_->GetNetworkState(wifi_path));
544 TEST_F(NetworkStateHandlerTest, NetworkConnectionStateChanged) {
545 const std::string eth1 = kShillManagerClientStubDefaultService;
546 EXPECT_EQ(0, test_observer_->ConnectionStateChangesForService(eth1));
548 // Change a network state.
549 base::StringValue connection_state_idle_value(shill::kStateIdle);
550 service_test_->SetServiceProperty(eth1, shill::kStateProperty,
551 connection_state_idle_value);
552 message_loop_.RunUntilIdle();
553 EXPECT_EQ(shill::kStateIdle,
554 test_observer_->NetworkConnectionStateForService(eth1));
555 EXPECT_EQ(1, test_observer_->ConnectionStateChangesForService(eth1));
556 // Confirm that changing the connection state to the same value does *not*
557 // signal the observer.
558 service_test_->SetServiceProperty(eth1, shill::kStateProperty,
559 connection_state_idle_value);
560 message_loop_.RunUntilIdle();
561 EXPECT_EQ(1, test_observer_->ConnectionStateChangesForService(eth1));
564 TEST_F(NetworkStateHandlerTest, DefaultServiceDisconnected) {
565 const std::string eth1 = kShillManagerClientStubDefaultService;
566 const std::string wifi1 = kShillManagerClientStubDefaultWifi;
568 EXPECT_EQ(0u, test_observer_->default_network_change_count());
569 // Disconnect ethernet.
570 base::StringValue connection_state_idle_value(shill::kStateIdle);
571 service_test_->SetServiceProperty(eth1, shill::kStateProperty,
572 connection_state_idle_value);
573 message_loop_.RunUntilIdle();
574 // Expect two changes: first when eth1 becomes disconnected, second when
575 // wifi1 becomes the default.
576 EXPECT_EQ(2u, test_observer_->default_network_change_count());
577 EXPECT_EQ(wifi1, test_observer_->default_network());
579 // Disconnect wifi.
580 test_observer_->reset_change_counts();
581 service_test_->SetServiceProperty(wifi1, shill::kStateProperty,
582 connection_state_idle_value);
583 message_loop_.RunUntilIdle();
584 EXPECT_EQ(1u, test_observer_->default_network_change_count());
585 EXPECT_EQ("", test_observer_->default_network());
588 TEST_F(NetworkStateHandlerTest, DefaultServiceConnected) {
589 const std::string eth1 = kShillManagerClientStubDefaultService;
590 const std::string wifi1 = kShillManagerClientStubDefaultWifi;
592 // Disconnect ethernet and wifi.
593 base::StringValue connection_state_idle_value(shill::kStateIdle);
594 service_test_->SetServiceProperty(eth1, shill::kStateProperty,
595 connection_state_idle_value);
596 service_test_->SetServiceProperty(wifi1, shill::kStateProperty,
597 connection_state_idle_value);
598 message_loop_.RunUntilIdle();
599 EXPECT_EQ(std::string(), test_observer_->default_network());
601 // Connect ethernet, should become the default network.
602 test_observer_->reset_change_counts();
603 base::StringValue connection_state_ready_value(shill::kStateReady);
604 service_test_->SetServiceProperty(eth1, shill::kStateProperty,
605 connection_state_ready_value);
606 message_loop_.RunUntilIdle();
607 EXPECT_EQ(eth1, test_observer_->default_network());
608 EXPECT_EQ(shill::kStateReady,
609 test_observer_->default_network_connection_state());
610 EXPECT_EQ(1u, test_observer_->default_network_change_count());
613 TEST_F(NetworkStateHandlerTest, DefaultServiceChanged) {
614 const std::string eth1 = kShillManagerClientStubDefaultService;
615 // The default service should be eth1.
616 EXPECT_EQ(eth1, test_observer_->default_network());
618 // Change the default network by changing Manager.DefaultService.
619 const std::string wifi1 = kShillManagerClientStubDefaultWifi;
620 base::StringValue wifi1_value(wifi1);
621 manager_test_->SetManagerProperty(
622 shill::kDefaultServiceProperty, wifi1_value);
623 message_loop_.RunUntilIdle();
624 EXPECT_EQ(wifi1, test_observer_->default_network());
625 EXPECT_EQ(1u, test_observer_->default_network_change_count());
627 // Change the state of the default network.
628 test_observer_->reset_change_counts();
629 base::StringValue connection_state_ready_value(shill::kStateReady);
630 service_test_->SetServiceProperty(wifi1, shill::kStateProperty,
631 connection_state_ready_value);
632 message_loop_.RunUntilIdle();
633 EXPECT_EQ(shill::kStateReady,
634 test_observer_->default_network_connection_state());
635 EXPECT_EQ(1u, test_observer_->default_network_change_count());
637 // Updating a property on the default network should trigger
638 // a default network change.
639 test_observer_->reset_change_counts();
640 DBusThreadManager::Get()->GetShillServiceClient()->SetProperty(
641 dbus::ObjectPath(wifi1),
642 shill::kSecurityProperty, base::StringValue("TestSecurity"),
643 base::Bind(&base::DoNothing), base::Bind(&ErrorCallbackFunction));
644 message_loop_.RunUntilIdle();
645 EXPECT_EQ(1u, test_observer_->default_network_change_count());
647 // No default network updates for signal strength changes.
648 test_observer_->reset_change_counts();
649 DBusThreadManager::Get()->GetShillServiceClient()->SetProperty(
650 dbus::ObjectPath(wifi1),
651 shill::kSignalStrengthProperty, base::FundamentalValue(32),
652 base::Bind(&base::DoNothing), base::Bind(&ErrorCallbackFunction));
653 message_loop_.RunUntilIdle();
654 EXPECT_EQ(0u, test_observer_->default_network_change_count());
657 TEST_F(NetworkStateHandlerTest, RequestUpdate) {
658 // Request an update for kShillManagerClientStubDefaultWifi.
659 EXPECT_EQ(1, test_observer_->PropertyUpdatesForService(
660 kShillManagerClientStubDefaultWifi));
661 network_state_handler_->RequestUpdateForNetwork(
662 kShillManagerClientStubDefaultWifi);
663 message_loop_.RunUntilIdle();
664 EXPECT_EQ(2, test_observer_->PropertyUpdatesForService(
665 kShillManagerClientStubDefaultWifi));
668 TEST_F(NetworkStateHandlerTest, NetworkGuidInProfile) {
669 const std::string profile = "/profile/profile1";
670 const std::string wifi_path = "/service/wifi_with_guid";
671 const std::string wifi_guid = "wifi_guid";
672 const std::string wifi_name = "WifiWithGuid";
673 const bool is_service_configured = true;
675 // Add a network to the default Profile with a specified GUID.
676 AddService(wifi_path, wifi_guid, wifi_name,
677 shill::kTypeWifi, shill::kStateOnline);
678 profile_test_->AddProfile(profile, "" /* userhash */);
679 EXPECT_TRUE(profile_test_->AddService(profile, wifi_path));
680 UpdateManagerProperties();
682 // Verify that a NetworkState exists with a matching GUID.
683 const NetworkState* network =
684 network_state_handler_->GetNetworkStateFromServicePath(
685 wifi_path, is_service_configured);
686 ASSERT_TRUE(network);
687 EXPECT_EQ(wifi_guid, network->guid());
689 // Remove the service (simulating a network going out of range).
690 service_test_->RemoveService(wifi_path);
691 UpdateManagerProperties();
692 EXPECT_FALSE(network_state_handler_->GetNetworkState(wifi_path));
694 // Add the service (simulating a network coming back in range) and verify that
695 // the NetworkState was created with the same GUID.
696 AddService(wifi_path, "" /* guid */, wifi_name,
697 shill::kTypeWifi, shill::kStateOnline);
698 UpdateManagerProperties();
699 network = network_state_handler_->GetNetworkStateFromServicePath(
700 wifi_path, is_service_configured);
701 ASSERT_TRUE(network);
702 EXPECT_EQ(wifi_guid, network->guid());
705 TEST_F(NetworkStateHandlerTest, NetworkGuidNotInProfile) {
706 const std::string wifi_path = "/service/wifi_with_guid";
707 const std::string wifi_name = "WifiWithGuid";
708 const bool is_service_configured = false;
710 // Add a network without specifying a GUID or adding it to a profile.
711 AddService(wifi_path, "" /* guid */, wifi_name,
712 shill::kTypeWifi, shill::kStateOnline);
713 UpdateManagerProperties();
715 // Verify that a NetworkState exists with an assigned GUID.
716 const NetworkState* network =
717 network_state_handler_->GetNetworkStateFromServicePath(
718 wifi_path, is_service_configured);
719 ASSERT_TRUE(network);
720 std::string wifi_guid = network->guid();
721 EXPECT_FALSE(wifi_guid.empty());
723 // Remove the service (simulating a network going out of range).
724 service_test_->RemoveService(wifi_path);
725 UpdateManagerProperties();
726 EXPECT_FALSE(network_state_handler_->GetNetworkState(wifi_path));
728 // Add the service (simulating a network coming back in range) and verify that
729 // the NetworkState was created with the same GUID.
730 AddService(wifi_path, "" /* guid */, wifi_name,
731 shill::kTypeWifi, shill::kStateOnline);
732 UpdateManagerProperties();
733 network = network_state_handler_->GetNetworkStateFromServicePath(
734 wifi_path, is_service_configured);
735 ASSERT_TRUE(network);
736 EXPECT_EQ(wifi_guid, network->guid());
739 TEST_F(NetworkStateHandlerTest, DeviceListChanged) {
740 size_t stub_device_count = test_observer_->device_count();
741 // Add an additional device.
742 const std::string wifi_device = "/service/stub_wifi_device2";
743 device_test_->AddDevice(wifi_device, shill::kTypeWifi, "stub_wifi_device2");
744 UpdateManagerProperties();
745 // Expect a device list update.
746 EXPECT_EQ(stub_device_count + 1, test_observer_->device_count());
747 EXPECT_EQ(0, test_observer_->PropertyUpdatesForDevice(wifi_device));
748 // Change a device property.
749 device_test_->SetDeviceProperty(
750 wifi_device, shill::kScanningProperty, base::FundamentalValue(true));
751 UpdateManagerProperties();
752 EXPECT_EQ(1, test_observer_->PropertyUpdatesForDevice(wifi_device));
755 TEST_F(NetworkStateHandlerTest, IPConfigChanged) {
756 test_observer_->reset_updates();
757 EXPECT_EQ(0, test_observer_->PropertyUpdatesForDevice(
758 kShillManagerClientStubWifiDevice));
759 EXPECT_EQ(0, test_observer_->PropertyUpdatesForService(
760 kShillManagerClientStubDefaultWifi));
762 // Change IPConfigs property.
763 ShillIPConfigClient::TestInterface* ip_config_test =
764 DBusThreadManager::Get()->GetShillIPConfigClient()->GetTestInterface();
765 const std::string kIPConfigPath = "test_ip_config";
766 base::DictionaryValue ip_config_properties;
767 ip_config_test->AddIPConfig(kIPConfigPath, ip_config_properties);
768 base::ListValue device_ip_configs;
769 device_ip_configs.Append(new base::StringValue(kIPConfigPath));
770 device_test_->SetDeviceProperty(
771 kShillManagerClientStubWifiDevice, shill::kIPConfigsProperty,
772 device_ip_configs);
773 service_test_->SetServiceProperty(
774 kShillManagerClientStubDefaultWifi, shill::kIPConfigProperty,
775 base::StringValue(kIPConfigPath));
776 UpdateManagerProperties();
777 EXPECT_EQ(1, test_observer_->PropertyUpdatesForDevice(
778 kShillManagerClientStubWifiDevice));
779 EXPECT_EQ(1, test_observer_->PropertyUpdatesForService(
780 kShillManagerClientStubDefaultWifi));
783 } // namespace chromeos