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) {
return;
}
// TODO(crbug.com/870192): Return the state based on the adapter's state.
std::move(callback).Run(State::kPoweredOff);
auto* properties =
GetBluetoothAdapterClient()->GetProperties(object_paths[0]);
std::move(callback).Run(properties->powered.value() ? State::kPoweredOn
: State::kPoweredOff);
}
bluez::BluetoothAdapterClient* BluetoothSystem::GetBluetoothAdapterClient() {
......
......@@ -19,6 +19,10 @@
namespace device {
constexpr const char kFooObjectPathStr[] = "fake/hci0";
namespace {
// Exposes high-level methods to simulate Bluetooth events e.g. a new adapter
// was added, adapter power state changed, etc.
//
......@@ -60,14 +64,26 @@ class DEVICE_BLUETOOTH_EXPORT TestBluetoothAdapterClient
TestBluetoothAdapterClient() = default;
~TestBluetoothAdapterClient() override = default;
// Simulates a new adapter being added to the system.
void SimulateAdapterAdded() {
static int next_adapter_index = 0;
dbus::ObjectPath adapter_object_path(
base::StringPrintf("fake/hci%d", next_adapter_index++));
// Simulates a new adapter with |object_path_str|. Its properties are empty,
// 0, or false.
void SimulateAdapterAdded(const std::string& object_path_str) {
dbus::ObjectPath object_path(object_path_str);
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));
adapter_object_paths_.push_back(adapter_object_path);
// Simulates adapter at |object_path_str| changing its powered state to
// |powered|.
void SimulateAdapterPowerStateChanged(const std::string& object_path_str,
bool powered) {
GetProperties(dbus::ObjectPath(object_path_str))
->powered.ReplaceValue(powered);
}
// BluetoothAdapterClient:
......@@ -83,13 +99,19 @@ class DEVICE_BLUETOOTH_EXPORT TestBluetoothAdapterClient
}
std::vector<dbus::ObjectPath> GetAdapters() override {
return std::vector<dbus::ObjectPath>(adapter_object_paths_.begin(),
adapter_object_paths_.end());
std::vector<dbus::ObjectPath> object_paths;
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 {
NOTIMPLEMENTED();
return nullptr;
auto it = adapter_object_paths_to_properties_.find(object_path);
if (it == adapter_object_paths_to_properties_.end())
return nullptr;
return &(it->second);
}
void StartDiscovery(const dbus::ObjectPath& object_path,
......@@ -145,11 +167,21 @@ class DEVICE_BLUETOOTH_EXPORT TestBluetoothAdapterClient
}
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_;
};
} // namespace
class BluetoothSystemTest : public DeviceServiceTestBase,
public mojom::BluetoothSystemClient {
public:
......@@ -231,9 +263,10 @@ TEST_F(BluetoothSystemTest, GetState_NoAdapter) {
}
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;
system->GetState(base::BindOnce(&BluetoothSystemTest::StateCallback,
......@@ -244,4 +277,20 @@ TEST_F(BluetoothSystemTest, GetState_PoweredOffAdapter) {
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
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