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

Side by Side Diff: device/bluetooth/bluetooth_device_unittest.cc

Issue 2744243008: Add unit tests for BluetoothDevice::GetPrimaryServices() etc. (Closed)
Patch Set: Add unit tests for BluetoothDevice::GetPrimaryServices() etc. Created 3 years, 9 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
« no previous file with comments | « device/bluetooth/bluetooth_device.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 1465 matching lines...) Expand 10 before | Expand all | Expand 10 after
1476 EXPECT_EQ(BLUETOOTH_TRANSPORT_LE, device->GetType()); 1476 EXPECT_EQ(BLUETOOTH_TRANSPORT_LE, device->GetType());
1477 1477
1478 BluetoothDevice* device2 = SimulateLowEnergyDevice(6); 1478 BluetoothDevice* device2 = SimulateLowEnergyDevice(6);
1479 EXPECT_EQ(BLUETOOTH_TRANSPORT_DUAL, device2->GetType()); 1479 EXPECT_EQ(BLUETOOTH_TRANSPORT_DUAL, device2->GetType());
1480 1480
1481 BluetoothDevice* device3 = SimulateClassicDevice(); 1481 BluetoothDevice* device3 = SimulateClassicDevice();
1482 EXPECT_EQ(BLUETOOTH_TRANSPORT_CLASSIC, device3->GetType()); 1482 EXPECT_EQ(BLUETOOTH_TRANSPORT_CLASSIC, device3->GetType());
1483 } 1483 }
1484 #endif // defined(OS_CHROMEOS) || defined(OS_LINUX) 1484 #endif // defined(OS_CHROMEOS) || defined(OS_LINUX)
1485 1485
1486 #if defined(OS_ANDROID) || defined(OS_MACOSX)
1487 TEST_F(BluetoothTest, GetPrimaryServices) {
1488 if (!PlatformSupportsLowEnergy()) {
1489 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
1490 return;
1491 }
1492 InitWithFakeAdapter();
scheib 2017/03/17 17:38:40 There's a lot of boilerplate for these tests. Cons
juncai 2017/03/17 21:25:47 I think the above 5 lines are also repeated in oth
scheib 2017/03/17 22:10:47 Look at e.g. the test fixture setup done for the c
juncai 2017/03/17 23:33:54 Done.
1493 StartLowEnergyDiscoverySession();
1494 BluetoothDevice* device = SimulateLowEnergyDevice(3);
1495 EXPECT_FALSE(device->IsConnected());
1496
1497 // Connect to the device
1498 ResetEventCounts();
1499 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
1500 GetConnectErrorCallback(Call::NOT_EXPECTED));
1501 TestBluetoothAdapterObserver observer(adapter_);
1502 SimulateGattConnection(device);
1503 base::RunLoop().RunUntilIdle();
1504 EXPECT_TRUE(device->IsConnected());
1505
1506 // Discover services
1507 std::vector<std::string> services;
1508 services.push_back("00000000-0000-1000-8000-00805f9b34fb");
1509 services.push_back("00000001-0000-1000-8000-00805f9b34fb");
scheib 2017/03/17 17:38:40 multiple services, same ID. So, probably use 1 uni
juncai 2017/03/17 21:25:47 Done.
1510 SimulateGattServicesDiscovered(device, services);
1511 base::RunLoop().RunUntilIdle();
1512 EXPECT_TRUE(device->IsGattServicesDiscoveryComplete());
1513
1514 EXPECT_EQ(2u, device->GetPrimaryServices().size());
1515
1516 // Disconnect from the device
scheib 2017/03/17 17:38:40 Everything after GetPrimaryServices is not needed.
juncai 2017/03/17 21:25:47 Done.
1517 device->DisconnectGatt();
1518 SimulateGattDisconnection(device);
1519 base::RunLoop().RunUntilIdle();
1520 EXPECT_TRUE(device->GetPrimaryServices().empty());
1521 }
1522
1523 TEST_F(BluetoothTest, GetPrimaryServicesByUUID) {
1524 if (!PlatformSupportsLowEnergy()) {
1525 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
1526 return;
1527 }
1528 InitWithFakeAdapter();
1529 StartLowEnergyDiscoverySession();
1530 BluetoothDevice* device = SimulateLowEnergyDevice(3);
1531 EXPECT_FALSE(device->IsConnected());
1532
1533 // Connect to the device
1534 ResetEventCounts();
1535 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
1536 GetConnectErrorCallback(Call::NOT_EXPECTED));
1537 TestBluetoothAdapterObserver observer(adapter_);
1538 SimulateGattConnection(device);
1539 base::RunLoop().RunUntilIdle();
1540 EXPECT_TRUE(device->IsConnected());
1541
1542 // Discover services
1543 std::vector<std::string> services;
1544 services.push_back("00000000-0000-1000-8000-00805f9b34fb");
1545 services.push_back("00000001-0000-1000-8000-00805f9b34fb");
1546 SimulateGattServicesDiscovered(device, services);
1547 base::RunLoop().RunUntilIdle();
1548 EXPECT_TRUE(device->IsGattServicesDiscoveryComplete());
1549
1550 EXPECT_EQ(
1551 1u, device->GetPrimaryServicesByUUID(BluetoothUUID(services[0])).size());
1552 EXPECT_EQ(
1553 1u, device->GetPrimaryServicesByUUID(BluetoothUUID(services[1])).size());
1554 EXPECT_TRUE(
1555 device->GetPrimaryServicesByUUID(BluetoothUUID("non-existent")).empty());
scheib 2017/03/17 17:38:40 Let's use a valid UUID that doesn't exist in the f
juncai 2017/03/17 21:25:48 Done.
1556
1557 // Disconnect from the device
1558 device->DisconnectGatt();
1559 SimulateGattDisconnection(device);
1560 base::RunLoop().RunUntilIdle();
1561 EXPECT_TRUE(
1562 device->GetPrimaryServicesByUUID(BluetoothUUID(services[0])).empty());
1563 EXPECT_TRUE(
1564 device->GetPrimaryServicesByUUID(BluetoothUUID(services[1])).empty());
1565 }
1566
1567 TEST_F(BluetoothTest, GetCharacteristicsByUUID) {
1568 if (!PlatformSupportsLowEnergy()) {
1569 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
1570 return;
1571 }
1572 InitWithFakeAdapter();
1573 StartLowEnergyDiscoverySession();
1574 BluetoothDevice* device = SimulateLowEnergyDevice(3);
1575 EXPECT_FALSE(device->IsConnected());
1576
1577 // Connect to the device
1578 ResetEventCounts();
1579 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
1580 GetConnectErrorCallback(Call::NOT_EXPECTED));
1581 TestBluetoothAdapterObserver observer(adapter_);
1582 SimulateGattConnection(device);
1583 base::RunLoop().RunUntilIdle();
1584 EXPECT_TRUE(device->IsConnected());
1585
1586 // Discover services
1587 std::vector<std::string> services;
1588 services.push_back("00000000-0000-1000-8000-00805f9b34fb");
1589 services.push_back("00000001-0000-1000-8000-00805f9b34fb");
1590 SimulateGattServicesDiscovered(device, services);
1591 base::RunLoop().RunUntilIdle();
1592 EXPECT_TRUE(device->IsGattServicesDiscoveryComplete());
1593
1594 std::vector<BluetoothRemoteGattService*> primary_services =
1595 device->GetPrimaryServices();
1596
1597 // 2 characteristics, 1 on each service.
1598 SimulateGattCharacteristic(primary_services[0], services[0],
scheib 2017/03/17 17:38:41 Let's use a unique ID for the characteristic inste
juncai 2017/03/17 21:25:48 Done.
1599 0 /* properties */);
1600 SimulateGattCharacteristic(primary_services[1], services[1],
1601 0 /* properties */);
1602
1603 std::string service_instance_id_0 = primary_services[0]->GetIdentifier();
1604 BluetoothUUID characteristic_uuid_0(services[0]);
1605 EXPECT_EQ(1u, device
1606 ->GetCharacteristicsByUUID(service_instance_id_0,
1607 characteristic_uuid_0)
1608 .size());
1609
1610 std::string service_instance_id_1 = primary_services[1]->GetIdentifier();
1611 BluetoothUUID characteristic_uuid_1(services[1]);
1612 EXPECT_EQ(1u, device
1613 ->GetCharacteristicsByUUID(service_instance_id_1,
1614 characteristic_uuid_1)
1615 .size());
1616
1617 EXPECT_TRUE(
1618 device->GetCharacteristicsByUUID("non-existent", characteristic_uuid_0)
scheib 2017/03/17 17:38:40 Let's use a valid UUID that doesn't exist in the f
juncai 2017/03/17 21:25:47 Done.
1619 .empty());
1620 EXPECT_TRUE(device
1621 ->GetCharacteristicsByUUID(service_instance_id_0,
1622 BluetoothUUID("non-existent"))
1623 .empty());
1624
1625 // Disconnect from the device
1626 device->DisconnectGatt();
1627 SimulateGattDisconnection(device);
1628 base::RunLoop().RunUntilIdle();
1629 EXPECT_TRUE(device
1630 ->GetCharacteristicsByUUID(service_instance_id_0,
1631 characteristic_uuid_0)
1632 .empty());
1633 EXPECT_TRUE(device
1634 ->GetCharacteristicsByUUID(service_instance_id_1,
1635 characteristic_uuid_1)
1636 .empty());
1637 }
1638 #endif // defined(OS_ANDROID) || defined(OS_MACOSX)
1639
1486 } // namespace device 1640 } // namespace device
OLDNEW
« no previous file with comments | « device/bluetooth/bluetooth_device.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698