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

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

Issue 2638653002: Bluetooth: macOS: DidModifyServices can happens while scanning (Closed)
Patch Set: Merge 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 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 318 matching lines...) Expand 10 before | Expand all | Expand 10 after
329 // Simulate remove of a primary service. 329 // Simulate remove of a primary service.
330 BluetoothRemoteGattService* service1 = device->GetGattServices()[0]; 330 BluetoothRemoteGattService* service1 = device->GetGattServices()[0];
331 BluetoothRemoteGattService* service2 = device->GetGattServices()[1]; 331 BluetoothRemoteGattService* service2 = device->GetGattServices()[1];
332 std::string removed_service = service1->GetIdentifier(); 332 std::string removed_service = service1->GetIdentifier();
333 SimulateGattServiceRemoved(device->GetGattService(removed_service)); 333 SimulateGattServiceRemoved(device->GetGattService(removed_service));
334 EXPECT_EQ(1, observer.gatt_service_removed_count()); 334 EXPECT_EQ(1, observer.gatt_service_removed_count());
335 EXPECT_EQ(1u, device->GetGattServices().size()); 335 EXPECT_EQ(1u, device->GetGattServices().size());
336 EXPECT_FALSE(device->GetGattService(removed_service)); 336 EXPECT_FALSE(device->GetGattService(removed_service));
337 EXPECT_EQ(device->GetGattServices()[0], service2); 337 EXPECT_EQ(device->GetGattServices()[0], service2);
338 } 338 }
339 #endif // defined(OS_WIN) || defined(OS_MACOSX) 339 #endif // defined(OS_MACOSX) || defined(OS_WIN)
340
341 #if defined(OS_MACOSX)
342 // Tests to receive a services changed notification from macOS, while
343 // discovering characteristics. The gatt device should scan again for services
344 // and characteristics, before scanning for descriptors. Only after the gatt
345 // service changed notification should be sent.
346 // Android: This test doesn't apply to Android because there is no services
347 // changed event that could arrive during a discovery procedure.
348 TEST_F(BluetoothRemoteGattServiceTest,
349 SimulateDeviceModificationWhileDiscoveringCharacteristics) {
350 if (!PlatformSupportsLowEnergy()) {
351 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
352 return;
353 }
354 InitWithFakeAdapter();
355 StartLowEnergyDiscoverySession();
356 BluetoothDevice* device = SimulateLowEnergyDevice(3);
357 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
358 GetConnectErrorCallback(Call::NOT_EXPECTED));
359
360 TestBluetoothAdapterObserver observer(adapter_);
361
362 // Starts first discovery process.
363 SimulateGattConnection(device);
364 AddServicesToDeviceMac(device, {kTestUUIDHeartRate});
365 SimulateDidDiscoverServicesMac(device);
366 EXPECT_EQ(1u, device->GetGattServices().size());
367 BluetoothRemoteGattService* service = device->GetGattServices()[0];
368 std::string characteristic_uuid1 = "11111111-0000-1000-8000-00805f9b34fb";
369 AddCharacteristicToServiceMac(service, characteristic_uuid1,
370 /* properties */ 0);
371 // Now waiting for characteristic discovery.
372
373 // Starts second discovery process.
374 SimulateGattServicesChanged(device);
375 SimulateDidDiscoverServicesMac(device);
376 // Now waiting for the second characteristic discovery.
377
378 // First system call to -[id<CBPeripheralDelegate>
379 // peripheral:didDiscoverCharacteristicsForService:error:]
380 SimulateDidDiscoverCharacteristicsMac(service);
381 EXPECT_EQ(0, observer.gatt_service_changed_count());
382 EXPECT_EQ(1u, service->GetCharacteristics().size());
383
384 // Finish discovery process.
385 std::string characteristic_uuid2 = "22222222-0000-1000-8000-00805f9b34fb";
386 AddCharacteristicToServiceMac(service, characteristic_uuid2,
387 /* properties */ 0);
388 // Second system call to -[id<CBPeripheralDelegate>
389 // peripheral:didDiscoverCharacteristicsForService:error:]
390 SimulateDidDiscoverCharacteristicsMac(service);
391 EXPECT_EQ(2u, service->GetCharacteristics().size());
392 EXPECT_EQ(0, observer.gatt_service_changed_count());
393 BluetoothRemoteGattCharacteristic* characteristic1 =
394 service->GetCharacteristics()[0];
395 BluetoothRemoteGattCharacteristic* characteristic2 =
396 service->GetCharacteristics()[1];
397 if (characteristic1->GetUUID().canonical_value() == characteristic_uuid2) {
398 BluetoothRemoteGattCharacteristic* tmp = characteristic1;
399 characteristic1 = characteristic2;
400 characteristic2 = tmp;
401 }
402 EXPECT_EQ(characteristic_uuid1, characteristic1->GetUUID().canonical_value());
403 EXPECT_EQ(characteristic_uuid2, characteristic2->GetUUID().canonical_value());
404 SimulateDidDiscoverDescriptorsMac(characteristic1);
405 SimulateDidDiscoverDescriptorsMac(characteristic2);
406 EXPECT_EQ(1, observer.gatt_service_changed_count());
407 }
408 #endif // defined(OS_MACOSX)
409
410 #if defined(OS_MACOSX)
411 // Simulates to receive an extra discovery characteristic notifications from
412 // macOS. Those notifications should be ignored.
413 // Android: This test doesn't apply to Android because there is no services
414 // changed event that could arrive during a discovery procedure.
415 TEST_F(BluetoothRemoteGattServiceTest, ExtraDidDiscoverServicesCall) {
416 if (!PlatformSupportsLowEnergy()) {
417 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
418 return;
419 }
420 InitWithFakeAdapter();
421 StartLowEnergyDiscoverySession();
422 BluetoothDevice* device = SimulateLowEnergyDevice(3);
423 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
424 GetConnectErrorCallback(Call::NOT_EXPECTED));
425
426 TestBluetoothAdapterObserver observer(adapter_);
427
428 // Starts first discovery process.
429 SimulateGattConnection(device);
430 AddServicesToDeviceMac(device, {kTestUUIDHeartRate});
431 SimulateDidDiscoverServicesMac(device);
432 EXPECT_EQ(1u, device->GetGattServices().size());
433 BluetoothRemoteGattService* service = device->GetGattServices()[0];
434 std::string characteristic_uuid = "11111111-0000-1000-8000-00805f9b34fb";
435 AddCharacteristicToServiceMac(service, characteristic_uuid,
436 /* properties */ 0);
437 SimulateDidDiscoverCharacteristicsMac(service);
438 EXPECT_EQ(1u, service->GetCharacteristics().size());
439 BluetoothRemoteGattCharacteristic* characteristic =
440 service->GetCharacteristics()[0];
441 std::string descriptor_uuid = "22222222-0000-1000-8000-00805f9b34fb";
442 AddDescriptorToCharacteristicMac(characteristic, descriptor_uuid);
443 SimulateDidDiscoverDescriptorsMac(characteristic);
444 EXPECT_EQ(1, observer.gatt_service_changed_count());
445 EXPECT_EQ(1u, service->GetCharacteristics().size());
446 EXPECT_EQ(1u, characteristic->GetDescriptors().size());
447
448 observer.Reset();
449 SimulateDidDiscoverServicesMac(device); // Extra system call.
450 SimulateGattServicesChanged(device);
451 SimulateDidDiscoverServicesMac(device);
452 SimulateDidDiscoverServicesMac(device); // Extra system call.
453 SimulateDidDiscoverCharacteristicsMac(service);
454 SimulateDidDiscoverServicesMac(device); // Extra system call.
455 SimulateDidDiscoverDescriptorsMac(characteristic);
456 SimulateDidDiscoverServicesMac(device); // Extra system call.
457 EXPECT_EQ(2, observer.device_changed_count());
458 }
459 #endif // defined(OS_MACOSX)
460
461 #if defined(OS_MACOSX)
462 // Simulates to receive an extra discovery characteristic notifications from
463 // macOS. Those notifications should be ignored.
464 // Android: This test doesn't apply to Android because there is no services
465 // changed event that could arrive during a discovery procedure.
466 TEST_F(BluetoothRemoteGattServiceTest, ExtraDidDiscoverCharacteristicsCall) {
467 if (!PlatformSupportsLowEnergy()) {
468 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
469 return;
470 }
471 InitWithFakeAdapter();
472 StartLowEnergyDiscoverySession();
473 BluetoothDevice* device = SimulateLowEnergyDevice(3);
474 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
475 GetConnectErrorCallback(Call::NOT_EXPECTED));
476
477 TestBluetoothAdapterObserver observer(adapter_);
478
479 // Starts first discovery process.
480 SimulateGattConnection(device);
481 AddServicesToDeviceMac(device, {kTestUUIDHeartRate});
482 SimulateDidDiscoverServicesMac(device);
483 EXPECT_EQ(1u, device->GetGattServices().size());
484 BluetoothRemoteGattService* service = device->GetGattServices()[0];
485 std::string characteristic_uuid = "11111111-0000-1000-8000-00805f9b34fb";
486 AddCharacteristicToServiceMac(service, characteristic_uuid,
487 /* properties */ 0);
488 SimulateDidDiscoverCharacteristicsMac(service);
489 EXPECT_EQ(1u, service->GetCharacteristics().size());
490 BluetoothRemoteGattCharacteristic* characteristic =
491 service->GetCharacteristics()[0];
492 std::string descriptor_uuid = "22222222-0000-1000-8000-00805f9b34fb";
493 AddDescriptorToCharacteristicMac(characteristic, descriptor_uuid);
494 SimulateDidDiscoverDescriptorsMac(characteristic);
495 EXPECT_EQ(1, observer.gatt_service_changed_count());
496 EXPECT_EQ(1u, service->GetCharacteristics().size());
497 EXPECT_EQ(1u, characteristic->GetDescriptors().size());
498
499 observer.Reset();
500 SimulateDidDiscoverCharacteristicsMac(service); // Extra system call.
501 SimulateGattServicesChanged(device);
502 SimulateDidDiscoverCharacteristicsMac(service); // Extra system call.
503 SimulateDidDiscoverServicesMac(device);
504 SimulateDidDiscoverCharacteristicsMac(service);
505 SimulateDidDiscoverCharacteristicsMac(service); // Extra system call.
506 SimulateDidDiscoverDescriptorsMac(characteristic);
507 SimulateDidDiscoverCharacteristicsMac(service); // Extra system call.
508 EXPECT_EQ(2, observer.device_changed_count());
509 }
510 #endif // defined(OS_MACOSX)
340 511
341 } // namespace device 512 } // namespace device
OLDNEW
« no previous file with comments | « device/bluetooth/bluetooth_remote_gatt_service_mac.mm ('k') | device/bluetooth/test/bluetooth_test_mac.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698