1 // Copyright 2013 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_commands.h"
7 #include "apps/shell_window.h"
8 #include "apps/ui/native_app_window.h"
9 #include "ash/ash_switches.h"
10 #include "ash/shell.h"
11 #include "ash/wm/window_state.h"
12 #include "base/command_line.h"
13 #include "chrome/browser/apps/app_browsertest_util.h"
14 #include "chrome/browser/ui/browser.h"
15 #include "chrome/browser/ui/browser_commands.h"
16 #include "chrome/browser/ui/browser_finder.h"
17 #include "chrome/browser/ui/browser_window.h"
18 #include "chrome/common/chrome_switches.h"
19 #include "chrome/test/base/in_process_browser_test.h"
20 #include "chrome/test/base/test_switches.h"
21 #include "ui/aura/client/aura_constants.h"
22 #include "ui/views/widget/widget.h"
23 #include "ui/views/widget/widget_delegate.h"
25 using testing::Combine
;
26 using testing::Values
;
27 using testing::WithParamInterface
;
31 // WidgetDelegateView which allows the widget to be maximized.
32 class MaximizableWidgetDelegate
: public views::WidgetDelegateView
{
34 MaximizableWidgetDelegate() {
36 virtual ~MaximizableWidgetDelegate() {
39 virtual bool CanMaximize() const OVERRIDE
{
44 DISALLOW_COPY_AND_ASSIGN(MaximizableWidgetDelegate
);
47 // Returns true if |window_state|'s window is in immersive fullscreen. Infer
48 // whether the window is in immersive fullscreen based on whether the shelf is
49 // hidden when the window is fullscreen. (This is not quite right because the
50 // shelf is hidden if a window is in both immersive fullscreen and tab
52 bool IsInImmersiveFullscreen(ash::wm::WindowState
* window_state
) {
53 return window_state
->IsFullscreen() &&
54 !window_state
->hide_shelf_when_fullscreen();
59 typedef InProcessBrowserTest AcceleratorCommandsBrowserTest
;
61 // Confirm that toggling window miximized works properly
62 IN_PROC_BROWSER_TEST_F(AcceleratorCommandsBrowserTest
, ToggleMaximized
) {
64 // Run the test on Win Ash only.
65 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests
))
69 ASSERT_TRUE(ash::Shell::HasInstance()) << "No Instance";
70 ash::wm::WindowState
* window_state
= ash::wm::GetActiveWindowState();
71 ASSERT_TRUE(window_state
);
73 // When not in fullscreen, accelerators::ToggleMaximized toggles Maximized.
74 EXPECT_FALSE(window_state
->IsMaximized());
75 ash::accelerators::ToggleMaximized();
76 EXPECT_TRUE(window_state
->IsMaximized());
77 ash::accelerators::ToggleMaximized();
78 EXPECT_FALSE(window_state
->IsMaximized());
80 // When in fullscreen accelerators::ToggleMaximized gets out of fullscreen.
81 EXPECT_FALSE(window_state
->IsFullscreen());
82 Browser
* browser
= chrome::FindBrowserWithWindow(window_state
->window());
84 chrome::ToggleFullscreenMode(browser
);
85 EXPECT_TRUE(window_state
->IsFullscreen());
86 ash::accelerators::ToggleMaximized();
87 EXPECT_FALSE(window_state
->IsFullscreen());
88 EXPECT_FALSE(window_state
->IsMaximized());
89 ash::accelerators::ToggleMaximized();
90 EXPECT_FALSE(window_state
->IsFullscreen());
91 EXPECT_TRUE(window_state
->IsMaximized());
94 class AcceleratorCommandsFullscreenBrowserTest
95 : public WithParamInterface
<std::tr1::tuple
<bool, ui::WindowShowState
> >,
96 public InProcessBrowserTest
{
98 AcceleratorCommandsFullscreenBrowserTest()
99 #if defined(OS_CHROMEOS)
100 : put_browser_in_immersive_(true),
101 put_all_windows_in_immersive_(std::tr1::get
<0>(GetParam())),
103 : put_browser_in_immersive_(false),
104 put_all_windows_in_immersive_(false),
106 initial_show_state_(std::tr1::get
<1>(GetParam())) {
108 virtual ~AcceleratorCommandsFullscreenBrowserTest() {
111 // BrowserTestBase override:
112 virtual void SetUpCommandLine(CommandLine
* command_line
) OVERRIDE
{
113 InProcessBrowserTest::SetUpCommandLine(command_line
);
114 if (put_all_windows_in_immersive_
) {
115 CommandLine::ForCurrentProcess()->AppendSwitch(
116 ash::switches::kAshEnableImmersiveFullscreenForAllWindows
);
118 CommandLine::ForCurrentProcess()->AppendSwitch(
119 ash::switches::kAshEnableImmersiveFullscreenForBrowserOnly
);
123 // Sets |window_state|'s show state to |initial_show_state_|.
124 void SetToInitialShowState(ash::wm::WindowState
* window_state
) {
125 if (initial_show_state_
== ui::SHOW_STATE_MAXIMIZED
)
126 window_state
->Maximize();
128 window_state
->Restore();
131 // Returns true if |window_state|'s show state is |initial_show_state_|.
132 bool IsInitialShowState(const ash::wm::WindowState
* window_state
) const {
133 return window_state
->GetShowState() == initial_show_state_
;
136 bool put_browser_in_immersive() const {
137 return put_browser_in_immersive_
;
140 bool put_all_windows_in_immersive() const {
141 return put_all_windows_in_immersive_
;
145 bool put_browser_in_immersive_
;
146 bool put_all_windows_in_immersive_
;
147 ui::WindowShowState initial_show_state_
;
149 DISALLOW_COPY_AND_ASSIGN(AcceleratorCommandsFullscreenBrowserTest
);
152 // Test that toggling window fullscreen works properly.
153 IN_PROC_BROWSER_TEST_P(AcceleratorCommandsFullscreenBrowserTest
,
156 // Run the test on Win Ash only.
157 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests
))
161 ASSERT_TRUE(ash::Shell::HasInstance()) << "No Instance";
163 // 1) Browser windows.
164 ASSERT_TRUE(browser()->is_type_tabbed());
165 ash::wm::WindowState
* window_state
=
166 ash::wm::GetWindowState(browser()->window()->GetNativeWindow());
167 ASSERT_TRUE(window_state
->IsActive());
168 SetToInitialShowState(window_state
);
169 EXPECT_TRUE(IsInitialShowState(window_state
));
171 ash::accelerators::ToggleFullscreen();
172 EXPECT_TRUE(window_state
->IsFullscreen());
173 EXPECT_EQ(put_browser_in_immersive(), IsInImmersiveFullscreen(window_state
));
175 ash::accelerators::ToggleFullscreen();
176 EXPECT_TRUE(IsInitialShowState(window_state
));
178 // 2) ToggleFullscreen() should have no effect on windows which cannot be
180 window_state
->window()->SetProperty(aura::client::kCanMaximizeKey
, false);
181 ash::accelerators::ToggleFullscreen();
182 EXPECT_TRUE(IsInitialShowState(window_state
));
185 Browser::CreateParams
browser_create_params(Browser::TYPE_POPUP
,
186 browser()->profile(), chrome::HOST_DESKTOP_TYPE_ASH
);
187 browser_create_params
.app_name
= "Test";
189 Browser
* app_host_browser
= new Browser(browser_create_params
);
190 ASSERT_TRUE(app_host_browser
->is_app());
191 AddBlankTabAndShow(app_host_browser
);
193 ash::wm::GetWindowState(app_host_browser
->window()->GetNativeWindow());
194 ASSERT_TRUE(window_state
->IsActive());
195 SetToInitialShowState(window_state
);
196 EXPECT_TRUE(IsInitialShowState(window_state
));
198 ash::accelerators::ToggleFullscreen();
199 EXPECT_TRUE(window_state
->IsFullscreen());
200 EXPECT_EQ(put_all_windows_in_immersive(),
201 IsInImmersiveFullscreen(window_state
));
203 ash::accelerators::ToggleFullscreen();
204 EXPECT_TRUE(IsInitialShowState(window_state
));
206 // 4) Popup browser windows.
207 browser_create_params
.app_name
= "";
208 Browser
* popup_browser
= new Browser(browser_create_params
);
209 ASSERT_TRUE(popup_browser
->is_type_popup());
210 ASSERT_FALSE(popup_browser
->is_app());
211 AddBlankTabAndShow(popup_browser
);
213 ash::wm::GetWindowState(popup_browser
->window()->GetNativeWindow());
214 ASSERT_TRUE(window_state
->IsActive());
215 SetToInitialShowState(window_state
);
216 EXPECT_TRUE(IsInitialShowState(window_state
));
218 ash::accelerators::ToggleFullscreen();
219 EXPECT_TRUE(window_state
->IsFullscreen());
220 EXPECT_EQ(put_all_windows_in_immersive(),
221 IsInImmersiveFullscreen(window_state
));
223 ash::accelerators::ToggleFullscreen();
224 EXPECT_TRUE(IsInitialShowState(window_state
));
226 // 5) Miscellaneous windows (e.g. task manager).
227 views::Widget::InitParams params
;
228 params
.delegate
= new MaximizableWidgetDelegate();
229 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
230 scoped_ptr
<views::Widget
> widget(new views::Widget
);
231 widget
->Init(params
);
234 window_state
= ash::wm::GetWindowState(widget
->GetNativeWindow());
235 ASSERT_TRUE(window_state
->IsActive());
236 SetToInitialShowState(window_state
);
237 EXPECT_TRUE(IsInitialShowState(window_state
));
239 ash::accelerators::ToggleFullscreen();
240 EXPECT_TRUE(window_state
->IsFullscreen());
241 EXPECT_EQ(put_all_windows_in_immersive(),
242 IsInImmersiveFullscreen(window_state
));
244 // TODO(pkotwicz|oshima): Make toggling fullscreen restore the window to its
245 // show state prior to entering fullscreen.
246 ash::accelerators::ToggleFullscreen();
247 EXPECT_FALSE(window_state
->IsFullscreen());
250 #if defined(OS_CHROMEOS)
251 INSTANTIATE_TEST_CASE_P(InitiallyRestored
,
252 AcceleratorCommandsFullscreenBrowserTest
,
253 Combine(Values(false, true),
254 Values(ui::SHOW_STATE_NORMAL
)));
255 INSTANTIATE_TEST_CASE_P(InitiallyMaximized
,
256 AcceleratorCommandsFullscreenBrowserTest
,
257 Combine(Values(false, true),
258 Values(ui::SHOW_STATE_MAXIMIZED
)));
260 // The kAshEnableImmersiveFullscreenForAllWindows flag should have no effect on
261 // Windows. Do not run the tests with and without the flag to spare some
263 INSTANTIATE_TEST_CASE_P(InitiallyRestored
,
264 AcceleratorCommandsFullscreenBrowserTest
,
265 Combine(Values(false),
266 Values(ui::SHOW_STATE_NORMAL
)));
267 INSTANTIATE_TEST_CASE_P(InitiallyMaximized
,
268 AcceleratorCommandsFullscreenBrowserTest
,
269 Combine(Values(false),
270 Values(ui::SHOW_STATE_MAXIMIZED
)));
273 class AcceleratorCommandsPlatformAppFullscreenBrowserTest
274 : public WithParamInterface
<std::tr1::tuple
<bool, ui::WindowShowState
> >,
275 public extensions::PlatformAppBrowserTest
{
277 AcceleratorCommandsPlatformAppFullscreenBrowserTest()
278 #if defined(OS_CHROMEOS)
279 : put_all_windows_in_immersive_(std::tr1::get
<0>(GetParam())),
281 : put_all_windows_in_immersive_(false),
283 initial_show_state_(std::tr1::get
<1>(GetParam())) {
285 virtual ~AcceleratorCommandsPlatformAppFullscreenBrowserTest() {
288 // Sets |shell_window|'s show state to |initial_show_state_|.
289 void SetToInitialShowState(apps::ShellWindow
* shell_window
) {
290 if (initial_show_state_
== ui::SHOW_STATE_MAXIMIZED
)
291 shell_window
->Maximize();
293 shell_window
->Restore();
296 // Returns true if |shell_window|'s show state is |initial_show_state_|.
297 bool IsInitialShowState(apps::ShellWindow
* shell_window
) const {
298 if (initial_show_state_
== ui::SHOW_STATE_MAXIMIZED
)
299 return shell_window
->GetBaseWindow()->IsMaximized();
301 return ui::BaseWindow::IsRestored(*shell_window
->GetBaseWindow());
304 // content::BrowserTestBase override:
305 virtual void SetUpCommandLine(CommandLine
* command_line
) OVERRIDE
{
306 if (put_all_windows_in_immersive_
) {
307 CommandLine::ForCurrentProcess()->AppendSwitch(
308 ash::switches::kAshEnableImmersiveFullscreenForAllWindows
);
310 CommandLine::ForCurrentProcess()->AppendSwitch(
311 ash::switches::kAshEnableImmersiveFullscreenForBrowserOnly
);
313 extensions::PlatformAppBrowserTest::SetUpCommandLine(command_line
);
316 bool put_all_windows_in_immersive() const {
317 return put_all_windows_in_immersive_
;
321 bool put_all_windows_in_immersive_
;
322 ui::WindowShowState initial_show_state_
;
324 DISALLOW_COPY_AND_ASSIGN(AcceleratorCommandsPlatformAppFullscreenBrowserTest
);
327 // Test the behavior of platform apps when ToggleFullscreen() is called.
328 IN_PROC_BROWSER_TEST_P(AcceleratorCommandsPlatformAppFullscreenBrowserTest
,
331 // Run the test on Win Ash only.
332 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests
))
336 ASSERT_TRUE(ash::Shell::HasInstance()) << "No Instance";
337 const extensions::Extension
* extension
= LoadAndLaunchPlatformApp("minimal");
340 // Test that ToggleFullscreen() toggles a platform's app's fullscreen
341 // state and that it additionally puts the app into immersive fullscreen
342 // if put_all_windows_in_immersive() returns true.
343 apps::ShellWindow::CreateParams params
;
344 params
.frame
= apps::ShellWindow::FRAME_CHROME
;
345 apps::ShellWindow
* shell_window
= CreateShellWindowFromParams(
347 apps::NativeAppWindow
* app_window
= shell_window
->GetBaseWindow();
348 SetToInitialShowState(shell_window
);
349 ASSERT_TRUE(shell_window
->GetBaseWindow()->IsActive());
350 EXPECT_TRUE(IsInitialShowState(shell_window
));
352 ash::accelerators::ToggleFullscreen();
353 EXPECT_TRUE(app_window
->IsFullscreen());
354 ash::wm::WindowState
* window_state
=
355 ash::wm::GetWindowState(app_window
->GetNativeWindow());
356 EXPECT_EQ(put_all_windows_in_immersive(),
357 IsInImmersiveFullscreen(window_state
));
359 ash::accelerators::ToggleFullscreen();
360 EXPECT_TRUE(IsInitialShowState(shell_window
));
362 CloseShellWindow(shell_window
);
366 // Repeat the test, but make sure that frameless platform apps are never put
367 // into immersive fullscreen.
368 apps::ShellWindow::CreateParams params
;
369 params
.frame
= apps::ShellWindow::FRAME_NONE
;
370 apps::ShellWindow
* shell_window
= CreateShellWindowFromParams(
372 apps::NativeAppWindow
* app_window
= shell_window
->GetBaseWindow();
373 ASSERT_TRUE(shell_window
->GetBaseWindow()->IsActive());
374 SetToInitialShowState(shell_window
);
375 EXPECT_TRUE(IsInitialShowState(shell_window
));
377 ash::accelerators::ToggleFullscreen();
378 EXPECT_TRUE(app_window
->IsFullscreen());
379 ash::wm::WindowState
* window_state
=
380 ash::wm::GetWindowState(app_window
->GetNativeWindow());
381 EXPECT_FALSE(IsInImmersiveFullscreen(window_state
));
383 ash::accelerators::ToggleFullscreen();
384 EXPECT_TRUE(IsInitialShowState(shell_window
));
386 CloseShellWindow(shell_window
);
390 #if defined(OS_CHROMEOS)
391 INSTANTIATE_TEST_CASE_P(InitiallyRestored
,
392 AcceleratorCommandsPlatformAppFullscreenBrowserTest
,
393 Combine(Values(false, true),
394 Values(ui::SHOW_STATE_NORMAL
)));
395 INSTANTIATE_TEST_CASE_P(InitiallyMaximized
,
396 AcceleratorCommandsPlatformAppFullscreenBrowserTest
,
397 Combine(Values(false, true),
398 Values(ui::SHOW_STATE_MAXIMIZED
)));
400 // The kAshEnableImmersiveFullscreenForAllWindows flag should have no effect on
401 // Windows. Do not run the tests with and without the flag to spare some
403 INSTANTIATE_TEST_CASE_P(InitiallyRestored
,
404 AcceleratorCommandsPlatformAppFullscreenBrowserTest
,
405 Combine(Values(false),
406 Values(ui::SHOW_STATE_NORMAL
)));
407 INSTANTIATE_TEST_CASE_P(InitiallyMaximized
,
408 AcceleratorCommandsPlatformAppFullscreenBrowserTest
,
409 Combine(Values(false),
410 Values(ui::SHOW_STATE_MAXIMIZED
)));