Chromium Code Reviews| 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 "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 Loading... | |
| 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, while waiting for | |
|
scheib
2017/04/10 20:55:54
The comment for TwoPendingServiceDiscoveryRequests
jlebel
2017/04/11 20:44:41
Is my comment clear?
| |
| 288 // characteristics. | |
| 289 TEST_F(BluetoothRemoteGattServiceTest, | |
| 290 SimulateDeviceModificationWhileDiscoveringCharacteristics) { | |
| 291 if (!PlatformSupportsLowEnergy()) { | |
| 292 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | |
| 293 return; | |
| 294 } | |
| 295 InitWithFakeAdapter(); | |
| 296 StartLowEnergyDiscoverySession(); | |
| 297 BluetoothDevice* device = SimulateLowEnergyDevice(3); | |
| 298 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | |
| 299 GetConnectErrorCallback(Call::NOT_EXPECTED)); | |
| 300 | |
| 301 TestBluetoothAdapterObserver observer(adapter_); | |
| 302 | |
| 303 // Starts first discovery process. | |
| 304 SimulateGattConnection(device); | |
| 305 AddServicesToDevice(device, {kTestUUIDHeartRate}); | |
| 306 SimulateDidDiscoverServices(device); | |
| 307 EXPECT_EQ(1u, device->GetGattServices().size()); | |
| 308 BluetoothRemoteGattService* service = device->GetGattServices()[0]; | |
| 309 std::string characteristic_uuid1 = "11111111-0000-1000-8000-00805f9b34fb"; | |
| 310 AddCharacteristicToService(service, characteristic_uuid1, /* properties */ 0); | |
| 311 // Now waiting for characteristic discovery. | |
| 312 | |
| 313 // Starts second discovery process. | |
| 314 SimulateGattServicesChanged(device); | |
| 315 SimulateDidDiscoverServices(device); | |
| 316 // Now waiting for the second characteristic discovery. | |
| 317 | |
| 318 // First system call to -[id<CBPeripheralDelegate> | |
| 319 // peripheral:didDiscoverCharacteristicsForService:error:] | |
| 320 SimulateDidDiscoverCharacteristics(service); | |
| 321 EXPECT_EQ(0, observer.gatt_service_changed_count()); | |
| 322 EXPECT_EQ(1u, service->GetCharacteristics().size()); | |
| 323 | |
| 324 // Finish discovery process. | |
| 325 std::string characteristic_uuid2 = "22222222-0000-1000-8000-00805f9b34fb"; | |
| 326 AddCharacteristicToService(service, characteristic_uuid2, /* properties */ 0); | |
| 327 // Second system call to -[id<CBPeripheralDelegate> | |
| 328 // peripheral:didDiscoverCharacteristicsForService:error:] | |
| 329 SimulateDidDiscoverCharacteristics(service); | |
| 330 EXPECT_EQ(2u, service->GetCharacteristics().size()); | |
| 331 EXPECT_EQ(0, observer.gatt_service_changed_count()); | |
| 332 BluetoothRemoteGattCharacteristic* characteristic1 = | |
| 333 service->GetCharacteristics()[0]; | |
| 334 BluetoothRemoteGattCharacteristic* characteristic2 = | |
| 335 service->GetCharacteristics()[1]; | |
| 336 if (characteristic1->GetUUID().canonical_value() == characteristic_uuid2) { | |
| 337 BluetoothRemoteGattCharacteristic* tmp = characteristic1; | |
| 338 characteristic1 = characteristic2; | |
| 339 characteristic2 = tmp; | |
| 340 } | |
| 341 EXPECT_EQ(characteristic_uuid1, characteristic1->GetUUID().canonical_value()); | |
| 342 EXPECT_EQ(characteristic_uuid2, characteristic2->GetUUID().canonical_value()); | |
| 343 SimulateDidDiscoverDescriptors(characteristic1); | |
| 344 SimulateDidDiscoverDescriptors(characteristic2); | |
| 345 EXPECT_EQ(1, observer.gatt_service_changed_count()); | |
| 346 } | |
| 347 | |
| 348 // Simulates to receive an extra discvoery characteristic notification from | |
|
scheib
2017/04/10 20:55:54
Keep the #if defined mac guard around each test. A
jlebel
2017/04/11 20:44:41
Done.
| |
| 349 // macOS. | |
| 350 TEST_F(BluetoothRemoteGattServiceTest, ExtraDidDiscoverCharacteristicsCall) { | |
| 351 if (!PlatformSupportsLowEnergy()) { | |
| 352 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | |
| 353 return; | |
| 354 } | |
| 355 InitWithFakeAdapter(); | |
| 356 StartLowEnergyDiscoverySession(); | |
| 357 BluetoothDevice* device = SimulateLowEnergyDevice(3); | |
| 358 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | |
| 359 GetConnectErrorCallback(Call::NOT_EXPECTED)); | |
| 360 | |
| 361 TestBluetoothAdapterObserver observer(adapter_); | |
| 362 | |
| 363 // Starts first discovery process. | |
| 364 SimulateGattConnection(device); | |
| 365 AddServicesToDevice(device, {kTestUUIDHeartRate}); | |
| 366 SimulateDidDiscoverServices(device); | |
| 367 EXPECT_EQ(1u, device->GetGattServices().size()); | |
| 368 BluetoothRemoteGattService* service = device->GetGattServices()[0]; | |
| 369 std::string characteristic_uuid = "11111111-0000-1000-8000-00805f9b34fb"; | |
| 370 AddCharacteristicToService(service, characteristic_uuid, /* properties */ 0); | |
| 371 SimulateDidDiscoverServices(device); | |
| 372 | |
| 373 // Legitimate system call to -[id<CBPeripheralDelegate> | |
| 374 // peripheral:didDiscoverCharacteristicsForService:error:] | |
| 375 SimulateDidDiscoverCharacteristics(service); | |
| 376 EXPECT_EQ(1u, service->GetCharacteristics().size()); | |
| 377 EXPECT_EQ(0, observer.gatt_service_changed_count()); | |
| 378 | |
| 379 // Unexpected system call to -[id<CBPeripheralDelegate> | |
| 380 // peripheral:didDiscoverCharacteristicsForService:error:] | |
| 381 SimulateDidDiscoverCharacteristics(service); | |
| 382 EXPECT_EQ(1u, service->GetCharacteristics().size()); | |
| 383 EXPECT_EQ(0, observer.gatt_service_changed_count()); | |
| 384 BluetoothRemoteGattCharacteristic* characteristic = | |
| 385 service->GetCharacteristics()[0]; | |
| 386 EXPECT_EQ(characteristic_uuid, characteristic->GetUUID().canonical_value()); | |
| 387 SimulateDidDiscoverDescriptors(characteristic); | |
| 388 EXPECT_EQ(1, observer.gatt_service_changed_count()); | |
| 389 | |
| 390 // Unexpected system call to -[id<CBPeripheralDelegate> | |
| 391 // peripheral:didDiscoverCharacteristicsForService:error:] | |
| 392 SimulateDidDiscoverCharacteristics(service); | |
| 393 EXPECT_EQ(1, observer.gatt_service_changed_count()); | |
| 394 } | |
| 395 #endif // defined(OS_MACOSX) | |
| 285 | 396 |
| 286 } // namespace device | 397 } // namespace device |
| OLD | NEW |