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 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_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_state_ = network ? network->state() : ""; |
55 } | 61 } |
56 | 62 |
57 virtual void ActiveNetworkStateChanged(const NetworkState* network) { | 63 virtual void NetworkStateChanged(const NetworkState* network) OVERRIDE { |
58 active_network_state_ = network ? network->state() : ""; | 64 network_state_[network->path()] = network->state(); |
59 } | 65 } |
60 | 66 |
61 virtual void NetworkServiceChanged(const NetworkState* network) { | 67 virtual void NetworkPropertyChanged(const NetworkState* network) OVERRIDE { |
62 DCHECK(network); | 68 DCHECK(network); |
63 std::map<std::string, int>::iterator iter = | 69 std::map<std::string, int>::iterator iter = |
64 property_changes_.find(network->path()); | 70 property_changes_.find(network->path()); |
65 if (iter == property_changes_.end()) | 71 if (iter == property_changes_.end()) |
66 property_changes_[network->path()] = 1; | 72 property_changes_[network->path()] = 1; |
67 else | 73 else |
68 iter->second++; | 74 iter->second++; |
69 } | 75 } |
70 | 76 |
71 size_t manager_changed_count() { return manager_changed_count_; } | 77 size_t manager_changed_count() { return manager_changed_count_; } |
72 size_t network_count() { return network_count_; } | 78 size_t network_count() { return network_count_; } |
73 std::string active_network() { return active_network_; } | 79 std::string default_network() { return default_network_; } |
74 std::string active_network_state() { return active_network_state_; } | 80 std::string default_network_state() { return default_network_state_; } |
75 | 81 |
76 int PropertyChangesForService(const std::string& service_path) { | 82 int PropertyChangesForService(const std::string& service_path) { |
77 std::map<std::string, int>::iterator iter = | 83 std::map<std::string, int>::iterator iter = |
78 property_changes_.find(service_path); | 84 property_changes_.find(service_path); |
79 if (iter == property_changes_.end()) | 85 if (iter == property_changes_.end()) |
80 return 0; | 86 return 0; |
81 return iter->second; | 87 return iter->second; |
82 } | 88 } |
83 | 89 |
| 90 std::string NetworkStateForService(const std::string& service_path) { |
| 91 return network_state_[service_path]; |
| 92 } |
| 93 |
84 private: | 94 private: |
| 95 NetworkStateHandler* handler_; |
85 size_t manager_changed_count_; | 96 size_t manager_changed_count_; |
86 size_t network_count_; | 97 size_t network_count_; |
87 std::string active_network_; | 98 std::string default_network_; |
88 std::string active_network_state_; | 99 std::string default_network_state_; |
89 std::map<std::string, int> property_changes_; | 100 std::map<std::string, int> property_changes_; |
| 101 std::map<std::string, std::string> network_state_; |
90 | 102 |
91 DISALLOW_COPY_AND_ASSIGN(TestObserver); | 103 DISALLOW_COPY_AND_ASSIGN(TestObserver); |
92 }; | 104 }; |
93 | 105 |
94 } // namespace | 106 } // namespace |
95 | 107 |
96 namespace chromeos { | 108 namespace chromeos { |
97 | 109 |
98 class NetworkStateHandlerTest : public testing::Test { | 110 class NetworkStateHandlerTest : public testing::Test { |
99 public: | 111 public: |
100 NetworkStateHandlerTest() {} | 112 NetworkStateHandlerTest() {} |
101 virtual ~NetworkStateHandlerTest() {} | 113 virtual ~NetworkStateHandlerTest() {} |
102 | 114 |
103 virtual void SetUp() OVERRIDE { | 115 virtual void SetUp() OVERRIDE { |
104 // Initialize DBusThreadManager with a stub implementation. | 116 // Initialize DBusThreadManager with a stub implementation. |
105 DBusThreadManager::InitializeWithStub(); | 117 DBusThreadManager::InitializeWithStub(); |
106 } | 118 } |
107 | 119 |
108 virtual void TearDown() OVERRIDE { | 120 virtual void TearDown() OVERRIDE { |
109 network_state_handler_.reset(); | 121 network_state_handler_.reset(); |
110 test_observer_.reset(); | 122 test_observer_.reset(); |
111 DBusThreadManager::Shutdown(); | 123 DBusThreadManager::Shutdown(); |
112 } | 124 } |
113 | 125 |
114 void SetupNetworkStateHandler() { | 126 void SetupNetworkStateHandler() { |
115 test_observer_.reset(new TestObserver); | |
116 network_state_handler_.reset(new NetworkStateHandler); | 127 network_state_handler_.reset(new NetworkStateHandler); |
| 128 test_observer_.reset(new TestObserver(network_state_handler_.get())); |
117 network_state_handler_->AddObserver(test_observer_.get()); | 129 network_state_handler_->AddObserver(test_observer_.get()); |
118 network_state_handler_->InitShillPropertyHandler(); | 130 network_state_handler_->InitShillPropertyHandler(); |
119 } | 131 } |
120 | 132 |
121 protected: | 133 protected: |
122 MessageLoopForUI message_loop_; | 134 MessageLoopForUI message_loop_; |
123 scoped_ptr<NetworkStateHandler> network_state_handler_; | 135 scoped_ptr<NetworkStateHandler> network_state_handler_; |
124 scoped_ptr<TestObserver> test_observer_; | 136 scoped_ptr<TestObserver> test_observer_; |
125 | 137 |
126 private: | 138 private: |
127 DISALLOW_COPY_AND_ASSIGN(NetworkStateHandlerTest); | 139 DISALLOW_COPY_AND_ASSIGN(NetworkStateHandlerTest); |
128 }; | 140 }; |
129 | 141 |
130 TEST_F(NetworkStateHandlerTest, NetworkStateHandlerStub) { | 142 TEST_F(NetworkStateHandlerTest, NetworkStateHandlerStub) { |
131 // This relies on the stub dbus implementations for ShillManagerClient, | 143 // This relies on the stub dbus implementations for ShillManagerClient, |
132 SetupNetworkStateHandler(); | 144 SetupNetworkStateHandler(); |
133 message_loop_.RunUntilIdle(); | 145 message_loop_.RunUntilIdle(); |
134 EXPECT_EQ(1u, test_observer_->manager_changed_count()); | 146 EXPECT_EQ(1u, test_observer_->manager_changed_count()); |
135 // Ensure that the network list is the expected size. | 147 // Ensure that the network list is the expected size. |
136 const size_t kNumShillManagerClientStubImplServices = 4; | 148 const size_t kNumShillManagerClientStubImplServices = 4; |
137 EXPECT_EQ(kNumShillManagerClientStubImplServices, | 149 EXPECT_EQ(kNumShillManagerClientStubImplServices, |
138 test_observer_->network_count()); | 150 test_observer_->network_count()); |
139 // Ensure that the first stub network is the active network. | 151 // Ensure that the first stub network is the default network. |
140 const std::string kShillManagerClientStubActiveService = "stub_ethernet"; | 152 const std::string kShillManagerClientStubDefaultService = "stub_ethernet"; |
141 EXPECT_EQ(kShillManagerClientStubActiveService, | 153 EXPECT_EQ(kShillManagerClientStubDefaultService, |
142 test_observer_->active_network()); | 154 test_observer_->default_network()); |
143 EXPECT_EQ(kShillManagerClientStubActiveService, | 155 EXPECT_EQ(kShillManagerClientStubDefaultService, |
144 network_state_handler_->ActiveNetwork()->path()); | 156 network_state_handler_->ConnectedNetworkByType( |
145 EXPECT_EQ(kShillManagerClientStubActiveService, | 157 NetworkState::kMatchTypeDefault)->path()); |
| 158 EXPECT_EQ(kShillManagerClientStubDefaultService, |
146 network_state_handler_->ConnectedNetworkByType( | 159 network_state_handler_->ConnectedNetworkByType( |
147 flimflam::kTypeEthernet)->path()); | 160 flimflam::kTypeEthernet)->path()); |
148 EXPECT_EQ(flimflam::kStateOnline, test_observer_->active_network_state()); | 161 const std::string kShillManagerClientStubDefaultWireless = "stub_wifi1"; |
| 162 EXPECT_EQ(kShillManagerClientStubDefaultWireless, |
| 163 network_state_handler_->ConnectedNetworkByType( |
| 164 NetworkState::kMatchTypeWireless)->path()); |
| 165 EXPECT_EQ(flimflam::kStateOnline, test_observer_->default_network_state()); |
149 } | 166 } |
150 | 167 |
151 TEST_F(NetworkStateHandlerTest, NetworkStateHandlerTechnologyChanged) { | 168 TEST_F(NetworkStateHandlerTest, TechnologyChanged) { |
152 // This relies on the stub dbus implementations for ShillManagerClient, | 169 // This relies on the stub dbus implementations for ShillManagerClient, |
153 SetupNetworkStateHandler(); | 170 SetupNetworkStateHandler(); |
154 message_loop_.RunUntilIdle(); | 171 message_loop_.RunUntilIdle(); |
155 EXPECT_EQ(1u, test_observer_->manager_changed_count()); | 172 EXPECT_EQ(1u, test_observer_->manager_changed_count()); |
156 // Enable a technology. | 173 // Enable a technology. |
157 EXPECT_FALSE(network_state_handler_->TechnologyEnabled(flimflam::kTypeWimax)); | 174 EXPECT_FALSE(network_state_handler_->TechnologyEnabled(flimflam::kTypeWimax)); |
158 network_state_handler_->SetTechnologyEnabled( | 175 network_state_handler_->SetTechnologyEnabled( |
159 flimflam::kTypeWimax, true, network_handler::ErrorCallback()); | 176 flimflam::kTypeWimax, true, network_handler::ErrorCallback()); |
160 message_loop_.RunUntilIdle(); | 177 message_loop_.RunUntilIdle(); |
161 // Ensure we get a manager changed callback when we change a property. | 178 // Ensure we get a manager changed callback when we change a property. |
162 EXPECT_EQ(2u, test_observer_->manager_changed_count()); | 179 EXPECT_EQ(2u, test_observer_->manager_changed_count()); |
163 EXPECT_TRUE(network_state_handler_->TechnologyEnabled(flimflam::kTypeWimax)); | 180 EXPECT_TRUE(network_state_handler_->TechnologyEnabled(flimflam::kTypeWimax)); |
164 } | 181 } |
165 | 182 |
166 TEST_F(NetworkStateHandlerTest, NetworkStateHandlerServicePropertyChanged) { | 183 TEST_F(NetworkStateHandlerTest, ServicePropertyChanged) { |
167 // This relies on the stub dbus implementations for ShillManagerClient, | 184 // This relies on the stub dbus implementations for ShillManagerClient, |
168 SetupNetworkStateHandler(); | 185 SetupNetworkStateHandler(); |
169 message_loop_.RunUntilIdle(); | 186 message_loop_.RunUntilIdle(); |
170 // Set a service property. | 187 // Set a service property. |
171 const std::string eth0 = "stub_ethernet"; | 188 const std::string eth0 = "stub_ethernet"; |
172 EXPECT_EQ("", network_state_handler_->GetNetworkState(eth0)->security()); | 189 EXPECT_EQ("", network_state_handler_->GetNetworkState(eth0)->security()); |
173 EXPECT_EQ(1, test_observer_->PropertyChangesForService(eth0)); | 190 EXPECT_EQ(1, test_observer_->PropertyChangesForService(eth0)); |
174 base::StringValue security_value("TestSecurity"); | 191 base::StringValue security_value("TestSecurity"); |
175 DBusThreadManager::Get()->GetShillServiceClient()->SetProperty( | 192 DBusThreadManager::Get()->GetShillServiceClient()->SetProperty( |
176 dbus::ObjectPath(eth0), | 193 dbus::ObjectPath(eth0), |
177 flimflam::kSecurityProperty, security_value, | 194 flimflam::kSecurityProperty, security_value, |
178 base::Bind(&base::DoNothing), base::Bind(&ErrorCallbackFunction)); | 195 base::Bind(&base::DoNothing), base::Bind(&ErrorCallbackFunction)); |
179 message_loop_.RunUntilIdle(); | 196 message_loop_.RunUntilIdle(); |
180 EXPECT_EQ("TestSecurity", | 197 EXPECT_EQ("TestSecurity", |
181 network_state_handler_->GetNetworkState(eth0)->security()); | 198 network_state_handler_->GetNetworkState(eth0)->security()); |
182 EXPECT_EQ(2, test_observer_->PropertyChangesForService(eth0)); | 199 EXPECT_EQ(2, test_observer_->PropertyChangesForService(eth0)); |
183 } | 200 } |
184 | 201 |
185 TEST_F(NetworkStateHandlerTest, NetworkStateHandlerActiveServiceChanged) { | 202 TEST_F(NetworkStateHandlerTest, NetworkStateChanged) { |
| 203 // This relies on the stub dbus implementations for ShillManagerClient, |
| 204 SetupNetworkStateHandler(); |
| 205 message_loop_.RunUntilIdle(); |
| 206 // Change a network state. |
| 207 ShillServiceClient::TestInterface* service_test = |
| 208 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface(); |
| 209 const std::string eth0 = "stub_ethernet"; |
| 210 base::StringValue state_idle_value(flimflam::kStateIdle); |
| 211 service_test->SetServiceProperty(eth0, flimflam::kStateProperty, |
| 212 state_idle_value); |
| 213 message_loop_.RunUntilIdle(); |
| 214 EXPECT_EQ(flimflam::kStateIdle, test_observer_->NetworkStateForService(eth0)); |
| 215 } |
| 216 |
| 217 TEST_F(NetworkStateHandlerTest, DefaultServiceChanged) { |
186 // This relies on the stub dbus implementations for ShillManagerClient, | 218 // This relies on the stub dbus implementations for ShillManagerClient, |
187 SetupNetworkStateHandler(); | 219 SetupNetworkStateHandler(); |
188 message_loop_.RunUntilIdle(); | 220 message_loop_.RunUntilIdle(); |
189 | 221 |
190 // Change the active network by inserting wifi1 at the front of the list. | |
191 ShillManagerClient::TestInterface* manager_test = | 222 ShillManagerClient::TestInterface* manager_test = |
192 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface(); | 223 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface(); |
193 ASSERT_TRUE(manager_test); | 224 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 = | 225 ShillServiceClient::TestInterface* service_test = |
203 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface(); | 226 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface(); |
204 ASSERT_TRUE(service_test); | 227 ASSERT_TRUE(service_test); |
205 base::StringValue state_value(flimflam::kStateConfiguration); | 228 |
206 service_test->SetServiceProperty(wifi1, flimflam::kStateProperty, | 229 // Change the default network by inserting wifi1 at the front of the list |
207 state_value); | 230 // and changing the state of stub_ethernet to Idle. |
| 231 const std::string wifi1 = "stub_wifi1"; |
| 232 manager_test->AddServiceAtIndex(wifi1, 0, true); |
| 233 const std::string eth0 = "stub_ethernet"; |
| 234 base::StringValue state_idle_value(flimflam::kStateIdle); |
| 235 service_test->SetServiceProperty(eth0, flimflam::kStateProperty, |
| 236 state_idle_value); |
208 message_loop_.RunUntilIdle(); | 237 message_loop_.RunUntilIdle(); |
209 EXPECT_EQ(wifi1, test_observer_->active_network()); | 238 EXPECT_EQ(wifi1, test_observer_->default_network()); |
210 EXPECT_EQ(flimflam::kStateConfiguration, | 239 EXPECT_EQ(flimflam::kStateOnline, test_observer_->default_network_state()); |
211 test_observer_->active_network_state()); | |
212 } | 240 } |
213 | 241 |
214 } // namespace chromeos | 242 } // namespace chromeos |
OLD | NEW |