Commit c1fc5428 authored by Jan Wilken Doerrie's avatar Jan Wilken Doerrie Committed by Commit Bot

[Bluetooth] Implement BluetoothAdapter::CanPower()

This change adds a CanPower() method to BluetoothAdapter. By default it
simply delegates to IsPresent(), but it is overriden for WinRT, where we
can be in a state where we were able to get accesss to the adapter, but
not the underlying radio.

Bug: 821766
Change-Id: I46544d542b49a73f7876a10daccb6d78c492b36c
Reviewed-on: https://chromium-review.googlesource.com/1189903
Commit-Queue: Jan Wilken Dörrie <jdoerrie@chromium.org>
Reviewed-by: default avatarGiovanni Ortuño Urquidi <ortuno@chromium.org>
Cr-Commit-Position: refs/heads/master@{#586248}
parent f8cd6a02
...@@ -58,6 +58,10 @@ bool BluetoothAdapter::HasObserver(BluetoothAdapter::Observer* observer) { ...@@ -58,6 +58,10 @@ bool BluetoothAdapter::HasObserver(BluetoothAdapter::Observer* observer) {
return observers_.HasObserver(observer); return observers_.HasObserver(observer);
} }
bool BluetoothAdapter::CanPower() const {
return IsPresent();
}
void BluetoothAdapter::SetPowered(bool powered, void BluetoothAdapter::SetPowered(bool powered,
const base::Closure& callback, const base::Closure& callback,
const ErrorCallback& error_callback) { const ErrorCallback& error_callback) {
......
...@@ -343,6 +343,11 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothAdapter ...@@ -343,6 +343,11 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothAdapter
// is only considered present if the address has been obtained. // is only considered present if the address has been obtained.
virtual bool IsPresent() const = 0; virtual bool IsPresent() const = 0;
// Indicates whether the adapter radio can be powered. Defaults to
// IsPresent(). Currently only overridden on Windows, where the adapter can be
// present, but we might fail to get access to the underlying radio.
virtual bool CanPower() const;
// Indicates whether the adapter radio is powered. // Indicates whether the adapter radio is powered.
virtual bool IsPowered() const = 0; virtual bool IsPowered() const = 0;
......
...@@ -514,12 +514,31 @@ TEST_F(BluetoothTest, MAYBE_ConstructFakeAdapter) { ...@@ -514,12 +514,31 @@ TEST_F(BluetoothTest, MAYBE_ConstructFakeAdapter) {
InitWithFakeAdapter(); InitWithFakeAdapter();
EXPECT_EQ(adapter_->GetAddress(), kTestAdapterAddress); EXPECT_EQ(adapter_->GetAddress(), kTestAdapterAddress);
EXPECT_EQ(adapter_->GetName(), kTestAdapterName); EXPECT_EQ(adapter_->GetName(), kTestAdapterName);
EXPECT_TRUE(adapter_->CanPower());
EXPECT_TRUE(adapter_->IsPresent()); EXPECT_TRUE(adapter_->IsPresent());
EXPECT_TRUE(adapter_->IsPowered()); EXPECT_TRUE(adapter_->IsPowered());
EXPECT_FALSE(adapter_->IsDiscoverable()); EXPECT_FALSE(adapter_->IsDiscoverable());
EXPECT_FALSE(adapter_->IsDiscovering()); EXPECT_FALSE(adapter_->IsDiscovering());
} }
#if defined(OS_WIN)
TEST_P(BluetoothTestWinrtOnly, ConstructFakeAdapterWithoutRadio) {
if (!PlatformSupportsLowEnergy()) {
LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
return;
}
InitFakeAdapterWithoutRadio();
EXPECT_EQ(adapter_->GetAddress(), kTestAdapterAddress);
EXPECT_EQ(adapter_->GetName(), kTestAdapterName);
EXPECT_TRUE(adapter_->IsPresent());
EXPECT_FALSE(adapter_->CanPower());
EXPECT_FALSE(adapter_->IsPowered());
EXPECT_FALSE(adapter_->IsDiscoverable());
EXPECT_FALSE(adapter_->IsDiscovering());
}
#endif // defined(OS_WIN)
// TODO(scheib): Enable BluetoothTest fixture tests on all platforms. // TODO(scheib): Enable BluetoothTest fixture tests on all platforms.
#if defined(OS_ANDROID) #if defined(OS_ANDROID)
#define MAYBE_DiscoverySession DiscoverySession #define MAYBE_DiscoverySession DiscoverySession
......
...@@ -465,6 +465,10 @@ bool BluetoothAdapterWinrt::IsPresent() const { ...@@ -465,6 +465,10 @@ bool BluetoothAdapterWinrt::IsPresent() const {
return adapter_ != nullptr; return adapter_ != nullptr;
} }
bool BluetoothAdapterWinrt::CanPower() const {
return radio_ != nullptr;
}
bool BluetoothAdapterWinrt::IsPowered() const { bool BluetoothAdapterWinrt::IsPowered() const {
// Due to an issue on WoW64 we might fail to obtain the radio in OnGetRadio(). // Due to an issue on WoW64 we might fail to obtain the radio in OnGetRadio().
// This is why it can be null here. // This is why it can be null here.
......
...@@ -37,6 +37,7 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothAdapterWinrt : public BluetoothAdapter { ...@@ -37,6 +37,7 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothAdapterWinrt : public BluetoothAdapter {
const ErrorCallback& error_callback) override; const ErrorCallback& error_callback) override;
bool IsInitialized() const override; bool IsInitialized() const override;
bool IsPresent() const override; bool IsPresent() const override;
bool CanPower() const override;
bool IsPowered() const override; bool IsPowered() const override;
bool IsDiscoverable() const override; bool IsDiscoverable() const override;
void SetDiscoverable(bool discoverable, void SetDiscoverable(bool discoverable,
......
...@@ -163,6 +163,10 @@ class BluetoothTestBase : public testing::Test { ...@@ -163,6 +163,10 @@ class BluetoothTestBase : public testing::Test {
// controlled by this test fixture. // controlled by this test fixture.
virtual void InitWithFakeAdapter() {} virtual void InitWithFakeAdapter() {}
// Similar to InitWithFakeAdapter(), but simulates a state where we fail to
// get access to the underlying radio.
virtual void InitFakeAdapterWithoutRadio() {}
// Configures the fake adapter to lack the necessary permissions to scan for // Configures the fake adapter to lack the necessary permissions to scan for
// devices. Returns false if the current platform always has permission. // devices. Returns false if the current platform always has permission.
virtual bool DenyPermission(); virtual bool DenyPermission();
......
...@@ -42,6 +42,7 @@ ...@@ -42,6 +42,7 @@
#include "device/bluetooth/test/fake_device_information_winrt.h" #include "device/bluetooth/test/fake_device_information_winrt.h"
#include "device/bluetooth/test/fake_gatt_characteristic_winrt.h" #include "device/bluetooth/test/fake_gatt_characteristic_winrt.h"
#include "device/bluetooth/test/fake_gatt_descriptor_winrt.h" #include "device/bluetooth/test/fake_gatt_descriptor_winrt.h"
#include "device/bluetooth/test/fake_radio_winrt.h"
// Note: As UWP does not provide int specializations for IObservableVector and // Note: As UWP does not provide int specializations for IObservableVector and
// VectorChangedEventHandler we need to supply our own. UUIDs were generated // VectorChangedEventHandler we need to supply our own. UUIDs were generated
...@@ -655,7 +656,17 @@ void BluetoothTestWinrt::InitWithFakeAdapter() { ...@@ -655,7 +656,17 @@ void BluetoothTestWinrt::InitWithFakeAdapter() {
base::RunLoop run_loop; base::RunLoop run_loop;
adapter_ = base::MakeRefCounted<TestBluetoothAdapterWinrt>( adapter_ = base::MakeRefCounted<TestBluetoothAdapterWinrt>(
Make<FakeBluetoothAdapterWinrt>(kTestAdapterAddress), Make<FakeBluetoothAdapterWinrt>(kTestAdapterAddress,
Make<FakeRadioWinrt>()),
Make<FakeDeviceInformationWinrt>(kTestAdapterName),
run_loop.QuitClosure(), this);
run_loop.Run();
}
void BluetoothTestWinrt::InitFakeAdapterWithoutRadio() {
base::RunLoop run_loop;
adapter_ = base::MakeRefCounted<TestBluetoothAdapterWinrt>(
Make<FakeBluetoothAdapterWinrt>(kTestAdapterAddress, nullptr /* radio */),
Make<FakeDeviceInformationWinrt>(kTestAdapterName), Make<FakeDeviceInformationWinrt>(kTestAdapterName),
run_loop.QuitClosure(), this); run_loop.QuitClosure(), this);
run_loop.Run(); run_loop.Run();
......
...@@ -121,6 +121,7 @@ class BluetoothTestWinrt : public BluetoothTestWin, ...@@ -121,6 +121,7 @@ class BluetoothTestWinrt : public BluetoothTestWin,
void InitWithDefaultAdapter() override; void InitWithDefaultAdapter() override;
void InitWithoutDefaultAdapter() override; void InitWithoutDefaultAdapter() override;
void InitWithFakeAdapter() override; void InitWithFakeAdapter() override;
void InitFakeAdapterWithoutRadio() override;
BluetoothDevice* SimulateLowEnergyDevice(int device_ordinal) override; BluetoothDevice* SimulateLowEnergyDevice(int device_ordinal) override;
void SimulateDevicePaired(BluetoothDevice* device, bool is_paired) override; void SimulateDevicePaired(BluetoothDevice* device, bool is_paired) override;
void SimulatePairingPinCode(BluetoothDevice* device, void SimulatePairingPinCode(BluetoothDevice* device,
......
...@@ -33,8 +33,10 @@ using Microsoft::WRL::Make; ...@@ -33,8 +33,10 @@ using Microsoft::WRL::Make;
} // namespace } // namespace
FakeBluetoothAdapterWinrt::FakeBluetoothAdapterWinrt(base::StringPiece address) FakeBluetoothAdapterWinrt::FakeBluetoothAdapterWinrt(
: raw_address_(ToRawBluetoothAddress(address)) {} base::StringPiece address,
Microsoft::WRL::ComPtr<ABI::Windows::Devices::Radios::IRadio> radio)
: raw_address_(ToRawBluetoothAddress(address)), radio_(std::move(radio)) {}
FakeBluetoothAdapterWinrt::~FakeBluetoothAdapterWinrt() = default; FakeBluetoothAdapterWinrt::~FakeBluetoothAdapterWinrt() = default;
......
...@@ -14,7 +14,6 @@ ...@@ -14,7 +14,6 @@
#include "base/macros.h" #include "base/macros.h"
#include "base/strings/string_piece_forward.h" #include "base/strings/string_piece_forward.h"
#include "device/bluetooth/test/fake_radio_winrt.h"
namespace device { namespace device {
...@@ -24,7 +23,9 @@ class FakeBluetoothAdapterWinrt ...@@ -24,7 +23,9 @@ class FakeBluetoothAdapterWinrt
Microsoft::WRL::WinRt | Microsoft::WRL::InhibitRoOriginateError>, Microsoft::WRL::WinRt | Microsoft::WRL::InhibitRoOriginateError>,
ABI::Windows::Devices::Bluetooth::IBluetoothAdapter> { ABI::Windows::Devices::Bluetooth::IBluetoothAdapter> {
public: public:
explicit FakeBluetoothAdapterWinrt(base::StringPiece address); FakeBluetoothAdapterWinrt(
base::StringPiece address,
Microsoft::WRL::ComPtr<ABI::Windows::Devices::Radios::IRadio> radio);
~FakeBluetoothAdapterWinrt() override; ~FakeBluetoothAdapterWinrt() override;
static uint64_t ToRawBluetoothAddress(base::StringPiece address); static uint64_t ToRawBluetoothAddress(base::StringPiece address);
...@@ -45,8 +46,7 @@ class FakeBluetoothAdapterWinrt ...@@ -45,8 +46,7 @@ class FakeBluetoothAdapterWinrt
private: private:
uint64_t raw_address_; uint64_t raw_address_;
Microsoft::WRL::ComPtr<ABI::Windows::Devices::Radios::IRadio> radio_ = Microsoft::WRL::ComPtr<ABI::Windows::Devices::Radios::IRadio> radio_;
Microsoft::WRL::Make<FakeRadioWinrt>();
DISALLOW_COPY_AND_ASSIGN(FakeBluetoothAdapterWinrt); DISALLOW_COPY_AND_ASSIGN(FakeBluetoothAdapterWinrt);
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment