Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(718)

Side by Side Diff: chromeos/network/network_state_handler_unittest.cc

Issue 979183003: Fix logic for default network notification (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@issue_432404_default_network_0
Patch Set: Rebase Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chromeos/network/network_state_handler.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
75 &networks); 75 &networks);
76 network_count_ = networks.size(); 76 network_count_ = networks.size();
77 if (network_count_ == 0) { 77 if (network_count_ == 0) {
78 default_network_ = ""; 78 default_network_ = "";
79 default_network_connection_state_ = ""; 79 default_network_connection_state_ = "";
80 } 80 }
81 ++network_list_changed_count_; 81 ++network_list_changed_count_;
82 } 82 }
83 83
84 void DefaultNetworkChanged(const NetworkState* network) override { 84 void DefaultNetworkChanged(const NetworkState* network) override {
85 EXPECT_TRUE(!network || network->IsConnectedState());
85 ++default_network_change_count_; 86 ++default_network_change_count_;
86 default_network_ = network ? network->path() : ""; 87 default_network_ = network ? network->path() : "";
87 default_network_connection_state_ = 88 default_network_connection_state_ =
88 network ? network->connection_state() : ""; 89 network ? network->connection_state() : "";
89 DVLOG(1) << "DefaultNetworkChanged: " << default_network_ 90 DVLOG(1) << "DefaultNetworkChanged: " << default_network_
90 << " State: " << default_network_connection_state_; 91 << " State: " << default_network_connection_state_;
91 } 92 }
92 93
93 void NetworkConnectionStateChanged(const NetworkState* network) override { 94 void NetworkConnectionStateChanged(const NetworkState* network) override {
94 network_connection_state_[network->path()] = network->connection_state(); 95 network_connection_state_[network->path()] = network->connection_state();
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
162 DISALLOW_COPY_AND_ASSIGN(TestObserver); 163 DISALLOW_COPY_AND_ASSIGN(TestObserver);
163 }; 164 };
164 165
165 } // namespace 166 } // namespace
166 167
167 namespace chromeos { 168 namespace chromeos {
168 169
169 class NetworkStateHandlerTest : public testing::Test { 170 class NetworkStateHandlerTest : public testing::Test {
170 public: 171 public:
171 NetworkStateHandlerTest() 172 NetworkStateHandlerTest()
172 : device_test_(NULL), 173 : device_test_(nullptr),
173 manager_test_(NULL), 174 manager_test_(nullptr),
174 profile_test_(NULL), 175 profile_test_(nullptr),
175 service_test_(NULL) {} 176 service_test_(nullptr) {}
176 ~NetworkStateHandlerTest() override {} 177 ~NetworkStateHandlerTest() override {}
177 178
178 void SetUp() override { 179 void SetUp() override {
179 // Initialize DBusThreadManager with a stub implementation. 180 // Initialize DBusThreadManager with a stub implementation.
180 DBusThreadManager::Initialize(); 181 DBusThreadManager::Initialize();
181 SetupDefaultShillState(); 182 SetupDefaultShillState();
182 network_state_handler_.reset(new NetworkStateHandler); 183 network_state_handler_.reset(new NetworkStateHandler);
183 test_observer_.reset(new TestObserver(network_state_handler_.get())); 184 test_observer_.reset(new TestObserver(network_state_handler_.get()));
184 network_state_handler_->AddObserver(test_observer_.get(), FROM_HERE); 185 network_state_handler_->AddObserver(test_observer_.get(), FROM_HERE);
185 network_state_handler_->InitShillPropertyHandler(); 186 network_state_handler_->InitShillPropertyHandler();
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
247 "cellular1_guid", 248 "cellular1_guid",
248 "cellular1", 249 "cellular1",
249 shill::kTypeCellular, 250 shill::kTypeCellular,
250 shill::kStateIdle); 251 shill::kStateIdle);
251 } 252 }
252 253
253 void UpdateManagerProperties() { 254 void UpdateManagerProperties() {
254 message_loop_.RunUntilIdle(); 255 message_loop_.RunUntilIdle();
255 } 256 }
256 257
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));
264 }
265
257 base::MessageLoopForUI message_loop_; 266 base::MessageLoopForUI message_loop_;
258 scoped_ptr<NetworkStateHandler> network_state_handler_; 267 scoped_ptr<NetworkStateHandler> network_state_handler_;
259 scoped_ptr<TestObserver> test_observer_; 268 scoped_ptr<TestObserver> test_observer_;
260 ShillDeviceClient::TestInterface* device_test_; 269 ShillDeviceClient::TestInterface* device_test_;
261 ShillManagerClient::TestInterface* manager_test_; 270 ShillManagerClient::TestInterface* manager_test_;
262 ShillProfileClient::TestInterface* profile_test_; 271 ShillProfileClient::TestInterface* profile_test_;
263 ShillServiceClient::TestInterface* service_test_; 272 ShillServiceClient::TestInterface* service_test_;
264 273
265 private: 274 private:
266 DISALLOW_COPY_AND_ASSIGN(NetworkStateHandlerTest); 275 DISALLOW_COPY_AND_ASSIGN(NetworkStateHandlerTest);
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
398 message_loop_.RunUntilIdle(); 407 message_loop_.RunUntilIdle();
399 EXPECT_EQ(kNumShillManagerClientStubImplServices + 1, 408 EXPECT_EQ(kNumShillManagerClientStubImplServices + 1,
400 test_observer_->network_count()); 409 test_observer_->network_count());
401 410
402 // Get visible networks. 411 // Get visible networks.
403 NetworkStateHandler::NetworkStateList networks; 412 NetworkStateHandler::NetworkStateList networks;
404 network_state_handler_->GetVisibleNetworkList(&networks); 413 network_state_handler_->GetVisibleNetworkList(&networks);
405 EXPECT_EQ(kNumShillManagerClientStubImplServices, networks.size()); 414 EXPECT_EQ(kNumShillManagerClientStubImplServices, networks.size());
406 415
407 // Change the visible state of a network. 416 // Change the visible state of a network.
408 DBusThreadManager::Get()->GetShillServiceClient()->SetProperty( 417 SetServiceProperty(kShillManagerClientStubWifi2, shill::kVisibleProperty,
409 dbus::ObjectPath(kShillManagerClientStubWifi2), 418 base::FundamentalValue(false));
410 shill::kVisibleProperty, base::FundamentalValue(false),
411 base::Bind(&base::DoNothing), base::Bind(&ErrorCallbackFunction));
412 message_loop_.RunUntilIdle(); 419 message_loop_.RunUntilIdle();
413 network_state_handler_->GetVisibleNetworkList(&networks); 420 network_state_handler_->GetVisibleNetworkList(&networks);
414 EXPECT_EQ(kNumShillManagerClientStubImplServices - 1, networks.size()); 421 EXPECT_EQ(kNumShillManagerClientStubImplServices - 1, networks.size());
415 } 422 }
416 423
417 TEST_F(NetworkStateHandlerTest, TechnologyChanged) { 424 TEST_F(NetworkStateHandlerTest, TechnologyChanged) {
418 // Disable a technology. Will immediately set the state to AVAILABLE and 425 // Disable a technology. Will immediately set the state to AVAILABLE and
419 // notify observers. 426 // notify observers.
420 network_state_handler_->SetTechnologyEnabled( 427 network_state_handler_->SetTechnologyEnabled(
421 NetworkTypePattern::WiFi(), false, network_handler::ErrorCallback()); 428 NetworkTypePattern::WiFi(), false, network_handler::ErrorCallback());
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
487 } 494 }
488 495
489 TEST_F(NetworkStateHandlerTest, ServicePropertyChanged) { 496 TEST_F(NetworkStateHandlerTest, ServicePropertyChanged) {
490 // Set a service property. 497 // Set a service property.
491 const std::string eth1 = kShillManagerClientStubDefaultService; 498 const std::string eth1 = kShillManagerClientStubDefaultService;
492 const NetworkState* ethernet = network_state_handler_->GetNetworkState(eth1); 499 const NetworkState* ethernet = network_state_handler_->GetNetworkState(eth1);
493 ASSERT_TRUE(ethernet); 500 ASSERT_TRUE(ethernet);
494 EXPECT_EQ("", ethernet->security_class()); 501 EXPECT_EQ("", ethernet->security_class());
495 EXPECT_EQ(1, test_observer_->PropertyUpdatesForService(eth1)); 502 EXPECT_EQ(1, test_observer_->PropertyUpdatesForService(eth1));
496 base::StringValue security_class_value("TestSecurityClass"); 503 base::StringValue security_class_value("TestSecurityClass");
497 DBusThreadManager::Get()->GetShillServiceClient()->SetProperty( 504 SetServiceProperty(eth1, shill::kSecurityClassProperty, security_class_value);
498 dbus::ObjectPath(eth1),
499 shill::kSecurityClassProperty, security_class_value,
500 base::Bind(&base::DoNothing), base::Bind(&ErrorCallbackFunction));
501 message_loop_.RunUntilIdle(); 505 message_loop_.RunUntilIdle();
502 ethernet = network_state_handler_->GetNetworkState(eth1); 506 ethernet = network_state_handler_->GetNetworkState(eth1);
503 EXPECT_EQ("TestSecurityClass", ethernet->security_class()); 507 EXPECT_EQ("TestSecurityClass", ethernet->security_class());
504 EXPECT_EQ(2, test_observer_->PropertyUpdatesForService(eth1)); 508 EXPECT_EQ(2, test_observer_->PropertyUpdatesForService(eth1));
505 509
506 // Changing a service to the existing value should not trigger an update. 510 // Changing a service to the existing value should not trigger an update.
507 DBusThreadManager::Get()->GetShillServiceClient()->SetProperty( 511 SetServiceProperty(eth1, shill::kSecurityClassProperty, security_class_value);
508 dbus::ObjectPath(eth1),
509 shill::kSecurityClassProperty, security_class_value,
510 base::Bind(&base::DoNothing), base::Bind(&ErrorCallbackFunction));
511 message_loop_.RunUntilIdle(); 512 message_loop_.RunUntilIdle();
512 EXPECT_EQ(2, test_observer_->PropertyUpdatesForService(eth1)); 513 EXPECT_EQ(2, test_observer_->PropertyUpdatesForService(eth1));
513 } 514 }
514 515
515 TEST_F(NetworkStateHandlerTest, GetState) { 516 TEST_F(NetworkStateHandlerTest, GetState) {
516 const std::string profile = "/profile/profile1"; 517 const std::string profile = "/profile/profile1";
517 const std::string wifi_path = kShillManagerClientStubDefaultWifi; 518 const std::string wifi_path = kShillManagerClientStubDefaultWifi;
518 519
519 // Add a wifi service to a Profile. 520 // Add a wifi service to a Profile.
520 profile_test_->AddProfile(profile, "" /* userhash */); 521 profile_test_->AddProfile(profile, "" /* userhash */);
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
607 test_observer_->default_network_connection_state()); 608 test_observer_->default_network_connection_state());
608 EXPECT_EQ(1u, test_observer_->default_network_change_count()); 609 EXPECT_EQ(1u, test_observer_->default_network_change_count());
609 } 610 }
610 611
611 TEST_F(NetworkStateHandlerTest, DefaultServiceChanged) { 612 TEST_F(NetworkStateHandlerTest, DefaultServiceChanged) {
612 const std::string eth1 = kShillManagerClientStubDefaultService; 613 const std::string eth1 = kShillManagerClientStubDefaultService;
613 // The default service should be eth1. 614 // The default service should be eth1.
614 EXPECT_EQ(eth1, test_observer_->default_network()); 615 EXPECT_EQ(eth1, test_observer_->default_network());
615 616
616 // Change the default network by changing Manager.DefaultService. 617 // Change the default network by changing Manager.DefaultService.
618 // This should only generate one default network notification when the
619 // DefaultService property changes.
617 const std::string wifi1 = kShillManagerClientStubDefaultWifi; 620 const std::string wifi1 = kShillManagerClientStubDefaultWifi;
618 base::StringValue wifi1_value(wifi1); 621 SetServiceProperty(eth1, shill::kStateProperty,
619 manager_test_->SetManagerProperty( 622 base::StringValue(shill::kStateIdle));
620 shill::kDefaultServiceProperty, wifi1_value); 623 manager_test_->SetManagerProperty(shill::kDefaultServiceProperty,
624 base::StringValue(wifi1));
621 message_loop_.RunUntilIdle(); 625 message_loop_.RunUntilIdle();
622 EXPECT_EQ(wifi1, test_observer_->default_network()); 626 EXPECT_EQ(wifi1, test_observer_->default_network());
623 EXPECT_EQ(1u, test_observer_->default_network_change_count()); 627 EXPECT_EQ(1u, test_observer_->default_network_change_count());
624 628
625 // Change the state of the default network. 629 // Change the state of the default network. This should generate a
630 // default network notification.
626 test_observer_->reset_change_counts(); 631 test_observer_->reset_change_counts();
627 base::StringValue connection_state_ready_value(shill::kStateReady);
628 service_test_->SetServiceProperty(wifi1, shill::kStateProperty, 632 service_test_->SetServiceProperty(wifi1, shill::kStateProperty,
629 connection_state_ready_value); 633 base::StringValue(shill::kStateReady));
630 message_loop_.RunUntilIdle(); 634 message_loop_.RunUntilIdle();
631 EXPECT_EQ(shill::kStateReady, 635 EXPECT_EQ(shill::kStateReady,
632 test_observer_->default_network_connection_state()); 636 test_observer_->default_network_connection_state());
633 EXPECT_EQ(1u, test_observer_->default_network_change_count()); 637 EXPECT_EQ(1u, test_observer_->default_network_change_count());
634 638
635 // Updating a property on the default network should trigger 639 // Updating a property on the default network should also trigger
636 // a default network change. 640 // a default network change.
637 test_observer_->reset_change_counts(); 641 test_observer_->reset_change_counts();
638 DBusThreadManager::Get()->GetShillServiceClient()->SetProperty( 642 SetServiceProperty(wifi1, shill::kSecurityClassProperty,
639 dbus::ObjectPath(wifi1), 643 base::StringValue("TestSecurityClass"));
640 shill::kSecurityClassProperty, base::StringValue("TestSecurityClass"),
641 base::Bind(&base::DoNothing), base::Bind(&ErrorCallbackFunction));
642 message_loop_.RunUntilIdle(); 644 message_loop_.RunUntilIdle();
643 EXPECT_EQ(1u, test_observer_->default_network_change_count()); 645 EXPECT_EQ(1u, test_observer_->default_network_change_count());
644 646
645 // No default network updates for signal strength changes. 647 // No default network updates for signal strength changes.
646 test_observer_->reset_change_counts(); 648 test_observer_->reset_change_counts();
647 DBusThreadManager::Get()->GetShillServiceClient()->SetProperty( 649 SetServiceProperty(wifi1, shill::kSignalStrengthProperty,
648 dbus::ObjectPath(wifi1), 650 base::FundamentalValue(32));
649 shill::kSignalStrengthProperty, base::FundamentalValue(32),
650 base::Bind(&base::DoNothing), base::Bind(&ErrorCallbackFunction));
651 message_loop_.RunUntilIdle(); 651 message_loop_.RunUntilIdle();
652 EXPECT_EQ(0u, test_observer_->default_network_change_count()); 652 EXPECT_EQ(0u, test_observer_->default_network_change_count());
653
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());
663
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());
653 } 675 }
654 676
655 TEST_F(NetworkStateHandlerTest, RequestUpdate) { 677 TEST_F(NetworkStateHandlerTest, RequestUpdate) {
656 // Request an update for kShillManagerClientStubDefaultWifi. 678 // Request an update for kShillManagerClientStubDefaultWifi.
657 EXPECT_EQ(1, test_observer_->PropertyUpdatesForService( 679 EXPECT_EQ(1, test_observer_->PropertyUpdatesForService(
658 kShillManagerClientStubDefaultWifi)); 680 kShillManagerClientStubDefaultWifi));
659 network_state_handler_->RequestUpdateForNetwork( 681 network_state_handler_->RequestUpdateForNetwork(
660 kShillManagerClientStubDefaultWifi); 682 kShillManagerClientStubDefaultWifi);
661 message_loop_.RunUntilIdle(); 683 message_loop_.RunUntilIdle();
662 EXPECT_EQ(2, test_observer_->PropertyUpdatesForService( 684 EXPECT_EQ(2, test_observer_->PropertyUpdatesForService(
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
772 kShillManagerClientStubDefaultWifi, shill::kIPConfigProperty, 794 kShillManagerClientStubDefaultWifi, shill::kIPConfigProperty,
773 base::StringValue(kIPConfigPath)); 795 base::StringValue(kIPConfigPath));
774 UpdateManagerProperties(); 796 UpdateManagerProperties();
775 EXPECT_EQ(1, test_observer_->PropertyUpdatesForDevice( 797 EXPECT_EQ(1, test_observer_->PropertyUpdatesForDevice(
776 kShillManagerClientStubWifiDevice)); 798 kShillManagerClientStubWifiDevice));
777 EXPECT_EQ(1, test_observer_->PropertyUpdatesForService( 799 EXPECT_EQ(1, test_observer_->PropertyUpdatesForService(
778 kShillManagerClientStubDefaultWifi)); 800 kShillManagerClientStubDefaultWifi));
779 } 801 }
780 802
781 } // namespace chromeos 803 } // namespace chromeos
OLDNEW
« no previous file with comments | « chromeos/network/network_state_handler.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698