OLD | NEW |
(Empty) | |
| 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 |
| 5 #include "ash/accelerators/accelerator_controller.h" |
| 6 |
| 7 #include "ash/shell.h" |
| 8 #include "ash/shell_observer.h" |
| 9 #include "ash/system/tray/system_tray_delegate.h" |
| 10 #include "ash/system/tray/system_tray_notifier.h" |
| 11 #include "ash/test/ash_test_base.h" |
| 12 #include "ash/test/test_screenshot_delegate.h" |
| 13 #include "ash/test/test_volume_control_delegate.h" |
| 14 #include "ash/wm/window_state.h" |
| 15 #include "ash/wm/window_util.h" |
| 16 #include "base/path_service.h" |
| 17 #include "base/run_loop.h" |
| 18 #include "ui/base/resource/resource_bundle.h" |
| 19 #include "ui/base/test/ui_controls.h" |
| 20 #include "ui/base/ui_base_paths.h" |
| 21 #include "ui/gl/gl_surface.h" |
| 22 |
| 23 #if defined(OS_CHROMEOS) |
| 24 #include "ash/system/chromeos/network/network_observer.h" |
| 25 #include "chromeos/network/network_handler.h" |
| 26 #endif // defined(OS_CHROMEOS) |
| 27 |
| 28 namespace ash { |
| 29 namespace test { |
| 30 |
| 31 namespace { |
| 32 |
| 33 #if defined(OS_CHROMEOS) |
| 34 // A network observer to watch for the toggle wifi events. |
| 35 class TestNetworkObserver : public NetworkObserver { |
| 36 public: |
| 37 TestNetworkObserver() : wifi_enabled_status_(false) {} |
| 38 |
| 39 // ash::NetworkObserver: |
| 40 void RequestToggleWifi() override { |
| 41 wifi_enabled_status_ = !wifi_enabled_status_; |
| 42 } |
| 43 |
| 44 bool wifi_enabled_status() const { return wifi_enabled_status_; } |
| 45 |
| 46 private: |
| 47 bool wifi_enabled_status_; |
| 48 |
| 49 DISALLOW_COPY_AND_ASSIGN(TestNetworkObserver); |
| 50 }; |
| 51 #endif // defined(OS_CHROMEOS) |
| 52 |
| 53 } // namespace |
| 54 |
| 55 //////////////////////////////////////////////////////////////////////////////// |
| 56 |
| 57 // This is intended to test few samples from each category of accelerators to |
| 58 // make sure they work properly. The test is done as an interactive ui test |
| 59 // using ui_controls::Send*() functions. |
| 60 // This is to catch any future regressions (crbug.com/469235). |
| 61 class AcceleratorInteractiveUITest : public AshTestBase, public ShellObserver { |
| 62 public: |
| 63 AcceleratorInteractiveUITest() : is_in_overview_mode_(false) {} |
| 64 |
| 65 void SetUp() override { |
| 66 gfx::GLSurface::InitializeOneOffForTests(); |
| 67 |
| 68 ui::RegisterPathProvider(); |
| 69 ui::ResourceBundle::InitSharedInstanceWithLocale( |
| 70 "en-US", NULL, ui::ResourceBundle::LOAD_COMMON_RESOURCES); |
| 71 base::FilePath resources_pack_path; |
| 72 PathService::Get(base::DIR_MODULE, &resources_pack_path); |
| 73 resources_pack_path = |
| 74 resources_pack_path.Append(FILE_PATH_LITERAL("resources.pak")); |
| 75 ResourceBundle::GetSharedInstance().AddDataPackFromPath( |
| 76 resources_pack_path, ui::SCALE_FACTOR_NONE); |
| 77 |
| 78 AshTestBase::SetUp(); |
| 79 |
| 80 Shell::GetInstance()->AddShellObserver(this); |
| 81 |
| 82 #if defined(OS_CHROMEOS) |
| 83 chromeos::NetworkHandler::Initialize(); |
| 84 #endif // defined(OS_CHROMEOS) |
| 85 } |
| 86 |
| 87 void TearDown() override { |
| 88 Shell::GetInstance()->RemoveShellObserver(this); |
| 89 |
| 90 #if defined(OS_CHROMEOS) |
| 91 chromeos::NetworkHandler::Shutdown(); |
| 92 #endif // defined(OS_CHROMEOS) |
| 93 |
| 94 AshTestBase::TearDown(); |
| 95 } |
| 96 |
| 97 // Sends a key press event and waits synchronously until it's completely |
| 98 // processed. |
| 99 void SendKeyPressSync(ui::KeyboardCode key, |
| 100 bool control, |
| 101 bool shift, |
| 102 bool alt) { |
| 103 base::RunLoop loop; |
| 104 ui_controls::SendKeyPressNotifyWhenDone(root_window(), key, control, shift, |
| 105 alt, false, loop.QuitClosure()); |
| 106 loop.Run(); |
| 107 } |
| 108 |
| 109 // ash::ShellObserver: |
| 110 void OnOverviewModeStarting() override { is_in_overview_mode_ = true; } |
| 111 void OnOverviewModeEnded() override { is_in_overview_mode_ = false; } |
| 112 |
| 113 Shell* shell() const { return Shell::GetInstance(); } |
| 114 aura::Window* root_window() const { return Shell::GetPrimaryRootWindow(); } |
| 115 |
| 116 protected: |
| 117 bool is_in_overview_mode_; |
| 118 |
| 119 private: |
| 120 DISALLOW_COPY_AND_ASSIGN(AcceleratorInteractiveUITest); |
| 121 }; |
| 122 |
| 123 //////////////////////////////////////////////////////////////////////////////// |
| 124 |
| 125 #if !defined(OS_CHROMEOS) |
| 126 #define MAYBE_TestNonRepeatableNeedingWindowActions \ |
| 127 DISABLED_TestNonRepeatableNeedingWindowActions |
| 128 #define MAYBE_ToggleAppList DISABLED_ToggleAppList |
| 129 #else |
| 130 #define MAYBE_TestNonRepeatableNeedingWindowActions \ |
| 131 TestNonRepeatableNeedingWindowActions |
| 132 #define MAYBE_ToggleAppList ToggleAppList |
| 133 #endif // !defined(OS_CHROMEOS) |
| 134 |
| 135 // Tests a sample of the non-repeatable accelerators that need windows to be |
| 136 // enabled. |
| 137 TEST_F(AcceleratorInteractiveUITest, |
| 138 MAYBE_TestNonRepeatableNeedingWindowActions) { |
| 139 // Create a bunch of windows to work with. |
| 140 aura::Window* window_1 = |
| 141 CreateTestWindowInShellWithBounds(gfx::Rect(0, 0, 100, 100)); |
| 142 aura::Window* window_2 = |
| 143 CreateTestWindowInShellWithBounds(gfx::Rect(0, 0, 100, 100)); |
| 144 window_1->Show(); |
| 145 wm::ActivateWindow(window_1); |
| 146 window_2->Show(); |
| 147 wm::ActivateWindow(window_2); |
| 148 |
| 149 // Test TOGGLE_OVERVIEW. |
| 150 EXPECT_FALSE(is_in_overview_mode_); |
| 151 SendKeyPressSync(ui::VKEY_MEDIA_LAUNCH_APP1, false, false, false); |
| 152 EXPECT_TRUE(is_in_overview_mode_); |
| 153 SendKeyPressSync(ui::VKEY_MEDIA_LAUNCH_APP1, false, false, false); |
| 154 EXPECT_FALSE(is_in_overview_mode_); |
| 155 |
| 156 // Test CYCLE_FORWARD_MRU and CYCLE_BACKWARD_MRU. |
| 157 wm::ActivateWindow(window_1); |
| 158 EXPECT_TRUE(wm::IsActiveWindow(window_1)); |
| 159 EXPECT_FALSE(wm::IsActiveWindow(window_2)); |
| 160 SendKeyPressSync(ui::VKEY_TAB, false, false, true); // CYCLE_FORWARD_MRU. |
| 161 EXPECT_TRUE(wm::IsActiveWindow(window_2)); |
| 162 EXPECT_FALSE(wm::IsActiveWindow(window_1)); |
| 163 SendKeyPressSync(ui::VKEY_TAB, false, true, true); // CYCLE_BACKWARD_MRU. |
| 164 EXPECT_TRUE(wm::IsActiveWindow(window_1)); |
| 165 EXPECT_FALSE(wm::IsActiveWindow(window_2)); |
| 166 |
| 167 // Test TOGGLE_FULLSCREEN. |
| 168 wm::WindowState* active_window_state = wm::GetActiveWindowState(); |
| 169 EXPECT_FALSE(active_window_state->IsFullscreen()); |
| 170 SendKeyPressSync(ui::VKEY_MEDIA_LAUNCH_APP2, false, false, false); |
| 171 EXPECT_TRUE(active_window_state->IsFullscreen()); |
| 172 SendKeyPressSync(ui::VKEY_MEDIA_LAUNCH_APP2, false, false, false); |
| 173 EXPECT_FALSE(active_window_state->IsFullscreen()); |
| 174 } |
| 175 |
| 176 #if defined(OS_CHROMEOS) |
| 177 // Tests a sample of ChromeOS specific accelerators. |
| 178 TEST_F(AcceleratorInteractiveUITest, ChromeOsAccelerators) { |
| 179 // Test TAKE_SCREENSHOT and TAKE_PARTIAL_SCREENSHOT. |
| 180 TestScreenshotDelegate* screenshot_delegate = GetScreenshotDelegate(); |
| 181 screenshot_delegate->set_can_take_screenshot(true); |
| 182 EXPECT_EQ(0, screenshot_delegate->handle_take_screenshot_count()); |
| 183 SendKeyPressSync(ui::VKEY_MEDIA_LAUNCH_APP1, true, false, false); |
| 184 EXPECT_EQ(1, screenshot_delegate->handle_take_screenshot_count()); |
| 185 SendKeyPressSync(ui::VKEY_PRINT, false, false, false); |
| 186 EXPECT_EQ(2, screenshot_delegate->handle_take_screenshot_count()); |
| 187 SendKeyPressSync(ui::VKEY_MEDIA_LAUNCH_APP1, true, true, false); |
| 188 EXPECT_EQ(2, screenshot_delegate->handle_take_screenshot_count()); |
| 189 // Press ESC to go out of the partial screenshot mode. |
| 190 SendKeyPressSync(ui::VKEY_ESCAPE, false, false, false); |
| 191 |
| 192 // Test VOLUME_MUTE, VOLUME_DOWN, and VOLUME_UP. |
| 193 TestVolumeControlDelegate* volume_delegate = new TestVolumeControlDelegate; |
| 194 shell()->system_tray_delegate()->SetVolumeControlDelegate( |
| 195 scoped_ptr<VolumeControlDelegate>(volume_delegate).Pass()); |
| 196 // VOLUME_MUTE. |
| 197 EXPECT_EQ(0, volume_delegate->handle_volume_mute_count()); |
| 198 SendKeyPressSync(ui::VKEY_VOLUME_MUTE, false, false, false); |
| 199 EXPECT_EQ(1, volume_delegate->handle_volume_mute_count()); |
| 200 // VOLUME_DOWN. |
| 201 EXPECT_EQ(0, volume_delegate->handle_volume_down_count()); |
| 202 SendKeyPressSync(ui::VKEY_VOLUME_DOWN, false, false, false); |
| 203 EXPECT_EQ(1, volume_delegate->handle_volume_down_count()); |
| 204 // VOLUME_UP. |
| 205 EXPECT_EQ(0, volume_delegate->handle_volume_up_count()); |
| 206 SendKeyPressSync(ui::VKEY_VOLUME_UP, false, false, false); |
| 207 EXPECT_EQ(1, volume_delegate->handle_volume_up_count()); |
| 208 |
| 209 // Test TOGGLE_WIFI. |
| 210 TestNetworkObserver network_observer; |
| 211 shell()->system_tray_notifier()->AddNetworkObserver(&network_observer); |
| 212 |
| 213 EXPECT_FALSE(network_observer.wifi_enabled_status()); |
| 214 SendKeyPressSync(ui::VKEY_WLAN, false, false, false); |
| 215 EXPECT_TRUE(network_observer.wifi_enabled_status()); |
| 216 SendKeyPressSync(ui::VKEY_WLAN, false, false, false); |
| 217 EXPECT_FALSE(network_observer.wifi_enabled_status()); |
| 218 |
| 219 shell()->system_tray_notifier()->RemoveNetworkObserver(&network_observer); |
| 220 } |
| 221 #endif // defined(OS_CHROMEOS) |
| 222 |
| 223 // Tests the app list accelerator. |
| 224 TEST_F(AcceleratorInteractiveUITest, MAYBE_ToggleAppList) { |
| 225 EXPECT_FALSE(shell()->GetAppListTargetVisibility()); |
| 226 SendKeyPressSync(ui::VKEY_LWIN, false, false, false); |
| 227 EXPECT_TRUE(shell()->GetAppListTargetVisibility()); |
| 228 SendKeyPressSync(ui::VKEY_LWIN, false, false, false); |
| 229 EXPECT_FALSE(shell()->GetAppListTargetVisibility()); |
| 230 } |
| 231 |
| 232 } // namespace test |
| 233 } // namespace ash |
OLD | NEW |