Commit 6d46cb28 authored by Giovanni Ortuño Urquidi's avatar Giovanni Ortuño Urquidi Committed by Commit Bot

bluetooth: Finish implementing GetState()

Use Adapter's power state for GetState().

Bug: 870192
Change-Id: I4c431fad09e540e8f20b21feb638ae6ba701f637
Reviewed-on: https://chromium-review.googlesource.com/c/1250581
Commit-Queue: Giovanni Ortuño Urquidi <ortuno@chromium.org>
Reviewed-by: default avatarReilly Grant <reillyg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#596532}
parent 4c3a22a6
...@@ -35,8 +35,10 @@ void BluetoothSystem::GetState(GetStateCallback callback) { ...@@ -35,8 +35,10 @@ void BluetoothSystem::GetState(GetStateCallback callback) {
return; return;
} }
// TODO(crbug.com/870192): Return the state based on the adapter's state. auto* properties =
std::move(callback).Run(State::kPoweredOff); GetBluetoothAdapterClient()->GetProperties(object_paths[0]);
std::move(callback).Run(properties->powered.value() ? State::kPoweredOn
: State::kPoweredOff);
} }
bluez::BluetoothAdapterClient* BluetoothSystem::GetBluetoothAdapterClient() { bluez::BluetoothAdapterClient* BluetoothSystem::GetBluetoothAdapterClient() {
......
...@@ -19,6 +19,10 @@ ...@@ -19,6 +19,10 @@
namespace device { namespace device {
constexpr const char kFooObjectPathStr[] = "fake/hci0";
namespace {
// Exposes high-level methods to simulate Bluetooth events e.g. a new adapter // Exposes high-level methods to simulate Bluetooth events e.g. a new adapter
// was added, adapter power state changed, etc. // was added, adapter power state changed, etc.
// //
...@@ -60,14 +64,26 @@ class DEVICE_BLUETOOTH_EXPORT TestBluetoothAdapterClient ...@@ -60,14 +64,26 @@ class DEVICE_BLUETOOTH_EXPORT TestBluetoothAdapterClient
TestBluetoothAdapterClient() = default; TestBluetoothAdapterClient() = default;
~TestBluetoothAdapterClient() override = default; ~TestBluetoothAdapterClient() override = default;
// Simulates a new adapter being added to the system. // Simulates a new adapter with |object_path_str|. Its properties are empty,
void SimulateAdapterAdded() { // 0, or false.
static int next_adapter_index = 0; void SimulateAdapterAdded(const std::string& object_path_str) {
dbus::ObjectPath adapter_object_path( dbus::ObjectPath object_path(object_path_str);
base::StringPrintf("fake/hci%d", next_adapter_index++));
ObjectPathToProperties::iterator it;
bool was_inserted;
std::tie(it, was_inserted) = adapter_object_paths_to_properties_.emplace(
object_path,
base::BindRepeating(&TestBluetoothAdapterClient::OnPropertyChanged,
base::Unretained(this), object_path));
DCHECK(was_inserted);
}
DCHECK(!base::ContainsValue(adapter_object_paths_, adapter_object_path)); // Simulates adapter at |object_path_str| changing its powered state to
adapter_object_paths_.push_back(adapter_object_path); // |powered|.
void SimulateAdapterPowerStateChanged(const std::string& object_path_str,
bool powered) {
GetProperties(dbus::ObjectPath(object_path_str))
->powered.ReplaceValue(powered);
} }
// BluetoothAdapterClient: // BluetoothAdapterClient:
...@@ -83,13 +99,19 @@ class DEVICE_BLUETOOTH_EXPORT TestBluetoothAdapterClient ...@@ -83,13 +99,19 @@ class DEVICE_BLUETOOTH_EXPORT TestBluetoothAdapterClient
} }
std::vector<dbus::ObjectPath> GetAdapters() override { std::vector<dbus::ObjectPath> GetAdapters() override {
return std::vector<dbus::ObjectPath>(adapter_object_paths_.begin(), std::vector<dbus::ObjectPath> object_paths;
adapter_object_paths_.end()); for (const auto& object_path_to_property :
adapter_object_paths_to_properties_) {
object_paths.push_back(object_path_to_property.first);
}
return object_paths;
} }
Properties* GetProperties(const dbus::ObjectPath& object_path) override { Properties* GetProperties(const dbus::ObjectPath& object_path) override {
NOTIMPLEMENTED(); auto it = adapter_object_paths_to_properties_.find(object_path);
if (it == adapter_object_paths_to_properties_.end())
return nullptr; return nullptr;
return &(it->second);
} }
void StartDiscovery(const dbus::ObjectPath& object_path, void StartDiscovery(const dbus::ObjectPath& object_path,
...@@ -145,11 +167,21 @@ class DEVICE_BLUETOOTH_EXPORT TestBluetoothAdapterClient ...@@ -145,11 +167,21 @@ class DEVICE_BLUETOOTH_EXPORT TestBluetoothAdapterClient
} }
private: private:
std::vector<dbus::ObjectPath> adapter_object_paths_; void OnPropertyChanged(const dbus::ObjectPath& object_path,
const std::string& property_name) {
for (auto& observer : observers_) {
observer.AdapterPropertyChanged(object_path, property_name);
}
}
using ObjectPathToProperties = std::map<dbus::ObjectPath, Properties>;
ObjectPathToProperties adapter_object_paths_to_properties_;
base::ObserverList<Observer>::Unchecked observers_; base::ObserverList<Observer>::Unchecked observers_;
}; };
} // namespace
class BluetoothSystemTest : public DeviceServiceTestBase, class BluetoothSystemTest : public DeviceServiceTestBase,
public mojom::BluetoothSystemClient { public mojom::BluetoothSystemClient {
public: public:
...@@ -231,9 +263,10 @@ TEST_F(BluetoothSystemTest, GetState_NoAdapter) { ...@@ -231,9 +263,10 @@ TEST_F(BluetoothSystemTest, GetState_NoAdapter) {
} }
TEST_F(BluetoothSystemTest, GetState_PoweredOffAdapter) { TEST_F(BluetoothSystemTest, GetState_PoweredOffAdapter) {
auto system = CreateBluetoothSystem(); test_bluetooth_adapter_client_->SimulateAdapterAdded(kFooObjectPathStr);
// Added adapters are Off by default.
test_bluetooth_adapter_client_->SimulateAdapterAdded(); auto system = CreateBluetoothSystem();
base::RunLoop run_loop; base::RunLoop run_loop;
system->GetState(base::BindOnce(&BluetoothSystemTest::StateCallback, system->GetState(base::BindOnce(&BluetoothSystemTest::StateCallback,
...@@ -244,4 +277,20 @@ TEST_F(BluetoothSystemTest, GetState_PoweredOffAdapter) { ...@@ -244,4 +277,20 @@ TEST_F(BluetoothSystemTest, GetState_PoweredOffAdapter) {
EXPECT_EQ(mojom::BluetoothSystem::State::kPoweredOff, last_state_.value()); EXPECT_EQ(mojom::BluetoothSystem::State::kPoweredOff, last_state_.value());
} }
TEST_F(BluetoothSystemTest, GetState_PoweredOnAdapter) {
test_bluetooth_adapter_client_->SimulateAdapterAdded(kFooObjectPathStr);
test_bluetooth_adapter_client_->SimulateAdapterPowerStateChanged(
kFooObjectPathStr, true);
auto system = CreateBluetoothSystem();
base::RunLoop run_loop;
system->GetState(base::BindOnce(&BluetoothSystemTest::StateCallback,
base::Unretained(this),
run_loop.QuitClosure()));
run_loop.Run();
EXPECT_EQ(mojom::BluetoothSystem::State::kPoweredOn, last_state_.value());
}
} // namespace device } // namespace device
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