Add per-day app launcher usage histogram
[chromium-blink-merge.git] / ash / accelerators / nested_dispatcher_controller_unittest.cc
blob4e42d6bb7cd6cbfff1c8ab3ccfc8bce7fce80e39
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/shell.h"
7 #include "ash/shell_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.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/base/events/event_constants.h"
19 #include "ui/base/events/event_utils.h"
21 #if defined(USE_X11)
22 #include <X11/Xlib.h>
23 #include "ui/base/x/x11_util.h"
24 #endif // USE_X11
26 namespace ash {
27 namespace test {
29 namespace {
31 class MockDispatcher : public MessageLoop::Dispatcher {
32 public:
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)
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);
44 #endif // defined(OS_WIN) || defined(USE_X11)
46 private:
47 int num_key_events_dispatched_;
50 class TestTarget : public ui::AcceleratorTarget {
51 public:
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_++;
64 return true;
66 virtual bool CanHandleAccelerators() const OVERRIDE {
67 return true;
70 private:
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.
80 #if defined(OS_WIN)
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)
86 XEvent native_event;
87 ui::InitXKeyEventForTesting(ui::ET_KEY_PRESSED,
88 ui::VKEY_A,
90 &native_event);
91 ash::Shell::GetPrimaryRootWindow()->PostNativeEvent(&native_event);
92 ui::InitXKeyEventForTesting(ui::ET_KEY_RELEASED,
93 ui::VKEY_A,
95 &native_event);
96 ash::Shell::GetPrimaryRootWindow()->PostNativeEvent(&native_event);
97 #endif
99 // Send noop event to signal dispatcher to exit.
100 ash::Shell::GetPrimaryRootWindow()->PostNativeEvent(ui::CreateNoopEvent());
103 } // namespace
105 typedef AshTestBase NestedDispatcherTest;
107 // Aura window below lock screen in z order.
108 TEST_F(NestedDispatcherTest, AssociatedWindowBelowLockScreen) {
109 MockDispatcher inner_dispatcher;
110 scoped_ptr<aura::Window> associated_window(CreateTestWindowInShellWithId(0));
112 Shell::GetInstance()->delegate()->LockScreen();
113 DispatchKeyReleaseA();
114 aura::RootWindow* root_window = ash::Shell::GetPrimaryRootWindow();
115 aura::client::GetDispatcherClient(root_window)->RunWithDispatcher(
116 &inner_dispatcher,
117 associated_window.get(),
118 true /* nestable_tasks_allowed */);
119 EXPECT_EQ(0, inner_dispatcher.num_key_events_dispatched());
120 Shell::GetInstance()->delegate()->UnlockScreen();
123 // Aura window above lock screen in z order.
124 TEST_F(NestedDispatcherTest, AssociatedWindowAboveLockScreen) {
125 MockDispatcher inner_dispatcher;
127 scoped_ptr<aura::Window>mock_lock_container(
128 CreateTestWindowInShellWithId(0));
129 aura::test::CreateTestWindowWithId(0, mock_lock_container.get());
130 scoped_ptr<aura::Window> associated_window(CreateTestWindowInShellWithId(0));
131 EXPECT_TRUE(aura::test::WindowIsAbove(associated_window.get(),
132 mock_lock_container.get()));
134 DispatchKeyReleaseA();
135 aura::RootWindow* root_window = ash::Shell::GetPrimaryRootWindow();
136 aura::client::GetDispatcherClient(root_window)->RunWithDispatcher(
137 &inner_dispatcher,
138 associated_window.get(),
139 true /* nestable_tasks_allowed */);
140 EXPECT_EQ(1, inner_dispatcher.num_key_events_dispatched());
143 // Test that the nested dispatcher handles accelerators.
144 TEST_F(NestedDispatcherTest, AcceleratorsHandled) {
145 MockDispatcher inner_dispatcher;
146 aura::RootWindow* root_window = ash::Shell::GetPrimaryRootWindow();
148 ui::Accelerator accelerator(ui::VKEY_A, ui::EF_NONE);
149 accelerator.set_type(ui::ET_KEY_RELEASED);
150 TestTarget target;
151 Shell::GetInstance()->accelerator_controller()->Register(accelerator,
152 &target);
154 DispatchKeyReleaseA();
155 aura::client::GetDispatcherClient(root_window)->RunWithDispatcher(
156 &inner_dispatcher,
157 root_window,
158 true /* nestable_tasks_allowed */);
159 EXPECT_EQ(0, inner_dispatcher.num_key_events_dispatched());
160 EXPECT_EQ(1, target.accelerator_pressed_count());
163 } // namespace test
164 } // namespace ash