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

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

Issue 1538173003: Implementing GATT connection/disconnect on OS X. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Skip tests if Bluetooth Low Energy is not available Created 4 years, 9 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 <stddef.h> 7 #include <stddef.h>
8 8
9 #include "base/macros.h" 9 #include "base/macros.h"
10 #include "base/run_loop.h" 10 #include "base/run_loop.h"
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
115 } 115 }
116 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) 116 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN)
117 117
118 // TODO(scheib): Test with a device with no name. http://crbug.com/506415 118 // TODO(scheib): Test with a device with no name. http://crbug.com/506415
119 // BluetoothDevice::GetAddressWithLocalizedDeviceTypeName() will run, which 119 // BluetoothDevice::GetAddressWithLocalizedDeviceTypeName() will run, which
120 // requires string resources to be loaded. For that, something like 120 // requires string resources to be loaded. For that, something like
121 // InitSharedInstance must be run. See unittest files that call that. It will 121 // InitSharedInstance must be run. See unittest files that call that. It will
122 // also require build configuration to generate string resources into a .pak 122 // also require build configuration to generate string resources into a .pak
123 // file. 123 // file.
124 124
125 #if defined(OS_ANDROID) 125 #if defined(OS_ANDROID) || defined(OS_MACOSX)
126 // Basic CreateGattConnection test. 126 // Basic CreateGattConnection test.
127 TEST_F(BluetoothTest, CreateGattConnection) { 127 TEST_F(BluetoothTest, CreateGattConnection) {
128 if (!PlatformSupportsLowEnergy()) {
129 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
130 return;
131 }
128 InitWithFakeAdapter(); 132 InitWithFakeAdapter();
129 StartLowEnergyDiscoverySession(); 133 StartLowEnergyDiscoverySession();
130 BluetoothDevice* device = DiscoverLowEnergyDevice(3); 134 BluetoothDevice* device = DiscoverLowEnergyDevice(3);
131 135
132 ResetEventCounts(); 136 ResetEventCounts();
133 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), 137 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
134 GetConnectErrorCallback(Call::NOT_EXPECTED)); 138 GetConnectErrorCallback(Call::NOT_EXPECTED));
135 SimulateGattConnection(device); 139 SimulateGattConnection(device);
136 ASSERT_EQ(1u, gatt_connections_.size()); 140 ASSERT_EQ(1u, gatt_connections_.size());
137 EXPECT_TRUE(device->IsGattConnected()); 141 EXPECT_TRUE(device->IsGattConnected());
138 EXPECT_TRUE(gatt_connections_[0]->IsConnected()); 142 EXPECT_TRUE(gatt_connections_[0]->IsConnected());
139 } 143 }
140 #endif // defined(OS_ANDROID) 144 #endif // defined(OS_ANDROID) || defined(OS_MACOSX)
141 145
142 #if defined(OS_ANDROID) 146 #if defined(OS_ANDROID) || defined(OS_MACOSX)
143 // Creates BluetoothGattConnection instances and tests that the interface 147 // Creates BluetoothGattConnection instances and tests that the interface
144 // functions even when some Disconnect and the BluetoothDevice is destroyed. 148 // functions even when some Disconnect and the BluetoothDevice is destroyed.
145 TEST_F(BluetoothTest, BluetoothGattConnection) { 149 TEST_F(BluetoothTest, BluetoothGattConnection) {
150 if (!PlatformSupportsLowEnergy()) {
151 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
152 return;
153 }
146 InitWithFakeAdapter(); 154 InitWithFakeAdapter();
147 StartLowEnergyDiscoverySession(); 155 StartLowEnergyDiscoverySession();
148 BluetoothDevice* device = DiscoverLowEnergyDevice(3); 156 BluetoothDevice* device = DiscoverLowEnergyDevice(3);
149 std::string device_address = device->GetAddress(); 157 std::string device_address = device->GetAddress();
150 158
151 // CreateGattConnection 159 // CreateGattConnection
152 ResetEventCounts(); 160 ResetEventCounts();
153 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), 161 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
154 GetConnectErrorCallback(Call::NOT_EXPECTED)); 162 GetConnectErrorCallback(Call::NOT_EXPECTED));
155 EXPECT_EQ(1, gatt_connection_attempts_); 163 EXPECT_EQ(1, gatt_connection_attempts_);
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
191 gatt_disconnection_attempts_ = 0; 199 gatt_disconnection_attempts_ = 0;
192 DeleteDevice(device); 200 DeleteDevice(device);
193 EXPECT_EQ(1, gatt_disconnection_attempts_); 201 EXPECT_EQ(1, gatt_disconnection_attempts_);
194 EXPECT_FALSE(gatt_connections_[0]->IsConnected()); 202 EXPECT_FALSE(gatt_connections_[0]->IsConnected());
195 EXPECT_FALSE(gatt_connections_[1]->IsConnected()); 203 EXPECT_FALSE(gatt_connections_[1]->IsConnected());
196 204
197 // Test GetDeviceAddress after device deleted. 205 // Test GetDeviceAddress after device deleted.
198 EXPECT_EQ(device_address, gatt_connections_[0]->GetDeviceAddress()); 206 EXPECT_EQ(device_address, gatt_connections_[0]->GetDeviceAddress());
199 EXPECT_EQ(device_address, gatt_connections_[1]->GetDeviceAddress()); 207 EXPECT_EQ(device_address, gatt_connections_[1]->GetDeviceAddress());
200 } 208 }
201 #endif // defined(OS_ANDROID) 209 #endif // defined(OS_ANDROID) || defined(OS_MACOSX)
202 210
203 #if defined(OS_ANDROID) 211 #if defined(OS_ANDROID) || defined(OS_MACOSX)
204 // Calls CreateGattConnection then simulates multiple connections from platform. 212 // Calls CreateGattConnection then simulates multiple connections from platform.
205 TEST_F(BluetoothTest, 213 TEST_F(BluetoothTest,
206 BluetoothGattConnection_ConnectWithMultipleOSConnections) { 214 BluetoothGattConnection_ConnectWithMultipleOSConnections) {
215 if (!PlatformSupportsLowEnergy()) {
216 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
217 return;
218 }
207 InitWithFakeAdapter(); 219 InitWithFakeAdapter();
208 StartLowEnergyDiscoverySession(); 220 StartLowEnergyDiscoverySession();
209 BluetoothDevice* device = DiscoverLowEnergyDevice(3); 221 BluetoothDevice* device = DiscoverLowEnergyDevice(3);
210 222
211 // CreateGattConnection, & multiple connections from platform only invoke 223 // CreateGattConnection, & multiple connections from platform only invoke
212 // callbacks once: 224 // callbacks once:
213 ResetEventCounts(); 225 ResetEventCounts();
214 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), 226 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
215 GetConnectErrorCallback(Call::NOT_EXPECTED)); 227 GetConnectErrorCallback(Call::NOT_EXPECTED));
216 SimulateGattConnection(device); 228 SimulateGattConnection(device);
217 SimulateGattConnection(device); 229 SimulateGattConnection(device);
218 EXPECT_EQ(1, gatt_connection_attempts_); 230 EXPECT_EQ(1, gatt_connection_attempts_);
219 EXPECT_EQ(1, callback_count_); 231 EXPECT_EQ(1, callback_count_);
220 EXPECT_EQ(0, error_callback_count_); 232 EXPECT_EQ(0, error_callback_count_);
221 EXPECT_TRUE(gatt_connections_[0]->IsConnected()); 233 EXPECT_TRUE(gatt_connections_[0]->IsConnected());
222 234
223 // Become disconnected: 235 // Become disconnected:
224 SimulateGattDisconnection(device); 236 SimulateGattDisconnection(device);
225 EXPECT_FALSE(gatt_connections_[0]->IsConnected()); 237 EXPECT_FALSE(gatt_connections_[0]->IsConnected());
226 } 238 }
227 #endif // defined(OS_ANDROID) 239 #endif // defined(OS_ANDROID) || defined(OS_MACOSX)
228 240
229 #if defined(OS_ANDROID) 241 #if defined(OS_ANDROID) || defined(OS_MACOSX)
230 // Calls CreateGattConnection after already connected. 242 // Calls CreateGattConnection after already connected.
231 TEST_F(BluetoothTest, BluetoothGattConnection_AlreadyConnected) { 243 TEST_F(BluetoothTest, BluetoothGattConnection_AlreadyConnected) {
244 if (!PlatformSupportsLowEnergy()) {
245 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
246 return;
247 }
232 InitWithFakeAdapter(); 248 InitWithFakeAdapter();
233 StartLowEnergyDiscoverySession(); 249 StartLowEnergyDiscoverySession();
234 BluetoothDevice* device = DiscoverLowEnergyDevice(3); 250 BluetoothDevice* device = DiscoverLowEnergyDevice(3);
235 251
236 // Be already connected: 252 // Be already connected:
237 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), 253 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
238 GetConnectErrorCallback(Call::NOT_EXPECTED)); 254 GetConnectErrorCallback(Call::NOT_EXPECTED));
239 SimulateGattConnection(device); 255 SimulateGattConnection(device);
240 EXPECT_TRUE(gatt_connections_[0]->IsConnected()); 256 EXPECT_TRUE(gatt_connections_[0]->IsConnected());
241 257
242 // Then CreateGattConnection: 258 // Then CreateGattConnection:
243 ResetEventCounts(); 259 ResetEventCounts();
244 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), 260 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
245 GetConnectErrorCallback(Call::NOT_EXPECTED)); 261 GetConnectErrorCallback(Call::NOT_EXPECTED));
246 EXPECT_EQ(0, gatt_connection_attempts_); 262 EXPECT_EQ(0, gatt_connection_attempts_);
247 EXPECT_TRUE(gatt_connections_[1]->IsConnected()); 263 EXPECT_TRUE(gatt_connections_[1]->IsConnected());
248 } 264 }
249 #endif // defined(OS_ANDROID) 265 #endif // defined(OS_ANDROID) || defined(OS_MACOSX)
250 266
251 #if defined(OS_ANDROID) 267 #if defined(OS_ANDROID) || defined(OS_MACOSX)
252 // Creates BluetoothGattConnection after one exists that has disconnected. 268 // Creates BluetoothGattConnection after one exists that has disconnected.
253 TEST_F(BluetoothTest, 269 TEST_F(BluetoothTest,
254 BluetoothGattConnection_NewConnectionLeavesPreviousDisconnected) { 270 BluetoothGattConnection_NewConnectionLeavesPreviousDisconnected) {
271 if (!PlatformSupportsLowEnergy()) {
272 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
273 return;
274 }
255 InitWithFakeAdapter(); 275 InitWithFakeAdapter();
256 StartLowEnergyDiscoverySession(); 276 StartLowEnergyDiscoverySession();
257 BluetoothDevice* device = DiscoverLowEnergyDevice(3); 277 BluetoothDevice* device = DiscoverLowEnergyDevice(3);
258 278
259 // Create connection: 279 // Create connection:
260 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), 280 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
261 GetConnectErrorCallback(Call::NOT_EXPECTED)); 281 GetConnectErrorCallback(Call::NOT_EXPECTED));
262 SimulateGattConnection(device); 282 SimulateGattConnection(device);
263 283
264 // Disconnect connection: 284 // Disconnect connection:
265 gatt_connections_[0]->Disconnect(); 285 gatt_connections_[0]->Disconnect();
266 SimulateGattDisconnection(device); 286 SimulateGattDisconnection(device);
267 287
268 // Create 2nd connection: 288 // Create 2nd connection:
269 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), 289 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
270 GetConnectErrorCallback(Call::NOT_EXPECTED)); 290 GetConnectErrorCallback(Call::NOT_EXPECTED));
271 SimulateGattConnection(device); 291 SimulateGattConnection(device);
272 292
273 EXPECT_FALSE(gatt_connections_[0]->IsConnected()) 293 EXPECT_FALSE(gatt_connections_[0]->IsConnected())
274 << "The disconnected connection shouldn't become connected when another " 294 << "The disconnected connection shouldn't become connected when another "
275 "connection is created."; 295 "connection is created.";
276 EXPECT_TRUE(gatt_connections_[1]->IsConnected()); 296 EXPECT_TRUE(gatt_connections_[1]->IsConnected());
277 } 297 }
278 #endif // defined(OS_ANDROID) 298 #endif // defined(OS_ANDROID) || defined(OS_MACOSX)
279 299
280 #if defined(OS_ANDROID) 300 #if defined(OS_ANDROID) || defined(OS_MACOSX)
281 // Deletes BluetoothGattConnection causing disconnection. 301 // Deletes BluetoothGattConnection causing disconnection.
282 TEST_F(BluetoothTest, BluetoothGattConnection_DisconnectWhenObjectsDestroyed) { 302 TEST_F(BluetoothTest, BluetoothGattConnection_DisconnectWhenObjectsDestroyed) {
303 if (!PlatformSupportsLowEnergy()) {
304 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
305 return;
306 }
283 InitWithFakeAdapter(); 307 InitWithFakeAdapter();
284 StartLowEnergyDiscoverySession(); 308 StartLowEnergyDiscoverySession();
285 BluetoothDevice* device = DiscoverLowEnergyDevice(3); 309 BluetoothDevice* device = DiscoverLowEnergyDevice(3);
286 310
287 // Create multiple connections and simulate connection complete: 311 // Create multiple connections and simulate connection complete:
288 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), 312 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
289 GetConnectErrorCallback(Call::NOT_EXPECTED)); 313 GetConnectErrorCallback(Call::NOT_EXPECTED));
290 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), 314 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
291 GetConnectErrorCallback(Call::NOT_EXPECTED)); 315 GetConnectErrorCallback(Call::NOT_EXPECTED));
292 SimulateGattConnection(device); 316 SimulateGattConnection(device);
293 317
294 // Delete all CreateGattConnection objects, observe disconnection: 318 // Delete all CreateGattConnection objects, observe disconnection:
295 ResetEventCounts(); 319 ResetEventCounts();
296 gatt_connections_.clear(); 320 gatt_connections_.clear();
297 EXPECT_EQ(1, gatt_disconnection_attempts_); 321 EXPECT_EQ(1, gatt_disconnection_attempts_);
298 } 322 }
299 #endif // defined(OS_ANDROID) 323 #endif // defined(OS_ANDROID) || defined(OS_MACOSX)
300 324
301 #if defined(OS_ANDROID) 325 #if defined(OS_ANDROID) || defined(OS_MACOSX)
302 // Starts process of disconnecting and then calls BluetoothGattConnection. 326 // Starts process of disconnecting and then calls BluetoothGattConnection.
303 TEST_F(BluetoothTest, BluetoothGattConnection_DisconnectInProgress) { 327 TEST_F(BluetoothTest, BluetoothGattConnection_DisconnectInProgress) {
328 if (!PlatformSupportsLowEnergy()) {
329 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
330 return;
331 }
304 InitWithFakeAdapter(); 332 InitWithFakeAdapter();
305 StartLowEnergyDiscoverySession(); 333 StartLowEnergyDiscoverySession();
306 BluetoothDevice* device = DiscoverLowEnergyDevice(3); 334 BluetoothDevice* device = DiscoverLowEnergyDevice(3);
307 335
308 // Create multiple connections and simulate connection complete: 336 // Create multiple connections and simulate connection complete:
309 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), 337 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
310 GetConnectErrorCallback(Call::NOT_EXPECTED)); 338 GetConnectErrorCallback(Call::NOT_EXPECTED));
311 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), 339 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
312 GetConnectErrorCallback(Call::NOT_EXPECTED)); 340 GetConnectErrorCallback(Call::NOT_EXPECTED));
313 SimulateGattConnection(device); 341 SimulateGattConnection(device);
(...skipping 12 matching lines...) Expand all
326 EXPECT_EQ(1, callback_count_); // Device is assumed still connected. 354 EXPECT_EQ(1, callback_count_); // Device is assumed still connected.
327 EXPECT_EQ(0, error_callback_count_); 355 EXPECT_EQ(0, error_callback_count_);
328 EXPECT_FALSE(gatt_connections_.front()->IsConnected()); 356 EXPECT_FALSE(gatt_connections_.front()->IsConnected());
329 EXPECT_TRUE(gatt_connections_.back()->IsConnected()); 357 EXPECT_TRUE(gatt_connections_.back()->IsConnected());
330 358
331 // Actually disconnect: 359 // Actually disconnect:
332 SimulateGattDisconnection(device); 360 SimulateGattDisconnection(device);
333 for (BluetoothGattConnection* connection : gatt_connections_) 361 for (BluetoothGattConnection* connection : gatt_connections_)
334 EXPECT_FALSE(connection->IsConnected()); 362 EXPECT_FALSE(connection->IsConnected());
335 } 363 }
336 #endif // defined(OS_ANDROID) 364 #endif // defined(OS_ANDROID) || defined(OS_MACOSX)
337 365
338 #if defined(OS_ANDROID) 366 #if defined(OS_ANDROID) || defined(OS_MACOSX)
339 // Calls CreateGattConnection but receives notice that the device disconnected 367 // Calls CreateGattConnection but receives notice that the device disconnected
340 // before it ever connects. 368 // before it ever connects.
341 TEST_F(BluetoothTest, BluetoothGattConnection_SimulateDisconnect) { 369 TEST_F(BluetoothTest, BluetoothGattConnection_SimulateDisconnect) {
370 if (!PlatformSupportsLowEnergy()) {
371 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
372 return;
373 }
342 InitWithFakeAdapter(); 374 InitWithFakeAdapter();
343 StartLowEnergyDiscoverySession(); 375 StartLowEnergyDiscoverySession();
344 BluetoothDevice* device = DiscoverLowEnergyDevice(3); 376 BluetoothDevice* device = DiscoverLowEnergyDevice(3);
345 377
346 ResetEventCounts(); 378 ResetEventCounts();
347 device->CreateGattConnection(GetGattConnectionCallback(Call::NOT_EXPECTED), 379 device->CreateGattConnection(GetGattConnectionCallback(Call::NOT_EXPECTED),
348 GetConnectErrorCallback(Call::EXPECTED)); 380 GetConnectErrorCallback(Call::EXPECTED));
349 EXPECT_EQ(1, gatt_connection_attempts_); 381 EXPECT_EQ(1, gatt_connection_attempts_);
350 SimulateGattDisconnection(device); 382 SimulateGattDisconnection(device);
351 EXPECT_EQ(BluetoothDevice::ERROR_FAILED, last_connect_error_code_); 383 EXPECT_EQ(BluetoothDevice::ERROR_FAILED, last_connect_error_code_);
352 for (BluetoothGattConnection* connection : gatt_connections_) 384 for (BluetoothGattConnection* connection : gatt_connections_)
353 EXPECT_FALSE(connection->IsConnected()); 385 EXPECT_FALSE(connection->IsConnected());
354 } 386 }
355 #endif // defined(OS_ANDROID) 387 #endif // defined(OS_ANDROID) || defined(OS_MACOSX)
356 388
357 #if defined(OS_ANDROID) 389 #if defined(OS_ANDROID) || defined(OS_MACOSX)
358 // Calls CreateGattConnection & DisconnectGatt, then simulates connection. 390 // Calls CreateGattConnection & DisconnectGatt, then simulates connection.
359 TEST_F(BluetoothTest, BluetoothGattConnection_DisconnectGatt_SimulateConnect) { 391 TEST_F(BluetoothTest, BluetoothGattConnection_DisconnectGatt_SimulateConnect) {
392 if (!PlatformSupportsLowEnergy()) {
393 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
394 return;
395 }
360 InitWithFakeAdapter(); 396 InitWithFakeAdapter();
361 StartLowEnergyDiscoverySession(); 397 StartLowEnergyDiscoverySession();
362 BluetoothDevice* device = DiscoverLowEnergyDevice(3); 398 BluetoothDevice* device = DiscoverLowEnergyDevice(3);
363 399
364 ResetEventCounts(); 400 ResetEventCounts();
365 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), 401 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
366 GetConnectErrorCallback(Call::NOT_EXPECTED)); 402 GetConnectErrorCallback(Call::NOT_EXPECTED));
367 device->DisconnectGatt(); 403 device->DisconnectGatt();
368 EXPECT_EQ(1, gatt_connection_attempts_); 404 EXPECT_EQ(1, gatt_connection_attempts_);
369 EXPECT_EQ(1, gatt_disconnection_attempts_); 405 EXPECT_EQ(1, gatt_disconnection_attempts_);
370 SimulateGattConnection(device); 406 SimulateGattConnection(device);
371 EXPECT_EQ(1, callback_count_); 407 EXPECT_EQ(1, callback_count_);
372 EXPECT_EQ(0, error_callback_count_); 408 EXPECT_EQ(0, error_callback_count_);
373 EXPECT_TRUE(gatt_connections_.back()->IsConnected()); 409 EXPECT_TRUE(gatt_connections_.back()->IsConnected());
374 ResetEventCounts(); 410 ResetEventCounts();
375 SimulateGattDisconnection(device); 411 SimulateGattDisconnection(device);
376 EXPECT_EQ(0, callback_count_); 412 EXPECT_EQ(0, callback_count_);
377 EXPECT_EQ(0, error_callback_count_); 413 EXPECT_EQ(0, error_callback_count_);
378 } 414 }
379 #endif // defined(OS_ANDROID) 415 #endif // defined(OS_ANDROID) || defined(OS_MACOSX)
380 416
381 #if defined(OS_ANDROID) 417 #if defined(OS_ANDROID) || defined(OS_MACOSX)
382 // Calls CreateGattConnection & DisconnectGatt, then simulates disconnection. 418 // Calls CreateGattConnection & DisconnectGatt, then simulates disconnection.
383 TEST_F(BluetoothTest, 419 TEST_F(BluetoothTest,
384 BluetoothGattConnection_DisconnectGatt_SimulateDisconnect) { 420 BluetoothGattConnection_DisconnectGatt_SimulateDisconnect) {
421 if (!PlatformSupportsLowEnergy()) {
422 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
423 return;
424 }
385 InitWithFakeAdapter(); 425 InitWithFakeAdapter();
386 StartLowEnergyDiscoverySession(); 426 StartLowEnergyDiscoverySession();
387 BluetoothDevice* device = DiscoverLowEnergyDevice(3); 427 BluetoothDevice* device = DiscoverLowEnergyDevice(3);
388 428
389 ResetEventCounts(); 429 ResetEventCounts();
390 device->CreateGattConnection(GetGattConnectionCallback(Call::NOT_EXPECTED), 430 device->CreateGattConnection(GetGattConnectionCallback(Call::NOT_EXPECTED),
391 GetConnectErrorCallback(Call::EXPECTED)); 431 GetConnectErrorCallback(Call::EXPECTED));
392 device->DisconnectGatt(); 432 device->DisconnectGatt();
393 EXPECT_EQ(1, gatt_connection_attempts_); 433 EXPECT_EQ(1, gatt_connection_attempts_);
394 EXPECT_EQ(1, gatt_disconnection_attempts_); 434 EXPECT_EQ(1, gatt_disconnection_attempts_);
395 SimulateGattDisconnection(device); 435 SimulateGattDisconnection(device);
396 EXPECT_EQ(BluetoothDevice::ERROR_FAILED, last_connect_error_code_); 436 EXPECT_EQ(BluetoothDevice::ERROR_FAILED, last_connect_error_code_);
397 for (BluetoothGattConnection* connection : gatt_connections_) 437 for (BluetoothGattConnection* connection : gatt_connections_)
398 EXPECT_FALSE(connection->IsConnected()); 438 EXPECT_FALSE(connection->IsConnected());
399 } 439 }
400 #endif // defined(OS_ANDROID) 440 #endif // defined(OS_ANDROID) || defined(OS_MACOSX)
401 441
402 #if defined(OS_ANDROID) 442 #if defined(OS_ANDROID)
403 // Calls CreateGattConnection & DisconnectGatt, then checks that gatt services 443 // Calls CreateGattConnection & DisconnectGatt, then checks that gatt services
404 // have been cleaned up. 444 // have been cleaned up.
405 TEST_F(BluetoothTest, BluetoothGattConnection_DisconnectGatt_Cleanup) { 445 TEST_F(BluetoothTest, BluetoothGattConnection_DisconnectGatt_Cleanup) {
446 if (!PlatformSupportsLowEnergy()) {
447 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
448 return;
449 }
406 InitWithFakeAdapter(); 450 InitWithFakeAdapter();
407 StartLowEnergyDiscoverySession(); 451 StartLowEnergyDiscoverySession();
408 BluetoothDevice* device = DiscoverLowEnergyDevice(3); 452 BluetoothDevice* device = DiscoverLowEnergyDevice(3);
409 EXPECT_FALSE(device->IsConnected()); 453 EXPECT_FALSE(device->IsConnected());
410 454
411 // Connect to the device 455 // Connect to the device
412 ResetEventCounts(); 456 ResetEventCounts();
413 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), 457 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
414 GetConnectErrorCallback(Call::NOT_EXPECTED)); 458 GetConnectErrorCallback(Call::NOT_EXPECTED));
415 TestBluetoothAdapterObserver observer(adapter_); 459 TestBluetoothAdapterObserver observer(adapter_);
(...skipping 29 matching lines...) Expand all
445 EXPECT_TRUE(device->IsGattServicesDiscoveryComplete()); 489 EXPECT_TRUE(device->IsGattServicesDiscoveryComplete());
446 EXPECT_EQ(1u, device->GetGattServices().size()); 490 EXPECT_EQ(1u, device->GetGattServices().size());
447 EXPECT_EQ(2, observer.gatt_services_discovered_count()); 491 EXPECT_EQ(2, observer.gatt_services_discovered_count());
448 } 492 }
449 #endif // defined(OS_ANDROID) 493 #endif // defined(OS_ANDROID)
450 494
451 #if defined(OS_ANDROID) 495 #if defined(OS_ANDROID)
452 // Calls CreateGattConnection, but simulate errors connecting. Also, verifies 496 // Calls CreateGattConnection, but simulate errors connecting. Also, verifies
453 // multiple errors should only invoke callbacks once. 497 // multiple errors should only invoke callbacks once.
454 TEST_F(BluetoothTest, BluetoothGattConnection_ErrorAfterConnection) { 498 TEST_F(BluetoothTest, BluetoothGattConnection_ErrorAfterConnection) {
499 if (!PlatformSupportsLowEnergy()) {
500 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
501 return;
502 }
455 InitWithFakeAdapter(); 503 InitWithFakeAdapter();
456 StartLowEnergyDiscoverySession(); 504 StartLowEnergyDiscoverySession();
457 BluetoothDevice* device = DiscoverLowEnergyDevice(3); 505 BluetoothDevice* device = DiscoverLowEnergyDevice(3);
458 506
459 ResetEventCounts(); 507 ResetEventCounts();
460 device->CreateGattConnection(GetGattConnectionCallback(Call::NOT_EXPECTED), 508 device->CreateGattConnection(GetGattConnectionCallback(Call::NOT_EXPECTED),
461 GetConnectErrorCallback(Call::EXPECTED)); 509 GetConnectErrorCallback(Call::EXPECTED));
462 EXPECT_EQ(1, gatt_connection_attempts_); 510 EXPECT_EQ(1, gatt_connection_attempts_);
463 SimulateGattConnectionError(device, BluetoothDevice::ERROR_AUTH_FAILED); 511 SimulateGattConnectionError(device, BluetoothDevice::ERROR_AUTH_FAILED);
464 SimulateGattConnectionError(device, BluetoothDevice::ERROR_FAILED); 512 SimulateGattConnectionError(device, BluetoothDevice::ERROR_FAILED);
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
526 std::string service_id2 = device->GetGattServices()[1]->GetIdentifier(); 574 std::string service_id2 = device->GetGattServices()[1]->GetIdentifier();
527 std::string service_id3 = device->GetGattServices()[2]->GetIdentifier(); 575 std::string service_id3 = device->GetGattServices()[2]->GetIdentifier();
528 EXPECT_TRUE(device->GetGattService(service_id1)); 576 EXPECT_TRUE(device->GetGattService(service_id1));
529 EXPECT_TRUE(device->GetGattService(service_id2)); 577 EXPECT_TRUE(device->GetGattService(service_id2));
530 EXPECT_TRUE(device->GetGattService(service_id3)); 578 EXPECT_TRUE(device->GetGattService(service_id3));
531 } 579 }
532 #endif // defined(OS_ANDROID) || defined(OS_WIN) 580 #endif // defined(OS_ANDROID) || defined(OS_WIN)
533 581
534 #if defined(OS_ANDROID) 582 #if defined(OS_ANDROID)
535 TEST_F(BluetoothTest, GetGattServices_DiscoveryError) { 583 TEST_F(BluetoothTest, GetGattServices_DiscoveryError) {
584 if (!PlatformSupportsLowEnergy()) {
585 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
586 return;
587 }
536 InitWithFakeAdapter(); 588 InitWithFakeAdapter();
537 StartLowEnergyDiscoverySession(); 589 StartLowEnergyDiscoverySession();
538 BluetoothDevice* device = DiscoverLowEnergyDevice(3); 590 BluetoothDevice* device = DiscoverLowEnergyDevice(3);
539 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), 591 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
540 GetConnectErrorCallback(Call::NOT_EXPECTED)); 592 GetConnectErrorCallback(Call::NOT_EXPECTED));
541 ResetEventCounts(); 593 ResetEventCounts();
542 SimulateGattConnection(device); 594 SimulateGattConnection(device);
543 EXPECT_EQ(1, gatt_discovery_attempts_); 595 EXPECT_EQ(1, gatt_discovery_attempts_);
544 596
545 SimulateGattServicesDiscoveryError(device); 597 SimulateGattServicesDiscoveryError(device);
546 EXPECT_EQ(0u, device->GetGattServices().size()); 598 EXPECT_EQ(0u, device->GetGattServices().size());
547 } 599 }
548 #endif // defined(OS_ANDROID) 600 #endif // defined(OS_ANDROID)
549 601
550 } // namespace device 602 } // namespace device
OLDNEW
« no previous file with comments | « device/bluetooth/bluetooth_adapter_mac_unittest.mm ('k') | device/bluetooth/bluetooth_low_energy_central_manager_delegate.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698