OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "device/bluetooth/bluetooth_device.h" | 5 #include "device/bluetooth/bluetooth_device.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
(...skipping 377 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
388 // the device holds and notify of device changed. | 388 // the device holds and notify of device changed. |
389 gatt_connections_[0]->Disconnect(); | 389 gatt_connections_[0]->Disconnect(); |
390 SimulateGattDisconnection(device); | 390 SimulateGattDisconnection(device); |
391 ASSERT_FALSE(device->IsGattConnected()); | 391 ASSERT_FALSE(device->IsGattConnected()); |
392 | 392 |
393 EXPECT_EQ(1, observer.device_changed_count()); | 393 EXPECT_EQ(1, observer.device_changed_count()); |
394 EXPECT_TRUE(device->GetUUIDs().empty()); | 394 EXPECT_TRUE(device->GetUUIDs().empty()); |
395 } | 395 } |
396 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) | 396 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) |
397 | 397 |
| 398 #if defined(OS_MACOSX) |
| 399 // Tests that receiving 2 notifications in a row from macOS that services has |
| 400 // changed is handled correctly. Each notification should generate a notfication |
| 401 // that the gatt device has changed, and each notification should ask to macOS |
| 402 // to scan for services. Only after the second service scan is received, the |
| 403 // device changed notification should be sent and the characteristic discovery |
| 404 // procedure should be started. |
| 405 // Android: This test doesn't apply to Android because there is no services |
| 406 // changed event that could arrive during a discovery procedure. |
| 407 TEST_F(BluetoothTest, TwoPendingServiceDiscoveryRequests) { |
| 408 if (!PlatformSupportsLowEnergy()) { |
| 409 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
| 410 return; |
| 411 } |
| 412 |
| 413 InitWithFakeAdapter(); |
| 414 TestBluetoothAdapterObserver observer(adapter_); |
| 415 |
| 416 BluetoothDevice* device = SimulateLowEnergyDevice(1); |
| 417 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
| 418 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
| 419 SimulateGattConnection(device); |
| 420 EXPECT_EQ(1, observer.device_changed_count()); |
| 421 EXPECT_FALSE(device->IsGattServicesDiscoveryComplete()); |
| 422 |
| 423 observer.Reset(); |
| 424 SimulateGattServicesChanged(device); |
| 425 EXPECT_EQ(1, observer.device_changed_count()); |
| 426 EXPECT_FALSE(device->IsGattServicesDiscoveryComplete()); |
| 427 |
| 428 // Fist system call to |
| 429 // -[id<CBPeripheralDelegate> peripheral:didDiscoverServices:] |
| 430 observer.Reset(); |
| 431 SimulateDidDiscoverServices(device, {kTestUUIDHeartRate}); |
| 432 EXPECT_EQ(0, observer.device_changed_count()); |
| 433 EXPECT_FALSE(device->IsGattServicesDiscoveryComplete()); |
| 434 EXPECT_EQ(gatt_characteristic_discovery_attempts_, 0); |
| 435 |
| 436 // Second system call to |
| 437 // -[id<CBPeripheralDelegate> peripheral:didDiscoverServices:] |
| 438 SimulateGattServicesDiscovered( |
| 439 device, std::vector<std::string>({kTestUUIDImmediateAlert})); |
| 440 EXPECT_EQ(1, observer.device_changed_count()); |
| 441 EXPECT_TRUE(device->IsGattServicesDiscoveryComplete()); |
| 442 // Characteristics are discovered once for each service. |
| 443 EXPECT_EQ(gatt_characteristic_discovery_attempts_, 2); |
| 444 |
| 445 EXPECT_EQ(2u, device->GetGattServices().size()); |
| 446 } |
| 447 |
| 448 // Simulate an unexpected call to -[id<CBPeripheralDelegate> |
| 449 // peripheral:didDiscoverServices:]. This should not happen, but if it does |
| 450 // (buggy device?), a discovery cycle should be done. |
| 451 TEST_F(BluetoothTest, ExtraDidDiscoverServicesCall) { |
| 452 if (!PlatformSupportsLowEnergy()) { |
| 453 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
| 454 return; |
| 455 } |
| 456 |
| 457 InitWithFakeAdapter(); |
| 458 TestBluetoothAdapterObserver observer(adapter_); |
| 459 |
| 460 BluetoothDevice* device = SimulateLowEnergyDevice(1); |
| 461 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
| 462 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
| 463 SimulateGattConnection(device); |
| 464 EXPECT_EQ(1, observer.device_changed_count()); |
| 465 EXPECT_FALSE(device->IsGattServicesDiscoveryComplete()); |
| 466 |
| 467 // Legitimate system call to |
| 468 // -[id<CBPeripheralDelegate> peripheral:didDiscoverServices:] |
| 469 observer.Reset(); |
| 470 SimulateGattServicesDiscovered( |
| 471 device, std::vector<std::string>({kTestUUIDHeartRate})); |
| 472 EXPECT_EQ(1, observer.device_changed_count()); |
| 473 EXPECT_TRUE(device->IsGattServicesDiscoveryComplete()); |
| 474 EXPECT_EQ(gatt_characteristic_discovery_attempts_, 1); |
| 475 EXPECT_EQ(1u, device->GetGattServices().size()); |
| 476 |
| 477 // Unexpected system call to |
| 478 // -[id<CBPeripheralDelegate> peripheral:didDiscoverServices:] |
| 479 SimulateDidDiscoverServices(device, {kTestUUIDImmediateAlert}); |
| 480 EXPECT_EQ(1, observer.device_changed_count()); |
| 481 EXPECT_TRUE(device->IsGattServicesDiscoveryComplete()); |
| 482 |
| 483 EXPECT_EQ(1u, device->GetGattServices().size()); |
| 484 } |
| 485 #endif // defined(OS_MACOSX) |
| 486 |
398 #if defined(OS_ANDROID) || defined(OS_MACOSX) | 487 #if defined(OS_ANDROID) || defined(OS_MACOSX) |
399 // Tests Advertisement Data is updated correctly when we start discovery | 488 // Tests Advertisement Data is updated correctly when we start discovery |
400 // during a connection. | 489 // during a connection. |
401 TEST_F(BluetoothTest, AdvertisementData_DiscoveryDuringConnection) { | 490 TEST_F(BluetoothTest, AdvertisementData_DiscoveryDuringConnection) { |
402 if (!PlatformSupportsLowEnergy()) { | 491 if (!PlatformSupportsLowEnergy()) { |
403 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | 492 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
404 return; | 493 return; |
405 } | 494 } |
406 | 495 |
407 InitWithFakeAdapter(); | 496 InitWithFakeAdapter(); |
(...skipping 924 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1332 | 1421 |
1333 BluetoothDevice* device2 = SimulateLowEnergyDevice(6); | 1422 BluetoothDevice* device2 = SimulateLowEnergyDevice(6); |
1334 EXPECT_EQ(BLUETOOTH_TRANSPORT_DUAL, device2->GetType()); | 1423 EXPECT_EQ(BLUETOOTH_TRANSPORT_DUAL, device2->GetType()); |
1335 | 1424 |
1336 BluetoothDevice* device3 = SimulateClassicDevice(); | 1425 BluetoothDevice* device3 = SimulateClassicDevice(); |
1337 EXPECT_EQ(BLUETOOTH_TRANSPORT_CLASSIC, device3->GetType()); | 1426 EXPECT_EQ(BLUETOOTH_TRANSPORT_CLASSIC, device3->GetType()); |
1338 } | 1427 } |
1339 #endif // defined(OS_CHROMEOS) || defined(OS_LINUX) | 1428 #endif // defined(OS_CHROMEOS) || defined(OS_LINUX) |
1340 | 1429 |
1341 } // namespace device | 1430 } // namespace device |
OLD | NEW |