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 "base/run_loop.h"
10 #include "ui/events/test/event_generator.h"
15 class TestPowerButtonObserver
: public PowerButtonObserver
{
17 TestPowerButtonObserver() : count_(0), state_(RELEASED
) {
18 InputManager::Get()->AddPowerButtonObserver(this);
20 ~TestPowerButtonObserver() override
{
21 InputManager::Get()->RemovePowerButtonObserver(this);
24 int count() const { return count_
; }
25 State
state() const { return state_
; }
27 bool WaitForLongPress() {
29 return state_
== LONG_PRESSED
;
33 virtual void OnPowerButtonStateChanged(
34 PowerButtonObserver::State state
) override
{
37 if (state
== LONG_PRESSED
) {
38 DCHECK(run_loop_
.running());
42 base::RunLoop run_loop_
;
46 DISALLOW_COPY_AND_ASSIGN(TestPowerButtonObserver
);
53 class ScopedPowerButtonTimeoutShortener
{
55 ScopedPowerButtonTimeoutShortener()
57 GetInputManagerImpl()->SetPowerButtonTimeoutMsForTest(1)) {}
58 ~ScopedPowerButtonTimeoutShortener() {
59 GetInputManagerImpl()->SetPowerButtonTimeoutMsForTest(original_timeout_
);
63 InputManagerImpl
* GetInputManagerImpl() {
64 return static_cast<InputManagerImpl
*>(InputManager::Get());
67 int original_timeout_
;
68 DISALLOW_COPY_AND_ASSIGN(ScopedPowerButtonTimeoutShortener
);
73 typedef test::AthenaTestBase InputManagerTest
;
75 // This fails on bots. crbug.com/424109.
76 // Investigate once crbug.com/424093 is fixed.
77 TEST_F(InputManagerTest
, DISABLED_PowerButton
) {
78 test::ScopedPowerButtonTimeoutShortener shortener
;
79 TestPowerButtonObserver observer
;
81 ui::test::EventGenerator
generator(root_window());
82 generator
.PressKey(ui::VKEY_P
, ui::EF_NONE
);
83 EXPECT_EQ(0, observer
.count());
86 generator
.PressKey(ui::VKEY_P
, ui::EF_ALT_DOWN
);
87 EXPECT_EQ(1, observer
.count());
88 EXPECT_EQ(PowerButtonObserver::PRESSED
, observer
.state());
89 generator
.ReleaseKey(ui::VKEY_P
, ui::EF_ALT_DOWN
);
90 EXPECT_EQ(2, observer
.count());
91 EXPECT_EQ(PowerButtonObserver::RELEASED
, observer
.state());
94 generator
.PressKey(ui::VKEY_P
, ui::EF_ALT_DOWN
);
95 EXPECT_EQ(3, observer
.count());
96 EXPECT_EQ(PowerButtonObserver::PRESSED
, observer
.state());
98 EXPECT_TRUE(observer
.WaitForLongPress());
99 EXPECT_EQ(4, observer
.count());
100 EXPECT_EQ(PowerButtonObserver::LONG_PRESSED
, observer
.state());
102 generator
.ReleaseKey(ui::VKEY_P
, ui::EF_ALT_DOWN
);
103 EXPECT_EQ(5, observer
.count());
104 EXPECT_EQ(PowerButtonObserver::RELEASED
, observer
.state());
107 } // namespace athena