| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/run_loop.h" |
| 6 #include "device/bluetooth/bluetooth_gatt_characteristic.h" |
| 7 #include "device/bluetooth/bluetooth_gatt_service.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 |
| 10 #if defined(OS_ANDROID) |
| 11 #include "device/bluetooth/test/bluetooth_test_android.h" |
| 12 #elif defined(OS_MACOSX) |
| 13 #include "device/bluetooth/test/bluetooth_test_mac.h" |
| 14 #endif |
| 15 |
| 16 namespace device { |
| 17 |
| 18 #if defined(OS_ANDROID) || defined(OS_MACOSX) |
| 19 class BluetoothGattDescriptorTest : public BluetoothTest {}; |
| 20 #endif |
| 21 |
| 22 #if defined(OS_ANDROID) |
| 23 TEST_F(BluetoothGattDescriptorTest, GetIdentifier) { |
| 24 InitWithFakeAdapter(); |
| 25 StartLowEnergyDiscoverySession(); |
| 26 // 2 devices to verify that descriptors on them have distinct IDs. |
| 27 BluetoothDevice* device1 = DiscoverLowEnergyDevice(3); |
| 28 BluetoothDevice* device2 = DiscoverLowEnergyDevice(4); |
| 29 device1->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
| 30 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
| 31 device2->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
| 32 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
| 33 SimulateGattConnection(device1); |
| 34 SimulateGattConnection(device2); |
| 35 |
| 36 // 3 services (all with same UUID). |
| 37 // 1 on the first device (to test characteristic instances across devices). |
| 38 // 2 on the second device (to test same device, multiple service instances). |
| 39 std::vector<std::string> services; |
| 40 std::string uuid = "00000000-0000-1000-8000-00805f9b34fb"; |
| 41 services.push_back(uuid); |
| 42 SimulateGattServicesDiscovered(device1, services); |
| 43 services.push_back(uuid); |
| 44 SimulateGattServicesDiscovered(device2, services); |
| 45 BluetoothGattService* service1 = device1->GetGattServices()[0]; |
| 46 BluetoothGattService* service2 = device2->GetGattServices()[0]; |
| 47 BluetoothGattService* service3 = device2->GetGattServices()[1]; |
| 48 // 6 characteristics (same UUID), 2 on each service. |
| 49 SimulateGattCharacteristic(service1, uuid, /* properties */ 0); |
| 50 SimulateGattCharacteristic(service1, uuid, /* properties */ 0); |
| 51 SimulateGattCharacteristic(service2, uuid, /* properties */ 0); |
| 52 SimulateGattCharacteristic(service2, uuid, /* properties */ 0); |
| 53 SimulateGattCharacteristic(service3, uuid, /* properties */ 0); |
| 54 SimulateGattCharacteristic(service3, uuid, /* properties */ 0); |
| 55 BluetoothGattCharacteristic* char1 = service1->GetCharacteristics()[0]; |
| 56 BluetoothGattCharacteristic* char2 = service1->GetCharacteristics()[1]; |
| 57 BluetoothGattCharacteristic* char3 = service2->GetCharacteristics()[0]; |
| 58 BluetoothGattCharacteristic* char4 = service2->GetCharacteristics()[1]; |
| 59 BluetoothGattCharacteristic* char5 = service3->GetCharacteristics()[0]; |
| 60 BluetoothGattCharacteristic* char6 = service3->GetCharacteristics()[1]; |
| 61 // 6 descriptors (same UUID), 1 on each characteristic |
| 62 // TODO(crbug.com/576900) Test multiple descriptors with same UUID on one |
| 63 // characteristic. |
| 64 SimulateGattDescriptor(char1, uuid); |
| 65 SimulateGattDescriptor(char2, uuid); |
| 66 SimulateGattDescriptor(char3, uuid); |
| 67 SimulateGattDescriptor(char4, uuid); |
| 68 SimulateGattDescriptor(char5, uuid); |
| 69 SimulateGattDescriptor(char6, uuid); |
| 70 BluetoothGattDescriptor* desc1 = char1->GetDescriptors()[0]; |
| 71 BluetoothGattDescriptor* desc2 = char2->GetDescriptors()[0]; |
| 72 BluetoothGattDescriptor* desc3 = char3->GetDescriptors()[0]; |
| 73 BluetoothGattDescriptor* desc4 = char4->GetDescriptors()[0]; |
| 74 BluetoothGattDescriptor* desc5 = char5->GetDescriptors()[0]; |
| 75 BluetoothGattDescriptor* desc6 = char6->GetDescriptors()[0]; |
| 76 |
| 77 // All IDs are unique. |
| 78 EXPECT_NE(desc1->GetIdentifier(), desc2->GetIdentifier()); |
| 79 EXPECT_NE(desc1->GetIdentifier(), desc3->GetIdentifier()); |
| 80 EXPECT_NE(desc1->GetIdentifier(), desc4->GetIdentifier()); |
| 81 EXPECT_NE(desc1->GetIdentifier(), desc5->GetIdentifier()); |
| 82 EXPECT_NE(desc1->GetIdentifier(), desc6->GetIdentifier()); |
| 83 |
| 84 EXPECT_NE(desc2->GetIdentifier(), desc3->GetIdentifier()); |
| 85 EXPECT_NE(desc2->GetIdentifier(), desc4->GetIdentifier()); |
| 86 EXPECT_NE(desc2->GetIdentifier(), desc5->GetIdentifier()); |
| 87 EXPECT_NE(desc2->GetIdentifier(), desc6->GetIdentifier()); |
| 88 |
| 89 EXPECT_NE(desc3->GetIdentifier(), desc4->GetIdentifier()); |
| 90 EXPECT_NE(desc3->GetIdentifier(), desc5->GetIdentifier()); |
| 91 EXPECT_NE(desc3->GetIdentifier(), desc6->GetIdentifier()); |
| 92 |
| 93 EXPECT_NE(desc4->GetIdentifier(), desc5->GetIdentifier()); |
| 94 EXPECT_NE(desc4->GetIdentifier(), desc6->GetIdentifier()); |
| 95 |
| 96 EXPECT_NE(desc5->GetIdentifier(), desc6->GetIdentifier()); |
| 97 } |
| 98 #endif // defined(OS_ANDROID) |
| 99 |
| 100 #if defined(OS_ANDROID) |
| 101 TEST_F(BluetoothGattDescriptorTest, GetUUID) { |
| 102 InitWithFakeAdapter(); |
| 103 StartLowEnergyDiscoverySession(); |
| 104 BluetoothDevice* device = DiscoverLowEnergyDevice(3); |
| 105 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
| 106 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
| 107 SimulateGattConnection(device); |
| 108 std::vector<std::string> services; |
| 109 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); |
| 110 SimulateGattServicesDiscovered(device, services); |
| 111 ASSERT_EQ(1u, device->GetGattServices().size()); |
| 112 BluetoothGattService* service = device->GetGattServices()[0]; |
| 113 |
| 114 SimulateGattCharacteristic(service, "00000000-0000-1000-8000-00805f9b34fb", |
| 115 /* properties */ 0); |
| 116 ASSERT_EQ(1u, service->GetCharacteristics().size()); |
| 117 BluetoothGattCharacteristic* characteristic = |
| 118 service->GetCharacteristics()[0]; |
| 119 |
| 120 // Create 2 descriptors. |
| 121 std::string uuid_str1("11111111-0000-1000-8000-00805f9b34fb"); |
| 122 std::string uuid_str2("22222222-0000-1000-8000-00805f9b34fb"); |
| 123 BluetoothUUID uuid1(uuid_str1); |
| 124 BluetoothUUID uuid2(uuid_str2); |
| 125 SimulateGattDescriptor(characteristic, uuid_str1); |
| 126 SimulateGattDescriptor(characteristic, uuid_str2); |
| 127 ASSERT_EQ(2u, characteristic->GetDescriptors().size()); |
| 128 BluetoothGattDescriptor* descriptor1 = characteristic->GetDescriptors()[0]; |
| 129 BluetoothGattDescriptor* descriptor2 = characteristic->GetDescriptors()[1]; |
| 130 |
| 131 // Swap as needed to have descriptor1 be the one with uuid1. |
| 132 if (descriptor2->GetUUID() == uuid1) |
| 133 std::swap(descriptor1, descriptor2); |
| 134 |
| 135 EXPECT_EQ(uuid1, descriptor1->GetUUID()); |
| 136 EXPECT_EQ(uuid2, descriptor2->GetUUID()); |
| 137 } |
| 138 #endif // defined(OS_ANDROID) |
| 139 |
| 140 } // namespace device |
| OLD | NEW |