File: power_button_screenshot_controller_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (549 lines) | stat: -rw-r--r-- 21,755 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/system/power/power_button_screenshot_controller.h"

#include <memory>

#include "ash/capture_mode/capture_mode_metrics.h"
#include "ash/login_status.h"
#include "ash/public/cpp/window_properties.h"
#include "ash/shell.h"
#include "ash/system/power/power_button_controller.h"
#include "ash/system/power/power_button_controller_test_api.h"
#include "ash/system/power/power_button_screenshot_controller_test_api.h"
#include "ash/system/power/power_button_test_base.h"
#include "ash/wm/lock_state_controller_test_api.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/simple_test_tick_clock.h"
#include "chromeos/dbus/power/fake_power_manager_client.h"
#include "ui/aura/test/test_window_delegate.h"
#include "ui/display/screen.h"
#include "ui/events/event.h"
#include "ui/wm/core/window_util.h"

namespace ash {

namespace {
class KeyEventWindowDelegate : public aura::test::TestWindowDelegate {
 public:
  KeyEventWindowDelegate() = default;
  ~KeyEventWindowDelegate() override = default;

  KeyEventWindowDelegate(const KeyEventWindowDelegate&) = delete;
  KeyEventWindowDelegate& operator=(const KeyEventWindowDelegate&) = delete;

  void OnKeyEvent(ui::KeyEvent* event) override {
    key_code_ = event->key_code();
  }

  ui::KeyboardCode GetReceivedKeyCodeAndReset() {
    ui::KeyboardCode tmp = key_code_;
    key_code_ = ui::VKEY_UNKNOWN;
    return tmp;
  }

 private:
  ui::KeyboardCode key_code_ = ui::VKEY_UNKNOWN;
};

}  // namespace

// Test fixture used for testing power button screenshot behavior under tablet
// power button.
class PowerButtonScreenshotControllerTest : public PowerButtonTestBase {
 public:
  PowerButtonScreenshotControllerTest() = default;

  PowerButtonScreenshotControllerTest(
      const PowerButtonScreenshotControllerTest&) = delete;
  PowerButtonScreenshotControllerTest& operator=(
      const PowerButtonScreenshotControllerTest&) = delete;

  ~PowerButtonScreenshotControllerTest() override = default;

  // PowerButtonTestBase:
  void SetUp() override {
    PowerButtonTestBase::SetUp();
    InitPowerButtonControllerMembers(
        chromeos::PowerManagerClient::TabletMode::ON);
    InitScreenshotTestApi();
    EnableTabletMode(true);

    // Advance a duration longer than |kIgnorePowerButtonAfterResumeDelay| to
    // avoid events being ignored.
    tick_clock_.Advance(
        PowerButtonController::kIgnorePowerButtonAfterResumeDelay +
        base::Milliseconds(2));

    ResetScreenshotCount();
  }

 protected:
  // PowerButtonTestBase:
  void PressKey(ui::KeyboardCode key_code) override {
    last_key_event_ = std::make_unique<ui::KeyEvent>(ui::EventType::kKeyPressed,
                                                     key_code, ui::EF_NONE);
    screenshot_controller_->OnKeyEvent(last_key_event_.get());
  }
  void ReleaseKey(ui::KeyboardCode key_code) override {
    last_key_event_ = std::make_unique<ui::KeyEvent>(
        ui::EventType::kKeyReleased, key_code, ui::EF_NONE);
    screenshot_controller_->OnKeyEvent(last_key_event_.get());
  }

  void InitScreenshotTestApi() {
    screenshot_test_api_ =
        std::make_unique<PowerButtonScreenshotControllerTestApi>(
            screenshot_controller_);
  }

  int GetScreenshotCount() const {
    constexpr char kClamshellHistogram[] =
        "Ash.CaptureModeController.EntryPoint.ClamshellMode";
    constexpr char kTabletHistogram[] =
        "Ash.CaptureModeController.EntryPoint.TabletMode";
    if (display::Screen::GetScreen()->InTabletMode()) {
      return histogram_tester_->GetBucketCount(
          kTabletHistogram, CaptureModeEntryType::kCaptureAllDisplays);
    }

    return histogram_tester_->GetBucketCount(
        kClamshellHistogram, CaptureModeEntryType::kCaptureAllDisplays);
  }

  void ResetScreenshotCount() {
    histogram_tester_ = std::make_unique<base::HistogramTester>();
  }

  bool LastKeyConsumed() const {
    DCHECK(last_key_event_);
    return last_key_event_->stopped_propagation();
  }

  std::unique_ptr<PowerButtonScreenshotControllerTestApi> screenshot_test_api_;

  // Stores the last key event. Can be NULL if not set through PressKey() or
  // ReleaseKey().
  std::unique_ptr<ui::KeyEvent> last_key_event_;

