| 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 "content/shell/browser/layout_test/layout_test_bluetooth_adapter_provid
er.h" | 5 #include "content/shell/browser/layout_test/layout_test_bluetooth_adapter_provid
er.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 60 NiceMockBluetoothGattNotifySession; | 60 NiceMockBluetoothGattNotifySession; |
| 61 | 61 |
| 62 namespace { | 62 namespace { |
| 63 // Bluetooth UUIDs suitable to pass to BluetoothUUID(): | 63 // Bluetooth UUIDs suitable to pass to BluetoothUUID(): |
| 64 // Services: | 64 // Services: |
| 65 const char kBatteryServiceUUID[] = "180f"; | 65 const char kBatteryServiceUUID[] = "180f"; |
| 66 const char kBlacklistTestServiceUUID[] = "611c954a-263b-4f4a-aab6-01ddb953f985"; | 66 const char kBlacklistTestServiceUUID[] = "611c954a-263b-4f4a-aab6-01ddb953f985"; |
| 67 const char kDeviceInformationServiceUUID[] = "180a"; | 67 const char kDeviceInformationServiceUUID[] = "180a"; |
| 68 const char kGenericAccessServiceUUID[] = "1800"; | 68 const char kGenericAccessServiceUUID[] = "1800"; |
| 69 const char kGlucoseServiceUUID[] = "1808"; | 69 const char kGlucoseServiceUUID[] = "1808"; |
| 70 const char kHealthThermometerUUID[] = "1809"; |
| 70 const char kHeartRateServiceUUID[] = "180d"; | 71 const char kHeartRateServiceUUID[] = "180d"; |
| 71 const char kHumanInterfaceDeviceServiceUUID[] = "1812"; | 72 const char kHumanInterfaceDeviceServiceUUID[] = "1812"; |
| 72 const char kRequestDisconnectionServiceUUID[] = | 73 const char kRequestDisconnectionServiceUUID[] = |
| 73 "01d7d889-7451-419f-aeb8-d65e7b9277af"; | 74 "01d7d889-7451-419f-aeb8-d65e7b9277af"; |
| 74 const char kTxPowerServiceUUID[] = "1804"; | 75 const char kTxPowerServiceUUID[] = "1804"; |
| 75 // Characteristics: | 76 // Characteristics: |
| 76 const char kBlacklistExcludeReadsCharacteristicUUID[] = | 77 const char kBlacklistExcludeReadsCharacteristicUUID[] = |
| 77 "bad1c9a2-9a5b-4015-8b60-1579bbbf2135"; | 78 "bad1c9a2-9a5b-4015-8b60-1579bbbf2135"; |
| 78 const char kRequestDisconnectionCharacteristicUUID[] = | 79 const char kRequestDisconnectionCharacteristicUUID[] = |
| 79 "01d7d88a-7451-419f-aeb8-d65e7b9277af"; | 80 "01d7d88a-7451-419f-aeb8-d65e7b9277af"; |
| 80 const char kBodySensorLocation[] = "2a38"; | 81 const char kBodySensorLocation[] = "2a38"; |
| 81 const char kDeviceNameUUID[] = "2a00"; | 82 const char kDeviceNameUUID[] = "2a00"; |
| 83 const char kMeasurementIntervalUUID[] = "2a21"; |
| 82 const char kHeartRateMeasurementUUID[] = "2a37"; | 84 const char kHeartRateMeasurementUUID[] = "2a37"; |
| 83 const char kSerialNumberStringUUID[] = "2a25"; | 85 const char kSerialNumberStringUUID[] = "2a25"; |
| 84 const char kPeripheralPrivacyFlagUUID[] = "2a02"; | 86 const char kPeripheralPrivacyFlagUUID[] = "2a02"; |
| 85 | 87 |
| 86 // Invokes Run() on the k-th argument of the function with no arguments. | 88 // Invokes Run() on the k-th argument of the function with no arguments. |
| 87 ACTION_TEMPLATE(RunCallback, | 89 ACTION_TEMPLATE(RunCallback, |
| 88 HAS_1_TEMPLATE_PARAMS(int, k), | 90 HAS_1_TEMPLATE_PARAMS(int, k), |
| 89 AND_0_VALUE_PARAMS()) { | 91 AND_0_VALUE_PARAMS()) { |
| 90 return ::testing::get<k>(args).Run(); | 92 return ::testing::get<k>(args).Run(); |
| 91 } | 93 } |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 if (fake_adapter_name == "HeartRateAdapter") | 178 if (fake_adapter_name == "HeartRateAdapter") |
| 177 return GetHeartRateAdapter(); | 179 return GetHeartRateAdapter(); |
| 178 if (fake_adapter_name == "EmptyNameHeartRateAdapter") | 180 if (fake_adapter_name == "EmptyNameHeartRateAdapter") |
| 179 return GetEmptyNameHeartRateAdapter(); | 181 return GetEmptyNameHeartRateAdapter(); |
| 180 if (fake_adapter_name == "NoNameHeartRateAdapter") | 182 if (fake_adapter_name == "NoNameHeartRateAdapter") |
| 181 return GetNoNameHeartRateAdapter(); | 183 return GetNoNameHeartRateAdapter(); |
| 182 if (fake_adapter_name == "TwoHeartRateServicesAdapter") | 184 if (fake_adapter_name == "TwoHeartRateServicesAdapter") |
| 183 return GetTwoHeartRateServicesAdapter(); | 185 return GetTwoHeartRateServicesAdapter(); |
| 184 if (fake_adapter_name == "DisconnectingHeartRateAdapter") | 186 if (fake_adapter_name == "DisconnectingHeartRateAdapter") |
| 185 return GetDisconnectingHeartRateAdapter(); | 187 return GetDisconnectingHeartRateAdapter(); |
| 188 if (fake_adapter_name == "DisconnectingHealthThermometerAdapter") |
| 189 return GetDisconnectingHealthThermometer(); |
| 186 if (fake_adapter_name == "DisconnectingDuringServiceRetrievalAdapter") | 190 if (fake_adapter_name == "DisconnectingDuringServiceRetrievalAdapter") |
| 187 return GetServicesDiscoveredAfterReconnectionAdapter(true /* disconnect */); | 191 return GetServicesDiscoveredAfterReconnectionAdapter(true /* disconnect */); |
| 188 if (fake_adapter_name == "ServicesDiscoveredAfterReconnectionAdapter") | 192 if (fake_adapter_name == "ServicesDiscoveredAfterReconnectionAdapter") |
| 189 return GetServicesDiscoveredAfterReconnectionAdapter( | 193 return GetServicesDiscoveredAfterReconnectionAdapter( |
| 190 false /* disconnect */); | 194 false /* disconnect */); |
| 191 if (fake_adapter_name == "BlacklistTestAdapter") | 195 if (fake_adapter_name == "BlacklistTestAdapter") |
| 192 return GetBlacklistTestAdapter(); | 196 return GetBlacklistTestAdapter(); |
| 193 if (fake_adapter_name == "FailingConnectionsAdapter") | 197 if (fake_adapter_name == "FailingConnectionsAdapter") |
| 194 return GetFailingConnectionsAdapter(); | 198 return GetFailingConnectionsAdapter(); |
| 195 if (fake_adapter_name == "FailingGATTOperationsAdapter") | 199 if (fake_adapter_name == "FailingGATTOperationsAdapter") |
| (...skipping 415 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 611 device->AddMockService(GetGenericAccessService(device.get())); | 615 device->AddMockService(GetGenericAccessService(device.get())); |
| 612 device->AddMockService(GetHeartRateService(adapter.get(), device.get())); | 616 device->AddMockService(GetHeartRateService(adapter.get(), device.get())); |
| 613 | 617 |
| 614 adapter->AddMockDevice(std::move(device)); | 618 adapter->AddMockDevice(std::move(device)); |
| 615 | 619 |
| 616 return adapter; | 620 return adapter; |
| 617 } | 621 } |
| 618 | 622 |
| 619 // static | 623 // static |
| 620 scoped_refptr<NiceMockBluetoothAdapter> | 624 scoped_refptr<NiceMockBluetoothAdapter> |
| 625 LayoutTestBluetoothAdapterProvider::GetDisconnectingHealthThermometer() { |
| 626 scoped_refptr<NiceMockBluetoothAdapter> adapter(GetEmptyAdapter()); |
| 627 NiceMockBluetoothAdapter* adapter_ptr = adapter.get(); |
| 628 |
| 629 std::unique_ptr<NiceMockBluetoothDevice> device(GetConnectableDevice( |
| 630 adapter_ptr, "Disconnecting Health Thermometer", std::vector<uint8_t>({ |
| 631 BluetoothUUID(kGenericAccessServiceUUID), |
| 632 BluetoothUUID(kHealthThermometerUUID)}))); |
| 633 |
| 634 device->AddMockService(GetGenericAccessService(device.get())); |
| 635 device->AddMockService(GetDisconnectingService(adapter.get(), device.get())); |
| 636 |
| 637 std::unique_ptr<NiceMockBluetoothGattService> health_thermometer( |
| 638 GetBaseGATTService("Health Thermometer", device.get(), |
| 639 kHealthThermometerUUID)); |
| 640 |
| 641 // Measurement Interval |
| 642 std::unique_ptr<NiceMockBluetoothGattCharacteristic> measurement_interval( |
| 643 GetBaseGATTCharacteristic( |
| 644 "Measurement Interval", health_thermometer.get(), |
| 645 kMeasurementIntervalUUID, |
| 646 BluetoothRemoteGattCharacteristic::PROPERTY_READ)); |
| 647 NiceMockBluetoothGattCharacteristic* measurement_ptr = |
| 648 measurement_interval.get(); |
| 649 |
| 650 ON_CALL(*measurement_interval, ReadRemoteCharacteristic(_, _)) |
| 651 .WillByDefault(RunCallbackWithResult<0 /* success_callback */>( |
| 652 [adapter_ptr, measurement_ptr]() { |
| 653 std::vector<uint8_t> interval({1}); |
| 654 for (auto& observer : adapter_ptr->GetObservers()) { |
| 655 observer.GattCharacteristicValueChanged( |
| 656 adapter_ptr, measurement_ptr, interval); |
| 657 } |
| 658 return interval; |
| 659 })); |
| 660 // TODO(crbug.com/608538): Mock Write and StartNotifySession. |
| 661 |
| 662 health_thermometer->AddMockCharacteristic(std::move(measurement_interval)); |
| 663 device->AddMockService(std::move(health_thermometer)); |
| 664 |
| 665 adapter->AddMockDevice(std::move(device)); |
| 666 |
| 667 return adapter; |
| 668 } |
| 669 |
| 670 // static |
| 671 scoped_refptr<NiceMockBluetoothAdapter> |
| 621 LayoutTestBluetoothAdapterProvider::GetEmptyNameHeartRateAdapter() { | 672 LayoutTestBluetoothAdapterProvider::GetEmptyNameHeartRateAdapter() { |
| 622 scoped_refptr<NiceMockBluetoothAdapter> adapter(GetEmptyAdapter()); | 673 scoped_refptr<NiceMockBluetoothAdapter> adapter(GetEmptyAdapter()); |
| 623 std::unique_ptr<NiceMockBluetoothDevice> device( | 674 std::unique_ptr<NiceMockBluetoothDevice> device( |
| 624 GetHeartRateDevice(adapter.get(), /* device_name */ "")); | 675 GetHeartRateDevice(adapter.get(), /* device_name */ "")); |
| 625 | 676 |
| 626 // TODO(ortuno): Implement the rest of the service's characteristics | 677 // TODO(ortuno): Implement the rest of the service's characteristics |
| 627 // See: http://crbug.com/529975 | 678 // See: http://crbug.com/529975 |
| 628 | 679 |
| 629 device->AddMockService(GetGenericAccessService(device.get())); | 680 device->AddMockService(GetGenericAccessService(device.get())); |
| 630 device->AddMockService(GetHeartRateService(adapter.get(), device.get())); | 681 device->AddMockService(GetHeartRateService(adapter.get(), device.get())); |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 703 | 754 |
| 704 adapter->AddMockDevice(std::move(device)); | 755 adapter->AddMockDevice(std::move(device)); |
| 705 | 756 |
| 706 return adapter; | 757 return adapter; |
| 707 } | 758 } |
| 708 | 759 |
| 709 // static | 760 // static |
| 710 scoped_refptr<NiceMockBluetoothAdapter> | 761 scoped_refptr<NiceMockBluetoothAdapter> |
| 711 LayoutTestBluetoothAdapterProvider::GetDisconnectingHeartRateAdapter() { | 762 LayoutTestBluetoothAdapterProvider::GetDisconnectingHeartRateAdapter() { |
| 712 scoped_refptr<NiceMockBluetoothAdapter> adapter(GetEmptyAdapter()); | 763 scoped_refptr<NiceMockBluetoothAdapter> adapter(GetEmptyAdapter()); |
| 713 NiceMockBluetoothAdapter* adapter_ptr = adapter.get(); | |
| 714 std::unique_ptr<NiceMockBluetoothDevice> device( | 764 std::unique_ptr<NiceMockBluetoothDevice> device( |
| 715 GetHeartRateDevice(adapter.get())); | 765 GetHeartRateDevice(adapter.get())); |
| 716 NiceMockBluetoothDevice* device_ptr = device.get(); | |
| 717 | 766 |
| 718 // TODO(ortuno): Implement the rest of the service's characteristics | 767 // TODO(ortuno): Implement the rest of the service's characteristics |
| 719 // See: http://crbug.com/529975 | 768 // See: http://crbug.com/529975 |
| 720 | 769 |
| 721 device->AddMockService(GetGenericAccessService(device.get())); | 770 device->AddMockService(GetGenericAccessService(device.get())); |
| 722 device->AddMockService(GetHeartRateService(adapter.get(), device.get())); | 771 device->AddMockService(GetHeartRateService(adapter.get(), device.get())); |
| 772 device->AddMockService(GetDisconnectingService(adapter.get(), device.get())); |
| 723 | 773 |
| 724 // Set up a service and a characteristic to disconnect the device when it's | |
| 725 // written to. | |
| 726 std::unique_ptr<NiceMockBluetoothGattService> disconnection_service = | |
| 727 GetBaseGATTService("Disconnection", device_ptr, | |
| 728 kRequestDisconnectionServiceUUID); | |
| 729 std::unique_ptr<NiceMockBluetoothGattCharacteristic> | |
| 730 disconnection_characteristic(GetBaseGATTCharacteristic( | |
| 731 "Disconnection Characteristic", disconnection_service.get(), | |
| 732 kRequestDisconnectionCharacteristicUUID, | |
| 733 BluetoothRemoteGattCharacteristic::PROPERTY_WRITE_WITHOUT_RESPONSE)); | |
| 734 ON_CALL(*disconnection_characteristic, WriteRemoteCharacteristic(_, _, _)) | |
| 735 .WillByDefault(Invoke([adapter_ptr, device_ptr]( | |
| 736 const std::vector<uint8_t>& value, const base::Closure& success, | |
| 737 const BluetoothRemoteGattCharacteristic::ErrorCallback& error) { | |
| 738 device_ptr->SetConnected(false); | |
| 739 for (auto& observer : adapter_ptr->GetObservers()) | |
| 740 observer.DeviceChanged(adapter_ptr, device_ptr); | |
| 741 success.Run(); | |
| 742 })); | |
| 743 | |
| 744 disconnection_service->AddMockCharacteristic( | |
| 745 std::move(disconnection_characteristic)); | |
| 746 device->AddMockService(std::move(disconnection_service)); | |
| 747 adapter->AddMockDevice(std::move(device)); | 774 adapter->AddMockDevice(std::move(device)); |
| 748 | 775 |
| 749 return adapter; | 776 return adapter; |
| 750 } | 777 } |
| 751 | 778 |
| 752 // static | 779 // static |
| 753 scoped_refptr<NiceMockBluetoothAdapter> LayoutTestBluetoothAdapterProvider:: | 780 scoped_refptr<NiceMockBluetoothAdapter> LayoutTestBluetoothAdapterProvider:: |
| 754 GetServicesDiscoveredAfterReconnectionAdapter(bool disconnect) { | 781 GetServicesDiscoveredAfterReconnectionAdapter(bool disconnect) { |
| 755 scoped_refptr<NiceMockBluetoothAdapter> adapter(GetEmptyAdapter()); | 782 scoped_refptr<NiceMockBluetoothAdapter> adapter(GetEmptyAdapter()); |
| 756 NiceMockBluetoothAdapter* adapter_ptr = adapter.get(); | 783 NiceMockBluetoothAdapter* adapter_ptr = adapter.get(); |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 862 scoped_refptr<NiceMockBluetoothAdapter> adapter(GetEmptyAdapter()); | 889 scoped_refptr<NiceMockBluetoothAdapter> adapter(GetEmptyAdapter()); |
| 863 | 890 |
| 864 const std::string errorsServiceUUID = errorUUID(0xA0); | 891 const std::string errorsServiceUUID = errorUUID(0xA0); |
| 865 | 892 |
| 866 BluetoothDevice::UUIDList uuids; | 893 BluetoothDevice::UUIDList uuids; |
| 867 uuids.push_back(BluetoothUUID(errorsServiceUUID)); | 894 uuids.push_back(BluetoothUUID(errorsServiceUUID)); |
| 868 | 895 |
| 869 std::unique_ptr<NiceMockBluetoothDevice> device( | 896 std::unique_ptr<NiceMockBluetoothDevice> device( |
| 870 GetConnectableDevice(adapter.get(), "Errors Device", uuids)); | 897 GetConnectableDevice(adapter.get(), "Errors Device", uuids)); |
| 871 | 898 |
| 899 device->AddMockService(GetDisconnectingService(adapter.get(), device.get())); |
| 900 |
| 872 std::unique_ptr<NiceMockBluetoothGattService> service( | 901 std::unique_ptr<NiceMockBluetoothGattService> service( |
| 873 GetBaseGATTService("Errors Service", device.get(), errorsServiceUUID)); | 902 GetBaseGATTService("Errors Service", device.get(), errorsServiceUUID)); |
| 874 | 903 |
| 875 for (int error = BluetoothRemoteGattService::GATT_ERROR_UNKNOWN; | 904 for (int error = BluetoothRemoteGattService::GATT_ERROR_UNKNOWN; |
| 876 error <= BluetoothRemoteGattService::GATT_ERROR_NOT_SUPPORTED; error++) { | 905 error <= BluetoothRemoteGattService::GATT_ERROR_NOT_SUPPORTED; error++) { |
| 877 service->AddMockCharacteristic(GetErrorCharacteristic( | 906 service->AddMockCharacteristic(GetErrorCharacteristic( |
| 878 service.get(), | 907 service.get(), |
| 879 static_cast<BluetoothRemoteGattService::GattErrorCode>(error))); | 908 static_cast<BluetoothRemoteGattService::GattErrorCode>(error))); |
| 880 } | 909 } |
| 881 | 910 |
| (...skipping 355 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1237 return location; | 1266 return location; |
| 1238 })); | 1267 })); |
| 1239 | 1268 |
| 1240 heart_rate->AddMockCharacteristic(std::move(heart_rate_measurement)); | 1269 heart_rate->AddMockCharacteristic(std::move(heart_rate_measurement)); |
| 1241 heart_rate->AddMockCharacteristic(std::move(body_sensor_location_chest)); | 1270 heart_rate->AddMockCharacteristic(std::move(body_sensor_location_chest)); |
| 1242 heart_rate->AddMockCharacteristic(std::move(body_sensor_location_wrist)); | 1271 heart_rate->AddMockCharacteristic(std::move(body_sensor_location_wrist)); |
| 1243 | 1272 |
| 1244 return heart_rate; | 1273 return heart_rate; |
| 1245 } | 1274 } |
| 1246 | 1275 |
| 1276 // static |
| 1277 std::unique_ptr<NiceMockBluetoothGattService> |
| 1278 LayoutTestBluetoothAdapterProvider::GetDisconnectingService( |
| 1279 MockBluetoothAdapter* adapter, |
| 1280 MockBluetoothDevice* device) { |
| 1281 // Set up a service and a characteristic to disconnect the device when it's |
| 1282 // written to. |
| 1283 std::unique_ptr<NiceMockBluetoothGattService> disconnection_service = |
| 1284 GetBaseGATTService("Disconnection", device, |
| 1285 kRequestDisconnectionServiceUUID); |
| 1286 std::unique_ptr<NiceMockBluetoothGattCharacteristic> |
| 1287 disconnection_characteristic(GetBaseGATTCharacteristic( |
| 1288 "Disconnection Characteristic", disconnection_service.get(), |
| 1289 kRequestDisconnectionCharacteristicUUID, |
| 1290 BluetoothRemoteGattCharacteristic::PROPERTY_WRITE_WITHOUT_RESPONSE)); |
| 1291 ON_CALL(*disconnection_characteristic, WriteRemoteCharacteristic(_, _, _)) |
| 1292 .WillByDefault(Invoke([adapter, device]( |
| 1293 const std::vector<uint8_t>& value, const base::Closure& success, |
| 1294 const BluetoothRemoteGattCharacteristic::ErrorCallback& error) { |
| 1295 device->SetConnected(false); |
| 1296 for (auto& observer : adapter->GetObservers()) |
| 1297 observer.DeviceChanged(adapter, device); |
| 1298 success.Run(); |
| 1299 })); |
| 1300 |
| 1301 disconnection_service->AddMockCharacteristic( |
| 1302 std::move(disconnection_characteristic)); |
| 1303 return disconnection_service; |
| 1304 } |
| 1305 |
| 1247 // Characteristics | 1306 // Characteristics |
| 1248 | 1307 |
| 1249 // static | 1308 // static |
| 1250 std::unique_ptr<NiceMockBluetoothGattCharacteristic> | 1309 std::unique_ptr<NiceMockBluetoothGattCharacteristic> |
| 1251 LayoutTestBluetoothAdapterProvider::GetBaseGATTCharacteristic( | 1310 LayoutTestBluetoothAdapterProvider::GetBaseGATTCharacteristic( |
| 1252 const std::string& identifier, | 1311 const std::string& identifier, |
| 1253 MockBluetoothGattService* service, | 1312 MockBluetoothGattService* service, |
| 1254 const std::string& uuid, | 1313 const std::string& uuid, |
| 1255 BluetoothRemoteGattCharacteristic::Properties properties) { | 1314 BluetoothRemoteGattCharacteristic::Properties properties) { |
| 1256 std::unique_ptr<NiceMockBluetoothGattCharacteristic> characteristic( | 1315 std::unique_ptr<NiceMockBluetoothGattCharacteristic> characteristic( |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1377 return BluetoothUUID(); | 1436 return BluetoothUUID(); |
| 1378 } | 1437 } |
| 1379 | 1438 |
| 1380 // static | 1439 // static |
| 1381 std::string LayoutTestBluetoothAdapterProvider::makeMACAddress(uint64_t addr) { | 1440 std::string LayoutTestBluetoothAdapterProvider::makeMACAddress(uint64_t addr) { |
| 1382 return BluetoothDevice::CanonicalizeAddress( | 1441 return BluetoothDevice::CanonicalizeAddress( |
| 1383 base::StringPrintf("%012" PRIx64, addr)); | 1442 base::StringPrintf("%012" PRIx64, addr)); |
| 1384 } | 1443 } |
| 1385 | 1444 |
| 1386 } // namespace content | 1445 } // namespace content |
| OLD | NEW |