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 1258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1269 // Connect to the device | 1269 // Connect to the device |
1270 ResetEventCounts(); | 1270 ResetEventCounts(); |
1271 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 1271 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
1272 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 1272 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
1273 TestBluetoothAdapterObserver observer(adapter_); | 1273 TestBluetoothAdapterObserver observer(adapter_); |
1274 SimulateGattConnection(device); | 1274 SimulateGattConnection(device); |
1275 base::RunLoop().RunUntilIdle(); | 1275 base::RunLoop().RunUntilIdle(); |
1276 EXPECT_TRUE(device->IsConnected()); | 1276 EXPECT_TRUE(device->IsConnected()); |
1277 | 1277 |
1278 // Discover services | 1278 // Discover services |
1279 std::vector<std::string> services; | 1279 SimulateGattServicesDiscovered( |
1280 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); | 1280 device, |
1281 services.push_back("00000001-0000-1000-8000-00805f9b34fb"); | 1281 std::vector<std::string>({kTestUUIDGenericAccess, kTestUUIDHeartRate})); |
1282 SimulateGattServicesDiscovered(device, services); | |
1283 base::RunLoop().RunUntilIdle(); | 1282 base::RunLoop().RunUntilIdle(); |
1284 EXPECT_TRUE(device->IsGattServicesDiscoveryComplete()); | 1283 EXPECT_TRUE(device->IsGattServicesDiscoveryComplete()); |
1285 EXPECT_EQ(2u, device->GetGattServices().size()); | 1284 EXPECT_EQ(2u, device->GetGattServices().size()); |
1286 EXPECT_EQ(1, observer.gatt_services_discovered_count()); | 1285 EXPECT_EQ(1, observer.gatt_services_discovered_count()); |
1287 | 1286 |
1288 // Disconnect from the device | 1287 // Disconnect from the device |
1289 device->DisconnectGatt(); | 1288 device->DisconnectGatt(); |
1290 SimulateGattDisconnection(device); | 1289 SimulateGattDisconnection(device); |
1291 base::RunLoop().RunUntilIdle(); | 1290 base::RunLoop().RunUntilIdle(); |
1292 EXPECT_FALSE(device->IsConnected()); | 1291 EXPECT_FALSE(device->IsConnected()); |
1293 EXPECT_FALSE(device->IsGattServicesDiscoveryComplete()); | 1292 EXPECT_FALSE(device->IsGattServicesDiscoveryComplete()); |
1294 EXPECT_EQ(0u, device->GetGattServices().size()); | 1293 EXPECT_EQ(0u, device->GetGattServices().size()); |
1295 | 1294 |
1296 // Verify that the device can be connected to again | 1295 // Verify that the device can be connected to again |
1297 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 1296 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
1298 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 1297 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
1299 SimulateGattConnection(device); | 1298 SimulateGattConnection(device); |
1300 base::RunLoop().RunUntilIdle(); | 1299 base::RunLoop().RunUntilIdle(); |
1301 EXPECT_TRUE(device->IsConnected()); | 1300 EXPECT_TRUE(device->IsConnected()); |
1302 | 1301 |
1303 // Verify that service discovery can be done again | 1302 // Verify that service discovery can be done again |
1304 std::vector<std::string> services2; | 1303 SimulateGattServicesDiscovered( |
1305 services2.push_back("00000002-0000-1000-8000-00805f9b34fb"); | 1304 device, std::vector<std::string>({kTestUUIDGenericAttribute})); |
1306 SimulateGattServicesDiscovered(device, services2); | |
1307 base::RunLoop().RunUntilIdle(); | 1305 base::RunLoop().RunUntilIdle(); |
1308 EXPECT_TRUE(device->IsGattServicesDiscoveryComplete()); | 1306 EXPECT_TRUE(device->IsGattServicesDiscoveryComplete()); |
1309 EXPECT_EQ(1u, device->GetGattServices().size()); | 1307 EXPECT_EQ(1u, device->GetGattServices().size()); |
1310 EXPECT_EQ(2, observer.gatt_services_discovered_count()); | 1308 EXPECT_EQ(2, observer.gatt_services_discovered_count()); |
1311 } | 1309 } |
1312 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) | 1310 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) |
1313 | 1311 |
1314 #if defined(OS_ANDROID) || defined(OS_MACOSX) | 1312 #if defined(OS_ANDROID) || defined(OS_MACOSX) |
1315 // Calls CreateGattConnection, but simulate errors connecting. Also, verifies | 1313 // Calls CreateGattConnection, but simulate errors connecting. Also, verifies |
1316 // multiple errors should only invoke callbacks once. | 1314 // multiple errors should only invoke callbacks once. |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1354 StartLowEnergyDiscoverySession(); | 1352 StartLowEnergyDiscoverySession(); |
1355 BluetoothDevice* device = SimulateLowEnergyDevice(3); | 1353 BluetoothDevice* device = SimulateLowEnergyDevice(3); |
1356 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 1354 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
1357 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 1355 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
1358 TestBluetoothAdapterObserver observer(adapter_); | 1356 TestBluetoothAdapterObserver observer(adapter_); |
1359 ResetEventCounts(); | 1357 ResetEventCounts(); |
1360 SimulateGattConnection(device); | 1358 SimulateGattConnection(device); |
1361 base::RunLoop().RunUntilIdle(); | 1359 base::RunLoop().RunUntilIdle(); |
1362 EXPECT_EQ(1, gatt_discovery_attempts_); | 1360 EXPECT_EQ(1, gatt_discovery_attempts_); |
1363 | 1361 |
1364 std::vector<std::string> services; | 1362 SimulateGattServicesDiscovered( |
1365 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); | 1363 device, |
1366 services.push_back("00000001-0000-1000-8000-00805f9b34fb"); | 1364 std::vector<std::string>({kTestUUIDGenericAccess, kTestUUIDHeartRate})); |
1367 SimulateGattServicesDiscovered(device, services); | |
1368 base::RunLoop().RunUntilIdle(); | 1365 base::RunLoop().RunUntilIdle(); |
1369 | 1366 |
1370 EXPECT_EQ(1, observer.gatt_services_discovered_count()); | 1367 EXPECT_EQ(1, observer.gatt_services_discovered_count()); |
1371 EXPECT_EQ(2, observer.gatt_service_added_count()); | 1368 EXPECT_EQ(2, observer.gatt_service_added_count()); |
1372 } | 1369 } |
1373 #endif // defined(OS_ANDROID) || defined(OS_WIN) || defined(OS_MACOSX) | 1370 #endif // defined(OS_ANDROID) || defined(OS_WIN) || defined(OS_MACOSX) |
1374 | 1371 |
1375 #if defined(OS_ANDROID) | 1372 #if defined(OS_ANDROID) |
1376 // macOS: Not applicable: This can never happen because when | 1373 // macOS: Not applicable: This can never happen because when |
1377 // the device gets destroyed the CBPeripheralDelegate is also destroyed | 1374 // the device gets destroyed the CBPeripheralDelegate is also destroyed |
(...skipping 11 matching lines...) Expand all Loading... |
1389 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 1386 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
1390 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 1387 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
1391 ResetEventCounts(); | 1388 ResetEventCounts(); |
1392 SimulateGattConnection(device); | 1389 SimulateGattConnection(device); |
1393 base::RunLoop().RunUntilIdle(); | 1390 base::RunLoop().RunUntilIdle(); |
1394 EXPECT_EQ(1, gatt_discovery_attempts_); | 1391 EXPECT_EQ(1, gatt_discovery_attempts_); |
1395 | 1392 |
1396 RememberDeviceForSubsequentAction(device); | 1393 RememberDeviceForSubsequentAction(device); |
1397 DeleteDevice(device); | 1394 DeleteDevice(device); |
1398 | 1395 |
1399 std::vector<std::string> services; | 1396 SimulateGattServicesDiscovered( |
1400 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); | 1397 nullptr /* use remembered device */, |
1401 services.push_back("00000001-0000-1000-8000-00805f9b34fb"); | 1398 std::vector<std::string>({kTestUUIDGenericAccess, kTestUUIDHeartRate})); |
1402 SimulateGattServicesDiscovered(nullptr /* use remembered device */, services); | |
1403 base::RunLoop().RunUntilIdle(); | 1399 base::RunLoop().RunUntilIdle(); |
1404 } | 1400 } |
1405 #endif // defined(OS_ANDROID) | 1401 #endif // defined(OS_ANDROID) |
1406 | 1402 |
1407 #if defined(OS_ANDROID) | 1403 #if defined(OS_ANDROID) |
1408 // macOS: Not applicable: This can never happen because when | 1404 // macOS: Not applicable: This can never happen because when |
1409 // the device gets destroyed the CBPeripheralDelegate is also destroyed | 1405 // the device gets destroyed the CBPeripheralDelegate is also destroyed |
1410 // and no more events are dispatched. | 1406 // and no more events are dispatched. |
1411 TEST_F(BluetoothTest, GattServicesDiscoveredError_AfterDeleted) { | 1407 TEST_F(BluetoothTest, GattServicesDiscoveredError_AfterDeleted) { |
1412 // Tests that we don't crash if there was an error discoverying services | 1408 // Tests that we don't crash if there was an error discoverying services |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1447 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 1443 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
1448 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 1444 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
1449 ResetEventCounts(); | 1445 ResetEventCounts(); |
1450 SimulateGattConnection(device); | 1446 SimulateGattConnection(device); |
1451 base::RunLoop().RunUntilIdle(); | 1447 base::RunLoop().RunUntilIdle(); |
1452 EXPECT_EQ(1, gatt_discovery_attempts_); | 1448 EXPECT_EQ(1, gatt_discovery_attempts_); |
1453 | 1449 |
1454 SimulateGattDisconnection(device); | 1450 SimulateGattDisconnection(device); |
1455 base::RunLoop().RunUntilIdle(); | 1451 base::RunLoop().RunUntilIdle(); |
1456 | 1452 |
1457 std::vector<std::string> services; | 1453 SimulateGattServicesDiscovered( |
1458 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); | 1454 device, |
1459 services.push_back("00000001-0000-1000-8000-00805f9b34fb"); | 1455 std::vector<std::string>({kTestUUIDGenericAccess, kTestUUIDHeartRate})); |
1460 SimulateGattServicesDiscovered(device, services); | |
1461 base::RunLoop().RunUntilIdle(); | 1456 base::RunLoop().RunUntilIdle(); |
1462 | 1457 |
1463 EXPECT_FALSE(device->IsGattServicesDiscoveryComplete()); | 1458 EXPECT_FALSE(device->IsGattServicesDiscoveryComplete()); |
1464 EXPECT_EQ(0u, device->GetGattServices().size()); | 1459 EXPECT_EQ(0u, device->GetGattServices().size()); |
1465 } | 1460 } |
1466 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) | 1461 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) |
1467 | 1462 |
1468 #if defined(OS_ANDROID) || defined(OS_MACOSX) | 1463 #if defined(OS_ANDROID) || defined(OS_MACOSX) |
1469 TEST_F(BluetoothTest, GattServicesDiscoveredError_AfterDisconnection) { | 1464 TEST_F(BluetoothTest, GattServicesDiscoveredError_AfterDisconnection) { |
1470 // Tests that we don't crash if services are discovered after | 1465 // Tests that we don't crash if services are discovered after |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1502 InitWithFakeAdapter(); | 1497 InitWithFakeAdapter(); |
1503 StartLowEnergyDiscoverySession(); | 1498 StartLowEnergyDiscoverySession(); |
1504 BluetoothDevice* device = SimulateLowEnergyDevice(3); | 1499 BluetoothDevice* device = SimulateLowEnergyDevice(3); |
1505 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 1500 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
1506 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 1501 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
1507 ResetEventCounts(); | 1502 ResetEventCounts(); |
1508 SimulateGattConnection(device); | 1503 SimulateGattConnection(device); |
1509 base::RunLoop().RunUntilIdle(); | 1504 base::RunLoop().RunUntilIdle(); |
1510 EXPECT_EQ(1, gatt_discovery_attempts_); | 1505 EXPECT_EQ(1, gatt_discovery_attempts_); |
1511 | 1506 |
1512 std::vector<std::string> services; | |
1513 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); | |
1514 // 2 duplicate UUIDs creating 2 instances. | 1507 // 2 duplicate UUIDs creating 2 instances. |
1515 services.push_back("00000001-0000-1000-8000-00805f9b34fb"); | 1508 SimulateGattServicesDiscovered( |
1516 services.push_back("00000001-0000-1000-8000-00805f9b34fb"); | 1509 device, |
1517 SimulateGattServicesDiscovered(device, services); | 1510 std::vector<std::string>( |
| 1511 {kTestUUIDGenericAccess, kTestUUIDHeartRate, kTestUUIDHeartRate})); |
1518 base::RunLoop().RunUntilIdle(); | 1512 base::RunLoop().RunUntilIdle(); |
1519 EXPECT_EQ(3u, device->GetGattServices().size()); | 1513 EXPECT_EQ(3u, device->GetGattServices().size()); |
1520 | 1514 |
1521 // Test GetGattService: | 1515 // Test GetGattService: |
1522 std::string service_id1 = device->GetGattServices()[0]->GetIdentifier(); | 1516 std::string service_id1 = device->GetGattServices()[0]->GetIdentifier(); |
1523 std::string service_id2 = device->GetGattServices()[1]->GetIdentifier(); | 1517 std::string service_id2 = device->GetGattServices()[1]->GetIdentifier(); |
1524 std::string service_id3 = device->GetGattServices()[2]->GetIdentifier(); | 1518 std::string service_id3 = device->GetGattServices()[2]->GetIdentifier(); |
1525 EXPECT_TRUE(device->GetGattService(service_id1)); | 1519 EXPECT_TRUE(device->GetGattService(service_id1)); |
1526 EXPECT_TRUE(device->GetGattService(service_id2)); | 1520 EXPECT_TRUE(device->GetGattService(service_id2)); |
1527 EXPECT_TRUE(device->GetGattService(service_id3)); | 1521 EXPECT_TRUE(device->GetGattService(service_id3)); |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1604 ->GetPrimaryServicesByUUID(BluetoothUUID( | 1598 ->GetPrimaryServicesByUUID(BluetoothUUID( |
1605 BluetoothTestBase::kTestUUIDGenericAttribute)) | 1599 BluetoothTestBase::kTestUUIDGenericAttribute)) |
1606 .empty()); | 1600 .empty()); |
1607 | 1601 |
1608 EXPECT_NE(services[0]->GetIdentifier(), services[1]->GetIdentifier()); | 1602 EXPECT_NE(services[0]->GetIdentifier(), services[1]->GetIdentifier()); |
1609 } | 1603 } |
1610 } | 1604 } |
1611 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) | 1605 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) |
1612 | 1606 |
1613 } // namespace device | 1607 } // namespace device |
OLD | NEW |