Commit 88b1b7e7 authored by warx's avatar warx Committed by Commit bot

ash: Add one second grace period For LockScreenIfRequired

Changes:
Add one second grace period For LockScreenIfRequired. Basically, an one second OneShotTimer is used to set/reset the behavior.

BUG=633304
BUG=680509
TEST=device test, and modify test coverage

Review-Url: https://codereview.chromium.org/2620383003
Cr-Commit-Position: refs/heads/master@{#443254}
parent 7a2e011d
......@@ -25,6 +25,9 @@ namespace {
// animation when in tablet mode.
constexpr int kShutdownTimeoutMs = 500;
// Amount of time to delay locking screen after display is forced off.
constexpr int kLockScreenTimeoutMs = 1000;
// Amount of time since last SuspendDone() that power button event needs to be
// ignored.
constexpr int kIgnorePowerButtonAfterResumeMs = 2000;
......@@ -64,6 +67,16 @@ void TabletPowerButtonController::TestApi::TriggerShutdownTimeout() {
controller_->shutdown_timer_.Stop();
}
bool TabletPowerButtonController::TestApi::LockScreenTimerIsRunning() const {
return controller_->lock_screen_timer_.IsRunning();
}
void TabletPowerButtonController::TestApi::TriggerLockScreenTimeout() {
DCHECK(LockScreenTimerIsRunning());
controller_->OnLockScreenTimeout();
controller_->lock_screen_timer_.Stop();
}
TabletPowerButtonController::TabletPowerButtonController(
LockStateController* controller)
: tick_clock_(new base::DefaultTickClock()),
......@@ -108,6 +121,7 @@ void TabletPowerButtonController::OnPowerButtonEvent(
}
screen_off_when_power_button_down_ = brightness_level_is_zero_;
SetDisplayForcedOff(false);
lock_screen_timer_.Stop();
StartShutdownTimer();
} else {
if (shutdown_timer_.IsRunning()) {
......@@ -159,22 +173,27 @@ void TabletPowerButtonController::OnKeyEvent(ui::KeyEvent* event) {
if (event->key_code() == ui::VKEY_POWER)
return;
if (!IsTabletModeActive() && backlights_forced_off_)
if (!IsTabletModeActive() && backlights_forced_off_) {
SetDisplayForcedOff(false);
lock_screen_timer_.Stop();
}
}
void TabletPowerButtonController::OnMouseEvent(ui::MouseEvent* event) {
if (event->flags() & ui::EF_IS_SYNTHESIZED)
return;
if (!IsTabletModeActive() && backlights_forced_off_)
if (!IsTabletModeActive() && backlights_forced_off_) {
SetDisplayForcedOff(false);
lock_screen_timer_.Stop();
}
}
void TabletPowerButtonController::OnStylusStateChanged(ui::StylusState state) {
if (IsTabletModeSupported() && state == ui::StylusState::REMOVED &&
backlights_forced_off_) {
SetDisplayForcedOff(false);
lock_screen_timer_.Stop();
}
}
......@@ -234,8 +253,14 @@ void TabletPowerButtonController::LockScreenIfRequired() {
session_state_delegate->CanLockScreen() &&
!session_state_delegate->IsUserSessionBlocked() &&
!controller_->LockRequested()) {
session_state_delegate->LockScreen();
lock_screen_timer_.Start(
FROM_HERE, base::TimeDelta::FromMilliseconds(kLockScreenTimeoutMs),
this, &TabletPowerButtonController::OnLockScreenTimeout);
}
}
void TabletPowerButtonController::OnLockScreenTimeout() {
WmShell::Get()->GetSessionStateDelegate()->LockScreen();
}
} // namespace ash
......@@ -42,6 +42,12 @@ class ASH_EXPORT TabletPowerButtonController
// Emulates |shutdown_timer_| timeout.
void TriggerShutdownTimeout();
// Returns true when |lock_screen_timer_| is running.
bool LockScreenTimerIsRunning() const;
// Emulates |lock_screen_timer_| timeout.
void TriggerLockScreenTimeout();
private:
TabletPowerButtonController* controller_; // Not owned.
......@@ -101,6 +107,9 @@ class ASH_EXPORT TabletPowerButtonController
// and locking is possible.
void LockScreenIfRequired();
// Called by |lock_screen_timer_| to start locking screen.
void OnLockScreenTimeout();
// True if the brightness level is currently set to off.
bool brightness_level_is_zero_ = false;
......@@ -124,6 +133,10 @@ class ASH_EXPORT TabletPowerButtonController
// released. Runs OnShutdownTimeout() to start shutdown.
base::OneShotTimer shutdown_timer_;
// Used to provide a grace period between forcing the display off and locking
// the screen. Runs OnLockScreenTimeout().
base::OneShotTimer lock_screen_timer_;
LockStateController* controller_; // Not owned.
base::WeakPtrFactory<TabletPowerButtonController> weak_ptr_factory_;
......
......@@ -139,11 +139,15 @@ TEST_F(TabletPowerButtonControllerTest, LockScreenIfRequired) {
// automatic screen-locking was requested.
PressPowerButton();
ReleasePowerButton();
EXPECT_TRUE(test_api_->LockScreenTimerIsRunning());
EXPECT_FALSE(GetLockedState());
test_api_->TriggerLockScreenTimeout();
EXPECT_TRUE(GetLockedState());
// On locked state, power-button-press-release should do nothing.
PressPowerButton();
ReleasePowerButton();
EXPECT_FALSE(test_api_->LockScreenTimerIsRunning());
EXPECT_TRUE(GetLockedState());
// Unlock the sceen.
......@@ -155,6 +159,26 @@ TEST_F(TabletPowerButtonControllerTest, LockScreenIfRequired) {
SetShouldLockScreenAutomatically(false);
PressPowerButton();
ReleasePowerButton();
EXPECT_FALSE(test_api_->LockScreenTimerIsRunning());
EXPECT_FALSE(GetLockedState());
}
// Tests when display is on, quickly tapping power button two times, screen is
// not locked.
TEST_F(TabletPowerButtonControllerTest, LockScreenGracePeriodTest) {
Initialize(LoginStatus::USER);
SetShouldLockScreenAutomatically(true);
EXPECT_FALSE(GetLockedState());
PressPowerButton();
ReleasePowerButton();
power_manager_client_->SendBrightnessChanged(0, false);
EXPECT_TRUE(test_api_->LockScreenTimerIsRunning());
PressPowerButton();
power_manager_client_->SendBrightnessChanged(kNonZeroBrightness, false);
EXPECT_FALSE(test_api_->LockScreenTimerIsRunning());
ReleasePowerButton();
EXPECT_FALSE(test_api_->LockScreenTimerIsRunning());
EXPECT_FALSE(GetLockedState());
}
......
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