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