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 28 matching lines...) Expand all Loading... | |
39 return static_cast<int8_t>(tx_power); | 39 return static_cast<int8_t>(tx_power); |
40 } | 40 } |
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 { | |
50 public: | |
51 // Creates |device_|, |services_|. | |
52 void FakeServiceBoilerplate() { | |
53 InitWithFakeAdapter(); | |
54 StartLowEnergyDiscoverySession(); | |
55 device_ = SimulateLowEnergyDevice(3); | |
56 EXPECT_FALSE(device_->IsConnected()); | |
57 | |
58 // Connect to the device. | |
59 ResetEventCounts(); | |
60 device_->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | |
61 GetConnectErrorCallback(Call::NOT_EXPECTED)); | |
62 TestBluetoothAdapterObserver observer(adapter_); | |
63 SimulateGattConnection(device_); | |
64 base::RunLoop().RunUntilIdle(); | |
65 EXPECT_TRUE(device_->IsConnected()); | |
66 | |
67 // Discover services. | |
68 services_.push_back("00000000-0000-1000-8000-00805f9b34fb"); | |
69 // 2 duplicate UUIDs creating 2 instances. | |
70 services_.push_back("00000001-0000-1000-8000-00805f9b34fb"); | |
71 services_.push_back("00000001-0000-1000-8000-00805f9b34fb"); | |
72 SimulateGattServicesDiscovered(device_, services_); | |
73 base::RunLoop().RunUntilIdle(); | |
74 EXPECT_TRUE(device_->IsGattServicesDiscoveryComplete()); | |
75 } | |
76 | |
77 protected: | |
78 BluetoothDevice* device_ = nullptr; | |
79 std::vector<std::string> services_; | |
80 }; | |
81 | |
49 TEST(BluetoothDeviceTest, CanonicalizeAddressFormat_AcceptsAllValidFormats) { | 82 TEST(BluetoothDeviceTest, CanonicalizeAddressFormat_AcceptsAllValidFormats) { |
50 // There are three valid separators (':', '-', and none). | 83 // There are three valid separators (':', '-', and none). |
51 // Case shouldn't matter. | 84 // Case shouldn't matter. |
52 const char* const kValidFormats[] = { | 85 const char* const kValidFormats[] = { |
53 "1A:2B:3C:4D:5E:6F", | 86 "1A:2B:3C:4D:5E:6F", |
54 "1a:2B:3c:4D:5e:6F", | 87 "1a:2B:3c:4D:5e:6F", |
55 "1a:2b:3c:4d:5e:6f", | 88 "1a:2b:3c:4d:5e:6f", |
56 "1A-2B-3C-4D-5E-6F", | 89 "1A-2B-3C-4D-5E-6F", |
57 "1a-2B-3c-4D-5e-6F", | 90 "1a-2B-3c-4D-5e-6F", |
58 "1a-2b-3c-4d-5e-6f", | 91 "1a-2b-3c-4d-5e-6f", |
(...skipping 1417 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1476 EXPECT_EQ(BLUETOOTH_TRANSPORT_LE, device->GetType()); | 1509 EXPECT_EQ(BLUETOOTH_TRANSPORT_LE, device->GetType()); |
1477 | 1510 |
1478 BluetoothDevice* device2 = SimulateLowEnergyDevice(6); | 1511 BluetoothDevice* device2 = SimulateLowEnergyDevice(6); |
1479 EXPECT_EQ(BLUETOOTH_TRANSPORT_DUAL, device2->GetType()); | 1512 EXPECT_EQ(BLUETOOTH_TRANSPORT_DUAL, device2->GetType()); |
1480 | 1513 |
1481 BluetoothDevice* device3 = SimulateClassicDevice(); | 1514 BluetoothDevice* device3 = SimulateClassicDevice(); |
1482 EXPECT_EQ(BLUETOOTH_TRANSPORT_CLASSIC, device3->GetType()); | 1515 EXPECT_EQ(BLUETOOTH_TRANSPORT_CLASSIC, device3->GetType()); |
1483 } | 1516 } |
1484 #endif // defined(OS_CHROMEOS) || defined(OS_LINUX) | 1517 #endif // defined(OS_CHROMEOS) || defined(OS_LINUX) |
1485 | 1518 |
1519 #if defined(OS_ANDROID) || defined(OS_MACOSX) | |
ortuno
2017/03/28 03:07:49
We usually add a note about why other platforms ar
juncai
2017/03/29 17:49:52
reopen the issue:
https://bugs.chromium.org/p/chro
| |
1520 TEST_F(BluetoothGetServiceOrCharacteristicTest, GetPrimaryServices) { | |
1521 if (!PlatformSupportsLowEnergy()) { | |
1522 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | |
1523 return; | |
1524 } | |
1525 ASSERT_NO_FATAL_FAILURE(FakeServiceBoilerplate()); | |
1526 | |
1527 EXPECT_EQ(3u, device_->GetPrimaryServices().size()); | |
1528 } | |
1529 | |
1530 TEST_F(BluetoothGetServiceOrCharacteristicTest, GetPrimaryServicesByUUID) { | |
ortuno
2017/03/28 03:07:49
This test could be improved by either checking the
juncai
2017/03/29 17:49:53
ditto.
| |
1531 if (!PlatformSupportsLowEnergy()) { | |
1532 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | |
1533 return; | |
1534 } | |
1535 ASSERT_NO_FATAL_FAILURE(FakeServiceBoilerplate()); | |
1536 | |
1537 EXPECT_EQ( | |
1538 1u, | |
1539 device_->GetPrimaryServicesByUUID(BluetoothUUID(services_[0])).size()); | |
1540 EXPECT_EQ( | |
1541 2u, | |
1542 device_->GetPrimaryServicesByUUID(BluetoothUUID(services_[1])).size()); | |
1543 std::string service_uuid_not_exist_in_setup = | |
ortuno
2017/03/28 03:07:49
nit: use existing test UUIDs: https://cs.chromium.
juncai
2017/03/29 17:49:53
ditto.
| |
1544 "00000002-0000-1000-8000-00805f9b34fb"; | |
1545 EXPECT_TRUE(device_ | |
1546 ->GetPrimaryServicesByUUID( | |
1547 BluetoothUUID(service_uuid_not_exist_in_setup)) | |
1548 .empty()); | |
1549 } | |
1550 | |
1551 TEST_F(BluetoothGetServiceOrCharacteristicTest, GetCharacteristicsByUUID) { | |
ortuno
2017/03/28 03:07:49
Similarly here we should make sure the right chara
juncai
2017/03/29 17:49:53
ditto.
| |
1552 if (!PlatformSupportsLowEnergy()) { | |
1553 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | |
1554 return; | |
1555 } | |
1556 ASSERT_NO_FATAL_FAILURE(FakeServiceBoilerplate()); | |
1557 | |
1558 std::vector<BluetoothRemoteGattService*> primary_services = | |
1559 device_->GetPrimaryServices(); | |
1560 std::string service_instance_id0 = primary_services[0]->GetIdentifier(); | |
1561 std::string service_instance_id1 = primary_services[1]->GetIdentifier(); | |
1562 std::string service_instance_id2 = primary_services[2]->GetIdentifier(); | |
1563 | |
1564 std::string characteristic_uuid0 = "00000002-0000-1000-8000-00805f9b34fb"; | |
1565 std::string characteristic_uuid1 = "00000003-0000-1000-8000-00805f9b34fb"; | |
1566 SimulateGattCharacteristic(primary_services[0], characteristic_uuid0, | |
1567 0 /* properties */); | |
1568 SimulateGattCharacteristic(primary_services[1], characteristic_uuid1, | |
1569 0 /* properties */); | |
1570 SimulateGattCharacteristic(primary_services[2], characteristic_uuid1, | |
1571 0 /* properties */); | |
1572 | |
1573 EXPECT_EQ(1u, | |
1574 device_ | |
1575 ->GetCharacteristicsByUUID(service_instance_id0, | |
ortuno
2017/03/28 03:07:49
Unrelated questions about these methods:
1. Why i
juncai
2017/03/29 17:49:53
For 1: it is mentioned in the TODO at:
https://cod
ortuno
2017/03/29 22:45:42
I see. Let's move them to the appropriate attribut
| |
1576 BluetoothUUID(characteristic_uuid0)) | |
1577 .size()); | |
1578 EXPECT_EQ(1u, | |
1579 device_ | |
1580 ->GetCharacteristicsByUUID(service_instance_id1, | |
1581 BluetoothUUID(characteristic_uuid1)) | |
1582 .size()); | |
1583 EXPECT_EQ(1u, | |
1584 device_ | |
1585 ->GetCharacteristicsByUUID(service_instance_id2, | |
1586 BluetoothUUID(characteristic_uuid1)) | |
1587 .size()); | |
1588 | |
1589 std::string service_instance_id_not_exist_in_setup = | |
1590 "non-existent platform specific service instance id"; | |
1591 EXPECT_TRUE( | |
1592 device_ | |
1593 ->GetCharacteristicsByUUID(service_instance_id_not_exist_in_setup, | |
1594 BluetoothUUID(characteristic_uuid0)) | |
1595 .empty()); | |
1596 EXPECT_TRUE( | |
1597 device_ | |
1598 ->GetCharacteristicsByUUID(service_instance_id_not_exist_in_setup, | |
1599 BluetoothUUID(characteristic_uuid1)) | |
1600 .empty()); | |
1601 | |
1602 std::string characteristic_uuid_not_exist_in_setup = | |
1603 "00000005-0000-1000-8000-00805f9b34fb"; | |
1604 EXPECT_TRUE(device_ | |
1605 ->GetCharacteristicsByUUID( | |
1606 service_instance_id0, | |
1607 BluetoothUUID(characteristic_uuid_not_exist_in_setup)) | |
1608 .empty()); | |
1609 EXPECT_TRUE(device_ | |
1610 ->GetCharacteristicsByUUID( | |
1611 service_instance_id1, | |
1612 BluetoothUUID(characteristic_uuid_not_exist_in_setup)) | |
1613 .empty()); | |
1614 EXPECT_TRUE(device_ | |
1615 ->GetCharacteristicsByUUID( | |
1616 service_instance_id2, | |
1617 BluetoothUUID(characteristic_uuid_not_exist_in_setup)) | |
1618 .empty()); | |
1619 } | |
1620 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) | |
ortuno
2017/03/28 03:07:49
Do we already have tests for GetDescriptorsByUUID?
juncai
2017/03/29 17:49:53
I don't think we have tests for GetDescriptorsByUU
| |
1621 | |
1486 } // namespace device | 1622 } // namespace device |
OLD | NEW |