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

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

Issue 395523004: Add DevicePropertiesUpdated to NetworkStateHandlerObserver (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Update for IPconfig(s) and add test Created 6 years, 5 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 | Annotate | Revision Log
« no previous file with comments | « chromeos/network/network_state_handler_observer.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
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_ipconfig_client.h"
17 #include "chromeos/dbus/shill_manager_client.h" 18 #include "chromeos/dbus/shill_manager_client.h"
18 #include "chromeos/dbus/shill_profile_client.h" 19 #include "chromeos/dbus/shill_profile_client.h"
19 #include "chromeos/dbus/shill_service_client.h" 20 #include "chromeos/dbus/shill_service_client.h"
21 #include "chromeos/network/device_state.h"
20 #include "chromeos/network/network_state.h" 22 #include "chromeos/network/network_state.h"
21 #include "chromeos/network/network_state_handler.h" 23 #include "chromeos/network/network_state_handler.h"
22 #include "chromeos/network/network_state_handler_observer.h" 24 #include "chromeos/network/network_state_handler_observer.h"
23 #include "dbus/object_path.h" 25 #include "dbus/object_path.h"
24 #include "testing/gtest/include/gtest/gtest.h" 26 #include "testing/gtest/include/gtest/gtest.h"
25 #include "third_party/cros_system_api/dbus/service_constants.h" 27 #include "third_party/cros_system_api/dbus/service_constants.h"
26 28
27 namespace { 29 namespace {
28 30
29 void ErrorCallbackFunction(const std::string& error_name, 31 void ErrorCallbackFunction(const std::string& error_name,
30 const std::string& error_message) { 32 const std::string& error_message) {
31 LOG(ERROR) << "Shill Error: " << error_name << " : " << error_message; 33 LOG(ERROR) << "Shill Error: " << error_name << " : " << error_message;
32 } 34 }
33 35
36 const std::string kShillManagerClientStubWifiDevice =
37 "/device/stub_wifi_device1";
38 const std::string kShillManagerClientStubCellularDevice =
39 "/device/stub_cellular_device1";
34 const std::string kShillManagerClientStubDefaultService = "/service/eth1"; 40 const std::string kShillManagerClientStubDefaultService = "/service/eth1";
35 const std::string kShillManagerClientStubDefaultWifi = "/service/wifi1"; 41 const std::string kShillManagerClientStubDefaultWifi = "/service/wifi1";
36 const std::string kShillManagerClientStubWifi2 = "/service/wifi2"; 42 const std::string kShillManagerClientStubWifi2 = "/service/wifi2";
37 const std::string kShillManagerClientStubCellular = "/service/cellular1"; 43 const std::string kShillManagerClientStubCellular = "/service/cellular1";
38 44
45 using chromeos::DeviceState;
39 using chromeos::NetworkState; 46 using chromeos::NetworkState;
40 using chromeos::NetworkStateHandler; 47 using chromeos::NetworkStateHandler;
41 48
42 class TestObserver : public chromeos::NetworkStateHandlerObserver { 49 class TestObserver : public chromeos::NetworkStateHandlerObserver {
43 public: 50 public:
44 explicit TestObserver(NetworkStateHandler* handler) 51 explicit TestObserver(NetworkStateHandler* handler)
45 : handler_(handler), 52 : handler_(handler),
46 device_list_changed_count_(0), 53 device_list_changed_count_(0),
47 device_count_(0), 54 device_count_(0),
48 network_list_changed_count_(0), 55 network_list_changed_count_(0),
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 const NetworkState* network) OVERRIDE { 95 const NetworkState* network) OVERRIDE {
89 network_connection_state_[network->path()] = network->connection_state(); 96 network_connection_state_[network->path()] = network->connection_state();
90 connection_state_changes_[network->path()]++; 97 connection_state_changes_[network->path()]++;
91 } 98 }
92 99
93 virtual void NetworkPropertiesUpdated(const NetworkState* network) OVERRIDE { 100 virtual void NetworkPropertiesUpdated(const NetworkState* network) OVERRIDE {
94 DCHECK(network); 101 DCHECK(network);
95 property_updates_[network->path()]++; 102 property_updates_[network->path()]++;
96 } 103 }
97 104
105 virtual void DevicePropertiesUpdated(const DeviceState* device) OVERRIDE {
106 DCHECK(device);
107 device_property_updates_[device->path()]++;
108 }
109
98 size_t device_list_changed_count() { return device_list_changed_count_; } 110 size_t device_list_changed_count() { return device_list_changed_count_; }
99 size_t device_count() { return device_count_; } 111 size_t device_count() { return device_count_; }
100 size_t network_list_changed_count() { return network_list_changed_count_; } 112 size_t network_list_changed_count() { return network_list_changed_count_; }
101 size_t network_count() { return network_count_; } 113 size_t network_count() { return network_count_; }
102 size_t default_network_change_count() { 114 size_t default_network_change_count() {
103 return default_network_change_count_; 115 return default_network_change_count_;
104 } 116 }
105 void reset_change_counts() { 117 void reset_change_counts() {
106 DVLOG(1) << "=== RESET CHANGE COUNTS ==="; 118 DVLOG(1) << "=== RESET CHANGE COUNTS ===";
107 default_network_change_count_ = 0; 119 default_network_change_count_ = 0;
108 device_list_changed_count_ = 0; 120 device_list_changed_count_ = 0;
109 network_list_changed_count_ = 0; 121 network_list_changed_count_ = 0;
122 connection_state_changes_.clear();
123 }
124 void reset_updates() {
125 property_updates_.clear();
126 device_property_updates_.clear();
110 } 127 }
111 std::string default_network() { return default_network_; } 128 std::string default_network() { return default_network_; }
112 std::string default_network_connection_state() { 129 std::string default_network_connection_state() {
113 return default_network_connection_state_; 130 return default_network_connection_state_;
114 } 131 }
115 132
116 int PropertyUpdatesForService(const std::string& service_path) { 133 int PropertyUpdatesForService(const std::string& service_path) {
117 return property_updates_[service_path]; 134 return property_updates_[service_path];
118 } 135 }
119 136
137 int PropertyUpdatesForDevice(const std::string& device_path) {
138 return device_property_updates_[device_path];
139 }
140
120 int ConnectionStateChangesForService(const std::string& service_path) { 141 int ConnectionStateChangesForService(const std::string& service_path) {
121 return connection_state_changes_[service_path]; 142 return connection_state_changes_[service_path];
122 } 143 }
123 144
124 std::string NetworkConnectionStateForService( 145 std::string NetworkConnectionStateForService(
125 const std::string& service_path) { 146 const std::string& service_path) {
126 return network_connection_state_[service_path]; 147 return network_connection_state_[service_path];
127 } 148 }
128 149
129 private: 150 private:
130 NetworkStateHandler* handler_; 151 NetworkStateHandler* handler_;
131 size_t device_list_changed_count_; 152 size_t device_list_changed_count_;
132 size_t device_count_; 153 size_t device_count_;
133 size_t network_list_changed_count_; 154 size_t network_list_changed_count_;
134 size_t network_count_; 155 size_t network_count_;
135 size_t default_network_change_count_; 156 size_t default_network_change_count_;
136 std::string default_network_; 157 std::string default_network_;
137 std::string default_network_connection_state_; 158 std::string default_network_connection_state_;
138 std::map<std::string, int> property_updates_; 159 std::map<std::string, int> property_updates_;
160 std::map<std::string, int> device_property_updates_;
139 std::map<std::string, int> connection_state_changes_; 161 std::map<std::string, int> connection_state_changes_;
140 std::map<std::string, std::string> network_connection_state_; 162 std::map<std::string, std::string> network_connection_state_;
141 163
142 DISALLOW_COPY_AND_ASSIGN(TestObserver); 164 DISALLOW_COPY_AND_ASSIGN(TestObserver);
143 }; 165 };
144 166
145 } // namespace 167 } // namespace
146 168
147 namespace chromeos { 169 namespace chromeos {
148 170
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
183 service_test_->AddService(service_path, guid, name, type, state, 205 service_test_->AddService(service_path, guid, name, type, state,
184 true /* add_to_visible */); 206 true /* add_to_visible */);
185 } 207 }
186 208
187 void SetupDefaultShillState() { 209 void SetupDefaultShillState() {
188 message_loop_.RunUntilIdle(); // Process any pending updates 210 message_loop_.RunUntilIdle(); // Process any pending updates
189 device_test_ = 211 device_test_ =
190 DBusThreadManager::Get()->GetShillDeviceClient()->GetTestInterface(); 212 DBusThreadManager::Get()->GetShillDeviceClient()->GetTestInterface();
191 ASSERT_TRUE(device_test_); 213 ASSERT_TRUE(device_test_);
192 device_test_->ClearDevices(); 214 device_test_->ClearDevices();
193 device_test_->AddDevice( 215 device_test_->AddDevice(kShillManagerClientStubWifiDevice,
194 "/device/stub_wifi_device1", shill::kTypeWifi, "stub_wifi_device1"); 216 shill::kTypeWifi, "stub_wifi_device1");
195 device_test_->AddDevice("/device/stub_cellular_device1", 217 device_test_->AddDevice(kShillManagerClientStubCellularDevice,
196 shill::kTypeCellular, 218 shill::kTypeCellular, "stub_cellular_device1");
197 "stub_cellular_device1");
198 219
199 manager_test_ = 220 manager_test_ =
200 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface(); 221 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface();
201 ASSERT_TRUE(manager_test_); 222 ASSERT_TRUE(manager_test_);
202 223
203 profile_test_ = 224 profile_test_ =
204 DBusThreadManager::Get()->GetShillProfileClient()->GetTestInterface(); 225 DBusThreadManager::Get()->GetShillProfileClient()->GetTestInterface();
205 ASSERT_TRUE(profile_test_); 226 ASSERT_TRUE(profile_test_);
206 profile_test_->ClearProfiles(); 227 profile_test_->ClearProfiles();
207 228
(...skipping 306 matching lines...) Expand 10 before | Expand all | Expand 10 after
514 network_state_handler_->GetNetworkStateFromGuid(wifi_network->guid()); 535 network_state_handler_->GetNetworkStateFromGuid(wifi_network->guid());
515 EXPECT_EQ(wifi_network, wifi_network_guid); 536 EXPECT_EQ(wifi_network, wifi_network_guid);
516 537
517 // Remove the service, verify that there is no longer a NetworkState for it. 538 // Remove the service, verify that there is no longer a NetworkState for it.
518 service_test_->RemoveService(wifi_path); 539 service_test_->RemoveService(wifi_path);
519 UpdateManagerProperties(); 540 UpdateManagerProperties();
520 EXPECT_FALSE(network_state_handler_->GetNetworkState(wifi_path)); 541 EXPECT_FALSE(network_state_handler_->GetNetworkState(wifi_path));
521 } 542 }
522 543
523 TEST_F(NetworkStateHandlerTest, NetworkConnectionStateChanged) { 544 TEST_F(NetworkStateHandlerTest, NetworkConnectionStateChanged) {
545 const std::string eth1 = kShillManagerClientStubDefaultService;
546 EXPECT_EQ(0, test_observer_->ConnectionStateChangesForService(eth1));
547
524 // Change a network state. 548 // Change a network state.
525 const std::string eth1 = kShillManagerClientStubDefaultService;
526 base::StringValue connection_state_idle_value(shill::kStateIdle); 549 base::StringValue connection_state_idle_value(shill::kStateIdle);
527 service_test_->SetServiceProperty(eth1, shill::kStateProperty, 550 service_test_->SetServiceProperty(eth1, shill::kStateProperty,
528 connection_state_idle_value); 551 connection_state_idle_value);
529 message_loop_.RunUntilIdle(); 552 message_loop_.RunUntilIdle();
530 EXPECT_EQ(shill::kStateIdle, 553 EXPECT_EQ(shill::kStateIdle,
531 test_observer_->NetworkConnectionStateForService(eth1)); 554 test_observer_->NetworkConnectionStateForService(eth1));
532 EXPECT_EQ(2, test_observer_->ConnectionStateChangesForService(eth1)); 555 EXPECT_EQ(1, test_observer_->ConnectionStateChangesForService(eth1));
pneubeck (no reviews) 2014/07/16 21:43:35 I'm confused, maybe because it's to late. Why did
stevenjb 2014/07/16 22:14:15 reset_change_counts() which gets called in SetUp()
533 // Confirm that changing the connection state to the same value does *not* 556 // Confirm that changing the connection state to the same value does *not*
534 // signal the observer. 557 // signal the observer.
535 service_test_->SetServiceProperty(eth1, shill::kStateProperty, 558 service_test_->SetServiceProperty(eth1, shill::kStateProperty,
536 connection_state_idle_value); 559 connection_state_idle_value);
537 message_loop_.RunUntilIdle(); 560 message_loop_.RunUntilIdle();
538 EXPECT_EQ(2, test_observer_->ConnectionStateChangesForService(eth1)); 561 EXPECT_EQ(1, test_observer_->ConnectionStateChangesForService(eth1));
539 } 562 }
540 563
541 TEST_F(NetworkStateHandlerTest, DefaultServiceDisconnected) { 564 TEST_F(NetworkStateHandlerTest, DefaultServiceDisconnected) {
542 const std::string eth1 = kShillManagerClientStubDefaultService; 565 const std::string eth1 = kShillManagerClientStubDefaultService;
543 const std::string wifi1 = kShillManagerClientStubDefaultWifi; 566 const std::string wifi1 = kShillManagerClientStubDefaultWifi;
544 567
545 EXPECT_EQ(0u, test_observer_->default_network_change_count()); 568 EXPECT_EQ(0u, test_observer_->default_network_change_count());
546 // Disconnect ethernet. 569 // Disconnect ethernet.
547 base::StringValue connection_state_idle_value(shill::kStateIdle); 570 base::StringValue connection_state_idle_value(shill::kStateIdle);
548 service_test_->SetServiceProperty(eth1, shill::kStateProperty, 571 service_test_->SetServiceProperty(eth1, shill::kStateProperty,
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after
706 // the NetworkState was created with the same GUID. 729 // the NetworkState was created with the same GUID.
707 AddService(wifi_path, "" /* guid */, wifi_name, 730 AddService(wifi_path, "" /* guid */, wifi_name,
708 shill::kTypeWifi, shill::kStateOnline); 731 shill::kTypeWifi, shill::kStateOnline);
709 UpdateManagerProperties(); 732 UpdateManagerProperties();
710 network = network_state_handler_->GetNetworkStateFromServicePath( 733 network = network_state_handler_->GetNetworkStateFromServicePath(
711 wifi_path, is_service_configured); 734 wifi_path, is_service_configured);
712 ASSERT_TRUE(network); 735 ASSERT_TRUE(network);
713 EXPECT_EQ(wifi_guid, network->guid()); 736 EXPECT_EQ(wifi_guid, network->guid());
714 } 737 }
715 738
739 TEST_F(NetworkStateHandlerTest, DeviceListChanged) {
740 size_t stub_device_count = test_observer_->device_count();
741 // Add an additional device.
742 const std::string wifi_device = "/service/stub_wifi_device2";
743 device_test_->AddDevice(wifi_device, shill::kTypeWifi, "stub_wifi_device2");
744 UpdateManagerProperties();
745 // Expect a device list update.
746 EXPECT_EQ(stub_device_count + 1, test_observer_->device_count());
747 EXPECT_EQ(0, test_observer_->PropertyUpdatesForDevice(wifi_device));
748 // Change a device property.
749 device_test_->SetDeviceProperty(
750 wifi_device, shill::kScanningProperty, base::FundamentalValue(true));
751 UpdateManagerProperties();
752 EXPECT_EQ(1, test_observer_->PropertyUpdatesForDevice(wifi_device));
753 }
754
755 TEST_F(NetworkStateHandlerTest, IPConfigChanged) {
756 test_observer_->reset_updates();
757 EXPECT_EQ(0, test_observer_->PropertyUpdatesForDevice(
758 kShillManagerClientStubWifiDevice));
759 EXPECT_EQ(0, test_observer_->PropertyUpdatesForService(
760 kShillManagerClientStubDefaultWifi));
761
762 // Change IPConfigs property.
763 ShillIPConfigClient::TestInterface* ip_config_test =
764 DBusThreadManager::Get()->GetShillIPConfigClient()->GetTestInterface();
765 const std::string kIPConfigPath = "test_ip_config";
766 base::DictionaryValue ip_config_properties;
767 ip_config_test->AddIPConfig(kIPConfigPath, ip_config_properties);
768 base::ListValue device_ip_configs;
769 device_ip_configs.Append(new base::StringValue(kIPConfigPath));
770 device_test_->SetDeviceProperty(
771 kShillManagerClientStubWifiDevice, shill::kIPConfigsProperty,
772 device_ip_configs);
773 service_test_->SetServiceProperty(
774 kShillManagerClientStubDefaultWifi, shill::kIPConfigProperty,
775 base::StringValue(kIPConfigPath));
776 UpdateManagerProperties();
777 EXPECT_EQ(1, test_observer_->PropertyUpdatesForDevice(
778 kShillManagerClientStubWifiDevice));
779 EXPECT_EQ(1, test_observer_->PropertyUpdatesForService(
780 kShillManagerClientStubDefaultWifi));
781 }
782
716 } // namespace chromeos 783 } // namespace chromeos
OLDNEW
« no previous file with comments | « chromeos/network/network_state_handler_observer.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698