OLD | NEW |
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 <stdint.h> | 5 #include <stdint.h> |
6 #include <utility> | 6 #include <utility> |
7 | 7 |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
(...skipping 2368 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2379 SimulateGattDisconnection(device_); | 2379 SimulateGattDisconnection(device_); |
2380 // Do not yet call RunUntilIdle() to process the disconnect task. | 2380 // Do not yet call RunUntilIdle() to process the disconnect task. |
2381 characteristic1_->ReadRemoteCharacteristic( | 2381 characteristic1_->ReadRemoteCharacteristic( |
2382 GetReadValueCallback(Call::NOT_EXPECTED), | 2382 GetReadValueCallback(Call::NOT_EXPECTED), |
2383 GetGattErrorCallback(Call::EXPECTED)); | 2383 GetGattErrorCallback(Call::EXPECTED)); |
2384 | 2384 |
2385 base::RunLoop().RunUntilIdle(); | 2385 base::RunLoop().RunUntilIdle(); |
2386 EXPECT_EQ(BluetoothRemoteGattService::GATT_ERROR_FAILED, | 2386 EXPECT_EQ(BluetoothRemoteGattService::GATT_ERROR_FAILED, |
2387 last_gatt_error_code_); | 2387 last_gatt_error_code_); |
2388 } | 2388 } |
2389 #endif | 2389 #endif // defined(OS_ANDROID) |
2390 | 2390 |
2391 #if defined(OS_ANDROID) | 2391 #if defined(OS_ANDROID) |
2392 // Tests that write requests after a device disconnects but before the | 2392 // Tests that write requests after a device disconnects but before the |
2393 // disconnect task runs result in an error. | 2393 // disconnect task runs result in an error. |
2394 // macOS: Does not apply. All events arrive on the UI Thread. | 2394 // macOS: Does not apply. All events arrive on the UI Thread. |
2395 // TODO(crbug.com/694102): Enable this test on Windows. | 2395 // TODO(crbug.com/694102): Enable this test on Windows. |
2396 TEST_F(BluetoothRemoteGattCharacteristicTest, WriteDuringDisconnect) { | 2396 TEST_F(BluetoothRemoteGattCharacteristicTest, WriteDuringDisconnect) { |
2397 if (!PlatformSupportsLowEnergy()) { | 2397 if (!PlatformSupportsLowEnergy()) { |
2398 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | 2398 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
2399 return; | 2399 return; |
2400 } | 2400 } |
2401 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate( | 2401 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate( |
2402 BluetoothRemoteGattCharacteristic::PROPERTY_WRITE)); | 2402 BluetoothRemoteGattCharacteristic::PROPERTY_WRITE)); |
2403 | 2403 |
2404 SimulateGattDisconnection(device_); | 2404 SimulateGattDisconnection(device_); |
2405 // Do not yet call RunUntilIdle() to process the disconnect task. | 2405 // Do not yet call RunUntilIdle() to process the disconnect task. |
2406 characteristic1_->WriteRemoteCharacteristic( | 2406 characteristic1_->WriteRemoteCharacteristic( |
2407 std::vector<uint8_t>(), GetCallback(Call::NOT_EXPECTED), | 2407 std::vector<uint8_t>(), GetCallback(Call::NOT_EXPECTED), |
2408 GetGattErrorCallback(Call::EXPECTED)); | 2408 GetGattErrorCallback(Call::EXPECTED)); |
2409 | 2409 |
2410 base::RunLoop().RunUntilIdle(); | 2410 base::RunLoop().RunUntilIdle(); |
2411 EXPECT_EQ(BluetoothRemoteGattService::GATT_ERROR_FAILED, | 2411 EXPECT_EQ(BluetoothRemoteGattService::GATT_ERROR_FAILED, |
2412 last_gatt_error_code_); | 2412 last_gatt_error_code_); |
2413 } | 2413 } |
2414 #endif | 2414 #endif // defined(OS_ANDROID) |
2415 | 2415 |
2416 #if defined(OS_ANDROID) | 2416 #if defined(OS_ANDROID) |
2417 // Tests that start notifications requests after a device disconnects but before | 2417 // Tests that start notifications requests after a device disconnects but before |
2418 // the disconnect task runs result in an error. | 2418 // the disconnect task runs result in an error. |
2419 // macOS: Does not apply. All events arrive on the UI Thread. | 2419 // macOS: Does not apply. All events arrive on the UI Thread. |
2420 // TODO(crbug.com/694102): Enable this test on Windows. | 2420 // TODO(crbug.com/694102): Enable this test on Windows. |
2421 TEST_F(BluetoothRemoteGattCharacteristicTest, | 2421 TEST_F(BluetoothRemoteGattCharacteristicTest, |
2422 StartNotifySessionDuringDisconnect) { | 2422 StartNotifySessionDuringDisconnect) { |
2423 if (!PlatformSupportsLowEnergy()) { | 2423 if (!PlatformSupportsLowEnergy()) { |
2424 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | 2424 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
2425 return; | 2425 return; |
2426 } | 2426 } |
2427 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate( | 2427 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate( |
2428 BluetoothRemoteGattCharacteristic::PROPERTY_NOTIFY)); | 2428 BluetoothRemoteGattCharacteristic::PROPERTY_NOTIFY)); |
2429 SimulateGattDescriptor( | 2429 SimulateGattDescriptor( |
2430 characteristic1_, | 2430 characteristic1_, |
2431 BluetoothRemoteGattDescriptor::ClientCharacteristicConfigurationUuid() | 2431 BluetoothRemoteGattDescriptor::ClientCharacteristicConfigurationUuid() |
2432 .canonical_value()); | 2432 .canonical_value()); |
2433 | 2433 |
2434 SimulateGattDisconnection(device_); | 2434 SimulateGattDisconnection(device_); |
2435 // Don't run the disconnect task. | 2435 // Don't run the disconnect task. |
2436 characteristic1_->StartNotifySession(GetNotifyCallback(Call::NOT_EXPECTED), | 2436 characteristic1_->StartNotifySession(GetNotifyCallback(Call::NOT_EXPECTED), |
2437 GetGattErrorCallback(Call::EXPECTED)); | 2437 GetGattErrorCallback(Call::EXPECTED)); |
2438 | 2438 |
2439 base::RunLoop().RunUntilIdle(); | 2439 base::RunLoop().RunUntilIdle(); |
2440 EXPECT_EQ(BluetoothRemoteGattService::GATT_ERROR_FAILED, | 2440 EXPECT_EQ(BluetoothRemoteGattService::GATT_ERROR_FAILED, |
2441 last_gatt_error_code_); | 2441 last_gatt_error_code_); |
2442 } | 2442 } |
2443 #endif | 2443 #endif // defined(OS_ANDROID) |
2444 | 2444 |
2445 #if defined(OS_ANDROID) | 2445 #if defined(OS_ANDROID) |
2446 // Tests that stop notifications requests after a device disconnects but before | 2446 // Tests that stop notifications requests after a device disconnects but before |
2447 // the disconnect task runs do not result in a crash. | 2447 // the disconnect task runs do not result in a crash. |
2448 // macOS: Does not apply. All events arrive on the UI Thread. | 2448 // macOS: Does not apply. All events arrive on the UI Thread. |
2449 // TODO(crbug.com/694102): Enable this test on Windows. | 2449 // TODO(crbug.com/694102): Enable this test on Windows. |
2450 TEST_F(BluetoothRemoteGattCharacteristicTest, | 2450 TEST_F(BluetoothRemoteGattCharacteristicTest, |
2451 StopNotifySessionDuringDisconnect) { | 2451 StopNotifySessionDuringDisconnect) { |
2452 if (!PlatformSupportsLowEnergy()) { | 2452 if (!PlatformSupportsLowEnergy()) { |
2453 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | 2453 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
2454 return; | 2454 return; |
2455 } | 2455 } |
2456 ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate( | 2456 ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate( |
2457 /* properties: NOTIFY */ 0x10, NotifyValueState::NOTIFY)); | 2457 /* properties: NOTIFY */ 0x10, NotifyValueState::NOTIFY)); |
2458 | 2458 |
2459 SimulateGattDisconnection(device_); | 2459 SimulateGattDisconnection(device_); |
2460 // Don't run the disconnect task. | 2460 // Don't run the disconnect task. |
2461 notify_sessions_[0]->Stop(GetStopNotifyCallback(Call::EXPECTED)); | 2461 notify_sessions_[0]->Stop(GetStopNotifyCallback(Call::EXPECTED)); |
2462 base::RunLoop().RunUntilIdle(); | 2462 base::RunLoop().RunUntilIdle(); |
2463 } | 2463 } |
2464 #endif | 2464 #endif // defined(OS_ANDROID) |
2465 | 2465 |
2466 #if defined(OS_ANDROID) | 2466 #if defined(OS_ANDROID) |
2467 // Tests that deleting notify sessions after a device disconnects but before the | 2467 // Tests that deleting notify sessions after a device disconnects but before the |
2468 // disconnect task runs do not result in a crash. | 2468 // disconnect task runs do not result in a crash. |
2469 // macOS: Does not apply. All events arrive on the UI Thread. | 2469 // macOS: Does not apply. All events arrive on the UI Thread. |
2470 // TODO(crbug.com/694102): Enable this test on Windows. | 2470 // TODO(crbug.com/694102): Enable this test on Windows. |
2471 TEST_F(BluetoothRemoteGattCharacteristicTest, | 2471 TEST_F(BluetoothRemoteGattCharacteristicTest, |
2472 DeleteNotifySessionDuringDisconnect) { | 2472 DeleteNotifySessionDuringDisconnect) { |
2473 if (!PlatformSupportsLowEnergy()) { | 2473 if (!PlatformSupportsLowEnergy()) { |
2474 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | 2474 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
2475 return; | 2475 return; |
2476 } | 2476 } |
2477 ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate( | 2477 ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate( |
2478 /* properties: NOTIFY */ 0x10, NotifyValueState::NOTIFY)); | 2478 /* properties: NOTIFY */ 0x10, NotifyValueState::NOTIFY)); |
2479 | 2479 |
2480 SimulateGattDisconnection(device_); | 2480 SimulateGattDisconnection(device_); |
2481 // Don't run the disconnect task. | 2481 // Don't run the disconnect task. |
2482 notify_sessions_.clear(); | 2482 notify_sessions_.clear(); |
2483 base::RunLoop().RunUntilIdle(); | 2483 base::RunLoop().RunUntilIdle(); |
2484 } | 2484 } |
2485 #endif | 2485 #endif // defined(OS_ANDROID) |
| 2486 |
| 2487 #if defined(OS_MACOSX) |
| 2488 // Tests to receive a services changed notification from macOS, while |
| 2489 // discovering descriptors. This test simulate having 2 descriptor scan at the |
| 2490 // same time. Only once both descriptor scanning is done, the gatt device is |
| 2491 // ready. |
| 2492 // Android: This test doesn't apply to Android because there is no services |
| 2493 // changed event that could arrive during a discovery procedure. |
| 2494 TEST_F(BluetoothRemoteGattCharacteristicTest, |
| 2495 SimulateDeviceModificationWhileDiscoveringDescriptors) { |
| 2496 if (!PlatformSupportsLowEnergy()) { |
| 2497 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
| 2498 return; |
| 2499 } |
| 2500 InitWithFakeAdapter(); |
| 2501 StartLowEnergyDiscoverySession(); |
| 2502 BluetoothDevice* device = SimulateLowEnergyDevice(3); |
| 2503 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
| 2504 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
| 2505 |
| 2506 TestBluetoothAdapterObserver observer(adapter_); |
| 2507 |
| 2508 // Starts first discovery process. |
| 2509 SimulateGattConnection(device); |
| 2510 EXPECT_EQ(1, observer.device_changed_count()); |
| 2511 AddServicesToDeviceMac(device, {kTestUUIDHeartRate}); |
| 2512 SimulateDidDiscoverServicesMac(device); |
| 2513 EXPECT_EQ(1u, device->GetGattServices().size()); |
| 2514 BluetoothRemoteGattService* service = device->GetGattServices()[0]; |
| 2515 std::string characteristic_uuid = "11111111-0000-1000-8000-00805f9b34fb"; |
| 2516 AddCharacteristicToServiceMac(service, characteristic_uuid, |
| 2517 /* properties */ 0); |
| 2518 SimulateDidDiscoverCharacteristicsMac(service); |
| 2519 EXPECT_EQ(1u, service->GetCharacteristics().size()); |
| 2520 BluetoothRemoteGattCharacteristic* characteristic = |
| 2521 service->GetCharacteristics()[0]; |
| 2522 std::string descriptor_uuid = "22222222-0000-1000-8000-00805f9b34fb"; |
| 2523 AddDescriptorToCharacteristicMac(characteristic, descriptor_uuid); |
| 2524 // Now waiting for descriptor discovery. |
| 2525 |
| 2526 // Starts second discovery process. |
| 2527 SimulateGattServicesChanged(device); |
| 2528 EXPECT_EQ(2, observer.device_changed_count()); |
| 2529 SimulateDidDiscoverServicesMac(device); |
| 2530 SimulateDidDiscoverCharacteristicsMac(service); |
| 2531 // Now waiting for a second descriptor discovery. |
| 2532 |
| 2533 // Finish discovery process. |
| 2534 // First system call to -[id<CBPeripheralDelegate> |
| 2535 // peripheral:didDiscoverDescriptorsForCharacteristic:error:] |
| 2536 SimulateDidDiscoverDescriptorsMac(characteristic); |
| 2537 EXPECT_EQ(0, observer.gatt_service_changed_count()); |
| 2538 EXPECT_EQ(1u, service->GetCharacteristics().size()); |
| 2539 EXPECT_EQ(1u, characteristic->GetDescriptors().size()); |
| 2540 EXPECT_EQ(2, observer.device_changed_count()); |
| 2541 |
| 2542 // Second system call to -[id<CBPeripheralDelegate> |
| 2543 // peripheral:didDiscoverDescriptorsForCharacteristic:error:] |
| 2544 // Finish second discovery process. |
| 2545 observer.Reset(); |
| 2546 SimulateDidDiscoverDescriptorsMac(characteristic); |
| 2547 EXPECT_EQ(1, observer.gatt_service_changed_count()); |
| 2548 EXPECT_EQ(1, observer.device_changed_count()); |
| 2549 } |
| 2550 #endif // defined(OS_MACOSX) |
| 2551 |
| 2552 #if defined(OS_MACOSX) |
| 2553 // Simulates to receive an extra discovery descriptor notifications from macOS. |
| 2554 // Those notifications should be ignored. |
| 2555 // Android: This test doesn't apply to Android because there is no services |
| 2556 // changed event that could arrive during a discovery procedure. |
| 2557 TEST_F(BluetoothRemoteGattCharacteristicTest, ExtraDidDiscoverDescriptorsCall) { |
| 2558 if (!PlatformSupportsLowEnergy()) { |
| 2559 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
| 2560 return; |
| 2561 } |
| 2562 InitWithFakeAdapter(); |
| 2563 StartLowEnergyDiscoverySession(); |
| 2564 BluetoothDevice* device = SimulateLowEnergyDevice(3); |
| 2565 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
| 2566 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
| 2567 |
| 2568 TestBluetoothAdapterObserver observer(adapter_); |
| 2569 |
| 2570 // Starts first discovery process. |
| 2571 SimulateGattConnection(device); |
| 2572 AddServicesToDeviceMac(device, {kTestUUIDHeartRate}); |
| 2573 SimulateDidDiscoverServicesMac(device); |
| 2574 EXPECT_EQ(1u, device->GetGattServices().size()); |
| 2575 BluetoothRemoteGattService* service = device->GetGattServices()[0]; |
| 2576 std::string characteristic_uuid = "11111111-0000-1000-8000-00805f9b34fb"; |
| 2577 AddCharacteristicToServiceMac(service, characteristic_uuid, |
| 2578 /* properties */ 0); |
| 2579 SimulateDidDiscoverCharacteristicsMac(service); |
| 2580 EXPECT_EQ(1u, service->GetCharacteristics().size()); |
| 2581 BluetoothRemoteGattCharacteristic* characteristic = |
| 2582 service->GetCharacteristics()[0]; |
| 2583 std::string descriptor_uuid = "22222222-0000-1000-8000-00805f9b34fb"; |
| 2584 AddDescriptorToCharacteristicMac(characteristic, descriptor_uuid); |
| 2585 SimulateDidDiscoverDescriptorsMac(characteristic); |
| 2586 EXPECT_EQ(1, observer.gatt_service_changed_count()); |
| 2587 EXPECT_EQ(1u, service->GetCharacteristics().size()); |
| 2588 EXPECT_EQ(1u, characteristic->GetDescriptors().size()); |
| 2589 |
| 2590 observer.Reset(); |
| 2591 SimulateDidDiscoverDescriptorsMac(characteristic); // Extra system call. |
| 2592 SimulateGattServicesChanged(device); |
| 2593 SimulateDidDiscoverDescriptorsMac(characteristic); // Extra system call. |
| 2594 SimulateDidDiscoverServicesMac(device); |
| 2595 SimulateDidDiscoverDescriptorsMac(characteristic); // Extra system call. |
| 2596 SimulateDidDiscoverCharacteristicsMac(service); |
| 2597 SimulateDidDiscoverDescriptorsMac(characteristic); |
| 2598 SimulateDidDiscoverDescriptorsMac(characteristic); // Extra system call. |
| 2599 EXPECT_EQ(2, observer.device_changed_count()); |
| 2600 } |
| 2601 #endif // defined(OS_MACOSX) |
2486 } // namespace device | 2602 } // namespace device |
OLD | NEW |