OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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_gatt_service.h" | 5 #include "device/bluetooth/bluetooth_gatt_service.h" |
6 | 6 |
7 #include "build/build_config.h" | 7 #include "build/build_config.h" |
8 #include "device/bluetooth/bluetooth_gatt_characteristic.h" | 8 #include "device/bluetooth/bluetooth_gatt_characteristic.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 | 10 |
11 #if defined(OS_ANDROID) | 11 #if defined(OS_ANDROID) |
12 #include "device/bluetooth/test/bluetooth_test_android.h" | 12 #include "device/bluetooth/test/bluetooth_test_android.h" |
13 #elif defined(OS_MACOSX) | 13 #elif defined(OS_MACOSX) |
14 #include "device/bluetooth/test/bluetooth_test_mac.h" | 14 #include "device/bluetooth/test/bluetooth_test_mac.h" |
| 15 #elif defined(OS_WIN) |
| 16 #include "device/bluetooth/test/bluetooth_test_win.h" |
15 #endif | 17 #endif |
16 | 18 |
17 namespace device { | 19 namespace device { |
18 | 20 |
19 #if defined(OS_ANDROID) || defined(OS_MACOSX) | 21 #if defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) |
20 class BluetoothGattServiceTest : public BluetoothTest {}; | 22 class BluetoothGattServiceTest : public BluetoothTest {}; |
21 #endif | 23 #endif |
22 | 24 |
23 #if defined(OS_ANDROID) | 25 #if defined(OS_ANDROID) || defined(OS_WIN) |
24 TEST_F(BluetoothGattServiceTest, GetIdentifier) { | 26 TEST_F(BluetoothGattServiceTest, GetIdentifier) { |
| 27 if (!PlatformSupportsLowEnergy()) { |
| 28 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
| 29 return; |
| 30 } |
25 InitWithFakeAdapter(); | 31 InitWithFakeAdapter(); |
26 StartLowEnergyDiscoverySession(); | 32 StartLowEnergyDiscoverySession(); |
27 // 2 devices to verify unique IDs across them. | 33 // 2 devices to verify unique IDs across them. |
28 BluetoothDevice* device1 = DiscoverLowEnergyDevice(3); | 34 BluetoothDevice* device1 = DiscoverLowEnergyDevice(3); |
29 BluetoothDevice* device2 = DiscoverLowEnergyDevice(4); | 35 BluetoothDevice* device2 = DiscoverLowEnergyDevice(4); |
30 device1->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 36 device1->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
31 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 37 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
32 device2->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 38 device2->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
33 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 39 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
34 SimulateGattConnection(device1); | 40 SimulateGattConnection(device1); |
(...skipping 14 matching lines...) Expand all Loading... |
49 // All IDs are unique, even though they have the same UUID. | 55 // All IDs are unique, even though they have the same UUID. |
50 EXPECT_NE(service1->GetIdentifier(), service2->GetIdentifier()); | 56 EXPECT_NE(service1->GetIdentifier(), service2->GetIdentifier()); |
51 EXPECT_NE(service1->GetIdentifier(), service3->GetIdentifier()); | 57 EXPECT_NE(service1->GetIdentifier(), service3->GetIdentifier()); |
52 EXPECT_NE(service1->GetIdentifier(), service4->GetIdentifier()); | 58 EXPECT_NE(service1->GetIdentifier(), service4->GetIdentifier()); |
53 | 59 |
54 EXPECT_NE(service2->GetIdentifier(), service3->GetIdentifier()); | 60 EXPECT_NE(service2->GetIdentifier(), service3->GetIdentifier()); |
55 EXPECT_NE(service2->GetIdentifier(), service4->GetIdentifier()); | 61 EXPECT_NE(service2->GetIdentifier(), service4->GetIdentifier()); |
56 | 62 |
57 EXPECT_NE(service3->GetIdentifier(), service4->GetIdentifier()); | 63 EXPECT_NE(service3->GetIdentifier(), service4->GetIdentifier()); |
58 } | 64 } |
59 #endif // defined(OS_ANDROID) | 65 #endif // defined(OS_ANDROID) || defined(OS_WIN) |
60 | 66 |
61 #if defined(OS_ANDROID) | 67 #if defined(OS_ANDROID) || defined(OS_WIN) |
62 TEST_F(BluetoothGattServiceTest, GetUUID) { | 68 TEST_F(BluetoothGattServiceTest, GetUUID) { |
| 69 if (!PlatformSupportsLowEnergy()) { |
| 70 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
| 71 return; |
| 72 } |
63 InitWithFakeAdapter(); | 73 InitWithFakeAdapter(); |
64 StartLowEnergyDiscoverySession(); | 74 StartLowEnergyDiscoverySession(); |
65 BluetoothDevice* device = DiscoverLowEnergyDevice(3); | 75 BluetoothDevice* device = DiscoverLowEnergyDevice(3); |
66 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 76 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
67 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 77 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
68 SimulateGattConnection(device); | 78 SimulateGattConnection(device); |
69 | 79 |
70 // Create multiple instances with the same UUID. | 80 // Create multiple instances with the same UUID. |
71 BluetoothUUID uuid("00000000-0000-1000-8000-00805f9b34fb"); | 81 BluetoothUUID uuid("00000000-0000-1000-8000-00805f9b34fb"); |
72 std::vector<std::string> services; | 82 std::vector<std::string> services; |
73 services.push_back(uuid.canonical_value()); | 83 services.push_back(uuid.canonical_value()); |
74 services.push_back(uuid.canonical_value()); | 84 services.push_back(uuid.canonical_value()); |
75 SimulateGattServicesDiscovered(device, services); | 85 SimulateGattServicesDiscovered(device, services); |
76 | 86 |
77 // Each has the same UUID. | 87 // Each has the same UUID. |
78 EXPECT_EQ(uuid, device->GetGattServices()[0]->GetUUID()); | 88 EXPECT_EQ(uuid, device->GetGattServices()[0]->GetUUID()); |
79 EXPECT_EQ(uuid, device->GetGattServices()[1]->GetUUID()); | 89 EXPECT_EQ(uuid, device->GetGattServices()[1]->GetUUID()); |
80 } | 90 } |
81 #endif // defined(OS_ANDROID) | 91 #endif // defined(OS_ANDROID) || defined(OS_WIN) |
82 | 92 |
83 #if defined(OS_ANDROID) | 93 #if defined(OS_ANDROID) || defined(OS_WIN) |
84 TEST_F(BluetoothGattServiceTest, GetCharacteristics_FindNone) { | 94 TEST_F(BluetoothGattServiceTest, GetCharacteristics_FindNone) { |
| 95 if (!PlatformSupportsLowEnergy()) { |
| 96 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
| 97 return; |
| 98 } |
85 InitWithFakeAdapter(); | 99 InitWithFakeAdapter(); |
86 StartLowEnergyDiscoverySession(); | 100 StartLowEnergyDiscoverySession(); |
87 BluetoothDevice* device = DiscoverLowEnergyDevice(3); | 101 BluetoothDevice* device = DiscoverLowEnergyDevice(3); |
88 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 102 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
89 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 103 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
90 SimulateGattConnection(device); | 104 SimulateGattConnection(device); |
91 | 105 |
92 // Simulate a service, with no Characteristics: | 106 // Simulate a service, with no Characteristics: |
93 std::vector<std::string> services; | 107 std::vector<std::string> services; |
94 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); | 108 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); |
95 SimulateGattServicesDiscovered(device, services); | 109 SimulateGattServicesDiscovered(device, services); |
96 BluetoothGattService* service = device->GetGattServices()[0]; | 110 BluetoothGattService* service = device->GetGattServices()[0]; |
97 | 111 |
98 EXPECT_EQ(0u, service->GetCharacteristics().size()); | 112 EXPECT_EQ(0u, service->GetCharacteristics().size()); |
99 } | 113 } |
100 #endif // defined(OS_ANDROID) | 114 #endif // defined(OS_ANDROID) || defined(OS_WIN) |
101 | 115 |
102 #if defined(OS_ANDROID) | 116 #if defined(OS_ANDROID) || defined(OS_WIN) |
103 TEST_F(BluetoothGattServiceTest, GetCharacteristics_and_GetCharacteristic) { | 117 TEST_F(BluetoothGattServiceTest, GetCharacteristics_and_GetCharacteristic) { |
| 118 if (!PlatformSupportsLowEnergy()) { |
| 119 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
| 120 return; |
| 121 } |
104 InitWithFakeAdapter(); | 122 InitWithFakeAdapter(); |
105 StartLowEnergyDiscoverySession(); | 123 StartLowEnergyDiscoverySession(); |
106 BluetoothDevice* device = DiscoverLowEnergyDevice(3); | 124 BluetoothDevice* device = DiscoverLowEnergyDevice(3); |
107 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 125 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
108 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 126 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
109 SimulateGattConnection(device); | 127 SimulateGattConnection(device); |
110 | 128 |
111 // Simulate a service, with several Characteristics: | 129 // Simulate a service, with several Characteristics: |
112 std::vector<std::string> services; | 130 std::vector<std::string> services; |
113 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); | 131 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); |
(...skipping 24 matching lines...) Expand all Loading... |
138 EXPECT_EQ(char_uuid3, service->GetCharacteristic(char_id3)->GetUUID()); | 156 EXPECT_EQ(char_uuid3, service->GetCharacteristic(char_id3)->GetUUID()); |
139 EXPECT_EQ(char_uuid4, service->GetCharacteristic(char_id4)->GetUUID()); | 157 EXPECT_EQ(char_uuid4, service->GetCharacteristic(char_id4)->GetUUID()); |
140 | 158 |
141 // GetCharacteristics & GetCharacteristic return the same object for the same | 159 // GetCharacteristics & GetCharacteristic return the same object for the same |
142 // ID: | 160 // ID: |
143 EXPECT_EQ(service->GetCharacteristics()[0], | 161 EXPECT_EQ(service->GetCharacteristics()[0], |
144 service->GetCharacteristic(char_id1)); | 162 service->GetCharacteristic(char_id1)); |
145 EXPECT_EQ(service->GetCharacteristic(char_id1), | 163 EXPECT_EQ(service->GetCharacteristic(char_id1), |
146 service->GetCharacteristic(char_id1)); | 164 service->GetCharacteristic(char_id1)); |
147 } | 165 } |
148 #endif // defined(OS_ANDROID) | 166 #endif // defined(OS_ANDROID) || defined(OS_WIN) |
| 167 |
| 168 #if defined(OS_WIN) |
| 169 TEST_F(BluetoothGattServiceTest, GetCharacteristic_CharacteristicRemoved) { |
| 170 if (!PlatformSupportsLowEnergy()) { |
| 171 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
| 172 return; |
| 173 } |
| 174 InitWithFakeAdapter(); |
| 175 StartLowEnergyDiscoverySession(); |
| 176 BluetoothDevice* device = DiscoverLowEnergyDevice(3); |
| 177 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
| 178 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
| 179 SimulateGattConnection(device); |
| 180 |
| 181 // Simulate a service, with several Characteristics: |
| 182 std::vector<std::string> services; |
| 183 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); |
| 184 SimulateGattServicesDiscovered(device, services); |
| 185 BluetoothGattService* service = device->GetGattServices()[0]; |
| 186 std::string characteristic_uuid1 = "11111111-0000-1000-8000-00805f9b34fb"; |
| 187 std::string characteristic_uuid2 = "22222222-0000-1000-8000-00805f9b34fb"; |
| 188 std::string characteristic_uuid3 = characteristic_uuid2; // Duplicate UUID. |
| 189 std::string characteristic_uuid4 = "33333333-0000-1000-8000-00805f9b34fb"; |
| 190 SimulateGattCharacteristic(service, characteristic_uuid1, /* properties */ 0); |
| 191 SimulateGattCharacteristic(service, characteristic_uuid2, /* properties */ 0); |
| 192 SimulateGattCharacteristic(service, characteristic_uuid3, /* properties */ 0); |
| 193 SimulateGattCharacteristic(service, characteristic_uuid4, /* properties */ 0); |
| 194 |
| 195 // Simulate remove of characteristics one by one. |
| 196 EXPECT_EQ(4u, service->GetCharacteristics().size()); |
| 197 std::string removed_char = service->GetCharacteristics()[0]->GetIdentifier(); |
| 198 SimulateGattCharacteristicRemove(service, |
| 199 service->GetCharacteristic(removed_char)); |
| 200 EXPECT_FALSE(service->GetCharacteristic(removed_char)); |
| 201 EXPECT_EQ(3u, service->GetCharacteristics().size()); |
| 202 removed_char = service->GetCharacteristics()[0]->GetIdentifier(); |
| 203 SimulateGattCharacteristicRemove(service, |
| 204 service->GetCharacteristic(removed_char)); |
| 205 EXPECT_FALSE(service->GetCharacteristic(removed_char)); |
| 206 EXPECT_EQ(2u, service->GetCharacteristics().size()); |
| 207 removed_char = service->GetCharacteristics()[0]->GetIdentifier(); |
| 208 SimulateGattCharacteristicRemove(service, |
| 209 service->GetCharacteristic(removed_char)); |
| 210 EXPECT_FALSE(service->GetCharacteristic(removed_char)); |
| 211 EXPECT_EQ(1u, service->GetCharacteristics().size()); |
| 212 removed_char = service->GetCharacteristics()[0]->GetIdentifier(); |
| 213 SimulateGattCharacteristicRemove(service, |
| 214 service->GetCharacteristic(removed_char)); |
| 215 EXPECT_FALSE(service->GetCharacteristic(removed_char)); |
| 216 EXPECT_EQ(0u, service->GetCharacteristics().size()); |
| 217 } |
| 218 #endif // defined(OS_WIN) |
| 219 |
| 220 #if defined(OS_WIN) |
| 221 TEST_F(BluetoothGattServiceTest, GetIncludedServices) { |
| 222 if (!PlatformSupportsLowEnergy()) { |
| 223 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
| 224 return; |
| 225 } |
| 226 InitWithFakeAdapter(); |
| 227 StartLowEnergyDiscoverySession(); |
| 228 BluetoothDevice* device = DiscoverLowEnergyDevice(3); |
| 229 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
| 230 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
| 231 SimulateGattConnection(device); |
| 232 |
| 233 // Simulate a service, with several included services: |
| 234 std::vector<std::string> services; |
| 235 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); |
| 236 SimulateGattServicesDiscovered(device, services); |
| 237 BluetoothGattService* service = device->GetGattServices()[0]; |
| 238 std::vector<std::string> included_service_uuids; |
| 239 included_service_uuids.push_back("11111111-0000-1000-8000-00805f9b34fb"); |
| 240 included_service_uuids.push_back("22222222-0000-1000-8000-00805f9b34fb"); |
| 241 included_service_uuids.push_back( |
| 242 "22222222-0000-1000-8000-00805f9b34fb"); // Duplicate UUID. |
| 243 included_service_uuids.push_back("33333333-0000-1000-8000-00805f9b34fb"); |
| 244 SimulateIncludedGattServicesDiscovered(service, included_service_uuids); |
| 245 |
| 246 // Verify that GetIncludedServices can retrieve included services. |
| 247 EXPECT_EQ(4u, service->GetIncludedServices().size()); |
| 248 std::vector<BluetoothGattService*> f_included_services = |
| 249 service->GetIncludedServices(); |
| 250 for (auto i_s : f_included_services) { |
| 251 EXPECT_FALSE(i_s->IsPrimary()); |
| 252 } |
| 253 std::vector<std::string> f_included_service_uuids; |
| 254 f_included_service_uuids.push_back(f_included_services[0]->GetUUID().value()); |
| 255 f_included_service_uuids.push_back(f_included_services[1]->GetUUID().value()); |
| 256 f_included_service_uuids.push_back(f_included_services[2]->GetUUID().value()); |
| 257 f_included_service_uuids.push_back(f_included_services[3]->GetUUID().value()); |
| 258 std::sort(f_included_service_uuids.begin(), f_included_service_uuids.end()); |
| 259 std::sort(included_service_uuids.begin(), included_service_uuids.end()); |
| 260 for (std::size_t i = 0; i < 4; i++) { |
| 261 EXPECT_EQ(f_included_service_uuids[i], included_service_uuids[i]); |
| 262 } |
| 263 } |
| 264 #endif // defined(OS_WIN) |
| 265 |
| 266 #if defined(OS_WIN) |
| 267 TEST_F(BluetoothGattServiceTest, SimulateGattServiceRemove) { |
| 268 if (!PlatformSupportsLowEnergy()) { |
| 269 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
| 270 return; |
| 271 } |
| 272 InitWithFakeAdapter(); |
| 273 StartLowEnergyDiscoverySession(); |
| 274 BluetoothDevice* device = DiscoverLowEnergyDevice(3); |
| 275 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
| 276 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
| 277 SimulateGattConnection(device); |
| 278 |
| 279 // Simulate two primary GATT services, with several included services: |
| 280 std::vector<std::string> services; |
| 281 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); |
| 282 services.push_back("01010101-0101-1000-8000-00805f9b34fb"); |
| 283 SimulateGattServicesDiscovered(device, services); |
| 284 EXPECT_EQ(2u, device->GetGattServices().size()); |
| 285 BluetoothGattService* service1 = device->GetGattServices()[0]; |
| 286 BluetoothGattService* service2 = device->GetGattServices()[1]; |
| 287 std::vector<std::string> included_service_uuids; |
| 288 included_service_uuids.push_back("11111111-0000-1000-8000-00805f9b34fb"); |
| 289 included_service_uuids.push_back("22222222-0000-1000-8000-00805f9b34fb"); |
| 290 included_service_uuids.push_back( |
| 291 "22222222-0000-1000-8000-00805f9b34fb"); // Duplicate UUID. |
| 292 included_service_uuids.push_back("33333333-0000-1000-8000-00805f9b34fb"); |
| 293 SimulateIncludedGattServicesDiscovered(service1, included_service_uuids); |
| 294 SimulateIncludedGattServicesDiscovered(service2, included_service_uuids); |
| 295 EXPECT_EQ(4u, service1->GetIncludedServices().size()); |
| 296 EXPECT_EQ(4u, service2->GetIncludedServices().size()); |
| 297 |
| 298 // Simulate remove of an included service. |
| 299 SimulateGattServiceRemoved(service1->GetIncludedServices()[0]); |
| 300 EXPECT_EQ(3u, service1->GetIncludedServices().size()); |
| 301 SimulateGattServiceRemoved(service2->GetIncludedServices()[0]); |
| 302 EXPECT_EQ(3u, service2->GetIncludedServices().size()); |
| 303 |
| 304 // Simulate remove of a primary service. |
| 305 std::string removed_service = service1->GetIdentifier(); |
| 306 SimulateGattServiceRemoved(device->GetGattService(removed_service)); |
| 307 EXPECT_EQ(1u, device->GetGattServices().size()); |
| 308 EXPECT_FALSE(device->GetGattService(removed_service)); |
| 309 } |
| 310 #endif // defined(OS_WIN) |
149 | 311 |
150 } // namespace device | 312 } // namespace device |
OLD | NEW |