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 2283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2294 SimulateGattDisconnection(device_); | 2294 SimulateGattDisconnection(device_); |
2295 // Do not yet call RunUntilIdle() to process the disconnect task. | 2295 // Do not yet call RunUntilIdle() to process the disconnect task. |
2296 characteristic1_->ReadRemoteCharacteristic( | 2296 characteristic1_->ReadRemoteCharacteristic( |
2297 GetReadValueCallback(Call::NOT_EXPECTED), | 2297 GetReadValueCallback(Call::NOT_EXPECTED), |
2298 GetGattErrorCallback(Call::EXPECTED)); | 2298 GetGattErrorCallback(Call::EXPECTED)); |
2299 | 2299 |
2300 base::RunLoop().RunUntilIdle(); | 2300 base::RunLoop().RunUntilIdle(); |
2301 EXPECT_EQ(BluetoothRemoteGattService::GATT_ERROR_FAILED, | 2301 EXPECT_EQ(BluetoothRemoteGattService::GATT_ERROR_FAILED, |
2302 last_gatt_error_code_); | 2302 last_gatt_error_code_); |
2303 } | 2303 } |
2304 #endif | 2304 #endif // defined(OS_ANDROID) |
2305 | 2305 |
2306 #if defined(OS_ANDROID) | 2306 #if defined(OS_ANDROID) |
2307 // Tests that write requests after a device disconnects but before the | 2307 // Tests that write requests after a device disconnects but before the |
2308 // disconnect task runs result in an error. | 2308 // disconnect task runs result in an error. |
2309 // macOS: Does not apply. All events arrive on the UI Thread. | 2309 // macOS: Does not apply. All events arrive on the UI Thread. |
2310 // TODO(crbug.com/694102): Enable this test on Windows. | 2310 // TODO(crbug.com/694102): Enable this test on Windows. |
2311 TEST_F(BluetoothRemoteGattCharacteristicTest, WriteDuringDisconnect) { | 2311 TEST_F(BluetoothRemoteGattCharacteristicTest, WriteDuringDisconnect) { |
2312 if (!PlatformSupportsLowEnergy()) { | 2312 if (!PlatformSupportsLowEnergy()) { |
2313 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | 2313 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
2314 return; | 2314 return; |
2315 } | 2315 } |
2316 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate( | 2316 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate( |
2317 BluetoothRemoteGattCharacteristic::PROPERTY_WRITE)); | 2317 BluetoothRemoteGattCharacteristic::PROPERTY_WRITE)); |
2318 | 2318 |
2319 SimulateGattDisconnection(device_); | 2319 SimulateGattDisconnection(device_); |
2320 // Do not yet call RunUntilIdle() to process the disconnect task. | 2320 // Do not yet call RunUntilIdle() to process the disconnect task. |
2321 characteristic1_->WriteRemoteCharacteristic( | 2321 characteristic1_->WriteRemoteCharacteristic( |
2322 std::vector<uint8_t>(), GetCallback(Call::NOT_EXPECTED), | 2322 std::vector<uint8_t>(), GetCallback(Call::NOT_EXPECTED), |
2323 GetGattErrorCallback(Call::EXPECTED)); | 2323 GetGattErrorCallback(Call::EXPECTED)); |
2324 | 2324 |
2325 base::RunLoop().RunUntilIdle(); | 2325 base::RunLoop().RunUntilIdle(); |
2326 EXPECT_EQ(BluetoothRemoteGattService::GATT_ERROR_FAILED, | 2326 EXPECT_EQ(BluetoothRemoteGattService::GATT_ERROR_FAILED, |
2327 last_gatt_error_code_); | 2327 last_gatt_error_code_); |
2328 } | 2328 } |
2329 #endif | 2329 #endif // defined(OS_ANDROID) |
2330 | 2330 |
2331 #if defined(OS_ANDROID) | 2331 #if defined(OS_ANDROID) |
2332 // Tests that start notifications requests after a device disconnects but before | 2332 // Tests that start notifications requests after a device disconnects but before |
2333 // the disconnect task runs result in an error. | 2333 // the disconnect task runs result in an error. |
2334 // macOS: Does not apply. All events arrive on the UI Thread. | 2334 // macOS: Does not apply. All events arrive on the UI Thread. |
2335 // TODO(crbug.com/694102): Enable this test on Windows. | 2335 // TODO(crbug.com/694102): Enable this test on Windows. |
2336 TEST_F(BluetoothRemoteGattCharacteristicTest, | 2336 TEST_F(BluetoothRemoteGattCharacteristicTest, |
2337 StartNotifySessionDuringDisconnect) { | 2337 StartNotifySessionDuringDisconnect) { |
2338 if (!PlatformSupportsLowEnergy()) { | 2338 if (!PlatformSupportsLowEnergy()) { |
2339 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | 2339 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
2340 return; | 2340 return; |
2341 } | 2341 } |
2342 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate( | 2342 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate( |
2343 BluetoothRemoteGattCharacteristic::PROPERTY_NOTIFY)); | 2343 BluetoothRemoteGattCharacteristic::PROPERTY_NOTIFY)); |
2344 SimulateGattDescriptor( | 2344 SimulateGattDescriptor( |
2345 characteristic1_, | 2345 characteristic1_, |
2346 BluetoothRemoteGattDescriptor::ClientCharacteristicConfigurationUuid() | 2346 BluetoothRemoteGattDescriptor::ClientCharacteristicConfigurationUuid() |
2347 .canonical_value()); | 2347 .canonical_value()); |
2348 | 2348 |
2349 SimulateGattDisconnection(device_); | 2349 SimulateGattDisconnection(device_); |
2350 // Don't run the disconnect task. | 2350 // Don't run the disconnect task. |
2351 characteristic1_->StartNotifySession(GetNotifyCallback(Call::NOT_EXPECTED), | 2351 characteristic1_->StartNotifySession(GetNotifyCallback(Call::NOT_EXPECTED), |
2352 GetGattErrorCallback(Call::EXPECTED)); | 2352 GetGattErrorCallback(Call::EXPECTED)); |
2353 | 2353 |
2354 base::RunLoop().RunUntilIdle(); | 2354 base::RunLoop().RunUntilIdle(); |
2355 EXPECT_EQ(BluetoothRemoteGattService::GATT_ERROR_FAILED, | 2355 EXPECT_EQ(BluetoothRemoteGattService::GATT_ERROR_FAILED, |
2356 last_gatt_error_code_); | 2356 last_gatt_error_code_); |
2357 } | 2357 } |
2358 #endif | 2358 #endif // defined(OS_ANDROID) |
2359 | 2359 |
2360 #if defined(OS_ANDROID) | 2360 #if defined(OS_ANDROID) |
2361 // Tests that stop notifications requests after a device disconnects but before | 2361 // Tests that stop notifications requests after a device disconnects but before |
2362 // the disconnect task runs do not result in a crash. | 2362 // the disconnect task runs do not result in a crash. |
2363 // macOS: Does not apply. All events arrive on the UI Thread. | 2363 // macOS: Does not apply. All events arrive on the UI Thread. |
2364 // TODO(crbug.com/694102): Enable this test on Windows. | 2364 // TODO(crbug.com/694102): Enable this test on Windows. |
2365 TEST_F(BluetoothRemoteGattCharacteristicTest, | 2365 TEST_F(BluetoothRemoteGattCharacteristicTest, |
2366 StopNotifySessionDuringDisconnect) { | 2366 StopNotifySessionDuringDisconnect) { |
2367 if (!PlatformSupportsLowEnergy()) { | 2367 if (!PlatformSupportsLowEnergy()) { |
2368 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | 2368 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
2369 return; | 2369 return; |
2370 } | 2370 } |
2371 ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate( | 2371 ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate( |
2372 /* properties: NOTIFY */ 0x10, NotifyValueState::NOTIFY)); | 2372 /* properties: NOTIFY */ 0x10, NotifyValueState::NOTIFY)); |
2373 | 2373 |
2374 SimulateGattDisconnection(device_); | 2374 SimulateGattDisconnection(device_); |
2375 // Don't run the disconnect task. | 2375 // Don't run the disconnect task. |
2376 notify_sessions_[0]->Stop(GetStopNotifyCallback(Call::EXPECTED)); | 2376 notify_sessions_[0]->Stop(GetStopNotifyCallback(Call::EXPECTED)); |
2377 base::RunLoop().RunUntilIdle(); | 2377 base::RunLoop().RunUntilIdle(); |
2378 } | 2378 } |
2379 #endif | 2379 #endif // defined(OS_ANDROID) |
2380 | 2380 |
2381 #if defined(OS_ANDROID) | 2381 #if defined(OS_ANDROID) |
2382 // Tests that deleting notify sessions after a device disconnects but before the | 2382 // Tests that deleting notify sessions after a device disconnects but before the |
2383 // disconnect task runs do not result in a crash. | 2383 // disconnect task runs do not result in a crash. |
2384 // macOS: Does not apply. All events arrive on the UI Thread. | 2384 // macOS: Does not apply. All events arrive on the UI Thread. |
2385 // TODO(crbug.com/694102): Enable this test on Windows. | 2385 // TODO(crbug.com/694102): Enable this test on Windows. |
2386 TEST_F(BluetoothRemoteGattCharacteristicTest, | 2386 TEST_F(BluetoothRemoteGattCharacteristicTest, |
2387 DeleteNotifySessionDuringDisconnect) { | 2387 DeleteNotifySessionDuringDisconnect) { |
2388 if (!PlatformSupportsLowEnergy()) { | 2388 if (!PlatformSupportsLowEnergy()) { |
2389 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | 2389 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
2390 return; | 2390 return; |
2391 } | 2391 } |
2392 ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate( | 2392 ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate( |
2393 /* properties: NOTIFY */ 0x10, NotifyValueState::NOTIFY)); | 2393 /* properties: NOTIFY */ 0x10, NotifyValueState::NOTIFY)); |
2394 | 2394 |
2395 SimulateGattDisconnection(device_); | 2395 SimulateGattDisconnection(device_); |
2396 // Don't run the disconnect task. | 2396 // Don't run the disconnect task. |
2397 notify_sessions_.clear(); | 2397 notify_sessions_.clear(); |
2398 base::RunLoop().RunUntilIdle(); | 2398 base::RunLoop().RunUntilIdle(); |
2399 } | 2399 } |
2400 #endif | 2400 #endif // defined(OS_ANDROID) |
| 2401 |
| 2402 #if defined(OS_MACOSX) |
| 2403 // Tests to receive a services changed notification, while waiting for |
| 2404 // characteristics. |
| 2405 TEST_F(BluetoothRemoteGattCharacteristicTest, |
| 2406 SimulateDeviceModificationWhileDiscoveringCharacteristics) { |
| 2407 if (!PlatformSupportsLowEnergy()) { |
| 2408 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
| 2409 return; |
| 2410 } |
| 2411 InitWithFakeAdapter(); |
| 2412 StartLowEnergyDiscoverySession(); |
| 2413 BluetoothDevice* device = SimulateLowEnergyDevice(3); |
| 2414 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
| 2415 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
| 2416 |
| 2417 TestBluetoothAdapterObserver observer(adapter_); |
| 2418 |
| 2419 // Starts first discovery process. |
| 2420 SimulateGattConnection(device); |
| 2421 AddServicesToDevice(device, {kTestUUIDHeartRate}); |
| 2422 SimulateDidDiscoverServices(device); |
| 2423 EXPECT_EQ(1u, device->GetGattServices().size()); |
| 2424 BluetoothRemoteGattService* service = device->GetGattServices()[0]; |
| 2425 std::string characteristic_uuid = "11111111-0000-1000-8000-00805f9b34fb"; |
| 2426 AddCharacteristicToService(service, characteristic_uuid, /* properties */ 0); |
| 2427 SimulateDidDiscoverCharacteristics(service); |
| 2428 EXPECT_EQ(1u, service->GetCharacteristics().size()); |
| 2429 BluetoothRemoteGattCharacteristic* characteristic = |
| 2430 service->GetCharacteristics()[0]; |
| 2431 std::string descriptor_uuid = "22222222-0000-1000-8000-00805f9b34fb"; |
| 2432 AddDescriptorToCharacteristic(characteristic, descriptor_uuid); |
| 2433 // Now waiting for descriptor discovery. |
| 2434 |
| 2435 // Starts second discovery process. |
| 2436 SimulateGattServicesChanged(device); |
| 2437 SimulateDidDiscoverServices(device); |
| 2438 SimulateDidDiscoverCharacteristics(service); |
| 2439 // Now waiting for a second descriptor discovery. |
| 2440 |
| 2441 // Finish discovery process. |
| 2442 // First system call to -[id<CBPeripheralDelegate> |
| 2443 // peripheral:didDiscoverDescriptorsForCharacteristic:error:] |
| 2444 SimulateDidDiscoverDescriptors(characteristic); |
| 2445 EXPECT_EQ(0, observer.gatt_service_changed_count()); |
| 2446 EXPECT_EQ(1u, service->GetCharacteristics().size()); |
| 2447 |
| 2448 // Second system call to -[id<CBPeripheralDelegate> |
| 2449 // peripheral:didDiscoverDescriptorsForCharacteristic:error:] |
| 2450 // Finish second discovery process. |
| 2451 SimulateDidDiscoverDescriptors(characteristic); |
| 2452 EXPECT_EQ(1, observer.gatt_service_changed_count()); |
| 2453 } |
| 2454 |
| 2455 // Simulates to receive an extra discvoery descriptor notification from macOS. |
| 2456 TEST_F(BluetoothRemoteGattCharacteristicTest, |
| 2457 ExtraDidDiscoverCharacteristicsCall) { |
| 2458 if (!PlatformSupportsLowEnergy()) { |
| 2459 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
| 2460 return; |
| 2461 } |
| 2462 InitWithFakeAdapter(); |
| 2463 StartLowEnergyDiscoverySession(); |
| 2464 BluetoothDevice* device = SimulateLowEnergyDevice(3); |
| 2465 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
| 2466 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
| 2467 |
| 2468 TestBluetoothAdapterObserver observer(adapter_); |
| 2469 |
| 2470 // Starts first discovery process. |
| 2471 SimulateGattConnection(device); |
| 2472 AddServicesToDevice(device, {kTestUUIDHeartRate}); |
| 2473 SimulateDidDiscoverServices(device); |
| 2474 EXPECT_EQ(1u, device->GetGattServices().size()); |
| 2475 BluetoothRemoteGattService* service = device->GetGattServices()[0]; |
| 2476 std::string characteristic_uuid = "11111111-0000-1000-8000-00805f9b34fb"; |
| 2477 AddCharacteristicToService(service, characteristic_uuid, /* properties */ 0); |
| 2478 SimulateDidDiscoverCharacteristics(service); |
| 2479 EXPECT_EQ(1u, service->GetCharacteristics().size()); |
| 2480 BluetoothRemoteGattCharacteristic* characteristic = |
| 2481 service->GetCharacteristics()[0]; |
| 2482 |
| 2483 // Extra system call to -[id<CBPeripheralDelegate> |
| 2484 // peripheral:didDiscoverDescriptorsForCharacteristic:error:] |
| 2485 SimulateDidDiscoverDescriptors(characteristic); |
| 2486 |
| 2487 std::string descriptor_uuid = "22222222-0000-1000-8000-00805f9b34fb"; |
| 2488 AddDescriptorToCharacteristic(characteristic, descriptor_uuid); |
| 2489 SimulateDidDiscoverDescriptors(characteristic); |
| 2490 EXPECT_EQ(1, observer.gatt_service_changed_count()); |
| 2491 EXPECT_EQ(1u, service->GetCharacteristics().size()); |
| 2492 |
| 2493 // Extra system call to -[id<CBPeripheralDelegate> |
| 2494 // peripheral:didDiscoverDescriptorsForCharacteristic:error:] |
| 2495 SimulateDidDiscoverDescriptors(characteristic); |
| 2496 EXPECT_EQ(1, observer.gatt_service_changed_count()); |
| 2497 } |
| 2498 #endif // defined(OS_MACOSX) |
2401 } // namespace device | 2499 } // namespace device |
OLD | NEW |