OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chromeos/network/network_state_handler.h" | 5 #include "chromeos/network/network_state_handler.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <set> | 8 #include <set> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
14 #include "base/values.h" | 14 #include "base/values.h" |
15 #include "chromeos/dbus/dbus_thread_manager.h" | 15 #include "chromeos/dbus/dbus_thread_manager.h" |
16 #include "chromeos/dbus/shill_device_client.h" | 16 #include "chromeos/dbus/shill_device_client.h" |
17 #include "chromeos/dbus/shill_manager_client.h" | 17 #include "chromeos/dbus/shill_manager_client.h" |
18 #include "chromeos/dbus/shill_profile_client.h" | 18 #include "chromeos/dbus/shill_profile_client.h" |
19 #include "chromeos/dbus/shill_service_client.h" | 19 #include "chromeos/dbus/shill_service_client.h" |
| 20 #include "chromeos/network/favorite_state.h" |
20 #include "chromeos/network/network_state.h" | 21 #include "chromeos/network/network_state.h" |
21 #include "chromeos/network/network_state_handler_observer.h" | 22 #include "chromeos/network/network_state_handler_observer.h" |
22 #include "chromeos/network/shill_property_util.h" | 23 #include "chromeos/network/shill_property_util.h" |
23 #include "dbus/object_path.h" | 24 #include "dbus/object_path.h" |
24 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
25 #include "third_party/cros_system_api/dbus/service_constants.h" | 26 #include "third_party/cros_system_api/dbus/service_constants.h" |
26 | 27 |
27 namespace { | 28 namespace { |
28 | 29 |
29 void ErrorCallbackFunction(const std::string& error_name, | 30 void ErrorCallbackFunction(const std::string& error_name, |
(...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
350 | 351 |
351 // Changing a service to the existing value should not trigger an update. | 352 // Changing a service to the existing value should not trigger an update. |
352 DBusThreadManager::Get()->GetShillServiceClient()->SetProperty( | 353 DBusThreadManager::Get()->GetShillServiceClient()->SetProperty( |
353 dbus::ObjectPath(eth1), | 354 dbus::ObjectPath(eth1), |
354 shill::kSecurityProperty, security_value, | 355 shill::kSecurityProperty, security_value, |
355 base::Bind(&base::DoNothing), base::Bind(&ErrorCallbackFunction)); | 356 base::Bind(&base::DoNothing), base::Bind(&ErrorCallbackFunction)); |
356 message_loop_.RunUntilIdle(); | 357 message_loop_.RunUntilIdle(); |
357 EXPECT_EQ(2, test_observer_->PropertyUpdatesForService(eth1)); | 358 EXPECT_EQ(2, test_observer_->PropertyUpdatesForService(eth1)); |
358 } | 359 } |
359 | 360 |
360 TEST_F(NetworkStateHandlerTest, FavoriteState) { | 361 TEST_F(NetworkStateHandlerTest, GetState) { |
361 // Set the profile entry of a service | |
362 const std::string profile = "/profile/profile1"; | 362 const std::string profile = "/profile/profile1"; |
363 const std::string wifi1 = kShillManagerClientStubDefaultWifi; | 363 const std::string wifi_path = kShillManagerClientStubDefaultWifi; |
| 364 |
| 365 // Add a wifi service to a Profile. |
364 profile_test_->AddProfile(profile, "" /* userhash */); | 366 profile_test_->AddProfile(profile, "" /* userhash */); |
365 EXPECT_TRUE(profile_test_->AddService(profile, wifi1)); | 367 EXPECT_TRUE(profile_test_->AddService(profile, wifi_path)); |
366 UpdateManagerProperties(); | 368 UpdateManagerProperties(); |
| 369 |
| 370 // Ensure that a NetworkState and corresponding FavoriteState exist. |
| 371 const NetworkState* wifi_network = |
| 372 network_state_handler_->GetNetworkState(wifi_path); |
| 373 ASSERT_TRUE(wifi_network); |
| 374 const FavoriteState* wifi_favorite = |
| 375 network_state_handler_->GetFavoriteStateFromServicePath( |
| 376 wifi_path, true /* configured_only */); |
| 377 ASSERT_TRUE(wifi_favorite); |
| 378 EXPECT_EQ(wifi_network->path(), wifi_favorite->path()); |
| 379 |
| 380 // Ensure that we are notified that a Favorite was added. |
367 EXPECT_EQ(1u, test_observer_->favorite_count()); | 381 EXPECT_EQ(1u, test_observer_->favorite_count()); |
| 382 |
| 383 // Test looking up by GUID. |
| 384 ASSERT_FALSE(wifi_favorite->guid().empty()); |
| 385 const FavoriteState* wifi_favorite_guid = |
| 386 network_state_handler_->GetFavoriteStateFromGuid(wifi_favorite->guid()); |
| 387 EXPECT_EQ(wifi_favorite, wifi_favorite_guid); |
| 388 |
| 389 // Remove the service, verify that there is no longer a NetworkState for it. |
| 390 service_test_->RemoveService(wifi_path); |
| 391 UpdateManagerProperties(); |
| 392 EXPECT_FALSE(network_state_handler_->GetNetworkState(wifi_path)); |
368 } | 393 } |
369 | 394 |
370 TEST_F(NetworkStateHandlerTest, NetworkConnectionStateChanged) { | 395 TEST_F(NetworkStateHandlerTest, NetworkConnectionStateChanged) { |
371 // Change a network state. | 396 // Change a network state. |
372 const std::string eth1 = kShillManagerClientStubDefaultService; | 397 const std::string eth1 = kShillManagerClientStubDefaultService; |
373 base::StringValue connection_state_idle_value(shill::kStateIdle); | 398 base::StringValue connection_state_idle_value(shill::kStateIdle); |
374 service_test_->SetServiceProperty(eth1, shill::kStateProperty, | 399 service_test_->SetServiceProperty(eth1, shill::kStateProperty, |
375 connection_state_idle_value); | 400 connection_state_idle_value); |
376 message_loop_.RunUntilIdle(); | 401 message_loop_.RunUntilIdle(); |
377 EXPECT_EQ(shill::kStateIdle, | 402 EXPECT_EQ(shill::kStateIdle, |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
482 TEST_F(NetworkStateHandlerTest, RequestUpdate) { | 507 TEST_F(NetworkStateHandlerTest, RequestUpdate) { |
483 // Request an update for kShillManagerClientStubDefaultWifi. | 508 // Request an update for kShillManagerClientStubDefaultWifi. |
484 EXPECT_EQ(1, test_observer_->PropertyUpdatesForService( | 509 EXPECT_EQ(1, test_observer_->PropertyUpdatesForService( |
485 kShillManagerClientStubDefaultWifi)); | 510 kShillManagerClientStubDefaultWifi)); |
486 network_state_handler_->RequestUpdateForNetwork( | 511 network_state_handler_->RequestUpdateForNetwork( |
487 kShillManagerClientStubDefaultWifi); | 512 kShillManagerClientStubDefaultWifi); |
488 message_loop_.RunUntilIdle(); | 513 message_loop_.RunUntilIdle(); |
489 EXPECT_EQ(2, test_observer_->PropertyUpdatesForService( | 514 EXPECT_EQ(2, test_observer_->PropertyUpdatesForService( |
490 kShillManagerClientStubDefaultWifi)); | 515 kShillManagerClientStubDefaultWifi)); |
491 } | 516 } |
| 517 |
| 518 TEST_F(NetworkStateHandlerTest, NetworkGuidInProfile) { |
| 519 const std::string profile = "/profile/profile1"; |
| 520 const std::string wifi_path = "wifi_with_guid"; |
| 521 const std::string wifi_guid = "WIFI_GUID"; |
| 522 const bool is_service_configured = true; |
| 523 |
| 524 // Add a network to the default Profile with a specified GUID. |
| 525 service_test_->AddServiceWithIPConfig( |
| 526 wifi_path, |
| 527 wifi_guid, |
| 528 wifi_path /* name */, |
| 529 shill::kTypeWifi, |
| 530 shill::kStateOnline, |
| 531 "" /* ipconfig_path */, |
| 532 true /* add_to_visible */, |
| 533 true /* add_to_watchlist */); |
| 534 profile_test_->AddProfile(profile, "" /* userhash */); |
| 535 EXPECT_TRUE(profile_test_->AddService(profile, wifi_path)); |
| 536 UpdateManagerProperties(); |
| 537 |
| 538 // Verify that a FavoriteState exists with a matching GUID. |
| 539 const FavoriteState* favorite = |
| 540 network_state_handler_->GetFavoriteStateFromServicePath( |
| 541 wifi_path, is_service_configured); |
| 542 ASSERT_TRUE(favorite); |
| 543 EXPECT_EQ(wifi_guid, favorite->guid()); |
| 544 |
| 545 // Verify that a NetworkState exists with the same GUID. |
| 546 const NetworkState* network = |
| 547 network_state_handler_->GetNetworkState(wifi_path); |
| 548 ASSERT_TRUE(network); |
| 549 EXPECT_EQ(wifi_guid, network->guid()); |
| 550 |
| 551 // Remove the service (simulating a network going out of range). |
| 552 service_test_->RemoveService(wifi_path); |
| 553 UpdateManagerProperties(); |
| 554 EXPECT_FALSE(network_state_handler_->GetNetworkState(wifi_path)); |
| 555 |
| 556 // Add the service (simulating a network coming back in range) and verify that |
| 557 // the NetworkState was created with the same GUID. |
| 558 AddService(wifi_path, wifi_path, shill::kTypeWifi, shill::kStateOnline); |
| 559 UpdateManagerProperties(); |
| 560 network = network_state_handler_->GetNetworkState(wifi_path); |
| 561 ASSERT_TRUE(network); |
| 562 EXPECT_EQ(wifi_guid, network->guid()); |
| 563 |
| 564 // Also verify FavoriteState (mostly to test the stub behavior). |
| 565 favorite = network_state_handler_->GetFavoriteStateFromServicePath( |
| 566 wifi_path, is_service_configured); |
| 567 ASSERT_TRUE(favorite); |
| 568 EXPECT_EQ(wifi_guid, favorite->guid()); |
| 569 } |
| 570 |
| 571 TEST_F(NetworkStateHandlerTest, NetworkGuidNotInProfile) { |
| 572 const std::string wifi_path = "wifi_with_guid"; |
| 573 const bool is_service_configured = false; |
| 574 |
| 575 // Add a network without adding it to a profile. |
| 576 AddService(wifi_path, wifi_path, shill::kTypeWifi, shill::kStateOnline); |
| 577 UpdateManagerProperties(); |
| 578 |
| 579 // Verify that a FavoriteState exists with an assigned GUID. |
| 580 const FavoriteState* favorite = |
| 581 network_state_handler_->GetFavoriteStateFromServicePath( |
| 582 wifi_path, is_service_configured); |
| 583 ASSERT_TRUE(favorite); |
| 584 std::string wifi_guid = favorite->guid(); |
| 585 EXPECT_FALSE(wifi_guid.empty()); |
| 586 |
| 587 // Verify that a NetworkState exists with the same GUID. |
| 588 const NetworkState* network = |
| 589 network_state_handler_->GetNetworkState(wifi_path); |
| 590 ASSERT_TRUE(network); |
| 591 EXPECT_EQ(wifi_guid, network->guid()); |
| 592 |
| 593 // Remove the service (simulating a network going out of range). |
| 594 service_test_->RemoveService(wifi_path); |
| 595 UpdateManagerProperties(); |
| 596 EXPECT_FALSE(network_state_handler_->GetNetworkState(wifi_path)); |
| 597 |
| 598 // Add the service (simulating a network coming back in range) and verify that |
| 599 // the NetworkState was created with the same GUID. |
| 600 AddService(wifi_path, wifi_path, shill::kTypeWifi, shill::kStateOnline); |
| 601 UpdateManagerProperties(); |
| 602 network = network_state_handler_->GetNetworkState(wifi_path); |
| 603 ASSERT_TRUE(network); |
| 604 EXPECT_EQ(wifi_guid, network->guid()); |
| 605 |
| 606 // Also verify FavoriteState (mostly to test the stub behavior). |
| 607 favorite = network_state_handler_->GetFavoriteStateFromServicePath( |
| 608 wifi_path, is_service_configured); |
| 609 ASSERT_TRUE(favorite); |
| 610 EXPECT_EQ(wifi_guid, favorite->guid()); |
| 611 } |
| 612 |
492 } // namespace chromeos | 613 } // namespace chromeos |
OLD | NEW |