Index: device/bluetooth/test/fake_bluetooth.cc |
diff --git a/device/bluetooth/test/fake_bluetooth.cc b/device/bluetooth/test/fake_bluetooth.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..b7f76cfee5cd0e9ff8689e3003c1bb886ad9e7c3 |
--- /dev/null |
+++ b/device/bluetooth/test/fake_bluetooth.cc |
@@ -0,0 +1,615 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "device/bluetooth/test/fake_bluetooth.h" |
+ |
+#include <utility> |
+ |
+#include "base/memory/ptr_util.h" |
+#include "device/bluetooth/bluetooth_adapter_factory.h" |
+#include "device/bluetooth/public/interfaces/fake_bluetooth.mojom.h" |
+#include "mojo/public/cpp/bindings/strong_binding.h" |
+ |
+namespace bluetooth { |
+ |
+FakeBluetooth::FakeBluetooth() {} |
+FakeBluetooth::~FakeBluetooth() {} |
+ |
+// static |
+void FakeBluetooth::Create(mojom::FakeBluetoothRequest request) { |
+ mojo::MakeStrongBinding(base::MakeUnique<FakeBluetooth>(), |
+ std::move(request)); |
+} |
+ |
+void FakeBluetooth::SetLEAvailability( |
+ bool available, |
+ const SetLEAvailabilityCallback& callback) { |
+ device::BluetoothAdapterFactory::SetAvailability(available); |
+ callback.Run(); |
+} |
+ |
+void FakeBluetooth::SimulateLECentralManager( |
+ mojom::FakeLECentralManagerOptionsPtr options, |
+ const SimulateLECentralManagerCallback& callback) { |
+ mojom::FakeLECentralManagerPtr central_manager_ptr; |
+ |
+ // TODO: Use a scoped_refptr |
+ new FakeLECentralManager(std::move(options), |
+ mojo::MakeRequest(¢ral_manager_ptr)); |
+ |
+ // device::BluetoothAdapterFactory::Get().SetBluetoothAdapterForTesting( |
+ // manager); |
+ |
+ callback.Run(std::move(central_manager_ptr)); |
+} |
+ |
+FakeLECentralManager::FakeLECentralManager( |
+ mojom::FakeLECentralManagerOptionsPtr options, |
+ mojom::FakeLECentralManagerRequest request) |
+ : state_(options->state), |
+ binding_(this, std::move(request)), |
+ start_scan_calls_(0), |
+ stop_scan_calls_(0) {} |
+ |
+FakeLECentralManager::~FakeLECentralManager() {} |
+ |
+void FakeLECentralManager::SimulateAdvertisementReceived( |
+ mojom::ScanResultPtr result, |
+ const SimulateAdvertisementReceivedCallback& callback) { |
+ // for all observers dispatch device added or changed. |
+ mojom::FakeLEPeripheralPtr peripheral_ptr; |
+ mojo::MakeStrongBinding( |
+ base::MakeUnique<FakeLEPeripheral>(result->device_address), |
+ mojo::MakeRequest(&peripheral_ptr)); |
+ callback.Run(std::move(peripheral_ptr)); |
+} |
+ |
+void FakeLECentralManager::AddConnectedLEPeripheral( |
+ mojom::FakeLEPeripheralOptionsPtr options, |
+ const AddConnectedLEPeripheralCallback& callback) { |
+ mojom::FakeLEPeripheralPtr peripheral_ptr; |
+ mojo::MakeStrongBinding(base::MakeUnique<FakeLEPeripheral>(options->address), |
+ mojo::MakeRequest(&peripheral_ptr)); |
+ callback.Run(std::move(peripheral_ptr)); |
+} |
+ |
+void FakeLECentralManager::SetState(mojom::FakeLECentralManagerState state, |
+ const SetStateCallback& callback) { |
+ state_ = state; |
+ callback.Run(); |
+} |
+ |
+void FakeLECentralManager::GetStartScanCalls( |
+ const GetStartScanCallsCallback& callback) { |
+ callback.Run(start_scan_calls_); |
+} |
+ |
+void FakeLECentralManager::GetStopScanCalls( |
+ const GetStopScanCallsCallback& callback) { |
+ callback.Run(stop_scan_calls_); |
+} |
+ |
+std::string FakeLECentralManager::GetAddress() const { |
+ return "Some address"; |
+} |
+ |
+std::string FakeLECentralManager::GetName() const { |
+ return "Some name"; |
+} |
+ |
+void FakeLECentralManager::SetName( |
+ const std::string& name, |
+ const base::Closure& callback, |
+ const device::BluetoothAdapter::ErrorCallback& error_callback) { |
+ error_callback.Run(); |
+} |
+ |
+bool FakeLECentralManager::IsInitialized() const { |
+ return true; |
+} |
+ |
+bool FakeLECentralManager::IsPresent() const { |
+ if (state_ == mojom::FakeLECentralManagerState::ABSENT) { |
+ return false; |
+ } |
+ return true; |
+} |
+ |
+bool FakeLECentralManager::IsPowered() const { |
+ switch(state_) { |
+ case mojom::FakeLECentralManagerState::ABSENT: |
+ case mojom::FakeLECentralManagerState::POWERED_OFF: |
+ return false; |
+ case mojom::FakeLECentralManagerState::POWERED_ON: |
+ return true; |
+ } |
+ NOTREACHED(); |
+ return false; |
+} |
+ |
+void FakeLECentralManager::SetPowered( |
+ bool powered, |
+ const base::Closure& callback, |
+ const device::BluetoothAdapter::ErrorCallback& error_callback) { |
+ error_callback.Run(); |
+} |
+ |
+bool FakeLECentralManager::IsDiscoverable() const { |
+ return false; |
+} |
+ |
+void FakeLECentralManager::SetDiscoverable( |
+ bool discoverable, |
+ const base::Closure& callback, |
+ const ErrorCallback& error_callback) { |
+ error_callback.Run(); |
+} |
+ |
+bool FakeLECentralManager::IsDiscovering() const { |
+ return false; |
+} |
+ |
+device::BluetoothAdapter::UUIDList FakeLECentralManager::GetUUIDs() const { |
+ return device::BluetoothAdapter::UUIDList(); |
+} |
+ |
+void FakeLECentralManager::CreateRfcommService( |
+ const device::BluetoothUUID& uuid, |
+ const device::BluetoothAdapter::ServiceOptions& options, |
+ const device::BluetoothAdapter::CreateServiceCallback& callback, |
+ const device::BluetoothAdapter::CreateServiceErrorCallback& |
+ error_callback) { |
+ error_callback.Run("Unsupported"); |
+} |
+ |
+void FakeLECentralManager::CreateL2capService( |
+ const device::BluetoothUUID& uuid, |
+ const device::BluetoothAdapter::ServiceOptions& options, |
+ const device::BluetoothAdapter::CreateServiceCallback& callback, |
+ const device::BluetoothAdapter::CreateServiceErrorCallback& |
+ error_callback) { |
+ error_callback.Run("Unsupported"); |
+} |
+ |
+void FakeLECentralManager::SetAdvertisingInterval( |
+ const base::TimeDelta& min, |
+ const base::TimeDelta& max, |
+ const base::Closure& callback, |
+ const device::BluetoothAdapter::AdvertisementErrorCallback& |
+ error_callback) { |
+ callback.Run(); |
+} |
+ |
+void FakeLECentralManager::RegisterAdvertisement( |
+ std::unique_ptr<device::BluetoothAdvertisement::Data> advertisement_data, |
+ const device::BluetoothAdapter::CreateAdvertisementCallback& callback, |
+ const device::BluetoothAdapter::AdvertisementErrorCallback& error_callback) { |
+} |
+ |
+ |
+device::BluetoothLocalGattService* |
+FakeLECentralManager::GetGattService(const std::string& identifier) const { |
+ return nullptr; |
+} |
+ |
+void FakeLECentralManager::AddDiscoverySession( |
+ device::BluetoothDiscoveryFilter* discovery_filter, |
+ const base::Closure& callback, |
+ const device::BluetoothAdapter::DiscoverySessionErrorCallback& |
+ error_callback) { |
+ callback.Run(); |
+} |
+ |
+void FakeLECentralManager::RemoveDiscoverySession( |
+ device::BluetoothDiscoveryFilter* discovery_filter, |
+ const base::Closure& callback, |
+ const device::BluetoothAdapter::DiscoverySessionErrorCallback& |
+ error_callback) { |
+ callback.Run(); |
+} |
+ |
+void FakeLECentralManager::SetDiscoveryFilter( |
+ std::unique_ptr<device::BluetoothDiscoveryFilter> |
+ discovery_filter, |
+ const base::Closure& callback, |
+ const device::BluetoothAdapter::DiscoverySessionErrorCallback& |
+ error_callback) { |
+ callback.Run(); |
+} |
+ |
+void FakeLECentralManager::RemovePairingDelegateInternal( |
+ device::BluetoothDevice::PairingDelegate* pairing_delegate) { |
+} |
+ |
+FakeLEPeripheral::FakeLEPeripheral(const std::string& address) |
+ : BluetoothDevice(nullptr), address_(address) {} |
+ |
+FakeLEPeripheral::~FakeLEPeripheral() { |
+} |
+ |
+void FakeLEPeripheral::SimulateGATTConnection( |
+ mojom::GATTConnectionResult result, |
+ const SimulateGATTConnectionCallback& callback) { |
+ callback.Run(); |
+} |
+ |
+void FakeLEPeripheral::SimulateGATTDiscoveryComplete( |
+ const SimulateGATTDiscoveryCompleteCallback& callback) { |
+ callback.Run(); |
+} |
+ |
+void FakeLEPeripheral::SimulateGATTServicesChanged( |
+ const SimulateGATTServicesChangedCallback& callback) { |
+ callback.Run(); |
+} |
+ |
+void FakeLEPeripheral::AddFakeService( |
+ mojom::FakeRemoteGATTServiceOptionsPtr options, |
+ const AddFakeServiceCallback& callback) { |
+ mojom::FakeRemoteGATTServicePtr service_ptr; |
+ mojo::MakeStrongBinding( |
+ base::MakeUnique<FakeRemoteGATTService>(std::move(options)), |
+ mojo::MakeRequest(&service_ptr)); |
+ callback.Run(std::move(service_ptr)); |
+} |
+ |
+void FakeLEPeripheral::GetConnectCalls( |
+ const GetConnectCallsCallback& callback) { |
+ callback.Run(connect_calls_); |
+} |
+ |
+void FakeLEPeripheral::GetDisconnectCalls( |
+ const GetDisconnectCallsCallback& callback) { |
+ callback.Run(disconnect_calls_); |
+} |
+ |
+uint32_t FakeLEPeripheral::GetBluetoothClass() const { |
+ return 0; |
+} |
+ |
+#if defined(OS_CHROMEOS) || defined(OS_LINUX) |
+device::BluetoothTransport FakeLEPeripheral::GetType() const { |
+ return device::BLUETOOTH_TRANSPORT_INVALID; |
+} |
+#endif |
+ |
+std::string FakeLEPeripheral::GetIdentifier() const { |
+ return "some identifier"; |
+} |
+ |
+std::string FakeLEPeripheral::GetAddress() const { |
+ return "some address"; |
+} |
+ |
+device::BluetoothDevice::VendorIDSource FakeLEPeripheral::GetVendorIDSource() const { |
+ return device::BluetoothDevice::VENDOR_ID_UNKNOWN; |
+} |
+ |
+uint16_t FakeLEPeripheral::GetVendorID() const { |
+ return 0; |
+} |
+ |
+uint16_t FakeLEPeripheral::GetProductID() const { |
+ return 1; |
+} |
+ |
+uint16_t FakeLEPeripheral::GetDeviceID() const { |
+ return 2; |
+} |
+ |
+uint16_t FakeLEPeripheral::GetAppearance() const { |
+ return 3; |
+} |
+ |
+base::Optional<std::string> FakeLEPeripheral::GetName() const { |
+ return std::string(); |
+} |
+ |
+bool FakeLEPeripheral::IsPaired() const { |
+ return false; |
+} |
+ |
+bool FakeLEPeripheral::IsConnected() const { |
+ return false; |
+} |
+ |
+bool FakeLEPeripheral::IsGattConnected() const { |
+ return false; |
+} |
+ |
+bool FakeLEPeripheral::IsConnectable() const { |
+ return true; |
+} |
+ |
+bool FakeLEPeripheral::IsConnecting() const { |
+ return false; |
+} |
+ |
+bool FakeLEPeripheral::ExpectingPinCode() const { |
+ return false; |
+} |
+ |
+bool FakeLEPeripheral::ExpectingPasskey() const { |
+ return false; |
+} |
+ |
+bool FakeLEPeripheral::ExpectingConfirmation() const { |
+ return false; |
+} |
+ |
+void FakeLEPeripheral::GetConnectionInfo(const device::BluetoothDevice::ConnectionInfoCallback& callback) { |
+} |
+ |
+void FakeLEPeripheral::Connect(device::BluetoothDevice::PairingDelegate* pairing_delegate, |
+ const base::Closure& callback, |
+ const device::BluetoothDevice::ConnectErrorCallback& error_callback) { |
+ callback.Run(); |
+} |
+ |
+void FakeLEPeripheral::SetPinCode(const std::string& pincode) { |
+} |
+ |
+void FakeLEPeripheral::SetPasskey(uint32_t passkey) { |
+} |
+ |
+void FakeLEPeripheral::ConfirmPairing() { |
+} |
+ |
+void FakeLEPeripheral::RejectPairing() { |
+} |
+ |
+void FakeLEPeripheral::CancelPairing() { |
+} |
+ |
+void FakeLEPeripheral::Disconnect(const base::Closure& callback, |
+ const device::BluetoothDevice::ErrorCallback& error_callback) { |
+ callback.Run(); |
+} |
+ |
+void FakeLEPeripheral::Forget(const base::Closure& callback, |
+ const device::BluetoothDevice::ErrorCallback& error_callback) { |
+ callback.Run(); |
+} |
+ |
+void FakeLEPeripheral::ConnectToService( |
+ const device::BluetoothUUID& uuid, |
+ const device::BluetoothDevice::ConnectToServiceCallback& callback, |
+ const device::BluetoothDevice::ConnectToServiceErrorCallback& error_callback) { |
+} |
+ |
+void FakeLEPeripheral::ConnectToServiceInsecurely( |
+ const device::BluetoothUUID& uuid, |
+ const device::BluetoothDevice::ConnectToServiceCallback& callback, |
+ const device::BluetoothDevice::ConnectToServiceErrorCallback& error_callback) { |
+} |
+ |
+void FakeLEPeripheral::CreateGattConnectionImpl() { |
+} |
+ |
+void FakeLEPeripheral::DisconnectGatt() { |
+} |
+ |
+FakeRemoteGATTService::FakeRemoteGATTService( |
+ mojom::FakeRemoteGATTServiceOptionsPtr options) |
+ : uuid_(options->uuid) {} |
+ |
+void FakeRemoteGATTService::AddFakeCharacteristic( |
+ mojom::FakeRemoteGATTCharacteristicOptionsPtr options, |
+ const AddFakeCharacteristicCallback& callback) { |
+ mojom::FakeRemoteGATTCharacteristicPtr char_ptr; |
+ mojo::MakeStrongBinding( |
+ base::MakeUnique<FakeRemoteGATTCharacteristic>(std::move(options)), |
+ mojo::MakeRequest(&char_ptr)); |
+ callback.Run(std::move(char_ptr)); |
+} |
+ |
+void FakeRemoteGATTService::AddFakeIncludedService( |
+ mojom::FakeRemoteGATTServiceOptionsPtr options, |
+ const AddFakeIncludedServiceCallback& callback) { |
+ mojom::FakeRemoteGATTServicePtr service_ptr; |
+ mojo::MakeStrongBinding( |
+ base::MakeUnique<FakeRemoteGATTService>(std::move(options)), |
+ mojo::MakeRequest(&service_ptr)); |
+ callback.Run(std::move(service_ptr)); |
+} |
+ |
+std::string FakeRemoteGATTService::GetIdentifier() const { |
+ return "some identifier"; |
+} |
+ |
+device::BluetoothUUID FakeRemoteGATTService::GetUUID() const { |
+ return device::BluetoothUUID(); |
+} |
+ |
+device::BluetoothDevice* FakeRemoteGATTService::GetDevice() const { |
+ return nullptr; |
+} |
+ |
+bool FakeRemoteGATTService::IsPrimary() const { |
+ return true; |
+} |
+ |
+std::vector<device::BluetoothRemoteGattCharacteristic*> FakeRemoteGATTService::GetCharacteristics() const { |
+ return std::vector<device::BluetoothRemoteGattCharacteristic*>(); |
+} |
+ |
+std::vector<device::BluetoothRemoteGattService*> FakeRemoteGATTService::GetIncludedServices() const { |
+ return std::vector<device::BluetoothRemoteGattService*>(); |
+} |
+ |
+device::BluetoothRemoteGattCharacteristic* FakeRemoteGATTService::GetCharacteristic(const std::string& identifier) const { |
+ return nullptr; |
+} |
+ |
+FakeRemoteGATTCharacteristic::FakeRemoteGATTCharacteristic( |
+ mojom::FakeRemoteGATTCharacteristicOptionsPtr options) |
+ : uuid_(options->uuid), permissions_(options->permissions) {} |
+ |
+FakeRemoteGATTCharacteristic::~FakeRemoteGATTCharacteristic() {} |
+ |
+void FakeRemoteGATTCharacteristic::SimulateRead( |
+ mojom::GATTOperationResult result, |
+ const base::Optional<std::vector<uint8_t>>& value, |
+ const SimulateReadCallback& callback) { |
+ callback.Run(); |
+} |
+ |
+void FakeRemoteGATTCharacteristic::SimulateWrite( |
+ mojom::GATTOperationResult result, |
+ const SimulateWriteCallback& callback) { |
+ callback.Run(std::vector<uint8_t>()); |
+} |
+ |
+void FakeRemoteGATTCharacteristic::SimulateNotificationsStarted( |
+ mojom::GATTOperationResult result, |
+ const SimulateNotificationsStartedCallback& callback) { |
+ callback.Run(); |
+} |
+ |
+void FakeRemoteGATTCharacteristic::SimulateNotificationsStopped( |
+ mojom::GATTOperationResult result, |
+ const SimulateNotificationsStoppedCallback& callback) { |
+ callback.Run(); |
+} |
+ |
+void FakeRemoteGATTCharacteristic::SimulateNotification( |
+ mojom::FakeNotificationOptionsPtr options, |
+ const SimulateNotificationCallback& callback) { |
+ callback.Run(); |
+} |
+ |
+void FakeRemoteGATTCharacteristic::AddFakeDescriptor( |
+ mojom::FakeRemoteGATTDescriptorOptionsPtr options, |
+ const AddFakeDescriptorCallback& callback) { |
+ mojom::FakeRemoteGATTDescriptorPtr desc_ptr; |
+ mojo::MakeStrongBinding( |
+ base::MakeUnique<FakeRemoteGATTDescriptor>(std::move(options)), |
+ mojo::MakeRequest(&desc_ptr)); |
+ callback.Run(std::move(desc_ptr)); |
+} |
+ |
+void FakeRemoteGATTCharacteristic::GetReadCalls( |
+ const GetReadCallsCallback& callback) { |
+ callback.Run(read_calls_); |
+} |
+ |
+void FakeRemoteGATTCharacteristic::GetWriteCalls( |
+ const GetWriteCallsCallback& callback) { |
+ callback.Run(write_calls_); |
+} |
+ |
+void FakeRemoteGATTCharacteristic::GetStartNotificationsCalls( |
+ const GetStartNotificationsCallsCallback& callback) { |
+ callback.Run(start_notifications_calls_); |
+} |
+ |
+void FakeRemoteGATTCharacteristic::GetStopNotificationsCalls( |
+ const GetStopNotificationsCallsCallback& callback) { |
+ callback.Run(stop_notifications_calls_); |
+} |
+ |
+std::string FakeRemoteGATTCharacteristic::GetIdentifier() const { |
+ return "Char identifier"; |
+} |
+ |
+device::BluetoothUUID FakeRemoteGATTCharacteristic::GetUUID() const { |
+ return device::BluetoothUUID("1208"); |
+} |
+ |
+device::BluetoothGattCharacteristic::Properties FakeRemoteGATTCharacteristic::GetProperties() const { |
+ return 0; |
+} |
+ |
+device::BluetoothRemoteGattCharacteristic::Permissions FakeRemoteGATTCharacteristic::GetPermissions() const { |
+ return 1; |
+} |
+ |
+const std::vector<uint8_t>& FakeRemoteGATTCharacteristic::GetValue() const { |
+ return value_; |
+} |
+ |
+device::BluetoothRemoteGattService* FakeRemoteGATTCharacteristic::GetService() const { |
+ return nullptr; |
+} |
+ |
+std::vector<device::BluetoothRemoteGattDescriptor*> FakeRemoteGATTCharacteristic::GetDescriptors() const { |
+ return std::vector<device::BluetoothRemoteGattDescriptor*>(); |
+} |
+ |
+device::BluetoothRemoteGattDescriptor* FakeRemoteGATTCharacteristic::GetDescriptor(const std::string& identifier) const { |
+ return nullptr; |
+} |
+ |
+void FakeRemoteGATTCharacteristic::ReadRemoteCharacteristic(const device::BluetoothRemoteGattCharacteristic::ValueCallback& callback, |
+ const device::BluetoothRemoteGattCharacteristic::ErrorCallback& error_callback) { |
+} |
+ |
+void FakeRemoteGATTCharacteristic::WriteRemoteCharacteristic(const std::vector<uint8_t>& value, |
+ const base::Closure& callback, |
+ const device::BluetoothRemoteGattCharacteristic::ErrorCallback& error_callback) { |
+ callback.Run(); |
+} |
+ |
+void FakeRemoteGATTCharacteristic::SubscribeToNotifications(device::BluetoothRemoteGattDescriptor* ccc_descriptor, |
+ const base::Closure& callback, |
+ const device::BluetoothRemoteGattCharacteristic::ErrorCallback& error_callback) { |
+ callback.Run(); |
+} |
+ |
+void FakeRemoteGATTCharacteristic::UnsubscribeFromNotifications( |
+ device::BluetoothRemoteGattDescriptor* ccc_descriptor, |
+ const base::Closure& callback, |
+ const device::BluetoothRemoteGattCharacteristic::ErrorCallback& error_callback) { |
+ callback.Run(); |
+} |
+ |
+FakeRemoteGATTDescriptor::FakeRemoteGATTDescriptor( |
+ mojom::FakeRemoteGATTDescriptorOptionsPtr options) |
+ : uuid_(options->uuid) {} |
+ |
+FakeRemoteGATTDescriptor::~FakeRemoteGATTDescriptor() {} |
+ |
+void FakeRemoteGATTDescriptor::SimulateRead( |
+ mojom::GATTOperationResult result, |
+ const base::Optional<std::vector<uint8_t>>& value, |
+ const SimulateReadCallback& callback) { |
+ callback.Run(); |
+} |
+ |
+void FakeRemoteGATTDescriptor::SimulateWrite( |
+ mojom::GATTOperationResult result, |
+ const SimulateWriteCallback& callback) { |
+ callback.Run(std::vector<uint8_t>()); |
+} |
+ |
+std::string FakeRemoteGATTDescriptor::GetIdentifier() const { |
+ return "desc identifier"; |
+} |
+ |
+device::BluetoothUUID FakeRemoteGATTDescriptor::GetUUID() const { |
+ return device::BluetoothUUID(); |
+} |
+ |
+device::BluetoothRemoteGattCharacteristic::Permissions FakeRemoteGATTDescriptor::GetPermissions() const { |
+ return 10; |
+} |
+const std::vector<uint8_t>& FakeRemoteGATTDescriptor::GetValue() const { |
+ return value_; |
+} |
+ |
+device::BluetoothRemoteGattCharacteristic* FakeRemoteGATTDescriptor::GetCharacteristic() const { |
+ return nullptr; |
+} |
+ |
+void FakeRemoteGATTDescriptor::ReadRemoteDescriptor(const device::BluetoothRemoteGattCharacteristic::ValueCallback& callback, |
+ const device::BluetoothRemoteGattCharacteristic::ErrorCallback& error_callback) { |
+} |
+ |
+void FakeRemoteGATTDescriptor::WriteRemoteDescriptor(const std::vector<uint8_t>& new_value, |
+ const base::Closure& callback, |
+ const device::BluetoothRemoteGattCharacteristic::ErrorCallback& error_callback) { |
+ callback.Run(); |
+} |
+ |
+} // namespace bluetooth |