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