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

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

Issue 2785383002: Update unit tests for BluetoothDevice::GetPrimaryServicesByUUID() etc. (Closed)
Patch Set: address more comments 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
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_|, |service_uuids_|.
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)
ortuno 2017/04/04 22:14:12 // TODO(crbug.com/507419): Check connection once C
juncai 2017/04/05 00:36:45 Done.
70 EXPECT_FALSE(device_->IsConnected());
71 #else
65 EXPECT_TRUE(device_->IsConnected()); 72 EXPECT_TRUE(device_->IsConnected());
73 #endif // defined(OS_WIN)
66 74
67 // Discover services. 75 // Discover services.
68 services_.push_back("00000000-0000-1000-8000-00805f9b34fb"); 76 service_uuids_.push_back(unique_service_uuid_.value());
ortuno 2017/04/04 22:14:12 nit: use canonical_value. value() can change depen
juncai 2017/04/05 00:36:45 Done.
69 // 2 duplicate UUIDs creating 2 instances. 77 // 2 duplicate UUIDs creating 2 instances.
70 services_.push_back("00000001-0000-1000-8000-00805f9b34fb"); 78 service_uuids_.push_back(duplicate_service_uuid_.value());
71 services_.push_back("00000001-0000-1000-8000-00805f9b34fb"); 79 service_uuids_.push_back(duplicate_service_uuid_.value());
72 SimulateGattServicesDiscovered(device_, services_); 80 SimulateGattServicesDiscovered(device_, service_uuids_);
73 base::RunLoop().RunUntilIdle(); 81 base::RunLoop().RunUntilIdle();
82 #if defined(OS_WIN)
83 EXPECT_FALSE(device_->IsGattServicesDiscoveryComplete());
84 #else
74 EXPECT_TRUE(device_->IsGattServicesDiscoveryComplete()); 85 EXPECT_TRUE(device_->IsGattServicesDiscoveryComplete());
86 #endif // defined(OS_WIN)
75 } 87 }
76 88
77 protected: 89 protected:
90 BluetoothUUID unique_service_uuid_;
91 BluetoothUUID duplicate_service_uuid_;
92
78 BluetoothDevice* device_ = nullptr; 93 BluetoothDevice* device_ = nullptr;
79 std::vector<std::string> services_; 94 std::vector<std::string> service_uuids_;
80 }; 95 };
81 96
82 TEST(BluetoothDeviceTest, CanonicalizeAddressFormat_AcceptsAllValidFormats) { 97 TEST(BluetoothDeviceTest, CanonicalizeAddressFormat_AcceptsAllValidFormats) {
83 // There are three valid separators (':', '-', and none). 98 // There are three valid separators (':', '-', and none).
84 // Case shouldn't matter. 99 // Case shouldn't matter.
85 const char* const kValidFormats[] = { 100 const char* const kValidFormats[] = {
86 "1A:2B:3C:4D:5E:6F", 101 "1A:2B:3C:4D:5E:6F",
87 "1a:2B:3c:4D:5e:6F", 102 "1a:2B:3c:4D:5e:6F",
88 "1a:2b:3c:4d:5e:6f", 103 "1a:2b:3c:4d:5e:6f",
89 "1A-2B-3C-4D-5E-6F", 104 "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()); 1524 EXPECT_EQ(BLUETOOTH_TRANSPORT_LE, device->GetType());
1510 1525
1511 BluetoothDevice* device2 = SimulateLowEnergyDevice(6); 1526 BluetoothDevice* device2 = SimulateLowEnergyDevice(6);
1512 EXPECT_EQ(BLUETOOTH_TRANSPORT_DUAL, device2->GetType()); 1527 EXPECT_EQ(BLUETOOTH_TRANSPORT_DUAL, device2->GetType());
1513 1528
1514 BluetoothDevice* device3 = SimulateClassicDevice(); 1529 BluetoothDevice* device3 = SimulateClassicDevice();
1515 EXPECT_EQ(BLUETOOTH_TRANSPORT_CLASSIC, device3->GetType()); 1530 EXPECT_EQ(BLUETOOTH_TRANSPORT_CLASSIC, device3->GetType());
1516 } 1531 }
1517 #endif // defined(OS_CHROMEOS) || defined(OS_LINUX) 1532 #endif // defined(OS_CHROMEOS) || defined(OS_LINUX)
1518 1533
1519 #if defined(OS_ANDROID) || defined(OS_MACOSX) 1534 #if defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN)
1520 TEST_F(BluetoothGetServiceOrCharacteristicTest, GetPrimaryServices) { 1535 TEST_F(BluetoothGetServiceTest, GetPrimaryServices) {
1521 if (!PlatformSupportsLowEnergy()) { 1536 if (!PlatformSupportsLowEnergy()) {
1522 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; 1537 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
1523 return; 1538 return;
1524 } 1539 }
1525 ASSERT_NO_FATAL_FAILURE(FakeServiceBoilerplate()); 1540 ASSERT_NO_FATAL_FAILURE(FakeServiceBoilerplate());
1526 1541
1527 EXPECT_EQ(3u, device_->GetPrimaryServices().size()); 1542 EXPECT_EQ(3u, device_->GetPrimaryServices().size());
1528 } 1543 }
1529 1544
1530 TEST_F(BluetoothGetServiceOrCharacteristicTest, GetPrimaryServicesByUUID) { 1545 TEST_F(BluetoothGetServiceTest, GetPrimaryServicesByUUID) {
1531 if (!PlatformSupportsLowEnergy()) { 1546 if (!PlatformSupportsLowEnergy()) {
1532 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; 1547 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
1533 return; 1548 return;
1534 } 1549 }
1535 ASSERT_NO_FATAL_FAILURE(FakeServiceBoilerplate()); 1550 ASSERT_NO_FATAL_FAILURE(FakeServiceBoilerplate());
1536 1551
1537 EXPECT_EQ( 1552 {
1538 1u, 1553 std::vector<BluetoothRemoteGattService*> services =
1539 device_->GetPrimaryServicesByUUID(BluetoothUUID(services_[0])).size()); 1554 device_->GetPrimaryServicesByUUID(unique_service_uuid_);
1540 EXPECT_EQ( 1555 EXPECT_EQ(1u, services.size());
1541 2u, 1556 EXPECT_EQ(unique_service_uuid_, services[0]->GetUUID());
1542 device_->GetPrimaryServicesByUUID(BluetoothUUID(services_[1])).size()); 1557 }
1543 std::string service_uuid_not_exist_in_setup = 1558
1544 "00000002-0000-1000-8000-00805f9b34fb"; 1559 {
1545 EXPECT_TRUE(device_ 1560 std::vector<BluetoothRemoteGattService*> services =
1546 ->GetPrimaryServicesByUUID( 1561 device_->GetPrimaryServicesByUUID(duplicate_service_uuid_);
1547 BluetoothUUID(service_uuid_not_exist_in_setup)) 1562 EXPECT_EQ(2u, services.size());
1548 .empty()); 1563 EXPECT_EQ(duplicate_service_uuid_, services[0]->GetUUID());
1564 EXPECT_EQ(duplicate_service_uuid_, services[1]->GetUUID());
1565
1566 EXPECT_TRUE(device_
1567 ->GetPrimaryServicesByUUID(BluetoothUUID(
1568 BluetoothTestBase::kTestUUIDGenericAttribute))
1569 .empty());
1570
1571 EXPECT_NE(services[0]->GetIdentifier(), services[1]->GetIdentifier());
1572 }
1549 } 1573 }
1550 1574 #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 1575
1622 } // namespace device 1576 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698