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

Side by Side Diff: chromeos/components/tether/tether_connector_unittest.cc

Issue 2857853005: [CrOS Tether] Create TetherDisconnector, which disconnects from active tethering sessions. (Closed)
Patch Set: hansberry@ comments. Created 3 years, 7 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
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 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/components/tether/tether_connector.h" 5 #include "chromeos/components/tether/tether_connector.h"
6 6
7 #include "base/memory/ptr_util.h" 7 #include "base/memory/ptr_util.h"
8 #include "base/message_loop/message_loop.h" 8 #include "base/message_loop/message_loop.h"
9 #include "chromeos/components/tether/connect_tethering_operation.h" 9 #include "chromeos/components/tether/connect_tethering_operation.h"
10 #include "chromeos/components/tether/device_id_tether_network_guid_map.h" 10 #include "chromeos/components/tether/device_id_tether_network_guid_map.h"
(...skipping 30 matching lines...) Expand all
41 std::string CreateWifiConfigurationJsonString() { 41 std::string CreateWifiConfigurationJsonString() {
42 std::stringstream ss; 42 std::stringstream ss;
43 ss << "{" 43 ss << "{"
44 << " \"GUID\": \"" << kWifiNetworkGuid << "\"," 44 << " \"GUID\": \"" << kWifiNetworkGuid << "\","
45 << " \"Type\": \"" << shill::kTypeWifi << "\"," 45 << " \"Type\": \"" << shill::kTypeWifi << "\","
46 << " \"State\": \"" << shill::kStateIdle << "\"" 46 << " \"State\": \"" << shill::kStateIdle << "\""
47 << "}"; 47 << "}";
48 return ss.str(); 48 return ss.str();
49 } 49 }
50 50
51 class TestNetworkConnectionHandler : public NetworkConnectionHandler {
52 public:
53 TestNetworkConnectionHandler() : NetworkConnectionHandler() {}
54 ~TestNetworkConnectionHandler() override {}
55
56 void CallTetherDelegate(
57 const std::string& tether_network_guid,
58 const base::Closure& success_callback,
59 const network_handler::ErrorCallback& error_callback) {
60 InitiateTetherNetworkConnection(tether_network_guid, success_callback,
61 error_callback);
62 }
63
64 // NetworkConnectionHandler:
65 void ConnectToNetwork(const std::string& service_path,
66 const base::Closure& success_callback,
67 const network_handler::ErrorCallback& error_callback,
68 bool check_error_state) override {}
69
70 void DisconnectNetwork(
71 const std::string& service_path,
72 const base::Closure& success_callback,
73 const network_handler::ErrorCallback& error_callback) override {}
74
75 bool HasConnectingNetwork(const std::string& service_path) override {
76 return false;
77 }
78
79 bool HasPendingConnectRequest() override { return false; }
80
81 void Init(NetworkStateHandler* network_state_handler,
82 NetworkConfigurationHandler* network_configuration_handler,
83 ManagedNetworkConfigurationHandler*
84 managed_network_configuration_handler) override {}
85 };
86
87 class FakeConnectTetheringOperation : public ConnectTetheringOperation { 51 class FakeConnectTetheringOperation : public ConnectTetheringOperation {
88 public: 52 public:
89 FakeConnectTetheringOperation( 53 FakeConnectTetheringOperation(
90 const cryptauth::RemoteDevice& device_to_connect, 54 const cryptauth::RemoteDevice& device_to_connect,
91 BleConnectionManager* connection_manager, 55 BleConnectionManager* connection_manager,
92 TetherHostResponseRecorder* tether_host_response_recorder) 56 TetherHostResponseRecorder* tether_host_response_recorder)
93 : ConnectTetheringOperation(device_to_connect, 57 : ConnectTetheringOperation(device_to_connect,
94 connection_manager, 58 connection_manager,
95 tether_host_response_recorder) {} 59 tether_host_response_recorder) {}
96 60
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 DBusThreadManager::Initialize(); 114 DBusThreadManager::Initialize();
151 NetworkStateTest::SetUp(); 115 NetworkStateTest::SetUp();
152 network_state_handler()->SetTetherTechnologyState( 116 network_state_handler()->SetTetherTechnologyState(
153 NetworkStateHandler::TECHNOLOGY_ENABLED); 117 NetworkStateHandler::TECHNOLOGY_ENABLED);
154 118
155 fake_operation_factory_ = 119 fake_operation_factory_ =
156 base::WrapUnique(new FakeConnectTetheringOperationFactory()); 120 base::WrapUnique(new FakeConnectTetheringOperationFactory());
157 ConnectTetheringOperation::Factory::SetInstanceForTesting( 121 ConnectTetheringOperation::Factory::SetInstanceForTesting(
158 fake_operation_factory_.get()); 122 fake_operation_factory_.get());
159 123
160 test_network_connection_handler_ =
161 base::WrapUnique(new TestNetworkConnectionHandler());
162 fake_wifi_hotspot_connector_ = 124 fake_wifi_hotspot_connector_ =
163 base::MakeUnique<FakeWifiHotspotConnector>(network_state_handler()); 125 base::MakeUnique<FakeWifiHotspotConnector>(network_state_handler());
164 fake_active_host_ = base::MakeUnique<FakeActiveHost>(); 126 fake_active_host_ = base::MakeUnique<FakeActiveHost>();
165 fake_tether_host_fetcher_ = base::MakeUnique<FakeTetherHostFetcher>( 127 fake_tether_host_fetcher_ = base::MakeUnique<FakeTetherHostFetcher>(
166 test_devices_, false /* synchronously_reply_with_results */); 128 test_devices_, false /* synchronously_reply_with_results */);
167 fake_ble_connection_manager_ = base::MakeUnique<FakeBleConnectionManager>(); 129 fake_ble_connection_manager_ = base::MakeUnique<FakeBleConnectionManager>();
168 mock_tether_host_response_recorder_ = 130 mock_tether_host_response_recorder_ =
169 base::MakeUnique<MockTetherHostResponseRecorder>(); 131 base::MakeUnique<MockTetherHostResponseRecorder>();
170 device_id_tether_network_guid_map_ = 132 device_id_tether_network_guid_map_ =
171 base::MakeUnique<DeviceIdTetherNetworkGuidMap>(); 133 base::MakeUnique<DeviceIdTetherNetworkGuidMap>();
172 134
173 result_.clear(); 135 result_.clear();
174 136
175 tether_connector_ = base::WrapUnique(new TetherConnector( 137 tether_connector_ = base::WrapUnique(new TetherConnector(
176 test_network_connection_handler_.get(), network_state_handler(), 138 network_state_handler(), fake_wifi_hotspot_connector_.get(),
177 fake_wifi_hotspot_connector_.get(), fake_active_host_.get(), 139 fake_active_host_.get(), fake_tether_host_fetcher_.get(),
178 fake_tether_host_fetcher_.get(), fake_ble_connection_manager_.get(), 140 fake_ble_connection_manager_.get(),
179 mock_tether_host_response_recorder_.get(), 141 mock_tether_host_response_recorder_.get(),
180 device_id_tether_network_guid_map_.get())); 142 device_id_tether_network_guid_map_.get()));
181 143
182 SetUpTetherNetworks(); 144 SetUpTetherNetworks();
183 } 145 }
184 146
185 void TearDown() override { 147 void TearDown() override {
186 // Must delete |fake_wifi_hotspot_connector_| before NetworkStateHandler is 148 // Must delete |fake_wifi_hotspot_connector_| before NetworkStateHandler is
187 // destroyed to ensure that NetworkStateHandler has zero observers by the 149 // destroyed to ensure that NetworkStateHandler has zero observers by the
188 // time it reaches its destructor. 150 // time it reaches its destructor.
189 fake_wifi_hotspot_connector_.reset(); 151 fake_wifi_hotspot_connector_.reset();
190 152
191 ShutdownNetworkState(); 153 ShutdownNetworkState();
192 NetworkStateTest::TearDown(); 154 NetworkStateTest::TearDown();
193 DBusThreadManager::Shutdown(); 155 DBusThreadManager::Shutdown();
194 } 156 }
195 157
196 std::string GetTetherNetworkGuid(const std::string& device_id) { 158 std::string GetTetherNetworkGuid(const std::string& device_id) {
197 return device_id_tether_network_guid_map_->GetTetherNetworkGuidForDeviceId( 159 return device_id_tether_network_guid_map_->GetTetherNetworkGuidForDeviceId(
198 device_id); 160 device_id);
199 } 161 }
200 162
201 void SetUpTetherNetworks() { 163 void SetUpTetherNetworks() {
202 // Add a tether network corresponding to both of the test devices. These 164 // Add a tether network corresponding to both of the test devices. These
203 // networks are expected to be added already before TetherConnector receives 165 // networks are expected to be added already before
204 // its ConnectToNetwork() callback. 166 // TetherConnector::ConnectToNetwork is called.
205 network_state_handler()->AddTetherNetworkState( 167 network_state_handler()->AddTetherNetworkState(
206 GetTetherNetworkGuid(test_devices_[0].GetDeviceId()), 168 GetTetherNetworkGuid(test_devices_[0].GetDeviceId()),
207 "TetherNetworkName1", "TetherNetworkCarrier1", 169 "TetherNetworkName1", "TetherNetworkCarrier1",
208 85 /* battery_percentage */, 75 /* signal_strength */, 170 85 /* battery_percentage */, 75 /* signal_strength */,
209 true /* has_connected_to_host */); 171 true /* has_connected_to_host */);
210 network_state_handler()->AddTetherNetworkState( 172 network_state_handler()->AddTetherNetworkState(
211 GetTetherNetworkGuid(test_devices_[1].GetDeviceId()), 173 GetTetherNetworkGuid(test_devices_[1].GetDeviceId()),
212 "TetherNetworkName2", "TetherNetworkCarrier2", 174 "TetherNetworkName2", "TetherNetworkCarrier2",
213 90 /* battery_percentage */, 50 /* signal_strength */, 175 90 /* battery_percentage */, 50 /* signal_strength */,
214 true /* has_connected_to_host */); 176 true /* has_connected_to_host */);
(...skipping 12 matching lines...) Expand all
227 EXPECT_EQ(kWifiNetworkGuid, tether_network_state->tether_guid()); 189 EXPECT_EQ(kWifiNetworkGuid, tether_network_state->tether_guid());
228 190
229 const NetworkState* wifi_network_state = 191 const NetworkState* wifi_network_state =
230 network_state_handler()->GetNetworkStateFromGuid(kWifiNetworkGuid); 192 network_state_handler()->GetNetworkStateFromGuid(kWifiNetworkGuid);
231 EXPECT_TRUE(wifi_network_state); 193 EXPECT_TRUE(wifi_network_state);
232 EXPECT_EQ(tether_network_guid, wifi_network_state->tether_guid()); 194 EXPECT_EQ(tether_network_guid, wifi_network_state->tether_guid());
233 } 195 }
234 196
235 void SuccessCallback() { result_ = kSuccessResult; } 197 void SuccessCallback() { result_ = kSuccessResult; }
236 198
237 void ErrorCallback(const std::string& error_name, 199 void ErrorCallback(const std::string& error_name) { result_ = error_name; }
238 std::unique_ptr<base::DictionaryValue> error_data) {
239 result_ = error_name;
240 }
241 200
242 void CallTetherDelegate(const std::string& tether_network_guid) { 201 void CallConnect(const std::string& tether_network_guid) {
243 test_network_connection_handler_->CallTetherDelegate( 202 tether_connector_->ConnectToNetwork(
244 tether_network_guid, 203 tether_network_guid,
245 base::Bind(&TetherConnectorTest::SuccessCallback, 204 base::Bind(&TetherConnectorTest::SuccessCallback,
246 base::Unretained(this)), 205 base::Unretained(this)),
247 base::Bind(&TetherConnectorTest::ErrorCallback, 206 base::Bind(&TetherConnectorTest::ErrorCallback,
248 base::Unretained(this))); 207 base::Unretained(this)));
249 } 208 }
250 209
251 std::string GetResultAndReset() { 210 std::string GetResultAndReset() {
252 std::string result; 211 std::string result;
253 result.swap(result_); 212 result.swap(result_);
254 return result; 213 return result;
255 } 214 }
256 215
257 const std::vector<cryptauth::RemoteDevice> test_devices_; 216 const std::vector<cryptauth::RemoteDevice> test_devices_;
258 const base::MessageLoop message_loop_; 217 const base::MessageLoop message_loop_;
259 218
260 std::unique_ptr<FakeConnectTetheringOperationFactory> fake_operation_factory_; 219 std::unique_ptr<FakeConnectTetheringOperationFactory> fake_operation_factory_;
261 std::unique_ptr<TestNetworkConnectionHandler>
262 test_network_connection_handler_;
263 std::unique_ptr<FakeWifiHotspotConnector> fake_wifi_hotspot_connector_; 220 std::unique_ptr<FakeWifiHotspotConnector> fake_wifi_hotspot_connector_;
264 std::unique_ptr<FakeActiveHost> fake_active_host_; 221 std::unique_ptr<FakeActiveHost> fake_active_host_;
265 std::unique_ptr<FakeTetherHostFetcher> fake_tether_host_fetcher_; 222 std::unique_ptr<FakeTetherHostFetcher> fake_tether_host_fetcher_;
266 std::unique_ptr<FakeBleConnectionManager> fake_ble_connection_manager_; 223 std::unique_ptr<FakeBleConnectionManager> fake_ble_connection_manager_;
267 std::unique_ptr<MockTetherHostResponseRecorder> 224 std::unique_ptr<MockTetherHostResponseRecorder>
268 mock_tether_host_response_recorder_; 225 mock_tether_host_response_recorder_;
269 // TODO(hansberry): Use a fake for this when a real mapping scheme is created. 226 // TODO(hansberry): Use a fake for this when a real mapping scheme is created.
270 std::unique_ptr<DeviceIdTetherNetworkGuidMap> 227 std::unique_ptr<DeviceIdTetherNetworkGuidMap>
271 device_id_tether_network_guid_map_; 228 device_id_tether_network_guid_map_;
272 229
273 std::string result_; 230 std::string result_;
274 231
275 std::unique_ptr<TetherConnector> tether_connector_; 232 std::unique_ptr<TetherConnector> tether_connector_;
276 233
277 private: 234 private:
278 DISALLOW_COPY_AND_ASSIGN(TetherConnectorTest); 235 DISALLOW_COPY_AND_ASSIGN(TetherConnectorTest);
279 }; 236 };
280 237
281 TEST_F(TetherConnectorTest, TestCannotFetchDevice) { 238 TEST_F(TetherConnectorTest, TestCannotFetchDevice) {
282 // Base64-encoded version of "nonexistentDeviceId". 239 // Base64-encoded version of "nonexistentDeviceId".
283 const char kNonexistentDeviceId[] = "bm9uZXhpc3RlbnREZXZpY2VJZA=="; 240 const char kNonexistentDeviceId[] = "bm9uZXhpc3RlbnREZXZpY2VJZA==";
284 241
285 CallTetherDelegate(GetTetherNetworkGuid(kNonexistentDeviceId)); 242 CallConnect(GetTetherNetworkGuid(kNonexistentDeviceId));
286 EXPECT_EQ(ActiveHost::ActiveHostStatus::CONNECTING, 243 EXPECT_EQ(ActiveHost::ActiveHostStatus::CONNECTING,
287 fake_active_host_->GetActiveHostStatus()); 244 fake_active_host_->GetActiveHostStatus());
288 EXPECT_EQ(kNonexistentDeviceId, fake_active_host_->GetActiveHostDeviceId()); 245 EXPECT_EQ(kNonexistentDeviceId, fake_active_host_->GetActiveHostDeviceId());
289 EXPECT_EQ(GetTetherNetworkGuid(kNonexistentDeviceId), 246 EXPECT_EQ(GetTetherNetworkGuid(kNonexistentDeviceId),
290 fake_active_host_->GetTetherNetworkGuid()); 247 fake_active_host_->GetTetherNetworkGuid());
291 EXPECT_TRUE(fake_active_host_->GetWifiNetworkGuid().empty()); 248 EXPECT_TRUE(fake_active_host_->GetWifiNetworkGuid().empty());
292 249
293 fake_tether_host_fetcher_->InvokePendingCallbacks(); 250 fake_tether_host_fetcher_->InvokePendingCallbacks();
294 251
295 // Since an invalid device ID was used, no connection should have been 252 // Since an invalid device ID was used, no connection should have been
296 // started. 253 // started.
297 EXPECT_EQ(ActiveHost::ActiveHostStatus::DISCONNECTED, 254 EXPECT_EQ(ActiveHost::ActiveHostStatus::DISCONNECTED,
298 fake_active_host_->GetActiveHostStatus()); 255 fake_active_host_->GetActiveHostStatus());
299 EXPECT_EQ(NetworkConnectionHandler::kErrorConnectFailed, GetResultAndReset()); 256 EXPECT_EQ(NetworkConnectionHandler::kErrorConnectFailed, GetResultAndReset());
300 } 257 }
301 258
302 TEST_F(TetherConnectorTest, TestConnectTetheringOperationFails) { 259 TEST_F(TetherConnectorTest, TestCancelWhileOperationActive) {
303 CallTetherDelegate(GetTetherNetworkGuid(test_devices_[0].GetDeviceId())); 260 CallConnect(GetTetherNetworkGuid(test_devices_[0].GetDeviceId()));
304 EXPECT_EQ(ActiveHost::ActiveHostStatus::CONNECTING, 261 EXPECT_EQ(ActiveHost::ActiveHostStatus::CONNECTING,
305 fake_active_host_->GetActiveHostStatus()); 262 fake_active_host_->GetActiveHostStatus());
306 EXPECT_EQ(test_devices_[0].GetDeviceId(), 263 EXPECT_EQ(test_devices_[0].GetDeviceId(),
264 fake_active_host_->GetActiveHostDeviceId());
265 EXPECT_EQ(GetTetherNetworkGuid(test_devices_[0].GetDeviceId()),
266 fake_active_host_->GetTetherNetworkGuid());
267 EXPECT_TRUE(fake_active_host_->GetWifiNetworkGuid().empty());
268
269 fake_tether_host_fetcher_->InvokePendingCallbacks();
270
271 // Simulate a failed connection attempt (either the host cannot provide
272 // tethering at this time or a timeout occurs).
273 EXPECT_EQ(1u, fake_operation_factory_->created_operations().size());
274 tether_connector_->CancelConnectionAttempt(
275 GetTetherNetworkGuid(test_devices_[0].GetDeviceId()));
276
277 EXPECT_EQ(ActiveHost::ActiveHostStatus::DISCONNECTED,
278 fake_active_host_->GetActiveHostStatus());
279 EXPECT_EQ(NetworkConnectionHandler::kErrorConnectCanceled,
280 GetResultAndReset());
281 }
282
283 TEST_F(TetherConnectorTest, TestConnectTetheringOperationFails) {
284 CallConnect(GetTetherNetworkGuid(test_devices_[0].GetDeviceId()));
285 EXPECT_EQ(ActiveHost::ActiveHostStatus::CONNECTING,
286 fake_active_host_->GetActiveHostStatus());
287 EXPECT_EQ(test_devices_[0].GetDeviceId(),
307 fake_active_host_->GetActiveHostDeviceId()); 288 fake_active_host_->GetActiveHostDeviceId());
308 EXPECT_EQ(GetTetherNetworkGuid(test_devices_[0].GetDeviceId()), 289 EXPECT_EQ(GetTetherNetworkGuid(test_devices_[0].GetDeviceId()),
309 fake_active_host_->GetTetherNetworkGuid()); 290 fake_active_host_->GetTetherNetworkGuid());
310 EXPECT_TRUE(fake_active_host_->GetWifiNetworkGuid().empty()); 291 EXPECT_TRUE(fake_active_host_->GetWifiNetworkGuid().empty());
311 292
312 fake_tether_host_fetcher_->InvokePendingCallbacks(); 293 fake_tether_host_fetcher_->InvokePendingCallbacks();
313 294
314 // Simulate a failed connection attempt (either the host cannot provide 295 // Simulate a failed connection attempt (either the host cannot provide
315 // tethering at this time or a timeout occurs). 296 // tethering at this time or a timeout occurs).
316 EXPECT_EQ(1u, fake_operation_factory_->created_operations().size()); 297 EXPECT_EQ(1u, fake_operation_factory_->created_operations().size());
317 fake_operation_factory_->created_operations()[0]->SendFailedResponse( 298 fake_operation_factory_->created_operations()[0]->SendFailedResponse(
318 ConnectTetheringResponse_ResponseCode:: 299 ConnectTetheringResponse_ResponseCode::
319 ConnectTetheringResponse_ResponseCode_UNKNOWN_ERROR); 300 ConnectTetheringResponse_ResponseCode_UNKNOWN_ERROR);
320 301
321 // The failure should have resulted in the host being disconnected. 302 // The failure should have resulted in the host being disconnected.
322 EXPECT_EQ(ActiveHost::ActiveHostStatus::DISCONNECTED, 303 EXPECT_EQ(ActiveHost::ActiveHostStatus::DISCONNECTED,
323 fake_active_host_->GetActiveHostStatus()); 304 fake_active_host_->GetActiveHostStatus());
324 EXPECT_EQ(NetworkConnectionHandler::kErrorConnectFailed, GetResultAndReset()); 305 EXPECT_EQ(NetworkConnectionHandler::kErrorConnectFailed, GetResultAndReset());
325 } 306 }
326 307
327 TEST_F(TetherConnectorTest, TestConnectingToWifiFails) { 308 TEST_F(TetherConnectorTest, TestConnectingToWifiFails) {
328 CallTetherDelegate(GetTetherNetworkGuid(test_devices_[0].GetDeviceId())); 309 CallConnect(GetTetherNetworkGuid(test_devices_[0].GetDeviceId()));
329 EXPECT_EQ(ActiveHost::ActiveHostStatus::CONNECTING, 310 EXPECT_EQ(ActiveHost::ActiveHostStatus::CONNECTING,
330 fake_active_host_->GetActiveHostStatus()); 311 fake_active_host_->GetActiveHostStatus());
331 EXPECT_EQ(test_devices_[0].GetDeviceId(), 312 EXPECT_EQ(test_devices_[0].GetDeviceId(),
332 fake_active_host_->GetActiveHostDeviceId()); 313 fake_active_host_->GetActiveHostDeviceId());
333 EXPECT_EQ(GetTetherNetworkGuid(test_devices_[0].GetDeviceId()), 314 EXPECT_EQ(GetTetherNetworkGuid(test_devices_[0].GetDeviceId()),
334 fake_active_host_->GetTetherNetworkGuid()); 315 fake_active_host_->GetTetherNetworkGuid());
335 EXPECT_TRUE(fake_active_host_->GetWifiNetworkGuid().empty()); 316 EXPECT_TRUE(fake_active_host_->GetWifiNetworkGuid().empty());
336 317
337 fake_tether_host_fetcher_->InvokePendingCallbacks(); 318 fake_tether_host_fetcher_->InvokePendingCallbacks();
338 319
(...skipping 10 matching lines...) Expand all
349 EXPECT_EQ(kSsid, fake_wifi_hotspot_connector_->most_recent_ssid()); 330 EXPECT_EQ(kSsid, fake_wifi_hotspot_connector_->most_recent_ssid());
350 EXPECT_EQ(kPassword, fake_wifi_hotspot_connector_->most_recent_password()); 331 EXPECT_EQ(kPassword, fake_wifi_hotspot_connector_->most_recent_password());
351 fake_wifi_hotspot_connector_->CallMostRecentCallback(""); 332 fake_wifi_hotspot_connector_->CallMostRecentCallback("");
352 333
353 // The failure should have resulted in the host being disconnected. 334 // The failure should have resulted in the host being disconnected.
354 EXPECT_EQ(ActiveHost::ActiveHostStatus::DISCONNECTED, 335 EXPECT_EQ(ActiveHost::ActiveHostStatus::DISCONNECTED,
355 fake_active_host_->GetActiveHostStatus()); 336 fake_active_host_->GetActiveHostStatus());
356 EXPECT_EQ(NetworkConnectionHandler::kErrorConnectFailed, GetResultAndReset()); 337 EXPECT_EQ(NetworkConnectionHandler::kErrorConnectFailed, GetResultAndReset());
357 } 338 }
358 339
359 TEST_F(TetherConnectorTest, TestSuccessfulConnection) { 340 TEST_F(TetherConnectorTest, TestCancelWhileConnectingToWifi) {
360 CallTetherDelegate(GetTetherNetworkGuid(test_devices_[0].GetDeviceId())); 341 CallConnect(GetTetherNetworkGuid(test_devices_[0].GetDeviceId()));
361 EXPECT_EQ(ActiveHost::ActiveHostStatus::CONNECTING, 342 EXPECT_EQ(ActiveHost::ActiveHostStatus::CONNECTING,
362 fake_active_host_->GetActiveHostStatus()); 343 fake_active_host_->GetActiveHostStatus());
363 EXPECT_EQ(test_devices_[0].GetDeviceId(), 344 EXPECT_EQ(test_devices_[0].GetDeviceId(),
345 fake_active_host_->GetActiveHostDeviceId());
346 EXPECT_EQ(GetTetherNetworkGuid(test_devices_[0].GetDeviceId()),
347 fake_active_host_->GetTetherNetworkGuid());
348 EXPECT_TRUE(fake_active_host_->GetWifiNetworkGuid().empty());
349
350 fake_tether_host_fetcher_->InvokePendingCallbacks();
351
352 // Receive a successful response. We should still be connecting.
353 EXPECT_EQ(1u, fake_operation_factory_->created_operations().size());
354 fake_operation_factory_->created_operations()[0]->SendSuccessfulResponse(
355 kSsid, kPassword);
356 EXPECT_EQ(ActiveHost::ActiveHostStatus::CONNECTING,
357 fake_active_host_->GetActiveHostStatus());
358
359 tether_connector_->CancelConnectionAttempt(
360 GetTetherNetworkGuid(test_devices_[0].GetDeviceId()));
361
362 EXPECT_EQ(ActiveHost::ActiveHostStatus::DISCONNECTED,
363 fake_active_host_->GetActiveHostStatus());
364 EXPECT_EQ(NetworkConnectionHandler::kErrorConnectCanceled,
365 GetResultAndReset());
366 }
367
368 TEST_F(TetherConnectorTest, TestSuccessfulConnection) {
369 CallConnect(GetTetherNetworkGuid(test_devices_[0].GetDeviceId()));
370 EXPECT_EQ(ActiveHost::ActiveHostStatus::CONNECTING,
371 fake_active_host_->GetActiveHostStatus());
372 EXPECT_EQ(test_devices_[0].GetDeviceId(),
364 fake_active_host_->GetActiveHostDeviceId()); 373 fake_active_host_->GetActiveHostDeviceId());
365 EXPECT_EQ(GetTetherNetworkGuid(test_devices_[0].GetDeviceId()), 374 EXPECT_EQ(GetTetherNetworkGuid(test_devices_[0].GetDeviceId()),
366 fake_active_host_->GetTetherNetworkGuid()); 375 fake_active_host_->GetTetherNetworkGuid());
367 EXPECT_TRUE(fake_active_host_->GetWifiNetworkGuid().empty()); 376 EXPECT_TRUE(fake_active_host_->GetWifiNetworkGuid().empty());
368 377
369 fake_tether_host_fetcher_->InvokePendingCallbacks(); 378 fake_tether_host_fetcher_->InvokePendingCallbacks();
370 379
371 // Receive a successful response. We should still be connecting. 380 // Receive a successful response. We should still be connecting.
372 EXPECT_EQ(1u, fake_operation_factory_->created_operations().size()); 381 EXPECT_EQ(1u, fake_operation_factory_->created_operations().size());
373 fake_operation_factory_->created_operations()[0]->SendSuccessfulResponse( 382 fake_operation_factory_->created_operations()[0]->SendSuccessfulResponse(
(...skipping 17 matching lines...) Expand all
391 EXPECT_EQ(GetTetherNetworkGuid(test_devices_[0].GetDeviceId()), 400 EXPECT_EQ(GetTetherNetworkGuid(test_devices_[0].GetDeviceId()),
392 fake_active_host_->GetTetherNetworkGuid()); 401 fake_active_host_->GetTetherNetworkGuid());
393 EXPECT_EQ(kWifiNetworkGuid, fake_active_host_->GetWifiNetworkGuid()); 402 EXPECT_EQ(kWifiNetworkGuid, fake_active_host_->GetWifiNetworkGuid());
394 VerifyTetherAndWifiNetworkAssociation( 403 VerifyTetherAndWifiNetworkAssociation(
395 GetTetherNetworkGuid(test_devices_[0].GetDeviceId())); 404 GetTetherNetworkGuid(test_devices_[0].GetDeviceId()));
396 EXPECT_EQ(kSuccessResult, GetResultAndReset()); 405 EXPECT_EQ(kSuccessResult, GetResultAndReset());
397 } 406 }
398 407
399 TEST_F(TetherConnectorTest, 408 TEST_F(TetherConnectorTest,
400 TestNewConnectionAttemptDuringFetch_DifferentDevice) { 409 TestNewConnectionAttemptDuringFetch_DifferentDevice) {
401 CallTetherDelegate(GetTetherNetworkGuid(test_devices_[0].GetDeviceId())); 410 CallConnect(GetTetherNetworkGuid(test_devices_[0].GetDeviceId()));
402 411
403 // Instead of invoking the pending callbacks on |fake_tether_host_fetcher_|, 412 // Instead of invoking the pending callbacks on |fake_tether_host_fetcher_|,
404 // attempt another connection attempt, this time to another device. 413 // attempt another connection attempt, this time to another device.
405 CallTetherDelegate(GetTetherNetworkGuid(test_devices_[1].GetDeviceId())); 414 CallConnect(GetTetherNetworkGuid(test_devices_[1].GetDeviceId()));
406 // The first connection attempt should have resulted in a connect canceled 415 // The first connection attempt should have resulted in a connect canceled
407 // error. 416 // error.
408 EXPECT_EQ(NetworkConnectionHandler::kErrorConnectCanceled, 417 EXPECT_EQ(NetworkConnectionHandler::kErrorConnectCanceled,
409 GetResultAndReset()); 418 GetResultAndReset());
410 419
411 // Now invoke the callbacks. An operation should have been created for the 420 // Now invoke the callbacks. An operation should have been created for the
412 // device 1, not device 0. 421 // device 1, not device 0.
413 fake_tether_host_fetcher_->InvokePendingCallbacks(); 422 fake_tether_host_fetcher_->InvokePendingCallbacks();
414 EXPECT_EQ(1u, fake_operation_factory_->created_operations().size()); 423 EXPECT_EQ(1u, fake_operation_factory_->created_operations().size());
415 EXPECT_EQ( 424 EXPECT_EQ(
416 test_devices_[1], 425 test_devices_[1],
417 fake_operation_factory_->created_operations()[0]->GetRemoteDevice()); 426 fake_operation_factory_->created_operations()[0]->GetRemoteDevice());
418 } 427 }
419 428
420 TEST_F(TetherConnectorTest, 429 TEST_F(TetherConnectorTest,
421 TestNewConnectionAttemptDuringOperation_DifferentDevice) { 430 TestNewConnectionAttemptDuringOperation_DifferentDevice) {
422 CallTetherDelegate(GetTetherNetworkGuid(test_devices_[0].GetDeviceId())); 431 CallConnect(GetTetherNetworkGuid(test_devices_[0].GetDeviceId()));
423 EXPECT_EQ(ActiveHost::ActiveHostStatus::CONNECTING, 432 EXPECT_EQ(ActiveHost::ActiveHostStatus::CONNECTING,
424 fake_active_host_->GetActiveHostStatus()); 433 fake_active_host_->GetActiveHostStatus());
425 EXPECT_EQ(test_devices_[0].GetDeviceId(), 434 EXPECT_EQ(test_devices_[0].GetDeviceId(),
426 fake_active_host_->GetActiveHostDeviceId()); 435 fake_active_host_->GetActiveHostDeviceId());
427 EXPECT_EQ(GetTetherNetworkGuid(test_devices_[0].GetDeviceId()), 436 EXPECT_EQ(GetTetherNetworkGuid(test_devices_[0].GetDeviceId()),
428 fake_active_host_->GetTetherNetworkGuid()); 437 fake_active_host_->GetTetherNetworkGuid());
429 EXPECT_TRUE(fake_active_host_->GetWifiNetworkGuid().empty()); 438 EXPECT_TRUE(fake_active_host_->GetWifiNetworkGuid().empty());
430 439
431 fake_tether_host_fetcher_->InvokePendingCallbacks(); 440 fake_tether_host_fetcher_->InvokePendingCallbacks();
432 441
433 // An operation should have been created. 442 // An operation should have been created.
434 EXPECT_EQ(1u, fake_operation_factory_->created_operations().size()); 443 EXPECT_EQ(1u, fake_operation_factory_->created_operations().size());
435 444
436 // Before the created operation replies, start a new connection to device 1. 445 // Before the created operation replies, start a new connection to device 1.
437 CallTetherDelegate(GetTetherNetworkGuid(test_devices_[1].GetDeviceId())); 446 CallConnect(GetTetherNetworkGuid(test_devices_[1].GetDeviceId()));
438 // The first connection attempt should have resulted in a connect canceled 447 // The first connection attempt should have resulted in a connect canceled
439 // error. 448 // error.
440 EXPECT_EQ(NetworkConnectionHandler::kErrorConnectCanceled, 449 EXPECT_EQ(NetworkConnectionHandler::kErrorConnectCanceled,
441 GetResultAndReset()); 450 GetResultAndReset());
442 fake_tether_host_fetcher_->InvokePendingCallbacks(); 451 fake_tether_host_fetcher_->InvokePendingCallbacks();
443 452
444 // Now, the active host should be the second device. 453 // Now, the active host should be the second device.
445 EXPECT_EQ(ActiveHost::ActiveHostStatus::CONNECTING, 454 EXPECT_EQ(ActiveHost::ActiveHostStatus::CONNECTING,
446 fake_active_host_->GetActiveHostStatus()); 455 fake_active_host_->GetActiveHostStatus());
447 EXPECT_EQ(test_devices_[1].GetDeviceId(), 456 EXPECT_EQ(test_devices_[1].GetDeviceId(),
(...skipping 12 matching lines...) Expand all
460 // The second operation replies successfully, and this response should 469 // The second operation replies successfully, and this response should
461 // result in a Wi-Fi connection attempt. 470 // result in a Wi-Fi connection attempt.
462 fake_operation_factory_->created_operations()[1]->SendSuccessfulResponse( 471 fake_operation_factory_->created_operations()[1]->SendSuccessfulResponse(
463 kSsid, kPassword); 472 kSsid, kPassword);
464 EXPECT_EQ(kSsid, fake_wifi_hotspot_connector_->most_recent_ssid()); 473 EXPECT_EQ(kSsid, fake_wifi_hotspot_connector_->most_recent_ssid());
465 EXPECT_EQ(kPassword, fake_wifi_hotspot_connector_->most_recent_password()); 474 EXPECT_EQ(kPassword, fake_wifi_hotspot_connector_->most_recent_password());
466 } 475 }
467 476
468 TEST_F(TetherConnectorTest, 477 TEST_F(TetherConnectorTest,
469 TestNewConnectionAttemptDuringWifiConnection_DifferentDevice) { 478 TestNewConnectionAttemptDuringWifiConnection_DifferentDevice) {
470 CallTetherDelegate(GetTetherNetworkGuid(test_devices_[0].GetDeviceId())); 479 CallConnect(GetTetherNetworkGuid(test_devices_[0].GetDeviceId()));
471 EXPECT_EQ(ActiveHost::ActiveHostStatus::CONNECTING, 480 EXPECT_EQ(ActiveHost::ActiveHostStatus::CONNECTING,
472 fake_active_host_->GetActiveHostStatus()); 481 fake_active_host_->GetActiveHostStatus());
473 EXPECT_EQ(test_devices_[0].GetDeviceId(), 482 EXPECT_EQ(test_devices_[0].GetDeviceId(),
474 fake_active_host_->GetActiveHostDeviceId()); 483 fake_active_host_->GetActiveHostDeviceId());
475 484
476 fake_tether_host_fetcher_->InvokePendingCallbacks(); 485 fake_tether_host_fetcher_->InvokePendingCallbacks();
477 486
478 EXPECT_EQ(1u, fake_operation_factory_->created_operations().size()); 487 EXPECT_EQ(1u, fake_operation_factory_->created_operations().size());
479 fake_operation_factory_->created_operations()[0]->SendSuccessfulResponse( 488 fake_operation_factory_->created_operations()[0]->SendSuccessfulResponse(
480 kSsid, kPassword); 489 kSsid, kPassword);
481 EXPECT_EQ(ActiveHost::ActiveHostStatus::CONNECTING, 490 EXPECT_EQ(ActiveHost::ActiveHostStatus::CONNECTING,
482 fake_active_host_->GetActiveHostStatus()); 491 fake_active_host_->GetActiveHostStatus());
483 EXPECT_EQ(kSsid, fake_wifi_hotspot_connector_->most_recent_ssid()); 492 EXPECT_EQ(kSsid, fake_wifi_hotspot_connector_->most_recent_ssid());
484 EXPECT_EQ(kPassword, fake_wifi_hotspot_connector_->most_recent_password()); 493 EXPECT_EQ(kPassword, fake_wifi_hotspot_connector_->most_recent_password());
485 494
486 // While the connection to the Wi-Fi network is in progress, start a new 495 // While the connection to the Wi-Fi network is in progress, start a new
487 // connection attempt. 496 // connection attempt.
488 CallTetherDelegate(GetTetherNetworkGuid(test_devices_[1].GetDeviceId())); 497 CallConnect(GetTetherNetworkGuid(test_devices_[1].GetDeviceId()));
489 // The first connection attempt should have resulted in a connect canceled 498 // The first connection attempt should have resulted in a connect canceled
490 // error. 499 // error.
491 EXPECT_EQ(NetworkConnectionHandler::kErrorConnectCanceled, 500 EXPECT_EQ(NetworkConnectionHandler::kErrorConnectCanceled,
492 GetResultAndReset()); 501 GetResultAndReset());
493 fake_tether_host_fetcher_->InvokePendingCallbacks(); 502 fake_tether_host_fetcher_->InvokePendingCallbacks();
494 503
495 // Connect successfully to the first Wi-Fi network. Even though a temporary 504 // Connect successfully to the first Wi-Fi network. Even though a temporary
496 // connection has succeeded, the active host should be CONNECTING to device 1. 505 // connection has succeeded, the active host should be CONNECTING to device 1.
497 SuccessfullyJoinWifiNetwork(); 506 SuccessfullyJoinWifiNetwork();
498 EXPECT_EQ(ActiveHost::ActiveHostStatus::CONNECTING, 507 EXPECT_EQ(ActiveHost::ActiveHostStatus::CONNECTING,
499 fake_active_host_->GetActiveHostStatus()); 508 fake_active_host_->GetActiveHostStatus());
500 EXPECT_EQ(test_devices_[1].GetDeviceId(), 509 EXPECT_EQ(test_devices_[1].GetDeviceId(),
501 fake_active_host_->GetActiveHostDeviceId()); 510 fake_active_host_->GetActiveHostDeviceId());
502 EXPECT_EQ(GetTetherNetworkGuid(test_devices_[1].GetDeviceId()), 511 EXPECT_EQ(GetTetherNetworkGuid(test_devices_[1].GetDeviceId()),
503 fake_active_host_->GetTetherNetworkGuid()); 512 fake_active_host_->GetTetherNetworkGuid());
504 EXPECT_TRUE(fake_active_host_->GetWifiNetworkGuid().empty()); 513 EXPECT_TRUE(fake_active_host_->GetWifiNetworkGuid().empty());
505 } 514 }
506 515
507 } // namespace tether 516 } // namespace tether
508 517
509 } // namespace chromeos 518 } // namespace chromeos
OLDNEW
« no previous file with comments | « chromeos/components/tether/tether_connector.cc ('k') | chromeos/components/tether/tether_disconnector.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698