network_state_handler_unittest.cc revision f8ee788a64d60abd8f2d742a5fdedde054ecd910
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. 4 5#include "chromeos/network/network_state_handler.h" 6 7#include <map> 8#include <set> 9#include <string> 10 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_manager_client.h" 18#include "chromeos/dbus/shill_profile_client.h" 19#include "chromeos/dbus/shill_service_client.h" 20#include "chromeos/network/network_state.h" 21#include "chromeos/network/network_state_handler.h" 22#include "chromeos/network/network_state_handler_observer.h" 23#include "dbus/object_path.h" 24#include "testing/gtest/include/gtest/gtest.h" 25#include "third_party/cros_system_api/dbus/service_constants.h" 26 27namespace { 28 29void ErrorCallbackFunction(const std::string& error_name, 30 const std::string& error_message) { 31 LOG(ERROR) << "Shill Error: " << error_name << " : " << error_message; 32} 33 34const std::string kShillManagerClientStubDefaultService = "/service/eth1"; 35const std::string kShillManagerClientStubDefaultWifi = "/service/wifi1"; 36const std::string kShillManagerClientStubWifi2 = "/service/wifi2"; 37const std::string kShillManagerClientStubCellular = "/service/cellular1"; 38 39using chromeos::NetworkState; 40using chromeos::NetworkStateHandler; 41 42class TestObserver : public chromeos::NetworkStateHandlerObserver { 43 public: 44 explicit TestObserver(NetworkStateHandler* handler) 45 : handler_(handler), 46 device_list_changed_count_(0), 47 device_count_(0), 48 network_count_(0), 49 default_network_change_count_(0) { 50 } 51 52 virtual ~TestObserver() { 53 } 54 55 virtual void DeviceListChanged() OVERRIDE { 56 NetworkStateHandler::DeviceStateList devices; 57 handler_->GetDeviceList(&devices); 58 device_count_ = devices.size(); 59 ++device_list_changed_count_; 60 } 61 62 virtual void NetworkListChanged() OVERRIDE { 63 NetworkStateHandler::NetworkStateList networks; 64 handler_->GetNetworkListByType(chromeos::NetworkTypePattern::Default(), 65 false /* configured_only */, 66 false /* visible_only */, 67 0 /* no limit */, 68 &networks); 69 network_count_ = networks.size(); 70 if (network_count_ == 0) { 71 default_network_ = ""; 72 default_network_connection_state_ = ""; 73 } 74 } 75 76 virtual void DefaultNetworkChanged(const NetworkState* network) OVERRIDE { 77 ++default_network_change_count_; 78 default_network_ = network ? network->path() : ""; 79 default_network_connection_state_ = 80 network ? network->connection_state() : ""; 81 DVLOG(1) << "DefaultNetworkChanged: " << default_network_ 82 << " State: " << default_network_connection_state_; 83 } 84 85 virtual void NetworkConnectionStateChanged( 86 const NetworkState* network) OVERRIDE { 87 network_connection_state_[network->path()] = network->connection_state(); 88 connection_state_changes_[network->path()]++; 89 } 90 91 virtual void NetworkPropertiesUpdated(const NetworkState* network) OVERRIDE { 92 DCHECK(network); 93 property_updates_[network->path()]++; 94 } 95 96 size_t device_list_changed_count() { return device_list_changed_count_; } 97 size_t device_count() { return device_count_; } 98 size_t network_count() { return network_count_; } 99 size_t default_network_change_count() { 100 return default_network_change_count_; 101 } 102 void reset_change_counts() { 103 DVLOG(1) << "=== RESET CHANGE COUNTS ==="; 104 default_network_change_count_ = 0; 105 device_list_changed_count_ = 0; 106 } 107 std::string default_network() { return default_network_; } 108 std::string default_network_connection_state() { 109 return default_network_connection_state_; 110 } 111 112 int PropertyUpdatesForService(const std::string& service_path) { 113 return property_updates_[service_path]; 114 } 115 116 int ConnectionStateChangesForService(const std::string& service_path) { 117 return connection_state_changes_[service_path]; 118 } 119 120 std::string NetworkConnectionStateForService( 121 const std::string& service_path) { 122 return network_connection_state_[service_path]; 123 } 124 125 private: 126 NetworkStateHandler* handler_; 127 size_t device_list_changed_count_; 128 size_t device_count_; 129 size_t network_count_; 130 size_t default_network_change_count_; 131 std::string default_network_; 132 std::string default_network_connection_state_; 133 std::map<std::string, int> property_updates_; 134 std::map<std::string, int> connection_state_changes_; 135 std::map<std::string, std::string> network_connection_state_; 136 137 DISALLOW_COPY_AND_ASSIGN(TestObserver); 138}; 139 140} // namespace 141 142namespace chromeos { 143 144class NetworkStateHandlerTest : public testing::Test { 145 public: 146 NetworkStateHandlerTest() 147 : device_test_(NULL), 148 manager_test_(NULL), 149 profile_test_(NULL), 150 service_test_(NULL) {} 151 virtual ~NetworkStateHandlerTest() {} 152 153 virtual void SetUp() OVERRIDE { 154 // Initialize DBusThreadManager with a stub implementation. 155 DBusThreadManager::InitializeWithStub(); 156 SetupDefaultShillState(); 157 network_state_handler_.reset(new NetworkStateHandler); 158 test_observer_.reset(new TestObserver(network_state_handler_.get())); 159 network_state_handler_->AddObserver(test_observer_.get(), FROM_HERE); 160 network_state_handler_->InitShillPropertyHandler(); 161 message_loop_.RunUntilIdle(); 162 test_observer_->reset_change_counts(); 163 } 164 165 virtual void TearDown() OVERRIDE { 166 network_state_handler_->RemoveObserver(test_observer_.get(), FROM_HERE); 167 test_observer_.reset(); 168 network_state_handler_.reset(); 169 DBusThreadManager::Shutdown(); 170 } 171 172 protected: 173 void AddService(const std::string& service_path, 174 const std::string& name, 175 const std::string& type, 176 const std::string& state) { 177 service_test_->AddService(service_path, name, type, state, 178 true /* add_to_visible */); 179 } 180 181 void SetupDefaultShillState() { 182 message_loop_.RunUntilIdle(); // Process any pending updates 183 device_test_ = 184 DBusThreadManager::Get()->GetShillDeviceClient()->GetTestInterface(); 185 ASSERT_TRUE(device_test_); 186 device_test_->ClearDevices(); 187 device_test_->AddDevice( 188 "/device/stub_wifi_device1", shill::kTypeWifi, "stub_wifi_device1"); 189 device_test_->AddDevice("/device/stub_cellular_device1", 190 shill::kTypeCellular, 191 "stub_cellular_device1"); 192 193 manager_test_ = 194 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface(); 195 ASSERT_TRUE(manager_test_); 196 197 profile_test_ = 198 DBusThreadManager::Get()->GetShillProfileClient()->GetTestInterface(); 199 ASSERT_TRUE(profile_test_); 200 profile_test_->ClearProfiles(); 201 202 service_test_ = 203 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface(); 204 ASSERT_TRUE(service_test_); 205 service_test_->ClearServices(); 206 AddService(kShillManagerClientStubDefaultService, 207 "eth1", 208 shill::kTypeEthernet, 209 shill::kStateOnline); 210 AddService(kShillManagerClientStubDefaultWifi, 211 "wifi1", 212 shill::kTypeWifi, 213 shill::kStateOnline); 214 AddService(kShillManagerClientStubWifi2, 215 "wifi2", 216 shill::kTypeWifi, 217 shill::kStateIdle); 218 AddService(kShillManagerClientStubCellular, 219 "cellular1", 220 shill::kTypeCellular, 221 shill::kStateIdle); 222 } 223 224 void UpdateManagerProperties() { 225 message_loop_.RunUntilIdle(); 226 } 227 228 base::MessageLoopForUI message_loop_; 229 scoped_ptr<NetworkStateHandler> network_state_handler_; 230 scoped_ptr<TestObserver> test_observer_; 231 ShillDeviceClient::TestInterface* device_test_; 232 ShillManagerClient::TestInterface* manager_test_; 233 ShillProfileClient::TestInterface* profile_test_; 234 ShillServiceClient::TestInterface* service_test_; 235 236 private: 237 DISALLOW_COPY_AND_ASSIGN(NetworkStateHandlerTest); 238}; 239 240TEST_F(NetworkStateHandlerTest, NetworkStateHandlerStub) { 241 // Ensure that the device and network list are the expected size. 242 const size_t kNumShillManagerClientStubImplDevices = 2; 243 EXPECT_EQ(kNumShillManagerClientStubImplDevices, 244 test_observer_->device_count()); 245 const size_t kNumShillManagerClientStubImplServices = 4; 246 EXPECT_EQ(kNumShillManagerClientStubImplServices, 247 test_observer_->network_count()); 248 // Ensure that the first stub network is the default network. 249 EXPECT_EQ(kShillManagerClientStubDefaultService, 250 test_observer_->default_network()); 251 ASSERT_TRUE(network_state_handler_->DefaultNetwork()); 252 EXPECT_EQ(kShillManagerClientStubDefaultService, 253 network_state_handler_->DefaultNetwork()->path()); 254 EXPECT_EQ(kShillManagerClientStubDefaultService, 255 network_state_handler_->ConnectedNetworkByType( 256 NetworkTypePattern::Ethernet())->path()); 257 EXPECT_EQ(kShillManagerClientStubDefaultWifi, 258 network_state_handler_->ConnectedNetworkByType( 259 NetworkTypePattern::WiFi())->path()); 260 EXPECT_EQ(kShillManagerClientStubCellular, 261 network_state_handler_->FirstNetworkByType( 262 NetworkTypePattern::Mobile())->path()); 263 EXPECT_EQ( 264 kShillManagerClientStubCellular, 265 network_state_handler_->FirstNetworkByType(NetworkTypePattern::Cellular()) 266 ->path()); 267 EXPECT_EQ(shill::kStateOnline, 268 test_observer_->default_network_connection_state()); 269} 270 271TEST_F(NetworkStateHandlerTest, GetNetworkList) { 272 // Ensure that the network list is the expected size. 273 const size_t kNumShillManagerClientStubImplServices = 4; 274 EXPECT_EQ(kNumShillManagerClientStubImplServices, 275 test_observer_->network_count()); 276 // Add a non-visible network to the profile. 277 const std::string profile = "/profile/profile1"; 278 const std::string wifi_favorite_path = "/service/wifi_faviorite"; 279 service_test_->AddService(wifi_favorite_path, "wifi_faviorite", 280 shill::kTypeWifi, shill::kStateIdle, 281 false /* add_to_visible */); 282 profile_test_->AddProfile(profile, "" /* userhash */); 283 EXPECT_TRUE(profile_test_->AddService(profile, wifi_favorite_path)); 284 UpdateManagerProperties(); 285 message_loop_.RunUntilIdle(); 286 EXPECT_EQ(kNumShillManagerClientStubImplServices + 1, 287 test_observer_->network_count()); 288 289 // Get all networks. 290 NetworkStateHandler::NetworkStateList networks; 291 network_state_handler_->GetNetworkListByType(NetworkTypePattern::Default(), 292 false /* configured_only */, 293 false /* visible_only */, 294 0 /* no limit */, 295 &networks); 296 EXPECT_EQ(kNumShillManagerClientStubImplServices + 1, networks.size()); 297 // Limit number of results. 298 network_state_handler_->GetNetworkListByType(NetworkTypePattern::Default(), 299 false /* configured_only */, 300 false /* visible_only */, 301 2 /* limit */, 302 &networks); 303 EXPECT_EQ(2u, networks.size()); 304 // Get all wifi networks. 305 network_state_handler_->GetNetworkListByType(NetworkTypePattern::WiFi(), 306 false /* configured_only */, 307 false /* visible_only */, 308 0 /* no limit */, 309 &networks); 310 EXPECT_EQ(3u, networks.size()); 311 // Get visible networks. 312 network_state_handler_->GetNetworkListByType(NetworkTypePattern::Default(), 313 false /* configured_only */, 314 true /* visible_only */, 315 0 /* no limit */, 316 &networks); 317 EXPECT_EQ(kNumShillManagerClientStubImplServices, networks.size()); 318 network_state_handler_->GetVisibleNetworkList(&networks); 319 EXPECT_EQ(kNumShillManagerClientStubImplServices, networks.size()); 320 // Get configured (profile) networks. 321 network_state_handler_->GetNetworkListByType(NetworkTypePattern::Default(), 322 true /* configured_only */, 323 false /* visible_only */, 324 0 /* no limit */, 325 &networks); 326 EXPECT_EQ(1u, networks.size()); 327} 328 329TEST_F(NetworkStateHandlerTest, GetVisibleNetworks) { 330 // Ensure that the network list is the expected size. 331 const size_t kNumShillManagerClientStubImplServices = 4; 332 EXPECT_EQ(kNumShillManagerClientStubImplServices, 333 test_observer_->network_count()); 334 // Add a non-visible network to the profile. 335 const std::string profile = "/profile/profile1"; 336 const std::string wifi_favorite_path = "/service/wifi_faviorite"; 337 service_test_->AddService(wifi_favorite_path, "wifi_faviorite", 338 shill::kTypeWifi, shill::kStateIdle, 339 false /* add_to_visible */); 340 message_loop_.RunUntilIdle(); 341 EXPECT_EQ(kNumShillManagerClientStubImplServices + 1, 342 test_observer_->network_count()); 343 344 // Get visible networks. 345 NetworkStateHandler::NetworkStateList networks; 346 network_state_handler_->GetVisibleNetworkList(&networks); 347 EXPECT_EQ(kNumShillManagerClientStubImplServices, networks.size()); 348 349 // Change the visible state of a network. 350 DBusThreadManager::Get()->GetShillServiceClient()->SetProperty( 351 dbus::ObjectPath(kShillManagerClientStubWifi2), 352 shill::kVisibleProperty, base::FundamentalValue(false), 353 base::Bind(&base::DoNothing), base::Bind(&ErrorCallbackFunction)); 354 message_loop_.RunUntilIdle(); 355 network_state_handler_->GetVisibleNetworkList(&networks); 356 EXPECT_EQ(kNumShillManagerClientStubImplServices - 1, networks.size()); 357} 358 359TEST_F(NetworkStateHandlerTest, TechnologyChanged) { 360 // Disable a technology. Will immediately set the state to AVAILABLE and 361 // notify observers. 362 network_state_handler_->SetTechnologyEnabled( 363 NetworkTypePattern::WiFi(), false, network_handler::ErrorCallback()); 364 EXPECT_EQ(1u, test_observer_->device_list_changed_count()); 365 EXPECT_EQ( 366 NetworkStateHandler::TECHNOLOGY_AVAILABLE, 367 network_state_handler_->GetTechnologyState(NetworkTypePattern::WiFi())); 368 369 // Run the message loop. An additional notification will be received when 370 // Shill updates the enabled technologies. The state should remain AVAILABLE. 371 test_observer_->reset_change_counts(); 372 message_loop_.RunUntilIdle(); 373 EXPECT_EQ(1u, test_observer_->device_list_changed_count()); 374 EXPECT_EQ( 375 NetworkStateHandler::TECHNOLOGY_AVAILABLE, 376 network_state_handler_->GetTechnologyState(NetworkTypePattern::WiFi())); 377 378 // Enable a technology. Will immediately set the state to ENABLING and 379 // notify observers. 380 test_observer_->reset_change_counts(); 381 network_state_handler_->SetTechnologyEnabled( 382 NetworkTypePattern::WiFi(), true, network_handler::ErrorCallback()); 383 EXPECT_EQ(1u, test_observer_->device_list_changed_count()); 384 EXPECT_EQ( 385 NetworkStateHandler::TECHNOLOGY_ENABLING, 386 network_state_handler_->GetTechnologyState(NetworkTypePattern::WiFi())); 387 388 // Run the message loop. State should change to ENABLED. 389 test_observer_->reset_change_counts(); 390 message_loop_.RunUntilIdle(); 391 EXPECT_EQ(1u, test_observer_->device_list_changed_count()); 392 EXPECT_EQ( 393 NetworkStateHandler::TECHNOLOGY_ENABLED, 394 network_state_handler_->GetTechnologyState(NetworkTypePattern::WiFi())); 395} 396 397TEST_F(NetworkStateHandlerTest, TechnologyState) { 398 manager_test_->RemoveTechnology(shill::kTypeWimax); 399 message_loop_.RunUntilIdle(); 400 EXPECT_EQ( 401 NetworkStateHandler::TECHNOLOGY_UNAVAILABLE, 402 network_state_handler_->GetTechnologyState(NetworkTypePattern::Wimax())); 403 404 manager_test_->AddTechnology(shill::kTypeWimax, false); 405 message_loop_.RunUntilIdle(); 406 EXPECT_EQ( 407 NetworkStateHandler::TECHNOLOGY_AVAILABLE, 408 network_state_handler_->GetTechnologyState(NetworkTypePattern::Wimax())); 409 410 manager_test_->SetTechnologyInitializing(shill::kTypeWimax, true); 411 message_loop_.RunUntilIdle(); 412 EXPECT_EQ( 413 NetworkStateHandler::TECHNOLOGY_UNINITIALIZED, 414 network_state_handler_->GetTechnologyState(NetworkTypePattern::Wimax())); 415 416 manager_test_->SetTechnologyInitializing(shill::kTypeWimax, false); 417 network_state_handler_->SetTechnologyEnabled( 418 NetworkTypePattern::Wimax(), true, network_handler::ErrorCallback()); 419 message_loop_.RunUntilIdle(); 420 EXPECT_EQ( 421 NetworkStateHandler::TECHNOLOGY_ENABLED, 422 network_state_handler_->GetTechnologyState(NetworkTypePattern::Wimax())); 423 424 manager_test_->RemoveTechnology(shill::kTypeWimax); 425 message_loop_.RunUntilIdle(); 426 EXPECT_EQ( 427 NetworkStateHandler::TECHNOLOGY_UNAVAILABLE, 428 network_state_handler_->GetTechnologyState(NetworkTypePattern::Wimax())); 429} 430 431TEST_F(NetworkStateHandlerTest, ServicePropertyChanged) { 432 // Set a service property. 433 const std::string eth1 = kShillManagerClientStubDefaultService; 434 const NetworkState* ethernet = network_state_handler_->GetNetworkState(eth1); 435 ASSERT_TRUE(ethernet); 436 EXPECT_EQ("", ethernet->security()); 437 EXPECT_EQ(1, test_observer_->PropertyUpdatesForService(eth1)); 438 base::StringValue security_value("TestSecurity"); 439 DBusThreadManager::Get()->GetShillServiceClient()->SetProperty( 440 dbus::ObjectPath(eth1), 441 shill::kSecurityProperty, security_value, 442 base::Bind(&base::DoNothing), base::Bind(&ErrorCallbackFunction)); 443 message_loop_.RunUntilIdle(); 444 ethernet = network_state_handler_->GetNetworkState(eth1); 445 EXPECT_EQ("TestSecurity", ethernet->security()); 446 EXPECT_EQ(2, test_observer_->PropertyUpdatesForService(eth1)); 447 448 // Changing a service to the existing value should not trigger an update. 449 DBusThreadManager::Get()->GetShillServiceClient()->SetProperty( 450 dbus::ObjectPath(eth1), 451 shill::kSecurityProperty, security_value, 452 base::Bind(&base::DoNothing), base::Bind(&ErrorCallbackFunction)); 453 message_loop_.RunUntilIdle(); 454 EXPECT_EQ(2, test_observer_->PropertyUpdatesForService(eth1)); 455} 456 457TEST_F(NetworkStateHandlerTest, GetState) { 458 const std::string profile = "/profile/profile1"; 459 const std::string wifi_path = kShillManagerClientStubDefaultWifi; 460 461 // Add a wifi service to a Profile. 462 profile_test_->AddProfile(profile, "" /* userhash */); 463 EXPECT_TRUE(profile_test_->AddService(profile, wifi_path)); 464 UpdateManagerProperties(); 465 466 // Ensure that a NetworkState exists. 467 const NetworkState* wifi_network = 468 network_state_handler_->GetNetworkStateFromServicePath( 469 wifi_path, true /* configured_only */); 470 ASSERT_TRUE(wifi_network); 471 472 // Test looking up by GUID. 473 ASSERT_FALSE(wifi_network->guid().empty()); 474 const NetworkState* wifi_network_guid = 475 network_state_handler_->GetNetworkStateFromGuid(wifi_network->guid()); 476 EXPECT_EQ(wifi_network, wifi_network_guid); 477 478 // Remove the service, verify that there is no longer a NetworkState for it. 479 service_test_->RemoveService(wifi_path); 480 UpdateManagerProperties(); 481 EXPECT_FALSE(network_state_handler_->GetNetworkState(wifi_path)); 482} 483 484TEST_F(NetworkStateHandlerTest, NetworkConnectionStateChanged) { 485 // Change a network state. 486 const std::string eth1 = kShillManagerClientStubDefaultService; 487 base::StringValue connection_state_idle_value(shill::kStateIdle); 488 service_test_->SetServiceProperty(eth1, shill::kStateProperty, 489 connection_state_idle_value); 490 message_loop_.RunUntilIdle(); 491 EXPECT_EQ(shill::kStateIdle, 492 test_observer_->NetworkConnectionStateForService(eth1)); 493 EXPECT_EQ(2, test_observer_->ConnectionStateChangesForService(eth1)); 494 // Confirm that changing the connection state to the same value does *not* 495 // signal the observer. 496 service_test_->SetServiceProperty(eth1, shill::kStateProperty, 497 connection_state_idle_value); 498 message_loop_.RunUntilIdle(); 499 EXPECT_EQ(2, test_observer_->ConnectionStateChangesForService(eth1)); 500} 501 502TEST_F(NetworkStateHandlerTest, DefaultServiceDisconnected) { 503 const std::string eth1 = kShillManagerClientStubDefaultService; 504 const std::string wifi1 = kShillManagerClientStubDefaultWifi; 505 506 EXPECT_EQ(0u, test_observer_->default_network_change_count()); 507 // Disconnect ethernet. 508 base::StringValue connection_state_idle_value(shill::kStateIdle); 509 service_test_->SetServiceProperty(eth1, shill::kStateProperty, 510 connection_state_idle_value); 511 message_loop_.RunUntilIdle(); 512 // Expect two changes: first when eth1 becomes disconnected, second when 513 // wifi1 becomes the default. 514 EXPECT_EQ(2u, test_observer_->default_network_change_count()); 515 EXPECT_EQ(wifi1, test_observer_->default_network()); 516 517 // Disconnect wifi. 518 test_observer_->reset_change_counts(); 519 service_test_->SetServiceProperty(wifi1, shill::kStateProperty, 520 connection_state_idle_value); 521 message_loop_.RunUntilIdle(); 522 EXPECT_EQ(1u, test_observer_->default_network_change_count()); 523 EXPECT_EQ("", test_observer_->default_network()); 524} 525 526TEST_F(NetworkStateHandlerTest, DefaultServiceConnected) { 527 const std::string eth1 = kShillManagerClientStubDefaultService; 528 const std::string wifi1 = kShillManagerClientStubDefaultWifi; 529 530 // Disconnect ethernet and wifi. 531 base::StringValue connection_state_idle_value(shill::kStateIdle); 532 service_test_->SetServiceProperty(eth1, shill::kStateProperty, 533 connection_state_idle_value); 534 service_test_->SetServiceProperty(wifi1, shill::kStateProperty, 535 connection_state_idle_value); 536 message_loop_.RunUntilIdle(); 537 EXPECT_EQ(std::string(), test_observer_->default_network()); 538 539 // Connect ethernet, should become the default network. 540 test_observer_->reset_change_counts(); 541 base::StringValue connection_state_ready_value(shill::kStateReady); 542 service_test_->SetServiceProperty(eth1, shill::kStateProperty, 543 connection_state_ready_value); 544 message_loop_.RunUntilIdle(); 545 EXPECT_EQ(eth1, test_observer_->default_network()); 546 EXPECT_EQ(shill::kStateReady, 547 test_observer_->default_network_connection_state()); 548 EXPECT_EQ(1u, test_observer_->default_network_change_count()); 549} 550 551TEST_F(NetworkStateHandlerTest, DefaultServiceChanged) { 552 const std::string eth1 = kShillManagerClientStubDefaultService; 553 // The default service should be eth1. 554 EXPECT_EQ(eth1, test_observer_->default_network()); 555 556 // Change the default network by changing Manager.DefaultService. 557 const std::string wifi1 = kShillManagerClientStubDefaultWifi; 558 base::StringValue wifi1_value(wifi1); 559 manager_test_->SetManagerProperty( 560 shill::kDefaultServiceProperty, wifi1_value); 561 message_loop_.RunUntilIdle(); 562 EXPECT_EQ(wifi1, test_observer_->default_network()); 563 EXPECT_EQ(1u, test_observer_->default_network_change_count()); 564 565 // Change the state of the default network. 566 test_observer_->reset_change_counts(); 567 base::StringValue connection_state_ready_value(shill::kStateReady); 568 service_test_->SetServiceProperty(wifi1, shill::kStateProperty, 569 connection_state_ready_value); 570 message_loop_.RunUntilIdle(); 571 EXPECT_EQ(shill::kStateReady, 572 test_observer_->default_network_connection_state()); 573 EXPECT_EQ(1u, test_observer_->default_network_change_count()); 574 575 // Updating a property on the default network should trigger 576 // a default network change. 577 test_observer_->reset_change_counts(); 578 DBusThreadManager::Get()->GetShillServiceClient()->SetProperty( 579 dbus::ObjectPath(wifi1), 580 shill::kSecurityProperty, base::StringValue("TestSecurity"), 581 base::Bind(&base::DoNothing), base::Bind(&ErrorCallbackFunction)); 582 message_loop_.RunUntilIdle(); 583 EXPECT_EQ(1u, test_observer_->default_network_change_count()); 584 585 // No default network updates for signal strength changes. 586 test_observer_->reset_change_counts(); 587 DBusThreadManager::Get()->GetShillServiceClient()->SetProperty( 588 dbus::ObjectPath(wifi1), 589 shill::kSignalStrengthProperty, base::FundamentalValue(32), 590 base::Bind(&base::DoNothing), base::Bind(&ErrorCallbackFunction)); 591 message_loop_.RunUntilIdle(); 592 EXPECT_EQ(0u, test_observer_->default_network_change_count()); 593} 594 595TEST_F(NetworkStateHandlerTest, RequestUpdate) { 596 // Request an update for kShillManagerClientStubDefaultWifi. 597 EXPECT_EQ(1, test_observer_->PropertyUpdatesForService( 598 kShillManagerClientStubDefaultWifi)); 599 network_state_handler_->RequestUpdateForNetwork( 600 kShillManagerClientStubDefaultWifi); 601 message_loop_.RunUntilIdle(); 602 EXPECT_EQ(2, test_observer_->PropertyUpdatesForService( 603 kShillManagerClientStubDefaultWifi)); 604} 605 606TEST_F(NetworkStateHandlerTest, NetworkGuidInProfile) { 607 const std::string profile = "/profile/profile1"; 608 const std::string wifi_path = "/service/wifi_with_guid"; 609 const std::string wifi_guid = "WIFI_GUID"; 610 const bool is_service_configured = true; 611 612 // Add a network to the default Profile with a specified GUID. 613 service_test_->AddServiceWithIPConfig( 614 wifi_path, 615 wifi_guid, 616 wifi_path /* name */, 617 shill::kTypeWifi, 618 shill::kStateOnline, 619 "" /* ipconfig_path */, 620 true /* add_to_visible */); 621 profile_test_->AddProfile(profile, "" /* userhash */); 622 EXPECT_TRUE(profile_test_->AddService(profile, wifi_path)); 623 UpdateManagerProperties(); 624 625 // Verify that a NetworkState exists with a matching GUID. 626 const NetworkState* network = 627 network_state_handler_->GetNetworkStateFromServicePath( 628 wifi_path, is_service_configured); 629 ASSERT_TRUE(network); 630 EXPECT_EQ(wifi_guid, network->guid()); 631 632 // Remove the service (simulating a network going out of range). 633 service_test_->RemoveService(wifi_path); 634 UpdateManagerProperties(); 635 EXPECT_FALSE(network_state_handler_->GetNetworkState(wifi_path)); 636 637 // Add the service (simulating a network coming back in range) and verify that 638 // the NetworkState was created with the same GUID. 639 AddService(wifi_path, wifi_path, shill::kTypeWifi, shill::kStateOnline); 640 UpdateManagerProperties(); 641 network = network_state_handler_->GetNetworkStateFromServicePath( 642 wifi_path, is_service_configured); 643 ASSERT_TRUE(network); 644 EXPECT_EQ(wifi_guid, network->guid()); 645} 646 647TEST_F(NetworkStateHandlerTest, NetworkGuidNotInProfile) { 648 const std::string wifi_path = "/service/wifi_with_guid"; 649 const bool is_service_configured = false; 650 651 // Add a network without adding it to a profile. 652 AddService(wifi_path, wifi_path, shill::kTypeWifi, shill::kStateOnline); 653 UpdateManagerProperties(); 654 655 // Verify that a NetworkState exists with an assigned GUID. 656 const NetworkState* network = 657 network_state_handler_->GetNetworkStateFromServicePath( 658 wifi_path, is_service_configured); 659 ASSERT_TRUE(network); 660 std::string wifi_guid = network->guid(); 661 EXPECT_FALSE(wifi_guid.empty()); 662 663 // Remove the service (simulating a network going out of range). 664 service_test_->RemoveService(wifi_path); 665 UpdateManagerProperties(); 666 EXPECT_FALSE(network_state_handler_->GetNetworkState(wifi_path)); 667 668 // Add the service (simulating a network coming back in range) and verify that 669 // the NetworkState was created with the same GUID. 670 AddService(wifi_path, wifi_path, shill::kTypeWifi, shill::kStateOnline); 671 UpdateManagerProperties(); 672 network = network_state_handler_->GetNetworkStateFromServicePath( 673 wifi_path, is_service_configured); 674 ASSERT_TRUE(network); 675 EXPECT_EQ(wifi_guid, network->guid()); 676} 677 678} // namespace chromeos 679