[content shell] implement testRunner.overridePreference
[chromium-blink-merge.git] / ash / wm / window_cycle_controller_unittest.cc
blobc3b1ce61a02e87b538f2edc4a80907b3afdce4cf
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.
5 #include "ash/wm/window_cycle_controller.h"
7 #include <algorithm>
9 #include "ash/display/display_controller.h"
10 #include "ash/display/display_manager.h"
11 #include "ash/shell.h"
12 #include "ash/shell_window_ids.h"
13 #include "ash/test/ash_test_base.h"
14 #include "ash/test/test_shell_delegate.h"
15 #include "ash/wm/window_cycle_list.h"
16 #include "ash/wm/window_util.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "ui/aura/client/aura_constants.h"
19 #include "ui/aura/client/screen_position_client.h"
20 #include "ui/aura/env.h"
21 #include "ui/aura/test/test_windows.h"
22 #include "ui/aura/window.h"
23 #include "ui/gfx/rect.h"
24 #include "ui/gfx/screen.h"
26 namespace ash {
28 namespace {
30 using aura::test::CreateTestWindowWithId;
31 using aura::test::TestWindowDelegate;
32 using aura::Window;
34 typedef test::AshTestBase WindowCycleControllerTest;
36 TEST_F(WindowCycleControllerTest, HandleCycleWindowBaseCases) {
37 WindowCycleController* controller =
38 Shell::GetInstance()->window_cycle_controller();
40 // Cycling doesn't crash if there are no windows.
41 controller->HandleCycleWindow(WindowCycleController::FORWARD, false);
43 // Create a single test window.
44 scoped_ptr<Window> window0(CreateTestWindowInShellWithId(0));
45 wm::ActivateWindow(window0.get());
46 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
48 // Cycling works for a single window, even though nothing changes.
49 controller->HandleCycleWindow(WindowCycleController::FORWARD, false);
50 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
53 // Verifies if there is only one window and it isn't active that cycling
54 // activates it.
55 TEST_F(WindowCycleControllerTest, SingleWindowNotActive) {
56 WindowCycleController* controller =
57 Shell::GetInstance()->window_cycle_controller();
59 // Create a single test window.
60 scoped_ptr<Window> window0(CreateTestWindowInShellWithId(0));
61 wm::ActivateWindow(window0.get());
62 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
64 // Rotate focus, this should move focus to another window that isn't part of
65 // the default container.
66 Shell::GetInstance()->RotateFocus(Shell::FORWARD);
67 EXPECT_FALSE(wm::IsActiveWindow(window0.get()));
69 // Cycling should activate the window.
70 controller->HandleCycleWindow(WindowCycleController::FORWARD, false);
71 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
74 TEST_F(WindowCycleControllerTest, HandleCycleWindow) {
75 WindowCycleController* controller =
76 Shell::GetInstance()->window_cycle_controller();
78 // Set up several windows to use to test cycling. Create them in reverse
79 // order so they are stacked 0 over 1 over 2.
80 scoped_ptr<Window> window2(CreateTestWindowInShellWithId(2));
81 scoped_ptr<Window> window1(CreateTestWindowInShellWithId(1));
82 scoped_ptr<Window> window0(CreateTestWindowInShellWithId(0));
83 wm::ActivateWindow(window0.get());
85 // Simulate pressing and releasing Alt-tab.
86 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
87 controller->HandleCycleWindow(WindowCycleController::FORWARD, true);
89 // Window lists should return the topmost window in front.
90 ASSERT_TRUE(controller->windows());
91 ASSERT_EQ(3u, controller->windows()->windows().size());
92 ASSERT_EQ(window0.get(), controller->windows()->windows()[0]);
93 ASSERT_EQ(window1.get(), controller->windows()->windows()[1]);
94 ASSERT_EQ(window2.get(), controller->windows()->windows()[2]);
96 controller->AltKeyReleased();
97 EXPECT_TRUE(wm::IsActiveWindow(window1.get()));
99 // Pressing and releasing Alt-tab again should cycle back to the most-
100 // recently-used window in the current child order.
101 controller->HandleCycleWindow(WindowCycleController::FORWARD, true);
102 controller->AltKeyReleased();
103 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
105 // Pressing Alt-tab multiple times without releasing Alt should cycle through
106 // all the windows and wrap around.
107 controller->HandleCycleWindow(WindowCycleController::FORWARD, true);
108 EXPECT_TRUE(controller->IsCycling());
109 EXPECT_TRUE(wm::IsActiveWindow(window1.get()));
111 controller->HandleCycleWindow(WindowCycleController::FORWARD, true);
112 EXPECT_TRUE(controller->IsCycling());
113 EXPECT_TRUE(wm::IsActiveWindow(window2.get()));
115 controller->HandleCycleWindow(WindowCycleController::FORWARD, true);
116 EXPECT_TRUE(controller->IsCycling());
117 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
119 controller->AltKeyReleased();
120 EXPECT_FALSE(controller->IsCycling());
121 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
123 // Reset our stacking order.
124 wm::ActivateWindow(window2.get());
125 wm::ActivateWindow(window1.get());
126 wm::ActivateWindow(window0.get());
128 // Likewise we can cycle backwards through all the windows.
129 controller->HandleCycleWindow(WindowCycleController::BACKWARD, true);
130 EXPECT_TRUE(wm::IsActiveWindow(window2.get()));
131 controller->HandleCycleWindow(WindowCycleController::BACKWARD, true);
132 EXPECT_TRUE(wm::IsActiveWindow(window1.get()));
133 controller->HandleCycleWindow(WindowCycleController::BACKWARD, true);
134 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
135 controller->AltKeyReleased();
136 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
138 // Passing false for is_alt_down does not start a cycle gesture.
139 controller->HandleCycleWindow(WindowCycleController::FORWARD, false);
140 EXPECT_FALSE(controller->IsCycling());
141 EXPECT_TRUE(wm::IsActiveWindow(window1.get()));
143 controller->HandleCycleWindow(WindowCycleController::FORWARD, false);
144 EXPECT_FALSE(controller->IsCycling());
145 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
147 // When the screen is locked, cycling window does not take effect.
148 Shell::GetInstance()->delegate()->LockScreen();
149 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
150 controller->HandleCycleWindow(WindowCycleController::FORWARD, false);
151 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
152 controller->HandleCycleWindow(WindowCycleController::BACKWARD, false);
153 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
155 Shell::GetInstance()->delegate()->UnlockScreen();
156 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
157 controller->HandleCycleWindow(WindowCycleController::FORWARD, false);
158 EXPECT_TRUE(wm::IsActiveWindow(window1.get()));
159 controller->HandleCycleWindow(WindowCycleController::FORWARD, false);
160 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
162 // When a modal window is active, cycling window does not take effect.
163 aura::Window* modal_container =
164 ash::Shell::GetContainer(
165 Shell::GetPrimaryRootWindow(),
166 internal::kShellWindowId_SystemModalContainer);
167 scoped_ptr<Window> modal_window(
168 CreateTestWindowWithId(-2, modal_container));
169 modal_window->SetProperty(aura::client::kModalKey, ui::MODAL_TYPE_SYSTEM);
170 wm::ActivateWindow(modal_window.get());
171 EXPECT_TRUE(wm::IsActiveWindow(modal_window.get()));
172 controller->HandleCycleWindow(WindowCycleController::FORWARD, false);
173 EXPECT_TRUE(wm::IsActiveWindow(modal_window.get()));
174 EXPECT_FALSE(wm::IsActiveWindow(window0.get()));
175 EXPECT_FALSE(wm::IsActiveWindow(window1.get()));
176 EXPECT_FALSE(wm::IsActiveWindow(window2.get()));
177 controller->HandleCycleWindow(WindowCycleController::BACKWARD, false);
178 EXPECT_TRUE(wm::IsActiveWindow(modal_window.get()));
179 EXPECT_FALSE(wm::IsActiveWindow(window0.get()));
180 EXPECT_FALSE(wm::IsActiveWindow(window1.get()));
181 EXPECT_FALSE(wm::IsActiveWindow(window2.get()));
184 // Cycles between a maximized and normal window.
185 TEST_F(WindowCycleControllerTest, MaximizedWindow) {
186 // Create a couple of test windows.
187 scoped_ptr<Window> window0(CreateTestWindowInShellWithId(0));
188 scoped_ptr<Window> window1(CreateTestWindowInShellWithId(1));
190 wm::MaximizeWindow(window1.get());
191 wm::ActivateWindow(window1.get());
192 EXPECT_TRUE(wm::IsActiveWindow(window1.get()));
194 // Rotate focus, this should move focus to window0.
195 WindowCycleController* controller =
196 Shell::GetInstance()->window_cycle_controller();
197 controller->HandleCycleWindow(WindowCycleController::FORWARD, false);
198 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
200 // One more time.
201 controller->HandleCycleWindow(WindowCycleController::FORWARD, false);
202 EXPECT_TRUE(wm::IsActiveWindow(window1.get()));
205 // Cycles to a minimized window.
206 TEST_F(WindowCycleControllerTest, Minimized) {
207 // Create a couple of test windows.
208 scoped_ptr<Window> window0(CreateTestWindowInShellWithId(0));
209 scoped_ptr<Window> window1(CreateTestWindowInShellWithId(1));
211 wm::MinimizeWindow(window1.get());
212 wm::ActivateWindow(window0.get());
213 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
215 // Rotate focus, this should move focus to window1 and unminimize it.
216 WindowCycleController* controller =
217 Shell::GetInstance()->window_cycle_controller();
218 controller->HandleCycleWindow(WindowCycleController::FORWARD, false);
219 EXPECT_FALSE(wm::IsWindowMinimized(window1.get()));
220 EXPECT_TRUE(wm::IsActiveWindow(window1.get()));
222 // One more time back to w0.
223 controller->HandleCycleWindow(WindowCycleController::FORWARD, false);
224 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
227 TEST_F(WindowCycleControllerTest, AlwaysOnTopWindow) {
228 WindowCycleController* controller =
229 Shell::GetInstance()->window_cycle_controller();
231 // Set up several windows to use to test cycling.
232 scoped_ptr<Window> window0(CreateTestWindowInShellWithId(0));
233 scoped_ptr<Window> window1(CreateTestWindowInShellWithId(1));
235 Window* top_container =
236 Shell::GetContainer(
237 Shell::GetPrimaryRootWindow(),
238 internal::kShellWindowId_AlwaysOnTopContainer);
239 scoped_ptr<Window> window2(CreateTestWindowWithId(2, top_container));
240 wm::ActivateWindow(window0.get());
242 // Simulate pressing and releasing Alt-tab.
243 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
244 controller->HandleCycleWindow(WindowCycleController::FORWARD, true);
246 // Window lists should return the topmost window in front.
247 ASSERT_TRUE(controller->windows());
248 ASSERT_EQ(3u, controller->windows()->windows().size());
249 EXPECT_EQ(window0.get(), controller->windows()->windows()[0]);
250 EXPECT_EQ(window2.get(), controller->windows()->windows()[1]);
251 EXPECT_EQ(window1.get(), controller->windows()->windows()[2]);
253 controller->AltKeyReleased();
254 EXPECT_TRUE(wm::IsActiveWindow(window2.get()));
256 controller->HandleCycleWindow(WindowCycleController::FORWARD, true);
257 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
259 controller->AltKeyReleased();
261 controller->HandleCycleWindow(WindowCycleController::FORWARD, true);
262 EXPECT_TRUE(wm::IsActiveWindow(window2.get()));
264 controller->HandleCycleWindow(WindowCycleController::FORWARD, true);
265 EXPECT_TRUE(wm::IsActiveWindow(window1.get()));
267 controller->HandleCycleWindow(WindowCycleController::FORWARD, true);
268 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
271 TEST_F(WindowCycleControllerTest, AlwaysOnTopMultiWindow) {
272 WindowCycleController* controller =
273 Shell::GetInstance()->window_cycle_controller();
275 // Set up several windows to use to test cycling.
276 scoped_ptr<Window> window0(CreateTestWindowInShellWithId(0));
277 scoped_ptr<Window> window1(CreateTestWindowInShellWithId(1));
279 Window* top_container =
280 Shell::GetContainer(
281 Shell::GetPrimaryRootWindow(),
282 internal::kShellWindowId_AlwaysOnTopContainer);
283 scoped_ptr<Window> window2(CreateTestWindowWithId(2, top_container));
284 scoped_ptr<Window> window3(CreateTestWindowWithId(3, top_container));
285 wm::ActivateWindow(window0.get());
287 // Simulate pressing and releasing Alt-tab.
288 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
289 controller->HandleCycleWindow(WindowCycleController::FORWARD, true);
291 // Window lists should return the topmost window in front.
292 ASSERT_TRUE(controller->windows());
293 ASSERT_EQ(4u, controller->windows()->windows().size());
294 EXPECT_EQ(window0.get(), controller->windows()->windows()[0]);
295 EXPECT_EQ(window3.get(), controller->windows()->windows()[1]);
296 EXPECT_EQ(window2.get(), controller->windows()->windows()[2]);
297 EXPECT_EQ(window1.get(), controller->windows()->windows()[3]);
299 controller->AltKeyReleased();
300 EXPECT_TRUE(wm::IsActiveWindow(window3.get()));
302 controller->HandleCycleWindow(WindowCycleController::FORWARD, true);
303 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
305 controller->AltKeyReleased();
307 controller->HandleCycleWindow(WindowCycleController::FORWARD, true);
308 EXPECT_TRUE(wm::IsActiveWindow(window3.get()));
310 controller->HandleCycleWindow(WindowCycleController::FORWARD, true);
311 EXPECT_TRUE(wm::IsActiveWindow(window2.get()));
313 controller->HandleCycleWindow(WindowCycleController::FORWARD, true);
314 EXPECT_TRUE(wm::IsActiveWindow(window1.get()));
316 controller->HandleCycleWindow(WindowCycleController::FORWARD, true);
317 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
320 TEST_F(WindowCycleControllerTest, AlwaysOnTopMultipleRootWindows) {
321 // Set up a second root window
322 UpdateDisplay("0+0-1000x600,1001+0-600x400");
323 Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
324 ASSERT_EQ(2U, root_windows.size());
326 WindowCycleController* controller =
327 Shell::GetInstance()->window_cycle_controller();
329 Shell::GetInstance()->set_active_root_window(root_windows[0]);
331 // Create two windows in the primary root.
332 scoped_ptr<Window> window0(CreateTestWindowInShellWithId(0));
333 EXPECT_EQ(root_windows[0], window0->GetRootWindow());
334 Window* top_container0 =
335 Shell::GetContainer(
336 root_windows[0],
337 internal::kShellWindowId_AlwaysOnTopContainer);
338 scoped_ptr<Window> window1(CreateTestWindowWithId(1, top_container0));
339 EXPECT_EQ(root_windows[0], window1->GetRootWindow());
341 // And two on the secondary root.
342 Shell::GetInstance()->set_active_root_window(root_windows[1]);
343 scoped_ptr<Window> window2(CreateTestWindowInShellWithId(2));
344 EXPECT_EQ(root_windows[1], window2->GetRootWindow());
346 Window* top_container1 =
347 Shell::GetContainer(
348 root_windows[1],
349 internal::kShellWindowId_AlwaysOnTopContainer);
350 scoped_ptr<Window> window3(CreateTestWindowWithId(3, top_container1));
351 EXPECT_EQ(root_windows[1], window3->GetRootWindow());
353 // Move the active root window to the secondary.
354 Shell::GetInstance()->set_active_root_window(root_windows[1]);
356 wm::ActivateWindow(window2.get());
358 EXPECT_EQ(root_windows[0], window0->GetRootWindow());
359 EXPECT_EQ(root_windows[0], window1->GetRootWindow());
360 EXPECT_EQ(root_windows[1], window2->GetRootWindow());
361 EXPECT_EQ(root_windows[1], window3->GetRootWindow());
363 // Simulate pressing and releasing Alt-tab.
364 EXPECT_TRUE(wm::IsActiveWindow(window2.get()));
365 controller->HandleCycleWindow(WindowCycleController::FORWARD, true);
367 // Window lists should return the topmost window in front.
368 ASSERT_TRUE(controller->windows());
369 ASSERT_EQ(4u, controller->windows()->windows().size());
370 EXPECT_EQ(window2.get(), controller->windows()->windows()[0]);
371 EXPECT_EQ(window3.get(), controller->windows()->windows()[1]);
372 EXPECT_EQ(window1.get(), controller->windows()->windows()[2]);
373 EXPECT_EQ(window0.get(), controller->windows()->windows()[3]);
375 controller->AltKeyReleased();
376 EXPECT_TRUE(wm::IsActiveWindow(window3.get()));
378 controller->HandleCycleWindow(WindowCycleController::FORWARD, true);
379 EXPECT_TRUE(wm::IsActiveWindow(window2.get()));
381 controller->AltKeyReleased();
383 controller->HandleCycleWindow(WindowCycleController::FORWARD, true);
384 EXPECT_TRUE(wm::IsActiveWindow(window3.get()));
386 controller->HandleCycleWindow(WindowCycleController::FORWARD, true);
387 EXPECT_TRUE(wm::IsActiveWindow(window1.get()));
389 controller->HandleCycleWindow(WindowCycleController::FORWARD, true);
390 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
392 controller->HandleCycleWindow(WindowCycleController::FORWARD, true);
393 EXPECT_TRUE(wm::IsActiveWindow(window2.get()));
396 TEST_F(WindowCycleControllerTest, MostRecentlyUsed) {
397 WindowCycleController* controller =
398 Shell::GetInstance()->window_cycle_controller();
400 // Set up several windows to use to test cycling.
401 scoped_ptr<Window> window0(CreateTestWindowInShellWithId(0));
402 scoped_ptr<Window> window1(CreateTestWindowInShellWithId(1));
403 scoped_ptr<Window> window2(CreateTestWindowInShellWithId(2));
405 wm::ActivateWindow(window0.get());
407 // Simulate pressing and releasing Alt-tab.
408 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
409 controller->HandleCycleWindow(WindowCycleController::FORWARD, true);
411 // Window lists should return the topmost window in front.
412 ASSERT_TRUE(controller->windows());
413 ASSERT_EQ(3u, controller->windows()->windows().size());
414 EXPECT_EQ(window0.get(), controller->windows()->windows()[0]);
415 EXPECT_EQ(window2.get(), controller->windows()->windows()[1]);
416 EXPECT_EQ(window1.get(), controller->windows()->windows()[2]);
418 controller->HandleCycleWindow(WindowCycleController::FORWARD, true);
419 controller->AltKeyReleased();
420 EXPECT_TRUE(wm::IsActiveWindow(window1.get()));
423 controller->HandleCycleWindow(WindowCycleController::FORWARD, true);
424 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
426 controller->AltKeyReleased();
428 controller->HandleCycleWindow(WindowCycleController::FORWARD, true);
429 EXPECT_TRUE(wm::IsActiveWindow(window1.get()));
431 controller->HandleCycleWindow(WindowCycleController::FORWARD, true);
432 EXPECT_TRUE(wm::IsActiveWindow(window2.get()));
434 controller->HandleCycleWindow(WindowCycleController::FORWARD, true);
435 EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
438 } // namespace
440 } // namespace ash