OLD | NEW |
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 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 return; | 160 return; |
161 } | 161 } |
162 InitWithFakeAdapter(); | 162 InitWithFakeAdapter(); |
163 StartLowEnergyDiscoverySession(); | 163 StartLowEnergyDiscoverySession(); |
164 BluetoothDevice* device = SimulateLowEnergyDevice(3); | 164 BluetoothDevice* device = SimulateLowEnergyDevice(3); |
165 ASSERT_TRUE(device); | 165 ASSERT_TRUE(device); |
166 UUIDSet uuids = device->GetUUIDs(); | 166 UUIDSet uuids = device->GetUUIDs(); |
167 EXPECT_EQ(0u, uuids.size()); | 167 EXPECT_EQ(0u, uuids.size()); |
168 } | 168 } |
169 | 169 |
170 #if defined(OS_MACOSX) || defined(OS_CHROMEOS) || defined(OS_LINUX) | 170 #if defined(OS_MACOSX) |
171 // TODO(ortuno): Enable on Android once it supports Service Data. | 171 // TODO(ortuno): Enable on Android once it supports Service Data. |
172 // http://crbug.com/639408 | 172 // http://crbug.com/639408 |
173 TEST_F(BluetoothTest, GetServiceDataUUIDs_GetServiceDataForUUID) { | 173 TEST_F(BluetoothTest, GetServiceDataUUIDs_GetServiceDataForUUID) { |
174 if (!PlatformSupportsLowEnergy()) { | 174 if (!PlatformSupportsLowEnergy()) { |
175 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | 175 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
176 return; | 176 return; |
177 } | 177 } |
178 InitWithFakeAdapter(); | 178 InitWithFakeAdapter(); |
179 | |
180 #if !defined(OS_LINUX) && !defined(OS_CHROMEOS) | |
181 // TODO(crbug.com/706043): Remove #if once StartLowEnergyDiscoverySession is | |
182 // implemented for bluez. | |
183 StartLowEnergyDiscoverySession(); | 179 StartLowEnergyDiscoverySession(); |
184 #endif // !defined(OS_LINUX) && !defined(OS_CHROMEOS) | |
185 | |
186 // Receive Advertisement with empty service data. | |
187 BluetoothDevice* device1 = SimulateLowEnergyDevice(4); | |
188 EXPECT_TRUE(device1->GetServiceData().empty()); | |
189 EXPECT_TRUE(device1->GetServiceDataUUIDs().empty()); | |
190 | 180 |
191 // Receive Advertisement with service data. | 181 // Receive Advertisement with service data. |
192 BluetoothDevice* device2 = SimulateLowEnergyDevice(1); | 182 BluetoothDevice* device = SimulateLowEnergyDevice(1); |
193 | 183 |
194 EXPECT_EQ(ServiceDataMap({{BluetoothUUID(kTestUUIDHeartRate), {1}}}), | 184 EXPECT_EQ(ServiceDataMap({{BluetoothUUID(kTestUUIDHeartRate), {1}}}), |
195 device2->GetServiceData()); | 185 device->GetServiceData()); |
196 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDHeartRate)}), | 186 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDHeartRate)}), |
197 device2->GetServiceDataUUIDs()); | 187 device->GetServiceDataUUIDs()); |
198 EXPECT_EQ(std::vector<uint8_t>({1}), | 188 EXPECT_EQ(std::vector<uint8_t>({1}), |
199 *device2->GetServiceDataForUUID(BluetoothUUID(kTestUUIDHeartRate))); | 189 *device->GetServiceDataForUUID(BluetoothUUID(kTestUUIDHeartRate))); |
200 | 190 |
201 // Receive Advertisement with no service data. | 191 // Receive Advertisement with no service data. |
202 SimulateLowEnergyDevice(3); | 192 SimulateLowEnergyDevice(3); |
203 | 193 |
204 // TODO(crbug.com/707039): Remove #if once the BlueZ caching behavior is | 194 EXPECT_TRUE(device->GetServiceData().empty()); |
205 // changed. | 195 EXPECT_TRUE(device->GetServiceDataUUIDs().empty()); |
206 #if defined(OS_CHROMEOS) || defined(OS_LINUX) | |
207 // On ChromeOS and Linux, BlueZ persists all service data meaning if | |
208 // a device stops advertising service data for a UUID, BlueZ will | |
209 // still return the cached value for that UUID. | |
210 EXPECT_EQ(ServiceDataMap({{BluetoothUUID(kTestUUIDHeartRate), {1}}}), | |
211 device2->GetServiceData()); | |
212 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDHeartRate)}), | |
213 device2->GetServiceDataUUIDs()); | |
214 EXPECT_EQ(std::vector<uint8_t>({1}), | |
215 *device2->GetServiceDataForUUID(BluetoothUUID(kTestUUIDHeartRate))); | |
216 #else | |
217 EXPECT_TRUE(device2->GetServiceData().empty()); | |
218 EXPECT_TRUE(device2->GetServiceDataUUIDs().empty()); | |
219 EXPECT_EQ(nullptr, | 196 EXPECT_EQ(nullptr, |
220 device2->GetServiceDataForUUID(BluetoothUUID(kTestUUIDHeartRate))); | 197 device->GetServiceDataForUUID(BluetoothUUID(kTestUUIDHeartRate))); |
221 #endif | |
222 | 198 |
223 // Receive Advertisement with new service data. | 199 // Receive Advertisement with new service data. |
224 SimulateLowEnergyDevice(2); | 200 SimulateLowEnergyDevice(2); |
225 | 201 |
226 EXPECT_EQ(ServiceDataMap( | 202 EXPECT_EQ(ServiceDataMap({{BluetoothUUID(kTestUUIDHeartRate), {2}}, |
227 {{BluetoothUUID(kTestUUIDHeartRate), std::vector<uint8_t>({})}, | 203 {BluetoothUUID(kTestUUIDImmediateAlert), {0}}}), |
228 {BluetoothUUID(kTestUUIDImmediateAlert), {0, 2}}}), | 204 device->GetServiceData()); |
229 device2->GetServiceData()); | |
230 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDHeartRate), | 205 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDHeartRate), |
231 BluetoothUUID(kTestUUIDImmediateAlert)}), | 206 BluetoothUUID(kTestUUIDImmediateAlert)}), |
232 device2->GetServiceDataUUIDs()); | 207 device->GetServiceDataUUIDs()); |
233 EXPECT_EQ(std::vector<uint8_t>({}), | 208 EXPECT_EQ(std::vector<uint8_t>({2}), |
234 *device2->GetServiceDataForUUID(BluetoothUUID(kTestUUIDHeartRate))); | 209 *device->GetServiceDataForUUID(BluetoothUUID(kTestUUIDHeartRate))); |
235 EXPECT_EQ( | 210 EXPECT_EQ( |
236 std::vector<uint8_t>({0, 2}), | 211 std::vector<uint8_t>({0}), |
237 *device2->GetServiceDataForUUID(BluetoothUUID(kTestUUIDImmediateAlert))); | 212 *device->GetServiceDataForUUID(BluetoothUUID(kTestUUIDImmediateAlert))); |
238 | 213 |
239 #if !defined(OS_LINUX) && !defined(OS_CHROMEOS) | |
240 // TODO(crbug.com/706043): Remove #if once StartLowEnergyDiscoverySession is | |
241 // implemented for bluez. | |
242 // Stop discovery. | 214 // Stop discovery. |
243 discovery_sessions_[0]->Stop(GetCallback(Call::EXPECTED), | 215 discovery_sessions_[0]->Stop(GetCallback(Call::EXPECTED), |
244 GetErrorCallback(Call::NOT_EXPECTED)); | 216 GetErrorCallback(Call::NOT_EXPECTED)); |
245 ASSERT_FALSE(adapter_->IsDiscovering()); | 217 ASSERT_FALSE(adapter_->IsDiscovering()); |
246 ASSERT_FALSE(discovery_sessions_[0]->IsActive()); | 218 ASSERT_FALSE(discovery_sessions_[0]->IsActive()); |
247 | 219 |
248 EXPECT_TRUE(device2->GetServiceData().empty()); | 220 EXPECT_TRUE(device->GetServiceData().empty()); |
249 EXPECT_TRUE(device2->GetServiceDataUUIDs().empty()); | 221 EXPECT_TRUE(device->GetServiceDataUUIDs().empty()); |
250 EXPECT_EQ(nullptr, | 222 EXPECT_EQ(nullptr, |
251 device2->GetServiceDataForUUID(BluetoothUUID(kTestUUIDHeartRate))); | 223 device->GetServiceDataForUUID(BluetoothUUID(kTestUUIDHeartRate))); |
252 EXPECT_EQ(nullptr, device2->GetServiceDataForUUID( | 224 EXPECT_EQ(nullptr, device->GetServiceDataForUUID( |
253 BluetoothUUID(kTestUUIDImmediateAlert))); | 225 BluetoothUUID(kTestUUIDImmediateAlert))); |
254 #endif // !defined(OS_LINUX) && !defined(OS_CHROMEOS) | |
255 } | 226 } |
256 #endif // defined(OS_MACOSX) || defined(OS_CHROMEOS) || defined(OS_LINUX) | 227 #endif // defined(OS_MACOSX) |
257 | 228 |
258 #if defined(OS_ANDROID) || defined(OS_MACOSX) | 229 #if defined(OS_ANDROID) || defined(OS_MACOSX) |
259 // Tests that the Advertisement Data fields are correctly updated during | 230 // Tests that the Advertisement Data fields are correctly updated during |
260 // discovery. | 231 // discovery. |
261 TEST_F(BluetoothTest, AdvertisementData_Discovery) { | 232 TEST_F(BluetoothTest, AdvertisementData_Discovery) { |
262 if (!PlatformSupportsLowEnergy()) { | 233 if (!PlatformSupportsLowEnergy()) { |
263 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | 234 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
264 return; | 235 return; |
265 } | 236 } |
266 InitWithFakeAdapter(); | 237 InitWithFakeAdapter(); |
(...skipping 1375 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1642 .empty()); | 1613 .empty()); |
1643 EXPECT_TRUE(device_ | 1614 EXPECT_TRUE(device_ |
1644 ->GetCharacteristicsByUUID( | 1615 ->GetCharacteristicsByUUID( |
1645 service_instance_id2, | 1616 service_instance_id2, |
1646 BluetoothUUID(characteristic_uuid_not_exist_in_setup)) | 1617 BluetoothUUID(characteristic_uuid_not_exist_in_setup)) |
1647 .empty()); | 1618 .empty()); |
1648 } | 1619 } |
1649 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) | 1620 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) |
1650 | 1621 |
1651 } // namespace device | 1622 } // namespace device |
OLD | NEW |