power_button_screenshot_controller_unittest.cc 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. // Copyright 2017 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ash/system/power/power_button_screenshot_controller.h"
  5. #include <memory>
  6. #include "ash/capture_mode/capture_mode_metrics.h"
  7. #include "ash/login_status.h"
  8. #include "ash/public/cpp/window_properties.h"
  9. #include "ash/shell.h"
  10. #include "ash/system/power/power_button_controller.h"
  11. #include "ash/system/power/power_button_controller_test_api.h"
  12. #include "ash/system/power/power_button_screenshot_controller_test_api.h"
  13. #include "ash/system/power/power_button_test_base.h"
  14. #include "ash/wm/lock_state_controller_test_api.h"
  15. #include "ash/wm/tablet_mode/tablet_mode_controller.h"
  16. #include "base/test/metrics/histogram_tester.h"
  17. #include "base/test/simple_test_tick_clock.h"
  18. #include "chromeos/dbus/power/fake_power_manager_client.h"
  19. #include "ui/aura/test/test_window_delegate.h"
  20. #include "ui/events/event.h"
  21. #include "ui/wm/core/window_util.h"
  22. namespace ash {
  23. namespace {
  24. class KeyEventWindowDelegate : public aura::test::TestWindowDelegate {
  25. public:
  26. KeyEventWindowDelegate() = default;
  27. ~KeyEventWindowDelegate() override = default;
  28. KeyEventWindowDelegate(const KeyEventWindowDelegate&) = delete;
  29. KeyEventWindowDelegate& operator=(const KeyEventWindowDelegate&) = delete;
  30. void OnKeyEvent(ui::KeyEvent* event) override {
  31. key_code_ = event->key_code();
  32. }
  33. ui::KeyboardCode GetReceivedKeyCodeAndReset() {
  34. ui::KeyboardCode tmp = key_code_;
  35. key_code_ = ui::VKEY_UNKNOWN;
  36. return tmp;
  37. }
  38. private:
  39. ui::KeyboardCode key_code_ = ui::VKEY_UNKNOWN;
  40. };
  41. } // namespace
  42. // Test fixture used for testing power button screenshot behavior under tablet
  43. // power button.
  44. class PowerButtonScreenshotControllerTest : public PowerButtonTestBase {
  45. public:
  46. PowerButtonScreenshotControllerTest() = default;
  47. PowerButtonScreenshotControllerTest(
  48. const PowerButtonScreenshotControllerTest&) = delete;
  49. PowerButtonScreenshotControllerTest& operator=(
  50. const PowerButtonScreenshotControllerTest&) = delete;
  51. ~PowerButtonScreenshotControllerTest() override = default;
  52. // PowerButtonTestBase:
  53. void SetUp() override {
  54. PowerButtonTestBase::SetUp();
  55. InitPowerButtonControllerMembers(
  56. chromeos::PowerManagerClient::TabletMode::ON);
  57. InitScreenshotTestApi();
  58. EnableTabletMode(true);
  59. // Advance a duration longer than |kIgnorePowerButtonAfterResumeDelay| to
  60. // avoid events being ignored.
  61. tick_clock_.Advance(
  62. PowerButtonController::kIgnorePowerButtonAfterResumeDelay +
  63. base::Milliseconds(2));
  64. ResetScreenshotCount();
  65. }
  66. protected:
  67. // PowerButtonTestBase:
  68. void PressKey(ui::KeyboardCode key_code) override {
  69. last_key_event_ = std::make_unique<ui::KeyEvent>(ui::ET_KEY_PRESSED,
  70. key_code, ui::EF_NONE);
  71. screenshot_controller_->OnKeyEvent(last_key_event_.get());
  72. }
  73. void ReleaseKey(ui::KeyboardCode key_code) override {
  74. last_key_event_ = std::make_unique<ui::KeyEvent>(ui::ET_KEY_RELEASED,
  75. key_code, ui::EF_NONE);
  76. screenshot_controller_->OnKeyEvent(last_key_event_.get());
  77. }
  78. void InitScreenshotTestApi() {
  79. screenshot_test_api_ =
  80. std::make_unique<PowerButtonScreenshotControllerTestApi>(
  81. screenshot_controller_);
  82. }
  83. int GetScreenshotCount() const {
  84. constexpr char kClamshellHistogram[] =
  85. "Ash.CaptureModeController.EntryPoint.ClamshellMode";
  86. constexpr char kTabletHistogram[] =
  87. "Ash.CaptureModeController.EntryPoint.TabletMode";
  88. if (Shell::Get()->tablet_mode_controller()->InTabletMode()) {
  89. return histogram_tester_->GetBucketCount(
  90. kTabletHistogram, CaptureModeEntryType::kCaptureAllDisplays);
  91. }
  92. return histogram_tester_->GetBucketCount(
  93. kClamshellHistogram, CaptureModeEntryType::kCaptureAllDisplays);
  94. }
  95. void ResetScreenshotCount() {
  96. histogram_tester_ = std::make_unique<base::HistogramTester>();
  97. }
  98. bool LastKeyConsumed() const {
  99. DCHECK(last_key_event_);
  100. return last_key_event_->stopped_propagation();
  101. }
  102. std::unique_ptr<PowerButtonScreenshotControllerTestApi> screenshot_test_api_;
  103. // Stores the last key event. Can be NULL if not set through PressKey() or
  104. // ReleaseKey().
  105. std::unique_ptr<ui::KeyEvent> last_key_event_;
  106. // Used to test capture mode invocations when the feature is on.
  107. std::unique_ptr<base::HistogramTester> histogram_tester_;
  108. };
  109. class PowerButtonScreenshotControllerWithSystemKeysTest
  110. : public PowerButtonScreenshotControllerTest,
  111. public ::testing::WithParamInterface<bool> {
  112. public:
  113. PowerButtonScreenshotControllerWithSystemKeysTest() = default;
  114. ~PowerButtonScreenshotControllerWithSystemKeysTest() override = default;
  115. PowerButtonScreenshotControllerWithSystemKeysTest(
  116. const PowerButtonScreenshotControllerWithSystemKeysTest&) = delete;
  117. PowerButtonScreenshotControllerWithSystemKeysTest& operator=(
  118. const PowerButtonScreenshotControllerWithSystemKeysTest&) = delete;
  119. void SetUp() override {
  120. PowerButtonScreenshotControllerTest::SetUp();
  121. if (GetParam()) {
  122. aura::Window* window =
  123. CreateTestWindowInShellWithDelegate(&delegate_, 1, gfx::Rect());
  124. window->SetProperty(ash::kCanConsumeSystemKeysKey, true);
  125. }
  126. }
  127. private:
  128. KeyEventWindowDelegate delegate_;
  129. };
  130. // Tests the functionalities that press the power button first and then press
  131. // volume down and volume up key alternative.
  132. TEST_P(PowerButtonScreenshotControllerWithSystemKeysTest,
  133. PowerButtonPressedFirst_Screenshot) {
  134. PressPowerButton();
  135. tick_clock_.Advance(PowerButtonScreenshotController::kScreenshotChordDelay -
  136. base::Milliseconds(5));
  137. PressKey(ui::VKEY_VOLUME_DOWN);
  138. // Verifies screenshot is taken, volume down is consumed.
  139. EXPECT_EQ(1, GetScreenshotCount());
  140. EXPECT_TRUE(LastKeyConsumed());
  141. // Presses volume up key under screenshot chord condition will not take
  142. // screenshot again, volume up is also consumed.
  143. tick_clock_.Advance(base::Milliseconds(2));
  144. ResetScreenshotCount();
  145. PressKey(ui::VKEY_VOLUME_UP);
  146. EXPECT_EQ(0, GetScreenshotCount());
  147. EXPECT_TRUE(LastKeyConsumed());
  148. // Presses volume down key again under screenshot chord condition will not
  149. // take screenshot and still consume volume down event.
  150. ResetScreenshotCount();
  151. tick_clock_.Advance(base::Milliseconds(2));
  152. PressKey(ui::VKEY_VOLUME_DOWN);
  153. EXPECT_EQ(0, GetScreenshotCount());
  154. EXPECT_TRUE(LastKeyConsumed());
  155. // Keeps pressing volume down key outside of screenshot chord condition will
  156. // not take screenshot and still consume volume down event.
  157. tick_clock_.Advance(base::Milliseconds(2));
  158. PressKey(ui::VKEY_VOLUME_DOWN);
  159. EXPECT_EQ(0, GetScreenshotCount());
  160. EXPECT_TRUE(LastKeyConsumed());
  161. // Pressing volume up key outside of screenshot chord condition will not take
  162. // screenshot and still consume volume up event.
  163. PressKey(ui::VKEY_VOLUME_UP);
  164. EXPECT_EQ(0, GetScreenshotCount());
  165. EXPECT_TRUE(LastKeyConsumed());
  166. // Releases power button now should not set display off.
  167. ReleasePowerButton();
  168. EXPECT_FALSE(power_manager_client()->backlights_forced_off());
  169. // Releases volume up key, and verifies nothing happens.
  170. ReleaseKey(ui::VKEY_VOLUME_UP);
  171. EXPECT_EQ(0, GetScreenshotCount());
  172. EXPECT_FALSE(LastKeyConsumed());
  173. // Releases volume down key, and verifies nothing happens.
  174. ReleaseKey(ui::VKEY_VOLUME_DOWN);
  175. EXPECT_EQ(0, GetScreenshotCount());
  176. EXPECT_FALSE(LastKeyConsumed());
  177. }
  178. INSTANTIATE_TEST_SUITE_P(All,
  179. PowerButtonScreenshotControllerWithSystemKeysTest,
  180. testing::Bool());
  181. // Tests the functionalities that press the volume key first and then press
  182. // volume down and volume up key alternative.
  183. TEST_F(PowerButtonScreenshotControllerTest, VolumeKeyPressedFirst_Screenshot) {
  184. // Tests when volume up pressed first, it waits for power button pressed
  185. // screenshot chord.
  186. PressKey(ui::VKEY_VOLUME_UP);
  187. EXPECT_TRUE(LastKeyConsumed());
  188. // Presses power button under screenshot chord condition, and verifies that
  189. // screenshot is taken.
  190. tick_clock_.Advance(PowerButtonScreenshotController::kScreenshotChordDelay -
  191. base::Milliseconds(5));
  192. PressPowerButton();
  193. EXPECT_EQ(1, GetScreenshotCount());
  194. // Presses volume down key under screenshot chord condition will not take
  195. // screenshot, volume down is also consumed.
  196. tick_clock_.Advance(base::Milliseconds(2));
  197. ResetScreenshotCount();
  198. PressKey(ui::VKEY_VOLUME_DOWN);
  199. EXPECT_EQ(0, GetScreenshotCount());
  200. EXPECT_TRUE(LastKeyConsumed());
  201. // Presses volume up key under screenshot chord condition again will not take
  202. // screenshot and still consume volume up event.
  203. tick_clock_.Advance(base::Milliseconds(2));
  204. PressKey(ui::VKEY_VOLUME_UP);
  205. EXPECT_EQ(0, GetScreenshotCount());
  206. EXPECT_TRUE(LastKeyConsumed());
  207. // Keeps pressing volume up key outside of screenshot chord condition will not
  208. // take screenshot and still consume volume up event.
  209. tick_clock_.Advance(base::Milliseconds(2));
  210. PressKey(ui::VKEY_VOLUME_UP);
  211. EXPECT_EQ(0, GetScreenshotCount());
  212. EXPECT_TRUE(LastKeyConsumed());
  213. // Presses volume down key outside of screenshot chord condition will not take
  214. // screenshot and still consume volume down event.
  215. PressKey(ui::VKEY_VOLUME_DOWN);
  216. EXPECT_EQ(0, GetScreenshotCount());
  217. EXPECT_TRUE(LastKeyConsumed());
  218. // Releases power button now should not set display off.
  219. ReleasePowerButton();
  220. EXPECT_FALSE(power_manager_client()->backlights_forced_off());
  221. // Releases volume down key, and verifies nothing happens.
  222. ReleaseKey(ui::VKEY_VOLUME_DOWN);
  223. EXPECT_EQ(0, GetScreenshotCount());
  224. EXPECT_FALSE(LastKeyConsumed());
  225. // Releases volume up key, and verifies nothing happens.
  226. ReleaseKey(ui::VKEY_VOLUME_UP);
  227. EXPECT_EQ(0, GetScreenshotCount());
  228. EXPECT_FALSE(LastKeyConsumed());
  229. }
  230. // If the window with kConsumeSystemKeysKey property is active in tablet mode,
  231. // volume keys will be passed to the window if they are pressed first.
  232. TEST_F(PowerButtonScreenshotControllerTest, WindowWithSystemKeys) {
  233. EnableTabletMode(true);
  234. KeyEventWindowDelegate delegate;
  235. std::unique_ptr<aura::Window> window = base::WrapUnique(
  236. CreateTestWindowInShellWithDelegate(&delegate, 1, gfx::Rect()));
  237. window->SetProperty(ash::kCanConsumeSystemKeysKey, true);
  238. ::wm::ActivateWindow(window.get());
  239. // Tests when volume up pressed first, it's consumed by an app.
  240. // screenshot chord.
  241. GetEventGenerator()->PressKey(ui::VKEY_VOLUME_UP, ui::EF_NONE);
  242. EXPECT_EQ(ui::VKEY_VOLUME_UP, delegate.GetReceivedKeyCodeAndReset());
  243. GetEventGenerator()->PressKey(ui::VKEY_VOLUME_UP, ui::EF_NONE);
  244. EXPECT_EQ(ui::VKEY_VOLUME_UP, delegate.GetReceivedKeyCodeAndReset());
  245. EXPECT_EQ(0, GetScreenshotCount());
  246. // Tests when volume down pressed first, it's consumed by an app.
  247. // screenshot chord.
  248. GetEventGenerator()->PressKey(ui::VKEY_VOLUME_DOWN, ui::EF_NONE);
  249. EXPECT_EQ(ui::VKEY_VOLUME_DOWN, delegate.GetReceivedKeyCodeAndReset());
  250. GetEventGenerator()->PressKey(ui::VKEY_VOLUME_DOWN, ui::EF_NONE);
  251. EXPECT_EQ(ui::VKEY_VOLUME_DOWN, delegate.GetReceivedKeyCodeAndReset());
  252. EXPECT_EQ(0, GetScreenshotCount());
  253. // Delete the window, and volume will be consumed by shortcut.
  254. // Screenshot using up.
  255. window.reset();
  256. GetEventGenerator()->PressKey(ui::VKEY_VOLUME_UP, ui::EF_NONE);
  257. EXPECT_EQ(ui::VKEY_UNKNOWN, delegate.GetReceivedKeyCodeAndReset());
  258. PressPowerButton();
  259. ReleasePowerButton();
  260. GetEventGenerator()->ReleaseKey(ui::VKEY_VOLUME_UP, ui::EF_NONE);
  261. EXPECT_EQ(ui::VKEY_UNKNOWN, delegate.GetReceivedKeyCodeAndReset());
  262. EXPECT_EQ(1, GetScreenshotCount());
  263. // Screenshot using down.
  264. GetEventGenerator()->PressKey(ui::VKEY_VOLUME_DOWN, ui::EF_NONE);
  265. EXPECT_EQ(ui::VKEY_UNKNOWN, delegate.GetReceivedKeyCodeAndReset());
  266. PressPowerButton();
  267. ReleasePowerButton();
  268. GetEventGenerator()->ReleaseKey(ui::VKEY_VOLUME_DOWN, ui::EF_NONE);
  269. EXPECT_EQ(ui::VKEY_UNKNOWN, delegate.GetReceivedKeyCodeAndReset());
  270. EXPECT_EQ(2, GetScreenshotCount());
  271. }
  272. class PowerButtonScreenshotControllerWithKeyCodeTest
  273. : public PowerButtonScreenshotControllerTest,
  274. public testing::WithParamInterface<ui::KeyboardCode> {
  275. public:
  276. PowerButtonScreenshotControllerWithKeyCodeTest() : key_code_(GetParam()) {}
  277. PowerButtonScreenshotControllerWithKeyCodeTest(
  278. const PowerButtonScreenshotControllerWithKeyCodeTest&) = delete;
  279. PowerButtonScreenshotControllerWithKeyCodeTest& operator=(
  280. const PowerButtonScreenshotControllerWithKeyCodeTest&) = delete;
  281. ui::KeyboardCode key_code() const { return key_code_; }
  282. private:
  283. // Value of the |key_code_| will only be ui::VKEY_VOLUME_DOWN or
  284. // ui::VKEY_VOLUME_UP.
  285. ui::KeyboardCode key_code_ = ui::VKEY_UNKNOWN;
  286. };
  287. // Tests power button screenshot accelerator works in tablet mode only.
  288. TEST_P(PowerButtonScreenshotControllerWithKeyCodeTest, TabletMode) {
  289. // Tests in tablet mode pressing power button and volume down/up
  290. // simultaneously takes a screenshot.
  291. PressKey(key_code());
  292. PressPowerButton();
  293. ReleaseKey(key_code());
  294. ReleasePowerButton();
  295. EXPECT_EQ(1, GetScreenshotCount());
  296. // Tests screenshot handling is not active when not in tablet mode.
  297. ResetScreenshotCount();
  298. EnableTabletMode(false);
  299. PressKey(key_code());
  300. PressPowerButton();
  301. ReleaseKey(key_code());
  302. ReleasePowerButton();
  303. EXPECT_EQ(0, GetScreenshotCount());
  304. }
  305. // Tests if power-button/volume-down(volume-up) is released before
  306. // volume-down(volume-up)/power-button is pressed, it does not take screenshot.
  307. TEST_P(PowerButtonScreenshotControllerWithKeyCodeTest,
  308. ReleaseBeforeAnotherPressed) {
  309. // Releases volume down/up before power button is pressed.
  310. PressKey(key_code());
  311. ReleaseKey(key_code());
  312. PressPowerButton();
  313. ReleasePowerButton();
  314. EXPECT_EQ(0, GetScreenshotCount());
  315. // Releases power button before volume down/up is pressed.
  316. PressPowerButton();
  317. ReleasePowerButton();
  318. PressKey(key_code());
  319. ReleaseKey(key_code());
  320. EXPECT_EQ(0, GetScreenshotCount());
  321. }
  322. // Tests power button is pressed first and meets screenshot chord condition.
  323. TEST_P(PowerButtonScreenshotControllerWithKeyCodeTest,
  324. PowerButtonPressedFirst_ScreenshotChord) {
  325. PressPowerButton();
  326. tick_clock_.Advance(PowerButtonScreenshotController::kScreenshotChordDelay -
  327. base::Milliseconds(2));
  328. PressKey(key_code());
  329. // Verifies screenshot is taken, volume down/up is consumed.
  330. EXPECT_EQ(1, GetScreenshotCount());
  331. EXPECT_TRUE(LastKeyConsumed());
  332. // Keeps pressing volume down/up key under screenshot chord condition will not
  333. // take screenshot again, volume down/up is also consumed.
  334. tick_clock_.Advance(base::Milliseconds(1));
  335. ResetScreenshotCount();
  336. PressKey(key_code());
  337. EXPECT_EQ(0, GetScreenshotCount());
  338. EXPECT_TRUE(LastKeyConsumed());
  339. // Keeps pressing volume down/up key off screenshot chord condition will not
  340. // take screenshot and still consume volume down/up event.
  341. tick_clock_.Advance(base::Milliseconds(2));
  342. PressKey(key_code());
  343. EXPECT_EQ(0, GetScreenshotCount());
  344. EXPECT_TRUE(LastKeyConsumed());
  345. // Releases power button now should not set display off.
  346. ReleasePowerButton();
  347. EXPECT_FALSE(power_manager_client()->backlights_forced_off());
  348. // Releases volume down/up key, and verifies nothing happens.
  349. ReleaseKey(key_code());
  350. EXPECT_EQ(0, GetScreenshotCount());
  351. EXPECT_FALSE(LastKeyConsumed());
  352. }
  353. // Tests power button is pressed first, and then volume down/up pressed but
  354. // doesn't meet screenshot chord condition.
  355. TEST_P(PowerButtonScreenshotControllerWithKeyCodeTest,
  356. PowerButtonPressedFirst_NoScreenshotChord) {
  357. PressPowerButton();
  358. tick_clock_.Advance(PowerButtonScreenshotController::kScreenshotChordDelay +
  359. base::Milliseconds(1));
  360. PressKey(key_code());
  361. // Verifies screenshot is not taken, volume down/up is not consumed.
  362. EXPECT_EQ(0, GetScreenshotCount());
  363. EXPECT_FALSE(LastKeyConsumed());
  364. // Keeps pressing volume down/up key should continue triggerring volume
  365. // down/up.
  366. tick_clock_.Advance(base::Milliseconds(2));
  367. PressKey(key_code());
  368. EXPECT_EQ(0, GetScreenshotCount());
  369. EXPECT_FALSE(LastKeyConsumed());
  370. // Releases power button now should not set display off.
  371. ReleasePowerButton();
  372. EXPECT_FALSE(power_manager_client()->backlights_forced_off());
  373. // Releases volume down/up key, and verifies nothing happens.
  374. ReleaseKey(key_code());
  375. EXPECT_EQ(0, GetScreenshotCount());
  376. EXPECT_FALSE(LastKeyConsumed());
  377. }
  378. // Tests volume key pressed cancels the ongoing power button behavior.
  379. TEST_P(PowerButtonScreenshotControllerWithKeyCodeTest,
  380. PowerButtonPressedFirst_VolumeKeyCancelPowerButton) {
  381. // Tests volume down/up key can stop power button's shutdown timer and power
  382. // button menu timer.
  383. PressPowerButton();
  384. EXPECT_TRUE(power_button_test_api_->PowerButtonMenuTimerIsRunning());
  385. PressKey(key_code());
  386. EXPECT_FALSE(power_button_test_api_->PowerButtonMenuTimerIsRunning());
  387. ReleasePowerButton();
  388. ReleaseKey(key_code());
  389. EXPECT_FALSE(power_manager_client()->backlights_forced_off());
  390. }
  391. // Tests volume key pressed can not cancel the started pre-shutdown animation.
  392. TEST_P(PowerButtonScreenshotControllerWithKeyCodeTest,
  393. PowerButtonPressedFirst_VolumeKeyNotCancelPowerButton) {
  394. PressPowerButton();
  395. ASSERT_TRUE(power_button_test_api_->TriggerPowerButtonMenuTimeout());
  396. EXPECT_TRUE(power_button_test_api_->PreShutdownTimerIsRunning());
  397. EXPECT_TRUE(power_button_test_api_->TriggerPreShutdownTimeout());
  398. EXPECT_TRUE(lock_state_test_api_->shutdown_timer_is_running());
  399. PressKey(key_code());
  400. ReleaseKey(key_code());
  401. EXPECT_TRUE(lock_state_test_api_->shutdown_timer_is_running());
  402. ReleasePowerButton();
  403. EXPECT_FALSE(lock_state_test_api_->shutdown_timer_is_running());
  404. }
  405. // Tests volume down/up key pressed first and meets screenshot chord condition.
  406. TEST_P(PowerButtonScreenshotControllerWithKeyCodeTest,
  407. VolumeKeyPressedFirst_ScreenshotChord) {
  408. // Tests when volume down/up pressed first, it waits for power button pressed
  409. // screenshot chord.
  410. PressKey(key_code());
  411. EXPECT_TRUE(LastKeyConsumed());
  412. // Presses power button under screenshot chord condition, and verifies that
  413. // screenshot is taken.
  414. tick_clock_.Advance(PowerButtonScreenshotController::kScreenshotChordDelay -
  415. base::Milliseconds(2));
  416. PressPowerButton();
  417. EXPECT_EQ(1, GetScreenshotCount());
  418. // Keeps pressing volume down/up key under screenshot chord condition will not
  419. // take screenshot again, volume down/up is also consumed.
  420. tick_clock_.Advance(base::Milliseconds(1));
  421. ResetScreenshotCount();
  422. PressKey(key_code());
  423. EXPECT_EQ(0, GetScreenshotCount());
  424. EXPECT_TRUE(LastKeyConsumed());
  425. // Keeps pressing volume down/up key off screenshot chord condition will not
  426. // take screenshot and still consume volume down/up event.
  427. tick_clock_.Advance(base::Milliseconds(2));
  428. PressKey(key_code());
  429. EXPECT_EQ(0, GetScreenshotCount());
  430. EXPECT_TRUE(LastKeyConsumed());
  431. // Releases power button now should not set display off.
  432. ReleasePowerButton();
  433. EXPECT_FALSE(power_manager_client()->backlights_forced_off());
  434. // Releases volume down/up key, and verifies nothing happens.
  435. ReleaseKey(key_code());
  436. EXPECT_EQ(0, GetScreenshotCount());
  437. EXPECT_FALSE(LastKeyConsumed());
  438. }
  439. // Tests volume down/up key pressed first, and then power button pressed but
  440. // doesn't meet screenshot chord condition.
  441. TEST_P(PowerButtonScreenshotControllerWithKeyCodeTest,
  442. VolumeKeyPressedFirst_NoScreenshotChord) {
  443. // Tests when volume down/up pressed first, it waits for power button pressed
  444. // screenshot chord.
  445. PressKey(key_code());
  446. EXPECT_TRUE(LastKeyConsumed());
  447. // Advances |tick_clock_| to off screenshot chord point. This will also
  448. // trigger volume down/up timer timeout, which will perform a volume down/up
  449. // operation.
  450. tick_clock_.Advance(PowerButtonScreenshotController::kScreenshotChordDelay +
  451. base::Milliseconds(1));
  452. if (key_code() == ui::VKEY_VOLUME_DOWN)
  453. EXPECT_TRUE(screenshot_test_api_->TriggerVolumeDownTimer());
  454. else
  455. EXPECT_TRUE(screenshot_test_api_->TriggerVolumeUpTimer());
  456. // Presses power button would not take screenshot.
  457. PressPowerButton();
  458. EXPECT_EQ(0, GetScreenshotCount());
  459. // Keeps pressing volume down/up key should continue triggerring volume
  460. // down/up.
  461. tick_clock_.Advance(base::Milliseconds(2));
  462. PressKey(key_code());
  463. EXPECT_EQ(0, GetScreenshotCount());
  464. EXPECT_FALSE(LastKeyConsumed());
  465. // Releases power button now should not set display off.
  466. ReleasePowerButton();
  467. EXPECT_FALSE(power_manager_client()->backlights_forced_off());
  468. // Releases volume down/up key, and verifies nothing happens.
  469. ReleaseKey(key_code());
  470. EXPECT_EQ(0, GetScreenshotCount());
  471. EXPECT_FALSE(LastKeyConsumed());
  472. }
  473. // Tests volume key pressed first invalidates the power button behavior.
  474. TEST_P(PowerButtonScreenshotControllerWithKeyCodeTest,
  475. VolumeKeyPressedFirst_InvalidateConvertiblePowerButton) {
  476. // Tests volume down/up key invalidates the power button behavior.
  477. PressKey(key_code());
  478. PressPowerButton();
  479. EXPECT_FALSE(power_button_test_api_->PowerButtonMenuTimerIsRunning());
  480. ReleasePowerButton();
  481. ReleaseKey(key_code());
  482. EXPECT_FALSE(power_manager_client()->backlights_forced_off());
  483. }
  484. INSTANTIATE_TEST_SUITE_P(AshPowerButtonScreenshot,
  485. PowerButtonScreenshotControllerWithKeyCodeTest,
  486. testing::Values(ui::VKEY_VOLUME_DOWN,
  487. ui::VKEY_VOLUME_UP));
  488. } // namespace ash