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

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

Issue 2785383002: Update unit tests for BluetoothDevice::GetPrimaryServicesByUUID() etc. (Closed)
Patch Set: added TODO Created 3 years, 8 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.cc ('k') | device/bluetooth/bluetooth_remote_gatt_service.h » ('j') | 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 28 matching lines...) Expand all
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 { 49 class BluetoothGetServiceTest : public BluetoothTest {
50 public: 50 public:
51 // Creates |device_|, |services_|. 51 BluetoothGetServiceTest()
52 : unique_service_uuid_(kTestUUIDGenericAccess),
53 duplicate_service_uuid_(kTestUUIDHeartRate) {}
54
55 // Creates |device_|.
52 void FakeServiceBoilerplate() { 56 void FakeServiceBoilerplate() {
53 InitWithFakeAdapter(); 57 InitWithFakeAdapter();
54 StartLowEnergyDiscoverySession(); 58 StartLowEnergyDiscoverySession();
55 device_ = SimulateLowEnergyDevice(3); 59 device_ = SimulateLowEnergyDevice(3);
56 EXPECT_FALSE(device_->IsConnected()); 60 EXPECT_FALSE(device_->IsConnected());
57 61
58 // Connect to the device. 62 // Connect to the device.
59 ResetEventCounts(); 63 ResetEventCounts();
60 device_->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), 64 device_->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED),
61 GetConnectErrorCallback(Call::NOT_EXPECTED)); 65 GetConnectErrorCallback(Call::NOT_EXPECTED));
62 TestBluetoothAdapterObserver observer(adapter_); 66 TestBluetoothAdapterObserver observer(adapter_);
63 SimulateGattConnection(device_); 67 SimulateGattConnection(device_);
64 base::RunLoop().RunUntilIdle(); 68 base::RunLoop().RunUntilIdle();
69 #if defined(OS_WIN)
70 // TODO(crbug.com/507419): Check connection once CreateGattConnection is
71 // implemented on Windows.
72 EXPECT_FALSE(device_->IsConnected());
73 #else
65 EXPECT_TRUE(device_->IsConnected()); 74 EXPECT_TRUE(device_->IsConnected());
75 #endif // defined(OS_WIN)
66 76
67 // Discover services. 77 // Discover services.
68 services_.push_back("00000000-0000-1000-8000-00805f9b34fb"); 78 std::vector<std::string> service_uuids;
79 service_uuids.push_back(unique_service_uuid_.canonical_value());
69 // 2 duplicate UUIDs creating 2 instances. 80 // 2 duplicate UUIDs creating 2 instances.
70 services_.push_back("00000001-0000-1000-8000-00805f9b34fb"); 81 service_uuids.push_back(duplicate_service_uuid_.canonical_value());
ortuno 2017/04/05 22:50:00 Out of curiosity why did you end up using push_bac
juncai 2017/04/05 22:55:15 From what I see, other places in this file use pus
71 services_.push_back("00000001-0000-1000-8000-00805f9b34fb"); 82 service_uuids.push_back(duplicate_service_uuid_.canonical_value());
72 SimulateGattServicesDiscovered(device_, services_); 83 SimulateGattServicesDiscovered(device_, service_uuids);
73 base::RunLoop().RunUntilIdle(); 84 base::RunLoop().RunUntilIdle();
85 #if defined(OS_WIN)
86 // TODO(crbug.com/507419): Check connection once CreateGattConnection is
87 // implemented on Windows.
88 EXPECT_FALSE(device_->IsGattServicesDiscoveryComplete());
89 #else
74 EXPECT_TRUE(device_->IsGattServicesDiscoveryComplete()); 90 EXPECT_TRUE(device_->IsGattServicesDiscoveryComplete());
91 #endif // defined(OS_WIN)
75 } 92 }
76 93
77 protected: 94 protected:
95 BluetoothUUID unique_service_uuid_;
96 BluetoothUUID duplicate_service_uuid_;
97
78 BluetoothDevice* device_ = nullptr; 98 BluetoothDevice* device_ = nullptr;
79 std::vector<std::string> services_;
80 }; 99 };
81 100
82 TEST(BluetoothDeviceTest, CanonicalizeAddressFormat_AcceptsAllValidFormats) { 101 TEST(BluetoothDeviceTest, CanonicalizeAddressFormat_AcceptsAllValidFormats) {
83 // There are three valid separators (':', '-', and none). 102 // There are three valid separators (':', '-', and none).
84 // Case shouldn't matter. 103 // Case shouldn't matter.
85 const char* const kValidFormats[] = { 104 const char* const kValidFormats[] = {
86 "1A:2B:3C:4D:5E:6F", 105 "1A:2B:3C:4D:5E:6F",
87 "1a:2B:3c:4D:5e:6F", 106 "1a:2B:3c:4D:5e:6F",
88 "1a:2b:3c:4d:5e:6f", 107 "1a:2b:3c:4d:5e:6f",
89 "1A-2B-3C-4D-5E-6F", 108 "1A-2B-3C-4D-5E-6F",
(...skipping 1419 matching lines...) Expand 10 before | Expand all | Expand 10 after
1509 EXPECT_EQ(BLUETOOTH_TRANSPORT_LE, device->GetType()); 1528 EXPECT_EQ(BLUETOOTH_TRANSPORT_LE, device->GetType());
1510 1529
1511 BluetoothDevice* device2 = SimulateLowEnergyDevice(6); 1530 BluetoothDevice* device2 = SimulateLowEnergyDevice(6);
1512 EXPECT_EQ(BLUETOOTH_TRANSPORT_DUAL, device2->GetType()); 1531 EXPECT_EQ(BLUETOOTH_TRANSPORT_DUAL, device2->GetType());
1513 1532
1514 BluetoothDevice* device3 = SimulateClassicDevice(); 1533 BluetoothDevice* device3 = SimulateClassicDevice();
1515 EXPECT_EQ(BLUETOOTH_TRANSPORT_CLASSIC, device3->GetType()); 1534 EXPECT_EQ(BLUETOOTH_TRANSPORT_CLASSIC, device3->GetType());
1516 } 1535 }
1517 #endif // defined(OS_CHROMEOS) || defined(OS_LINUX) 1536 #endif // defined(OS_CHROMEOS) || defined(OS_LINUX)
1518 1537
1519 #if defined(OS_ANDROID) || defined(OS_MACOSX) 1538 #if defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN)
1520 TEST_F(BluetoothGetServiceOrCharacteristicTest, GetPrimaryServices) { 1539 TEST_F(BluetoothGetServiceTest, GetPrimaryServices) {
1521 if (!PlatformSupportsLowEnergy()) { 1540 if (!PlatformSupportsLowEnergy()) {
1522 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; 1541 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
1523 return; 1542 return;
1524 } 1543 }
1525 ASSERT_NO_FATAL_FAILURE(FakeServiceBoilerplate()); 1544 ASSERT_NO_FATAL_FAILURE(FakeServiceBoilerplate());
1526 1545
1527 EXPECT_EQ(3u, device_->GetPrimaryServices().size()); 1546 EXPECT_EQ(3u, device_->GetPrimaryServices().size());
1528 } 1547 }
1529 1548
1530 TEST_F(BluetoothGetServiceOrCharacteristicTest, GetPrimaryServicesByUUID) { 1549 TEST_F(BluetoothGetServiceTest, GetPrimaryServicesByUUID) {
1531 if (!PlatformSupportsLowEnergy()) { 1550 if (!PlatformSupportsLowEnergy()) {
1532 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; 1551 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
1533 return; 1552 return;
1534 } 1553 }
1535 ASSERT_NO_FATAL_FAILURE(FakeServiceBoilerplate()); 1554 ASSERT_NO_FATAL_FAILURE(FakeServiceBoilerplate());
1536 1555
1537 EXPECT_EQ( 1556 {
1538 1u, 1557 std::vector<BluetoothRemoteGattService*> services =
1539 device_->GetPrimaryServicesByUUID(BluetoothUUID(services_[0])).size()); 1558 device_->GetPrimaryServicesByUUID(unique_service_uuid_);
1540 EXPECT_EQ( 1559 EXPECT_EQ(1u, services.size());
1541 2u, 1560 EXPECT_EQ(unique_service_uuid_, services[0]->GetUUID());
1542 device_->GetPrimaryServicesByUUID(BluetoothUUID(services_[1])).size()); 1561 }
1543 std::string service_uuid_not_exist_in_setup = 1562
1544 "00000002-0000-1000-8000-00805f9b34fb"; 1563 {
1545 EXPECT_TRUE(device_ 1564 std::vector<BluetoothRemoteGattService*> services =
1546 ->GetPrimaryServicesByUUID( 1565 device_->GetPrimaryServicesByUUID(duplicate_service_uuid_);
1547 BluetoothUUID(service_uuid_not_exist_in_setup)) 1566 EXPECT_EQ(2u, services.size());
1548 .empty()); 1567 EXPECT_EQ(duplicate_service_uuid_, services[0]->GetUUID());
1568 EXPECT_EQ(duplicate_service_uuid_, services[1]->GetUUID());
1569
1570 EXPECT_TRUE(device_
1571 ->GetPrimaryServicesByUUID(BluetoothUUID(
1572 BluetoothTestBase::kTestUUIDGenericAttribute))
1573 .empty());
1574
1575 EXPECT_NE(services[0]->GetIdentifier(), services[1]->GetIdentifier());
1576 }
1549 } 1577 }
1550 1578 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN)
1551 TEST_F(BluetoothGetServiceOrCharacteristicTest, GetCharacteristicsByUUID) {
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,
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)
1621 1579
1622 } // namespace device 1580 } // namespace device
OLDNEW
« no previous file with comments | « device/bluetooth/bluetooth_device.cc ('k') | device/bluetooth/bluetooth_remote_gatt_service.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698