| 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.h" | 13 #include "base/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_manager_client.h" | 17 #include "chromeos/dbus/shill_manager_client.h" |
| 17 #include "chromeos/dbus/shill_service_client.h" | 18 #include "chromeos/dbus/shill_service_client.h" |
| 18 #include "chromeos/network/network_state.h" | 19 #include "chromeos/network/network_state.h" |
| 19 #include "chromeos/network/network_state_handler_observer.h" | 20 #include "chromeos/network/network_state_handler_observer.h" |
| 20 #include "dbus/object_path.h" | 21 #include "dbus/object_path.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
| 22 #include "third_party/cros_system_api/dbus/service_constants.h" | 23 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 23 | 24 |
| 24 namespace { | 25 namespace { |
| 25 | 26 |
| 26 void ErrorCallbackFunction(const std::string& error_name, | 27 void ErrorCallbackFunction(const std::string& error_name, |
| 27 const std::string& error_message) { | 28 const std::string& error_message) { |
| 28 LOG(ERROR) << "Shill Error: " << error_name << " : " << error_message; | 29 LOG(ERROR) << "Shill Error: " << error_name << " : " << error_message; |
| 29 } | 30 } |
| 30 | 31 |
| 32 const std::string kShillManagerClientStubDefaultService = "stub_ethernet"; |
| 33 const std::string kShillManagerClientStubDefaultWireless = "stub_wifi1"; |
| 34 const std::string kShillManagerClientStubWireless2 = "stub_wifi2"; |
| 35 const std::string kShillManagerClientStubCellular = "stub_cellular"; |
| 36 |
| 31 using chromeos::NetworkState; | 37 using chromeos::NetworkState; |
| 32 using chromeos::NetworkStateHandler; | 38 using chromeos::NetworkStateHandler; |
| 33 | 39 |
| 34 class TestObserver : public chromeos::NetworkStateHandlerObserver { | 40 class TestObserver : public chromeos::NetworkStateHandlerObserver { |
| 35 public: | 41 public: |
| 36 explicit TestObserver(NetworkStateHandler* handler) | 42 explicit TestObserver(NetworkStateHandler* handler) |
| 37 : handler_(handler), | 43 : handler_(handler), |
| 38 manager_changed_count_(0), | 44 manager_changed_count_(0), |
| 39 network_count_(0) { | 45 network_count_(0) { |
| 40 } | 46 } |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 DBusThreadManager::InitializeWithStub(); | 126 DBusThreadManager::InitializeWithStub(); |
| 121 } | 127 } |
| 122 | 128 |
| 123 virtual void TearDown() OVERRIDE { | 129 virtual void TearDown() OVERRIDE { |
| 124 network_state_handler_.reset(); | 130 network_state_handler_.reset(); |
| 125 test_observer_.reset(); | 131 test_observer_.reset(); |
| 126 DBusThreadManager::Shutdown(); | 132 DBusThreadManager::Shutdown(); |
| 127 } | 133 } |
| 128 | 134 |
| 129 void SetupNetworkStateHandler() { | 135 void SetupNetworkStateHandler() { |
| 136 SetupDefaultShillState(); |
| 130 network_state_handler_.reset(new NetworkStateHandler); | 137 network_state_handler_.reset(new NetworkStateHandler); |
| 131 test_observer_.reset(new TestObserver(network_state_handler_.get())); | 138 test_observer_.reset(new TestObserver(network_state_handler_.get())); |
| 132 network_state_handler_->AddObserver(test_observer_.get()); | 139 network_state_handler_->AddObserver(test_observer_.get()); |
| 133 network_state_handler_->InitShillPropertyHandler(); | 140 network_state_handler_->InitShillPropertyHandler(); |
| 134 } | 141 } |
| 135 | 142 |
| 136 protected: | 143 protected: |
| 144 void SetupDefaultShillState() { |
| 145 message_loop_.RunUntilIdle(); // Process any pending updates |
| 146 ShillDeviceClient::TestInterface* device_test = |
| 147 DBusThreadManager::Get()->GetShillDeviceClient()->GetTestInterface(); |
| 148 device_test->ClearDevices(); |
| 149 device_test->AddDevice("/device/stub_wifi_device1", |
| 150 flimflam::kTypeWifi, "stub_wifi_device1"); |
| 151 device_test->AddDevice("/device/stub_cellular_device1", |
| 152 flimflam::kTypeCellular, "stub_cellular_device1"); |
| 153 |
| 154 ShillServiceClient::TestInterface* service_test = |
| 155 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface(); |
| 156 service_test->ClearServices(); |
| 157 const bool add_to_watchlist = true; |
| 158 service_test->AddService(kShillManagerClientStubDefaultService, |
| 159 kShillManagerClientStubDefaultService, |
| 160 flimflam::kTypeEthernet, flimflam::kStateOnline, |
| 161 add_to_watchlist); |
| 162 service_test->AddService(kShillManagerClientStubDefaultWireless, |
| 163 kShillManagerClientStubDefaultWireless, |
| 164 flimflam::kTypeWifi, flimflam::kStateOnline, |
| 165 add_to_watchlist); |
| 166 service_test->AddService(kShillManagerClientStubWireless2, |
| 167 kShillManagerClientStubWireless2, |
| 168 flimflam::kTypeWifi, flimflam::kStateIdle, |
| 169 add_to_watchlist); |
| 170 service_test->AddService(kShillManagerClientStubCellular, |
| 171 kShillManagerClientStubCellular, |
| 172 flimflam::kTypeCellular, flimflam::kStateIdle, |
| 173 add_to_watchlist); |
| 174 } |
| 175 |
| 137 MessageLoopForUI message_loop_; | 176 MessageLoopForUI message_loop_; |
| 138 scoped_ptr<NetworkStateHandler> network_state_handler_; | 177 scoped_ptr<NetworkStateHandler> network_state_handler_; |
| 139 scoped_ptr<TestObserver> test_observer_; | 178 scoped_ptr<TestObserver> test_observer_; |
| 140 | 179 |
| 141 private: | 180 private: |
| 142 DISALLOW_COPY_AND_ASSIGN(NetworkStateHandlerTest); | 181 DISALLOW_COPY_AND_ASSIGN(NetworkStateHandlerTest); |
| 143 }; | 182 }; |
| 144 | 183 |
| 145 TEST_F(NetworkStateHandlerTest, NetworkStateHandlerStub) { | 184 TEST_F(NetworkStateHandlerTest, NetworkStateHandlerStub) { |
| 146 // This relies on the stub dbus implementations for ShillManagerClient, | |
| 147 SetupNetworkStateHandler(); | 185 SetupNetworkStateHandler(); |
| 148 message_loop_.RunUntilIdle(); | 186 message_loop_.RunUntilIdle(); |
| 149 EXPECT_EQ(1u, test_observer_->manager_changed_count()); | 187 EXPECT_EQ(1u, test_observer_->manager_changed_count()); |
| 150 // Ensure that the network list is the expected size. | 188 // Ensure that the network list is the expected size. |
| 151 const size_t kNumShillManagerClientStubImplServices = 4; | 189 const size_t kNumShillManagerClientStubImplServices = 4; |
| 152 EXPECT_EQ(kNumShillManagerClientStubImplServices, | 190 EXPECT_EQ(kNumShillManagerClientStubImplServices, |
| 153 test_observer_->network_count()); | 191 test_observer_->network_count()); |
| 154 // Ensure that the first stub network is the default network. | 192 // Ensure that the first stub network is the default network. |
| 155 const std::string kShillManagerClientStubDefaultService = "stub_ethernet"; | |
| 156 EXPECT_EQ(kShillManagerClientStubDefaultService, | 193 EXPECT_EQ(kShillManagerClientStubDefaultService, |
| 157 test_observer_->default_network()); | 194 test_observer_->default_network()); |
| 158 EXPECT_EQ(kShillManagerClientStubDefaultService, | 195 EXPECT_EQ(kShillManagerClientStubDefaultService, |
| 159 network_state_handler_->ConnectedNetworkByType( | 196 network_state_handler_->ConnectedNetworkByType( |
| 160 NetworkStateHandler::kMatchTypeDefault)->path()); | 197 NetworkStateHandler::kMatchTypeDefault)->path()); |
| 161 EXPECT_EQ(kShillManagerClientStubDefaultService, | 198 EXPECT_EQ(kShillManagerClientStubDefaultService, |
| 162 network_state_handler_->ConnectedNetworkByType( | 199 network_state_handler_->ConnectedNetworkByType( |
| 163 flimflam::kTypeEthernet)->path()); | 200 flimflam::kTypeEthernet)->path()); |
| 164 const std::string kShillManagerClientStubDefaultWireless = "stub_wifi1"; | |
| 165 EXPECT_EQ(kShillManagerClientStubDefaultWireless, | 201 EXPECT_EQ(kShillManagerClientStubDefaultWireless, |
| 166 network_state_handler_->ConnectedNetworkByType( | 202 network_state_handler_->ConnectedNetworkByType( |
| 167 NetworkStateHandler::kMatchTypeWireless)->path()); | 203 NetworkStateHandler::kMatchTypeWireless)->path()); |
| 168 EXPECT_EQ(flimflam::kStateOnline, | 204 EXPECT_EQ(flimflam::kStateOnline, |
| 169 test_observer_->default_network_connection_state()); | 205 test_observer_->default_network_connection_state()); |
| 170 } | 206 } |
| 171 | 207 |
| 172 TEST_F(NetworkStateHandlerTest, TechnologyChanged) { | 208 TEST_F(NetworkStateHandlerTest, TechnologyChanged) { |
| 173 // This relies on the stub dbus implementations for ShillManagerClient, | |
| 174 SetupNetworkStateHandler(); | 209 SetupNetworkStateHandler(); |
| 175 message_loop_.RunUntilIdle(); | 210 message_loop_.RunUntilIdle(); |
| 176 EXPECT_EQ(1u, test_observer_->manager_changed_count()); | 211 EXPECT_EQ(1u, test_observer_->manager_changed_count()); |
| 177 // Enable a technology. | 212 // Enable a technology. |
| 178 EXPECT_FALSE(network_state_handler_->TechnologyEnabled(flimflam::kTypeWimax)); | 213 EXPECT_FALSE(network_state_handler_->TechnologyEnabled(flimflam::kTypeWimax)); |
| 179 network_state_handler_->SetTechnologyEnabled( | 214 network_state_handler_->SetTechnologyEnabled( |
| 180 flimflam::kTypeWimax, true, network_handler::ErrorCallback()); | 215 flimflam::kTypeWimax, true, network_handler::ErrorCallback()); |
| 181 message_loop_.RunUntilIdle(); | 216 message_loop_.RunUntilIdle(); |
| 182 // Ensure we get a manager changed callback when we change a property. | 217 // Ensure we get a manager changed callback when we change a property. |
| 183 EXPECT_EQ(2u, test_observer_->manager_changed_count()); | 218 EXPECT_EQ(2u, test_observer_->manager_changed_count()); |
| 184 EXPECT_TRUE(network_state_handler_->TechnologyEnabled(flimflam::kTypeWimax)); | 219 EXPECT_TRUE(network_state_handler_->TechnologyEnabled(flimflam::kTypeWimax)); |
| 185 } | 220 } |
| 186 | 221 |
| 187 TEST_F(NetworkStateHandlerTest, ServicePropertyChanged) { | 222 TEST_F(NetworkStateHandlerTest, ServicePropertyChanged) { |
| 188 // This relies on the stub dbus implementations for ShillManagerClient, | |
| 189 SetupNetworkStateHandler(); | 223 SetupNetworkStateHandler(); |
| 190 message_loop_.RunUntilIdle(); | 224 message_loop_.RunUntilIdle(); |
| 191 // Set a service property. | 225 // Set a service property. |
| 192 const std::string eth0 = "stub_ethernet"; | 226 const std::string eth0 = "stub_ethernet"; |
| 193 EXPECT_EQ("", network_state_handler_->GetNetworkState(eth0)->security()); | 227 EXPECT_EQ("", network_state_handler_->GetNetworkState(eth0)->security()); |
| 194 EXPECT_EQ(1, test_observer_->PropertyUpdatesForService(eth0)); | 228 EXPECT_EQ(1, test_observer_->PropertyUpdatesForService(eth0)); |
| 195 base::StringValue security_value("TestSecurity"); | 229 base::StringValue security_value("TestSecurity"); |
| 196 DBusThreadManager::Get()->GetShillServiceClient()->SetProperty( | 230 DBusThreadManager::Get()->GetShillServiceClient()->SetProperty( |
| 197 dbus::ObjectPath(eth0), | 231 dbus::ObjectPath(eth0), |
| 198 flimflam::kSecurityProperty, security_value, | 232 flimflam::kSecurityProperty, security_value, |
| 199 base::Bind(&base::DoNothing), base::Bind(&ErrorCallbackFunction)); | 233 base::Bind(&base::DoNothing), base::Bind(&ErrorCallbackFunction)); |
| 200 message_loop_.RunUntilIdle(); | 234 message_loop_.RunUntilIdle(); |
| 201 EXPECT_EQ("TestSecurity", | 235 EXPECT_EQ("TestSecurity", |
| 202 network_state_handler_->GetNetworkState(eth0)->security()); | 236 network_state_handler_->GetNetworkState(eth0)->security()); |
| 203 EXPECT_EQ(2, test_observer_->PropertyUpdatesForService(eth0)); | 237 EXPECT_EQ(2, test_observer_->PropertyUpdatesForService(eth0)); |
| 204 } | 238 } |
| 205 | 239 |
| 206 TEST_F(NetworkStateHandlerTest, NetworkConnectionStateChanged) { | 240 TEST_F(NetworkStateHandlerTest, NetworkConnectionStateChanged) { |
| 207 // This relies on the stub dbus implementations for ShillManagerClient, | |
| 208 SetupNetworkStateHandler(); | 241 SetupNetworkStateHandler(); |
| 209 message_loop_.RunUntilIdle(); | 242 message_loop_.RunUntilIdle(); |
| 210 // Change a network state. | 243 // Change a network state. |
| 211 ShillServiceClient::TestInterface* service_test = | 244 ShillServiceClient::TestInterface* service_test = |
| 212 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface(); | 245 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface(); |
| 213 const std::string eth0 = "stub_ethernet"; | 246 const std::string eth0 = "stub_ethernet"; |
| 214 base::StringValue connection_state_idle_value(flimflam::kStateIdle); | 247 base::StringValue connection_state_idle_value(flimflam::kStateIdle); |
| 215 service_test->SetServiceProperty(eth0, flimflam::kStateProperty, | 248 service_test->SetServiceProperty(eth0, flimflam::kStateProperty, |
| 216 connection_state_idle_value); | 249 connection_state_idle_value); |
| 217 message_loop_.RunUntilIdle(); | 250 message_loop_.RunUntilIdle(); |
| 218 EXPECT_EQ(flimflam::kStateIdle, | 251 EXPECT_EQ(flimflam::kStateIdle, |
| 219 test_observer_->NetworkConnectionStateForService(eth0)); | 252 test_observer_->NetworkConnectionStateForService(eth0)); |
| 220 EXPECT_EQ(2, test_observer_->ConnectionStateChangesForService(eth0)); | 253 EXPECT_EQ(2, test_observer_->ConnectionStateChangesForService(eth0)); |
| 221 // Confirm that changing the connection state to the same value does *not* | 254 // Confirm that changing the connection state to the same value does *not* |
| 222 // signal the observer. | 255 // signal the observer. |
| 223 service_test->SetServiceProperty(eth0, flimflam::kStateProperty, | 256 service_test->SetServiceProperty(eth0, flimflam::kStateProperty, |
| 224 connection_state_idle_value); | 257 connection_state_idle_value); |
| 225 message_loop_.RunUntilIdle(); | 258 message_loop_.RunUntilIdle(); |
| 226 EXPECT_EQ(2, test_observer_->ConnectionStateChangesForService(eth0)); | 259 EXPECT_EQ(2, test_observer_->ConnectionStateChangesForService(eth0)); |
| 227 } | 260 } |
| 228 | 261 |
| 229 TEST_F(NetworkStateHandlerTest, DefaultServiceChanged) { | 262 TEST_F(NetworkStateHandlerTest, DefaultServiceChanged) { |
| 230 // This relies on the stub dbus implementations for ShillManagerClient, | |
| 231 SetupNetworkStateHandler(); | 263 SetupNetworkStateHandler(); |
| 232 message_loop_.RunUntilIdle(); | 264 message_loop_.RunUntilIdle(); |
| 233 | 265 |
| 234 ShillManagerClient::TestInterface* manager_test = | 266 ShillManagerClient::TestInterface* manager_test = |
| 235 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface(); | 267 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface(); |
| 236 ASSERT_TRUE(manager_test); | 268 ASSERT_TRUE(manager_test); |
| 237 ShillServiceClient::TestInterface* service_test = | 269 ShillServiceClient::TestInterface* service_test = |
| 238 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface(); | 270 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface(); |
| 239 ASSERT_TRUE(service_test); | 271 ASSERT_TRUE(service_test); |
| 240 | 272 |
| 241 // Change the default network by inserting wifi1 at the front of the list | 273 // Change the default network by inserting wifi1 at the front of the list |
| 242 // and changing the state of stub_ethernet to Idle. | 274 // and changing the state of stub_ethernet to Idle. |
| 243 const std::string wifi1 = "stub_wifi1"; | 275 const std::string wifi1 = "stub_wifi1"; |
| 244 manager_test->AddServiceAtIndex(wifi1, 0, true); | 276 manager_test->AddServiceAtIndex(wifi1, 0, true); |
| 245 const std::string eth0 = "stub_ethernet"; | 277 const std::string eth0 = "stub_ethernet"; |
| 246 base::StringValue connection_state_idle_value(flimflam::kStateIdle); | 278 base::StringValue connection_state_idle_value(flimflam::kStateIdle); |
| 247 service_test->SetServiceProperty(eth0, flimflam::kStateProperty, | 279 service_test->SetServiceProperty(eth0, flimflam::kStateProperty, |
| 248 connection_state_idle_value); | 280 connection_state_idle_value); |
| 249 message_loop_.RunUntilIdle(); | 281 message_loop_.RunUntilIdle(); |
| 250 EXPECT_EQ(wifi1, test_observer_->default_network()); | 282 EXPECT_EQ(wifi1, test_observer_->default_network()); |
| 251 EXPECT_EQ(flimflam::kStateOnline, | 283 EXPECT_EQ(flimflam::kStateOnline, |
| 252 test_observer_->default_network_connection_state()); | 284 test_observer_->default_network_connection_state()); |
| 253 } | 285 } |
| 254 | 286 |
| 255 } // namespace chromeos | 287 } // namespace chromeos |
| OLD | NEW |