diff options
author | gogerald <gogerald@chromium.org> | 2016-02-03 13:20:33 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-02-03 21:21:36 +0000 |
commit | cab3123dd3d86927cbc5e310100ec498f130ea80 (patch) | |
tree | 73b854b4bc327ed59b718a71ead655d499f7449c /device | |
parent | f1005f6e964841763ae3792ff6aea399f9514a67 (diff) | |
download | chromium_src-cab3123dd3d86927cbc5e310100ec498f130ea80.zip chromium_src-cab3123dd3d86927cbc5e310100ec498f130ea80.tar.gz chromium_src-cab3123dd3d86927cbc5e310100ec498f130ea80.tar.bz2 |
Refactor bluetooth_low_energy_win to prepare for new Bluetooth test fixture
This CL refactors bluetooth_low_energy_win to prepare for simulating fake Bluetooth low energy device for the new Bluetooth test fixture.
BUG=579202
Review URL: https://codereview.chromium.org/1646023002
Cr-Commit-Position: refs/heads/master@{#373341}
Diffstat (limited to 'device')
-rw-r--r-- | device/BUILD.gn | 7 | ||||
-rw-r--r-- | device/bluetooth/bluetooth_low_energy_win.cc | 47 | ||||
-rw-r--r-- | device/bluetooth/bluetooth_low_energy_win.h | 75 | ||||
-rw-r--r-- | device/bluetooth/bluetooth_low_energy_win_fake.cc | 37 | ||||
-rw-r--r-- | device/bluetooth/bluetooth_low_energy_win_fake.h | 34 | ||||
-rw-r--r-- | device/bluetooth/bluetooth_task_manager_win.cc | 19 | ||||
-rw-r--r-- | device/device_tests.gyp | 6 |
7 files changed, 175 insertions, 50 deletions
diff --git a/device/BUILD.gn b/device/BUILD.gn index 2f959b7..2b68111 100644 --- a/device/BUILD.gn +++ b/device/BUILD.gn @@ -189,6 +189,13 @@ test("device_unittests") { ] } } + + if (is_win) { + sources += [ + "bluetooth/bluetooth_low_energy_win_fake.cc", + "bluetooth/bluetooth_low_energy_win_fake.h", + ] + } } if (is_android) { diff --git a/device/bluetooth/bluetooth_low_energy_win.cc b/device/bluetooth/bluetooth_low_energy_win.cc index 54af750..e0a8432 100644 --- a/device/bluetooth/bluetooth_low_energy_win.cc +++ b/device/bluetooth/bluetooth_low_energy_win.cc @@ -652,7 +652,36 @@ bool IsBluetoothLowEnergySupported() { return base::win::GetVersion() >= base::win::VERSION_WIN8; } -bool EnumerateKnownBluetoothLowEnergyDevices( +bool ExtractBluetoothAddressFromDeviceInstanceIdForTesting( + const std::string& instance_id, + BLUETOOTH_ADDRESS* btha, + std::string* error) { + return ExtractBluetoothAddressFromDeviceInstanceId(instance_id, btha, error); +} + +static BluetoothLowEnergyWrapper* instance_ = nullptr; +BluetoothLowEnergyWrapper* BluetoothLowEnergyWrapper::GetInstance() { + if (instance_ == nullptr) { + instance_ = new BluetoothLowEnergyWrapper(); + } + return instance_; +} + +void BluetoothLowEnergyWrapper::DeleteInstance() { + delete instance_; + instance_ = nullptr; +} + +void BluetoothLowEnergyWrapper::SetInstanceForTest( + BluetoothLowEnergyWrapper* instance) { + delete instance_; + instance_ = instance; +} + +BluetoothLowEnergyWrapper::BluetoothLowEnergyWrapper() {} +BluetoothLowEnergyWrapper::~BluetoothLowEnergyWrapper() {} + +bool BluetoothLowEnergyWrapper::EnumerateKnownBluetoothLowEnergyDevices( ScopedVector<BluetoothLowEnergyDeviceInfo>* devices, std::string* error) { if (!IsBluetoothLowEnergySupported()) { @@ -664,9 +693,10 @@ bool EnumerateKnownBluetoothLowEnergyDevices( GUID_BLUETOOTHLE_DEVICE_INTERFACE, devices, error); } -bool EnumerateKnownBluetoothLowEnergyGattServiceDevices( - ScopedVector<BluetoothLowEnergyDeviceInfo>* devices, - std::string* error) { +bool BluetoothLowEnergyWrapper:: + EnumerateKnownBluetoothLowEnergyGattServiceDevices( + ScopedVector<BluetoothLowEnergyDeviceInfo>* devices, + std::string* error) { if (!IsBluetoothLowEnergySupported()) { *error = kPlatformNotSupported; return false; @@ -676,7 +706,7 @@ bool EnumerateKnownBluetoothLowEnergyGattServiceDevices( GUID_BLUETOOTH_GATT_SERVICE_DEVICE_INTERFACE, devices, error); } -bool EnumerateKnownBluetoothLowEnergyServices( +bool BluetoothLowEnergyWrapper::EnumerateKnownBluetoothLowEnergyServices( const base::FilePath& device_path, ScopedVector<BluetoothLowEnergyServiceInfo>* services, std::string* error) { @@ -688,12 +718,5 @@ bool EnumerateKnownBluetoothLowEnergyServices( return CollectBluetoothLowEnergyDeviceServices(device_path, services, error); } -bool ExtractBluetoothAddressFromDeviceInstanceIdForTesting( - const std::string& instance_id, - BLUETOOTH_ADDRESS* btha, - std::string* error) { - return ExtractBluetoothAddressFromDeviceInstanceId(instance_id, btha, error); -} - } // namespace win } // namespace device diff --git a/device/bluetooth/bluetooth_low_energy_win.h b/device/bluetooth/bluetooth_low_energy_win.h index 52347ce..9abdf34 100644 --- a/device/bluetooth/bluetooth_low_energy_win.h +++ b/device/bluetooth/bluetooth_low_energy_win.h @@ -12,7 +12,6 @@ #include "base/macros.h" #include "base/memory/scoped_ptr.h" #include "base/memory/scoped_vector.h" -#include "base/win/scoped_handle.h" #include "device/bluetooth/bluetooth_export.h" #include "device/bluetooth/bluetooth_low_energy_defs_win.h" @@ -100,42 +99,56 @@ struct BluetoothLowEnergyDeviceInfo { bool connected; }; -// Enumerates the list of known (i.e. already paired) Bluetooth LE devices on -// this machine. In case of error, returns false and sets |error| with an error -// message describing the problem. -// Note: This function returns an error if Bluetooth Low Energy is not supported -// on this Windows platform. -bool EnumerateKnownBluetoothLowEnergyDevices( - ScopedVector<BluetoothLowEnergyDeviceInfo>* devices, - std::string* error); - -// Enumerates the list of known Bluetooth LE GATT service devices on this -// machine (a Bluetooth LE device usually has more than one GATT -// services that each of them has a device interface on the machine). In case -// of error, returns false and sets |error| with an error message describing the -// problem. -// Note: This function returns an error if Bluetooth Low Energy is not supported -// on this Windows platform. -bool EnumerateKnownBluetoothLowEnergyGattServiceDevices( - ScopedVector<BluetoothLowEnergyDeviceInfo>* devices, - std::string* error); - -// Enumerates the list of known (i.e. cached) GATT services for a given -// Bluetooth LE device |device_path| into |services|. In case of error, returns -// false and sets |error| with an error message describing the problem. Note: -// This function returns an error if Bluetooth Low Energy is not supported on -// this Windows platform. -bool EnumerateKnownBluetoothLowEnergyServices( - const base::FilePath& device_path, - ScopedVector<BluetoothLowEnergyServiceInfo>* services, - std::string* error); - bool DEVICE_BLUETOOTH_EXPORT ExtractBluetoothAddressFromDeviceInstanceIdForTesting( const std::string& instance_id, BLUETOOTH_ADDRESS* btha, std::string* error); +// Wraps Windows APIs used to access Bluetooth Low Energy devices, providing an +// interface that can be replaced with fakes in tests. +class DEVICE_BLUETOOTH_EXPORT BluetoothLowEnergyWrapper { + public: + static BluetoothLowEnergyWrapper* GetInstance(); + static void DeleteInstance(); + static void SetInstanceForTest(BluetoothLowEnergyWrapper* instance); + + // Enumerates the list of known (i.e. already paired) Bluetooth LE devices on + // this machine. In case of error, returns false and sets |error| with an + // error message describing the problem. + // Note: This function returns an error if Bluetooth Low Energy is not + // supported on this Windows platform. + virtual bool EnumerateKnownBluetoothLowEnergyDevices( + ScopedVector<BluetoothLowEnergyDeviceInfo>* devices, + std::string* error); + + // Enumerates the list of known Bluetooth LE GATT service devices on this + // machine (a Bluetooth LE device usually has more than one GATT + // services that each of them has a device interface on the machine). In case + // of error, returns false and sets |error| with an error message describing + // the problem. + // Note: This function returns an error if Bluetooth Low Energy is not + // supported on this Windows platform. + virtual bool EnumerateKnownBluetoothLowEnergyGattServiceDevices( + ScopedVector<BluetoothLowEnergyDeviceInfo>* devices, + std::string* error); + + // Enumerates the list of known (i.e. cached) GATT services for a given + // Bluetooth LE device |device_path| into |services|. In case of error, + // returns false and sets |error| with an error message describing the + // problem. + // Note: This function returns an error if Bluetooth Low Energy is not + // supported on this Windows platform. + virtual bool EnumerateKnownBluetoothLowEnergyServices( + const base::FilePath& device_path, + ScopedVector<BluetoothLowEnergyServiceInfo>* services, + std::string* error); + + protected: + BluetoothLowEnergyWrapper(); + virtual ~BluetoothLowEnergyWrapper(); +}; + } // namespace win } // namespace device diff --git a/device/bluetooth/bluetooth_low_energy_win_fake.cc b/device/bluetooth/bluetooth_low_energy_win_fake.cc new file mode 100644 index 0000000..d1ef1df --- /dev/null +++ b/device/bluetooth/bluetooth_low_energy_win_fake.cc @@ -0,0 +1,37 @@ +// 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/bluetooth_low_energy_win_fake.h" + +namespace device { +namespace win { + +BluetoothLowEnergyWrapperFake::BluetoothLowEnergyWrapperFake() {} +BluetoothLowEnergyWrapperFake::~BluetoothLowEnergyWrapperFake() {} + +bool BluetoothLowEnergyWrapperFake::EnumerateKnownBluetoothLowEnergyDevices( + ScopedVector<BluetoothLowEnergyDeviceInfo>* devices, + std::string* error) { + NOTIMPLEMENTED(); + return false; +} + +bool BluetoothLowEnergyWrapperFake:: + EnumerateKnownBluetoothLowEnergyGattServiceDevices( + ScopedVector<BluetoothLowEnergyDeviceInfo>* devices, + std::string* error) { + NOTIMPLEMENTED(); + return false; +} + +bool BluetoothLowEnergyWrapperFake::EnumerateKnownBluetoothLowEnergyServices( + const base::FilePath& device_path, + ScopedVector<BluetoothLowEnergyServiceInfo>* services, + std::string* error) { + NOTIMPLEMENTED(); + return false; +} + +} // namespace win +} // namespace device diff --git a/device/bluetooth/bluetooth_low_energy_win_fake.h b/device/bluetooth/bluetooth_low_energy_win_fake.h new file mode 100644 index 0000000..a22d5cb --- /dev/null +++ b/device/bluetooth/bluetooth_low_energy_win_fake.h @@ -0,0 +1,34 @@ +// 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. + +#ifndef DEVICE_BLUETOOTH_BLUETOOTH_LOW_ENERGY_WIN_FAKE_H_ +#define DEVICE_BLUETOOTH_BLUETOOTH_LOW_ENERGY_WIN_FAKE_H_ + +#include "device/bluetooth/bluetooth_low_energy_win.h" + +namespace device { +namespace win { + +// Fake implementation of BluetoothLowEnergyWrapper. Used for BluetoothTestWin. +class BluetoothLowEnergyWrapperFake : public BluetoothLowEnergyWrapper { + public: + BluetoothLowEnergyWrapperFake(); + ~BluetoothLowEnergyWrapperFake() override; + + bool EnumerateKnownBluetoothLowEnergyDevices( + ScopedVector<BluetoothLowEnergyDeviceInfo>* devices, + std::string* error) override; + bool EnumerateKnownBluetoothLowEnergyGattServiceDevices( + ScopedVector<BluetoothLowEnergyDeviceInfo>* devices, + std::string* error) override; + bool EnumerateKnownBluetoothLowEnergyServices( + const base::FilePath& device_path, + ScopedVector<BluetoothLowEnergyServiceInfo>* services, + std::string* error) override; +}; + +} // namespace win +} // namespace device + +#endif // DEVICE_BLUETOOTH_BLUETOOTH_LOW_ENERGY_WIN_FAKE_H_ diff --git a/device/bluetooth/bluetooth_task_manager_win.cc b/device/bluetooth/bluetooth_task_manager_win.cc index b34718f..302f8a5 100644 --- a/device/bluetooth/bluetooth_task_manager_win.cc +++ b/device/bluetooth/bluetooth_task_manager_win.cc @@ -142,6 +142,7 @@ BluetoothTaskManagerWin::BluetoothTaskManagerWin( } BluetoothTaskManagerWin::~BluetoothTaskManagerWin() { + win::BluetoothLowEnergyWrapper::DeleteInstance(); } void BluetoothTaskManagerWin::AddObserver(Observer* observer) { @@ -480,7 +481,8 @@ bool BluetoothTaskManagerWin::SearchLowEnergyDevices( ScopedVector<win::BluetoothLowEnergyDeviceInfo> btle_devices; std::string error; bool success = - win::EnumerateKnownBluetoothLowEnergyDevices(&btle_devices, &error); + win::BluetoothLowEnergyWrapper::GetInstance() + ->EnumerateKnownBluetoothLowEnergyDevices(&btle_devices, &error); if (!success) { LogPollingError(error.c_str(), 0); return false; @@ -637,8 +639,9 @@ bool BluetoothTaskManagerWin::DiscoverLowEnergyDeviceServices( std::string error; ScopedVector<win::BluetoothLowEnergyServiceInfo> services; - bool success = win::EnumerateKnownBluetoothLowEnergyServices( - device_path, &services, &error); + bool success = win::BluetoothLowEnergyWrapper::GetInstance() + ->EnumerateKnownBluetoothLowEnergyServices( + device_path, &services, &error); if (!success) { LogPollingError(error.c_str(), 0); return false; @@ -669,8 +672,9 @@ bool BluetoothTaskManagerWin::SearchForGattServiceDevicePaths( // List all known GATT service devices on the machine. ScopedVector<win::BluetoothLowEnergyDeviceInfo> gatt_service_devices; - bool success = win::EnumerateKnownBluetoothLowEnergyGattServiceDevices( - &gatt_service_devices, &error); + bool success = win::BluetoothLowEnergyWrapper::GetInstance() + ->EnumerateKnownBluetoothLowEnergyGattServiceDevices( + &gatt_service_devices, &error); if (!success) { LogPollingError(error.c_str(), 0); return false; @@ -685,8 +689,9 @@ bool BluetoothTaskManagerWin::SearchForGattServiceDevicePaths( // Discover this service device's contained services. ScopedVector<win::BluetoothLowEnergyServiceInfo> gatt_services; - if (!win::EnumerateKnownBluetoothLowEnergyServices( - gatt_service_device->path, &gatt_services, &error)) { + if (!win::BluetoothLowEnergyWrapper::GetInstance() + ->EnumerateKnownBluetoothLowEnergyServices( + gatt_service_device->path, &gatt_services, &error)) { LogPollingError(error.c_str(), 0); continue; } diff --git a/device/device_tests.gyp b/device/device_tests.gyp index 9112d40..c78f4eb 100644 --- a/device/device_tests.gyp +++ b/device/device_tests.gyp @@ -175,6 +175,12 @@ 'battery/battery_status_manager_linux_unittest.cc', ], }], + ['OS=="win"', { + 'sources': [ + 'bluetooth/bluetooth_low_energy_win_fake.h', + 'bluetooth/bluetooth_low_energy_win_fake.cc', + ], + }], ], }, ], |