OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chromeos/network/network_state_handler.h" |
| 6 |
| 7 #include <map> |
| 8 #include <set> |
| 9 #include <string> |
| 10 |
| 11 #include "base/bind.h" |
| 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/message_loop.h" |
| 14 #include "base/values.h" |
| 15 #include "chromeos/dbus/dbus_thread_manager.h" |
| 16 #include "chromeos/dbus/shill_manager_client.h" |
| 17 #include "chromeos/dbus/shill_service_client.h" |
| 18 #include "chromeos/network/network_state.h" |
| 19 #include "chromeos/network/network_state_handler_observer.h" |
| 20 #include "dbus/object_path.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" |
| 22 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 23 |
| 24 namespace { |
| 25 |
| 26 void ErrorCallbackFunction(const std::string& error_name, |
| 27 const std::string& error_message) { |
| 28 LOG(ERROR) << "Shill Error: " << error_name << " : " << error_message; |
| 29 } |
| 30 |
| 31 using chromeos::NetworkState; |
| 32 |
| 33 class TestObserver : public chromeos::NetworkStateHandlerObserver { |
| 34 public: |
| 35 TestObserver() |
| 36 : manager_changed_count_(0), |
| 37 network_count_(0) { |
| 38 } |
| 39 |
| 40 virtual ~TestObserver() { |
| 41 } |
| 42 |
| 43 virtual void NetworkManagerChanged() { |
| 44 ++manager_changed_count_; |
| 45 } |
| 46 |
| 47 virtual void NetworkListChanged( |
| 48 const chromeos::NetworkStateHandler::NetworkStateList& networks) { |
| 49 network_count_ = networks.size(); |
| 50 } |
| 51 |
| 52 virtual void ActiveNetworkChanged(const NetworkState* network) { |
| 53 active_network_ = network ? network->path() : ""; |
| 54 active_network_state_ = network ? network->state() : ""; |
| 55 } |
| 56 |
| 57 virtual void ActiveNetworkStateChanged(const NetworkState* network) { |
| 58 active_network_state_ = network ? network->state() : ""; |
| 59 } |
| 60 |
| 61 virtual void NetworkServiceChanged(const NetworkState* network) { |
| 62 DCHECK(network); |
| 63 std::map<std::string, int>::iterator iter = |
| 64 property_changes_.find(network->path()); |
| 65 if (iter == property_changes_.end()) |
| 66 property_changes_[network->path()] = 1; |
| 67 else |
| 68 iter->second++; |
| 69 } |
| 70 |
| 71 size_t manager_changed_count() { return manager_changed_count_; } |
| 72 size_t network_count() { return network_count_; } |
| 73 std::string active_network() { return active_network_; } |
| 74 std::string active_network_state() { return active_network_state_; } |
| 75 |
| 76 int PropertyChangesForService(const std::string& service_path) { |
| 77 std::map<std::string, int>::iterator iter = |
| 78 property_changes_.find(service_path); |
| 79 if (iter == property_changes_.end()) |
| 80 return 0; |
| 81 return iter->second; |
| 82 } |
| 83 |
| 84 private: |
| 85 size_t manager_changed_count_; |
| 86 size_t network_count_; |
| 87 std::string active_network_; |
| 88 std::string active_network_state_; |
| 89 std::map<std::string, int> property_changes_; |
| 90 |
| 91 DISALLOW_COPY_AND_ASSIGN(TestObserver); |
| 92 }; |
| 93 |
| 94 } // namespace |
| 95 |
| 96 namespace chromeos { |
| 97 |
| 98 class NetworkStateHandlerTest : public testing::Test { |
| 99 public: |
| 100 NetworkStateHandlerTest() {} |
| 101 virtual ~NetworkStateHandlerTest() {} |
| 102 |
| 103 virtual void SetUp() OVERRIDE { |
| 104 // Initialize DBusThreadManager with a stub implementation. |
| 105 DBusThreadManager::InitializeWithStub(); |
| 106 } |
| 107 |
| 108 virtual void TearDown() OVERRIDE { |
| 109 network_state_handler_.reset(); |
| 110 test_observer_.reset(); |
| 111 DBusThreadManager::Shutdown(); |
| 112 } |
| 113 |
| 114 void SetupNetworkStateHandler() { |
| 115 test_observer_.reset(new TestObserver); |
| 116 network_state_handler_.reset(new NetworkStateHandler); |
| 117 network_state_handler_->AddObserver(test_observer_.get()); |
| 118 network_state_handler_->Init(); |
| 119 } |
| 120 |
| 121 protected: |
| 122 MessageLoopForUI message_loop_; |
| 123 scoped_ptr<NetworkStateHandler> network_state_handler_; |
| 124 scoped_ptr<TestObserver> test_observer_; |
| 125 |
| 126 private: |
| 127 DISALLOW_COPY_AND_ASSIGN(NetworkStateHandlerTest); |
| 128 }; |
| 129 |
| 130 TEST_F(NetworkStateHandlerTest, NetworkStateHandlerStub) { |
| 131 // This relies on the stub dbus implementations for ShillManagerClient, |
| 132 SetupNetworkStateHandler(); |
| 133 message_loop_.RunUntilIdle(); |
| 134 EXPECT_EQ(1u, test_observer_->manager_changed_count()); |
| 135 // Ensure that the network list is the expected size. |
| 136 const size_t kNumShillManagerClientStubImplServices = 4; |
| 137 EXPECT_EQ(kNumShillManagerClientStubImplServices, |
| 138 test_observer_->network_count()); |
| 139 // Ensure that the first stub network is the active network. |
| 140 const std::string kShillManagerClientStubActiveService = "stub_ethernet"; |
| 141 EXPECT_EQ(kShillManagerClientStubActiveService, |
| 142 test_observer_->active_network()); |
| 143 EXPECT_EQ(kShillManagerClientStubActiveService, |
| 144 network_state_handler_->ActiveNetwork()->path()); |
| 145 EXPECT_EQ(kShillManagerClientStubActiveService, |
| 146 network_state_handler_->ConnectedNetworkByType( |
| 147 flimflam::kTypeEthernet)->path()); |
| 148 EXPECT_EQ(flimflam::kStateOnline, test_observer_->active_network_state()); |
| 149 } |
| 150 |
| 151 TEST_F(NetworkStateHandlerTest, NetworkStateHandlerTechnologyChanged) { |
| 152 // This relies on the stub dbus implementations for ShillManagerClient, |
| 153 SetupNetworkStateHandler(); |
| 154 message_loop_.RunUntilIdle(); |
| 155 EXPECT_EQ(1u, test_observer_->manager_changed_count()); |
| 156 // Enable a technology. |
| 157 EXPECT_FALSE(network_state_handler_->TechnologyEnabled(flimflam::kTypeWimax)); |
| 158 network_state_handler_->SetTechnologyEnabled(flimflam::kTypeWimax, true); |
| 159 message_loop_.RunUntilIdle(); |
| 160 // Ensure we get a manager changed callback when we change a property. |
| 161 EXPECT_EQ(2u, test_observer_->manager_changed_count()); |
| 162 EXPECT_TRUE(network_state_handler_->TechnologyEnabled(flimflam::kTypeWimax)); |
| 163 } |
| 164 |
| 165 TEST_F(NetworkStateHandlerTest, NetworkStateHandlerServicePropertyChanged) { |
| 166 // This relies on the stub dbus implementations for ShillManagerClient, |
| 167 SetupNetworkStateHandler(); |
| 168 message_loop_.RunUntilIdle(); |
| 169 // Set a service property. |
| 170 const std::string eth0 = "stub_ethernet"; |
| 171 EXPECT_EQ("", network_state_handler_->GetNetworkState(eth0)->security()); |
| 172 EXPECT_EQ(1, test_observer_->PropertyChangesForService(eth0)); |
| 173 base::StringValue security_value("TestSecurity"); |
| 174 DBusThreadManager::Get()->GetShillServiceClient()->SetProperty( |
| 175 dbus::ObjectPath(eth0), |
| 176 flimflam::kSecurityProperty, security_value, |
| 177 base::Bind(&base::DoNothing), base::Bind(&ErrorCallbackFunction)); |
| 178 message_loop_.RunUntilIdle(); |
| 179 EXPECT_EQ("TestSecurity", |
| 180 network_state_handler_->GetNetworkState(eth0)->security()); |
| 181 EXPECT_EQ(2, test_observer_->PropertyChangesForService(eth0)); |
| 182 } |
| 183 |
| 184 TEST_F(NetworkStateHandlerTest, NetworkStateHandlerActiveServiceChanged) { |
| 185 // This relies on the stub dbus implementations for ShillManagerClient, |
| 186 SetupNetworkStateHandler(); |
| 187 message_loop_.RunUntilIdle(); |
| 188 |
| 189 // Change the active network by inserting wifi1 at the front of the list. |
| 190 ShillManagerClient::TestInterface* manager_test = |
| 191 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface(); |
| 192 ASSERT_TRUE(manager_test); |
| 193 const std::string wifi1 = "stub_wifi1"; |
| 194 manager_test->AddServiceAtIndex(wifi1, 0, true); |
| 195 message_loop_.RunUntilIdle(); |
| 196 EXPECT_EQ(wifi1, test_observer_->active_network()); |
| 197 EXPECT_EQ(flimflam::kStateOnline, test_observer_->active_network_state()); |
| 198 |
| 199 // Change the state of wifi1, ensure that triggers the active state changed |
| 200 // observer. |
| 201 ShillServiceClient::TestInterface* service_test = |
| 202 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface(); |
| 203 ASSERT_TRUE(service_test); |
| 204 base::StringValue state_value(flimflam::kStateConfiguration); |
| 205 service_test->SetServiceProperty(wifi1, flimflam::kStateProperty, |
| 206 state_value); |
| 207 message_loop_.RunUntilIdle(); |
| 208 EXPECT_EQ(wifi1, test_observer_->active_network()); |
| 209 EXPECT_EQ(flimflam::kStateConfiguration, |
| 210 test_observer_->active_network_state()); |
| 211 } |
| 212 |
| 213 } // namespace chromeos |
OLD | NEW |