accelerator_filter_unittest.cc revision 4e180b6a0b4720a9b8e9e959a882386f690f08ff
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.
4
5#include "ash/accelerators/accelerator_filter.h"
6
7#include "ash/accelerators/accelerator_controller.h"
8#include "ash/shell.h"
9#include "ash/shell_window_ids.h"
10#include "ash/test/ash_test_base.h"
11#include "ash/test/test_screenshot_delegate.h"
12#include "ash/wm/window_state.h"
13#include "ash/wm/window_util.h"
14#include "base/memory/scoped_ptr.h"
15#include "testing/gtest/include/gtest/gtest.h"
16#include "ui/aura/test/aura_test_base.h"
17#include "ui/aura/test/event_generator.h"
18#include "ui/aura/test/test_windows.h"
19#include "ui/aura/window.h"
20#include "ui/events/event.h"
21#include "ui/gfx/rect.h"
22
23namespace ash {
24namespace test {
25
26namespace {
27
28AcceleratorController* GetController() {
29  return Shell::GetInstance()->accelerator_controller();
30}
31
32}  // namespace
33
34typedef AshTestBase AcceleratorFilterTest;
35
36// Tests if AcceleratorFilter works without a focused window.
37TEST_F(AcceleratorFilterTest, TestFilterWithoutFocus) {
38  const TestScreenshotDelegate* delegate = GetScreenshotDelegate();
39  EXPECT_EQ(0, delegate->handle_take_screenshot_count());
40
41  aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
42  // AcceleratorController calls ScreenshotDelegate::HandleTakeScreenshot() when
43  // VKEY_PRINT is pressed. See kAcceleratorData[] in accelerator_controller.cc.
44  generator.PressKey(ui::VKEY_PRINT, 0);
45  EXPECT_EQ(1, delegate->handle_take_screenshot_count());
46  generator.ReleaseKey(ui::VKEY_PRINT, 0);
47  EXPECT_EQ(1, delegate->handle_take_screenshot_count());
48}
49
50// Tests if AcceleratorFilter works as expected with a focused window.
51TEST_F(AcceleratorFilterTest, TestFilterWithFocus) {
52  aura::test::TestWindowDelegate test_delegate;
53  scoped_ptr<aura::Window> window(CreateTestWindowInShellWithDelegate(
54      &test_delegate,
55      -1,
56      gfx::Rect()));
57  wm::ActivateWindow(window.get());
58
59  const TestScreenshotDelegate* delegate = GetScreenshotDelegate();
60  EXPECT_EQ(0, delegate->handle_take_screenshot_count());
61
62  // AcceleratorFilter should ignore the key events since the root window is
63  // not focused.
64  aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
65  generator.PressKey(ui::VKEY_PRINT, 0);
66  EXPECT_EQ(0, delegate->handle_take_screenshot_count());
67  generator.ReleaseKey(ui::VKEY_PRINT, 0);
68  EXPECT_EQ(0, delegate->handle_take_screenshot_count());
69
70  // Reset window before |test_delegate| gets deleted.
71  window.reset();
72}
73
74// Tests if AcceleratorFilter ignores the flag for Caps Lock.
75TEST_F(AcceleratorFilterTest, TestCapsLockMask) {
76  const TestScreenshotDelegate* delegate = GetScreenshotDelegate();
77  EXPECT_EQ(0, delegate->handle_take_screenshot_count());
78
79  aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
80  generator.PressKey(ui::VKEY_PRINT, 0);
81  EXPECT_EQ(1, delegate->handle_take_screenshot_count());
82  generator.ReleaseKey(ui::VKEY_PRINT, 0);
83  EXPECT_EQ(1, delegate->handle_take_screenshot_count());
84
85  // Check if AcceleratorFilter ignores the mask for Caps Lock. Note that there
86  // is no ui::EF_ mask for Num Lock.
87  generator.PressKey(ui::VKEY_PRINT, ui::EF_CAPS_LOCK_DOWN);
88  EXPECT_EQ(2, delegate->handle_take_screenshot_count());
89  generator.ReleaseKey(ui::VKEY_PRINT, ui::EF_CAPS_LOCK_DOWN);
90  EXPECT_EQ(2, delegate->handle_take_screenshot_count());
91}
92
93#if defined(OS_CHROMEOS)
94// Tests if special hardware keys like brightness and volume are consumed as
95// expected by the shell.
96TEST_F(AcceleratorFilterTest, CanConsumeSystemKeys) {
97  internal::AcceleratorFilter filter;
98
99  // Normal keys are not consumed.
100  ui::KeyEvent press_a(ui::ET_KEY_PRESSED, ui::VKEY_A, ui::EF_NONE, false);
101  filter.OnKeyEvent(&press_a);
102  EXPECT_FALSE(press_a.stopped_propagation());
103
104  // System keys are directly consumed.
105  ui::KeyEvent press_mute(
106      ui::ET_KEY_PRESSED, ui::VKEY_VOLUME_MUTE, ui::EF_NONE, false);
107  filter.OnKeyEvent(&press_mute);
108  EXPECT_TRUE(press_mute.stopped_propagation());
109
110  // Setting a window property on the target allows system keys to pass through.
111  scoped_ptr<aura::Window> window(CreateTestWindowInShellWithId(1));
112  wm::GetWindowState(window.get())->set_can_consume_system_keys(true);
113  ui::KeyEvent press_volume_up(
114      ui::ET_KEY_PRESSED, ui::VKEY_VOLUME_UP, ui::EF_NONE, false);
115  ui::Event::DispatcherApi dispatch_helper(&press_volume_up);
116  dispatch_helper.set_target(window.get());
117  filter.OnKeyEvent(&press_volume_up);
118  EXPECT_FALSE(press_volume_up.stopped_propagation());
119
120  // System keys pass through to a child window if the parent (top level)
121  // window has the property set.
122  scoped_ptr<aura::Window> child(CreateTestWindowInShellWithId(2));
123  window->AddChild(child.get());
124  dispatch_helper.set_target(child.get());
125  filter.OnKeyEvent(&press_volume_up);
126  EXPECT_FALSE(press_volume_up.stopped_propagation());
127}
128#endif  // defined(OS_CHROMEOS)
129
130}  // namespace test
131}  // namespace ash
132