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

Side by Side Diff: device/bluetooth/bluetooth_device_unittest.cc

Issue 1256313002: bluetooth: android: Implement & test CreateGattConnection. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Split tests up into smaller tests Created 5 years, 2 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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "device/bluetooth/bluetooth_device.h" 5 #include "device/bluetooth/bluetooth_device.h"
6 6
7 #include "base/macros.h" 7 #include "base/macros.h"
8 #include "base/run_loop.h" 8 #include "base/run_loop.h"
9 #include "base/strings/utf_string_conversions.h" 9 #include "base/strings/utf_string_conversions.h"
10 #include "device/bluetooth/test/test_bluetooth_adapter_observer.h" 10 #include "device/bluetooth/test/test_bluetooth_adapter_observer.h"
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
118 } 118 }
119 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) 119 #endif // defined(OS_ANDROID) || defined(OS_MACOSX)
120 120
121 // TODO(scheib): Test with a device with no name. http://crbug.com/506415 121 // TODO(scheib): Test with a device with no name. http://crbug.com/506415
122 // BluetoothDevice::GetAddressWithLocalizedDeviceTypeName() will run, which 122 // BluetoothDevice::GetAddressWithLocalizedDeviceTypeName() will run, which
123 // requires string resources to be loaded. For that, something like 123 // requires string resources to be loaded. For that, something like
124 // InitSharedInstance must be run. See unittest files that call that. It will 124 // InitSharedInstance must be run. See unittest files that call that. It will
125 // also require build configuration to generate string resources into a .pak 125 // also require build configuration to generate string resources into a .pak
126 // file. 126 // file.
127 127
128 #if defined(OS_ANDROID)
129 // Basic CreateGattConnection test.
130 TEST_F(BluetoothTest, CreateGattConnection) {
131 InitWithFakeAdapter();
132 TestBluetoothAdapterObserver observer(adapter_);
133
134 // Get a device.
135 adapter_->StartDiscoverySession(GetDiscoverySessionCallback(),
136 GetErrorCallback());
137 base::RunLoop().RunUntilIdle();
138 DiscoverLowEnergyDevice(3);
139 base::RunLoop().RunUntilIdle();
140 BluetoothDevice* device = observer.last_device();
141
142 callback_count_ = error_callback_count_ = 0;
143 device->CreateGattConnection(GetGattConnectionCallback(),
144 GetConnectErrorCallback());
145 CompleteGattConnection(device);
146 EXPECT_EQ(1, callback_count_);
147 EXPECT_EQ(0, error_callback_count_);
148 ASSERT_EQ(1u, gatt_connections_.size());
149 EXPECT_TRUE(device->IsGattConnected());
150 EXPECT_TRUE(gatt_connections_[0]->IsConnected());
151 }
152 #endif // defined(OS_ANDROID)
153
154 #if defined(OS_ANDROID)
155 // Creates BluetoothGattConnection instances and tests that the interface
156 // functions even when some Disconnect and the BluetoothDevice is destroyed.
157 TEST_F(BluetoothTest, BluetoothGattConnection) {
158 InitWithFakeAdapter();
159 TestBluetoothAdapterObserver observer(adapter_);
160
161 // Get a device.
162 adapter_->StartDiscoverySession(GetDiscoverySessionCallback(),
163 GetErrorCallback());
164 base::RunLoop().RunUntilIdle();
165 DiscoverLowEnergyDevice(3);
166 base::RunLoop().RunUntilIdle();
167 BluetoothDevice* device = observer.last_device();
168 std::string device_address = device->GetAddress();
169
170 // CreateGattConnection
171 callback_count_ = error_callback_count_ = 0;
172 device->CreateGattConnection(GetGattConnectionCallback(),
173 GetConnectErrorCallback());
174 EXPECT_EQ(1, gatt_connection_attempt_count_);
175 CompleteGattConnection(device);
176 EXPECT_EQ(1, callback_count_);
177 EXPECT_EQ(0, error_callback_count_);
178 ASSERT_EQ(1u, gatt_connections_.size());
179 EXPECT_TRUE(device->IsGattConnected());
180 EXPECT_TRUE(gatt_connections_[0]->IsConnected());
181
182 // Connect again once already connected.
183 device->CreateGattConnection(GetGattConnectionCallback(),
184 GetConnectErrorCallback());
185 device->CreateGattConnection(GetGattConnectionCallback(),
186 GetConnectErrorCallback());
187 EXPECT_EQ(1, gatt_connection_attempt_count_);
188 EXPECT_EQ(3, callback_count_);
189 EXPECT_EQ(0, error_callback_count_);
190 ASSERT_EQ(3u, gatt_connections_.size());
191
192 // Test GetDeviceAddress
193 EXPECT_EQ(device_address, gatt_connections_[0]->GetDeviceAddress());
194
195 // Test IsConnected
196 EXPECT_TRUE(gatt_connections_[0]->IsConnected());
197 EXPECT_TRUE(gatt_connections_[1]->IsConnected());
198 EXPECT_TRUE(gatt_connections_[2]->IsConnected());
199
200 // Disconnect & Delete connection objects. Device stays connected.
201 gatt_connections_[0]->Disconnect(); // Disconnect first.
202 gatt_connections_.pop_back(); // Delete last.
203 EXPECT_FALSE(gatt_connections_[0]->IsConnected());
204 EXPECT_TRUE(gatt_connections_[1]->IsConnected());
205 EXPECT_TRUE(device->IsGattConnected());
206 EXPECT_EQ(0, gatt_disconnection_attempt_count_);
207
208 // Delete device, connection objects should all be disconnected.
209 DeleteDevice(device);
210 EXPECT_FALSE(gatt_connections_[0]->IsConnected());
211 EXPECT_FALSE(gatt_connections_[1]->IsConnected());
212
213 // Test GetDeviceAddress after device deleted.
214 EXPECT_EQ(device_address, gatt_connections_[0]->GetDeviceAddress());
215 EXPECT_EQ(device_address, gatt_connections_[1]->GetDeviceAddress());
216 }
217 #endif // defined(OS_ANDROID)
218
219 #if defined(OS_ANDROID)
220 // Calls CreateGattConnection then simulates multiple connections from platform.
221 TEST_F(BluetoothTest,
222 BluetoothGattConnection_ConnectWithMultipleOSConnections) {
223 InitWithFakeAdapter();
224 TestBluetoothAdapterObserver observer(adapter_);
225
226 // Get a device.
227 adapter_->StartDiscoverySession(GetDiscoverySessionCallback(),
228 GetErrorCallback());
229 base::RunLoop().RunUntilIdle();
230 DiscoverLowEnergyDevice(3);
231 base::RunLoop().RunUntilIdle();
232 BluetoothDevice* device = observer.last_device();
233
234 // CreateGattConnection, & multiple connections from platform only invoke
235 // callbacks once:
236 callback_count_ = error_callback_count_ = 0;
237 device->CreateGattConnection(GetGattConnectionCallback(),
238 GetConnectErrorCallback());
239 CompleteGattConnection(device);
240 CompleteGattConnection(device);
241 EXPECT_EQ(1, gatt_connection_attempt_count_);
242 EXPECT_EQ(1, callback_count_);
243 EXPECT_EQ(0, error_callback_count_);
244 EXPECT_TRUE(gatt_connections_[0]->IsConnected());
245
246 // Become disconnected:
247 CompleteGattDisconnection(device);
248 EXPECT_EQ(1, callback_count_);
249 EXPECT_EQ(0, error_callback_count_);
250 EXPECT_FALSE(gatt_connections_[0]->IsConnected());
251 }
252 #endif // defined(OS_ANDROID)
253
254 #if defined(OS_ANDROID)
255 // Calls CreateGattConnection after already connected.
256 TEST_F(BluetoothTest, BluetoothGattConnection_AlreadyConnected) {
257 InitWithFakeAdapter();
258 TestBluetoothAdapterObserver observer(adapter_);
259
260 // Get a device.
261 adapter_->StartDiscoverySession(GetDiscoverySessionCallback(),
262 GetErrorCallback());
263 base::RunLoop().RunUntilIdle();
264 DiscoverLowEnergyDevice(3);
265 base::RunLoop().RunUntilIdle();
266 BluetoothDevice* device = observer.last_device();
267
268 // Be already connected:
269 device->CreateGattConnection(GetGattConnectionCallback(),
270 GetConnectErrorCallback());
271 CompleteGattConnection(device);
272 EXPECT_TRUE(gatt_connections_[0]->IsConnected());
273
274 // Then CreateGattConnection:
275 callback_count_ = error_callback_count_ = gatt_connection_attempt_count_ =
276 gatt_disconnection_attempt_count_ = 0;
277 device->CreateGattConnection(GetGattConnectionCallback(),
278 GetConnectErrorCallback());
279 EXPECT_EQ(0, gatt_connection_attempt_count_);
280 EXPECT_EQ(1, callback_count_);
281 EXPECT_EQ(0, error_callback_count_);
282 EXPECT_TRUE(gatt_connections_[1]->IsConnected());
283 }
284 #endif // defined(OS_ANDROID)
285
286 #if defined(OS_ANDROID)
287 // Creates BluetoothGattConnection after one exists that has disconnected.
288 TEST_F(BluetoothTest,
289 BluetoothGattConnection_NewConnectionLeavesPreviousDisconnected) {
290 InitWithFakeAdapter();
291 TestBluetoothAdapterObserver observer(adapter_);
292
293 // Get a device.
294 adapter_->StartDiscoverySession(GetDiscoverySessionCallback(),
295 GetErrorCallback());
296 base::RunLoop().RunUntilIdle();
297 DiscoverLowEnergyDevice(3);
298 base::RunLoop().RunUntilIdle();
299 BluetoothDevice* device = observer.last_device();
300
301 // Create connection:
302 device->CreateGattConnection(GetGattConnectionCallback(),
303 GetConnectErrorCallback());
304 CompleteGattConnection(device);
305
306 // Disconnect connection:
307 gatt_connections_[0]->Disconnect();
308 CompleteGattDisconnection(device);
309
310 // Create 2nd connection:
311 device->CreateGattConnection(GetGattConnectionCallback(),
312 GetConnectErrorCallback());
313 CompleteGattConnection(device);
314
315 EXPECT_FALSE(gatt_connections_[0]->IsConnected())
316 << "The disconnected connection shouldn't become connected when another "
317 "connection is created.";
318 EXPECT_TRUE(gatt_connections_[1]->IsConnected());
319 }
320 #endif // defined(OS_ANDROID)
321
322 #if defined(OS_ANDROID)
323 // Deletes BluetoothGattConnection causing disconnection.
324 TEST_F(BluetoothTest, BluetoothGattConnection_DisconnectWhenObjectsDestroyed) {
325 InitWithFakeAdapter();
326 TestBluetoothAdapterObserver observer(adapter_);
327
328 // Get a device.
329 adapter_->StartDiscoverySession(GetDiscoverySessionCallback(),
330 GetErrorCallback());
331 base::RunLoop().RunUntilIdle();
332 DiscoverLowEnergyDevice(3);
333 base::RunLoop().RunUntilIdle();
334 BluetoothDevice* device = observer.last_device();
335
336 // Create multiple connections and simulate connection complete:
337 device->CreateGattConnection(GetGattConnectionCallback(),
338 GetConnectErrorCallback());
339 device->CreateGattConnection(GetGattConnectionCallback(),
340 GetConnectErrorCallback());
341 CompleteGattConnection(device);
342
343 // Delete all CreateGattConnection objects, observe disconnection:
344 callback_count_ = error_callback_count_ = 0;
345 gatt_connections_.clear();
346 EXPECT_EQ(1, gatt_disconnection_attempt_count_);
347 }
348 #endif // defined(OS_ANDROID)
349
350 #if defined(OS_ANDROID)
351 // Starts process of disconnecting and then calls BluetoothGattConnection.
352 TEST_F(BluetoothTest, BluetoothGattConnection_DisconnectInProgress) {
353 InitWithFakeAdapter();
354 TestBluetoothAdapterObserver observer(adapter_);
355
356 // Get a device.
357 adapter_->StartDiscoverySession(GetDiscoverySessionCallback(),
358 GetErrorCallback());
359 base::RunLoop().RunUntilIdle();
360 DiscoverLowEnergyDevice(3);
361 base::RunLoop().RunUntilIdle();
362 BluetoothDevice* device = observer.last_device();
363
364 // Create multiple connections and simulate connection complete:
365 device->CreateGattConnection(GetGattConnectionCallback(),
366 GetConnectErrorCallback());
367 device->CreateGattConnection(GetGattConnectionCallback(),
368 GetConnectErrorCallback());
369 CompleteGattConnection(device);
370
371 // Disconnect all CreateGattConnection objects & create a new connection.
372 // But, don't yet simulate the device disconnecting:
373 callback_count_ = error_callback_count_ = gatt_connection_attempt_count_ =
374 gatt_disconnection_attempt_count_ = 0;
375 for (BluetoothGattConnection* connection : gatt_connections_)
376 connection->Disconnect();
377 EXPECT_EQ(1, gatt_disconnection_attempt_count_);
378
379 // Create a connection.
380 device->CreateGattConnection(GetGattConnectionCallback(),
381 GetConnectErrorCallback());
382 EXPECT_EQ(0, gatt_connection_attempt_count_); // No connection attempt.
383 EXPECT_EQ(1, callback_count_); // Device is assumed still connected.
384 EXPECT_EQ(0, error_callback_count_);
385 callback_count_ = error_callback_count_ = 0;
386 EXPECT_FALSE(gatt_connections_.front()->IsConnected());
387 EXPECT_TRUE(gatt_connections_.back()->IsConnected());
388
389 // Actually disconnect:
390 CompleteGattDisconnection(device);
391 EXPECT_EQ(0, callback_count_);
392 EXPECT_EQ(0, error_callback_count_);
393 for (BluetoothGattConnection* connection : gatt_connections_)
394 EXPECT_FALSE(connection->IsConnected());
395 }
396 #endif // defined(OS_ANDROID)
397
398 #if defined(OS_ANDROID)
399 // Calls CreateGattConnection but receives notice that the device disconnected
400 // before it ever connects.
401 TEST_F(BluetoothTest, BluetoothGattConnection_SimulateDisconnect) {
402 InitWithFakeAdapter();
403 TestBluetoothAdapterObserver observer(adapter_);
404
405 // Get a device.
406 adapter_->StartDiscoverySession(GetDiscoverySessionCallback(),
407 GetErrorCallback());
408 base::RunLoop().RunUntilIdle();
409 DiscoverLowEnergyDevice(3);
410 base::RunLoop().RunUntilIdle();
411 BluetoothDevice* device = observer.last_device();
412
413 callback_count_ = error_callback_count_ = 0;
414 last_connect_error_code_ = BluetoothDevice::ERROR_UNKNOWN;
415 device->CreateGattConnection(GetGattConnectionCallback(),
416 GetConnectErrorCallback());
417 EXPECT_EQ(1, gatt_connection_attempt_count_);
418 CompleteGattDisconnection(device);
419 EXPECT_EQ(0, callback_count_);
420 EXPECT_EQ(1, error_callback_count_);
421 EXPECT_EQ(BluetoothDevice::ERROR_FAILED, last_connect_error_code_);
422 for (BluetoothGattConnection* connection : gatt_connections_)
423 EXPECT_FALSE(connection->IsConnected());
424 }
425 #endif // defined(OS_ANDROID)
426
427 #if defined(OS_ANDROID)
428 // Calls CreateGattConnection & DisconnectGatt, then simulates connection.
429 TEST_F(BluetoothTest, BluetoothGattConnection_DisconnectGatt_SimulateConnect) {
430 InitWithFakeAdapter();
431 TestBluetoothAdapterObserver observer(adapter_);
432
433 // Get a device.
434 adapter_->StartDiscoverySession(GetDiscoverySessionCallback(),
435 GetErrorCallback());
436 base::RunLoop().RunUntilIdle();
437 DiscoverLowEnergyDevice(3);
438 base::RunLoop().RunUntilIdle();
439 BluetoothDevice* device = observer.last_device();
440
441 callback_count_ = error_callback_count_ = gatt_connection_attempt_count_ =
442 gatt_disconnection_attempt_count_ = 0;
443 device->CreateGattConnection(GetGattConnectionCallback(),
444 GetConnectErrorCallback());
445 device->DisconnectGatt();
446 EXPECT_EQ(1, gatt_connection_attempt_count_);
447 EXPECT_EQ(1, gatt_disconnection_attempt_count_);
448 CompleteGattConnection(device);
449 EXPECT_EQ(1, callback_count_);
450 EXPECT_EQ(0, error_callback_count_);
451 EXPECT_TRUE(gatt_connections_.back()->IsConnected());
452 callback_count_ = error_callback_count_ = gatt_connection_attempt_count_ =
453 gatt_disconnection_attempt_count_ = 0;
454 CompleteGattDisconnection(device);
455 EXPECT_EQ(0, callback_count_);
456 EXPECT_EQ(0, error_callback_count_);
457 }
458 #endif // defined(OS_ANDROID)
459
460 #if defined(OS_ANDROID)
461 // Calls CreateGattConnection & DisconnectGatt, then simulates disconnection.
462 TEST_F(BluetoothTest,
463 BluetoothGattConnection_DisconnectGatt_SimulateDisconnect) {
464 InitWithFakeAdapter();
465 TestBluetoothAdapterObserver observer(adapter_);
466
467 // Get a device.
468 adapter_->StartDiscoverySession(GetDiscoverySessionCallback(),
469 GetErrorCallback());
470 base::RunLoop().RunUntilIdle();
471 DiscoverLowEnergyDevice(3);
472 base::RunLoop().RunUntilIdle();
473 BluetoothDevice* device = observer.last_device();
474
475 callback_count_ = error_callback_count_ = gatt_connection_attempt_count_ =
476 gatt_disconnection_attempt_count_ = 0;
477 last_connect_error_code_ = BluetoothDevice::ERROR_UNKNOWN;
478 device->CreateGattConnection(GetGattConnectionCallback(),
479 GetConnectErrorCallback());
480 device->DisconnectGatt();
481 EXPECT_EQ(1, gatt_connection_attempt_count_);
482 EXPECT_EQ(1, gatt_disconnection_attempt_count_);
483 CompleteGattDisconnection(device);
484 EXPECT_EQ(0, callback_count_);
485 EXPECT_EQ(1, error_callback_count_);
486 EXPECT_EQ(BluetoothDevice::ERROR_FAILED, last_connect_error_code_);
487 for (BluetoothGattConnection* connection : gatt_connections_)
488 EXPECT_FALSE(connection->IsConnected());
489 }
490 #endif // defined(OS_ANDROID)
491
492 #if defined(OS_ANDROID)
493 // Calls CreateGattConnection, but simulate errors connecting. Also, verifies
494 // multiple errors should only invoke callbacks once.
495 TEST_F(BluetoothTest, BluetoothGattConnection_ErrorAfterConnection) {
496 InitWithFakeAdapter();
497 TestBluetoothAdapterObserver observer(adapter_);
498
499 // Get a device.
500 adapter_->StartDiscoverySession(GetDiscoverySessionCallback(),
501 GetErrorCallback());
502 base::RunLoop().RunUntilIdle();
503 DiscoverLowEnergyDevice(3);
504 base::RunLoop().RunUntilIdle();
505 BluetoothDevice* device = observer.last_device();
506
507 callback_count_ = error_callback_count_ = 0;
508 device->CreateGattConnection(GetGattConnectionCallback(),
509 GetConnectErrorCallback());
510 EXPECT_EQ(1, gatt_connection_attempt_count_);
511 FailGattConnection(device, BluetoothDevice::ERROR_AUTH_FAILED);
512 FailGattConnection(device, BluetoothDevice::ERROR_FAILED);
513 EXPECT_EQ(0, callback_count_);
514 EXPECT_EQ(1, error_callback_count_);
515 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_FAILED, last_connect_error_code_);
516 for (BluetoothGattConnection* connection : gatt_connections_)
517 EXPECT_FALSE(connection->IsConnected());
518 }
519 #endif // defined(OS_ANDROID)
520
128 } // namespace device 521 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698