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_remote_gatt_characteristic.h" | 8 #include "device/bluetooth/bluetooth_remote_gatt_characteristic.h" |
9 #include "device/bluetooth/test/test_bluetooth_adapter_observer.h" | 9 #include "device/bluetooth/test/test_bluetooth_adapter_observer.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
11 | 11 |
12 #if defined(OS_ANDROID) | 12 #if defined(OS_ANDROID) |
13 #include "device/bluetooth/test/bluetooth_test_android.h" | 13 #include "device/bluetooth/test/bluetooth_test_android.h" |
14 #elif defined(OS_MACOSX) | 14 #elif defined(OS_MACOSX) |
15 #include "device/bluetooth/test/bluetooth_test_mac.h" | 15 #include "device/bluetooth/test/bluetooth_test_mac.h" |
16 #elif defined(OS_WIN) | 16 #elif defined(OS_WIN) |
17 #include "device/bluetooth/test/bluetooth_test_win.h" | 17 #include "device/bluetooth/test/bluetooth_test_win.h" |
18 #endif | 18 #endif |
19 | 19 |
20 namespace device { | 20 namespace device { |
21 | 21 |
22 #if defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) | 22 #if defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) |
23 class BluetoothRemoteGattServiceTest : public BluetoothTest {}; | 23 class BluetoothRemoteGattServiceTest : public BluetoothTest {}; |
24 #endif | 24 #endif |
25 | 25 |
26 #if defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) | 26 #if defined(OS_ANDROID) || defined(OS_WIN) || defined(OS_MACOSX) |
27 TEST_F(BluetoothRemoteGattServiceTest, GetIdentifier) { | 27 TEST_F(BluetoothRemoteGattServiceTest, GetIdentifier) { |
28 if (!PlatformSupportsLowEnergy()) { | 28 if (!PlatformSupportsLowEnergy()) { |
29 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | 29 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
30 return; | 30 return; |
31 } | 31 } |
32 InitWithFakeAdapter(); | 32 InitWithFakeAdapter(); |
33 StartLowEnergyDiscoverySession(); | 33 StartLowEnergyDiscoverySession(); |
34 // 2 devices to verify unique IDs across them. | 34 // 2 devices to verify unique IDs across them. |
35 BluetoothDevice* device1 = SimulateLowEnergyDevice(3); | 35 BluetoothDevice* device1 = SimulateLowEnergyDevice(3); |
36 BluetoothDevice* device2 = SimulateLowEnergyDevice(4); | 36 BluetoothDevice* device2 = SimulateLowEnergyDevice(4); |
(...skipping 19 matching lines...) Expand all Loading... |
56 // All IDs are unique, even though they have the same UUID. | 56 // All IDs are unique, even though they have the same UUID. |
57 EXPECT_NE(service1->GetIdentifier(), service2->GetIdentifier()); | 57 EXPECT_NE(service1->GetIdentifier(), service2->GetIdentifier()); |
58 EXPECT_NE(service1->GetIdentifier(), service3->GetIdentifier()); | 58 EXPECT_NE(service1->GetIdentifier(), service3->GetIdentifier()); |
59 EXPECT_NE(service1->GetIdentifier(), service4->GetIdentifier()); | 59 EXPECT_NE(service1->GetIdentifier(), service4->GetIdentifier()); |
60 | 60 |
61 EXPECT_NE(service2->GetIdentifier(), service3->GetIdentifier()); | 61 EXPECT_NE(service2->GetIdentifier(), service3->GetIdentifier()); |
62 EXPECT_NE(service2->GetIdentifier(), service4->GetIdentifier()); | 62 EXPECT_NE(service2->GetIdentifier(), service4->GetIdentifier()); |
63 | 63 |
64 EXPECT_NE(service3->GetIdentifier(), service4->GetIdentifier()); | 64 EXPECT_NE(service3->GetIdentifier(), service4->GetIdentifier()); |
65 } | 65 } |
66 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) | 66 #endif // defined(OS_ANDROID) || defined(OS_WIN) || defined(OS_MACOSX) |
67 | 67 |
68 #if defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) | 68 #if defined(OS_ANDROID) || defined(OS_WIN) || defined(OS_MACOSX) |
69 TEST_F(BluetoothRemoteGattServiceTest, GetUUID) { | 69 TEST_F(BluetoothRemoteGattServiceTest, GetUUID) { |
70 if (!PlatformSupportsLowEnergy()) { | 70 if (!PlatformSupportsLowEnergy()) { |
71 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | 71 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
72 return; | 72 return; |
73 } | 73 } |
74 InitWithFakeAdapter(); | 74 InitWithFakeAdapter(); |
75 StartLowEnergyDiscoverySession(); | 75 StartLowEnergyDiscoverySession(); |
76 BluetoothDevice* device = SimulateLowEnergyDevice(3); | 76 BluetoothDevice* device = SimulateLowEnergyDevice(3); |
77 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 77 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
78 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 78 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
79 SimulateGattConnection(device); | 79 SimulateGattConnection(device); |
80 | 80 |
81 // Create multiple instances with the same UUID. | 81 // Create multiple instances with the same UUID. |
82 BluetoothUUID uuid("00000000-0000-1000-8000-00805f9b34fb"); | 82 BluetoothUUID uuid("00000000-0000-1000-8000-00805f9b34fb"); |
83 std::vector<std::string> services; | 83 std::vector<std::string> services; |
84 services.push_back(uuid.canonical_value()); | 84 services.push_back(uuid.canonical_value()); |
85 services.push_back(uuid.canonical_value()); | 85 services.push_back(uuid.canonical_value()); |
86 SimulateGattServicesDiscovered(device, services); | 86 SimulateGattServicesDiscovered(device, services); |
87 | 87 |
88 // Each has the same UUID. | 88 // Each has the same UUID. |
89 EXPECT_EQ(uuid, device->GetGattServices()[0]->GetUUID()); | 89 EXPECT_EQ(uuid, device->GetGattServices()[0]->GetUUID()); |
90 EXPECT_EQ(uuid, device->GetGattServices()[1]->GetUUID()); | 90 EXPECT_EQ(uuid, device->GetGattServices()[1]->GetUUID()); |
91 } | 91 } |
92 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) | 92 #endif // defined(OS_ANDROID) || defined(OS_WIN) || defined(OS_MACOSX) |
93 | 93 |
94 #if defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) | 94 #if defined(OS_ANDROID) || defined(OS_WIN) |
95 TEST_F(BluetoothRemoteGattServiceTest, GetCharacteristics_FindNone) { | 95 TEST_F(BluetoothRemoteGattServiceTest, GetCharacteristics_FindNone) { |
96 if (!PlatformSupportsLowEnergy()) { | 96 if (!PlatformSupportsLowEnergy()) { |
97 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | 97 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
98 return; | 98 return; |
99 } | 99 } |
100 InitWithFakeAdapter(); | 100 InitWithFakeAdapter(); |
101 StartLowEnergyDiscoverySession(); | 101 StartLowEnergyDiscoverySession(); |
102 BluetoothDevice* device = SimulateLowEnergyDevice(3); | 102 BluetoothDevice* device = SimulateLowEnergyDevice(3); |
103 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 103 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
104 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 104 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
105 SimulateGattConnection(device); | 105 SimulateGattConnection(device); |
106 | 106 |
107 // Simulate a service, with no Characteristics: | 107 // Simulate a service, with no Characteristics: |
108 std::vector<std::string> services; | 108 std::vector<std::string> services; |
109 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); | 109 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); |
110 SimulateGattServicesDiscovered(device, services); | 110 SimulateGattServicesDiscovered(device, services); |
111 BluetoothRemoteGattService* service = device->GetGattServices()[0]; | 111 BluetoothRemoteGattService* service = device->GetGattServices()[0]; |
112 | 112 |
113 EXPECT_EQ(0u, service->GetCharacteristics().size()); | 113 EXPECT_EQ(0u, service->GetCharacteristics().size()); |
114 } | 114 } |
115 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) | 115 #endif // defined(OS_ANDROID) || defined(OS_WIN) |
116 | 116 |
117 #if defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) | 117 #if defined(OS_ANDROID) || defined(OS_WIN) |
118 TEST_F(BluetoothRemoteGattServiceTest, | 118 TEST_F(BluetoothRemoteGattServiceTest, |
119 GetCharacteristics_and_GetCharacteristic) { | 119 GetCharacteristics_and_GetCharacteristic) { |
120 if (!PlatformSupportsLowEnergy()) { | |
121 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | |
122 return; | |
123 } | |
124 InitWithFakeAdapter(); | 120 InitWithFakeAdapter(); |
125 StartLowEnergyDiscoverySession(); | 121 StartLowEnergyDiscoverySession(); |
126 BluetoothDevice* device = SimulateLowEnergyDevice(3); | 122 BluetoothDevice* device = SimulateLowEnergyDevice(3); |
127 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 123 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
128 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 124 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
129 SimulateGattConnection(device); | 125 SimulateGattConnection(device); |
130 | 126 |
131 // Simulate a service, with several Characteristics: | 127 // Simulate a service, with several Characteristics: |
132 std::vector<std::string> services; | 128 std::vector<std::string> services; |
133 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); | 129 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); |
(...skipping 24 matching lines...) Expand all Loading... |
158 EXPECT_EQ(char_uuid3, service->GetCharacteristic(char_id3)->GetUUID()); | 154 EXPECT_EQ(char_uuid3, service->GetCharacteristic(char_id3)->GetUUID()); |
159 EXPECT_EQ(char_uuid4, service->GetCharacteristic(char_id4)->GetUUID()); | 155 EXPECT_EQ(char_uuid4, service->GetCharacteristic(char_id4)->GetUUID()); |
160 | 156 |
161 // GetCharacteristics & GetCharacteristic return the same object for the same | 157 // GetCharacteristics & GetCharacteristic return the same object for the same |
162 // ID: | 158 // ID: |
163 EXPECT_EQ(service->GetCharacteristics()[0], | 159 EXPECT_EQ(service->GetCharacteristics()[0], |
164 service->GetCharacteristic(char_id1)); | 160 service->GetCharacteristic(char_id1)); |
165 EXPECT_EQ(service->GetCharacteristic(char_id1), | 161 EXPECT_EQ(service->GetCharacteristic(char_id1), |
166 service->GetCharacteristic(char_id1)); | 162 service->GetCharacteristic(char_id1)); |
167 } | 163 } |
168 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) | 164 #endif // defined(OS_ANDROID) || defined(OS_WIN) |
169 | 165 |
170 #if defined(OS_MACOSX) || defined(OS_WIN) | 166 #if defined(OS_WIN) |
171 TEST_F(BluetoothRemoteGattServiceTest, | 167 TEST_F(BluetoothRemoteGattServiceTest, |
172 GetCharacteristic_CharacteristicRemoved) { | 168 GetCharacteristic_CharacteristicRemoved) { |
173 if (!PlatformSupportsLowEnergy()) { | |
174 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | |
175 return; | |
176 } | |
177 InitWithFakeAdapter(); | 169 InitWithFakeAdapter(); |
178 StartLowEnergyDiscoverySession(); | 170 StartLowEnergyDiscoverySession(); |
179 BluetoothDevice* device = SimulateLowEnergyDevice(3); | 171 BluetoothDevice* device = SimulateLowEnergyDevice(3); |
180 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 172 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
181 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 173 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
182 SimulateGattConnection(device); | 174 SimulateGattConnection(device); |
183 | 175 |
184 TestBluetoothAdapterObserver observer(adapter_); | 176 TestBluetoothAdapterObserver observer(adapter_); |
185 | 177 |
186 // Simulate a service, with several Characteristics: | 178 // Simulate a service, with several Characteristics: |
(...skipping 30 matching lines...) Expand all Loading... |
217 EXPECT_EQ(3, observer.gatt_characteristic_removed_count()); | 209 EXPECT_EQ(3, observer.gatt_characteristic_removed_count()); |
218 EXPECT_FALSE(service->GetCharacteristic(removed_char)); | 210 EXPECT_FALSE(service->GetCharacteristic(removed_char)); |
219 EXPECT_EQ(1u, service->GetCharacteristics().size()); | 211 EXPECT_EQ(1u, service->GetCharacteristics().size()); |
220 removed_char = service->GetCharacteristics()[0]->GetIdentifier(); | 212 removed_char = service->GetCharacteristics()[0]->GetIdentifier(); |
221 SimulateGattCharacteristicRemoved(service, | 213 SimulateGattCharacteristicRemoved(service, |
222 service->GetCharacteristic(removed_char)); | 214 service->GetCharacteristic(removed_char)); |
223 EXPECT_EQ(4, observer.gatt_characteristic_removed_count()); | 215 EXPECT_EQ(4, observer.gatt_characteristic_removed_count()); |
224 EXPECT_FALSE(service->GetCharacteristic(removed_char)); | 216 EXPECT_FALSE(service->GetCharacteristic(removed_char)); |
225 EXPECT_EQ(0u, service->GetCharacteristics().size()); | 217 EXPECT_EQ(0u, service->GetCharacteristics().size()); |
226 | 218 |
227 #if defined(OS_MACOSX) | |
228 // SimulateGattServicesDiscovered | |
229 // 4 * SimulateGattCharacteristic | |
230 // 4 * SimulateGattCharacteristicRemoved | |
231 EXPECT_EQ(9, observer.gatt_service_changed_count()); | |
232 #else // defined(OS_MACOSX) | |
233 EXPECT_EQ(4, observer.gatt_service_changed_count()); | 219 EXPECT_EQ(4, observer.gatt_service_changed_count()); |
234 #endif // defined(OS_MACOSX) | |
235 } | 220 } |
236 #endif // defined(OS_MACOSX) || defined(OS_WIN) | 221 #endif // defined(OS_WIN) |
237 | 222 |
238 #if defined(OS_WIN) || defined(OS_MACOSX) | 223 #if defined(OS_WIN) || defined(OS_MACOSX) |
239 TEST_F(BluetoothRemoteGattServiceTest, SimulateGattServiceRemove) { | 224 TEST_F(BluetoothRemoteGattServiceTest, SimulateGattServiceRemove) { |
240 if (!PlatformSupportsLowEnergy()) { | 225 if (!PlatformSupportsLowEnergy()) { |
241 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | 226 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
242 return; | 227 return; |
243 } | 228 } |
244 InitWithFakeAdapter(); | 229 InitWithFakeAdapter(); |
245 StartLowEnergyDiscoverySession(); | 230 StartLowEnergyDiscoverySession(); |
246 BluetoothDevice* device = SimulateLowEnergyDevice(3); | 231 BluetoothDevice* device = SimulateLowEnergyDevice(3); |
(...skipping 16 matching lines...) Expand all Loading... |
263 std::string removed_service = service1->GetIdentifier(); | 248 std::string removed_service = service1->GetIdentifier(); |
264 SimulateGattServiceRemoved(device->GetGattService(removed_service)); | 249 SimulateGattServiceRemoved(device->GetGattService(removed_service)); |
265 EXPECT_EQ(1, observer.gatt_service_removed_count()); | 250 EXPECT_EQ(1, observer.gatt_service_removed_count()); |
266 EXPECT_EQ(1u, device->GetGattServices().size()); | 251 EXPECT_EQ(1u, device->GetGattServices().size()); |
267 EXPECT_FALSE(device->GetGattService(removed_service)); | 252 EXPECT_FALSE(device->GetGattService(removed_service)); |
268 EXPECT_EQ(device->GetGattServices()[0], service2); | 253 EXPECT_EQ(device->GetGattServices()[0], service2); |
269 } | 254 } |
270 #endif // defined(OS_WIN) || defined(OS_MACOSX) | 255 #endif // defined(OS_WIN) || defined(OS_MACOSX) |
271 | 256 |
272 } // namespace device | 257 } // namespace device |
OLD | NEW |