accelerator_unittest.cc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // Copyright 2015 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 "ash/accelerators/accelerator_controller_impl.h"
  5. #include "ash/app_list/test/app_list_test_helper.h"
  6. #include "ash/shell.h"
  7. #include "ash/shell_observer.h"
  8. #include "ash/system/network/network_observer.h"
  9. #include "ash/system/tray/system_tray_notifier.h"
  10. #include "ash/test/ash_test_base.h"
  11. #include "ash/test/ui_controls_factory_ash.h"
  12. #include "ash/wm/overview/overview_controller.h"
  13. #include "ash/wm/overview/overview_observer.h"
  14. #include "ash/wm/window_state.h"
  15. #include "ash/wm/window_util.h"
  16. #include "base/run_loop.h"
  17. #include "base/test/metrics/user_action_tester.h"
  18. #include "chromeos/ash/components/dbus/shill/shill_clients.h"
  19. #include "chromeos/ash/components/network/network_handler.h"
  20. #include "ui/aura/window.h"
  21. #include "ui/base/accelerators/accelerator.h"
  22. #include "ui/base/accelerators/test_accelerator_target.h"
  23. #include "ui/base/test/ui_controls.h"
  24. #include "ui/events/event.h"
  25. #include "ui/events/test/event_generator.h"
  26. namespace ash {
  27. namespace {
  28. // A network observer to watch for the toggle wifi events.
  29. class TestNetworkObserver : public NetworkObserver {
  30. public:
  31. TestNetworkObserver() = default;
  32. TestNetworkObserver(const TestNetworkObserver&) = delete;
  33. TestNetworkObserver& operator=(const TestNetworkObserver&) = delete;
  34. ~TestNetworkObserver() override = default;
  35. // ash::NetworkObserver:
  36. void RequestToggleWifi() override {
  37. wifi_enabled_status_ = !wifi_enabled_status_;
  38. }
  39. bool wifi_enabled_status() const { return wifi_enabled_status_; }
  40. private:
  41. bool wifi_enabled_status_ = false;
  42. };
  43. } // namespace
  44. ////////////////////////////////////////////////////////////////////////////////
  45. // This is intended to test few samples from each category of accelerators to
  46. // make sure they work properly. The test is done as an interactive ui test
  47. // using ui_controls::Send*() functions.
  48. // This is to catch any future regressions (crbug.com/469235).
  49. class AcceleratorTest : public AshTestBase, public OverviewObserver {
  50. public:
  51. AcceleratorTest() : is_in_overview_mode_(false) {}
  52. AcceleratorTest(const AcceleratorTest&) = delete;
  53. AcceleratorTest& operator=(const AcceleratorTest&) = delete;
  54. void SetUp() override {
  55. ui_controls::InstallUIControlsAura(test::CreateAshUIControls());
  56. AshTestBase::SetUp();
  57. Shell::Get()->overview_controller()->AddObserver(this);
  58. }
  59. void TearDown() override {
  60. Shell::Get()->overview_controller()->RemoveObserver(this);
  61. AshTestBase::TearDown();
  62. ui_controls::InstallUIControlsAura(nullptr);
  63. }
  64. // Sends a key press event and waits synchronously until it's completely
  65. // processed.
  66. void SendKeyPressSync(ui::KeyboardCode key,
  67. bool control,
  68. bool shift,
  69. bool alt) {
  70. base::RunLoop loop;
  71. ui_controls::SendKeyPressNotifyWhenDone(Shell::GetPrimaryRootWindow(), key,
  72. control, shift, alt, false,
  73. loop.QuitClosure());
  74. loop.Run();
  75. }
  76. // OverviewObserver:
  77. void OnOverviewModeStarting() override { is_in_overview_mode_ = true; }
  78. void OnOverviewModeEnded() override { is_in_overview_mode_ = false; }
  79. protected:
  80. bool is_in_overview_mode_;
  81. };
  82. ////////////////////////////////////////////////////////////////////////////////
  83. // Tests a sample of accelerators.
  84. TEST_F(AcceleratorTest, Basic) {
  85. // Test VOLUME_MUTE.
  86. base::UserActionTester user_action_tester;
  87. EXPECT_EQ(0, user_action_tester.GetActionCount("Accel_VolumeMute_F8"));
  88. SendKeyPressSync(ui::VKEY_VOLUME_MUTE, false, false, false);
  89. EXPECT_EQ(1, user_action_tester.GetActionCount("Accel_VolumeMute_F8"));
  90. // Test VOLUME_DOWN.
  91. EXPECT_EQ(0, user_action_tester.GetActionCount("Accel_VolumeDown_F9"));
  92. SendKeyPressSync(ui::VKEY_VOLUME_DOWN, false, false, false);
  93. EXPECT_EQ(1, user_action_tester.GetActionCount("Accel_VolumeDown_F9"));
  94. // Test VOLUME_UP.
  95. EXPECT_EQ(0, user_action_tester.GetActionCount("Accel_VolumeUp_F10"));
  96. SendKeyPressSync(ui::VKEY_VOLUME_UP, false, false, false);
  97. EXPECT_EQ(1, user_action_tester.GetActionCount("Accel_VolumeUp_F10"));
  98. // Test TOGGLE_WIFI.
  99. TestNetworkObserver network_observer;
  100. Shell::Get()->system_tray_notifier()->AddNetworkObserver(&network_observer);
  101. EXPECT_FALSE(network_observer.wifi_enabled_status());
  102. SendKeyPressSync(ui::VKEY_WLAN, false, false, false);
  103. EXPECT_TRUE(network_observer.wifi_enabled_status());
  104. SendKeyPressSync(ui::VKEY_WLAN, false, false, false);
  105. EXPECT_FALSE(network_observer.wifi_enabled_status());
  106. Shell::Get()->system_tray_notifier()->RemoveNetworkObserver(
  107. &network_observer);
  108. }
  109. // Tests a sample of the non-repeatable accelerators that need windows to be
  110. // enabled.
  111. TEST_F(AcceleratorTest, NonRepeatableNeedingWindowActions) {
  112. // Create a bunch of windows to work with.
  113. aura::Window* window_1 =
  114. CreateTestWindowInShellWithBounds(gfx::Rect(0, 0, 100, 100));
  115. aura::Window* window_2 =
  116. CreateTestWindowInShellWithBounds(gfx::Rect(0, 0, 100, 100));
  117. window_1->Show();
  118. wm::ActivateWindow(window_1);
  119. window_2->Show();
  120. wm::ActivateWindow(window_2);
  121. // Test TOGGLE_OVERVIEW.
  122. EXPECT_FALSE(is_in_overview_mode_);
  123. SendKeyPressSync(ui::VKEY_MEDIA_LAUNCH_APP1, false, false, false);
  124. EXPECT_TRUE(is_in_overview_mode_);
  125. SendKeyPressSync(ui::VKEY_MEDIA_LAUNCH_APP1, false, false, false);
  126. EXPECT_FALSE(is_in_overview_mode_);
  127. // Test CYCLE_FORWARD_MRU and CYCLE_BACKWARD_MRU.
  128. wm::ActivateWindow(window_1);
  129. EXPECT_TRUE(wm::IsActiveWindow(window_1));
  130. EXPECT_FALSE(wm::IsActiveWindow(window_2));
  131. SendKeyPressSync(ui::VKEY_TAB, false, false, true); // CYCLE_FORWARD_MRU.
  132. EXPECT_TRUE(wm::IsActiveWindow(window_2));
  133. EXPECT_FALSE(wm::IsActiveWindow(window_1));
  134. SendKeyPressSync(ui::VKEY_TAB, false, true, true); // CYCLE_BACKWARD_MRU.
  135. EXPECT_TRUE(wm::IsActiveWindow(window_1));
  136. EXPECT_FALSE(wm::IsActiveWindow(window_2));
  137. // Test TOGGLE_FULLSCREEN.
  138. WindowState* active_window_state = WindowState::ForActiveWindow();
  139. EXPECT_FALSE(active_window_state->IsFullscreen());
  140. SendKeyPressSync(ui::VKEY_ZOOM, false, false, false);
  141. EXPECT_TRUE(active_window_state->IsFullscreen());
  142. SendKeyPressSync(ui::VKEY_ZOOM, false, false, false);
  143. EXPECT_FALSE(active_window_state->IsFullscreen());
  144. }
  145. // Tests the app list accelerator.
  146. TEST_F(AcceleratorTest, ToggleAppList) {
  147. GetAppListTestHelper()->CheckVisibility(false);
  148. SendKeyPressSync(ui::VKEY_LWIN, false, false, false);
  149. base::RunLoop().RunUntilIdle();
  150. GetAppListTestHelper()->CheckVisibility(true);
  151. SendKeyPressSync(ui::VKEY_LWIN, false, false, false);
  152. base::RunLoop().RunUntilIdle();
  153. GetAppListTestHelper()->CheckVisibility(false);
  154. }
  155. } // namespace ash