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 "ui/wm/core/nested_accelerator_controller.h"
8 #include "base/event_types.h"
9 #include "base/message_loop/message_loop.h"
10 #include "ui/aura/test/aura_test_base.h"
11 #include "ui/aura/test/test_windows.h"
12 #include "ui/aura/window.h"
13 #include "ui/aura/window_event_dispatcher.h"
14 #include "ui/base/accelerators/accelerator.h"
15 #include "ui/base/accelerators/accelerator.h"
16 #include "ui/base/accelerators/accelerator_manager.h"
17 #include "ui/events/event_constants.h"
18 #include "ui/events/event_utils.h"
19 #include "ui/events/platform/platform_event_dispatcher.h"
20 #include "ui/events/platform/platform_event_source.h"
21 #include "ui/events/platform/scoped_event_dispatcher.h"
22 #include "ui/wm/core/nested_accelerator_delegate.h"
23 #include "ui/wm/public/dispatcher_client.h"
27 #include "ui/aura/test/x11_event_sender.h"
28 #include "ui/events/test/events_test_utils_x11.h"
36 class MockDispatcher
: public ui::PlatformEventDispatcher
{
38 MockDispatcher() : num_key_events_dispatched_(0) {}
40 int num_key_events_dispatched() { return num_key_events_dispatched_
; }
43 // ui::PlatformEventDispatcher:
44 bool CanDispatchEvent(const ui::PlatformEvent
& event
) override
{
47 uint32_t DispatchEvent(const ui::PlatformEvent
& event
) override
{
48 if (ui::EventTypeFromNative(event
) == ui::ET_KEY_RELEASED
)
49 num_key_events_dispatched_
++;
50 return ui::POST_DISPATCH_NONE
;
53 int num_key_events_dispatched_
;
55 DISALLOW_COPY_AND_ASSIGN(MockDispatcher
);
58 class TestTarget
: public ui::AcceleratorTarget
{
60 TestTarget() : accelerator_pressed_count_(0) {}
61 ~TestTarget() override
{}
63 int accelerator_pressed_count() const { return accelerator_pressed_count_
; }
65 // Overridden from ui::AcceleratorTarget:
66 bool AcceleratorPressed(const ui::Accelerator
& accelerator
) override
{
67 accelerator_pressed_count_
++;
70 bool CanHandleAccelerators() const override
{ return true; }
73 int accelerator_pressed_count_
;
75 DISALLOW_COPY_AND_ASSIGN(TestTarget
);
78 void DispatchKeyReleaseA(aura::Window
* root_window
) {
79 // Sending both keydown and keyup is necessary here because the accelerator
80 // manager only checks a keyup event following a keydown event. See
81 // ShouldHandle() in ui/base/accelerators/accelerator_manager.cc for details.
83 aura::WindowTreeHost
* host
= root_window
->GetHost();
84 HWND hwnd
= host
->GetAcceleratedWidget();
85 ::PostMessage(hwnd
, WM_KEYDOWN
, ui::VKEY_A
, 0);
86 ::PostMessage(hwnd
, WM_KEYUP
, ui::VKEY_A
, 0);
87 #elif defined(USE_X11)
88 ui::ScopedXI2Event native_event
;
89 native_event
.InitKeyEvent(ui::ET_KEY_PRESSED
, ui::VKEY_A
, 0);
90 aura::WindowTreeHost
* host
= root_window
->GetHost();
91 aura::test::PostEventToWindowTreeHost(*native_event
, host
);
92 native_event
.InitKeyEvent(ui::ET_KEY_RELEASED
, ui::VKEY_A
, 0);
93 aura::test::PostEventToWindowTreeHost(*native_event
, host
);
95 // Make sure the inner message-loop terminates after dispatching the events.
96 base::MessageLoop::current()->PostTask(
97 FROM_HERE
, base::MessageLoop::current()->QuitClosure());
100 class MockNestedAcceleratorDelegate
: public NestedAcceleratorDelegate
{
102 MockNestedAcceleratorDelegate()
103 : accelerator_manager_(new ui::AcceleratorManager
) {}
104 ~MockNestedAcceleratorDelegate() override
{}
106 // NestedAcceleratorDelegate:
107 Result
ProcessAccelerator(const ui::Accelerator
& accelerator
) override
{
108 return accelerator_manager_
->Process(accelerator
) ?
109 RESULT_PROCESSED
: RESULT_NOT_PROCESSED
;
112 void Register(const ui::Accelerator
& accelerator
,
113 ui::AcceleratorTarget
* target
) {
114 accelerator_manager_
->Register(
115 accelerator
, ui::AcceleratorManager::kNormalPriority
, target
);
119 scoped_ptr
<ui::AcceleratorManager
> accelerator_manager_
;
121 DISALLOW_COPY_AND_ASSIGN(MockNestedAcceleratorDelegate
);
124 class NestedAcceleratorTest
: public aura::test::AuraTestBase
{
126 NestedAcceleratorTest() {}
127 ~NestedAcceleratorTest() override
{}
129 void SetUp() override
{
130 AuraTestBase::SetUp();
131 delegate_
= new MockNestedAcceleratorDelegate();
132 nested_accelerator_controller_
.reset(
133 new NestedAcceleratorController(delegate_
));
134 aura::client::SetDispatcherClient(root_window(),
135 nested_accelerator_controller_
.get());
138 void TearDown() override
{
139 aura::client::SetDispatcherClient(root_window(), NULL
);
140 AuraTestBase::TearDown();
142 nested_accelerator_controller_
.reset();
145 MockNestedAcceleratorDelegate
* delegate() { return delegate_
; }
148 scoped_ptr
<NestedAcceleratorController
> nested_accelerator_controller_
;
149 MockNestedAcceleratorDelegate
* delegate_
;
151 DISALLOW_COPY_AND_ASSIGN(NestedAcceleratorTest
);
156 // Aura window above lock screen in z order.
157 // http://crbug.com/396494
158 TEST_F(NestedAcceleratorTest
, DISABLED_AssociatedWindowAboveLockScreen
) {
159 // TODO(oshima|sadrul): remove when Win implements PES.
160 if (!ui::PlatformEventSource::GetInstance())
162 MockDispatcher inner_dispatcher
;
163 scoped_ptr
<aura::Window
> mock_lock_container(
164 CreateNormalWindow(0, root_window(), NULL
));
165 aura::test::CreateTestWindowWithId(1, mock_lock_container
.get());
167 scoped_ptr
<aura::Window
> associated_window(
168 CreateNormalWindow(2, root_window(), NULL
));
169 EXPECT_TRUE(aura::test::WindowIsAbove(associated_window
.get(),
170 mock_lock_container
.get()));
172 DispatchKeyReleaseA(root_window());
173 scoped_ptr
<ui::ScopedEventDispatcher
> override_dispatcher
=
174 ui::PlatformEventSource::GetInstance()->OverrideDispatcher(
176 aura::client::DispatcherRunLoop
run_loop(
177 aura::client::GetDispatcherClient(root_window()), NULL
);
179 EXPECT_EQ(1, inner_dispatcher
.num_key_events_dispatched());
182 // Test that the nested dispatcher handles accelerators.
183 // http://crbug.com/396494
184 TEST_F(NestedAcceleratorTest
, DISABLED_AcceleratorsHandled
) {
185 // TODO(oshima|sadrul): remove when Win implements PES.
186 if (!ui::PlatformEventSource::GetInstance())
188 MockDispatcher inner_dispatcher
;
189 ui::Accelerator
accelerator(ui::VKEY_A
, ui::EF_NONE
);
190 accelerator
.set_type(ui::ET_KEY_RELEASED
);
192 delegate()->Register(accelerator
, &target
);
194 DispatchKeyReleaseA(root_window());
195 scoped_ptr
<ui::ScopedEventDispatcher
> override_dispatcher
=
196 ui::PlatformEventSource::GetInstance()->OverrideDispatcher(
198 aura::client::DispatcherRunLoop
run_loop(
199 aura::client::GetDispatcherClient(root_window()), NULL
);
201 EXPECT_EQ(0, inner_dispatcher
.num_key_events_dispatched());
202 EXPECT_EQ(1, target
.accelerator_pressed_count());