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 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 observer.GattServicesDiscovered(adapter, device); | 140 observer.GattServicesDiscovered(adapter, device); |
141 } | 141 } |
142 | 142 |
143 // Notifies the adapter's observers that a device has changed. | 143 // Notifies the adapter's observers that a device has changed. |
144 void NotifyDeviceChanged(MockBluetoothAdapter* adapter, | 144 void NotifyDeviceChanged(MockBluetoothAdapter* adapter, |
145 MockBluetoothDevice* device) { | 145 MockBluetoothDevice* device) { |
146 for (auto& observer : adapter->GetObservers()) | 146 for (auto& observer : adapter->GetObservers()) |
147 observer.DeviceChanged(adapter, device); | 147 observer.DeviceChanged(adapter, device); |
148 } | 148 } |
149 | 149 |
| 150 void PerformReadValue( |
| 151 MockBluetoothAdapter* adapter, |
| 152 MockBluetoothGattCharacteristic* characteristic, |
| 153 const BluetoothRemoteGattCharacteristic::ValueCallback& callback, |
| 154 const std::vector<uint8_t>& value) { |
| 155 for (auto& observer : adapter->GetObservers()) { |
| 156 observer.GattCharacteristicValueChanged(adapter, characteristic, value); |
| 157 } |
| 158 callback.Run(value); |
| 159 } |
| 160 |
150 } // namespace | 161 } // namespace |
151 | 162 |
152 namespace content { | 163 namespace content { |
153 | 164 |
154 // static | 165 // static |
155 scoped_refptr<BluetoothAdapter> | 166 scoped_refptr<BluetoothAdapter> |
156 LayoutTestBluetoothAdapterProvider::GetBluetoothAdapter( | 167 LayoutTestBluetoothAdapterProvider::GetBluetoothAdapter( |
157 const std::string& fake_adapter_name) { | 168 const std::string& fake_adapter_name) { |
158 if (fake_adapter_name == "BaseAdapter") | 169 if (fake_adapter_name == "BaseAdapter") |
159 return GetBaseAdapter(); | 170 return GetBaseAdapter(); |
(...skipping 25 matching lines...) Expand all Loading... |
185 return GetTwoHeartRateServicesAdapter(); | 196 return GetTwoHeartRateServicesAdapter(); |
186 if (fake_adapter_name == "DisconnectingHeartRateAdapter") | 197 if (fake_adapter_name == "DisconnectingHeartRateAdapter") |
187 return GetDisconnectingHeartRateAdapter(); | 198 return GetDisconnectingHeartRateAdapter(); |
188 if (fake_adapter_name == "DisconnectingHealthThermometerAdapter") | 199 if (fake_adapter_name == "DisconnectingHealthThermometerAdapter") |
189 return GetDisconnectingHealthThermometer(); | 200 return GetDisconnectingHealthThermometer(); |
190 if (fake_adapter_name == "DisconnectingDuringServiceRetrievalAdapter") | 201 if (fake_adapter_name == "DisconnectingDuringServiceRetrievalAdapter") |
191 return GetServicesDiscoveredAfterReconnectionAdapter(true /* disconnect */); | 202 return GetServicesDiscoveredAfterReconnectionAdapter(true /* disconnect */); |
192 if (fake_adapter_name == "ServicesDiscoveredAfterReconnectionAdapter") | 203 if (fake_adapter_name == "ServicesDiscoveredAfterReconnectionAdapter") |
193 return GetServicesDiscoveredAfterReconnectionAdapter( | 204 return GetServicesDiscoveredAfterReconnectionAdapter( |
194 false /* disconnect */); | 205 false /* disconnect */); |
| 206 if (fake_adapter_name == "DisconnectingDuringSuccessGATTOperationAdapter") { |
| 207 return GetGATTOperationFinishesAfterReconnectionAdapter( |
| 208 true /* disconnect */, true /* succeeds */); |
| 209 } |
| 210 if (fake_adapter_name == "DisconnectingDuringFailureGATTOperationAdapter") { |
| 211 return GetGATTOperationFinishesAfterReconnectionAdapter( |
| 212 true /* disconnect */, false /* succeeds */); |
| 213 } |
| 214 if (fake_adapter_name == "GATTOperationSucceedsAfterReconnectionAdapter") { |
| 215 return GetGATTOperationFinishesAfterReconnectionAdapter( |
| 216 false /* disconnect */, true /* succeeds */); |
| 217 } |
| 218 if (fake_adapter_name == "GATTOperationFailsAfterReconnectionAdapter") { |
| 219 return GetGATTOperationFinishesAfterReconnectionAdapter( |
| 220 false /* disconnect */, false /* succeeds */); |
| 221 } |
195 if (fake_adapter_name == "BlacklistTestAdapter") | 222 if (fake_adapter_name == "BlacklistTestAdapter") |
196 return GetBlacklistTestAdapter(); | 223 return GetBlacklistTestAdapter(); |
197 if (fake_adapter_name == "FailingConnectionsAdapter") | 224 if (fake_adapter_name == "FailingConnectionsAdapter") |
198 return GetFailingConnectionsAdapter(); | 225 return GetFailingConnectionsAdapter(); |
199 if (fake_adapter_name == "FailingGATTOperationsAdapter") | 226 if (fake_adapter_name == "FailingGATTOperationsAdapter") |
200 return GetFailingGATTOperationsAdapter(); | 227 return GetFailingGATTOperationsAdapter(); |
201 if (fake_adapter_name == "SecondDiscoveryFindsHeartRateAdapter") | 228 if (fake_adapter_name == "SecondDiscoveryFindsHeartRateAdapter") |
202 return GetSecondDiscoveryFindsHeartRateAdapter(); | 229 return GetSecondDiscoveryFindsHeartRateAdapter(); |
203 if (fake_adapter_name == "DeviceEventAdapter") | 230 if (fake_adapter_name == "DeviceEventAdapter") |
204 return GetDeviceEventAdapter(); | 231 return GetDeviceEventAdapter(); |
(...skipping 631 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
836 } | 863 } |
837 | 864 |
838 return true; | 865 return true; |
839 })); | 866 })); |
840 adapter->AddMockDevice(std::move(device)); | 867 adapter->AddMockDevice(std::move(device)); |
841 | 868 |
842 return adapter; | 869 return adapter; |
843 } | 870 } |
844 | 871 |
845 // static | 872 // static |
| 873 scoped_refptr<NiceMockBluetoothAdapter> LayoutTestBluetoothAdapterProvider:: |
| 874 GetGATTOperationFinishesAfterReconnectionAdapter(bool disconnect, |
| 875 bool succeeds) { |
| 876 scoped_refptr<NiceMockBluetoothAdapter> adapter(GetEmptyAdapter()); |
| 877 NiceMockBluetoothAdapter* adapter_ptr = adapter.get(); |
| 878 |
| 879 std::unique_ptr<NiceMockBluetoothDevice> device(GetConnectableDevice( |
| 880 adapter_ptr, "GATT Operation finishes after reconnection Device", |
| 881 BluetoothDevice::UUIDList({BluetoothUUID(kGenericAccessServiceUUID), |
| 882 BluetoothUUID(kHealthThermometerUUID)}))); |
| 883 NiceMockBluetoothDevice* device_ptr = device.get(); |
| 884 |
| 885 ON_CALL(*device, CreateGattConnection(_, _)) |
| 886 .WillByDefault(Invoke([adapter_ptr, device_ptr]( |
| 887 const BluetoothDevice::GattConnectionCallback& callback, |
| 888 const BluetoothDevice::ConnectErrorCallback& error_callback) { |
| 889 callback.Run(base::MakeUnique<NiceMockBluetoothGattConnection>( |
| 890 adapter_ptr, device_ptr->GetAddress())); |
| 891 device_ptr->RunPendingCallbacks(); |
| 892 })); |
| 893 |
| 894 device->AddMockService(GetGenericAccessService(device.get())); |
| 895 |
| 896 std::unique_ptr<NiceMockBluetoothGattService> health_thermometer( |
| 897 GetBaseGATTService("Health Thermometer", device.get(), |
| 898 kHealthThermometerUUID)); |
| 899 |
| 900 // Measurement Interval |
| 901 std::unique_ptr<NiceMockBluetoothGattCharacteristic> measurement_interval( |
| 902 GetBaseGATTCharacteristic( |
| 903 "Measurement Interval", health_thermometer.get(), |
| 904 kMeasurementIntervalUUID, |
| 905 BluetoothRemoteGattCharacteristic::PROPERTY_READ)); |
| 906 NiceMockBluetoothGattCharacteristic* measurement_ptr = |
| 907 measurement_interval.get(); |
| 908 |
| 909 ON_CALL(*measurement_interval, ReadRemoteCharacteristic(_, _)) |
| 910 .WillByDefault(Invoke([adapter_ptr, device_ptr, measurement_ptr, |
| 911 disconnect, succeeds]( |
| 912 const BluetoothRemoteGattCharacteristic::ValueCallback& callback, |
| 913 const BluetoothRemoteGattCharacteristic::ErrorCallback& |
| 914 error_callback) { |
| 915 base::Closure pending; |
| 916 if (succeeds) { |
| 917 pending = |
| 918 base::Bind(&PerformReadValue, base::RetainedRef(adapter_ptr), |
| 919 measurement_ptr, callback, std::vector<uint8_t>({1})); |
| 920 } else { |
| 921 pending = base::Bind(error_callback, |
| 922 BluetoothRemoteGattService::GATT_ERROR_FAILED); |
| 923 } |
| 924 device_ptr->PushPendingCallback(pending); |
| 925 if (disconnect) { |
| 926 device_ptr->SetConnected(false); |
| 927 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 928 FROM_HERE, |
| 929 base::Bind(&NotifyDeviceChanged, base::RetainedRef(adapter_ptr), |
| 930 device_ptr)); |
| 931 } |
| 932 })); |
| 933 |
| 934 health_thermometer->AddMockCharacteristic(std::move(measurement_interval)); |
| 935 device->AddMockService(std::move(health_thermometer)); |
| 936 adapter->AddMockDevice(std::move(device)); |
| 937 |
| 938 return adapter; |
| 939 } |
| 940 |
| 941 // static |
846 scoped_refptr<NiceMockBluetoothAdapter> | 942 scoped_refptr<NiceMockBluetoothAdapter> |
847 LayoutTestBluetoothAdapterProvider::GetBlacklistTestAdapter() { | 943 LayoutTestBluetoothAdapterProvider::GetBlacklistTestAdapter() { |
848 scoped_refptr<NiceMockBluetoothAdapter> adapter(GetEmptyAdapter()); | 944 scoped_refptr<NiceMockBluetoothAdapter> adapter(GetEmptyAdapter()); |
849 | 945 |
850 BluetoothDevice::UUIDList uuids; | 946 BluetoothDevice::UUIDList uuids; |
851 uuids.push_back(BluetoothUUID(kBlacklistTestServiceUUID)); | 947 uuids.push_back(BluetoothUUID(kBlacklistTestServiceUUID)); |
852 uuids.push_back(BluetoothUUID(kDeviceInformationServiceUUID)); | 948 uuids.push_back(BluetoothUUID(kDeviceInformationServiceUUID)); |
853 uuids.push_back(BluetoothUUID(kGenericAccessServiceUUID)); | 949 uuids.push_back(BluetoothUUID(kGenericAccessServiceUUID)); |
854 uuids.push_back(BluetoothUUID(kHeartRateServiceUUID)); | 950 uuids.push_back(BluetoothUUID(kHeartRateServiceUUID)); |
855 uuids.push_back(BluetoothUUID(kHumanInterfaceDeviceServiceUUID)); | 951 uuids.push_back(BluetoothUUID(kHumanInterfaceDeviceServiceUUID)); |
(...skipping 580 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1436 return BluetoothUUID(); | 1532 return BluetoothUUID(); |
1437 } | 1533 } |
1438 | 1534 |
1439 // static | 1535 // static |
1440 std::string LayoutTestBluetoothAdapterProvider::makeMACAddress(uint64_t addr) { | 1536 std::string LayoutTestBluetoothAdapterProvider::makeMACAddress(uint64_t addr) { |
1441 return BluetoothDevice::CanonicalizeAddress( | 1537 return BluetoothDevice::CanonicalizeAddress( |
1442 base::StringPrintf("%012" PRIx64, addr)); | 1538 base::StringPrintf("%012" PRIx64, addr)); |
1443 } | 1539 } |
1444 | 1540 |
1445 } // namespace content | 1541 } // namespace content |
OLD | NEW |