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"
17 class TestPowerButtonObserver
: public PowerButtonObserver
{
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() {
31 return state_
== LONG_PRESSED
;
35 virtual void OnPowerButtonStateChanged(
36 PowerButtonObserver::State state
) override
{
39 if (state
== LONG_PRESSED
) {
40 DCHECK(run_loop_
.running());
44 base::RunLoop run_loop_
;
48 DISALLOW_COPY_AND_ASSIGN(TestPowerButtonObserver
);
51 class InputManagerTest
: public test::AthenaTestBase
{
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();
63 DISALLOW_COPY_AND_ASSIGN(InputManagerTest
);
70 class ScopedPowerButtonTimeoutShortener
{
72 ScopedPowerButtonTimeoutShortener()
74 GetInputManagerImpl()->SetPowerButtonTimeoutMsForTest(1)) {}
75 ~ScopedPowerButtonTimeoutShortener() {
76 GetInputManagerImpl()->SetPowerButtonTimeoutMsForTest(original_timeout_
);
80 InputManagerImpl
* GetInputManagerImpl() {
81 return static_cast<InputManagerImpl
*>(InputManager::Get());
84 int original_timeout_
;
85 DISALLOW_COPY_AND_ASSIGN(ScopedPowerButtonTimeoutShortener
);
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());
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());
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