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"
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"
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
{
51 explicit TestObserver(NetworkStateHandler
* handler
)
53 device_list_changed_count_(0),
55 network_list_changed_count_(0),
57 default_network_change_count_(0) {
60 ~TestObserver() override
{}
62 void DeviceListChanged() override
{
63 NetworkStateHandler::DeviceStateList devices
;
64 handler_
->GetDeviceList(&devices
);
65 device_count_
= devices
.size();
66 ++device_list_changed_count_
;
69 void NetworkListChanged() override
{
70 NetworkStateHandler::NetworkStateList networks
;
71 handler_
->GetNetworkListByType(chromeos::NetworkTypePattern::Default(),
72 false /* configured_only */,
73 false /* visible_only */,
76 network_count_
= networks
.size();
77 if (network_count_
== 0) {
78 default_network_
= "";
79 default_network_connection_state_
= "";
81 ++network_list_changed_count_
;
84 void DefaultNetworkChanged(const NetworkState
* network
) override
{
85 ++default_network_change_count_
;
86 default_network_
= network
? network
->path() : "";
87 default_network_connection_state_
=
88 network
? network
->connection_state() : "";
89 DVLOG(1) << "DefaultNetworkChanged: " << default_network_
90 << " State: " << default_network_connection_state_
;
93 void NetworkConnectionStateChanged(const NetworkState
* network
) override
{
94 network_connection_state_
[network
->path()] = network
->connection_state();
95 connection_state_changes_
[network
->path()]++;
98 void NetworkPropertiesUpdated(const NetworkState
* network
) override
{
100 property_updates_
[network
->path()]++;
103 void DevicePropertiesUpdated(const DeviceState
* device
) override
{
105 device_property_updates_
[device
->path()]++;
108 size_t device_list_changed_count() { return device_list_changed_count_
; }
109 size_t device_count() { return device_count_
; }
110 size_t network_list_changed_count() { return network_list_changed_count_
; }
111 size_t network_count() { return network_count_
; }
112 size_t default_network_change_count() {
113 return default_network_change_count_
;
115 void reset_change_counts() {
116 DVLOG(1) << "=== RESET CHANGE COUNTS ===";
117 default_network_change_count_
= 0;
118 device_list_changed_count_
= 0;
119 network_list_changed_count_
= 0;
120 connection_state_changes_
.clear();
122 void reset_updates() {
123 property_updates_
.clear();
124 device_property_updates_
.clear();
126 std::string
default_network() { return default_network_
; }
127 std::string
default_network_connection_state() {
128 return default_network_connection_state_
;
131 int PropertyUpdatesForService(const std::string
& service_path
) {
132 return property_updates_
[service_path
];
135 int PropertyUpdatesForDevice(const std::string
& device_path
) {
136 return device_property_updates_
[device_path
];
139 int ConnectionStateChangesForService(const std::string
& service_path
) {
140 return connection_state_changes_
[service_path
];
143 std::string
NetworkConnectionStateForService(
144 const std::string
& service_path
) {
145 return network_connection_state_
[service_path
];
149 NetworkStateHandler
* handler_
;
150 size_t device_list_changed_count_
;
151 size_t device_count_
;
152 size_t network_list_changed_count_
;
153 size_t network_count_
;
154 size_t default_network_change_count_
;
155 std::string default_network_
;
156 std::string default_network_connection_state_
;
157 std::map
<std::string
, int> property_updates_
;
158 std::map
<std::string
, int> device_property_updates_
;
159 std::map
<std::string
, int> connection_state_changes_
;
160 std::map
<std::string
, std::string
> network_connection_state_
;
162 DISALLOW_COPY_AND_ASSIGN(TestObserver
);
169 class NetworkStateHandlerTest
: public testing::Test
{
171 NetworkStateHandlerTest()
172 : device_test_(NULL
),
175 service_test_(NULL
) {}
176 ~NetworkStateHandlerTest() override
{}
178 void SetUp() override
{
179 // Initialize DBusThreadManager with a stub implementation.
180 DBusThreadManager::Initialize();
181 SetupDefaultShillState();
182 network_state_handler_
.reset(new NetworkStateHandler
);
183 test_observer_
.reset(new TestObserver(network_state_handler_
.get()));
184 network_state_handler_
->AddObserver(test_observer_
.get(), FROM_HERE
);
185 network_state_handler_
->InitShillPropertyHandler();
186 message_loop_
.RunUntilIdle();
187 test_observer_
->reset_change_counts();
190 void TearDown() override
{
191 network_state_handler_
->RemoveObserver(test_observer_
.get(), FROM_HERE
);
192 test_observer_
.reset();
193 network_state_handler_
.reset();
194 DBusThreadManager::Shutdown();
198 void AddService(const std::string
& service_path
,
199 const std::string
& guid
,
200 const std::string
& name
,
201 const std::string
& type
,
202 const std::string
& state
) {
203 service_test_
->AddService(service_path
, guid
, name
, type
, state
,
204 true /* add_to_visible */);
207 void SetupDefaultShillState() {
208 message_loop_
.RunUntilIdle(); // Process any pending updates
210 DBusThreadManager::Get()->GetShillDeviceClient()->GetTestInterface();
211 ASSERT_TRUE(device_test_
);
212 device_test_
->ClearDevices();
213 device_test_
->AddDevice(kShillManagerClientStubWifiDevice
,
214 shill::kTypeWifi
, "stub_wifi_device1");
215 device_test_
->AddDevice(kShillManagerClientStubCellularDevice
,
216 shill::kTypeCellular
, "stub_cellular_device1");
219 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface();
220 ASSERT_TRUE(manager_test_
);
223 DBusThreadManager::Get()->GetShillProfileClient()->GetTestInterface();
224 ASSERT_TRUE(profile_test_
);
225 profile_test_
->ClearProfiles();
228 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface();
229 ASSERT_TRUE(service_test_
);
230 service_test_
->ClearServices();
231 AddService(kShillManagerClientStubDefaultService
,
234 shill::kTypeEthernet
,
235 shill::kStateOnline
);
236 AddService(kShillManagerClientStubDefaultWifi
,
240 shill::kStateOnline
);
241 AddService(kShillManagerClientStubWifi2
,
246 AddService(kShillManagerClientStubCellular
,
249 shill::kTypeCellular
,
253 void UpdateManagerProperties() {
254 message_loop_
.RunUntilIdle();
257 base::MessageLoopForUI message_loop_
;
258 scoped_ptr
<NetworkStateHandler
> network_state_handler_
;
259 scoped_ptr
<TestObserver
> test_observer_
;
260 ShillDeviceClient::TestInterface
* device_test_
;
261 ShillManagerClient::TestInterface
* manager_test_
;
262 ShillProfileClient::TestInterface
* profile_test_
;
263 ShillServiceClient::TestInterface
* service_test_
;
266 DISALLOW_COPY_AND_ASSIGN(NetworkStateHandlerTest
);
269 TEST_F(NetworkStateHandlerTest
, NetworkStateHandlerStub
) {
270 // Ensure that the device and network list are the expected size.
271 const size_t kNumShillManagerClientStubImplDevices
= 2;
272 EXPECT_EQ(kNumShillManagerClientStubImplDevices
,
273 test_observer_
->device_count());
274 const size_t kNumShillManagerClientStubImplServices
= 4;
275 EXPECT_EQ(kNumShillManagerClientStubImplServices
,
276 test_observer_
->network_count());
277 // Ensure that the first stub network is the default network.
278 EXPECT_EQ(kShillManagerClientStubDefaultService
,
279 test_observer_
->default_network());
280 ASSERT_TRUE(network_state_handler_
->DefaultNetwork());
281 EXPECT_EQ(kShillManagerClientStubDefaultService
,
282 network_state_handler_
->DefaultNetwork()->path());
283 EXPECT_EQ(kShillManagerClientStubDefaultService
,
284 network_state_handler_
->ConnectedNetworkByType(
285 NetworkTypePattern::Ethernet())->path());
286 EXPECT_EQ(kShillManagerClientStubDefaultWifi
,
287 network_state_handler_
->ConnectedNetworkByType(
288 NetworkTypePattern::WiFi())->path());
289 EXPECT_EQ(kShillManagerClientStubCellular
,
290 network_state_handler_
->FirstNetworkByType(
291 NetworkTypePattern::Mobile())->path());
293 kShillManagerClientStubCellular
,
294 network_state_handler_
->FirstNetworkByType(NetworkTypePattern::Cellular())
296 EXPECT_EQ(shill::kStateOnline
,
297 test_observer_
->default_network_connection_state());
300 TEST_F(NetworkStateHandlerTest
, GetNetworkList
) {
301 // Ensure that the network list is the expected size.
302 const size_t kNumShillManagerClientStubImplServices
= 4;
303 EXPECT_EQ(kNumShillManagerClientStubImplServices
,
304 test_observer_
->network_count());
305 // Add a non-visible network to the profile.
306 const std::string profile
= "/profile/profile1";
307 const std::string wifi_favorite_path
= "/service/wifi_faviorite";
308 service_test_
->AddService(wifi_favorite_path
,
309 "wifi_faviorite_guid",
313 false /* add_to_visible */);
314 profile_test_
->AddProfile(profile
, "" /* userhash */);
315 EXPECT_TRUE(profile_test_
->AddService(profile
, wifi_favorite_path
));
316 UpdateManagerProperties();
317 message_loop_
.RunUntilIdle();
318 EXPECT_EQ(kNumShillManagerClientStubImplServices
+ 1,
319 test_observer_
->network_count());
322 NetworkStateHandler::NetworkStateList networks
;
323 network_state_handler_
->GetNetworkListByType(NetworkTypePattern::Default(),
324 false /* configured_only */,
325 false /* visible_only */,
328 EXPECT_EQ(kNumShillManagerClientStubImplServices
+ 1, networks
.size());
329 // Limit number of results.
330 network_state_handler_
->GetNetworkListByType(NetworkTypePattern::Default(),
331 false /* configured_only */,
332 false /* visible_only */,
335 EXPECT_EQ(2u, networks
.size());
336 // Get all wifi networks.
337 network_state_handler_
->GetNetworkListByType(NetworkTypePattern::WiFi(),
338 false /* configured_only */,
339 false /* visible_only */,
342 EXPECT_EQ(3u, networks
.size());
343 // Get visible networks.
344 network_state_handler_
->GetNetworkListByType(NetworkTypePattern::Default(),
345 false /* configured_only */,
346 true /* visible_only */,
349 EXPECT_EQ(kNumShillManagerClientStubImplServices
, networks
.size());
350 network_state_handler_
->GetVisibleNetworkList(&networks
);
351 EXPECT_EQ(kNumShillManagerClientStubImplServices
, networks
.size());
352 // Get configured (profile) networks.
353 network_state_handler_
->GetNetworkListByType(NetworkTypePattern::Default(),
354 true /* configured_only */,
355 false /* visible_only */,
358 EXPECT_EQ(1u, networks
.size());
361 TEST_F(NetworkStateHandlerTest
, NetworkListChanged
) {
362 size_t stub_network_count
= test_observer_
->network_count();
363 // Set up two additional visible networks.
364 const std::string wifi3
= "/service/wifi3";
365 const std::string wifi4
= "/service/wifi4";
366 service_test_
->SetServiceProperties(
367 wifi3
, "wifi3_guid", "wifi3",
368 shill::kTypeWifi
, shill::kStateIdle
, true /* visible */);
369 service_test_
->SetServiceProperties(
370 wifi4
, "wifi4_guid", "wifi4",
371 shill::kTypeWifi
, shill::kStateIdle
, true /* visible */);
372 // Add the services to the Manager. Only notify when the second service is
374 manager_test_
->AddManagerService(wifi3
, false);
375 manager_test_
->AddManagerService(wifi4
, true);
376 UpdateManagerProperties();
377 // Expect two service updates and one list update.
378 EXPECT_EQ(stub_network_count
+ 2, test_observer_
->network_count());
379 EXPECT_EQ(1, test_observer_
->PropertyUpdatesForService(wifi3
));
380 EXPECT_EQ(1, test_observer_
->PropertyUpdatesForService(wifi4
));
381 EXPECT_EQ(1u, test_observer_
->network_list_changed_count());
384 TEST_F(NetworkStateHandlerTest
, GetVisibleNetworks
) {
385 // Ensure that the network list is the expected size.
386 const size_t kNumShillManagerClientStubImplServices
= 4;
387 EXPECT_EQ(kNumShillManagerClientStubImplServices
,
388 test_observer_
->network_count());
389 // Add a non-visible network to the profile.
390 const std::string profile
= "/profile/profile1";
391 const std::string wifi_favorite_path
= "/service/wifi_faviorite";
392 service_test_
->AddService(wifi_favorite_path
,
393 "wifi_faviorite_guid",
397 false /* add_to_visible */);
398 message_loop_
.RunUntilIdle();
399 EXPECT_EQ(kNumShillManagerClientStubImplServices
+ 1,
400 test_observer_
->network_count());
402 // Get visible networks.
403 NetworkStateHandler::NetworkStateList networks
;
404 network_state_handler_
->GetVisibleNetworkList(&networks
);
405 EXPECT_EQ(kNumShillManagerClientStubImplServices
, networks
.size());
407 // Change the visible state of a network.
408 DBusThreadManager::Get()->GetShillServiceClient()->SetProperty(
409 dbus::ObjectPath(kShillManagerClientStubWifi2
),
410 shill::kVisibleProperty
, base::FundamentalValue(false),
411 base::Bind(&base::DoNothing
), base::Bind(&ErrorCallbackFunction
));
412 message_loop_
.RunUntilIdle();
413 network_state_handler_
->GetVisibleNetworkList(&networks
);
414 EXPECT_EQ(kNumShillManagerClientStubImplServices
- 1, networks
.size());
417 TEST_F(NetworkStateHandlerTest
, TechnologyChanged
) {
418 // Disable a technology. Will immediately set the state to AVAILABLE and
420 network_state_handler_
->SetTechnologyEnabled(
421 NetworkTypePattern::WiFi(), false, network_handler::ErrorCallback());
422 EXPECT_EQ(1u, test_observer_
->device_list_changed_count());
424 NetworkStateHandler::TECHNOLOGY_AVAILABLE
,
425 network_state_handler_
->GetTechnologyState(NetworkTypePattern::WiFi()));
427 // Run the message loop. An additional notification will be received when
428 // Shill updates the enabled technologies. The state should remain AVAILABLE.
429 test_observer_
->reset_change_counts();
430 message_loop_
.RunUntilIdle();
431 EXPECT_EQ(1u, test_observer_
->device_list_changed_count());
433 NetworkStateHandler::TECHNOLOGY_AVAILABLE
,
434 network_state_handler_
->GetTechnologyState(NetworkTypePattern::WiFi()));
436 // Enable a technology. Will immediately set the state to ENABLING and
438 test_observer_
->reset_change_counts();
439 network_state_handler_
->SetTechnologyEnabled(
440 NetworkTypePattern::WiFi(), true, network_handler::ErrorCallback());
441 EXPECT_EQ(1u, test_observer_
->device_list_changed_count());
443 NetworkStateHandler::TECHNOLOGY_ENABLING
,
444 network_state_handler_
->GetTechnologyState(NetworkTypePattern::WiFi()));
446 // Run the message loop. State should change to ENABLED.
447 test_observer_
->reset_change_counts();
448 message_loop_
.RunUntilIdle();
449 EXPECT_EQ(1u, test_observer_
->device_list_changed_count());
451 NetworkStateHandler::TECHNOLOGY_ENABLED
,
452 network_state_handler_
->GetTechnologyState(NetworkTypePattern::WiFi()));
455 TEST_F(NetworkStateHandlerTest
, TechnologyState
) {
456 manager_test_
->RemoveTechnology(shill::kTypeWimax
);
457 message_loop_
.RunUntilIdle();
459 NetworkStateHandler::TECHNOLOGY_UNAVAILABLE
,
460 network_state_handler_
->GetTechnologyState(NetworkTypePattern::Wimax()));
462 manager_test_
->AddTechnology(shill::kTypeWimax
, false);
463 message_loop_
.RunUntilIdle();
465 NetworkStateHandler::TECHNOLOGY_AVAILABLE
,
466 network_state_handler_
->GetTechnologyState(NetworkTypePattern::Wimax()));
468 manager_test_
->SetTechnologyInitializing(shill::kTypeWimax
, true);
469 message_loop_
.RunUntilIdle();
471 NetworkStateHandler::TECHNOLOGY_UNINITIALIZED
,
472 network_state_handler_
->GetTechnologyState(NetworkTypePattern::Wimax()));
474 manager_test_
->SetTechnologyInitializing(shill::kTypeWimax
, false);
475 network_state_handler_
->SetTechnologyEnabled(
476 NetworkTypePattern::Wimax(), true, network_handler::ErrorCallback());
477 message_loop_
.RunUntilIdle();
479 NetworkStateHandler::TECHNOLOGY_ENABLED
,
480 network_state_handler_
->GetTechnologyState(NetworkTypePattern::Wimax()));
482 manager_test_
->RemoveTechnology(shill::kTypeWimax
);
483 message_loop_
.RunUntilIdle();
485 NetworkStateHandler::TECHNOLOGY_UNAVAILABLE
,
486 network_state_handler_
->GetTechnologyState(NetworkTypePattern::Wimax()));
489 TEST_F(NetworkStateHandlerTest
, ServicePropertyChanged
) {
490 // Set a service property.
491 const std::string eth1
= kShillManagerClientStubDefaultService
;
492 const NetworkState
* ethernet
= network_state_handler_
->GetNetworkState(eth1
);
493 ASSERT_TRUE(ethernet
);
494 EXPECT_EQ("", ethernet
->security_class());
495 EXPECT_EQ(1, test_observer_
->PropertyUpdatesForService(eth1
));
496 base::StringValue
security_class_value("TestSecurityClass");
497 DBusThreadManager::Get()->GetShillServiceClient()->SetProperty(
498 dbus::ObjectPath(eth1
),
499 shill::kSecurityClassProperty
, security_class_value
,
500 base::Bind(&base::DoNothing
), base::Bind(&ErrorCallbackFunction
));
501 message_loop_
.RunUntilIdle();
502 ethernet
= network_state_handler_
->GetNetworkState(eth1
);
503 EXPECT_EQ("TestSecurityClass", ethernet
->security_class());
504 EXPECT_EQ(2, test_observer_
->PropertyUpdatesForService(eth1
));
506 // Changing a service to the existing value should not trigger an update.
507 DBusThreadManager::Get()->GetShillServiceClient()->SetProperty(
508 dbus::ObjectPath(eth1
),
509 shill::kSecurityClassProperty
, security_class_value
,
510 base::Bind(&base::DoNothing
), base::Bind(&ErrorCallbackFunction
));
511 message_loop_
.RunUntilIdle();
512 EXPECT_EQ(2, test_observer_
->PropertyUpdatesForService(eth1
));
515 TEST_F(NetworkStateHandlerTest
, GetState
) {
516 const std::string profile
= "/profile/profile1";
517 const std::string wifi_path
= kShillManagerClientStubDefaultWifi
;
519 // Add a wifi service to a Profile.
520 profile_test_
->AddProfile(profile
, "" /* userhash */);
521 EXPECT_TRUE(profile_test_
->AddService(profile
, wifi_path
));
522 UpdateManagerProperties();
524 // Ensure that a NetworkState exists.
525 const NetworkState
* wifi_network
=
526 network_state_handler_
->GetNetworkStateFromServicePath(
527 wifi_path
, true /* configured_only */);
528 ASSERT_TRUE(wifi_network
);
530 // Test looking up by GUID.
531 ASSERT_FALSE(wifi_network
->guid().empty());
532 const NetworkState
* wifi_network_guid
=
533 network_state_handler_
->GetNetworkStateFromGuid(wifi_network
->guid());
534 EXPECT_EQ(wifi_network
, wifi_network_guid
);
536 // Remove the service, verify that there is no longer a NetworkState for it.
537 service_test_
->RemoveService(wifi_path
);
538 UpdateManagerProperties();
539 EXPECT_FALSE(network_state_handler_
->GetNetworkState(wifi_path
));
542 TEST_F(NetworkStateHandlerTest
, NetworkConnectionStateChanged
) {
543 const std::string eth1
= kShillManagerClientStubDefaultService
;
544 EXPECT_EQ(0, test_observer_
->ConnectionStateChangesForService(eth1
));
546 // Change a network state.
547 base::StringValue
connection_state_idle_value(shill::kStateIdle
);
548 service_test_
->SetServiceProperty(eth1
, shill::kStateProperty
,
549 connection_state_idle_value
);
550 message_loop_
.RunUntilIdle();
551 EXPECT_EQ(shill::kStateIdle
,
552 test_observer_
->NetworkConnectionStateForService(eth1
));
553 EXPECT_EQ(1, test_observer_
->ConnectionStateChangesForService(eth1
));
554 // Confirm that changing the connection state to the same value does *not*
555 // signal the observer.
556 service_test_
->SetServiceProperty(eth1
, shill::kStateProperty
,
557 connection_state_idle_value
);
558 message_loop_
.RunUntilIdle();
559 EXPECT_EQ(1, test_observer_
->ConnectionStateChangesForService(eth1
));
562 TEST_F(NetworkStateHandlerTest
, DefaultServiceDisconnected
) {
563 const std::string eth1
= kShillManagerClientStubDefaultService
;
564 const std::string wifi1
= kShillManagerClientStubDefaultWifi
;
566 EXPECT_EQ(0u, test_observer_
->default_network_change_count());
567 // Disconnect ethernet.
568 base::StringValue
connection_state_idle_value(shill::kStateIdle
);
569 service_test_
->SetServiceProperty(eth1
, shill::kStateProperty
,
570 connection_state_idle_value
);
571 message_loop_
.RunUntilIdle();
572 // Expect two changes: first when eth1 becomes disconnected, second when
573 // wifi1 becomes the default.
574 EXPECT_EQ(2u, test_observer_
->default_network_change_count());
575 EXPECT_EQ(wifi1
, test_observer_
->default_network());
578 test_observer_
->reset_change_counts();
579 service_test_
->SetServiceProperty(wifi1
, shill::kStateProperty
,
580 connection_state_idle_value
);
581 message_loop_
.RunUntilIdle();
582 EXPECT_EQ(1u, test_observer_
->default_network_change_count());
583 EXPECT_EQ("", test_observer_
->default_network());
586 TEST_F(NetworkStateHandlerTest
, DefaultServiceConnected
) {
587 const std::string eth1
= kShillManagerClientStubDefaultService
;
588 const std::string wifi1
= kShillManagerClientStubDefaultWifi
;
590 // Disconnect ethernet and wifi.
591 base::StringValue
connection_state_idle_value(shill::kStateIdle
);
592 service_test_
->SetServiceProperty(eth1
, shill::kStateProperty
,
593 connection_state_idle_value
);
594 service_test_
->SetServiceProperty(wifi1
, shill::kStateProperty
,
595 connection_state_idle_value
);
596 message_loop_
.RunUntilIdle();
597 EXPECT_EQ(std::string(), test_observer_
->default_network());
599 // Connect ethernet, should become the default network.
600 test_observer_
->reset_change_counts();
601 base::StringValue
connection_state_ready_value(shill::kStateReady
);
602 service_test_
->SetServiceProperty(eth1
, shill::kStateProperty
,
603 connection_state_ready_value
);
604 message_loop_
.RunUntilIdle();
605 EXPECT_EQ(eth1
, test_observer_
->default_network());
606 EXPECT_EQ(shill::kStateReady
,
607 test_observer_
->default_network_connection_state());
608 EXPECT_EQ(1u, test_observer_
->default_network_change_count());
611 TEST_F(NetworkStateHandlerTest
, DefaultServiceChanged
) {
612 const std::string eth1
= kShillManagerClientStubDefaultService
;
613 // The default service should be eth1.
614 EXPECT_EQ(eth1
, test_observer_
->default_network());
616 // Change the default network by changing Manager.DefaultService.
617 const std::string wifi1
= kShillManagerClientStubDefaultWifi
;
618 base::StringValue
wifi1_value(wifi1
);
619 manager_test_
->SetManagerProperty(
620 shill::kDefaultServiceProperty
, wifi1_value
);
621 message_loop_
.RunUntilIdle();
622 EXPECT_EQ(wifi1
, test_observer_
->default_network());
623 EXPECT_EQ(1u, test_observer_
->default_network_change_count());
625 // Change the state of the default network.
626 test_observer_
->reset_change_counts();
627 base::StringValue
connection_state_ready_value(shill::kStateReady
);
628 service_test_
->SetServiceProperty(wifi1
, shill::kStateProperty
,
629 connection_state_ready_value
);
630 message_loop_
.RunUntilIdle();
631 EXPECT_EQ(shill::kStateReady
,
632 test_observer_
->default_network_connection_state());
633 EXPECT_EQ(1u, test_observer_
->default_network_change_count());
635 // Updating a property on the default network should trigger
636 // a default network change.
637 test_observer_
->reset_change_counts();
638 DBusThreadManager::Get()->GetShillServiceClient()->SetProperty(
639 dbus::ObjectPath(wifi1
),
640 shill::kSecurityClassProperty
, base::StringValue("TestSecurityClass"),
641 base::Bind(&base::DoNothing
), base::Bind(&ErrorCallbackFunction
));
642 message_loop_
.RunUntilIdle();
643 EXPECT_EQ(1u, test_observer_
->default_network_change_count());
645 // No default network updates for signal strength changes.
646 test_observer_
->reset_change_counts();
647 DBusThreadManager::Get()->GetShillServiceClient()->SetProperty(
648 dbus::ObjectPath(wifi1
),
649 shill::kSignalStrengthProperty
, base::FundamentalValue(32),
650 base::Bind(&base::DoNothing
), base::Bind(&ErrorCallbackFunction
));
651 message_loop_
.RunUntilIdle();
652 EXPECT_EQ(0u, test_observer_
->default_network_change_count());
655 TEST_F(NetworkStateHandlerTest
, RequestUpdate
) {
656 // Request an update for kShillManagerClientStubDefaultWifi.
657 EXPECT_EQ(1, test_observer_
->PropertyUpdatesForService(
658 kShillManagerClientStubDefaultWifi
));
659 network_state_handler_
->RequestUpdateForNetwork(
660 kShillManagerClientStubDefaultWifi
);
661 message_loop_
.RunUntilIdle();
662 EXPECT_EQ(2, test_observer_
->PropertyUpdatesForService(
663 kShillManagerClientStubDefaultWifi
));
666 TEST_F(NetworkStateHandlerTest
, NetworkGuidInProfile
) {
667 const std::string profile
= "/profile/profile1";
668 const std::string wifi_path
= "/service/wifi_with_guid";
669 const std::string wifi_guid
= "wifi_guid";
670 const std::string wifi_name
= "WifiWithGuid";
671 const bool is_service_configured
= true;
673 // Add a network to the default Profile with a specified GUID.
674 AddService(wifi_path
, wifi_guid
, wifi_name
,
675 shill::kTypeWifi
, shill::kStateOnline
);
676 profile_test_
->AddProfile(profile
, "" /* userhash */);
677 EXPECT_TRUE(profile_test_
->AddService(profile
, wifi_path
));
678 UpdateManagerProperties();
680 // Verify that a NetworkState exists with a matching GUID.
681 const NetworkState
* network
=
682 network_state_handler_
->GetNetworkStateFromServicePath(
683 wifi_path
, is_service_configured
);
684 ASSERT_TRUE(network
);
685 EXPECT_EQ(wifi_guid
, network
->guid());
687 // Remove the service (simulating a network going out of range).
688 service_test_
->RemoveService(wifi_path
);
689 UpdateManagerProperties();
690 EXPECT_FALSE(network_state_handler_
->GetNetworkState(wifi_path
));
692 // Add the service (simulating a network coming back in range) and verify that
693 // the NetworkState was created with the same GUID.
694 AddService(wifi_path
, "" /* guid */, wifi_name
,
695 shill::kTypeWifi
, shill::kStateOnline
);
696 UpdateManagerProperties();
697 network
= network_state_handler_
->GetNetworkStateFromServicePath(
698 wifi_path
, is_service_configured
);
699 ASSERT_TRUE(network
);
700 EXPECT_EQ(wifi_guid
, network
->guid());
703 TEST_F(NetworkStateHandlerTest
, NetworkGuidNotInProfile
) {
704 const std::string wifi_path
= "/service/wifi_with_guid";
705 const std::string wifi_name
= "WifiWithGuid";
706 const bool is_service_configured
= false;
708 // Add a network without specifying a GUID or adding it to a profile.
709 AddService(wifi_path
, "" /* guid */, wifi_name
,
710 shill::kTypeWifi
, shill::kStateOnline
);
711 UpdateManagerProperties();
713 // Verify that a NetworkState exists with an assigned GUID.
714 const NetworkState
* network
=
715 network_state_handler_
->GetNetworkStateFromServicePath(
716 wifi_path
, is_service_configured
);
717 ASSERT_TRUE(network
);
718 std::string wifi_guid
= network
->guid();
719 EXPECT_FALSE(wifi_guid
.empty());
721 // Remove the service (simulating a network going out of range).
722 service_test_
->RemoveService(wifi_path
);
723 UpdateManagerProperties();
724 EXPECT_FALSE(network_state_handler_
->GetNetworkState(wifi_path
));
726 // Add the service (simulating a network coming back in range) and verify that
727 // the NetworkState was created with the same GUID.
728 AddService(wifi_path
, "" /* guid */, wifi_name
,
729 shill::kTypeWifi
, shill::kStateOnline
);
730 UpdateManagerProperties();
731 network
= network_state_handler_
->GetNetworkStateFromServicePath(
732 wifi_path
, is_service_configured
);
733 ASSERT_TRUE(network
);
734 EXPECT_EQ(wifi_guid
, network
->guid());
737 TEST_F(NetworkStateHandlerTest
, DeviceListChanged
) {
738 size_t stub_device_count
= test_observer_
->device_count();
739 // Add an additional device.
740 const std::string wifi_device
= "/service/stub_wifi_device2";
741 device_test_
->AddDevice(wifi_device
, shill::kTypeWifi
, "stub_wifi_device2");
742 UpdateManagerProperties();
743 // Expect a device list update.
744 EXPECT_EQ(stub_device_count
+ 1, test_observer_
->device_count());
745 EXPECT_EQ(0, test_observer_
->PropertyUpdatesForDevice(wifi_device
));
746 // Change a device property.
747 device_test_
->SetDeviceProperty(
748 wifi_device
, shill::kScanningProperty
, base::FundamentalValue(true));
749 UpdateManagerProperties();
750 EXPECT_EQ(1, test_observer_
->PropertyUpdatesForDevice(wifi_device
));
753 TEST_F(NetworkStateHandlerTest
, IPConfigChanged
) {
754 test_observer_
->reset_updates();
755 EXPECT_EQ(0, test_observer_
->PropertyUpdatesForDevice(
756 kShillManagerClientStubWifiDevice
));
757 EXPECT_EQ(0, test_observer_
->PropertyUpdatesForService(
758 kShillManagerClientStubDefaultWifi
));
760 // Change IPConfigs property.
761 ShillIPConfigClient::TestInterface
* ip_config_test
=
762 DBusThreadManager::Get()->GetShillIPConfigClient()->GetTestInterface();
763 const std::string kIPConfigPath
= "test_ip_config";
764 base::DictionaryValue ip_config_properties
;
765 ip_config_test
->AddIPConfig(kIPConfigPath
, ip_config_properties
);
766 base::ListValue device_ip_configs
;
767 device_ip_configs
.Append(new base::StringValue(kIPConfigPath
));
768 device_test_
->SetDeviceProperty(
769 kShillManagerClientStubWifiDevice
, shill::kIPConfigsProperty
,
771 service_test_
->SetServiceProperty(
772 kShillManagerClientStubDefaultWifi
, shill::kIPConfigProperty
,
773 base::StringValue(kIPConfigPath
));
774 UpdateManagerProperties();
775 EXPECT_EQ(1, test_observer_
->PropertyUpdatesForDevice(
776 kShillManagerClientStubWifiDevice
));
777 EXPECT_EQ(1, test_observer_
->PropertyUpdatesForService(
778 kShillManagerClientStubDefaultWifi
));
781 } // namespace chromeos