Commit 06b85d85 authored by Meilin Wang's avatar Meilin Wang Committed by Commit Bot

ambient: Fix screen goes off when battery is full.

Bug: b/160202872
Test: unittest.
Change-Id: I355c704fd2adda7d96dfbb3c972964e2da7a1e6a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2275342
Commit-Queue: Meilin Wang <meilinw@chromium.org>
Reviewed-by: default avatarXiaohui Chen <xiaohuic@chromium.org>
Cr-Commit-Position: refs/heads/master@{#784515}
parent 70a9b218
...@@ -80,6 +80,13 @@ std::string GetWidgetName() { ...@@ -80,6 +80,13 @@ std::string GetWidgetName() {
return "InSessionAmbientModeContainer"; return "InSessionAmbientModeContainer";
} }
// Returns true if the device is currently connected to a charger.
bool IsChargerConnected() {
return (PowerStatus::Get()->IsBatteryCharging() ||
PowerStatus::Get()->IsBatteryFull()) &&
PowerStatus::Get()->IsLinePowerConnected();
}
} // namespace } // namespace
// AmbientController::InactivityMonitor---------------------------------- // AmbientController::InactivityMonitor----------------------------------
...@@ -168,9 +175,8 @@ void AmbientController::OnAmbientUiVisibilityChanged( ...@@ -168,9 +175,8 @@ void AmbientController::OnAmbientUiVisibilityChanged(
// This will be no-op if the view is already visible. // This will be no-op if the view is already visible.
container_view_->SetVisible(true); container_view_->SetVisible(true);
if (PowerStatus::Get()->IsBatteryCharging()) { if (IsChargerConnected()) {
// Requires wake lock to prevent display from sleeping when the battery // Requires wake lock to prevent display from sleeping.
// is charging.
AcquireWakeLock(); AcquireWakeLock();
} }
// Observes the |PowerStatus| on the battery charging status change for // Observes the |PowerStatus| on the battery charging status change for
...@@ -262,7 +268,7 @@ void AmbientController::OnPowerStatusChanged() { ...@@ -262,7 +268,7 @@ void AmbientController::OnPowerStatusChanged() {
return; return;
} }
if (PowerStatus::Get()->IsBatteryCharging()) { if (IsChargerConnected()) {
AcquireWakeLock(); AcquireWakeLock();
} else { } else {
ReleaseWakeLock(); ReleaseWakeLock();
......
...@@ -180,7 +180,7 @@ TEST_F(AmbientControllerTest, ...@@ -180,7 +180,7 @@ TEST_F(AmbientControllerTest,
} }
TEST_F(AmbientControllerTest, TEST_F(AmbientControllerTest,
CheckAcquireAndReleaseWakeLockWhenBatteryChargingStateChanged) { CheckAcquireAndReleaseWakeLockWhenBatteryStateChanged) {
// Flush the loop first to ensure the |PowerStatus| has picked up the initial // Flush the loop first to ensure the |PowerStatus| has picked up the initial
// status. // status.
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
...@@ -189,6 +189,8 @@ TEST_F(AmbientControllerTest, ...@@ -189,6 +189,8 @@ TEST_F(AmbientControllerTest,
power_manager::PowerSupplyProperties proto; power_manager::PowerSupplyProperties proto;
proto.set_battery_state( proto.set_battery_state(
power_manager::PowerSupplyProperties_BatteryState_DISCHARGING); power_manager::PowerSupplyProperties_BatteryState_DISCHARGING);
proto.set_external_power(
power_manager::PowerSupplyProperties_ExternalPower_DISCONNECTED);
PowerStatus::Get()->SetProtoForTesting(proto); PowerStatus::Get()->SetProtoForTesting(proto);
// Lock screen to start ambient mode. // Lock screen to start ambient mode.
LockScreen(); LockScreen();
...@@ -200,6 +202,8 @@ TEST_F(AmbientControllerTest, ...@@ -200,6 +202,8 @@ TEST_F(AmbientControllerTest,
// Connect the device with a charger. // Connect the device with a charger.
proto.set_battery_state( proto.set_battery_state(
power_manager::PowerSupplyProperties_BatteryState_CHARGING); power_manager::PowerSupplyProperties_BatteryState_CHARGING);
proto.set_external_power(
power_manager::PowerSupplyProperties_ExternalPower_AC);
PowerStatus::Get()->SetProtoForTesting(proto); PowerStatus::Get()->SetProtoForTesting(proto);
// Notify the controller about the power status change, and flush the loop to // Notify the controller about the power status change, and flush the loop to
// ensure the wake lock request has reached the wake lock provider. // ensure the wake lock request has reached the wake lock provider.
...@@ -210,9 +214,23 @@ TEST_F(AmbientControllerTest, ...@@ -210,9 +214,23 @@ TEST_F(AmbientControllerTest,
EXPECT_EQ(1, GetNumOfActiveWakeLocks( EXPECT_EQ(1, GetNumOfActiveWakeLocks(
device::mojom::WakeLockType::kPreventDisplaySleep)); device::mojom::WakeLockType::kPreventDisplaySleep));
// Simulates a full battery.
proto.set_battery_state(
power_manager::PowerSupplyProperties_BatteryState_FULL);
proto.set_external_power(
power_manager::PowerSupplyProperties_ExternalPower_AC);
PowerStatus::Get()->SetProtoForTesting(proto);
ambient_controller()->OnPowerStatusChanged();
// Should keep the wake lock as the charger is still connected.
EXPECT_EQ(1, GetNumOfActiveWakeLocks(
device::mojom::WakeLockType::kPreventDisplaySleep));
// Disconnects the charger again. // Disconnects the charger again.
proto.set_battery_state( proto.set_battery_state(
power_manager::PowerSupplyProperties_BatteryState_DISCHARGING); power_manager::PowerSupplyProperties_BatteryState_DISCHARGING);
proto.set_external_power(
power_manager::PowerSupplyProperties_ExternalPower_DISCONNECTED);
PowerStatus::Get()->SetProtoForTesting(proto); PowerStatus::Get()->SetProtoForTesting(proto);
ambient_controller()->OnPowerStatusChanged(); ambient_controller()->OnPowerStatusChanged();
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
......
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