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

Side by Side Diff: device/bluetooth/bluetooth_remote_gatt_characteristic_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 <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
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 from macOS, while
2404 // discovering descriptors. This test simulate having 2 descriptor scan at the
2405 // same time. Only once both descriptor scanning is done, the gatt device is
2406 // ready.
2407 // Android: This test doesn't apply to Android because there is no services
2408 // changed event that could arrive during a discovery procedure.
2409 TEST_F(BluetoothRemoteGattCharacteristicTest,
2410 SimulateDeviceModificationWhileDiscoveringDescriptors) {
2411 if (!PlatformSupportsLowEnergy()) {
2412 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
2413 return;
2414 }
2415 InitWithFakeAdapter();
2416 StartLowEnergyDiscoverySession();
2417 BluetoothDevice* device = SimulateLowEnergyDevice(3);
2418 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
2419 GetConnectErrorCallback(Call::NOT_EXPECTED));
2420
2421 TestBluetoothAdapterObserver observer(adapter_);
2422
2423 // Starts first discovery process.
2424 SimulateGattConnection(device);
2425 EXPECT_EQ(1, observer.device_changed_count());
2426 AddServicesToDevice(device, {kTestUUIDHeartRate});
2427 SimulateDidDiscoverServices(device);
2428 EXPECT_EQ(1u, device->GetGattServices().size());
2429 BluetoothRemoteGattService* service = device->GetGattServices()[0];
2430 std::string characteristic_uuid = "11111111-0000-1000-8000-00805f9b34fb";
2431 AddCharacteristicToService(service, characteristic_uuid, /* properties */ 0);
2432 SimulateDidDiscoverCharacteristics(service);
2433 EXPECT_EQ(1u, service->GetCharacteristics().size());
2434 BluetoothRemoteGattCharacteristic* characteristic =
2435 service->GetCharacteristics()[0];
2436 std::string descriptor_uuid = "22222222-0000-1000-8000-00805f9b34fb";
2437 AddDescriptorToCharacteristic(characteristic, descriptor_uuid);
2438 // Now waiting for descriptor discovery.
2439
2440 // Starts second discovery process.
2441 SimulateGattServicesChanged(device);
2442 EXPECT_EQ(2, observer.device_changed_count());
2443 SimulateDidDiscoverServices(device);
2444 SimulateDidDiscoverCharacteristics(service);
2445 // Now waiting for a second descriptor discovery.
2446
2447 // Finish discovery process.
2448 // First system call to -[id<CBPeripheralDelegate>
2449 // peripheral:didDiscoverDescriptorsForCharacteristic:error:]
2450 SimulateDidDiscoverDescriptors(characteristic);
2451 EXPECT_EQ(0, observer.gatt_service_changed_count());
2452 EXPECT_EQ(1u, service->GetCharacteristics().size());
2453 EXPECT_EQ(1u, characteristic->GetDescriptors().size());
2454 EXPECT_EQ(2, observer.device_changed_count());
2455
2456 // Second system call to -[id<CBPeripheralDelegate>
2457 // peripheral:didDiscoverDescriptorsForCharacteristic:error:]
2458 // Finish second discovery process.
2459 observer.Reset();
2460 SimulateDidDiscoverDescriptors(characteristic);
2461 EXPECT_EQ(1, observer.gatt_service_changed_count());
2462 EXPECT_EQ(1, observer.device_changed_count());
2463 }
2464 #endif // defined(OS_MACOSX)
2465
2466 #if defined(OS_MACOSX)
2467 // Simulates to receive an extra discovery descriptor notifications from macOS.
2468 // Those notifications should be ignored.
2469 // Android: This test doesn't apply to Android because there is no services
2470 // changed event that could arrive during a discovery procedure.
2471 TEST_F(BluetoothRemoteGattCharacteristicTest, ExtraDidDiscoverDescriptorsCall) {
2472 if (!PlatformSupportsLowEnergy()) {
2473 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
2474 return;
2475 }
2476 InitWithFakeAdapter();
2477 StartLowEnergyDiscoverySession();
2478 BluetoothDevice* device = SimulateLowEnergyDevice(3);
2479 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
2480 GetConnectErrorCallback(Call::NOT_EXPECTED));
2481
2482 TestBluetoothAdapterObserver observer(adapter_);
2483
2484 // Starts first discovery process.
2485 SimulateGattConnection(device);
2486 AddServicesToDevice(device, {kTestUUIDHeartRate});
2487 SimulateDidDiscoverServices(device);
2488 EXPECT_EQ(1u, device->GetGattServices().size());
2489 BluetoothRemoteGattService* service = device->GetGattServices()[0];
2490 std::string characteristic_uuid = "11111111-0000-1000-8000-00805f9b34fb";
2491 AddCharacteristicToService(service, characteristic_uuid, /* properties */ 0);
2492 SimulateDidDiscoverCharacteristics(service);
2493 EXPECT_EQ(1u, service->GetCharacteristics().size());
2494 BluetoothRemoteGattCharacteristic* characteristic =
2495 service->GetCharacteristics()[0];
2496 std::string descriptor_uuid = "22222222-0000-1000-8000-00805f9b34fb";
2497 AddDescriptorToCharacteristic(characteristic, descriptor_uuid);
2498 SimulateDidDiscoverDescriptors(characteristic);
2499 EXPECT_EQ(1, observer.gatt_service_changed_count());
2500 EXPECT_EQ(1u, service->GetCharacteristics().size());
2501 EXPECT_EQ(1u, characteristic->GetDescriptors().size());
2502
2503 observer.Reset();
2504 SimulateDidDiscoverDescriptors(characteristic); // Extra system call.
2505 SimulateGattServicesChanged(device);
2506 SimulateDidDiscoverDescriptors(characteristic); // Extra system call.
2507 SimulateDidDiscoverServices(device);
2508 SimulateDidDiscoverDescriptors(characteristic); // Extra system call.
2509 SimulateDidDiscoverCharacteristics(service);
2510 SimulateDidDiscoverDescriptors(characteristic);
2511 SimulateDidDiscoverDescriptors(characteristic); // Extra system call.
2512 EXPECT_EQ(2, observer.device_changed_count());
2513 }
2514 #endif // defined(OS_MACOSX)
2401 } // namespace device 2515 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698