Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(631)

Side by Side Diff: athena/input/input_manager_unittest.cc

Issue 654343002: Intorduce PowerButtonObserver API (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: speculative fix Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « athena/input/input_manager_impl.cc ('k') | athena/input/power_button_controller.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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.
4
5 #include "athena/input/input_manager_impl.h"
6
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"
11
12 namespace athena {
13 namespace {
14
15 class TestPowerButtonObserver : public PowerButtonObserver {
16 public:
17 TestPowerButtonObserver() : count_(0), state_(RELEASED) {
18 InputManager::Get()->AddPowerButtonObserver(this);
19 }
20 virtual ~TestPowerButtonObserver() {
21 InputManager::Get()->RemovePowerButtonObserver(this);
22 }
23
24 int count() const { return count_; }
25 State state() const { return state_; }
26
27 bool WaitForLongPress() {
28 run_loop_.Run();
29 return state_ == LONG_PRESSED;
30 }
31
32 private:
33 virtual void OnPowerButtonStateChanged(
34 PowerButtonObserver::State state) override {
35 state_ = state;
36 count_++;
37 if (state == LONG_PRESSED) {
38 DCHECK(run_loop_.running());
39 run_loop_.Quit();
40 }
41 }
42 base::RunLoop run_loop_;
43 int count_;
44 State state_;
45
46 DISALLOW_COPY_AND_ASSIGN(TestPowerButtonObserver);
47 };
48
49 } // namespace
50
51 namespace test {
52
53 class ScopedPowerButtonTimeoutShortener {
54 public:
55 ScopedPowerButtonTimeoutShortener()
56 : original_timeout_(
57 GetInputManagerImpl()->SetPowerButtonTimeoutMsForTest(1)) {}
58 ~ScopedPowerButtonTimeoutShortener() {
59 GetInputManagerImpl()->SetPowerButtonTimeoutMsForTest(original_timeout_);
60 }
61
62 private:
63 InputManagerImpl* GetInputManagerImpl() {
64 return static_cast<InputManagerImpl*>(InputManager::Get());
65 }
66
67 int original_timeout_;
68 DISALLOW_COPY_AND_ASSIGN(ScopedPowerButtonTimeoutShortener);
69 };
70
71 } // namespace test
72
73 typedef test::AthenaTestBase InputManagerTest;
74
75 TEST_F(InputManagerTest, PowerButton) {
76 test::ScopedPowerButtonTimeoutShortener shortener;
77 TestPowerButtonObserver observer;
78
79 ui::test::EventGenerator generator(root_window());
80 generator.PressKey(ui::VKEY_P, ui::EF_NONE);
81 EXPECT_EQ(0, observer.count());
82
83 // Test short press.
84 generator.PressKey(ui::VKEY_P, ui::EF_ALT_DOWN);
85 EXPECT_EQ(1, observer.count());
86 EXPECT_EQ(PowerButtonObserver::PRESSED, observer.state());
87 generator.ReleaseKey(ui::VKEY_P, ui::EF_ALT_DOWN);
88 EXPECT_EQ(2, observer.count());
89 EXPECT_EQ(PowerButtonObserver::RELEASED, observer.state());
90
91 // Test long press.
92 generator.PressKey(ui::VKEY_P, ui::EF_ALT_DOWN);
93 EXPECT_EQ(3, observer.count());
94 EXPECT_EQ(PowerButtonObserver::PRESSED, observer.state());
95
96 EXPECT_TRUE(observer.WaitForLongPress());
97 EXPECT_EQ(4, observer.count());
98 EXPECT_EQ(PowerButtonObserver::LONG_PRESSED, observer.state());
99
100 generator.ReleaseKey(ui::VKEY_P, ui::EF_ALT_DOWN);
101 EXPECT_EQ(5, observer.count());
102 EXPECT_EQ(PowerButtonObserver::RELEASED, observer.state());
103 }
104
105 } // namespace athena
OLDNEW
« no previous file with comments | « athena/input/input_manager_impl.cc ('k') | athena/input/power_button_controller.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698