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 1259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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 std::vector<std::string> services; |
1280 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); | 1280 services.push_back(kTestUUIDGenericAccess); |
ortuno
2017/04/06 00:56:19
optional: We used push_back because initializer li
juncai
2017/04/07 18:50:19
Done.
| |
1281 services.push_back("00000001-0000-1000-8000-00805f9b34fb"); | 1281 services.push_back(kTestUUIDHeartRate); |
1282 SimulateGattServicesDiscovered(device, services); | 1282 SimulateGattServicesDiscovered(device, services); |
1283 base::RunLoop().RunUntilIdle(); | 1283 base::RunLoop().RunUntilIdle(); |
1284 EXPECT_TRUE(device->IsGattServicesDiscoveryComplete()); | 1284 EXPECT_TRUE(device->IsGattServicesDiscoveryComplete()); |
1285 EXPECT_EQ(2u, device->GetGattServices().size()); | 1285 EXPECT_EQ(2u, device->GetGattServices().size()); |
1286 EXPECT_EQ(1, observer.gatt_services_discovered_count()); | 1286 EXPECT_EQ(1, observer.gatt_services_discovered_count()); |
1287 | 1287 |
1288 // Disconnect from the device | 1288 // Disconnect from the device |
1289 device->DisconnectGatt(); | 1289 device->DisconnectGatt(); |
1290 SimulateGattDisconnection(device); | 1290 SimulateGattDisconnection(device); |
1291 base::RunLoop().RunUntilIdle(); | 1291 base::RunLoop().RunUntilIdle(); |
1292 EXPECT_FALSE(device->IsConnected()); | 1292 EXPECT_FALSE(device->IsConnected()); |
1293 EXPECT_FALSE(device->IsGattServicesDiscoveryComplete()); | 1293 EXPECT_FALSE(device->IsGattServicesDiscoveryComplete()); |
1294 EXPECT_EQ(0u, device->GetGattServices().size()); | 1294 EXPECT_EQ(0u, device->GetGattServices().size()); |
1295 | 1295 |
1296 // Verify that the device can be connected to again | 1296 // Verify that the device can be connected to again |
1297 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 1297 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
1298 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 1298 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
1299 SimulateGattConnection(device); | 1299 SimulateGattConnection(device); |
1300 base::RunLoop().RunUntilIdle(); | 1300 base::RunLoop().RunUntilIdle(); |
1301 EXPECT_TRUE(device->IsConnected()); | 1301 EXPECT_TRUE(device->IsConnected()); |
1302 | 1302 |
1303 // Verify that service discovery can be done again | 1303 // Verify that service discovery can be done again |
1304 std::vector<std::string> services2; | 1304 std::vector<std::string> services2; |
1305 services2.push_back("00000002-0000-1000-8000-00805f9b34fb"); | 1305 services2.push_back(kTestUUIDGenericAttribute); |
1306 SimulateGattServicesDiscovered(device, services2); | 1306 SimulateGattServicesDiscovered(device, services2); |
1307 base::RunLoop().RunUntilIdle(); | 1307 base::RunLoop().RunUntilIdle(); |
1308 EXPECT_TRUE(device->IsGattServicesDiscoveryComplete()); | 1308 EXPECT_TRUE(device->IsGattServicesDiscoveryComplete()); |
1309 EXPECT_EQ(1u, device->GetGattServices().size()); | 1309 EXPECT_EQ(1u, device->GetGattServices().size()); |
1310 EXPECT_EQ(2, observer.gatt_services_discovered_count()); | 1310 EXPECT_EQ(2, observer.gatt_services_discovered_count()); |
1311 } | 1311 } |
1312 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) | 1312 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) |
1313 | 1313 |
1314 #if defined(OS_ANDROID) || defined(OS_MACOSX) | 1314 #if defined(OS_ANDROID) || defined(OS_MACOSX) |
1315 // Calls CreateGattConnection, but simulate errors connecting. Also, verifies | 1315 // Calls CreateGattConnection, but simulate errors connecting. Also, verifies |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1355 BluetoothDevice* device = SimulateLowEnergyDevice(3); | 1355 BluetoothDevice* device = SimulateLowEnergyDevice(3); |
1356 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 1356 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
1357 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 1357 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
1358 TestBluetoothAdapterObserver observer(adapter_); | 1358 TestBluetoothAdapterObserver observer(adapter_); |
1359 ResetEventCounts(); | 1359 ResetEventCounts(); |
1360 SimulateGattConnection(device); | 1360 SimulateGattConnection(device); |
1361 base::RunLoop().RunUntilIdle(); | 1361 base::RunLoop().RunUntilIdle(); |
1362 EXPECT_EQ(1, gatt_discovery_attempts_); | 1362 EXPECT_EQ(1, gatt_discovery_attempts_); |
1363 | 1363 |
1364 std::vector<std::string> services; | 1364 std::vector<std::string> services; |
1365 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); | 1365 services.push_back(kTestUUIDGenericAccess); |
1366 services.push_back("00000001-0000-1000-8000-00805f9b34fb"); | 1366 services.push_back(kTestUUIDHeartRate); |
1367 SimulateGattServicesDiscovered(device, services); | 1367 SimulateGattServicesDiscovered(device, services); |
1368 base::RunLoop().RunUntilIdle(); | 1368 base::RunLoop().RunUntilIdle(); |
1369 | 1369 |
1370 EXPECT_EQ(1, observer.gatt_services_discovered_count()); | 1370 EXPECT_EQ(1, observer.gatt_services_discovered_count()); |
1371 EXPECT_EQ(2, observer.gatt_service_added_count()); | 1371 EXPECT_EQ(2, observer.gatt_service_added_count()); |
1372 } | 1372 } |
1373 #endif // defined(OS_ANDROID) || defined(OS_WIN) || defined(OS_MACOSX) | 1373 #endif // defined(OS_ANDROID) || defined(OS_WIN) || defined(OS_MACOSX) |
1374 | 1374 |
1375 #if defined(OS_ANDROID) | 1375 #if defined(OS_ANDROID) |
1376 // macOS: Not applicable: This can never happen because when | 1376 // macOS: Not applicable: This can never happen because when |
(...skipping 13 matching lines...) Expand all Loading... | |
1390 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 1390 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
1391 ResetEventCounts(); | 1391 ResetEventCounts(); |
1392 SimulateGattConnection(device); | 1392 SimulateGattConnection(device); |
1393 base::RunLoop().RunUntilIdle(); | 1393 base::RunLoop().RunUntilIdle(); |
1394 EXPECT_EQ(1, gatt_discovery_attempts_); | 1394 EXPECT_EQ(1, gatt_discovery_attempts_); |
1395 | 1395 |
1396 RememberDeviceForSubsequentAction(device); | 1396 RememberDeviceForSubsequentAction(device); |
1397 DeleteDevice(device); | 1397 DeleteDevice(device); |
1398 | 1398 |
1399 std::vector<std::string> services; | 1399 std::vector<std::string> services; |
1400 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); | 1400 services.push_back(kTestUUIDGenericAccess); |
1401 services.push_back("00000001-0000-1000-8000-00805f9b34fb"); | 1401 services.push_back(kTestUUIDHeartRate); |
1402 SimulateGattServicesDiscovered(nullptr /* use remembered device */, services); | 1402 SimulateGattServicesDiscovered(nullptr /* use remembered device */, services); |
1403 base::RunLoop().RunUntilIdle(); | 1403 base::RunLoop().RunUntilIdle(); |
1404 } | 1404 } |
1405 #endif // defined(OS_ANDROID) | 1405 #endif // defined(OS_ANDROID) |
1406 | 1406 |
1407 #if defined(OS_ANDROID) | 1407 #if defined(OS_ANDROID) |
1408 // macOS: Not applicable: This can never happen because when | 1408 // macOS: Not applicable: This can never happen because when |
1409 // the device gets destroyed the CBPeripheralDelegate is also destroyed | 1409 // the device gets destroyed the CBPeripheralDelegate is also destroyed |
1410 // and no more events are dispatched. | 1410 // and no more events are dispatched. |
1411 TEST_F(BluetoothTest, GattServicesDiscoveredError_AfterDeleted) { | 1411 TEST_F(BluetoothTest, GattServicesDiscoveredError_AfterDeleted) { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1448 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 1448 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
1449 ResetEventCounts(); | 1449 ResetEventCounts(); |
1450 SimulateGattConnection(device); | 1450 SimulateGattConnection(device); |
1451 base::RunLoop().RunUntilIdle(); | 1451 base::RunLoop().RunUntilIdle(); |
1452 EXPECT_EQ(1, gatt_discovery_attempts_); | 1452 EXPECT_EQ(1, gatt_discovery_attempts_); |
1453 | 1453 |
1454 SimulateGattDisconnection(device); | 1454 SimulateGattDisconnection(device); |
1455 base::RunLoop().RunUntilIdle(); | 1455 base::RunLoop().RunUntilIdle(); |
1456 | 1456 |
1457 std::vector<std::string> services; | 1457 std::vector<std::string> services; |
1458 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); | 1458 services.push_back(kTestUUIDGenericAccess); |
1459 services.push_back("00000001-0000-1000-8000-00805f9b34fb"); | 1459 services.push_back(kTestUUIDHeartRate); |
1460 SimulateGattServicesDiscovered(device, services); | 1460 SimulateGattServicesDiscovered(device, services); |
1461 base::RunLoop().RunUntilIdle(); | 1461 base::RunLoop().RunUntilIdle(); |
1462 | 1462 |
1463 EXPECT_FALSE(device->IsGattServicesDiscoveryComplete()); | 1463 EXPECT_FALSE(device->IsGattServicesDiscoveryComplete()); |
1464 EXPECT_EQ(0u, device->GetGattServices().size()); | 1464 EXPECT_EQ(0u, device->GetGattServices().size()); |
1465 } | 1465 } |
1466 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) | 1466 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) |
1467 | 1467 |
1468 #if defined(OS_ANDROID) || defined(OS_MACOSX) | 1468 #if defined(OS_ANDROID) || defined(OS_MACOSX) |
1469 TEST_F(BluetoothTest, GattServicesDiscoveredError_AfterDisconnection) { | 1469 TEST_F(BluetoothTest, GattServicesDiscoveredError_AfterDisconnection) { |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1503 StartLowEnergyDiscoverySession(); | 1503 StartLowEnergyDiscoverySession(); |
1504 BluetoothDevice* device = SimulateLowEnergyDevice(3); | 1504 BluetoothDevice* device = SimulateLowEnergyDevice(3); |
1505 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 1505 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
1506 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 1506 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
1507 ResetEventCounts(); | 1507 ResetEventCounts(); |
1508 SimulateGattConnection(device); | 1508 SimulateGattConnection(device); |
1509 base::RunLoop().RunUntilIdle(); | 1509 base::RunLoop().RunUntilIdle(); |
1510 EXPECT_EQ(1, gatt_discovery_attempts_); | 1510 EXPECT_EQ(1, gatt_discovery_attempts_); |
1511 | 1511 |
1512 std::vector<std::string> services; | 1512 std::vector<std::string> services; |
1513 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); | 1513 services.push_back(kTestUUIDGenericAccess); |
1514 // 2 duplicate UUIDs creating 2 instances. | 1514 // 2 duplicate UUIDs creating 2 instances. |
1515 services.push_back("00000001-0000-1000-8000-00805f9b34fb"); | 1515 services.push_back(kTestUUIDHeartRate); |
1516 services.push_back("00000001-0000-1000-8000-00805f9b34fb"); | 1516 services.push_back(kTestUUIDHeartRate); |
1517 SimulateGattServicesDiscovered(device, services); | 1517 SimulateGattServicesDiscovered(device, services); |
1518 base::RunLoop().RunUntilIdle(); | 1518 base::RunLoop().RunUntilIdle(); |
1519 EXPECT_EQ(3u, device->GetGattServices().size()); | 1519 EXPECT_EQ(3u, device->GetGattServices().size()); |
1520 | 1520 |
1521 // Test GetGattService: | 1521 // Test GetGattService: |
1522 std::string service_id1 = device->GetGattServices()[0]->GetIdentifier(); | 1522 std::string service_id1 = device->GetGattServices()[0]->GetIdentifier(); |
1523 std::string service_id2 = device->GetGattServices()[1]->GetIdentifier(); | 1523 std::string service_id2 = device->GetGattServices()[1]->GetIdentifier(); |
1524 std::string service_id3 = device->GetGattServices()[2]->GetIdentifier(); | 1524 std::string service_id3 = device->GetGattServices()[2]->GetIdentifier(); |
1525 EXPECT_TRUE(device->GetGattService(service_id1)); | 1525 EXPECT_TRUE(device->GetGattService(service_id1)); |
1526 EXPECT_TRUE(device->GetGattService(service_id2)); | 1526 EXPECT_TRUE(device->GetGattService(service_id2)); |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1604 ->GetPrimaryServicesByUUID(BluetoothUUID( | 1604 ->GetPrimaryServicesByUUID(BluetoothUUID( |
1605 BluetoothTestBase::kTestUUIDGenericAttribute)) | 1605 BluetoothTestBase::kTestUUIDGenericAttribute)) |
1606 .empty()); | 1606 .empty()); |
1607 | 1607 |
1608 EXPECT_NE(services[0]->GetIdentifier(), services[1]->GetIdentifier()); | 1608 EXPECT_NE(services[0]->GetIdentifier(), services[1]->GetIdentifier()); |
1609 } | 1609 } |
1610 } | 1610 } |
1611 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) | 1611 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) |
1612 | 1612 |
1613 } // namespace device | 1613 } // namespace device |
OLD | NEW |