Commit 328cc054 authored by Giovanni Ortuño Urquidi's avatar Giovanni Ortuño Urquidi Committed by Commit Bot

bluetooth: Change state to kTransitioning when SetPowered gets called

Bug: 896113
Change-Id: I60f8e082a01dd1a7007004ae26ff275c87eaa8bf
Reviewed-on: https://chromium-review.googlesource.com/c/1290576Reviewed-by: default avatarDominick Ng <dominickn@chromium.org>
Reviewed-by: default avatarOvidio Henriquez <odejesush@chromium.org>
Commit-Queue: Giovanni Ortuño Urquidi <ortuno@chromium.org>
Cr-Commit-Position: refs/heads/master@{#601462}
parent de283c85
...@@ -103,6 +103,13 @@ void BluetoothSystem::SetPowered(bool powered, SetPoweredCallback callback) { ...@@ -103,6 +103,13 @@ void BluetoothSystem::SetPowered(bool powered, SetPoweredCallback callback) {
return; return;
} }
// Update the BluetoothSystem state to kTransitioning if a previous call to
// SetPowered() has not done so already.
if (state_ != State::kTransitioning) {
state_ = State::kTransitioning;
client_ptr_->OnStateChanged(state_);
}
GetBluetoothAdapterClient() GetBluetoothAdapterClient()
->GetProperties(active_adapter_.value()) ->GetProperties(active_adapter_.value())
->powered.Set(powered, ->powered.Set(powered,
...@@ -134,6 +141,13 @@ void BluetoothSystem::UpdateStateAndNotifyIfNecessary() { ...@@ -134,6 +141,13 @@ void BluetoothSystem::UpdateStateAndNotifyIfNecessary() {
void BluetoothSystem::OnSetPoweredFinished(SetPoweredCallback callback, void BluetoothSystem::OnSetPoweredFinished(SetPoweredCallback callback,
bool succeeded) { bool succeeded) {
if (!succeeded) {
// We change |state_| to `kTransitioning` before trying to set 'powered'. If
// the call to set 'powered' fails, then we need to change it back to
// `kPoweredOn` or `kPoweredOff` depending on the `active_adapter_` state.
UpdateStateAndNotifyIfNecessary();
}
std::move(callback).Run(succeeded ? SetPoweredResult::kSuccess std::move(callback).Run(succeeded ? SetPoweredResult::kSuccess
: SetPoweredResult::kFailedUnknownReason); : SetPoweredResult::kFailedUnknownReason);
} }
......
...@@ -606,16 +606,18 @@ TEST_F(BluetoothSystemTest, SetPoweredOff_SucceedsAdapterInitiallyOn) { ...@@ -606,16 +606,18 @@ TEST_F(BluetoothSystemTest, SetPoweredOff_SucceedsAdapterInitiallyOn) {
SetPoweredAndWait(system, false)); SetPoweredAndWait(system, false));
EXPECT_EQ(1u, test_bluetooth_adapter_client_->GetSetPoweredCallCount( EXPECT_EQ(1u, test_bluetooth_adapter_client_->GetSetPoweredCallCount(
kFooObjectPathStr)); kFooObjectPathStr));
// TODO(ortuno): Change to kTransitioning once implemented. EXPECT_EQ(mojom::BluetoothSystem::State::kTransitioning,
EXPECT_EQ(mojom::BluetoothSystem::State::kPoweredOn, GetStateAndWait(system)); GetStateAndWait(system));
EXPECT_TRUE(on_state_changed_states_.empty()); EXPECT_EQ(StateVector({mojom::BluetoothSystem::State::kTransitioning}),
on_state_changed_states_);
test_bluetooth_adapter_client_->SimulateSetPoweredCompleted( test_bluetooth_adapter_client_->SimulateSetPoweredCompleted(
kFooObjectPathStr); kFooObjectPathStr);
EXPECT_EQ(mojom::BluetoothSystem::State::kPoweredOff, EXPECT_EQ(mojom::BluetoothSystem::State::kPoweredOff,
GetStateAndWait(system)); GetStateAndWait(system));
EXPECT_EQ(StateVector({mojom::BluetoothSystem::State::kPoweredOff}), EXPECT_EQ(StateVector({mojom::BluetoothSystem::State::kTransitioning,
mojom::BluetoothSystem::State::kPoweredOff}),
on_state_changed_states_); on_state_changed_states_);
} }
...@@ -632,16 +634,17 @@ TEST_F(BluetoothSystemTest, SetPoweredOn_SucceedsAdapterInitiallyOff) { ...@@ -632,16 +634,17 @@ TEST_F(BluetoothSystemTest, SetPoweredOn_SucceedsAdapterInitiallyOff) {
SetPoweredAndWait(system, true)); SetPoweredAndWait(system, true));
EXPECT_EQ(1u, test_bluetooth_adapter_client_->GetSetPoweredCallCount( EXPECT_EQ(1u, test_bluetooth_adapter_client_->GetSetPoweredCallCount(
kFooObjectPathStr)); kFooObjectPathStr));
// TODO(ortuno): Change to kTransitioning once implemented. EXPECT_EQ(mojom::BluetoothSystem::State::kTransitioning,
EXPECT_EQ(mojom::BluetoothSystem::State::kPoweredOff,
GetStateAndWait(system)); GetStateAndWait(system));
EXPECT_TRUE(on_state_changed_states_.empty()); EXPECT_EQ(StateVector({mojom::BluetoothSystem::State::kTransitioning}),
on_state_changed_states_);
test_bluetooth_adapter_client_->SimulateSetPoweredCompleted( test_bluetooth_adapter_client_->SimulateSetPoweredCompleted(
kFooObjectPathStr); kFooObjectPathStr);
EXPECT_EQ(mojom::BluetoothSystem::State::kPoweredOn, GetStateAndWait(system)); EXPECT_EQ(mojom::BluetoothSystem::State::kPoweredOn, GetStateAndWait(system));
EXPECT_EQ(StateVector({mojom::BluetoothSystem::State::kPoweredOn}), EXPECT_EQ(StateVector({mojom::BluetoothSystem::State::kTransitioning,
mojom::BluetoothSystem::State::kPoweredOn}),
on_state_changed_states_); on_state_changed_states_);
} }
...@@ -661,9 +664,9 @@ TEST_F(BluetoothSystemTest, SetPoweredOff_FailsAdapterInitiallyOn) { ...@@ -661,9 +664,9 @@ TEST_F(BluetoothSystemTest, SetPoweredOff_FailsAdapterInitiallyOn) {
EXPECT_EQ(1u, test_bluetooth_adapter_client_->GetSetPoweredCallCount( EXPECT_EQ(1u, test_bluetooth_adapter_client_->GetSetPoweredCallCount(
kFooObjectPathStr)); kFooObjectPathStr));
EXPECT_EQ(mojom::BluetoothSystem::State::kPoweredOn, GetStateAndWait(system)); EXPECT_EQ(mojom::BluetoothSystem::State::kPoweredOn, GetStateAndWait(system));
// TODO(ortuno): Test that the state change to kTransitioning and then back EXPECT_EQ(StateVector({mojom::BluetoothSystem::State::kTransitioning,
// to kPoweredOff after the call failed. mojom::BluetoothSystem::State::kPoweredOn}),
EXPECT_TRUE(on_state_changed_states_.empty()); on_state_changed_states_);
} }
// Tests failing to set powered to "On" when the adapter is "Off". // Tests failing to set powered to "On" when the adapter is "Off".
...@@ -681,9 +684,9 @@ TEST_F(BluetoothSystemTest, SetPoweredOn_FailsAdapterInitiallyOff) { ...@@ -681,9 +684,9 @@ TEST_F(BluetoothSystemTest, SetPoweredOn_FailsAdapterInitiallyOff) {
kFooObjectPathStr)); kFooObjectPathStr));
EXPECT_EQ(mojom::BluetoothSystem::State::kPoweredOff, EXPECT_EQ(mojom::BluetoothSystem::State::kPoweredOff,
GetStateAndWait(system)); GetStateAndWait(system));
// TODO(ortuno): Test that the state change to kTransitioning and then back EXPECT_EQ(StateVector({mojom::BluetoothSystem::State::kTransitioning,
// to kPoweredOff after the call failed. mojom::BluetoothSystem::State::kPoweredOff}),
EXPECT_TRUE(on_state_changed_states_.empty()); on_state_changed_states_);
} }
} // namespace device } // namespace device
...@@ -48,8 +48,9 @@ interface BluetoothSystem { ...@@ -48,8 +48,9 @@ interface BluetoothSystem {
// Attempts to change the state of the Bluetooth to `kPoweredOn` if |powered| // Attempts to change the state of the Bluetooth to `kPoweredOn` if |powered|
// and `kPoweredOff` otherwise . Callback is run with `kSuccess if the command // and `kPoweredOff` otherwise . Callback is run with `kSuccess if the command
// was successfully sent to the BT Radio. Once the BT radio actually changes // was successfully sent to the BT Radio. The state immediately changes to
// state BluetoothSystemClient::OnStateChanged will be called. // kTransitioning and once the BT radio actually changes state
// BluetoothSystemClient::OnStateChanged will be called.
// TODO(https://crbug.com/896113): This function is missing two features: // TODO(https://crbug.com/896113): This function is missing two features:
// 1. The new state should be saved in the user's pref so that the next time // 1. The new state should be saved in the user's pref so that the next time
// the machine turns off the state matches the user pref. // the machine turns off the state matches the user pref.
......
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