Only send _NET_ACTIVE_WINDOW hint if the chromium window is not already active.
[chromium-blink-merge.git] / chromeos / network / network_state_handler_unittest.cc
blob3da383191d9e6d1890252cda8cd61ff751e8a286
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 ~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 */,
74 0 /* no limit */,
75 &networks);
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 EXPECT_TRUE(!network || network->IsConnectedState());
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 void NetworkConnectionStateChanged(const NetworkState* network) override {
95 network_connection_state_[network->path()] = network->connection_state();
96 connection_state_changes_[network->path()]++;
99 void NetworkPropertiesUpdated(const NetworkState* network) override {
100 DCHECK(network);
101 property_updates_[network->path()]++;
104 void DevicePropertiesUpdated(const DeviceState* device) override {
105 DCHECK(device);
106 device_property_updates_[device->path()]++;
109 size_t device_list_changed_count() { return device_list_changed_count_; }
110 size_t device_count() { return device_count_; }
111 size_t network_list_changed_count() { return network_list_changed_count_; }
112 size_t network_count() { return network_count_; }
113 size_t default_network_change_count() {
114 return default_network_change_count_;
116 void reset_change_counts() {
117 DVLOG(1) << "=== RESET CHANGE COUNTS ===";
118 default_network_change_count_ = 0;
119 device_list_changed_count_ = 0;
120 network_list_changed_count_ = 0;
121 connection_state_changes_.clear();
123 void reset_updates() {
124 property_updates_.clear();
125 device_property_updates_.clear();
127 std::string default_network() { return default_network_; }
128 std::string default_network_connection_state() {
129 return default_network_connection_state_;
132 int PropertyUpdatesForService(const std::string& service_path) {
133 return property_updates_[service_path];
136 int PropertyUpdatesForDevice(const std::string& device_path) {
137 return device_property_updates_[device_path];
140 int ConnectionStateChangesForService(const std::string& service_path) {
141 return connection_state_changes_[service_path];
144 std::string NetworkConnectionStateForService(
145 const std::string& service_path) {
146 return network_connection_state_[service_path];
149 private:
150 NetworkStateHandler* handler_;
151 size_t device_list_changed_count_;
152 size_t device_count_;
153 size_t network_list_changed_count_;
154 size_t network_count_;
155 size_t default_network_change_count_;
156 std::string default_network_;
157 std::string default_network_connection_state_;
158 std::map<std::string, int> property_updates_;
159 std::map<std::string, int> device_property_updates_;
160 std::map<std::string, int> connection_state_changes_;
161 std::map<std::string, std::string> network_connection_state_;
163 DISALLOW_COPY_AND_ASSIGN(TestObserver);
166 } // namespace
168 namespace chromeos {
170 class NetworkStateHandlerTest : public testing::Test {
171 public:
172 NetworkStateHandlerTest()
173 : device_test_(nullptr),
174 manager_test_(nullptr),
175 profile_test_(nullptr),
176 service_test_(nullptr) {}
177 ~NetworkStateHandlerTest() override {}
179 void SetUp() override {
180 // Initialize DBusThreadManager with a stub implementation.
181 DBusThreadManager::Initialize();
182 SetupDefaultShillState();
183 network_state_handler_.reset(new NetworkStateHandler);
184 test_observer_.reset(new TestObserver(network_state_handler_.get()));
185 network_state_handler_->AddObserver(test_observer_.get(), FROM_HERE);
186 network_state_handler_->InitShillPropertyHandler();
187 message_loop_.RunUntilIdle();
188 test_observer_->reset_change_counts();
191 void TearDown() override {
192 network_state_handler_->RemoveObserver(test_observer_.get(), FROM_HERE);
193 test_observer_.reset();
194 network_state_handler_.reset();
195 DBusThreadManager::Shutdown();
198 protected:
199 void AddService(const std::string& service_path,
200 const std::string& guid,
201 const std::string& name,
202 const std::string& type,
203 const std::string& state) {
204 service_test_->AddService(service_path, guid, name, type, state,
205 true /* add_to_visible */);
208 void SetupDefaultShillState() {
209 message_loop_.RunUntilIdle(); // Process any pending updates
210 device_test_ =
211 DBusThreadManager::Get()->GetShillDeviceClient()->GetTestInterface();
212 ASSERT_TRUE(device_test_);
213 device_test_->ClearDevices();
214 device_test_->AddDevice(kShillManagerClientStubWifiDevice,
215 shill::kTypeWifi, "stub_wifi_device1");
216 device_test_->AddDevice(kShillManagerClientStubCellularDevice,
217 shill::kTypeCellular, "stub_cellular_device1");
219 manager_test_ =
220 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface();
221 ASSERT_TRUE(manager_test_);
223 profile_test_ =
224 DBusThreadManager::Get()->GetShillProfileClient()->GetTestInterface();
225 ASSERT_TRUE(profile_test_);
226 profile_test_->ClearProfiles();
228 service_test_ =
229 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface();
230 ASSERT_TRUE(service_test_);
231 service_test_->ClearServices();
232 AddService(kShillManagerClientStubDefaultService,
233 "eth1_guid",
234 "eth1",
235 shill::kTypeEthernet,
236 shill::kStateOnline);
237 AddService(kShillManagerClientStubDefaultWifi,
238 "wifi1_guid",
239 "wifi1",
240 shill::kTypeWifi,
241 shill::kStateOnline);
242 AddService(kShillManagerClientStubWifi2,
243 "wifi2_guid",
244 "wifi2",
245 shill::kTypeWifi,
246 shill::kStateIdle);
247 AddService(kShillManagerClientStubCellular,
248 "cellular1_guid",
249 "cellular1",
250 shill::kTypeCellular,
251 shill::kStateIdle);
254 void UpdateManagerProperties() {
255 message_loop_.RunUntilIdle();
258 void SetServiceProperty(const std::string& service_path,
259 const std::string& key,
260 const base::Value& value) {
261 DBusThreadManager::Get()->GetShillServiceClient()->SetProperty(
262 dbus::ObjectPath(service_path), key, value,
263 base::Bind(&base::DoNothing), base::Bind(&ErrorCallbackFunction));
266 base::MessageLoopForUI message_loop_;
267 scoped_ptr<NetworkStateHandler> network_state_handler_;
268 scoped_ptr<TestObserver> test_observer_;
269 ShillDeviceClient::TestInterface* device_test_;
270 ShillManagerClient::TestInterface* manager_test_;
271 ShillProfileClient::TestInterface* profile_test_;
272 ShillServiceClient::TestInterface* service_test_;
274 private:
275 DISALLOW_COPY_AND_ASSIGN(NetworkStateHandlerTest);
278 TEST_F(NetworkStateHandlerTest, NetworkStateHandlerStub) {
279 // Ensure that the device and network list are the expected size.
280 const size_t kNumShillManagerClientStubImplDevices = 2;
281 EXPECT_EQ(kNumShillManagerClientStubImplDevices,
282 test_observer_->device_count());
283 const size_t kNumShillManagerClientStubImplServices = 4;
284 EXPECT_EQ(kNumShillManagerClientStubImplServices,
285 test_observer_->network_count());
286 // Ensure that the first stub network is the default network.
287 EXPECT_EQ(kShillManagerClientStubDefaultService,
288 test_observer_->default_network());
289 ASSERT_TRUE(network_state_handler_->DefaultNetwork());
290 EXPECT_EQ(kShillManagerClientStubDefaultService,
291 network_state_handler_->DefaultNetwork()->path());
292 EXPECT_EQ(kShillManagerClientStubDefaultService,
293 network_state_handler_->ConnectedNetworkByType(
294 NetworkTypePattern::Ethernet())->path());
295 EXPECT_EQ(kShillManagerClientStubDefaultWifi,
296 network_state_handler_->ConnectedNetworkByType(
297 NetworkTypePattern::WiFi())->path());
298 EXPECT_EQ(kShillManagerClientStubCellular,
299 network_state_handler_->FirstNetworkByType(
300 NetworkTypePattern::Mobile())->path());
301 EXPECT_EQ(
302 kShillManagerClientStubCellular,
303 network_state_handler_->FirstNetworkByType(NetworkTypePattern::Cellular())
304 ->path());
305 EXPECT_EQ(shill::kStateOnline,
306 test_observer_->default_network_connection_state());
309 TEST_F(NetworkStateHandlerTest, GetNetworkList) {
310 // Ensure that the network list is the expected size.
311 const size_t kNumShillManagerClientStubImplServices = 4;
312 EXPECT_EQ(kNumShillManagerClientStubImplServices,
313 test_observer_->network_count());
314 // Add a non-visible network to the profile.
315 const std::string profile = "/profile/profile1";
316 const std::string wifi_favorite_path = "/service/wifi_faviorite";
317 service_test_->AddService(wifi_favorite_path,
318 "wifi_faviorite_guid",
319 "wifi_faviorite",
320 shill::kTypeWifi,
321 shill::kStateIdle,
322 false /* add_to_visible */);
323 profile_test_->AddProfile(profile, "" /* userhash */);
324 EXPECT_TRUE(profile_test_->AddService(profile, wifi_favorite_path));
325 UpdateManagerProperties();
326 message_loop_.RunUntilIdle();
327 EXPECT_EQ(kNumShillManagerClientStubImplServices + 1,
328 test_observer_->network_count());
330 // Get all networks.
331 NetworkStateHandler::NetworkStateList networks;
332 network_state_handler_->GetNetworkListByType(NetworkTypePattern::Default(),
333 false /* configured_only */,
334 false /* visible_only */,
335 0 /* no limit */,
336 &networks);
337 EXPECT_EQ(kNumShillManagerClientStubImplServices + 1, networks.size());
338 // Limit number of results.
339 network_state_handler_->GetNetworkListByType(NetworkTypePattern::Default(),
340 false /* configured_only */,
341 false /* visible_only */,
342 2 /* limit */,
343 &networks);
344 EXPECT_EQ(2u, networks.size());
345 // Get all wifi networks.
346 network_state_handler_->GetNetworkListByType(NetworkTypePattern::WiFi(),
347 false /* configured_only */,
348 false /* visible_only */,
349 0 /* no limit */,
350 &networks);
351 EXPECT_EQ(3u, networks.size());
352 // Get visible networks.
353 network_state_handler_->GetNetworkListByType(NetworkTypePattern::Default(),
354 false /* configured_only */,
355 true /* visible_only */,
356 0 /* no limit */,
357 &networks);
358 EXPECT_EQ(kNumShillManagerClientStubImplServices, networks.size());
359 network_state_handler_->GetVisibleNetworkList(&networks);
360 EXPECT_EQ(kNumShillManagerClientStubImplServices, networks.size());
361 // Get configured (profile) networks.
362 network_state_handler_->GetNetworkListByType(NetworkTypePattern::Default(),
363 true /* configured_only */,
364 false /* visible_only */,
365 0 /* no limit */,
366 &networks);
367 EXPECT_EQ(1u, networks.size());
370 TEST_F(NetworkStateHandlerTest, NetworkListChanged) {
371 size_t stub_network_count = test_observer_->network_count();
372 // Set up two additional visible networks.
373 const std::string wifi3 = "/service/wifi3";
374 const std::string wifi4 = "/service/wifi4";
375 service_test_->SetServiceProperties(
376 wifi3, "wifi3_guid", "wifi3",
377 shill::kTypeWifi, shill::kStateIdle, true /* visible */);
378 service_test_->SetServiceProperties(
379 wifi4, "wifi4_guid", "wifi4",
380 shill::kTypeWifi, shill::kStateIdle, true /* visible */);
381 // Add the services to the Manager. Only notify when the second service is
382 // added.
383 manager_test_->AddManagerService(wifi3, false);
384 manager_test_->AddManagerService(wifi4, true);
385 UpdateManagerProperties();
386 // Expect two service updates and one list update.
387 EXPECT_EQ(stub_network_count + 2, test_observer_->network_count());
388 EXPECT_EQ(1, test_observer_->PropertyUpdatesForService(wifi3));
389 EXPECT_EQ(1, test_observer_->PropertyUpdatesForService(wifi4));
390 EXPECT_EQ(1u, test_observer_->network_list_changed_count());
393 TEST_F(NetworkStateHandlerTest, GetVisibleNetworks) {
394 // Ensure that the network list is the expected size.
395 const size_t kNumShillManagerClientStubImplServices = 4;
396 EXPECT_EQ(kNumShillManagerClientStubImplServices,
397 test_observer_->network_count());
398 // Add a non-visible network to the profile.
399 const std::string profile = "/profile/profile1";
400 const std::string wifi_favorite_path = "/service/wifi_faviorite";
401 service_test_->AddService(wifi_favorite_path,
402 "wifi_faviorite_guid",
403 "wifi_faviorite",
404 shill::kTypeWifi,
405 shill::kStateIdle,
406 false /* add_to_visible */);
407 message_loop_.RunUntilIdle();
408 EXPECT_EQ(kNumShillManagerClientStubImplServices + 1,
409 test_observer_->network_count());
411 // Get visible networks.
412 NetworkStateHandler::NetworkStateList networks;
413 network_state_handler_->GetVisibleNetworkList(&networks);
414 EXPECT_EQ(kNumShillManagerClientStubImplServices, networks.size());
416 // Change the visible state of a network.
417 SetServiceProperty(kShillManagerClientStubWifi2, shill::kVisibleProperty,
418 base::FundamentalValue(false));
419 message_loop_.RunUntilIdle();
420 network_state_handler_->GetVisibleNetworkList(&networks);
421 EXPECT_EQ(kNumShillManagerClientStubImplServices - 1, networks.size());
424 TEST_F(NetworkStateHandlerTest, TechnologyChanged) {
425 // Disable a technology. Will immediately set the state to AVAILABLE and
426 // notify observers.
427 network_state_handler_->SetTechnologyEnabled(
428 NetworkTypePattern::WiFi(), false, network_handler::ErrorCallback());
429 EXPECT_EQ(1u, test_observer_->device_list_changed_count());
430 EXPECT_EQ(
431 NetworkStateHandler::TECHNOLOGY_AVAILABLE,
432 network_state_handler_->GetTechnologyState(NetworkTypePattern::WiFi()));
434 // Run the message loop. An additional notification will be received when
435 // Shill updates the enabled technologies. The state should remain AVAILABLE.
436 test_observer_->reset_change_counts();
437 message_loop_.RunUntilIdle();
438 EXPECT_EQ(1u, test_observer_->device_list_changed_count());
439 EXPECT_EQ(
440 NetworkStateHandler::TECHNOLOGY_AVAILABLE,
441 network_state_handler_->GetTechnologyState(NetworkTypePattern::WiFi()));
443 // Enable a technology. Will immediately set the state to ENABLING and
444 // notify observers.
445 test_observer_->reset_change_counts();
446 network_state_handler_->SetTechnologyEnabled(
447 NetworkTypePattern::WiFi(), true, network_handler::ErrorCallback());
448 EXPECT_EQ(1u, test_observer_->device_list_changed_count());
449 EXPECT_EQ(
450 NetworkStateHandler::TECHNOLOGY_ENABLING,
451 network_state_handler_->GetTechnologyState(NetworkTypePattern::WiFi()));
453 // Run the message loop. State should change to ENABLED.
454 test_observer_->reset_change_counts();
455 message_loop_.RunUntilIdle();
456 EXPECT_EQ(1u, test_observer_->device_list_changed_count());
457 EXPECT_EQ(
458 NetworkStateHandler::TECHNOLOGY_ENABLED,
459 network_state_handler_->GetTechnologyState(NetworkTypePattern::WiFi()));
462 TEST_F(NetworkStateHandlerTest, TechnologyState) {
463 manager_test_->RemoveTechnology(shill::kTypeWimax);
464 message_loop_.RunUntilIdle();
465 EXPECT_EQ(
466 NetworkStateHandler::TECHNOLOGY_UNAVAILABLE,
467 network_state_handler_->GetTechnologyState(NetworkTypePattern::Wimax()));
469 manager_test_->AddTechnology(shill::kTypeWimax, false);
470 message_loop_.RunUntilIdle();
471 EXPECT_EQ(
472 NetworkStateHandler::TECHNOLOGY_AVAILABLE,
473 network_state_handler_->GetTechnologyState(NetworkTypePattern::Wimax()));
475 manager_test_->SetTechnologyInitializing(shill::kTypeWimax, true);
476 message_loop_.RunUntilIdle();
477 EXPECT_EQ(
478 NetworkStateHandler::TECHNOLOGY_UNINITIALIZED,
479 network_state_handler_->GetTechnologyState(NetworkTypePattern::Wimax()));
481 manager_test_->SetTechnologyInitializing(shill::kTypeWimax, false);
482 network_state_handler_->SetTechnologyEnabled(
483 NetworkTypePattern::Wimax(), true, network_handler::ErrorCallback());
484 message_loop_.RunUntilIdle();
485 EXPECT_EQ(
486 NetworkStateHandler::TECHNOLOGY_ENABLED,
487 network_state_handler_->GetTechnologyState(NetworkTypePattern::Wimax()));
489 manager_test_->RemoveTechnology(shill::kTypeWimax);
490 message_loop_.RunUntilIdle();
491 EXPECT_EQ(
492 NetworkStateHandler::TECHNOLOGY_UNAVAILABLE,
493 network_state_handler_->GetTechnologyState(NetworkTypePattern::Wimax()));
496 TEST_F(NetworkStateHandlerTest, ServicePropertyChanged) {
497 // Set a service property.
498 const std::string eth1 = kShillManagerClientStubDefaultService;
499 const NetworkState* ethernet = network_state_handler_->GetNetworkState(eth1);
500 ASSERT_TRUE(ethernet);
501 EXPECT_EQ("", ethernet->security_class());
502 EXPECT_EQ(1, test_observer_->PropertyUpdatesForService(eth1));
503 base::StringValue security_class_value("TestSecurityClass");
504 SetServiceProperty(eth1, shill::kSecurityClassProperty, security_class_value);
505 message_loop_.RunUntilIdle();
506 ethernet = network_state_handler_->GetNetworkState(eth1);
507 EXPECT_EQ("TestSecurityClass", ethernet->security_class());
508 EXPECT_EQ(2, test_observer_->PropertyUpdatesForService(eth1));
510 // Changing a service to the existing value should not trigger an update.
511 SetServiceProperty(eth1, shill::kSecurityClassProperty, security_class_value);
512 message_loop_.RunUntilIdle();
513 EXPECT_EQ(2, test_observer_->PropertyUpdatesForService(eth1));
516 TEST_F(NetworkStateHandlerTest, GetState) {
517 const std::string profile = "/profile/profile1";
518 const std::string wifi_path = kShillManagerClientStubDefaultWifi;
520 // Add a wifi service to a Profile.
521 profile_test_->AddProfile(profile, "" /* userhash */);
522 EXPECT_TRUE(profile_test_->AddService(profile, wifi_path));
523 UpdateManagerProperties();
525 // Ensure that a NetworkState exists.
526 const NetworkState* wifi_network =
527 network_state_handler_->GetNetworkStateFromServicePath(
528 wifi_path, true /* configured_only */);
529 ASSERT_TRUE(wifi_network);
531 // Test looking up by GUID.
532 ASSERT_FALSE(wifi_network->guid().empty());
533 const NetworkState* wifi_network_guid =
534 network_state_handler_->GetNetworkStateFromGuid(wifi_network->guid());
535 EXPECT_EQ(wifi_network, wifi_network_guid);
537 // Remove the service, verify that there is no longer a NetworkState for it.
538 service_test_->RemoveService(wifi_path);
539 UpdateManagerProperties();
540 EXPECT_FALSE(network_state_handler_->GetNetworkState(wifi_path));
543 TEST_F(NetworkStateHandlerTest, NetworkConnectionStateChanged) {
544 const std::string eth1 = kShillManagerClientStubDefaultService;
545 EXPECT_EQ(0, test_observer_->ConnectionStateChangesForService(eth1));
547 // Change a network state.
548 base::StringValue connection_state_idle_value(shill::kStateIdle);
549 service_test_->SetServiceProperty(eth1, shill::kStateProperty,
550 connection_state_idle_value);
551 message_loop_.RunUntilIdle();
552 EXPECT_EQ(shill::kStateIdle,
553 test_observer_->NetworkConnectionStateForService(eth1));
554 EXPECT_EQ(1, test_observer_->ConnectionStateChangesForService(eth1));
555 // Confirm that changing the connection state to the same value does *not*
556 // signal the observer.
557 service_test_->SetServiceProperty(eth1, shill::kStateProperty,
558 connection_state_idle_value);
559 message_loop_.RunUntilIdle();
560 EXPECT_EQ(1, test_observer_->ConnectionStateChangesForService(eth1));
563 TEST_F(NetworkStateHandlerTest, DefaultServiceDisconnected) {
564 const std::string eth1 = kShillManagerClientStubDefaultService;
565 const std::string wifi1 = kShillManagerClientStubDefaultWifi;
567 EXPECT_EQ(0u, test_observer_->default_network_change_count());
568 // Disconnect ethernet.
569 base::StringValue connection_state_idle_value(shill::kStateIdle);
570 service_test_->SetServiceProperty(eth1, shill::kStateProperty,
571 connection_state_idle_value);
572 message_loop_.RunUntilIdle();
573 // Expect two changes: first when eth1 becomes disconnected, second when
574 // wifi1 becomes the default.
575 EXPECT_EQ(2u, test_observer_->default_network_change_count());
576 EXPECT_EQ(wifi1, test_observer_->default_network());
578 // Disconnect wifi.
579 test_observer_->reset_change_counts();
580 service_test_->SetServiceProperty(wifi1, shill::kStateProperty,
581 connection_state_idle_value);
582 message_loop_.RunUntilIdle();
583 EXPECT_EQ(1u, test_observer_->default_network_change_count());
584 EXPECT_EQ("", test_observer_->default_network());
587 TEST_F(NetworkStateHandlerTest, DefaultServiceConnected) {
588 const std::string eth1 = kShillManagerClientStubDefaultService;
589 const std::string wifi1 = kShillManagerClientStubDefaultWifi;
591 // Disconnect ethernet and wifi.
592 base::StringValue connection_state_idle_value(shill::kStateIdle);
593 service_test_->SetServiceProperty(eth1, shill::kStateProperty,
594 connection_state_idle_value);
595 service_test_->SetServiceProperty(wifi1, shill::kStateProperty,
596 connection_state_idle_value);
597 message_loop_.RunUntilIdle();
598 EXPECT_EQ(std::string(), test_observer_->default_network());
600 // Connect ethernet, should become the default network.
601 test_observer_->reset_change_counts();
602 base::StringValue connection_state_ready_value(shill::kStateReady);
603 service_test_->SetServiceProperty(eth1, shill::kStateProperty,
604 connection_state_ready_value);
605 message_loop_.RunUntilIdle();
606 EXPECT_EQ(eth1, test_observer_->default_network());
607 EXPECT_EQ(shill::kStateReady,
608 test_observer_->default_network_connection_state());
609 EXPECT_EQ(1u, test_observer_->default_network_change_count());
612 TEST_F(NetworkStateHandlerTest, DefaultServiceChanged) {
613 const std::string eth1 = kShillManagerClientStubDefaultService;
614 // The default service should be eth1.
615 EXPECT_EQ(eth1, test_observer_->default_network());
617 // Change the default network by changing Manager.DefaultService.
618 // This should only generate one default network notification when the
619 // DefaultService property changes.
620 const std::string wifi1 = kShillManagerClientStubDefaultWifi;
621 SetServiceProperty(eth1, shill::kStateProperty,
622 base::StringValue(shill::kStateIdle));
623 manager_test_->SetManagerProperty(shill::kDefaultServiceProperty,
624 base::StringValue(wifi1));
625 message_loop_.RunUntilIdle();
626 EXPECT_EQ(wifi1, test_observer_->default_network());
627 EXPECT_EQ(1u, test_observer_->default_network_change_count());
629 // Change the state of the default network. This should generate a
630 // default network notification.
631 test_observer_->reset_change_counts();
632 service_test_->SetServiceProperty(wifi1, shill::kStateProperty,
633 base::StringValue(shill::kStateReady));
634 message_loop_.RunUntilIdle();
635 EXPECT_EQ(shill::kStateReady,
636 test_observer_->default_network_connection_state());
637 EXPECT_EQ(1u, test_observer_->default_network_change_count());
639 // Updating a property on the default network should also trigger
640 // a default network change.
641 test_observer_->reset_change_counts();
642 SetServiceProperty(wifi1, shill::kSecurityClassProperty,
643 base::StringValue("TestSecurityClass"));
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 SetServiceProperty(wifi1, shill::kSignalStrengthProperty,
650 base::FundamentalValue(32));
651 message_loop_.RunUntilIdle();
652 EXPECT_EQ(0u, test_observer_->default_network_change_count());
654 // Change the default network to a Connecting network, then set the
655 // state to Connected. The DefaultNetworkChange notification should only
656 // fire once when the network is connected.
657 test_observer_->reset_change_counts();
658 SetServiceProperty(wifi1, shill::kStateProperty,
659 base::StringValue(shill::kStateIdle));
660 message_loop_.RunUntilIdle();
661 EXPECT_EQ(1u, test_observer_->default_network_change_count());
662 EXPECT_EQ(std::string(), test_observer_->default_network());
664 const std::string wifi2 = kShillManagerClientStubWifi2;
665 manager_test_->SetManagerProperty(shill::kDefaultServiceProperty,
666 base::StringValue(wifi2));
667 message_loop_.RunUntilIdle();
668 EXPECT_EQ(1u, test_observer_->default_network_change_count());
669 // Change the connection state of the default network, observer should fire.
670 SetServiceProperty(wifi2, shill::kStateProperty,
671 base::StringValue(shill::kStateReady));
672 message_loop_.RunUntilIdle();
673 EXPECT_EQ(wifi2, test_observer_->default_network());
674 EXPECT_EQ(2u, test_observer_->default_network_change_count());
677 TEST_F(NetworkStateHandlerTest, RequestUpdate) {
678 // Request an update for kShillManagerClientStubDefaultWifi.
679 EXPECT_EQ(1, test_observer_->PropertyUpdatesForService(
680 kShillManagerClientStubDefaultWifi));
681 network_state_handler_->RequestUpdateForNetwork(
682 kShillManagerClientStubDefaultWifi);
683 message_loop_.RunUntilIdle();
684 EXPECT_EQ(2, test_observer_->PropertyUpdatesForService(
685 kShillManagerClientStubDefaultWifi));
688 TEST_F(NetworkStateHandlerTest, NetworkGuidInProfile) {
689 const std::string profile = "/profile/profile1";
690 const std::string wifi_path = "/service/wifi_with_guid";
691 const std::string wifi_guid = "wifi_guid";
692 const std::string wifi_name = "WifiWithGuid";
693 const bool is_service_configured = true;
695 // Add a network to the default Profile with a specified GUID.
696 AddService(wifi_path, wifi_guid, wifi_name,
697 shill::kTypeWifi, shill::kStateOnline);
698 profile_test_->AddProfile(profile, "" /* userhash */);
699 EXPECT_TRUE(profile_test_->AddService(profile, wifi_path));
700 UpdateManagerProperties();
702 // Verify that a NetworkState exists with a matching GUID.
703 const NetworkState* network =
704 network_state_handler_->GetNetworkStateFromServicePath(
705 wifi_path, is_service_configured);
706 ASSERT_TRUE(network);
707 EXPECT_EQ(wifi_guid, network->guid());
709 // Remove the service (simulating a network going out of range).
710 service_test_->RemoveService(wifi_path);
711 UpdateManagerProperties();
712 EXPECT_FALSE(network_state_handler_->GetNetworkState(wifi_path));
714 // Add the service (simulating a network coming back in range) and verify that
715 // the NetworkState was created with the same GUID.
716 AddService(wifi_path, "" /* guid */, wifi_name,
717 shill::kTypeWifi, shill::kStateOnline);
718 UpdateManagerProperties();
719 network = network_state_handler_->GetNetworkStateFromServicePath(
720 wifi_path, is_service_configured);
721 ASSERT_TRUE(network);
722 EXPECT_EQ(wifi_guid, network->guid());
725 TEST_F(NetworkStateHandlerTest, NetworkGuidNotInProfile) {
726 const std::string wifi_path = "/service/wifi_with_guid";
727 const std::string wifi_name = "WifiWithGuid";
728 const bool is_service_configured = false;
730 // Add a network without specifying a GUID or adding it to a profile.
731 AddService(wifi_path, "" /* guid */, wifi_name,
732 shill::kTypeWifi, shill::kStateOnline);
733 UpdateManagerProperties();
735 // Verify that a NetworkState exists with an assigned GUID.
736 const NetworkState* network =
737 network_state_handler_->GetNetworkStateFromServicePath(
738 wifi_path, is_service_configured);
739 ASSERT_TRUE(network);
740 std::string wifi_guid = network->guid();
741 EXPECT_FALSE(wifi_guid.empty());
743 // Remove the service (simulating a network going out of range).
744 service_test_->RemoveService(wifi_path);
745 UpdateManagerProperties();
746 EXPECT_FALSE(network_state_handler_->GetNetworkState(wifi_path));
748 // Add the service (simulating a network coming back in range) and verify that
749 // the NetworkState was created with the same GUID.
750 AddService(wifi_path, "" /* guid */, wifi_name,
751 shill::kTypeWifi, shill::kStateOnline);
752 UpdateManagerProperties();
753 network = network_state_handler_->GetNetworkStateFromServicePath(
754 wifi_path, is_service_configured);
755 ASSERT_TRUE(network);
756 EXPECT_EQ(wifi_guid, network->guid());
759 TEST_F(NetworkStateHandlerTest, DeviceListChanged) {
760 size_t stub_device_count = test_observer_->device_count();
761 // Add an additional device.
762 const std::string wifi_device = "/service/stub_wifi_device2";
763 device_test_->AddDevice(wifi_device, shill::kTypeWifi, "stub_wifi_device2");
764 UpdateManagerProperties();
765 // Expect a device list update.
766 EXPECT_EQ(stub_device_count + 1, test_observer_->device_count());
767 EXPECT_EQ(0, test_observer_->PropertyUpdatesForDevice(wifi_device));
768 // Change a device property.
769 device_test_->SetDeviceProperty(
770 wifi_device, shill::kScanningProperty, base::FundamentalValue(true));
771 UpdateManagerProperties();
772 EXPECT_EQ(1, test_observer_->PropertyUpdatesForDevice(wifi_device));
775 TEST_F(NetworkStateHandlerTest, IPConfigChanged) {
776 test_observer_->reset_updates();
777 EXPECT_EQ(0, test_observer_->PropertyUpdatesForDevice(
778 kShillManagerClientStubWifiDevice));
779 EXPECT_EQ(0, test_observer_->PropertyUpdatesForService(
780 kShillManagerClientStubDefaultWifi));
782 // Change IPConfigs property.
783 ShillIPConfigClient::TestInterface* ip_config_test =
784 DBusThreadManager::Get()->GetShillIPConfigClient()->GetTestInterface();
785 const std::string kIPConfigPath = "test_ip_config";
786 base::DictionaryValue ip_config_properties;
787 ip_config_test->AddIPConfig(kIPConfigPath, ip_config_properties);
788 base::ListValue device_ip_configs;
789 device_ip_configs.Append(new base::StringValue(kIPConfigPath));
790 device_test_->SetDeviceProperty(
791 kShillManagerClientStubWifiDevice, shill::kIPConfigsProperty,
792 device_ip_configs);
793 service_test_->SetServiceProperty(
794 kShillManagerClientStubDefaultWifi, shill::kIPConfigProperty,
795 base::StringValue(kIPConfigPath));
796 UpdateManagerProperties();
797 EXPECT_EQ(1, test_observer_->PropertyUpdatesForDevice(
798 kShillManagerClientStubWifiDevice));
799 EXPECT_EQ(1, test_observer_->PropertyUpdatesForService(
800 kShillManagerClientStubDefaultWifi));
803 } // namespace chromeos