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

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

Issue 2638653002: Bluetooth: macOS: DidModifyServices can happens while scanning (Closed)
Patch Set: More unit tests Created 3 years, 8 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 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 "base/run_loop.h" 5 #include "base/run_loop.h"
6 #include "build/build_config.h" 6 #include "build/build_config.h"
7 #include "device/bluetooth/bluetooth_gatt_service.h" 7 #include "device/bluetooth/bluetooth_gatt_service.h"
8 #include "device/bluetooth/bluetooth_remote_gatt_characteristic.h" 8 #include "device/bluetooth/bluetooth_remote_gatt_characteristic.h"
9 #include "device/bluetooth/test/test_bluetooth_adapter_observer.h" 9 #include "device/bluetooth/test/test_bluetooth_adapter_observer.h"
10 #include "testing/gtest/include/gtest/gtest.h" 10 #include "testing/gtest/include/gtest/gtest.h"
(...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after
274 // Simulate remove of a primary service. 274 // Simulate remove of a primary service.
275 BluetoothRemoteGattService* service1 = device->GetGattServices()[0]; 275 BluetoothRemoteGattService* service1 = device->GetGattServices()[0];
276 BluetoothRemoteGattService* service2 = device->GetGattServices()[1]; 276 BluetoothRemoteGattService* service2 = device->GetGattServices()[1];
277 std::string removed_service = service1->GetIdentifier(); 277 std::string removed_service = service1->GetIdentifier();
278 SimulateGattServiceRemoved(device->GetGattService(removed_service)); 278 SimulateGattServiceRemoved(device->GetGattService(removed_service));
279 EXPECT_EQ(1, observer.gatt_service_removed_count()); 279 EXPECT_EQ(1, observer.gatt_service_removed_count());
280 EXPECT_EQ(1u, device->GetGattServices().size()); 280 EXPECT_EQ(1u, device->GetGattServices().size());
281 EXPECT_FALSE(device->GetGattService(removed_service)); 281 EXPECT_FALSE(device->GetGattService(removed_service));
282 EXPECT_EQ(device->GetGattServices()[0], service2); 282 EXPECT_EQ(device->GetGattServices()[0], service2);
283 } 283 }
284 #endif // defined(OS_WIN) || defined(OS_MACOSX) 284 #endif // defined(OS_MACOSX) || defined(OS_WIN)
285
286 #if defined(OS_MACOSX)
287 // Tests to receive a services changed notification from macOS, while
288 // discovering characteristics. The gatt device should scan again for services
289 // and characteristics, before scanning for descriptors. Only after the gatt
290 // service changed notification should be sent.
291 // Android: This test doesn't apply to Android because there is no services
292 // changed event that could arrive during a discovery procedure.
293 TEST_F(BluetoothRemoteGattServiceTest,
294 SimulateDeviceModificationWhileDiscoveringCharacteristics) {
295 if (!PlatformSupportsLowEnergy()) {
296 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
297 return;
298 }
299 InitWithFakeAdapter();
300 StartLowEnergyDiscoverySession();
301 BluetoothDevice* device = SimulateLowEnergyDevice(3);
302 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
303 GetConnectErrorCallback(Call::NOT_EXPECTED));
304
305 TestBluetoothAdapterObserver observer(adapter_);
306
307 // Starts first discovery process.
308 SimulateGattConnection(device);
309 AddServicesToDevice(device, {kTestUUIDHeartRate});
310 SimulateDidDiscoverServices(device);
311 EXPECT_EQ(1u, device->GetGattServices().size());
312 BluetoothRemoteGattService* service = device->GetGattServices()[0];
313 std::string characteristic_uuid1 = "11111111-0000-1000-8000-00805f9b34fb";
314 AddCharacteristicToService(service, characteristic_uuid1, /* properties */ 0);
315 // Now waiting for characteristic discovery.
316
317 // Starts second discovery process.
318 SimulateGattServicesChanged(device);
319 SimulateDidDiscoverServices(device);
320 // Now waiting for the second characteristic discovery.
321
322 // First system call to -[id<CBPeripheralDelegate>
323 // peripheral:didDiscoverCharacteristicsForService:error:]
324 SimulateDidDiscoverCharacteristics(service);
325 EXPECT_EQ(0, observer.gatt_service_changed_count());
326 EXPECT_EQ(1u, service->GetCharacteristics().size());
327
328 // Finish discovery process.
329 std::string characteristic_uuid2 = "22222222-0000-1000-8000-00805f9b34fb";
330 AddCharacteristicToService(service, characteristic_uuid2, /* properties */ 0);
331 // Second system call to -[id<CBPeripheralDelegate>
332 // peripheral:didDiscoverCharacteristicsForService:error:]
333 SimulateDidDiscoverCharacteristics(service);
334 EXPECT_EQ(2u, service->GetCharacteristics().size());
335 EXPECT_EQ(0, observer.gatt_service_changed_count());
336 BluetoothRemoteGattCharacteristic* characteristic1 =
337 service->GetCharacteristics()[0];
338 BluetoothRemoteGattCharacteristic* characteristic2 =
339 service->GetCharacteristics()[1];
340 if (characteristic1->GetUUID().canonical_value() == characteristic_uuid2) {
341 BluetoothRemoteGattCharacteristic* tmp = characteristic1;
342 characteristic1 = characteristic2;
343 characteristic2 = tmp;
344 }
345 EXPECT_EQ(characteristic_uuid1, characteristic1->GetUUID().canonical_value());
346 EXPECT_EQ(characteristic_uuid2, characteristic2->GetUUID().canonical_value());
347 SimulateDidDiscoverDescriptors(characteristic1);
348 SimulateDidDiscoverDescriptors(characteristic2);
349 EXPECT_EQ(1, observer.gatt_service_changed_count());
350 }
351 #endif // defined(OS_MACOSX)
352
353 #if defined(OS_MACOSX)
354 // Simulates to receive an extra discovery characteristic notifications from
355 // macOS. Those notifications should be ignored.
356 // Android: This test doesn't apply to Android because there is no services
357 // changed event that could arrive during a discovery procedure.
358 TEST_F(BluetoothRemoteGattServiceTest, ExtraDidDiscoverServicesCall) {
359 if (!PlatformSupportsLowEnergy()) {
360 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
361 return;
362 }
363 InitWithFakeAdapter();
364 StartLowEnergyDiscoverySession();
365 BluetoothDevice* device = SimulateLowEnergyDevice(3);
366 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
367 GetConnectErrorCallback(Call::NOT_EXPECTED));
368
369 TestBluetoothAdapterObserver observer(adapter_);
370
371 // Starts first discovery process.
372 SimulateGattConnection(device);
373 AddServicesToDevice(device, {kTestUUIDHeartRate});
374 SimulateDidDiscoverServices(device);
375 EXPECT_EQ(1u, device->GetGattServices().size());
376 BluetoothRemoteGattService* service = device->GetGattServices()[0];
377 std::string characteristic_uuid = "11111111-0000-1000-8000-00805f9b34fb";
378 AddCharacteristicToService(service, characteristic_uuid, /* properties */ 0);
379 SimulateDidDiscoverCharacteristics(service);
380 EXPECT_EQ(1u, service->GetCharacteristics().size());
381 BluetoothRemoteGattCharacteristic* characteristic =
382 service->GetCharacteristics()[0];
383 std::string descriptor_uuid = "22222222-0000-1000-8000-00805f9b34fb";
384 AddDescriptorToCharacteristic(characteristic, descriptor_uuid);
385 SimulateDidDiscoverDescriptors(characteristic);
386 EXPECT_EQ(1, observer.gatt_service_changed_count());
387 EXPECT_EQ(1u, service->GetCharacteristics().size());
388 EXPECT_EQ(1u, characteristic->GetDescriptors().size());
389
390 observer.Reset();
391 SimulateDidDiscoverServices(device); // Extra system call.
392 SimulateGattServicesChanged(device);
393 SimulateDidDiscoverServices(device);
394 SimulateDidDiscoverServices(device); // Extra system call.
395 SimulateDidDiscoverCharacteristics(service);
396 SimulateDidDiscoverServices(device); // Extra system call.
397 SimulateDidDiscoverDescriptors(characteristic);
398 SimulateDidDiscoverServices(device); // Extra system call.
399 EXPECT_EQ(2, observer.device_changed_count());
400 }
401 #endif // defined(OS_MACOSX)
402
403 #if defined(OS_MACOSX)
404 // Simulates to receive an extra discovery characteristic notifications from
405 // macOS. Those notifications should be ignored.
406 // Android: This test doesn't apply to Android because there is no services
407 // changed event that could arrive during a discovery procedure.
408 TEST_F(BluetoothRemoteGattServiceTest, ExtraDidDiscoverCharacteristicsCall) {
409 if (!PlatformSupportsLowEnergy()) {
410 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
411 return;
412 }
413 InitWithFakeAdapter();
414 StartLowEnergyDiscoverySession();
415 BluetoothDevice* device = SimulateLowEnergyDevice(3);
416 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
417 GetConnectErrorCallback(Call::NOT_EXPECTED));
418
419 TestBluetoothAdapterObserver observer(adapter_);
420
421 // Starts first discovery process.
422 SimulateGattConnection(device);
423 AddServicesToDevice(device, {kTestUUIDHeartRate});
424 SimulateDidDiscoverServices(device);
425 EXPECT_EQ(1u, device->GetGattServices().size());
426 BluetoothRemoteGattService* service = device->GetGattServices()[0];
427 std::string characteristic_uuid = "11111111-0000-1000-8000-00805f9b34fb";
428 AddCharacteristicToService(service, characteristic_uuid, /* properties */ 0);
429 SimulateDidDiscoverCharacteristics(service);
430 EXPECT_EQ(1u, service->GetCharacteristics().size());
431 BluetoothRemoteGattCharacteristic* characteristic =
432 service->GetCharacteristics()[0];
433 std::string descriptor_uuid = "22222222-0000-1000-8000-00805f9b34fb";
434 AddDescriptorToCharacteristic(characteristic, descriptor_uuid);
435 SimulateDidDiscoverDescriptors(characteristic);
436 EXPECT_EQ(1, observer.gatt_service_changed_count());
437 EXPECT_EQ(1u, service->GetCharacteristics().size());
438 EXPECT_EQ(1u, characteristic->GetDescriptors().size());
439
440 observer.Reset();
441 SimulateDidDiscoverCharacteristics(service); // Extra system call.
442 SimulateGattServicesChanged(device);
443 SimulateDidDiscoverCharacteristics(service); // Extra system call.
444 SimulateDidDiscoverServices(device);
445 SimulateDidDiscoverCharacteristics(service);
446 SimulateDidDiscoverCharacteristics(service); // Extra system call.
447 SimulateDidDiscoverDescriptors(characteristic);
448 SimulateDidDiscoverCharacteristics(service); // Extra system call.
449 EXPECT_EQ(2, observer.device_changed_count());
450 }
451 #endif // defined(OS_MACOSX)
285 452
286 } // namespace device 453 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698