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 "ash/ash_switches.h"
9 #include "ash/wm/window_state.h"
10 #include "base/command_line.h"
11 #include "chrome/browser/apps/app_browsertest_util.h"
12 #include "chrome/browser/ui/browser.h"
13 #include "chrome/browser/ui/browser_commands.h"
14 #include "chrome/browser/ui/browser_finder.h"
15 #include "chrome/browser/ui/browser_window.h"
16 #include "chrome/common/chrome_switches.h"
17 #include "chrome/test/base/in_process_browser_test.h"
18 #include "chrome/test/base/test_switches.h"
19 #include "extensions/browser/app_window/app_window.h"
20 #include "extensions/browser/app_window/native_app_window.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 ~MaximizableWidgetDelegate() override
{}
38 bool CanMaximize() const override
{ return true; }
41 DISALLOW_COPY_AND_ASSIGN(MaximizableWidgetDelegate
);
44 // Returns true if |window_state|'s window is in immersive fullscreen. Infer
45 // whether the window is in immersive fullscreen based on whether the shelf is
46 // hidden when the window is fullscreen. (This is not quite right because the
47 // shelf is hidden if a window is in both immersive fullscreen and tab
49 bool IsInImmersiveFullscreen(ash::wm::WindowState
* window_state
) {
50 return window_state
->IsFullscreen() &&
51 !window_state
->hide_shelf_when_fullscreen();
56 typedef InProcessBrowserTest AcceleratorCommandsBrowserTest
;
58 // Confirm that toggling window miximized works properly
59 IN_PROC_BROWSER_TEST_F(AcceleratorCommandsBrowserTest
, ToggleMaximized
) {
61 // Run the test on Win Ash only.
62 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
63 switches::kAshBrowserTests
))
67 ASSERT_TRUE(ash::Shell::HasInstance()) << "No Instance";
68 ash::wm::WindowState
* window_state
= ash::wm::GetActiveWindowState();
69 ASSERT_TRUE(window_state
);
71 // When not in fullscreen, accelerators::ToggleMaximized toggles Maximized.
72 EXPECT_FALSE(window_state
->IsMaximized());
73 ash::accelerators::ToggleMaximized();
74 EXPECT_TRUE(window_state
->IsMaximized());
75 ash::accelerators::ToggleMaximized();
76 EXPECT_FALSE(window_state
->IsMaximized());
78 // When in fullscreen accelerators::ToggleMaximized gets out of fullscreen.
79 EXPECT_FALSE(window_state
->IsFullscreen());
80 Browser
* browser
= chrome::FindBrowserWithWindow(window_state
->window());
82 chrome::ToggleFullscreenMode(browser
);
83 EXPECT_TRUE(window_state
->IsFullscreen());
84 ash::accelerators::ToggleMaximized();
85 EXPECT_FALSE(window_state
->IsFullscreen());
86 EXPECT_FALSE(window_state
->IsMaximized());
87 ash::accelerators::ToggleMaximized();
88 EXPECT_FALSE(window_state
->IsFullscreen());
89 EXPECT_TRUE(window_state
->IsMaximized());
92 class AcceleratorCommandsFullscreenBrowserTest
93 : public WithParamInterface
<ui::WindowShowState
>,
94 public InProcessBrowserTest
{
96 AcceleratorCommandsFullscreenBrowserTest()
97 : initial_show_state_(GetParam()) {
99 virtual ~AcceleratorCommandsFullscreenBrowserTest() {
102 // Sets |window_state|'s show state to |initial_show_state_|.
103 void SetToInitialShowState(ash::wm::WindowState
* window_state
) {
104 if (initial_show_state_
== ui::SHOW_STATE_MAXIMIZED
)
105 window_state
->Maximize();
107 window_state
->Restore();
110 // Returns true if |window_state|'s show state is |initial_show_state_|.
111 bool IsInitialShowState(const ash::wm::WindowState
* window_state
) const {
112 if (initial_show_state_
== ui::SHOW_STATE_MAXIMIZED
)
113 return window_state
->IsMaximized();
115 return window_state
->IsNormalStateType();
119 ui::WindowShowState initial_show_state_
;
121 DISALLOW_COPY_AND_ASSIGN(AcceleratorCommandsFullscreenBrowserTest
);
124 // Test that toggling window fullscreen works properly.
125 IN_PROC_BROWSER_TEST_P(AcceleratorCommandsFullscreenBrowserTest
,
128 // Run the test on Win Ash only.
129 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
130 switches::kAshBrowserTests
))
134 ASSERT_TRUE(ash::Shell::HasInstance()) << "No Instance";
136 // 1) Browser windows.
137 ASSERT_TRUE(browser()->is_type_tabbed());
138 ash::wm::WindowState
* window_state
=
139 ash::wm::GetWindowState(browser()->window()->GetNativeWindow());
140 ASSERT_TRUE(window_state
->IsActive());
141 SetToInitialShowState(window_state
);
142 EXPECT_TRUE(IsInitialShowState(window_state
));
144 ash::accelerators::ToggleFullscreen();
145 EXPECT_TRUE(window_state
->IsFullscreen());
146 EXPECT_TRUE(IsInImmersiveFullscreen(window_state
));
148 ash::accelerators::ToggleFullscreen();
149 EXPECT_TRUE(IsInitialShowState(window_state
));
151 // 2) ToggleFullscreen() should have no effect on windows which cannot be
153 window_state
->window()->SetProperty(aura::client::kCanMaximizeKey
, false);
154 ash::accelerators::ToggleFullscreen();
155 EXPECT_TRUE(IsInitialShowState(window_state
));
158 Browser::CreateParams
browser_create_params(
159 Browser::CreateParams::CreateForApp("Test",
160 true /* trusted_source */,
162 browser()->profile(),
163 chrome::HOST_DESKTOP_TYPE_ASH
));
165 Browser
* app_host_browser
= new Browser(browser_create_params
);
166 ASSERT_TRUE(app_host_browser
->is_app());
167 AddBlankTabAndShow(app_host_browser
);
169 ash::wm::GetWindowState(app_host_browser
->window()->GetNativeWindow());
170 ASSERT_TRUE(window_state
->IsActive());
171 SetToInitialShowState(window_state
);
172 EXPECT_TRUE(IsInitialShowState(window_state
));
174 ash::accelerators::ToggleFullscreen();
175 EXPECT_TRUE(window_state
->IsFullscreen());
176 EXPECT_TRUE(IsInImmersiveFullscreen(window_state
));
178 ash::accelerators::ToggleFullscreen();
179 EXPECT_TRUE(IsInitialShowState(window_state
));
181 // 4) Popup browser windows.
182 browser_create_params
= Browser::CreateParams(
183 Browser::TYPE_POPUP
, browser()->profile(), chrome::HOST_DESKTOP_TYPE_ASH
);
184 Browser
* popup_browser
= new Browser(browser_create_params
);
185 ASSERT_TRUE(popup_browser
->is_type_popup());
186 ASSERT_FALSE(popup_browser
->is_app());
187 AddBlankTabAndShow(popup_browser
);
189 ash::wm::GetWindowState(popup_browser
->window()->GetNativeWindow());
190 ASSERT_TRUE(window_state
->IsActive());
191 SetToInitialShowState(window_state
);
192 EXPECT_TRUE(IsInitialShowState(window_state
));
194 ash::accelerators::ToggleFullscreen();
195 EXPECT_TRUE(window_state
->IsFullscreen());
196 EXPECT_TRUE(IsInImmersiveFullscreen(window_state
));
198 ash::accelerators::ToggleFullscreen();
199 EXPECT_TRUE(IsInitialShowState(window_state
));
201 // 5) Miscellaneous windows (e.g. task manager).
202 views::Widget::InitParams params
;
203 params
.delegate
= new MaximizableWidgetDelegate();
204 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
205 scoped_ptr
<views::Widget
> widget(new views::Widget
);
206 widget
->Init(params
);
209 window_state
= ash::wm::GetWindowState(widget
->GetNativeWindow());
210 ASSERT_TRUE(window_state
->IsActive());
211 SetToInitialShowState(window_state
);
212 EXPECT_TRUE(IsInitialShowState(window_state
));
214 ash::accelerators::ToggleFullscreen();
215 EXPECT_TRUE(window_state
->IsFullscreen());
216 EXPECT_TRUE(IsInImmersiveFullscreen(window_state
));
218 // TODO(pkotwicz|oshima): Make toggling fullscreen restore the window to its
219 // show state prior to entering fullscreen.
220 ash::accelerators::ToggleFullscreen();
221 EXPECT_FALSE(window_state
->IsFullscreen());
224 INSTANTIATE_TEST_CASE_P(InitiallyRestored
,
225 AcceleratorCommandsFullscreenBrowserTest
,
226 Values(ui::SHOW_STATE_NORMAL
));
227 INSTANTIATE_TEST_CASE_P(InitiallyMaximized
,
228 AcceleratorCommandsFullscreenBrowserTest
,
229 Values(ui::SHOW_STATE_MAXIMIZED
));
231 class AcceleratorCommandsPlatformAppFullscreenBrowserTest
232 : public WithParamInterface
<ui::WindowShowState
>,
233 public extensions::PlatformAppBrowserTest
{
235 AcceleratorCommandsPlatformAppFullscreenBrowserTest()
236 : initial_show_state_(GetParam()) {
238 virtual ~AcceleratorCommandsPlatformAppFullscreenBrowserTest() {
241 // Sets |app_window|'s show state to |initial_show_state_|.
242 void SetToInitialShowState(extensions::AppWindow
* app_window
) {
243 if (initial_show_state_
== ui::SHOW_STATE_MAXIMIZED
)
244 app_window
->Maximize();
246 app_window
->Restore();
249 // Returns true if |app_window|'s show state is |initial_show_state_|.
250 bool IsInitialShowState(extensions::AppWindow
* app_window
) const {
251 if (initial_show_state_
== ui::SHOW_STATE_MAXIMIZED
)
252 return app_window
->GetBaseWindow()->IsMaximized();
254 return ui::BaseWindow::IsRestored(*app_window
->GetBaseWindow());
258 ui::WindowShowState initial_show_state_
;
260 DISALLOW_COPY_AND_ASSIGN(AcceleratorCommandsPlatformAppFullscreenBrowserTest
);
263 // Test the behavior of platform apps when ToggleFullscreen() is called.
264 IN_PROC_BROWSER_TEST_P(AcceleratorCommandsPlatformAppFullscreenBrowserTest
,
267 // Run the test on Win Ash only.
268 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
269 switches::kAshBrowserTests
))
273 ASSERT_TRUE(ash::Shell::HasInstance()) << "No Instance";
274 const extensions::Extension
* extension
= LoadAndLaunchPlatformApp("minimal",
278 // Test that ToggleFullscreen() toggles a platform's app's fullscreen
279 // state and that it additionally puts the app into immersive fullscreen
280 // if put_all_windows_in_immersive() returns true.
281 extensions::AppWindow::CreateParams params
;
282 params
.frame
= extensions::AppWindow::FRAME_CHROME
;
283 extensions::AppWindow
* app_window
=
284 CreateAppWindowFromParams(extension
, params
);
285 extensions::NativeAppWindow
* native_app_window
=
286 app_window
->GetBaseWindow();
287 SetToInitialShowState(app_window
);
288 ASSERT_TRUE(app_window
->GetBaseWindow()->IsActive());
289 EXPECT_TRUE(IsInitialShowState(app_window
));
291 ash::accelerators::ToggleFullscreen();
292 EXPECT_TRUE(native_app_window
->IsFullscreen());
293 ash::wm::WindowState
* window_state
=
294 ash::wm::GetWindowState(native_app_window
->GetNativeWindow());
295 EXPECT_TRUE(IsInImmersiveFullscreen(window_state
));
297 ash::accelerators::ToggleFullscreen();
298 EXPECT_TRUE(IsInitialShowState(app_window
));
300 CloseAppWindow(app_window
);
304 // Repeat the test, but make sure that frameless platform apps are never put
305 // into immersive fullscreen.
306 extensions::AppWindow::CreateParams params
;
307 params
.frame
= extensions::AppWindow::FRAME_NONE
;
308 extensions::AppWindow
* app_window
=
309 CreateAppWindowFromParams(extension
, params
);
310 extensions::NativeAppWindow
* native_app_window
=
311 app_window
->GetBaseWindow();
312 ASSERT_TRUE(app_window
->GetBaseWindow()->IsActive());
313 SetToInitialShowState(app_window
);
314 EXPECT_TRUE(IsInitialShowState(app_window
));
316 ash::accelerators::ToggleFullscreen();
317 EXPECT_TRUE(native_app_window
->IsFullscreen());
318 ash::wm::WindowState
* window_state
=
319 ash::wm::GetWindowState(native_app_window
->GetNativeWindow());
320 EXPECT_FALSE(IsInImmersiveFullscreen(window_state
));
322 ash::accelerators::ToggleFullscreen();
323 EXPECT_TRUE(IsInitialShowState(app_window
));
325 CloseAppWindow(app_window
);
329 INSTANTIATE_TEST_CASE_P(InitiallyRestored
,
330 AcceleratorCommandsPlatformAppFullscreenBrowserTest
,
331 Values(ui::SHOW_STATE_NORMAL
));
332 INSTANTIATE_TEST_CASE_P(InitiallyMaximized
,
333 AcceleratorCommandsPlatformAppFullscreenBrowserTest
,
334 Values(ui::SHOW_STATE_MAXIMIZED
));