accelerator_filter_unittest.cc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. #include "ui/wm/core/accelerator_filter.h"
  5. #include <memory>
  6. #include "ash/accelerators/accelerator_controller_impl.h"
  7. #include "ash/accelerators/pre_target_accelerator_handler.h"
  8. #include "ash/app_list/test/app_list_test_helper.h"
  9. #include "ash/capture_mode/capture_mode_controller.h"
  10. #include "ash/session/session_controller_impl.h"
  11. #include "ash/shell.h"
  12. #include "ash/test/ash_test_base.h"
  13. #include "ash/wm/window_state.h"
  14. #include "ash/wm/window_util.h"
  15. #include "testing/gtest/include/gtest/gtest.h"
  16. #include "ui/aura/client/aura_constants.h"
  17. #include "ui/aura/test/test_windows.h"
  18. #include "ui/aura/window.h"
  19. #include "ui/events/event.h"
  20. #include "ui/events/test/event_generator.h"
  21. #include "ui/gfx/geometry/rect.h"
  22. namespace ash {
  23. using AcceleratorFilterTest = AshTestBase;
  24. // Tests if AcceleratorFilter works without a focused window.
  25. TEST_F(AcceleratorFilterTest, TestFilterWithoutFocus) {
  26. // VKEY_SNAPSHOT opens capture mode.
  27. PressAndReleaseKey(ui::VKEY_SNAPSHOT);
  28. EXPECT_TRUE(CaptureModeController::Get()->IsActive());
  29. }
  30. // Tests if AcceleratorFilter works as expected with a focused window.
  31. TEST_F(AcceleratorFilterTest, TestFilterWithFocus) {
  32. aura::test::TestWindowDelegate test_delegate;
  33. std::unique_ptr<aura::Window> window(
  34. CreateTestWindowInShellWithDelegate(&test_delegate, -1, gfx::Rect()));
  35. wm::ActivateWindow(window.get());
  36. // AcceleratorFilter should ignore the key events since the root window is
  37. // not focused.
  38. PressAndReleaseKey(ui::VKEY_SNAPSHOT);
  39. EXPECT_FALSE(CaptureModeController::Get()->IsActive());
  40. // Reset window before |test_delegate| gets deleted.
  41. window.reset();
  42. }
  43. // Tests if AcceleratorFilter ignores the flag for Caps Lock.
  44. TEST_F(AcceleratorFilterTest, TestCapsLockMask) {
  45. PressAndReleaseKey(ui::VKEY_SNAPSHOT);
  46. auto* controller = CaptureModeController::Get();
  47. EXPECT_TRUE(controller->IsActive());
  48. controller->Stop();
  49. // Check if AcceleratorFilter ignores the mask for Caps Lock. Note that there
  50. // is no ui::EF_ mask for Num Lock.
  51. PressAndReleaseKey(ui::VKEY_SNAPSHOT, ui::EF_CAPS_LOCK_ON);
  52. EXPECT_TRUE(controller->IsActive());
  53. }
  54. // Tests if special hardware keys like brightness and volume are consumed as
  55. // expected by the shell.
  56. TEST_F(AcceleratorFilterTest, CanConsumeSystemKeys) {
  57. ::wm::AcceleratorFilter filter(
  58. std::make_unique<PreTargetAcceleratorHandler>());
  59. aura::Window* root_window = Shell::GetPrimaryRootWindow();
  60. // Normal keys are not consumed.
  61. ui::KeyEvent press_a(ui::ET_KEY_PRESSED, ui::VKEY_A, ui::EF_NONE);
  62. {
  63. ui::Event::DispatcherApi dispatch_helper(&press_a);
  64. dispatch_helper.set_target(root_window);
  65. }
  66. filter.OnKeyEvent(&press_a);
  67. EXPECT_FALSE(press_a.stopped_propagation());
  68. // System keys are directly consumed.
  69. ui::KeyEvent press_mute(ui::ET_KEY_PRESSED, ui::VKEY_VOLUME_MUTE,
  70. ui::EF_NONE);
  71. {
  72. ui::Event::DispatcherApi dispatch_helper(&press_mute);
  73. dispatch_helper.set_target(root_window);
  74. }
  75. filter.OnKeyEvent(&press_mute);
  76. EXPECT_TRUE(press_mute.stopped_propagation());
  77. // Setting a window property on the target allows system keys to pass through.
  78. std::unique_ptr<aura::Window> window(CreateTestWindowInShellWithId(1));
  79. WindowState::Get(window.get())->SetCanConsumeSystemKeys(true);
  80. ui::KeyEvent press_volume_up(ui::ET_KEY_PRESSED, ui::VKEY_VOLUME_UP,
  81. ui::EF_NONE);
  82. ui::Event::DispatcherApi dispatch_helper(&press_volume_up);
  83. dispatch_helper.set_target(window.get());
  84. filter.OnKeyEvent(&press_volume_up);
  85. EXPECT_FALSE(press_volume_up.stopped_propagation());
  86. // System keys pass through to a child window if the parent (top level)
  87. // window has the property set.
  88. std::unique_ptr<aura::Window> child(CreateTestWindowInShellWithId(2));
  89. window->AddChild(child.get());
  90. dispatch_helper.set_target(child.get());
  91. filter.OnKeyEvent(&press_volume_up);
  92. EXPECT_FALSE(press_volume_up.stopped_propagation());
  93. }
  94. TEST_F(AcceleratorFilterTest, SearchKeyShortcutsAreAlwaysHandled) {
  95. SessionControllerImpl* const session_controller =
  96. Shell::Get()->session_controller();
  97. EXPECT_FALSE(session_controller->IsScreenLocked());
  98. // We can lock the screen (Search+L) if a window is not present.
  99. PressAndReleaseKey(ui::VKEY_L, ui::EF_COMMAND_DOWN);
  100. GetSessionControllerClient()->FlushForTest(); // LockScreen is an async call.
  101. EXPECT_TRUE(session_controller->IsScreenLocked());
  102. UnblockUserSession();
  103. EXPECT_FALSE(session_controller->IsScreenLocked());
  104. // Search+L is processed when the app_list target visibility is false.
  105. GetAppListTestHelper()->DismissAndRunLoop();
  106. GetAppListTestHelper()->CheckVisibility(false);
  107. PressAndReleaseKey(ui::VKEY_L, ui::EF_COMMAND_DOWN);
  108. GetSessionControllerClient()->FlushForTest(); // LockScreen is an async call.
  109. EXPECT_TRUE(session_controller->IsScreenLocked());
  110. UnblockUserSession();
  111. EXPECT_FALSE(session_controller->IsScreenLocked());
  112. // Search+L is also processed when there is a full screen window.
  113. aura::test::TestWindowDelegate window_delegate;
  114. std::unique_ptr<aura::Window> window(CreateTestWindowInShellWithDelegate(
  115. &window_delegate, 0, gfx::Rect(200, 200)));
  116. window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_FULLSCREEN);
  117. PressAndReleaseKey(ui::VKEY_L, ui::EF_COMMAND_DOWN);
  118. GetSessionControllerClient()->FlushForTest(); // LockScreen is an async call.
  119. EXPECT_TRUE(session_controller->IsScreenLocked());
  120. UnblockUserSession();
  121. EXPECT_FALSE(session_controller->IsScreenLocked());
  122. }
  123. TEST_F(AcceleratorFilterTest, ToggleAppListInterruptedByMouseEvent) {
  124. ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
  125. GetAppListTestHelper()->CheckVisibility(false);
  126. // The AppList should toggle if no mouse event occurs between key press and
  127. // key release.
  128. generator.PressKey(ui::VKEY_LWIN, ui::EF_NONE);
  129. generator.ReleaseKey(ui::VKEY_LWIN, ui::EF_NONE);
  130. GetAppListTestHelper()->WaitUntilIdle();
  131. GetAppListTestHelper()->CheckVisibility(true);
  132. // Close the app list.
  133. GetAppListTestHelper()->DismissAndRunLoop();
  134. GetAppListTestHelper()->CheckVisibility(false);
  135. // When pressed key is interrupted by mouse, the AppList should not toggle.
  136. generator.PressKey(ui::VKEY_LWIN, ui::EF_NONE);
  137. generator.ClickLeftButton();
  138. generator.ReleaseKey(ui::VKEY_LWIN, ui::EF_NONE);
  139. GetAppListTestHelper()->WaitUntilIdle();
  140. GetAppListTestHelper()->CheckVisibility(false);
  141. }
  142. } // namespace ash