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 30 matching lines...) Expand all Loading... | |
41 | 41 |
42 } // namespace | 42 } // namespace |
43 | 43 |
44 #endif | 44 #endif |
45 | 45 |
46 using UUIDSet = BluetoothDevice::UUIDSet; | 46 using UUIDSet = BluetoothDevice::UUIDSet; |
47 using ServiceDataMap = BluetoothDevice::ServiceDataMap; | 47 using ServiceDataMap = BluetoothDevice::ServiceDataMap; |
48 | 48 |
49 class BluetoothGetServiceOrCharacteristicTest : public BluetoothTest { | 49 class BluetoothGetServiceOrCharacteristicTest : public BluetoothTest { |
50 public: | 50 public: |
51 // Creates |device_|, |services_|. | 51 // Creates |device_|, |service_uuids_|. |
52 void FakeServiceBoilerplate() { | 52 void FakeServiceBoilerplate() { |
53 InitWithFakeAdapter(); | 53 InitWithFakeAdapter(); |
54 StartLowEnergyDiscoverySession(); | 54 StartLowEnergyDiscoverySession(); |
55 device_ = SimulateLowEnergyDevice(3); | 55 device_ = SimulateLowEnergyDevice(3); |
56 EXPECT_FALSE(device_->IsConnected()); | 56 EXPECT_FALSE(device_->IsConnected()); |
57 | 57 |
58 // Connect to the device. | 58 // Connect to the device. |
59 ResetEventCounts(); | 59 ResetEventCounts(); |
60 device_->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 60 device_->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
61 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 61 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
62 TestBluetoothAdapterObserver observer(adapter_); | 62 TestBluetoothAdapterObserver observer(adapter_); |
63 SimulateGattConnection(device_); | 63 SimulateGattConnection(device_); |
64 base::RunLoop().RunUntilIdle(); | 64 base::RunLoop().RunUntilIdle(); |
65 EXPECT_TRUE(device_->IsConnected()); | 65 EXPECT_TRUE(device_->IsConnected()); |
66 | 66 |
67 // Discover services. | 67 // Discover services. |
68 services_.push_back("00000000-0000-1000-8000-00805f9b34fb"); | 68 service_uuids_.push_back(kTestUUIDGenericAccess); |
69 // 2 duplicate UUIDs creating 2 instances. | 69 // 2 duplicate UUIDs creating 2 instances. |
70 services_.push_back("00000001-0000-1000-8000-00805f9b34fb"); | 70 service_uuids_.push_back(kTestUUIDHeartRate); |
71 services_.push_back("00000001-0000-1000-8000-00805f9b34fb"); | 71 service_uuids_.push_back(kTestUUIDHeartRate); |
72 SimulateGattServicesDiscovered(device_, services_); | 72 SimulateGattServicesDiscovered(device_, service_uuids_); |
73 base::RunLoop().RunUntilIdle(); | 73 base::RunLoop().RunUntilIdle(); |
74 EXPECT_TRUE(device_->IsGattServicesDiscoveryComplete()); | 74 EXPECT_TRUE(device_->IsGattServicesDiscoveryComplete()); |
75 } | 75 } |
76 | 76 |
77 protected: | 77 protected: |
78 BluetoothDevice* device_ = nullptr; | 78 BluetoothDevice* device_ = nullptr; |
79 std::vector<std::string> services_; | 79 std::vector<std::string> service_uuids_; |
80 }; | 80 }; |
81 | 81 |
82 TEST(BluetoothDeviceTest, CanonicalizeAddressFormat_AcceptsAllValidFormats) { | 82 TEST(BluetoothDeviceTest, CanonicalizeAddressFormat_AcceptsAllValidFormats) { |
83 // There are three valid separators (':', '-', and none). | 83 // There are three valid separators (':', '-', and none). |
84 // Case shouldn't matter. | 84 // Case shouldn't matter. |
85 const char* const kValidFormats[] = { | 85 const char* const kValidFormats[] = { |
86 "1A:2B:3C:4D:5E:6F", | 86 "1A:2B:3C:4D:5E:6F", |
87 "1a:2B:3c:4D:5e:6F", | 87 "1a:2B:3c:4D:5e:6F", |
88 "1a:2b:3c:4d:5e:6f", | 88 "1a:2b:3c:4d:5e:6f", |
89 "1A-2B-3C-4D-5E-6F", | 89 "1A-2B-3C-4D-5E-6F", |
(...skipping 1420 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1510 | 1510 |
1511 BluetoothDevice* device2 = SimulateLowEnergyDevice(6); | 1511 BluetoothDevice* device2 = SimulateLowEnergyDevice(6); |
1512 EXPECT_EQ(BLUETOOTH_TRANSPORT_DUAL, device2->GetType()); | 1512 EXPECT_EQ(BLUETOOTH_TRANSPORT_DUAL, device2->GetType()); |
1513 | 1513 |
1514 BluetoothDevice* device3 = SimulateClassicDevice(); | 1514 BluetoothDevice* device3 = SimulateClassicDevice(); |
1515 EXPECT_EQ(BLUETOOTH_TRANSPORT_CLASSIC, device3->GetType()); | 1515 EXPECT_EQ(BLUETOOTH_TRANSPORT_CLASSIC, device3->GetType()); |
1516 } | 1516 } |
1517 #endif // defined(OS_CHROMEOS) || defined(OS_LINUX) | 1517 #endif // defined(OS_CHROMEOS) || defined(OS_LINUX) |
1518 | 1518 |
1519 #if defined(OS_ANDROID) || defined(OS_MACOSX) | 1519 #if defined(OS_ANDROID) || defined(OS_MACOSX) |
1520 // TODO(https://crbug.com/507419): Run test on Windows once Bluetooth | |
1521 // connection is implemented. | |
1520 TEST_F(BluetoothGetServiceOrCharacteristicTest, GetPrimaryServices) { | 1522 TEST_F(BluetoothGetServiceOrCharacteristicTest, GetPrimaryServices) { |
1521 if (!PlatformSupportsLowEnergy()) { | 1523 if (!PlatformSupportsLowEnergy()) { |
1522 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | 1524 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
1523 return; | 1525 return; |
1524 } | 1526 } |
1525 ASSERT_NO_FATAL_FAILURE(FakeServiceBoilerplate()); | 1527 ASSERT_NO_FATAL_FAILURE(FakeServiceBoilerplate()); |
1526 | 1528 |
1527 EXPECT_EQ(3u, device_->GetPrimaryServices().size()); | 1529 EXPECT_EQ(3u, device_->GetPrimaryServices().size()); |
1528 } | 1530 } |
1529 | 1531 |
1530 TEST_F(BluetoothGetServiceOrCharacteristicTest, GetPrimaryServicesByUUID) { | 1532 TEST_F(BluetoothGetServiceOrCharacteristicTest, GetPrimaryServicesByUUID) { |
1531 if (!PlatformSupportsLowEnergy()) { | 1533 if (!PlatformSupportsLowEnergy()) { |
1532 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | 1534 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
1533 return; | 1535 return; |
1534 } | 1536 } |
1535 ASSERT_NO_FATAL_FAILURE(FakeServiceBoilerplate()); | 1537 ASSERT_NO_FATAL_FAILURE(FakeServiceBoilerplate()); |
1536 | 1538 |
1537 EXPECT_EQ( | 1539 { |
1538 1u, | 1540 std::vector<BluetoothRemoteGattService*> services = |
1539 device_->GetPrimaryServicesByUUID(BluetoothUUID(services_[0])).size()); | 1541 device_->GetPrimaryServicesByUUID(BluetoothUUID(service_uuids_[0])); |
1540 EXPECT_EQ( | 1542 EXPECT_EQ(1u, services.size()); |
1541 2u, | 1543 EXPECT_EQ(BluetoothUUID(service_uuids_[0]), services[0]->GetUUID()); |
1542 device_->GetPrimaryServicesByUUID(BluetoothUUID(services_[1])).size()); | 1544 } |
1543 std::string service_uuid_not_exist_in_setup = | 1545 |
1544 "00000002-0000-1000-8000-00805f9b34fb"; | 1546 { |
1545 EXPECT_TRUE(device_ | 1547 std::vector<BluetoothRemoteGattService*> services = |
1546 ->GetPrimaryServicesByUUID( | 1548 device_->GetPrimaryServicesByUUID(BluetoothUUID(service_uuids_[1])); |
1547 BluetoothUUID(service_uuid_not_exist_in_setup)) | 1549 EXPECT_EQ(2u, services.size()); |
1548 .empty()); | 1550 EXPECT_EQ(BluetoothUUID(service_uuids_[1]), services[0]->GetUUID()); |
1551 EXPECT_EQ(BluetoothUUID(service_uuids_[1]), services[1]->GetUUID()); | |
1552 | |
1553 EXPECT_TRUE(device_ | |
1554 ->GetPrimaryServicesByUUID(BluetoothUUID( | |
1555 BluetoothTestBase::kTestUUIDGenericAttribute)) | |
1556 .empty()); | |
1557 | |
1558 EXPECT_NE(services[0]->GetIdentifier(), services[1]->GetIdentifier()); | |
1559 } | |
1549 } | 1560 } |
1550 | 1561 |
1551 TEST_F(BluetoothGetServiceOrCharacteristicTest, GetCharacteristicsByUUID) { | 1562 TEST_F(BluetoothGetServiceOrCharacteristicTest, GetCharacteristicsByUUID) { |
1552 if (!PlatformSupportsLowEnergy()) { | 1563 if (!PlatformSupportsLowEnergy()) { |
1553 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | 1564 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
1554 return; | 1565 return; |
1555 } | 1566 } |
1556 ASSERT_NO_FATAL_FAILURE(FakeServiceBoilerplate()); | 1567 ASSERT_NO_FATAL_FAILURE(FakeServiceBoilerplate()); |
1557 | 1568 |
1558 std::vector<BluetoothRemoteGattService*> primary_services = | 1569 std::vector<BluetoothRemoteGattService*> primary_services = |
1559 device_->GetPrimaryServices(); | 1570 device_->GetPrimaryServices(); |
1560 std::string service_instance_id0 = primary_services[0]->GetIdentifier(); | 1571 std::string service_instance_id0 = primary_services[0]->GetIdentifier(); |
1561 std::string service_instance_id1 = primary_services[1]->GetIdentifier(); | 1572 std::string service_instance_id1 = primary_services[1]->GetIdentifier(); |
1562 std::string service_instance_id2 = primary_services[2]->GetIdentifier(); | 1573 std::string service_instance_id2 = primary_services[2]->GetIdentifier(); |
1563 | 1574 |
1564 std::string characteristic_uuid0 = "00000002-0000-1000-8000-00805f9b34fb"; | 1575 std::string characteristic_uuid0 = "00002a00-0000-1000-8000-00805f9b34fb"; |
ortuno
2017/04/04 03:32:12
Move these to bluetooth_test.h
juncai
2017/04/04 20:33:40
Not used any more.
| |
1565 std::string characteristic_uuid1 = "00000003-0000-1000-8000-00805f9b34fb"; | 1576 std::string characteristic_uuid1 = "00002a37-0000-1000-8000-00805f9b34fb"; |
1566 SimulateGattCharacteristic(primary_services[0], characteristic_uuid0, | 1577 SimulateGattCharacteristic(primary_services[0], characteristic_uuid0, |
1567 0 /* properties */); | 1578 0 /* properties */); |
1579 // 2 duplicate UUIDs creating 2 instances. | |
1580 SimulateGattCharacteristic(primary_services[1], characteristic_uuid1, | |
1581 0 /* properties */); | |
1568 SimulateGattCharacteristic(primary_services[1], characteristic_uuid1, | 1582 SimulateGattCharacteristic(primary_services[1], characteristic_uuid1, |
1569 0 /* properties */); | 1583 0 /* properties */); |
1570 SimulateGattCharacteristic(primary_services[2], characteristic_uuid1, | 1584 SimulateGattCharacteristic(primary_services[2], characteristic_uuid1, |
1571 0 /* properties */); | 1585 0 /* properties */); |
1572 | 1586 |
1573 EXPECT_EQ(1u, | 1587 { |
1574 device_ | 1588 std::vector<BluetoothRemoteGattCharacteristic*> characteristics = |
1575 ->GetCharacteristicsByUUID(service_instance_id0, | 1589 device_->GetCharacteristicsByUUID(service_instance_id0, |
1576 BluetoothUUID(characteristic_uuid0)) | 1590 BluetoothUUID(characteristic_uuid0)); |
1577 .size()); | 1591 EXPECT_EQ(1u, characteristics.size()); |
1578 EXPECT_EQ(1u, | 1592 EXPECT_EQ(BluetoothUUID(characteristic_uuid0), |
1579 device_ | 1593 characteristics[0]->GetUUID()); |
1580 ->GetCharacteristicsByUUID(service_instance_id1, | 1594 } |
1581 BluetoothUUID(characteristic_uuid1)) | 1595 |
1582 .size()); | 1596 { |
1583 EXPECT_EQ(1u, | 1597 std::vector<BluetoothRemoteGattCharacteristic*> characteristics = |
1584 device_ | 1598 device_->GetCharacteristicsByUUID(service_instance_id1, |
1585 ->GetCharacteristicsByUUID(service_instance_id2, | 1599 BluetoothUUID(characteristic_uuid1)); |
1586 BluetoothUUID(characteristic_uuid1)) | 1600 EXPECT_EQ(2u, characteristics.size()); |
1587 .size()); | 1601 EXPECT_EQ(BluetoothUUID(characteristic_uuid1), |
1602 characteristics[0]->GetUUID()); | |
1603 EXPECT_EQ(BluetoothUUID(characteristic_uuid1), | |
1604 characteristics[1]->GetUUID()); | |
1605 } | |
ortuno
2017/04/04 03:32:12
Compare the identifiers to make sure they are two
juncai
2017/04/04 20:33:40
Done.
| |
1606 | |
1607 { | |
1608 std::vector<BluetoothRemoteGattCharacteristic*> characteristics = | |
1609 device_->GetCharacteristicsByUUID(service_instance_id2, | |
1610 BluetoothUUID(characteristic_uuid1)); | |
1611 EXPECT_EQ(1u, characteristics.size()); | |
1612 EXPECT_EQ(BluetoothUUID(characteristic_uuid1), | |
1613 characteristics[0]->GetUUID()); | |
1614 } | |
1588 | 1615 |
1589 std::string service_instance_id_not_exist_in_setup = | 1616 std::string service_instance_id_not_exist_in_setup = |
1590 "non-existent platform specific service instance id"; | 1617 "non-existent platform specific service instance id"; |
1591 EXPECT_TRUE( | 1618 EXPECT_TRUE( |
1592 device_ | 1619 device_ |
1593 ->GetCharacteristicsByUUID(service_instance_id_not_exist_in_setup, | 1620 ->GetCharacteristicsByUUID(service_instance_id_not_exist_in_setup, |
1594 BluetoothUUID(characteristic_uuid0)) | 1621 BluetoothUUID(characteristic_uuid0)) |
1595 .empty()); | 1622 .empty()); |
1596 EXPECT_TRUE( | 1623 EXPECT_TRUE( |
1597 device_ | 1624 device_ |
1598 ->GetCharacteristicsByUUID(service_instance_id_not_exist_in_setup, | 1625 ->GetCharacteristicsByUUID(service_instance_id_not_exist_in_setup, |
1599 BluetoothUUID(characteristic_uuid1)) | 1626 BluetoothUUID(characteristic_uuid1)) |
1600 .empty()); | 1627 .empty()); |
1601 | 1628 |
1602 std::string characteristic_uuid_not_exist_in_setup = | 1629 std::string characteristic_uuid_not_exist_in_setup = |
ortuno
2017/04/04 03:32:12
Also move this to bluetooth_test.h
juncai
2017/04/04 20:33:40
Not used any more.
| |
1603 "00000005-0000-1000-8000-00805f9b34fb"; | 1630 "00000005-0000-1000-8000-00805f9b34fb"; |
1604 EXPECT_TRUE(device_ | 1631 EXPECT_TRUE(device_ |
1605 ->GetCharacteristicsByUUID( | 1632 ->GetCharacteristicsByUUID( |
1606 service_instance_id0, | 1633 service_instance_id0, |
1607 BluetoothUUID(characteristic_uuid_not_exist_in_setup)) | 1634 BluetoothUUID(characteristic_uuid_not_exist_in_setup)) |
1608 .empty()); | 1635 .empty()); |
1609 EXPECT_TRUE(device_ | 1636 EXPECT_TRUE(device_ |
1610 ->GetCharacteristicsByUUID( | 1637 ->GetCharacteristicsByUUID( |
1611 service_instance_id1, | 1638 service_instance_id1, |
1612 BluetoothUUID(characteristic_uuid_not_exist_in_setup)) | 1639 BluetoothUUID(characteristic_uuid_not_exist_in_setup)) |
1613 .empty()); | 1640 .empty()); |
1614 EXPECT_TRUE(device_ | 1641 EXPECT_TRUE(device_ |
1615 ->GetCharacteristicsByUUID( | 1642 ->GetCharacteristicsByUUID( |
1616 service_instance_id2, | 1643 service_instance_id2, |
1617 BluetoothUUID(characteristic_uuid_not_exist_in_setup)) | 1644 BluetoothUUID(characteristic_uuid_not_exist_in_setup)) |
1618 .empty()); | 1645 .empty()); |
1619 } | 1646 } |
1620 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) | 1647 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) |
1621 | 1648 |
1622 } // namespace device | 1649 } // namespace device |
OLD | NEW |