| 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) | 170 #if defined(OS_MACOSX) || defined(OS_CHROMEOS) || defined(OS_LINUX) |
| 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. |
| 179 StartLowEnergyDiscoverySession(); | 183 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()); |
| 180 | 190 |
| 181 // Receive Advertisement with service data. | 191 // Receive Advertisement with service data. |
| 182 BluetoothDevice* device = SimulateLowEnergyDevice(1); | 192 BluetoothDevice* device2 = SimulateLowEnergyDevice(1); |
| 183 | 193 |
| 184 EXPECT_EQ(ServiceDataMap({{BluetoothUUID(kTestUUIDHeartRate), {1}}}), | 194 EXPECT_EQ(ServiceDataMap({{BluetoothUUID(kTestUUIDHeartRate), {1}}}), |
| 185 device->GetServiceData()); | 195 device2->GetServiceData()); |
| 186 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDHeartRate)}), | 196 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDHeartRate)}), |
| 187 device->GetServiceDataUUIDs()); | 197 device2->GetServiceDataUUIDs()); |
| 188 EXPECT_EQ(std::vector<uint8_t>({1}), | 198 EXPECT_EQ(std::vector<uint8_t>({1}), |
| 189 *device->GetServiceDataForUUID(BluetoothUUID(kTestUUIDHeartRate))); | 199 *device2->GetServiceDataForUUID(BluetoothUUID(kTestUUIDHeartRate))); |
| 190 | 200 |
| 191 // Receive Advertisement with no service data. | 201 // Receive Advertisement with no service data. |
| 192 SimulateLowEnergyDevice(3); | 202 SimulateLowEnergyDevice(3); |
| 193 | 203 |
| 194 EXPECT_TRUE(device->GetServiceData().empty()); | 204 // TODO(crbug.com/707039): Remove #if once the BlueZ caching behavior is |
| 195 EXPECT_TRUE(device->GetServiceDataUUIDs().empty()); | 205 // changed. |
| 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()); |
| 196 EXPECT_EQ(nullptr, | 219 EXPECT_EQ(nullptr, |
| 197 device->GetServiceDataForUUID(BluetoothUUID(kTestUUIDHeartRate))); | 220 device2->GetServiceDataForUUID(BluetoothUUID(kTestUUIDHeartRate))); |
| 221 #endif |
| 198 | 222 |
| 199 // Receive Advertisement with new service data. | 223 // Receive Advertisement with new service data. |
| 200 SimulateLowEnergyDevice(2); | 224 SimulateLowEnergyDevice(2); |
| 201 | 225 |
| 202 EXPECT_EQ(ServiceDataMap({{BluetoothUUID(kTestUUIDHeartRate), {2}}, | 226 EXPECT_EQ(ServiceDataMap( |
| 203 {BluetoothUUID(kTestUUIDImmediateAlert), {0}}}), | 227 {{BluetoothUUID(kTestUUIDHeartRate), std::vector<uint8_t>({})}, |
| 204 device->GetServiceData()); | 228 {BluetoothUUID(kTestUUIDImmediateAlert), {0, 2}}}), |
| 229 device2->GetServiceData()); |
| 205 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDHeartRate), | 230 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDHeartRate), |
| 206 BluetoothUUID(kTestUUIDImmediateAlert)}), | 231 BluetoothUUID(kTestUUIDImmediateAlert)}), |
| 207 device->GetServiceDataUUIDs()); | 232 device2->GetServiceDataUUIDs()); |
| 208 EXPECT_EQ(std::vector<uint8_t>({2}), | 233 EXPECT_EQ(std::vector<uint8_t>({}), |
| 209 *device->GetServiceDataForUUID(BluetoothUUID(kTestUUIDHeartRate))); | 234 *device2->GetServiceDataForUUID(BluetoothUUID(kTestUUIDHeartRate))); |
| 210 EXPECT_EQ( | 235 EXPECT_EQ( |
| 211 std::vector<uint8_t>({0}), | 236 std::vector<uint8_t>({0, 2}), |
| 212 *device->GetServiceDataForUUID(BluetoothUUID(kTestUUIDImmediateAlert))); | 237 *device2->GetServiceDataForUUID(BluetoothUUID(kTestUUIDImmediateAlert))); |
| 213 | 238 |
| 239 #if !defined(OS_LINUX) && !defined(OS_CHROMEOS) |
| 240 // TODO(crbug.com/706043): Remove #if once StartLowEnergyDiscoverySession is |
| 241 // implemented for bluez. |
| 214 // Stop discovery. | 242 // Stop discovery. |
| 215 discovery_sessions_[0]->Stop(GetCallback(Call::EXPECTED), | 243 discovery_sessions_[0]->Stop(GetCallback(Call::EXPECTED), |
| 216 GetErrorCallback(Call::NOT_EXPECTED)); | 244 GetErrorCallback(Call::NOT_EXPECTED)); |
| 217 ASSERT_FALSE(adapter_->IsDiscovering()); | 245 ASSERT_FALSE(adapter_->IsDiscovering()); |
| 218 ASSERT_FALSE(discovery_sessions_[0]->IsActive()); | 246 ASSERT_FALSE(discovery_sessions_[0]->IsActive()); |
| 219 | 247 |
| 220 EXPECT_TRUE(device->GetServiceData().empty()); | 248 EXPECT_TRUE(device2->GetServiceData().empty()); |
| 221 EXPECT_TRUE(device->GetServiceDataUUIDs().empty()); | 249 EXPECT_TRUE(device2->GetServiceDataUUIDs().empty()); |
| 222 EXPECT_EQ(nullptr, | 250 EXPECT_EQ(nullptr, |
| 223 device->GetServiceDataForUUID(BluetoothUUID(kTestUUIDHeartRate))); | 251 device2->GetServiceDataForUUID(BluetoothUUID(kTestUUIDHeartRate))); |
| 224 EXPECT_EQ(nullptr, device->GetServiceDataForUUID( | 252 EXPECT_EQ(nullptr, device2->GetServiceDataForUUID( |
| 225 BluetoothUUID(kTestUUIDImmediateAlert))); | 253 BluetoothUUID(kTestUUIDImmediateAlert))); |
| 254 #endif // !defined(OS_LINUX) && !defined(OS_CHROMEOS) |
| 226 } | 255 } |
| 227 #endif // defined(OS_MACOSX) | 256 #endif // defined(OS_MACOSX) || defined(OS_CHROMEOS) || defined(OS_LINUX) |
| 228 | 257 |
| 229 #if defined(OS_ANDROID) || defined(OS_MACOSX) | 258 #if defined(OS_ANDROID) || defined(OS_MACOSX) |
| 230 // Tests that the Advertisement Data fields are correctly updated during | 259 // Tests that the Advertisement Data fields are correctly updated during |
| 231 // discovery. | 260 // discovery. |
| 232 TEST_F(BluetoothTest, AdvertisementData_Discovery) { | 261 TEST_F(BluetoothTest, AdvertisementData_Discovery) { |
| 233 if (!PlatformSupportsLowEnergy()) { | 262 if (!PlatformSupportsLowEnergy()) { |
| 234 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | 263 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
| 235 return; | 264 return; |
| 236 } | 265 } |
| 237 InitWithFakeAdapter(); | 266 InitWithFakeAdapter(); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 287 SimulateLowEnergyDevice(2); | 316 SimulateLowEnergyDevice(2); |
| 288 EXPECT_EQ(2, observer.device_changed_count()); | 317 EXPECT_EQ(2, observer.device_changed_count()); |
| 289 | 318 |
| 290 EXPECT_EQ(ToInt8(TestRSSI::LOWER), device->GetInquiryRSSI().value()); | 319 EXPECT_EQ(ToInt8(TestRSSI::LOWER), device->GetInquiryRSSI().value()); |
| 291 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDImmediateAlert), | 320 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDImmediateAlert), |
| 292 BluetoothUUID(kTestUUIDLinkLoss)}), | 321 BluetoothUUID(kTestUUIDLinkLoss)}), |
| 293 device->GetUUIDs()); | 322 device->GetUUIDs()); |
| 294 #if defined(OS_MACOSX) | 323 #if defined(OS_MACOSX) |
| 295 // TODO(ortuno): Enable on Android once it supports Service Data. | 324 // TODO(ortuno): Enable on Android once it supports Service Data. |
| 296 // http://crbug.com/639408 | 325 // http://crbug.com/639408 |
| 297 EXPECT_EQ(ServiceDataMap({{BluetoothUUID(kTestUUIDHeartRate), {2}}, | 326 EXPECT_EQ(ServiceDataMap( |
| 298 {BluetoothUUID(kTestUUIDImmediateAlert), {0}}}), | 327 {{BluetoothUUID(kTestUUIDHeartRate), std::vector<uint8_t>({})}, |
| 328 {BluetoothUUID(kTestUUIDImmediateAlert), {0, 2}}}), |
| 299 device->GetServiceData()); | 329 device->GetServiceData()); |
| 300 #endif // defined(OS_MACOSX) | 330 #endif // defined(OS_MACOSX) |
| 301 EXPECT_EQ(ToInt8(TestTxPower::LOWER), device->GetInquiryTxPower().value()); | 331 EXPECT_EQ(ToInt8(TestTxPower::LOWER), device->GetInquiryTxPower().value()); |
| 302 | 332 |
| 303 // Stop discovery session, should notify of device changed. | 333 // Stop discovery session, should notify of device changed. |
| 304 // - GetInquiryRSSI: Should return nullopt because we are no longer | 334 // - GetInquiryRSSI: Should return nullopt because we are no longer |
| 305 // discovering. | 335 // discovering. |
| 306 // - GetUUIDs: Should not return any UUIDs. | 336 // - GetUUIDs: Should not return any UUIDs. |
| 307 // - GetServiceData: Should return empty map. | 337 // - GetServiceData: Should return empty map. |
| 308 // - GetInquiryTxPower: Should return nullopt because we are no longer | 338 // - GetInquiryTxPower: Should return nullopt because we are no longer |
| (...skipping 295 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 604 | 634 |
| 605 EXPECT_EQ(3, observer.device_changed_count()); | 635 EXPECT_EQ(3, observer.device_changed_count()); |
| 606 EXPECT_EQ(ToInt8(TestRSSI::LOWER), device->GetInquiryRSSI().value()); | 636 EXPECT_EQ(ToInt8(TestRSSI::LOWER), device->GetInquiryRSSI().value()); |
| 607 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDLinkLoss), | 637 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDLinkLoss), |
| 608 BluetoothUUID(kTestUUIDImmediateAlert), | 638 BluetoothUUID(kTestUUIDImmediateAlert), |
| 609 BluetoothUUID(kTestUUIDHeartRate)}), | 639 BluetoothUUID(kTestUUIDHeartRate)}), |
| 610 device->GetUUIDs()); | 640 device->GetUUIDs()); |
| 611 #if defined(OS_MACOSX) | 641 #if defined(OS_MACOSX) |
| 612 // TODO(ortuno): Enable on Android once it supports Service Data. | 642 // TODO(ortuno): Enable on Android once it supports Service Data. |
| 613 // http://crbug.com/639408 | 643 // http://crbug.com/639408 |
| 614 EXPECT_EQ(ServiceDataMap({{BluetoothUUID(kTestUUIDHeartRate), {2}}, | 644 EXPECT_EQ(ServiceDataMap( |
| 615 {BluetoothUUID(kTestUUIDImmediateAlert), {0}}}), | 645 {{BluetoothUUID(kTestUUIDHeartRate), std::vector<uint8_t>({})}, |
| 646 {BluetoothUUID(kTestUUIDImmediateAlert), {0, 2}}}), |
| 616 device->GetServiceData()); | 647 device->GetServiceData()); |
| 617 #endif // defined(OS_MACOSX) | 648 #endif // defined(OS_MACOSX) |
| 618 EXPECT_EQ(ToInt8(TestTxPower::LOWER), device->GetInquiryTxPower().value()); | 649 EXPECT_EQ(ToInt8(TestTxPower::LOWER), device->GetInquiryTxPower().value()); |
| 619 | 650 |
| 620 // Stop discovery session, should notify of device changed. | 651 // Stop discovery session, should notify of device changed. |
| 621 // - GetInquiryRSSI: Should return nullopt because we are no longer | 652 // - GetInquiryRSSI: Should return nullopt because we are no longer |
| 622 // discovering. | 653 // discovering. |
| 623 // - GetUUIDs: Should only return Service UUIDs. | 654 // - GetUUIDs: Should only return Service UUIDs. |
| 624 // - GetServiceData: Should return an empty map since we are no longer | 655 // - GetServiceData: Should return an empty map since we are no longer |
| 625 // discovering. | 656 // discovering. |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 710 device = SimulateLowEnergyDevice(2); | 741 device = SimulateLowEnergyDevice(2); |
| 711 | 742 |
| 712 EXPECT_EQ(1, observer.device_changed_count()); | 743 EXPECT_EQ(1, observer.device_changed_count()); |
| 713 EXPECT_EQ(ToInt8(TestRSSI::LOWER), device->GetInquiryRSSI().value()); | 744 EXPECT_EQ(ToInt8(TestRSSI::LOWER), device->GetInquiryRSSI().value()); |
| 714 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDLinkLoss), | 745 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDLinkLoss), |
| 715 BluetoothUUID(kTestUUIDImmediateAlert)}), | 746 BluetoothUUID(kTestUUIDImmediateAlert)}), |
| 716 device->GetUUIDs()); | 747 device->GetUUIDs()); |
| 717 #if defined(OS_MACOSX) | 748 #if defined(OS_MACOSX) |
| 718 // TODO(ortuno): Enable on Android once it supports Service Data. | 749 // TODO(ortuno): Enable on Android once it supports Service Data. |
| 719 // http://crbug.com/639408 | 750 // http://crbug.com/639408 |
| 720 EXPECT_EQ(ServiceDataMap({{BluetoothUUID(kTestUUIDHeartRate), {2}}, | 751 EXPECT_EQ(ServiceDataMap( |
| 721 {BluetoothUUID(kTestUUIDImmediateAlert), {0}}}), | 752 {{BluetoothUUID(kTestUUIDHeartRate), std::vector<uint8_t>({})}, |
| 753 {BluetoothUUID(kTestUUIDImmediateAlert), {0, 2}}}), |
| 722 device->GetServiceData()); | 754 device->GetServiceData()); |
| 723 #endif // defined(OS_MACOSX) | 755 #endif // defined(OS_MACOSX) |
| 724 EXPECT_EQ(ToInt8(TestTxPower::LOWER), device->GetInquiryTxPower().value()); | 756 EXPECT_EQ(ToInt8(TestTxPower::LOWER), device->GetInquiryTxPower().value()); |
| 725 | 757 |
| 726 // Discover Services, should notify of device changed. | 758 // Discover Services, should notify of device changed. |
| 727 // - GetUUIDs: Should return Advertised UUIDs and Service UUIDs. | 759 // - GetUUIDs: Should return Advertised UUIDs and Service UUIDs. |
| 728 std::vector<std::string> services; | 760 std::vector<std::string> services; |
| 729 services.push_back(kTestUUIDHeartRate); | 761 services.push_back(kTestUUIDHeartRate); |
| 730 SimulateGattServicesDiscovered(device, services); | 762 SimulateGattServicesDiscovered(device, services); |
| 731 base::RunLoop().RunUntilIdle(); | 763 base::RunLoop().RunUntilIdle(); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 749 | 781 |
| 750 EXPECT_EQ(3, observer.device_changed_count()); | 782 EXPECT_EQ(3, observer.device_changed_count()); |
| 751 EXPECT_EQ(ToInt8(TestRSSI::LOWER), device->GetInquiryRSSI().value()); | 783 EXPECT_EQ(ToInt8(TestRSSI::LOWER), device->GetInquiryRSSI().value()); |
| 752 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDLinkLoss), | 784 EXPECT_EQ(UUIDSet({BluetoothUUID(kTestUUIDLinkLoss), |
| 753 BluetoothUUID(kTestUUIDImmediateAlert)}), | 785 BluetoothUUID(kTestUUIDImmediateAlert)}), |
| 754 device->GetUUIDs()); | 786 device->GetUUIDs()); |
| 755 | 787 |
| 756 #if defined(OS_MACOSX) | 788 #if defined(OS_MACOSX) |
| 757 // TODO(ortuno): Enable on Android once it supports Service Data. | 789 // TODO(ortuno): Enable on Android once it supports Service Data. |
| 758 // http://crbug.com/639408 | 790 // http://crbug.com/639408 |
| 759 EXPECT_EQ(ServiceDataMap({{BluetoothUUID(kTestUUIDHeartRate), {2}}, | 791 EXPECT_EQ(ServiceDataMap( |
| 760 {BluetoothUUID(kTestUUIDImmediateAlert), {0}}}), | 792 {{BluetoothUUID(kTestUUIDHeartRate), std::vector<uint8_t>({})}, |
| 793 {BluetoothUUID(kTestUUIDImmediateAlert), {0, 2}}}), |
| 761 device->GetServiceData()); | 794 device->GetServiceData()); |
| 762 #endif // defined(OS_MACOSX) | 795 #endif // defined(OS_MACOSX) |
| 763 EXPECT_EQ(ToInt8(TestTxPower::LOWER), device->GetInquiryTxPower().value()); | 796 EXPECT_EQ(ToInt8(TestTxPower::LOWER), device->GetInquiryTxPower().value()); |
| 764 | 797 |
| 765 // Receive Advertisement with new UUIDs, should notify of device changed. | 798 // Receive Advertisement with new UUIDs, should notify of device changed. |
| 766 // - GetInquiryRSSI: Should return last packet's rssi. | 799 // - GetInquiryRSSI: Should return last packet's rssi. |
| 767 // - GetUUIDs: Should return only new Advertised UUIDs. | 800 // - GetUUIDs: Should return only new Advertised UUIDs. |
| 768 // - GetServiceData: Should return only new advertised Service Data. | 801 // - GetServiceData: Should return only new advertised Service Data. |
| 769 // - GetInquiryTxPower: Should return the last packet's advertised Tx Power. | 802 // - GetInquiryTxPower: Should return the last packet's advertised Tx Power. |
| 770 device = SimulateLowEnergyDevice(1); | 803 device = SimulateLowEnergyDevice(1); |
| (...skipping 842 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1613 .empty()); | 1646 .empty()); |
| 1614 EXPECT_TRUE(device_ | 1647 EXPECT_TRUE(device_ |
| 1615 ->GetCharacteristicsByUUID( | 1648 ->GetCharacteristicsByUUID( |
| 1616 service_instance_id2, | 1649 service_instance_id2, |
| 1617 BluetoothUUID(characteristic_uuid_not_exist_in_setup)) | 1650 BluetoothUUID(characteristic_uuid_not_exist_in_setup)) |
| 1618 .empty()); | 1651 .empty()); |
| 1619 } | 1652 } |
| 1620 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) | 1653 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) |
| 1621 | 1654 |
| 1622 } // namespace device | 1655 } // namespace device |
| OLD | NEW |