Commit 8d2cfc84 authored by Min Chen's avatar Min Chen Committed by Commit Bot

Make PowerButton+VolumeUp can also take screenshot.

Bug: 937907
Change-Id: I2cd5baccd1f5acef58b051937447794eb49b7338
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1607183
Commit-Queue: Min Chen <minch@chromium.org>
Reviewed-by: default avatarDan Erat <derat@chromium.org>
Cr-Commit-Position: refs/heads/master@{#659326}
parent b24394e4
......@@ -50,6 +50,7 @@ bool PowerButtonScreenshotController::OnPowerButtonEvent(
power_button_pressed_ = down;
if (power_button_pressed_) {
volume_down_timer_.Stop();
volume_up_timer_.Stop();
power_button_pressed_time_ = tick_clock_->NowTicks();
if (InterceptScreenshotChord())
return true;
......@@ -71,81 +72,104 @@ void PowerButtonScreenshotController::OnKeyEvent(ui::KeyEvent* event) {
if (key_code != ui::VKEY_VOLUME_DOWN && key_code != ui::VKEY_VOLUME_UP)
return;
if (key_code == ui::VKEY_VOLUME_DOWN) {
if (event->type() == ui::ET_KEY_PRESSED) {
if (!volume_down_key_pressed_) {
const bool is_volume_down = key_code == ui::VKEY_VOLUME_DOWN;
if (event->type() == ui::ET_KEY_PRESSED) {
if (!volume_down_key_pressed_ && !volume_up_key_pressed_) {
if (is_volume_down) {
volume_down_key_pressed_ = true;
volume_down_key_pressed_time_ = tick_clock_->NowTicks();
consume_volume_down_ = false;
InterceptScreenshotChord();
} else {
volume_up_key_pressed_ = true;
volume_up_key_pressed_time_ = tick_clock_->NowTicks();
consume_volume_up_ = false;
InterceptScreenshotChord();
}
// Do not propagate volume down key pressed event if the first
// one is consumed by screenshot.
if (consume_volume_down_)
event->StopPropagation();
} else {
volume_down_key_pressed_ = false;
}
}
if (key_code == ui::VKEY_VOLUME_UP)
volume_up_key_pressed_ = event->type() == ui::ET_KEY_PRESSED;
if (consume_volume_down_ || consume_volume_up_)
event->StopPropagation();
} else {
is_volume_down ? volume_down_key_pressed_ = false
: volume_up_key_pressed_ = false;
}
// When volume key is pressed, cancel the ongoing power button behavior.
if (volume_down_key_pressed_ || volume_up_key_pressed_)
Shell::Get()->power_button_controller()->CancelPowerButtonEvent();
// On volume down key pressed while power button not pressed yet state, do not
// propagate volume down key pressed event for chord delay time. Start the
// timer to wait power button pressed for screenshot operation, and on timeout
// perform the delayed volume down operation.
if (volume_down_key_pressed_ && !power_button_pressed_) {
base::TimeTicks now = tick_clock_->NowTicks();
if (now <= volume_down_key_pressed_time_ + kScreenshotChordDelay) {
event->StopPropagation();
// On volume down/up key pressed while power button not pressed yet state, do
// not propagate volume down/up key pressed event for chord delay time. Start
// the timer to wait power button pressed for screenshot operation, and on
// timeout perform the delayed volume down/up operation.
if (power_button_pressed_)
return;
if (!volume_down_timer_.IsRunning()) {
volume_down_timer_.Start(
FROM_HERE, kScreenshotChordDelay,
base::BindOnce(
&PowerButtonScreenshotController::OnVolumeDownTimeout,
base::Unretained(this), ui::Accelerator(*event)));
}
base::TimeTicks now = tick_clock_->NowTicks();
if (volume_down_key_pressed_ && is_volume_down &&
now <= volume_down_key_pressed_time_ + kScreenshotChordDelay) {
event->StopPropagation();
if (!volume_down_timer_.IsRunning()) {
volume_down_timer_.Start(
FROM_HERE, kScreenshotChordDelay,
base::BindOnce(
&PowerButtonScreenshotController::OnVolumeControlTimeout,
base::Unretained(this), ui::Accelerator(*event), /*down=*/true));
}
} else if (volume_up_key_pressed_ && !is_volume_down &&
now <= volume_up_key_pressed_time_ + kScreenshotChordDelay) {
event->StopPropagation();
if (!volume_up_timer_.IsRunning()) {
volume_up_timer_.Start(
FROM_HERE, kScreenshotChordDelay,
base::BindOnce(
&PowerButtonScreenshotController::OnVolumeControlTimeout,
base::Unretained(this), ui::Accelerator(*event), /*down=*/false));
}
}
}
bool PowerButtonScreenshotController::InterceptScreenshotChord() {
if (volume_down_key_pressed_ && power_button_pressed_) {
// Record the delay when power button and volume down key are both pressed,
// which indicates user might want to use accelerator to take screenshot.
// This will help us determine the best chord delay among metrics.
const base::TimeDelta key_pressed_delay =
power_button_pressed_time_ - volume_down_key_pressed_time_;
UMA_HISTOGRAM_TIMES("Ash.PowerButtonScreenshot.DelayBetweenAccelKeyPressed",
key_pressed_delay.magnitude());
base::TimeTicks now = tick_clock_->NowTicks();
if (now <= volume_down_key_pressed_time_ + kScreenshotChordDelay &&
now <= power_button_pressed_time_ + kScreenshotChordDelay) {
Shell::Get()->accelerator_controller()->PerformActionIfEnabled(
TAKE_SCREENSHOT, {});
consume_volume_down_ = true;
base::RecordAction(
base::UserMetricsAction("Accel_PowerButton_Screenshot"));
return true;
}
if (!power_button_pressed_ ||
(!volume_down_key_pressed_ && !volume_up_key_pressed_)) {
return false;
}
// Record the delay when power button and volume down/up key are both pressed,
// which indicates user might want to use accelerator to take screenshot.
// This will help us determine the best chord delay among metrics.
const base::TimeDelta key_pressed_delay =
volume_down_key_pressed_
? power_button_pressed_time_ - volume_down_key_pressed_time_
: power_button_pressed_time_ - volume_up_key_pressed_time_;
UMA_HISTOGRAM_TIMES("Ash.PowerButtonScreenshot.DelayBetweenAccelKeyPressed",
key_pressed_delay.magnitude());
base::TimeTicks now = tick_clock_->NowTicks();
if (now > power_button_pressed_time_ + kScreenshotChordDelay)
return false;
consume_volume_down_ =
volume_down_key_pressed_ &&
now <= volume_down_key_pressed_time_ + kScreenshotChordDelay;
consume_volume_up_ =
volume_up_key_pressed_ &&
now <= volume_up_key_pressed_time_ + kScreenshotChordDelay;
if (consume_volume_down_ || consume_volume_up_) {
Shell::Get()->accelerator_controller()->PerformActionIfEnabled(
TAKE_SCREENSHOT, {});
base::RecordAction(base::UserMetricsAction("Accel_PowerButton_Screenshot"));
}
return false;
return consume_volume_down_ || consume_volume_up_;
}
void PowerButtonScreenshotController::OnVolumeDownTimeout(
const ui::Accelerator& accelerator) {
Shell::Get()->accelerator_controller()->PerformActionIfEnabled(VOLUME_DOWN,
accelerator);
void PowerButtonScreenshotController::OnVolumeControlTimeout(
const ui::Accelerator& accelerator,
bool down) {
Shell::Get()->accelerator_controller()->PerformActionIfEnabled(
down ? VOLUME_DOWN : VOLUME_UP, accelerator);
}
} // namespace ash
......@@ -47,31 +47,33 @@ class ASH_EXPORT PowerButtonScreenshotController : public ui::EventHandler {
// indicate that power button and volume down key is consumed by screenshot.
bool InterceptScreenshotChord();
// Called by |volume_down_timer_| to perform volume down accelerator.
void OnVolumeDownTimeout(const ui::Accelerator& accelerator);
// Called by |volume_down_timer_| or |volume_up_timer_| to perform volume down
// or up accelerator.
void OnVolumeControlTimeout(const ui::Accelerator& accelerator, bool down);
// True if volume down key is pressed.
// True if volume down/up key is pressed.
bool volume_down_key_pressed_ = false;
// True if volume up key is pressed.
bool volume_up_key_pressed_ = false;
// True if volume down key is consumed by screenshot accelerator.
// True if volume down/up key is consumed by screenshot accelerator.
bool consume_volume_down_ = false;
bool consume_volume_up_ = false;
// True if power button is pressed.
bool power_button_pressed_ = false;
// Saves the most recent volume down key pressed time.
// Saves the most recent volume down/up key pressed time.
base::TimeTicks volume_down_key_pressed_time_;
base::TimeTicks volume_up_key_pressed_time_;
// Saves the most recent power button pressed time.
base::TimeTicks power_button_pressed_time_;
// Started when volume down key is pressed and power button is not pressed.
// Stopped when power button is pressed. Runs OnVolumeDownTimeout to perform a
// volume down accelerator.
// Started when volume down/up key is pressed and power button is not pressed.
// Stopped when power button is pressed. Runs OnVolumeControlTimeout to
// perform a volume down/up accelerator.
base::OneShotTimer volume_down_timer_;
base::OneShotTimer volume_up_timer_;
// Time source for performed action times.
const base::TickClock* tick_clock_; // Not owned.
......
......@@ -25,4 +25,12 @@ bool PowerButtonScreenshotControllerTestApi::TriggerVolumeDownTimer() {
return true;
}
bool PowerButtonScreenshotControllerTestApi::TriggerVolumeUpTimer() {
if (!controller_->volume_up_timer_.IsRunning())
return false;
controller_->volume_up_timer_.FireNow();
return true;
}
} // namespace ash
......@@ -24,6 +24,10 @@ class PowerButtonScreenshotControllerTestApi {
// and returns true. Otherwise returns false.
bool TriggerVolumeDownTimer() WARN_UNUSED_RESULT;
// If |controller_->volume_up_timer_| is running, stops it, runs its task,
// and returns true. Otherwise returns false.
bool TriggerVolumeUpTimer() WARN_UNUSED_RESULT;
private:
PowerButtonScreenshotController* controller_;
......
......@@ -85,149 +85,256 @@ class PowerButtonScreenshotControllerTest : public PowerButtonTestBase {
DISALLOW_COPY_AND_ASSIGN(PowerButtonScreenshotControllerTest);
};
// Tests power button screenshot accelerator works on tablet mode only.
TEST_F(PowerButtonScreenshotControllerTest, TabletMode) {
// Tests on tablet mode pressing power button and volume down simultaneously
// takes a screenshot.
// Tests the functionalities that press the power button first and then press
// volume down and volume up key alternative.
TEST_F(PowerButtonScreenshotControllerTest,
PowerButtonPressedFirst_Screenshot) {
PressPowerButton();
tick_clock_.Advance(PowerButtonScreenshotController::kScreenshotChordDelay -
base::TimeDelta::FromMilliseconds(5));
PressKey(ui::VKEY_VOLUME_DOWN);
// Verifies screenshot is taken, volume down is consumed.
EXPECT_EQ(1, GetScreenshotCount());
EXPECT_TRUE(LastKeyConsumed());
// Presses volume up key under screenshot chord condition will not take
// screenshot again, volume up is also consumed.
tick_clock_.Advance(base::TimeDelta::FromMilliseconds(2));
ResetScreenshotCount();
PressKey(ui::VKEY_VOLUME_UP);
EXPECT_EQ(0, GetScreenshotCount());
EXPECT_TRUE(LastKeyConsumed());
// Presses volume down key again under screenshot chord condition will not
// take screenshot and still consume volume down event.
ResetScreenshotCount();
tick_clock_.Advance(base::TimeDelta::FromMilliseconds(2));
PressKey(ui::VKEY_VOLUME_DOWN);
EXPECT_EQ(0, GetScreenshotCount());
EXPECT_TRUE(LastKeyConsumed());
// Keeps pressing volume down key outside of screenshot chord condition will
// not take screenshot and still consume volume down event.
tick_clock_.Advance(base::TimeDelta::FromMilliseconds(2));
PressKey(ui::VKEY_VOLUME_DOWN);
EXPECT_EQ(0, GetScreenshotCount());
EXPECT_TRUE(LastKeyConsumed());
// Pressing volume up key outside of screenshot chord condition will not take
// screenshot and still consume volume up event.
PressKey(ui::VKEY_VOLUME_UP);
EXPECT_EQ(0, GetScreenshotCount());
EXPECT_TRUE(LastKeyConsumed());
// Releases power button now should not set display off.
ReleasePowerButton();
EXPECT_FALSE(power_manager_client()->backlights_forced_off());
// Releases volume up key, and verifies nothing happens.
ReleaseKey(ui::VKEY_VOLUME_UP);
EXPECT_EQ(0, GetScreenshotCount());
EXPECT_FALSE(LastKeyConsumed());
// Releases volume down key, and verifies nothing happens.
ReleaseKey(ui::VKEY_VOLUME_DOWN);
EXPECT_EQ(0, GetScreenshotCount());
EXPECT_FALSE(LastKeyConsumed());
}
// Tests the functionalities that press the volume key first and then press
// volume down and volume up key alternative.
TEST_F(PowerButtonScreenshotControllerTest, VolumeKeyPressedFirst_Screenshot) {
// Tests when volume up pressed first, it waits for power button pressed
// screenshot chord.
PressKey(ui::VKEY_VOLUME_UP);
EXPECT_TRUE(LastKeyConsumed());
// Presses power button under screenshot chord condition, and verifies that
// screenshot is taken.
tick_clock_.Advance(PowerButtonScreenshotController::kScreenshotChordDelay -
base::TimeDelta::FromMilliseconds(5));
PressPowerButton();
EXPECT_EQ(1, GetScreenshotCount());
// Presses volume down key under screenshot chord condition will not take
// screenshot, volume down is also consumed.
tick_clock_.Advance(base::TimeDelta::FromMilliseconds(2));
ResetScreenshotCount();
PressKey(ui::VKEY_VOLUME_DOWN);
EXPECT_EQ(0, GetScreenshotCount());
EXPECT_TRUE(LastKeyConsumed());
// Presses volume up key under screenshot chord condition again will not take
// screenshot and still consume volume up event.
tick_clock_.Advance(base::TimeDelta::FromMilliseconds(2));
PressKey(ui::VKEY_VOLUME_UP);
EXPECT_EQ(0, GetScreenshotCount());
EXPECT_TRUE(LastKeyConsumed());
// Keeps pressing volume up key outside of screenshot chord condition will not
// take screenshot and still consume volume up event.
tick_clock_.Advance(base::TimeDelta::FromMilliseconds(2));
PressKey(ui::VKEY_VOLUME_UP);
EXPECT_EQ(0, GetScreenshotCount());
EXPECT_TRUE(LastKeyConsumed());
// Presses volume down key outside of screenshot chord condition will not take
// screenshot and still consume volume down event.
PressKey(ui::VKEY_VOLUME_DOWN);
EXPECT_EQ(0, GetScreenshotCount());
EXPECT_TRUE(LastKeyConsumed());
// Releases power button now should not set display off.
ReleasePowerButton();
EXPECT_FALSE(power_manager_client()->backlights_forced_off());
// Releases volume down key, and verifies nothing happens.
ReleaseKey(ui::VKEY_VOLUME_DOWN);
EXPECT_EQ(0, GetScreenshotCount());
EXPECT_FALSE(LastKeyConsumed());
// Releases volume up key, and verifies nothing happens.
ReleaseKey(ui::VKEY_VOLUME_UP);
EXPECT_EQ(0, GetScreenshotCount());
EXPECT_FALSE(LastKeyConsumed());
}
class PowerButtonScreenshotControllerWithKeyCodeTest
: public PowerButtonScreenshotControllerTest,
public testing::WithParamInterface<ui::KeyboardCode> {
public:
PowerButtonScreenshotControllerWithKeyCodeTest() : key_code_(GetParam()) {}
ui::KeyboardCode key_code() const { return key_code_; }
private:
// Value of the |key_code_| will only be ui::VKEY_VOLUME_DOWN or
// ui::VKEY_VOLUME_UP.
ui::KeyboardCode key_code_ = ui::VKEY_UNKNOWN;
DISALLOW_COPY_AND_ASSIGN(PowerButtonScreenshotControllerWithKeyCodeTest);
};
// Tests power button screenshot accelerator works in tablet mode only.
TEST_P(PowerButtonScreenshotControllerWithKeyCodeTest, TabletMode) {
// Tests in tablet mode pressing power button and volume down/up
// simultaneously takes a screenshot.
PressKey(key_code());
PressPowerButton();
ReleaseKey(key_code());
ReleasePowerButton();
EXPECT_EQ(1, GetScreenshotCount());
// Tests screenshot handling is not active when not on tablet mode.
// Tests screenshot handling is not active when not in tablet mode.
ResetScreenshotCount();
EnableTabletMode(false);
PressKey(ui::VKEY_VOLUME_DOWN);
PressKey(key_code());
PressPowerButton();
ReleaseKey(ui::VKEY_VOLUME_DOWN);
ReleaseKey(key_code());
ReleasePowerButton();
EXPECT_EQ(0, GetScreenshotCount());
}
// Tests if power-button/volume-down is released before volume-down/power-button
// is pressed, it does not take screenshot.
TEST_F(PowerButtonScreenshotControllerTest, ReleaseBeforeAnotherPressed) {
// Releases volume down before power button is pressed.
PressKey(ui::VKEY_VOLUME_DOWN);
ReleaseKey(ui::VKEY_VOLUME_DOWN);
// Tests if power-button/volume-down(volume-up) is released before
// volume-down(volume-up)/power-button is pressed, it does not take screenshot.
TEST_P(PowerButtonScreenshotControllerWithKeyCodeTest,
ReleaseBeforeAnotherPressed) {
// Releases volume down/up before power button is pressed.
PressKey(key_code());
ReleaseKey(key_code());
PressPowerButton();
ReleasePowerButton();
EXPECT_EQ(0, GetScreenshotCount());
// Releases power button before volume down is pressed.
// Releases power button before volume down/up is pressed.
PressPowerButton();
ReleasePowerButton();
PressKey(ui::VKEY_VOLUME_DOWN);
ReleaseKey(ui::VKEY_VOLUME_DOWN);
PressKey(key_code());
ReleaseKey(key_code());
EXPECT_EQ(0, GetScreenshotCount());
}
// Tests power button is pressed first and meets screenshot chord condition.
TEST_F(PowerButtonScreenshotControllerTest,
TEST_P(PowerButtonScreenshotControllerWithKeyCodeTest,
PowerButtonPressedFirst_ScreenshotChord) {
PressPowerButton();
tick_clock_.Advance(PowerButtonScreenshotController::kScreenshotChordDelay -
base::TimeDelta::FromMilliseconds(2));
PressKey(ui::VKEY_VOLUME_DOWN);
// Verifies screenshot is taken, volume down is consumed.
PressKey(key_code());
// Verifies screenshot is taken, volume down/up is consumed.
EXPECT_EQ(1, GetScreenshotCount());
EXPECT_TRUE(LastKeyConsumed());
// Keeps pressing volume down key under screenshot chord condition will not
// take screenshot again, volume down is also consumed.
// Keeps pressing volume down/up key under screenshot chord condition will not
// take screenshot again, volume down/up is also consumed.
tick_clock_.Advance(base::TimeDelta::FromMilliseconds(1));
ResetScreenshotCount();
PressKey(ui::VKEY_VOLUME_DOWN);
PressKey(key_code());
EXPECT_EQ(0, GetScreenshotCount());
EXPECT_TRUE(LastKeyConsumed());
// Keeps pressing volume down key off screenshot chord condition will not
// take screenshot and still consume volume down event.
// Keeps pressing volume down/up key off screenshot chord condition will not
// take screenshot and still consume volume down/up event.
tick_clock_.Advance(base::TimeDelta::FromMilliseconds(2));
PressKey(ui::VKEY_VOLUME_DOWN);
PressKey(key_code());
EXPECT_EQ(0, GetScreenshotCount());
EXPECT_TRUE(LastKeyConsumed());
// Releases power button now should not set display off.
ReleasePowerButton();
EXPECT_FALSE(power_manager_client()->backlights_forced_off());
// Releases volume down key, and verifies nothing happens.
ReleaseKey(ui::VKEY_VOLUME_DOWN);
// Releases volume down/up key, and verifies nothing happens.
ReleaseKey(key_code());
EXPECT_EQ(0, GetScreenshotCount());
EXPECT_FALSE(LastKeyConsumed());
}
// Tests power button is pressed first, and then volume down pressed but doesn't
// meet screenshot chord condition.
TEST_F(PowerButtonScreenshotControllerTest,
// Tests power button is pressed first, and then volume down/up pressed but
// doesn't meet screenshot chord condition.
TEST_P(PowerButtonScreenshotControllerWithKeyCodeTest,
PowerButtonPressedFirst_NoScreenshotChord) {
PressPowerButton();
tick_clock_.Advance(PowerButtonScreenshotController::kScreenshotChordDelay +
base::TimeDelta::FromMilliseconds(1));
PressKey(ui::VKEY_VOLUME_DOWN);
// Verifies screenshot is not taken, volume down is not consumed.
PressKey(key_code());
// Verifies screenshot is not taken, volume down/up is not consumed.
EXPECT_EQ(0, GetScreenshotCount());
EXPECT_FALSE(LastKeyConsumed());
// Keeps pressing volume down key should continue triggerring volume down.
// Keeps pressing volume down/up key should continue triggerring volume
// down/up.
tick_clock_.Advance(base::TimeDelta::FromMilliseconds(2));
PressKey(ui::VKEY_VOLUME_DOWN);
PressKey(key_code());
EXPECT_EQ(0, GetScreenshotCount());
EXPECT_FALSE(LastKeyConsumed());
// Releases power button now should not set display off.
ReleasePowerButton();
EXPECT_FALSE(power_manager_client()->backlights_forced_off());
// Releases volume down key, and verifies nothing happens.
ReleaseKey(ui::VKEY_VOLUME_DOWN);
// Releases volume down/up key, and verifies nothing happens.
ReleaseKey(key_code());
EXPECT_EQ(0, GetScreenshotCount());
EXPECT_FALSE(LastKeyConsumed());
}
// Tests volume key pressed cancels the ongoing power button behavior.
TEST_F(PowerButtonScreenshotControllerTest,
TEST_P(PowerButtonScreenshotControllerWithKeyCodeTest,
PowerButtonPressedFirst_VolumeKeyCancelPowerButton) {
// Tests volume down key can stop power button's shutdown timer and power
// Tests volume down/up key can stop power button's shutdown timer and power
// button menu timer.
PressPowerButton();
EXPECT_TRUE(power_button_test_api_->PowerButtonMenuTimerIsRunning());
PressKey(ui::VKEY_VOLUME_DOWN);
EXPECT_FALSE(power_button_test_api_->PowerButtonMenuTimerIsRunning());
ReleasePowerButton();
ReleaseKey(ui::VKEY_VOLUME_DOWN);
EXPECT_FALSE(power_manager_client()->backlights_forced_off());
// Tests volume up key can stop power button's shutdown timer and power button
// menu timer. Also tests that volume up key is not consumed.
PressPowerButton();
EXPECT_TRUE(power_button_test_api_->PowerButtonMenuTimerIsRunning());
PressKey(ui::VKEY_VOLUME_UP);
EXPECT_FALSE(LastKeyConsumed());
PressKey(key_code());
EXPECT_FALSE(power_button_test_api_->PowerButtonMenuTimerIsRunning());
ReleasePowerButton();
ReleaseKey(ui::VKEY_VOLUME_UP);
ReleaseKey(key_code());
EXPECT_FALSE(power_manager_client()->backlights_forced_off());
EXPECT_FALSE(LastKeyConsumed());
}
// Tests volume key pressed can not cancel the started pre-shutdown animation.
TEST_F(PowerButtonScreenshotControllerTest,
TEST_P(PowerButtonScreenshotControllerWithKeyCodeTest,
PowerButtonPressedFirst_VolumeKeyNotCancelPowerButton) {
PressPowerButton();
ASSERT_TRUE(power_button_test_api_->TriggerPowerButtonMenuTimeout());
EXPECT_TRUE(power_button_test_api_->PreShutdownTimerIsRunning());
EXPECT_TRUE(power_button_test_api_->TriggerPreShutdownTimeout());
EXPECT_TRUE(lock_state_test_api_->shutdown_timer_is_running());
PressKey(ui::VKEY_VOLUME_DOWN);
ReleaseKey(ui::VKEY_VOLUME_DOWN);
PressKey(key_code());
ReleaseKey(key_code());
EXPECT_TRUE(lock_state_test_api_->shutdown_timer_is_running());
ReleasePowerButton();
EXPECT_FALSE(lock_state_test_api_->shutdown_timer_is_running());
}
// Tests volume down key pressed first and meets screenshot chord condition.
TEST_F(PowerButtonScreenshotControllerTest,
VolumeDownPressedFirst_ScreenshotChord) {
// Tests when volume down pressed first, it waits for power button pressed
// Tests volume down/up key pressed first and meets screenshot chord condition.
TEST_P(PowerButtonScreenshotControllerWithKeyCodeTest,
VolumeKeyPressedFirst_ScreenshotChord) {
// Tests when volume down/up pressed first, it waits for power button pressed
// screenshot chord.
PressKey(ui::VKEY_VOLUME_DOWN);
PressKey(key_code());
EXPECT_TRUE(LastKeyConsumed());
// Presses power button under screenshot chord condition, and verifies that
// screenshot is taken.
......@@ -235,81 +342,79 @@ TEST_F(PowerButtonScreenshotControllerTest,
base::TimeDelta::FromMilliseconds(2));
PressPowerButton();
EXPECT_EQ(1, GetScreenshotCount());
// Keeps pressing volume down key under screenshot chord condition will not
// take screenshot again, volume down is also consumed.
// Keeps pressing volume down/up key under screenshot chord condition will not
// take screenshot again, volume down/up is also consumed.
tick_clock_.Advance(base::TimeDelta::FromMilliseconds(1));
ResetScreenshotCount();
PressKey(ui::VKEY_VOLUME_DOWN);
PressKey(key_code());
EXPECT_EQ(0, GetScreenshotCount());
EXPECT_TRUE(LastKeyConsumed());
// Keeps pressing volume down key off screenshot chord condition will not take
// screenshot and still consume volume down event.
// Keeps pressing volume down/up key off screenshot chord condition will not
// take screenshot and still consume volume down/up event.
tick_clock_.Advance(base::TimeDelta::FromMilliseconds(2));
PressKey(ui::VKEY_VOLUME_DOWN);
PressKey(key_code());
EXPECT_EQ(0, GetScreenshotCount());
EXPECT_TRUE(LastKeyConsumed());
// Releases power button now should not set display off.
ReleasePowerButton();
EXPECT_FALSE(power_manager_client()->backlights_forced_off());
// Releases volume down key, and verifies nothing happens.
ReleaseKey(ui::VKEY_VOLUME_DOWN);
// Releases volume down/up key, and verifies nothing happens.
ReleaseKey(key_code());
EXPECT_EQ(0, GetScreenshotCount());
EXPECT_FALSE(LastKeyConsumed());
}
// Tests volume down key pressed first, and then power button pressed but
// Tests volume down/up key pressed first, and then power button pressed but
// doesn't meet screenshot chord condition.
TEST_F(PowerButtonScreenshotControllerTest,
VolumeDownPressedFirst_NoScreenshotChord) {
// Tests when volume down pressed first, it waits for power button pressed
TEST_P(PowerButtonScreenshotControllerWithKeyCodeTest,
VolumeKeyPressedFirst_NoScreenshotChord) {
// Tests when volume down/up pressed first, it waits for power button pressed
// screenshot chord.
PressKey(ui::VKEY_VOLUME_DOWN);
PressKey(key_code());
EXPECT_TRUE(LastKeyConsumed());
// Advances |tick_clock_| to off screenshot chord point. This will also
// trigger volume down timer timeout, which will perform a volume down
// trigger volume down/up timer timeout, which will perform a volume down/up
// operation.
tick_clock_.Advance(PowerButtonScreenshotController::kScreenshotChordDelay +
base::TimeDelta::FromMilliseconds(1));
EXPECT_TRUE(screenshot_test_api_->TriggerVolumeDownTimer());
if (key_code() == ui::VKEY_VOLUME_DOWN)
EXPECT_TRUE(screenshot_test_api_->TriggerVolumeDownTimer());
else
EXPECT_TRUE(screenshot_test_api_->TriggerVolumeUpTimer());
// Presses power button would not take screenshot.
PressPowerButton();
EXPECT_EQ(0, GetScreenshotCount());
// Keeps pressing volume down key should continue triggerring volume down.
// Keeps pressing volume down/up key should continue triggerring volume
// down/up.
tick_clock_.Advance(base::TimeDelta::FromMilliseconds(2));
PressKey(ui::VKEY_VOLUME_DOWN);
PressKey(key_code());
EXPECT_EQ(0, GetScreenshotCount());
EXPECT_FALSE(LastKeyConsumed());
// Releases power button now should not set display off.
ReleasePowerButton();
EXPECT_FALSE(power_manager_client()->backlights_forced_off());
// Releases volume down key, and verifies nothing happens.
ReleaseKey(ui::VKEY_VOLUME_DOWN);
// Releases volume down/up key, and verifies nothing happens.
ReleaseKey(key_code());
EXPECT_EQ(0, GetScreenshotCount());
EXPECT_FALSE(LastKeyConsumed());
}
// Tests volume key pressed first invalidates the power button behavior.
TEST_F(PowerButtonScreenshotControllerTest,
TEST_P(PowerButtonScreenshotControllerWithKeyCodeTest,
VolumeKeyPressedFirst_InvalidateConvertiblePowerButton) {
// Tests volume down key invalidates the power button behavior.
PressKey(ui::VKEY_VOLUME_DOWN);
// Tests volume down/up key invalidates the power button behavior.
PressKey(key_code());
PressPowerButton();
EXPECT_FALSE(power_button_test_api_->PowerButtonMenuTimerIsRunning());
ReleasePowerButton();
ReleaseKey(ui::VKEY_VOLUME_DOWN);
ReleaseKey(key_code());
EXPECT_FALSE(power_manager_client()->backlights_forced_off());
// Tests volume up key invalidates the power button behavior. Also
// tests that volume up key is not consumed.
PressKey(ui::VKEY_VOLUME_UP);
PressPowerButton();
EXPECT_FALSE(LastKeyConsumed());
EXPECT_FALSE(power_button_test_api_->PowerButtonMenuTimerIsRunning());
ReleasePowerButton();
ReleaseKey(ui::VKEY_VOLUME_UP);
EXPECT_FALSE(power_manager_client()->backlights_forced_off());
EXPECT_FALSE(LastKeyConsumed());
}
INSTANTIATE_TEST_SUITE_P(AshPowerButtonScreenshot,
PowerButtonScreenshotControllerWithKeyCodeTest,
testing::Values(ui::VKEY_VOLUME_DOWN,
ui::VKEY_VOLUME_UP));
} // namespace ash
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