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