Disable some more flaky AppWindowInterceptAllKeysTest.* on Windows
[chromium-blink-merge.git] / athena / input / input_manager_unittest.cc
blob5b0305bb24054d6e09d648bf54f8c4da83258e90
1 // Copyright 2014 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.
5 #include "athena/input/input_manager_impl.h"
7 #include "athena/input/public/accelerator_manager.h"
8 #include "athena/test/base/athena_test_base.h"
9 #include "athena/util/switches.h"
10 #include "base/command_line.h"
11 #include "base/run_loop.h"
12 #include "ui/events/test/event_generator.h"
14 namespace athena {
15 namespace {
17 class TestPowerButtonObserver : public PowerButtonObserver {
18 public:
19 TestPowerButtonObserver() : count_(0), state_(RELEASED) {
20 InputManager::Get()->AddPowerButtonObserver(this);
22 ~TestPowerButtonObserver() override {
23 InputManager::Get()->RemovePowerButtonObserver(this);
26 int count() const { return count_; }
27 State state() const { return state_; }
29 bool WaitForLongPress() {
30 run_loop_.Run();
31 return state_ == LONG_PRESSED;
34 private:
35 virtual void OnPowerButtonStateChanged(
36 PowerButtonObserver::State state) override {
37 state_ = state;
38 count_++;
39 if (state == LONG_PRESSED) {
40 DCHECK(run_loop_.running());
41 run_loop_.Quit();
44 base::RunLoop run_loop_;
45 int count_;
46 State state_;
48 DISALLOW_COPY_AND_ASSIGN(TestPowerButtonObserver);
51 class InputManagerTest : public test::AthenaTestBase {
52 public:
53 InputManagerTest() {}
54 ~InputManagerTest() override {}
56 void SetUp() override {
57 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
58 command_line->AppendSwitch(switches::kEnableDebugAccelerators);
59 test::AthenaTestBase::SetUp();
62 private:
63 DISALLOW_COPY_AND_ASSIGN(InputManagerTest);
66 } // namespace
68 namespace test {
70 class ScopedPowerButtonTimeoutShortener {
71 public:
72 ScopedPowerButtonTimeoutShortener()
73 : original_timeout_(
74 GetInputManagerImpl()->SetPowerButtonTimeoutMsForTest(1)) {}
75 ~ScopedPowerButtonTimeoutShortener() {
76 GetInputManagerImpl()->SetPowerButtonTimeoutMsForTest(original_timeout_);
79 private:
80 InputManagerImpl* GetInputManagerImpl() {
81 return static_cast<InputManagerImpl*>(InputManager::Get());
84 int original_timeout_;
85 DISALLOW_COPY_AND_ASSIGN(ScopedPowerButtonTimeoutShortener);
88 } // namespace test
90 TEST_F(InputManagerTest, PowerButton) {
91 test::ScopedPowerButtonTimeoutShortener shortener;
92 TestPowerButtonObserver observer;
94 ui::test::EventGenerator generator(root_window());
95 generator.PressKey(ui::VKEY_P, ui::EF_NONE);
96 EXPECT_EQ(0, observer.count());
98 // Test short press.
99 generator.PressKey(ui::VKEY_P, ui::EF_ALT_DOWN);
100 EXPECT_EQ(1, observer.count());
101 EXPECT_EQ(PowerButtonObserver::PRESSED, observer.state());
102 generator.ReleaseKey(ui::VKEY_P, ui::EF_ALT_DOWN);
103 EXPECT_EQ(2, observer.count());
104 EXPECT_EQ(PowerButtonObserver::RELEASED, observer.state());
106 // Test long press.
107 generator.PressKey(ui::VKEY_P, ui::EF_ALT_DOWN);
108 EXPECT_EQ(3, observer.count());
109 EXPECT_EQ(PowerButtonObserver::PRESSED, observer.state());
111 EXPECT_TRUE(observer.WaitForLongPress());
112 EXPECT_EQ(4, observer.count());
113 EXPECT_EQ(PowerButtonObserver::LONG_PRESSED, observer.state());
115 generator.ReleaseKey(ui::VKEY_P, ui::EF_ALT_DOWN);
116 EXPECT_EQ(5, observer.count());
117 EXPECT_EQ(PowerButtonObserver::RELEASED, observer.state());
120 } // namespace athena