  // Used to test capture mode invocations when the feature is on.
  std::unique_ptr<base::HistogramTester> histogram_tester_;
};

class PowerButtonScreenshotControllerWithSystemKeysTest
    : public PowerButtonScreenshotControllerTest,
      public ::testing::WithParamInterface<bool> {
 public:
  PowerButtonScreenshotControllerWithSystemKeysTest() = default;
  ~PowerButtonScreenshotControllerWithSystemKeysTest() override = default;

  PowerButtonScreenshotControllerWithSystemKeysTest(
      const PowerButtonScreenshotControllerWithSystemKeysTest&) = delete;
  PowerButtonScreenshotControllerWithSystemKeysTest& operator=(
      const PowerButtonScreenshotControllerWithSystemKeysTest&) = delete;

  void SetUp() override {
    PowerButtonScreenshotControllerTest::SetUp();
    if (GetParam()) {
      aura::Window* window =
          CreateTestWindowInShellWithDelegate(&delegate_, 1, gfx::Rect());
      window->SetProperty(ash::kCanConsumeSystemKeysKey, true);
    }
  }

 private:
  KeyEventWindowDelegate delegate_;
};

// Tests the functionalities that press the power button first and then press
// volume down and volume up key alternative.
TEST_P(PowerButtonScreenshotControllerWithSystemKeysTest,
       PowerButtonPressedFirst_Screenshot) {
  PressPowerButton();
  tick_clock_.Advance(PowerButtonScreenshotController::kScreenshotChordDelay -
                      base::Milliseconds(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::Milliseconds(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::Milliseconds(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::Milliseconds(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());
}

INSTANTIATE_TEST_SUITE_P(All,
                         PowerButtonScreenshotControllerWithSystemKeysTest,
                         testing::Bool());

// 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::Milliseconds(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::Milliseconds(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::Milliseconds(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::Milliseconds(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());
}

// If the window with kConsumeSystemKeysKey property is active in tablet mode,
// volume keys will be passed to the window if they are pressed first.
TEST_F(PowerButtonScreenshotControllerTest, WindowWithSystemKeys) {
  EnableTabletMode(true);

  KeyEventWindowDelegate delegate;
  std::unique_ptr<aura::Window> window = base::WrapUnique(
      CreateTestWindowInShellWithDelegate(&delegate, 1, gfx::Rect()));
  window->SetProperty(ash::kCanConsumeSystemKeysKey, true);
  ::wm::ActivateWindow(window.get());

  // Tests when volume up pressed first, it's consumed by an app.
  // screenshot chord.
  GetEventGenerator()->PressKey(ui::VKEY_VOLUME_UP, ui::EF_NONE);
  EXPECT_EQ(ui::VKEY_VOLUME_UP, delegate.GetReceivedKeyCodeAndReset());

  GetEventGenerator()->PressKey(ui::VKEY_VOLUME_UP, ui::EF_NONE);
  EXPECT_EQ(ui::VKEY_VOLUME_UP, delegate.GetReceivedKeyCodeAndReset());
  EXPECT_EQ(0, GetScreenshotCount());

  // Tests when volume down pressed first, it's consumed by an app.
  // screenshot chord.
  GetEventGenerator()->PressKey(ui::VKEY_VOLUME_DOWN, ui::EF_NONE);
  EXPECT_EQ(ui::VKEY_VOLUME_DOWN, delegate.GetReceivedKeyCodeAndReset());

  GetEventGenerator()->PressKey(ui::VKEY_VOLUME_DOWN, ui::EF_NONE);
  EXPECT_EQ(ui::VKEY_VOLUME_DOWN, delegate.GetReceivedKeyCodeAndReset());
  EXPECT_EQ(0, GetScreenshotCount());

  // Delete the window, and volume will be consumed by shortcut.
  // Screenshot using up.
  window.reset();
  GetEventGenerator()->PressKey(ui::VKEY_VOLUME_UP, ui::EF_NONE);
  EXPECT_EQ(ui::VKEY_UNKNOWN, delegate.GetReceivedKeyCodeAndReset());
  PressPowerButton();
  ReleasePowerButton();
  GetEventGenerator()->ReleaseKey(ui::VKEY_VOLUME_UP, ui::EF_NONE);
  EXPECT_EQ(ui::VKEY_UNKNOWN, delegate.GetReceivedKeyCodeAndReset());
  EXPECT_EQ(1, GetScreenshotCount());

  // Screenshot using down.
  GetEventGenerator()->PressKey(ui::VKEY_VOLUME_DOWN, ui::EF_NONE);
  EXPECT_EQ(ui::VKEY_UNKNOWN, delegate.GetReceivedKeyCodeAndReset());
  PressPowerButton();
  ReleasePowerButton();
  GetEventGenerator()->ReleaseKey(ui::VKEY_VOLUME_DOWN, ui::EF_NONE);
  EXPECT_EQ(ui::VKEY_UNKNOWN, delegate.GetReceivedKeyCodeAndReset());
  EXPECT_EQ(2, GetScreenshotCount());
}

class PowerButtonScreenshotControllerWithKeyCodeTest
    : public PowerButtonScreenshotControllerTest,
      public testing::WithParamInterface<ui::KeyboardCode> {
 public:
  PowerButtonScreenshotControllerWithKeyCodeTest() : key_code_(GetParam()) {}

  PowerButtonScreenshotControllerWithKeyCodeTest(
      const PowerButtonScreenshotControllerWithKeyCodeTest&) = delete;
  PowerButtonScreenshotControllerWithKeyCodeTest& operator=(
      const PowerButtonScreenshotControllerWithKeyCodeTest&) = delete;

  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;
};

// 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 in tablet mode.
  ResetScreenshotCount();
  EnableTabletMode(false);
  PressKey(key_code());
  PressPowerButton();
  ReleaseKey(key_code());
  ReleasePowerButton();
  EXPECT_EQ(0, GetScreenshotCount());
}

// 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/up is pressed.
  PressPowerButton();
  ReleasePowerButton();
  PressKey(key_code());
  ReleaseKey(key_code());
  EXPECT_EQ(0, GetScreenshotCount());
}

// Tests power button is pressed first and meets screenshot chord condition.
TEST_P(PowerButtonScreenshotControllerWithKeyCodeTest,
       PowerButtonPressedFirst_ScreenshotChord) {
  PressPowerButton();
  tick_clock_.Advance(PowerButtonScreenshotController::kScreenshotChordDelay -
                      base::Milliseconds(2));
  PressKey(key_code());
  // Verifies screenshot is taken, volume down/up is consumed.
  EXPECT_EQ(1, GetScreenshotCount());
  EXPECT_TRUE(LastKeyConsumed());
  // 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::Milliseconds(1));
  ResetScreenshotCount();
  PressKey(key_code());
  EXPECT_EQ(0, GetScreenshotCount());
  EXPECT_TRUE(LastKeyConsumed());
  // 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::Milliseconds(2));
  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/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/up pressed but
// doesn't meet screenshot chord condition.
TEST_P(PowerButtonScreenshotControllerWithKeyCodeTest,
       PowerButtonPressedFirst_NoScreenshotChord) {
  PressPowerButton();
  tick_clock_.Advance(PowerButtonScreenshotController::kScreenshotChordDelay +
                      base::Milliseconds(1));
  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/up key should continue triggerring volume
  // down/up.
  tick_clock_.Advance(base::Milliseconds(2));
  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/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_P(PowerButtonScreenshotControllerWithKeyCodeTest,
       PowerButtonPressedFirst_VolumeKeyCancelPowerButton) {
  // 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(key_code());
  EXPECT_FALSE(power_button_test_api_->PowerButtonMenuTimerIsRunning());
  ReleasePowerButton();
  ReleaseKey(key_code());
  EXPECT_FALSE(power_manager_client()->backlights_forced_off());
}

// Tests volume key pressed can not cancel the started pre-shutdown animation.
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(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/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(key_code());
  EXPECT_TRUE(LastKeyConsumed());
  // Presses power button under screenshot chord condition, and verifies that
  // screenshot is taken.
  tick_clock_.Advance(PowerButtonScreenshotController::kScreenshotChordDelay -
                      base::Milliseconds(2));
  PressPowerButton();
  EXPECT_EQ(1, GetScreenshotCount());
  // 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::Milliseconds(1));
  ResetScreenshotCount();
  PressKey(key_code());
  EXPECT_EQ(0, GetScreenshotCount());
  EXPECT_TRUE(LastKeyConsumed());
  // 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::Milliseconds(2));
  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/up key, and verifies nothing happens.
  ReleaseKey(key_code());
  EXPECT_EQ(0, GetScreenshotCount());
  EXPECT_FALSE(LastKeyConsumed());
}

// Tests volume down/up key pressed first, and then power button pressed but
// doesn't meet screenshot chord condition.
TEST_P(PowerButtonScreenshotControllerWithKeyCodeTest,
       VolumeKeyPressedFirst_NoScreenshotChord) {
  // Tests when volume down/up pressed first, it waits for power button pressed
  // screenshot chord.
  PressKey(key_code());
  EXPECT_TRUE(LastKeyConsumed());
  // Advances |tick_clock_| to off screenshot chord point. This will also
  // trigger volume down/up timer timeout, which will perform a volume down/up
  // operation.
  tick_clock_.Advance(PowerButtonScreenshotController::kScreenshotChordDelay +
                      base::Milliseconds(1));
  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/up key should continue triggerring volume
  // down/up.
  tick_clock_.Advance(base::Milliseconds(2));
  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/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_P(PowerButtonScreenshotControllerWithKeyCodeTest,
       VolumeKeyPressedFirst_InvalidateConvertiblePowerButton) {
  // Tests volume down/up key invalidates the power button behavior.
  PressKey(key_code());
  PressPowerButton();
  EXPECT_FALSE(power_button_test_api_->PowerButtonMenuTimerIsRunning());
  ReleasePowerButton();
  ReleaseKey(key_code());
  EXPECT_FALSE(power_manager_client()->backlights_forced_off());
}

INSTANTIATE_TEST_SUITE_P(AshPowerButtonScreenshot,
                         PowerButtonScreenshotControllerWithKeyCodeTest,
                         testing::Values(ui::VKEY_VOLUME_DOWN,
                                         ui::VKEY_VOLUME_UP));

}  // namespace ash