1 // Copyright (c) 2012 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 "ash/accelerators/accelerator_controller.h"
6 #include "ash/session_state_delegate.h"
8 #include "ash/shell_window_ids.h"
9 #include "ash/test/ash_test_base.h"
10 #include "base/bind.h"
11 #include "base/event_types.h"
12 #include "base/message_loop/message_loop.h"
13 #include "ui/aura/client/dispatcher_client.h"
14 #include "ui/aura/root_window.h"
15 #include "ui/aura/test/test_windows.h"
16 #include "ui/aura/window.h"
17 #include "ui/base/accelerators/accelerator.h"
18 #include "ui/events/event_constants.h"
19 #include "ui/events/event_utils.h"
23 #include "ui/events/x/events_x_utils.h"
31 class MockDispatcher : public base::MessageLoop::Dispatcher {
33 MockDispatcher() : num_key_events_dispatched_(0) {
36 int num_key_events_dispatched() { return num_key_events_dispatched_; }
38 #if defined(OS_WIN) || defined(USE_X11) || defined(USE_OZONE)
39 virtual bool Dispatch(const base::NativeEvent& event) OVERRIDE {
40 if (ui::EventTypeFromNative(event) == ui::ET_KEY_RELEASED)
41 num_key_events_dispatched_++;
42 return !ui::IsNoopEvent(event);
47 int num_key_events_dispatched_;
50 class TestTarget : public ui::AcceleratorTarget {
52 TestTarget() : accelerator_pressed_count_(0) {
54 virtual ~TestTarget() {
57 int accelerator_pressed_count() const {
58 return accelerator_pressed_count_;
61 // Overridden from ui::AcceleratorTarget:
62 virtual bool AcceleratorPressed(const ui::Accelerator& accelerator) OVERRIDE {
63 accelerator_pressed_count_++;
66 virtual bool CanHandleAccelerators() const OVERRIDE {
71 int accelerator_pressed_count_;
73 DISALLOW_COPY_AND_ASSIGN(TestTarget);
76 void DispatchKeyReleaseA() {
77 // Sending both keydown and keyup is necessary here because the accelerator
78 // manager only checks a keyup event following a keydown event. See
79 // ShouldHandle() in ui/base/accelerators/accelerator_manager.cc for details.
81 MSG native_event_down = { NULL, WM_KEYDOWN, ui::VKEY_A, 0 };
82 ash::Shell::GetPrimaryRootWindow()->PostNativeEvent(native_event_down);
83 MSG native_event_up = { NULL, WM_KEYUP, ui::VKEY_A, 0 };
84 ash::Shell::GetPrimaryRootWindow()->PostNativeEvent(native_event_up);
85 #elif defined(USE_X11)
87 ui::InitXKeyEventForTesting(ui::ET_KEY_PRESSED,
91 aura::WindowEventDispatcher* dispatcher =
92 ash::Shell::GetPrimaryRootWindow()->GetDispatcher();
93 dispatcher->PostNativeEvent(&native_event);
94 ui::InitXKeyEventForTesting(ui::ET_KEY_RELEASED,
98 dispatcher->PostNativeEvent(&native_event);
101 // Send noop event to signal dispatcher to exit.
102 dispatcher->PostNativeEvent(ui::CreateNoopEvent());
107 typedef AshTestBase NestedDispatcherTest;
109 // Aura window below lock screen in z order.
110 TEST_F(NestedDispatcherTest, AssociatedWindowBelowLockScreen) {
111 MockDispatcher inner_dispatcher;
112 scoped_ptr<aura::Window> associated_window(CreateTestWindowInShellWithId(0));
114 Shell::GetInstance()->session_state_delegate()->LockScreen();
115 DispatchKeyReleaseA();
116 aura::Window* root_window = ash::Shell::GetPrimaryRootWindow();
117 aura::client::GetDispatcherClient(root_window)->RunWithDispatcher(
119 associated_window.get(),
120 true /* nestable_tasks_allowed */);
121 EXPECT_EQ(0, inner_dispatcher.num_key_events_dispatched());
122 Shell::GetInstance()->session_state_delegate()->UnlockScreen();
125 // Aura window above lock screen in z order.
126 TEST_F(NestedDispatcherTest, AssociatedWindowAboveLockScreen) {
127 MockDispatcher inner_dispatcher;
129 scoped_ptr<aura::Window>mock_lock_container(
130 CreateTestWindowInShellWithId(0));
131 aura::test::CreateTestWindowWithId(0, mock_lock_container.get());
132 scoped_ptr<aura::Window> associated_window(CreateTestWindowInShellWithId(0));
133 EXPECT_TRUE(aura::test::WindowIsAbove(associated_window.get(),
134 mock_lock_container.get()));
136 DispatchKeyReleaseA();
137 aura::Window* root_window = ash::Shell::GetPrimaryRootWindow();
138 aura::client::GetDispatcherClient(root_window)->RunWithDispatcher(
140 associated_window.get(),
141 true /* nestable_tasks_allowed */);
142 EXPECT_EQ(1, inner_dispatcher.num_key_events_dispatched());
145 // Test that the nested dispatcher handles accelerators.
146 TEST_F(NestedDispatcherTest, AcceleratorsHandled) {
147 MockDispatcher inner_dispatcher;
148 aura::Window* root_window = ash::Shell::GetPrimaryRootWindow();
150 ui::Accelerator accelerator(ui::VKEY_A, ui::EF_NONE);
151 accelerator.set_type(ui::ET_KEY_RELEASED);
153 Shell::GetInstance()->accelerator_controller()->Register(accelerator,
156 DispatchKeyReleaseA();
157 aura::client::GetDispatcherClient(root_window)->RunWithDispatcher(
160 true /* nestable_tasks_allowed */);
161 EXPECT_EQ(0, inner_dispatcher.num_key_events_dispatched());
162 EXPECT_EQ(1, target.accelerator_pressed_count());