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/public/accelerator_manager.h"
7 #include "athena/activity/public/activity.h"
8 #include "athena/activity/public/activity_factory.h"
9 #include "athena/input/public/input_manager.h"
10 #include "athena/test/base/athena_test_base.h"
11 #include "athena/wm/public/window_manager.h"
12 #include "ui/events/test/event_generator.h"
17 const int kInvalidCommandId
= -1;
19 class TestHandler
: public AcceleratorHandler
{
21 TestHandler() : fired_command_id_(kInvalidCommandId
), enabled_(true) {}
22 ~TestHandler() override
{}
24 void set_enabled(bool enabled
) { enabled_
= enabled
; }
26 int GetFiredCommandIdAndReset() {
27 int fired
= fired_command_id_
;
28 fired_command_id_
= kInvalidCommandId
;
33 // AcceleratorHandler:
34 virtual bool IsCommandEnabled(int command_id
) const override
{
37 virtual bool OnAcceleratorFired(int command_id
,
38 const ui::Accelerator
& accelerator
) override
{
39 fired_command_id_
= command_id
;
43 int fired_command_id_
;
46 DISALLOW_COPY_AND_ASSIGN(TestHandler
);
51 typedef test::AthenaTestBase AcceleratorManagerTest
;
53 TEST_F(AcceleratorManagerTest
, Basic
) {
61 const AcceleratorData data
[] = {
62 {TRIGGER_ON_PRESS
, ui::VKEY_A
, ui::EF_SHIFT_DOWN
, COMMAND_A
, AF_RESERVED
},
63 {TRIGGER_ON_RELEASE
, ui::VKEY_B
, ui::EF_SHIFT_DOWN
, COMMAND_B
,
65 {TRIGGER_ON_PRESS
, ui::VKEY_C
, ui::EF_SHIFT_DOWN
, COMMAND_C
,
66 AF_RESERVED
| AF_DEBUG
},
67 {TRIGGER_ON_PRESS
, ui::VKEY_D
, ui::EF_SHIFT_DOWN
, COMMAND_D
,
68 AF_RESERVED
| AF_NON_AUTO_REPEATABLE
},
69 {TRIGGER_ON_PRESS
, ui::VKEY_E
, ui::EF_SHIFT_DOWN
, COMMAND_E
, AF_NONE
},
71 AcceleratorManager
* accelerator_manager
=
72 InputManager::Get()->GetAcceleratorManager();
73 TestHandler test_handler
;
74 accelerator_manager
->RegisterAccelerators(
75 data
, arraysize(data
), &test_handler
);
77 ui::test::EventGenerator
generator(root_window());
78 generator
.PressKey(ui::VKEY_A
, ui::EF_NONE
);
79 EXPECT_EQ(kInvalidCommandId
, test_handler
.GetFiredCommandIdAndReset());
82 generator
.PressKey(ui::VKEY_A
, ui::EF_SHIFT_DOWN
);
83 EXPECT_EQ(COMMAND_A
, test_handler
.GetFiredCommandIdAndReset());
84 generator
.ReleaseKey(ui::VKEY_A
, ui::EF_SHIFT_DOWN
);
85 EXPECT_EQ(kInvalidCommandId
, test_handler
.GetFiredCommandIdAndReset());
87 // Trigger on release.
88 generator
.PressKey(ui::VKEY_B
, ui::EF_SHIFT_DOWN
);
89 EXPECT_EQ(kInvalidCommandId
, test_handler
.GetFiredCommandIdAndReset());
90 generator
.ReleaseKey(ui::VKEY_B
, ui::EF_SHIFT_DOWN
);
91 EXPECT_EQ(COMMAND_B
, test_handler
.GetFiredCommandIdAndReset());
94 test_handler
.set_enabled(false);
95 generator
.PressKey(ui::VKEY_A
, ui::EF_SHIFT_DOWN
);
96 EXPECT_EQ(kInvalidCommandId
, test_handler
.GetFiredCommandIdAndReset());
97 test_handler
.set_enabled(true);
98 generator
.PressKey(ui::VKEY_A
, ui::EF_SHIFT_DOWN
);
99 EXPECT_EQ(COMMAND_A
, test_handler
.GetFiredCommandIdAndReset());
101 // Debug accelerators.
102 accelerator_manager
->SetDebugAcceleratorsEnabled(false);
103 generator
.PressKey(ui::VKEY_C
, ui::EF_SHIFT_DOWN
);
104 EXPECT_EQ(kInvalidCommandId
, test_handler
.GetFiredCommandIdAndReset());
105 accelerator_manager
->SetDebugAcceleratorsEnabled(true);
106 generator
.PressKey(ui::VKEY_C
, ui::EF_SHIFT_DOWN
);
107 EXPECT_EQ(COMMAND_C
, test_handler
.GetFiredCommandIdAndReset());
108 accelerator_manager
->SetDebugAcceleratorsEnabled(false);
110 // Non auto repeatable
111 generator
.PressKey(ui::VKEY_D
, ui::EF_SHIFT_DOWN
);
112 EXPECT_EQ(COMMAND_D
, test_handler
.GetFiredCommandIdAndReset());
113 generator
.PressKey(ui::VKEY_D
, ui::EF_SHIFT_DOWN
| ui::EF_IS_REPEAT
);
114 EXPECT_EQ(kInvalidCommandId
, test_handler
.GetFiredCommandIdAndReset());
116 // TODO(oshima): Add scenario where the key event is consumed by
118 generator
.PressKey(ui::VKEY_E
, ui::EF_SHIFT_DOWN
);
119 EXPECT_EQ(COMMAND_E
, test_handler
.GetFiredCommandIdAndReset());
122 TEST_F(AcceleratorManagerTest
, CloseActivity
) {
123 ActivityFactory
* factory
= ActivityFactory::Get();
124 Activity
* activity1
=
125 factory
->CreateWebActivity(NULL
, base::string16(), GURL());
126 Activity::Show(activity1
);
127 Activity::Delete(activity1
);
129 Activity
* activity2
=
130 factory
->CreateWebActivity(NULL
, base::string16(), GURL());
131 Activity::Show(activity2
);
133 // TODO(oshima): This shouldn't be necessary. Remove this once
134 // crbug.com/427113 is fixed.
135 RunAllPendingInMessageLoop();
137 ui::test::EventGenerator
generator(root_window());
138 generator
.PressKey(ui::VKEY_F6
, ui::EF_NONE
);
139 EXPECT_TRUE(WindowManager::Get()->IsOverviewModeActive());
140 Activity::Delete(activity2
);
143 } // namespace athena