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 |
(...skipping 11 matching lines...) Expand all Loading... |
22 #include "third_party/cros_system_api/dbus/service_constants.h" | 22 #include "third_party/cros_system_api/dbus/service_constants.h" |
23 | 23 |
24 namespace { | 24 namespace { |
25 | 25 |
26 void ErrorCallbackFunction(const std::string& error_name, | 26 void ErrorCallbackFunction(const std::string& error_name, |
27 const std::string& error_message) { | 27 const std::string& error_message) { |
28 LOG(ERROR) << "Shill Error: " << error_name << " : " << error_message; | 28 LOG(ERROR) << "Shill Error: " << error_name << " : " << error_message; |
29 } | 29 } |
30 | 30 |
31 using chromeos::NetworkState; | 31 using chromeos::NetworkState; |
| 32 using chromeos::NetworkStateHandler; |
32 | 33 |
33 class TestObserver : public chromeos::NetworkStateHandlerObserver { | 34 class TestObserver : public chromeos::NetworkStateHandlerObserver { |
34 public: | 35 public: |
35 TestObserver() | 36 explicit TestObserver(NetworkStateHandler* handler) |
36 : manager_changed_count_(0), | 37 : handler_(handler), |
| 38 manager_changed_count_(0), |
37 network_count_(0) { | 39 network_count_(0) { |
38 } | 40 } |
39 | 41 |
40 virtual ~TestObserver() { | 42 virtual ~TestObserver() { |
41 } | 43 } |
42 | 44 |
43 virtual void NetworkManagerChanged() { | 45 virtual void NetworkManagerChanged() OVERRIDE { |
44 ++manager_changed_count_; | 46 ++manager_changed_count_; |
45 } | 47 } |
46 | 48 |
47 virtual void NetworkListChanged( | 49 virtual void NetworkListChanged( |
48 const chromeos::NetworkStateHandler::NetworkStateList& networks) { | 50 const NetworkStateHandler::NetworkStateList& networks) OVERRIDE { |
49 network_count_ = networks.size(); | 51 network_count_ = networks.size(); |
| 52 if (network_count_ == 0) { |
| 53 default_network_ = ""; |
| 54 default_network_connection_state_ = ""; |
| 55 } |
50 } | 56 } |
51 | 57 |
52 virtual void ActiveNetworkChanged(const NetworkState* network) { | 58 virtual void DefaultNetworkChanged(const NetworkState* network) OVERRIDE { |
53 active_network_ = network ? network->path() : ""; | 59 default_network_ = network ? network->path() : ""; |
54 active_network_state_ = network ? network->state() : ""; | 60 default_network_connection_state_ = |
| 61 network ? network->connection_state() : ""; |
55 } | 62 } |
56 | 63 |
57 virtual void ActiveNetworkStateChanged(const NetworkState* network) { | 64 virtual void NetworkConnectionStateChanged( |
58 active_network_state_ = network ? network->state() : ""; | 65 const NetworkState* network) OVERRIDE { |
| 66 network_connection_state_[network->path()] = network->connection_state(); |
| 67 connection_state_changes_[network->path()]++; |
59 } | 68 } |
60 | 69 |
61 virtual void NetworkServiceChanged(const NetworkState* network) { | 70 virtual void NetworkPropertiesUpdated(const NetworkState* network) OVERRIDE { |
62 DCHECK(network); | 71 DCHECK(network); |
63 std::map<std::string, int>::iterator iter = | 72 property_updates_[network->path()]++; |
64 property_changes_.find(network->path()); | |
65 if (iter == property_changes_.end()) | |
66 property_changes_[network->path()] = 1; | |
67 else | |
68 iter->second++; | |
69 } | 73 } |
70 | 74 |
71 size_t manager_changed_count() { return manager_changed_count_; } | 75 size_t manager_changed_count() { return manager_changed_count_; } |
72 size_t network_count() { return network_count_; } | 76 size_t network_count() { return network_count_; } |
73 std::string active_network() { return active_network_; } | 77 std::string default_network() { return default_network_; } |
74 std::string active_network_state() { return active_network_state_; } | 78 std::string default_network_connection_state() { |
| 79 return default_network_connection_state_; |
| 80 } |
75 | 81 |
76 int PropertyChangesForService(const std::string& service_path) { | 82 int PropertyUpdatesForService(const std::string& service_path) { |
77 std::map<std::string, int>::iterator iter = | 83 return property_updates_[service_path]; |
78 property_changes_.find(service_path); | 84 } |
79 if (iter == property_changes_.end()) | 85 |
80 return 0; | 86 int ConnectionStateChangesForService(const std::string& service_path) { |
81 return iter->second; | 87 return connection_state_changes_[service_path]; |
| 88 } |
| 89 |
| 90 std::string NetworkConnectionStateForService( |
| 91 const std::string& service_path) { |
| 92 return network_connection_state_[service_path]; |
82 } | 93 } |
83 | 94 |
84 private: | 95 private: |
| 96 NetworkStateHandler* handler_; |
85 size_t manager_changed_count_; | 97 size_t manager_changed_count_; |
86 size_t network_count_; | 98 size_t network_count_; |
87 std::string active_network_; | 99 std::string default_network_; |
88 std::string active_network_state_; | 100 std::string default_network_connection_state_; |
89 std::map<std::string, int> property_changes_; | 101 std::map<std::string, int> property_updates_; |
| 102 std::map<std::string, int> connection_state_changes_; |
| 103 std::map<std::string, std::string> network_connection_state_; |
90 | 104 |
91 DISALLOW_COPY_AND_ASSIGN(TestObserver); | 105 DISALLOW_COPY_AND_ASSIGN(TestObserver); |
92 }; | 106 }; |
93 | 107 |
94 } // namespace | 108 } // namespace |
95 | 109 |
96 namespace chromeos { | 110 namespace chromeos { |
97 | 111 |
98 class NetworkStateHandlerTest : public testing::Test { | 112 class NetworkStateHandlerTest : public testing::Test { |
99 public: | 113 public: |
100 NetworkStateHandlerTest() {} | 114 NetworkStateHandlerTest() {} |
101 virtual ~NetworkStateHandlerTest() {} | 115 virtual ~NetworkStateHandlerTest() {} |
102 | 116 |
103 virtual void SetUp() OVERRIDE { | 117 virtual void SetUp() OVERRIDE { |
104 // Initialize DBusThreadManager with a stub implementation. | 118 // Initialize DBusThreadManager with a stub implementation. |
105 DBusThreadManager::InitializeWithStub(); | 119 DBusThreadManager::InitializeWithStub(); |
106 } | 120 } |
107 | 121 |
108 virtual void TearDown() OVERRIDE { | 122 virtual void TearDown() OVERRIDE { |
109 network_state_handler_.reset(); | 123 network_state_handler_.reset(); |
110 test_observer_.reset(); | 124 test_observer_.reset(); |
111 DBusThreadManager::Shutdown(); | 125 DBusThreadManager::Shutdown(); |
112 } | 126 } |
113 | 127 |
114 void SetupNetworkStateHandler() { | 128 void SetupNetworkStateHandler() { |
115 test_observer_.reset(new TestObserver); | |
116 network_state_handler_.reset(new NetworkStateHandler); | 129 network_state_handler_.reset(new NetworkStateHandler); |
| 130 test_observer_.reset(new TestObserver(network_state_handler_.get())); |
117 network_state_handler_->AddObserver(test_observer_.get()); | 131 network_state_handler_->AddObserver(test_observer_.get()); |
118 network_state_handler_->InitShillPropertyHandler(); | 132 network_state_handler_->InitShillPropertyHandler(); |
119 } | 133 } |
120 | 134 |
121 protected: | 135 protected: |
122 MessageLoopForUI message_loop_; | 136 MessageLoopForUI message_loop_; |
123 scoped_ptr<NetworkStateHandler> network_state_handler_; | 137 scoped_ptr<NetworkStateHandler> network_state_handler_; |
124 scoped_ptr<TestObserver> test_observer_; | 138 scoped_ptr<TestObserver> test_observer_; |
125 | 139 |
126 private: | 140 private: |
127 DISALLOW_COPY_AND_ASSIGN(NetworkStateHandlerTest); | 141 DISALLOW_COPY_AND_ASSIGN(NetworkStateHandlerTest); |
128 }; | 142 }; |
129 | 143 |
130 TEST_F(NetworkStateHandlerTest, NetworkStateHandlerStub) { | 144 TEST_F(NetworkStateHandlerTest, NetworkStateHandlerStub) { |
131 // This relies on the stub dbus implementations for ShillManagerClient, | 145 // This relies on the stub dbus implementations for ShillManagerClient, |
132 SetupNetworkStateHandler(); | 146 SetupNetworkStateHandler(); |
133 message_loop_.RunUntilIdle(); | 147 message_loop_.RunUntilIdle(); |
134 EXPECT_EQ(1u, test_observer_->manager_changed_count()); | 148 EXPECT_EQ(1u, test_observer_->manager_changed_count()); |
135 // Ensure that the network list is the expected size. | 149 // Ensure that the network list is the expected size. |
136 const size_t kNumShillManagerClientStubImplServices = 4; | 150 const size_t kNumShillManagerClientStubImplServices = 4; |
137 EXPECT_EQ(kNumShillManagerClientStubImplServices, | 151 EXPECT_EQ(kNumShillManagerClientStubImplServices, |
138 test_observer_->network_count()); | 152 test_observer_->network_count()); |
139 // Ensure that the first stub network is the active network. | 153 // Ensure that the first stub network is the default network. |
140 const std::string kShillManagerClientStubActiveService = "stub_ethernet"; | 154 const std::string kShillManagerClientStubDefaultService = "stub_ethernet"; |
141 EXPECT_EQ(kShillManagerClientStubActiveService, | 155 EXPECT_EQ(kShillManagerClientStubDefaultService, |
142 test_observer_->active_network()); | 156 test_observer_->default_network()); |
143 EXPECT_EQ(kShillManagerClientStubActiveService, | 157 EXPECT_EQ(kShillManagerClientStubDefaultService, |
144 network_state_handler_->ActiveNetwork()->path()); | 158 network_state_handler_->ConnectedNetworkByType( |
145 EXPECT_EQ(kShillManagerClientStubActiveService, | 159 NetworkStateHandler::kMatchTypeDefault)->path()); |
| 160 EXPECT_EQ(kShillManagerClientStubDefaultService, |
146 network_state_handler_->ConnectedNetworkByType( | 161 network_state_handler_->ConnectedNetworkByType( |
147 flimflam::kTypeEthernet)->path()); | 162 flimflam::kTypeEthernet)->path()); |
148 EXPECT_EQ(flimflam::kStateOnline, test_observer_->active_network_state()); | 163 const std::string kShillManagerClientStubDefaultWireless = "stub_wifi1"; |
| 164 EXPECT_EQ(kShillManagerClientStubDefaultWireless, |
| 165 network_state_handler_->ConnectedNetworkByType( |
| 166 NetworkStateHandler::kMatchTypeWireless)->path()); |
| 167 EXPECT_EQ(flimflam::kStateOnline, |
| 168 test_observer_->default_network_connection_state()); |
149 } | 169 } |
150 | 170 |
151 TEST_F(NetworkStateHandlerTest, NetworkStateHandlerTechnologyChanged) { | 171 TEST_F(NetworkStateHandlerTest, TechnologyChanged) { |
152 // This relies on the stub dbus implementations for ShillManagerClient, | 172 // This relies on the stub dbus implementations for ShillManagerClient, |
153 SetupNetworkStateHandler(); | 173 SetupNetworkStateHandler(); |
154 message_loop_.RunUntilIdle(); | 174 message_loop_.RunUntilIdle(); |
155 EXPECT_EQ(1u, test_observer_->manager_changed_count()); | 175 EXPECT_EQ(1u, test_observer_->manager_changed_count()); |
156 // Enable a technology. | 176 // Enable a technology. |
157 EXPECT_FALSE(network_state_handler_->TechnologyEnabled(flimflam::kTypeWimax)); | 177 EXPECT_FALSE(network_state_handler_->TechnologyEnabled(flimflam::kTypeWimax)); |
158 network_state_handler_->SetTechnologyEnabled( | 178 network_state_handler_->SetTechnologyEnabled( |
159 flimflam::kTypeWimax, true, network_handler::ErrorCallback()); | 179 flimflam::kTypeWimax, true, network_handler::ErrorCallback()); |
160 message_loop_.RunUntilIdle(); | 180 message_loop_.RunUntilIdle(); |
161 // Ensure we get a manager changed callback when we change a property. | 181 // Ensure we get a manager changed callback when we change a property. |
162 EXPECT_EQ(2u, test_observer_->manager_changed_count()); | 182 EXPECT_EQ(2u, test_observer_->manager_changed_count()); |
163 EXPECT_TRUE(network_state_handler_->TechnologyEnabled(flimflam::kTypeWimax)); | 183 EXPECT_TRUE(network_state_handler_->TechnologyEnabled(flimflam::kTypeWimax)); |
164 } | 184 } |
165 | 185 |
166 TEST_F(NetworkStateHandlerTest, NetworkStateHandlerServicePropertyChanged) { | 186 TEST_F(NetworkStateHandlerTest, ServicePropertyChanged) { |
167 // This relies on the stub dbus implementations for ShillManagerClient, | 187 // This relies on the stub dbus implementations for ShillManagerClient, |
168 SetupNetworkStateHandler(); | 188 SetupNetworkStateHandler(); |
169 message_loop_.RunUntilIdle(); | 189 message_loop_.RunUntilIdle(); |
170 // Set a service property. | 190 // Set a service property. |
171 const std::string eth0 = "stub_ethernet"; | 191 const std::string eth0 = "stub_ethernet"; |
172 EXPECT_EQ("", network_state_handler_->GetNetworkState(eth0)->security()); | 192 EXPECT_EQ("", network_state_handler_->GetNetworkState(eth0)->security()); |
173 EXPECT_EQ(1, test_observer_->PropertyChangesForService(eth0)); | 193 EXPECT_EQ(1, test_observer_->PropertyUpdatesForService(eth0)); |
174 base::StringValue security_value("TestSecurity"); | 194 base::StringValue security_value("TestSecurity"); |
175 DBusThreadManager::Get()->GetShillServiceClient()->SetProperty( | 195 DBusThreadManager::Get()->GetShillServiceClient()->SetProperty( |
176 dbus::ObjectPath(eth0), | 196 dbus::ObjectPath(eth0), |
177 flimflam::kSecurityProperty, security_value, | 197 flimflam::kSecurityProperty, security_value, |
178 base::Bind(&base::DoNothing), base::Bind(&ErrorCallbackFunction)); | 198 base::Bind(&base::DoNothing), base::Bind(&ErrorCallbackFunction)); |
179 message_loop_.RunUntilIdle(); | 199 message_loop_.RunUntilIdle(); |
180 EXPECT_EQ("TestSecurity", | 200 EXPECT_EQ("TestSecurity", |
181 network_state_handler_->GetNetworkState(eth0)->security()); | 201 network_state_handler_->GetNetworkState(eth0)->security()); |
182 EXPECT_EQ(2, test_observer_->PropertyChangesForService(eth0)); | 202 EXPECT_EQ(2, test_observer_->PropertyUpdatesForService(eth0)); |
183 } | 203 } |
184 | 204 |
185 TEST_F(NetworkStateHandlerTest, NetworkStateHandlerActiveServiceChanged) { | 205 TEST_F(NetworkStateHandlerTest, NetworkConnectionStateChanged) { |
| 206 // This relies on the stub dbus implementations for ShillManagerClient, |
| 207 SetupNetworkStateHandler(); |
| 208 message_loop_.RunUntilIdle(); |
| 209 // Change a network state. |
| 210 ShillServiceClient::TestInterface* service_test = |
| 211 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface(); |
| 212 const std::string eth0 = "stub_ethernet"; |
| 213 base::StringValue connection_state_idle_value(flimflam::kStateIdle); |
| 214 service_test->SetServiceProperty(eth0, flimflam::kStateProperty, |
| 215 connection_state_idle_value); |
| 216 message_loop_.RunUntilIdle(); |
| 217 EXPECT_EQ(flimflam::kStateIdle, |
| 218 test_observer_->NetworkConnectionStateForService(eth0)); |
| 219 EXPECT_EQ(2, test_observer_->ConnectionStateChangesForService(eth0)); |
| 220 // Confirm that changing the connection state to the same value does *not* |
| 221 // signal the observer. |
| 222 service_test->SetServiceProperty(eth0, flimflam::kStateProperty, |
| 223 connection_state_idle_value); |
| 224 message_loop_.RunUntilIdle(); |
| 225 EXPECT_EQ(2, test_observer_->ConnectionStateChangesForService(eth0)); |
| 226 } |
| 227 |
| 228 TEST_F(NetworkStateHandlerTest, DefaultServiceChanged) { |
186 // This relies on the stub dbus implementations for ShillManagerClient, | 229 // This relies on the stub dbus implementations for ShillManagerClient, |
187 SetupNetworkStateHandler(); | 230 SetupNetworkStateHandler(); |
188 message_loop_.RunUntilIdle(); | 231 message_loop_.RunUntilIdle(); |
189 | 232 |
190 // Change the active network by inserting wifi1 at the front of the list. | |
191 ShillManagerClient::TestInterface* manager_test = | 233 ShillManagerClient::TestInterface* manager_test = |
192 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface(); | 234 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface(); |
193 ASSERT_TRUE(manager_test); | 235 ASSERT_TRUE(manager_test); |
194 const std::string wifi1 = "stub_wifi1"; | |
195 manager_test->AddServiceAtIndex(wifi1, 0, true); | |
196 message_loop_.RunUntilIdle(); | |
197 EXPECT_EQ(wifi1, test_observer_->active_network()); | |
198 EXPECT_EQ(flimflam::kStateOnline, test_observer_->active_network_state()); | |
199 | |
200 // Change the state of wifi1, ensure that triggers the active state changed | |
201 // observer. | |
202 ShillServiceClient::TestInterface* service_test = | 236 ShillServiceClient::TestInterface* service_test = |
203 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface(); | 237 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface(); |
204 ASSERT_TRUE(service_test); | 238 ASSERT_TRUE(service_test); |
205 base::StringValue state_value(flimflam::kStateConfiguration); | 239 |
206 service_test->SetServiceProperty(wifi1, flimflam::kStateProperty, | 240 // Change the default network by inserting wifi1 at the front of the list |
207 state_value); | 241 // and changing the state of stub_ethernet to Idle. |
| 242 const std::string wifi1 = "stub_wifi1"; |
| 243 manager_test->AddServiceAtIndex(wifi1, 0, true); |
| 244 const std::string eth0 = "stub_ethernet"; |
| 245 base::StringValue connection_state_idle_value(flimflam::kStateIdle); |
| 246 service_test->SetServiceProperty(eth0, flimflam::kStateProperty, |
| 247 connection_state_idle_value); |
208 message_loop_.RunUntilIdle(); | 248 message_loop_.RunUntilIdle(); |
209 EXPECT_EQ(wifi1, test_observer_->active_network()); | 249 EXPECT_EQ(wifi1, test_observer_->default_network()); |
210 EXPECT_EQ(flimflam::kStateConfiguration, | 250 EXPECT_EQ(flimflam::kStateOnline, |
211 test_observer_->active_network_state()); | 251 test_observer_->default_network_connection_state()); |
212 } | 252 } |
213 | 253 |
214 } // namespace chromeos | 254 } // namespace chromeos |
OLD | NEW |