blob: 9fcc2ada3f961dfa9aadcd9216104472e12ce44a [file] [log] [blame]
zijiehebb317b92017-07-15 00:01:401// Copyright 2017 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 <memory>
6#include <string>
7
8#include "base/macros.h"
9#include "base/memory/ptr_util.h"
10#include "base/strings/string_util.h"
11#include "build/build_config.h"
12#include "chrome/app/chrome_command_ids.h"
13#include "chrome/browser/chrome_notification_types.h"
14#include "chrome/browser/ui/browser.h"
15#include "chrome/browser/ui/browser_commands.h"
16#include "chrome/browser/ui/tabs/tab_strip_model.h"
zijiehebb317b92017-07-15 00:01:4017#include "chrome/test/base/in_process_browser_test.h"
18#include "chrome/test/base/interactive_test_utils.h"
19#include "content/public/browser/notification_service.h"
Hzj_jie24cdbc102017-08-11 20:02:2420#include "content/public/common/browser_side_navigation_policy.h"
zijiehebb317b92017-07-15 00:01:4021#include "content/public/test/browser_test_utils.h"
22#include "content/public/test/test_utils.h"
23#include "ui/events/keycodes/dom/keycode_converter.h"
24#include "ui/events/keycodes/keyboard_code_conversion.h"
25#include "ui/events/keycodes/keyboard_codes.h"
26#include "url/gurl.h"
27#include "url/url_constants.h"
28
29#if defined(OS_MACOSX)
30#include "base/mac/mac_util.h"
31#endif
32
33namespace {
Hzj_jie24cdbc102017-08-11 20:02:2434
zijiehebb317b92017-07-15 00:01:4035// The html file to receive key events, prevent defaults and export all the
36// events with "getKeyEventReport()" function. It has two magic keys: pressing
37// "S" to enter fullscreen mode; pressing "X" to indicate the end of all the
38// keys (see FinishTestAndVerifyResult() function).
39constexpr char kFullscreenKeyboardLockHTML[] = "/fullscreen_keyboardlock.html";
40
41// On MacOSX command key is used for most of the shortcuts, so replace it with
42// control to reduce the complexity of comparison of the results.
43void NormalizeMetaKeyForMacOS(std::string* output) {
44#if defined(OS_MACOSX)
45 base::ReplaceSubstringsAfterOffset(output, 0, "MetaLeft", "ControlLeft");
46#endif
47}
48
49} // namespace
50
51class BrowserCommandControllerInteractiveTest : public InProcessBrowserTest {
52 public:
53 BrowserCommandControllerInteractiveTest() = default;
54 ~BrowserCommandControllerInteractiveTest() override = default;
55
56 protected:
Hzj_jie24cdbc102017-08-11 20:02:2457 // Starts |kFullscreenKeyboardLockHTML| in a new tab and waits for load.
58 void StartFullscreenLockPage();
zijiehebb317b92017-07-15 00:01:4059
60 // Sends a control or command + |key| shortcut to the focused window. Shift
61 // modifier will be added if |shift| is true.
62 void SendShortcut(ui::KeyboardCode key, bool shift = false);
63
64 // Sends a control or command + shift + |key| shortcut to the focused window.
65 void SendShiftShortcut(ui::KeyboardCode key);
66
67 // Sends a fullscreen shortcut to the focused window and wait for the
68 // operation to take effect.
69 void SendFullscreenShortcutAndWait();
70
71 // Sends a KeyS to the focused window to trigger JavaScript fullscreen and
72 // wait for the operation to take effect.
73 void SendJsFullscreenShortcutAndWait();
74
75 // Sends an ESC to the focused window.
76 void SendEscape();
77
78 // Sends an ESC to the focused window to exit JavaScript fullscreen and wait
79 // for the operation to take effect.
80 void SendEscapeAndWaitForExitingFullscreen();
81
Hzj_jie24cdbc102017-08-11 20:02:2482 // Sends a set of preventable shortcuts to the web page and expects them to be
83 // prevented.
84 void SendShortcutsAndExpectPrevented();
85
86 // Sends a set of preventable shortcuts to the web page and expects them to
87 // not be prevented. If |js_fullscreen| is true, the test will use
88 // SendJsFullscreenShortcutAndWait() to trigger the fullscreen mode. Otherwise
89 // SendFullscreenShortcutAndWait() will be used.
90 void SendShortcutsAndExpectNotPrevented(bool js_fullscreen);
zijiehebb317b92017-07-15 00:01:4091
92 // Sends a magic KeyX to the focused window to stop the test case, receives
93 // the result and verifies if it is equal to |expected_result_|.
94 void FinishTestAndVerifyResult();
95
Hzj_jie24cdbc102017-08-11 20:02:2496 // Returns whether the active tab is in html fullscreen mode.
97 bool IsActiveTabFullscreen() const {
98 auto* contents = GetActiveWebContents();
99 return contents->GetDelegate()->IsFullscreenForTabOrPending(contents);
100 }
101
102 // Returns whether the GetActiveBrowser() is in browser fullscreen mode.
103 bool IsInBrowserFullscreen() const {
104 return GetActiveBrowser()
105 ->exclusive_access_manager()
106 ->fullscreen_controller()
107 ->IsFullscreenForBrowser();
108 }
109
110 content::WebContents* GetActiveWebContents() const {
111 return GetActiveBrowser()->tab_strip_model()->GetActiveWebContents();
112 }
113
114 // Gets the current active tab index.
115 int GetActiveTabIndex() const {
116 return GetActiveBrowser()->tab_strip_model()->active_index();
117 }
118
119 // Gets the count of tabs in current browser.
120 int GetTabCount() const {
121 return GetActiveBrowser()->tab_strip_model()->count();
122 }
123
124 // Gets the count of browser instances.
125 size_t GetBrowserCount() const {
126 return BrowserList::GetInstance()->size();
127 }
128
129 // Gets the last active Browser instance.
130 Browser* GetActiveBrowser() const {
131 return BrowserList::GetInstance()->GetLastActive();
132 }
133
134 // Ensures GetActiveBrowser() is focused.
135 void FocusOnLastActiveBrowser() {
136 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(
137 GetActiveBrowser()));
138 }
139
140 // Waits until the count of Browser instances becomes |expected|.
141 void WaitForBrowserCount(size_t expected) {
142 while (GetBrowserCount() != expected)
143 content::RunAllPendingInMessageLoop();
144 }
145
146 // Waits until the count of the tabs in active Browser instance becomes
147 // |expected|.
148 void WaitForTabCount(int expected) {
149 while (GetTabCount() != expected)
150 content::RunAllPendingInMessageLoop();
151 }
152
153 // Waits until the index of active tab in active Browser instance becomes
154 // |expected|.
155 void WaitForActiveTabIndex(int expected) {
156 while (GetActiveTabIndex() != expected)
157 content::RunAllPendingInMessageLoop();
158 }
159
160 // Waits until the index of active tab in active Browser instance is not
161 // |expected|.
162 void WaitForInactiveTabIndex(int expected) {
163 while (GetActiveTabIndex() == expected)
164 content::RunAllPendingInMessageLoop();
165 }
166
zijiehebb317b92017-07-15 00:01:40167 private:
168 void SetUpOnMainThread() override;
169
170 // The expected output from the web page. This string is generated by
171 // appending key presses from Send* functions above.
172 std::string expected_result_;
173
174 DISALLOW_COPY_AND_ASSIGN(BrowserCommandControllerInteractiveTest);
175};
176
Hzj_jie24cdbc102017-08-11 20:02:24177void BrowserCommandControllerInteractiveTest::StartFullscreenLockPage() {
zijiehebb317b92017-07-15 00:01:40178 // Ensures the initial states.
Hzj_jie24cdbc102017-08-11 20:02:24179 ASSERT_EQ(1, GetTabCount());
180 ASSERT_EQ(0, GetActiveTabIndex());
181 ASSERT_EQ(1U, GetBrowserCount());
zijiehebb317b92017-07-15 00:01:40182 // Add a second tab for counting and focus purposes.
183 AddTabAtIndex(1, GURL(url::kAboutBlankURL), ui::PAGE_TRANSITION_LINK);
Hzj_jie24cdbc102017-08-11 20:02:24184 ASSERT_EQ(2, GetTabCount());
185 ASSERT_EQ(1U, GetBrowserCount());
zijiehebb317b92017-07-15 00:01:40186
Hzj_jie24cdbc102017-08-11 20:02:24187 if (!embedded_test_server()->Started())
188 ASSERT_TRUE(embedded_test_server()->Start());
zijiehebb317b92017-07-15 00:01:40189 ui_test_utils::NavigateToURLWithDisposition(
Hzj_jie24cdbc102017-08-11 20:02:24190 GetActiveBrowser(),
191 embedded_test_server()->GetURL(kFullscreenKeyboardLockHTML),
zijiehebb317b92017-07-15 00:01:40192 WindowOpenDisposition::CURRENT_TAB,
193 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
194}
195
196void BrowserCommandControllerInteractiveTest::SendShortcut(
197 ui::KeyboardCode key,
198 bool shift /* = false */) {
199#if defined(OS_MACOSX)
200 const bool control_modifier = false;
201 const bool command_modifier = true;
202#else
203 const bool control_modifier = true;
204 const bool command_modifier = false;
205#endif
Hzj_jie24cdbc102017-08-11 20:02:24206 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
207 GetActiveBrowser(), key, control_modifier, shift, false,
208 command_modifier));
zijiehebb317b92017-07-15 00:01:40209
210 expected_result_ += ui::KeycodeConverter::DomCodeToCodeString(
211 ui::UsLayoutKeyboardCodeToDomCode(key));
212 expected_result_ += " ctrl:";
213 expected_result_ += control_modifier ? "true" : "false";
214 expected_result_ += " shift:";
215 expected_result_ += shift ? "true" : "false";
216 expected_result_ += " alt:false";
217 expected_result_ += " meta:";
218 expected_result_ += command_modifier ? "true" : "false";
219 expected_result_ += '\n';
220}
221
222void BrowserCommandControllerInteractiveTest::SendShiftShortcut(
223 ui::KeyboardCode key) {
224 ASSERT_NO_FATAL_FAILURE(SendShortcut(key, true));
225}
226
227void BrowserCommandControllerInteractiveTest::SendFullscreenShortcutAndWait() {
228 // On MacOSX, entering and exiting fullscreen are not synchronous. So we wait
229 // for the observer to notice the change of fullscreen state.
230 content::WindowedNotificationObserver observer(
231 chrome::NOTIFICATION_FULLSCREEN_CHANGED,
232 content::NotificationService::AllSources());
233 // Enter fullscreen.
234#if defined(OS_MACOSX)
235 // On MACOSX, Command + Control + F is used.
Hzj_jie24cdbc102017-08-11 20:02:24236 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
237 GetActiveBrowser(), ui::VKEY_F, true, false, false, true));
zijiehebb317b92017-07-15 00:01:40238#elif defined(OS_CHROMEOS)
239 // A dedicated fullscreen key is used on Chrome OS, so send a fullscreen
240 // command directly instead, to avoid constructing the key press.
Hzj_jie24cdbc102017-08-11 20:02:24241 ASSERT_TRUE(chrome::ExecuteCommand(GetActiveBrowser(), IDC_FULLSCREEN));
zijiehebb317b92017-07-15 00:01:40242#else
Hzj_jie24cdbc102017-08-11 20:02:24243 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
244 GetActiveBrowser(), ui::VKEY_F11, false, false, false, false));
zijiehebb317b92017-07-15 00:01:40245#endif
246
247 observer.Wait();
248}
249
250void BrowserCommandControllerInteractiveTest::
251 SendJsFullscreenShortcutAndWait() {
252 content::WindowedNotificationObserver observer(
253 chrome::NOTIFICATION_FULLSCREEN_CHANGED,
254 content::NotificationService::AllSources());
Hzj_jie24cdbc102017-08-11 20:02:24255 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
256 GetActiveBrowser(), ui::VKEY_S, false, false, false, false));
zijiehebb317b92017-07-15 00:01:40257 expected_result_ += "KeyS ctrl:false shift:false alt:false meta:false\n";
258 observer.Wait();
Hzj_jie24cdbc102017-08-11 20:02:24259 ASSERT_TRUE(IsActiveTabFullscreen());
zijiehebb317b92017-07-15 00:01:40260}
261
262void BrowserCommandControllerInteractiveTest::SendEscape() {
Hzj_jie24cdbc102017-08-11 20:02:24263 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
264 GetActiveBrowser(), ui::VKEY_ESCAPE, false, false, false, false));
zijiehebb317b92017-07-15 00:01:40265 expected_result_ += "Escape ctrl:false shift:false alt:false meta:false\n";
266}
267
268void BrowserCommandControllerInteractiveTest ::
269 SendEscapeAndWaitForExitingFullscreen() {
270 content::WindowedNotificationObserver observer(
271 chrome::NOTIFICATION_FULLSCREEN_CHANGED,
272 content::NotificationService::AllSources());
Hzj_jie24cdbc102017-08-11 20:02:24273 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
274 GetActiveBrowser(), ui::VKEY_ESCAPE, false, false, false, false));
zijiehebb317b92017-07-15 00:01:40275 observer.Wait();
Hzj_jie24cdbc102017-08-11 20:02:24276 ASSERT_FALSE(IsActiveTabFullscreen());
zijiehebb317b92017-07-15 00:01:40277}
278
Hzj_jie24cdbc102017-08-11 20:02:24279void BrowserCommandControllerInteractiveTest::
280 SendShortcutsAndExpectPrevented() {
281 const int initial_active_index = GetActiveTabIndex();
282 const int initial_tab_count = GetTabCount();
283 const size_t initial_browser_count = GetBrowserCount();
zijiehebb317b92017-07-15 00:01:40284 // The tab should not be closed.
285 ASSERT_NO_FATAL_FAILURE(SendShortcut(ui::VKEY_W));
Hzj_jie24cdbc102017-08-11 20:02:24286 ASSERT_EQ(initial_tab_count, GetTabCount());
zijiehebb317b92017-07-15 00:01:40287 // The window should not be closed.
288 ASSERT_NO_FATAL_FAILURE(SendShiftShortcut(ui::VKEY_W));
Hzj_jie24cdbc102017-08-11 20:02:24289 ASSERT_EQ(initial_browser_count, GetBrowserCount());
zijiehebb317b92017-07-15 00:01:40290 // A new tab should not be created.
291 ASSERT_NO_FATAL_FAILURE(SendShortcut(ui::VKEY_T));
Hzj_jie24cdbc102017-08-11 20:02:24292 ASSERT_EQ(initial_tab_count, GetTabCount());
zijiehebb317b92017-07-15 00:01:40293 // A new window should not be created.
294 ASSERT_NO_FATAL_FAILURE(SendShortcut(ui::VKEY_N));
Hzj_jie24cdbc102017-08-11 20:02:24295 ASSERT_EQ(initial_browser_count, GetBrowserCount());
zijiehebb317b92017-07-15 00:01:40296 // A new incognito window should not be created.
297 ASSERT_NO_FATAL_FAILURE(SendShiftShortcut(ui::VKEY_N));
Hzj_jie24cdbc102017-08-11 20:02:24298 ASSERT_EQ(initial_browser_count, GetBrowserCount());
zijiehebb317b92017-07-15 00:01:40299 // Last closed tab should not be restored.
300 ASSERT_NO_FATAL_FAILURE(SendShiftShortcut(ui::VKEY_T));
Hzj_jie24cdbc102017-08-11 20:02:24301 ASSERT_EQ(initial_tab_count, GetTabCount());
zijiehebb317b92017-07-15 00:01:40302 // Browser should not switch to the next tab.
303 ASSERT_NO_FATAL_FAILURE(SendShortcut(ui::VKEY_TAB));
Hzj_jie24cdbc102017-08-11 20:02:24304 ASSERT_EQ(initial_active_index, GetActiveTabIndex());
zijiehebb317b92017-07-15 00:01:40305 // Browser should not switch to the previous tab.
306 ASSERT_NO_FATAL_FAILURE(SendShiftShortcut(ui::VKEY_TAB));
Hzj_jie24cdbc102017-08-11 20:02:24307 ASSERT_EQ(initial_active_index, GetActiveTabIndex());
308}
309
310void BrowserCommandControllerInteractiveTest::
311 SendShortcutsAndExpectNotPrevented(bool js_fullscreen) {
312 const int initial_active_index = GetActiveTabIndex();
313 const int initial_tab_count = GetTabCount();
314 const size_t initial_browser_count = GetBrowserCount();
315 const auto enter_fullscreen = [this, js_fullscreen]() {
316 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(
317 this->GetActiveBrowser()));
318 if (js_fullscreen) {
319 if (!this->IsActiveTabFullscreen()) {
Zijie He850df0e2017-08-18 23:24:24320 static const std::string page =
321 "<html><head></head><body></body><script>"
Hzj_jie24cdbc102017-08-11 20:02:24322 "document.addEventListener('keydown', "
Zijie He850df0e2017-08-18 23:24:24323 " (e) => {"
324 " if (e.code == 'KeyS') { "
325 " document.body.webkitRequestFullscreen();"
326 " }"
327 " });"
328 "</script></html>";
Hzj_jie24cdbc102017-08-11 20:02:24329 ui_test_utils::NavigateToURLWithDisposition(
330 this->GetActiveBrowser(),
331 GURL("data:text/html," + page),
332 WindowOpenDisposition::CURRENT_TAB,
333 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
Hzj_jie24cdbc102017-08-11 20:02:24334 ASSERT_NO_FATAL_FAILURE(this->SendJsFullscreenShortcutAndWait());
Hzj_jie24cdbc102017-08-11 20:02:24335 }
336 } else {
337 if (!this->IsInBrowserFullscreen()) {
338 ASSERT_NO_FATAL_FAILURE(this->SendFullscreenShortcutAndWait());
339 }
340 ASSERT_TRUE(this->IsInBrowserFullscreen());
341 }
342 };
343
344 ASSERT_NO_FATAL_FAILURE(enter_fullscreen());
345
346 // A new tab should be created and focused.
347 ASSERT_NO_FATAL_FAILURE(SendShortcut(ui::VKEY_T));
348 WaitForTabCount(initial_tab_count + 1);
349 ASSERT_NE(initial_active_index, GetActiveTabIndex());
350
351 ASSERT_NO_FATAL_FAILURE(enter_fullscreen());
352
353 // The newly created tab should be closed.
354 ASSERT_NO_FATAL_FAILURE(SendShortcut(ui::VKEY_W));
355 WaitForTabCount(initial_tab_count);
356 ASSERT_EQ(initial_active_index, GetActiveTabIndex());
357
358 ASSERT_NO_FATAL_FAILURE(enter_fullscreen());
359
360 // A new tab should be created and focused.
361 ASSERT_NO_FATAL_FAILURE(SendShortcut(ui::VKEY_T));
362 WaitForTabCount(initial_tab_count + 1);
363 ASSERT_NE(initial_active_index, GetActiveTabIndex());
364
365 ASSERT_NO_FATAL_FAILURE(enter_fullscreen());
366
367 // The previous tab should be focused.
368 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
369 GetActiveBrowser(), ui::VKEY_TAB, true, true, false, false));
370 WaitForActiveTabIndex(initial_active_index);
371 ASSERT_EQ(initial_active_index, GetActiveTabIndex());
372
373 ASSERT_NO_FATAL_FAILURE(enter_fullscreen());
374
375 // The newly created tab should be focused.
376 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
377 GetActiveBrowser(), ui::VKEY_TAB, true, false, false, false));
378 WaitForInactiveTabIndex(initial_active_index);
379 ASSERT_NE(initial_active_index, GetActiveTabIndex());
380
381 ASSERT_NO_FATAL_FAILURE(enter_fullscreen());
382
383 // The newly created tab should be closed.
384 ASSERT_NO_FATAL_FAILURE(SendShortcut(ui::VKEY_W));
385 WaitForTabCount(initial_tab_count);
386 ASSERT_EQ(initial_active_index, GetActiveTabIndex());
387
388 ASSERT_NO_FATAL_FAILURE(enter_fullscreen());
389
390 // A new window should be created and focused.
391 ASSERT_NO_FATAL_FAILURE(SendShortcut(ui::VKEY_N));
392 WaitForBrowserCount(initial_browser_count + 1);
393 ASSERT_EQ(initial_browser_count + 1, GetBrowserCount());
394
395 ASSERT_NO_FATAL_FAILURE(enter_fullscreen());
396
397 // The newly created window should be closed.
398 ASSERT_NO_FATAL_FAILURE(SendShiftShortcut(ui::VKEY_W));
399 WaitForBrowserCount(initial_browser_count);
400
401 ASSERT_EQ(initial_browser_count, GetBrowserCount());
402 ASSERT_EQ(initial_active_index, GetActiveTabIndex());
403
404 ASSERT_NO_FATAL_FAILURE(enter_fullscreen());
zijiehebb317b92017-07-15 00:01:40405}
406
407void BrowserCommandControllerInteractiveTest::FinishTestAndVerifyResult() {
408 // The renderer process receives key events through IPC channel,
409 // SendKeyPressSync() cannot guarantee the JS has processed the key event it
410 // sent. So we sent a KeyX to the webpage to indicate the end of the test
411 // case. After processing this key event, web page is safe to send the record
412 // back through window.domAutomationController.
Hzj_jie24cdbc102017-08-11 20:02:24413 EXPECT_TRUE(ui_test_utils::SendKeyPressSync(
414 GetActiveBrowser(), ui::VKEY_X, false, false, false, false));
zijiehebb317b92017-07-15 00:01:40415 expected_result_ += "KeyX ctrl:false shift:false alt:false meta:false";
416 std::string result;
417 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
Hzj_jie24cdbc102017-08-11 20:02:24418 GetActiveWebContents()->GetRenderViewHost(),
zijiehebb317b92017-07-15 00:01:40419 "getKeyEventReport();", &result));
420 NormalizeMetaKeyForMacOS(&result);
421 NormalizeMetaKeyForMacOS(&expected_result_);
422 base::TrimWhitespaceASCII(result, base::TRIM_ALL, &result);
423 ASSERT_EQ(expected_result_, result);
424}
425
426void BrowserCommandControllerInteractiveTest::SetUpOnMainThread() {
Hzj_jie24cdbc102017-08-11 20:02:24427 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(GetActiveBrowser()));
zijiehebb317b92017-07-15 00:01:40428}
429
430IN_PROC_BROWSER_TEST_F(BrowserCommandControllerInteractiveTest,
431 ShortcutsShouldTakeEffectInWindowMode) {
Hzj_jie24cdbc102017-08-11 20:02:24432 ASSERT_EQ(1, GetTabCount());
zijiehebb317b92017-07-15 00:01:40433 ASSERT_NO_FATAL_FAILURE(SendShortcut(ui::VKEY_T));
Hzj_jie24cdbc102017-08-11 20:02:24434 ASSERT_EQ(2, GetTabCount());
zijiehebb317b92017-07-15 00:01:40435 ASSERT_NO_FATAL_FAILURE(SendShortcut(ui::VKEY_T));
Hzj_jie24cdbc102017-08-11 20:02:24436 ASSERT_EQ(3, GetTabCount());
zijiehebb317b92017-07-15 00:01:40437 ASSERT_NO_FATAL_FAILURE(SendShortcut(ui::VKEY_W));
Hzj_jie24cdbc102017-08-11 20:02:24438 ASSERT_EQ(2, GetTabCount());
zijiehebb317b92017-07-15 00:01:40439 ASSERT_NO_FATAL_FAILURE(SendShortcut(ui::VKEY_W));
Hzj_jie24cdbc102017-08-11 20:02:24440 ASSERT_EQ(1, GetTabCount());
zijiehebb317b92017-07-15 00:01:40441 ASSERT_NO_FATAL_FAILURE(SendFullscreenShortcutAndWait());
Hzj_jie24cdbc102017-08-11 20:02:24442 ASSERT_TRUE(IsInBrowserFullscreen());
443 ASSERT_FALSE(IsActiveTabFullscreen());
zijiehebb317b92017-07-15 00:01:40444}
445
446IN_PROC_BROWSER_TEST_F(BrowserCommandControllerInteractiveTest,
447 UnpreservedShortcutsShouldBePreventable) {
Hzj_jie24cdbc102017-08-11 20:02:24448 ASSERT_NO_FATAL_FAILURE(StartFullscreenLockPage());
zijiehebb317b92017-07-15 00:01:40449
450 // The browser print function should be blocked by the web page.
451 ASSERT_NO_FATAL_FAILURE(SendShortcut(ui::VKEY_P));
452 // The system print function should be blocked by the web page.
453 ASSERT_NO_FATAL_FAILURE(SendShiftShortcut(ui::VKEY_P));
454 ASSERT_NO_FATAL_FAILURE(FinishTestAndVerifyResult());
455}
456
457#if defined(OS_MACOSX)
458// TODO(zijiehe): Figure out why this test crashes on Mac OSX. The suspicious
459// command is "SendFullscreenShortcutAndWait()". See, http://crbug.com/738949.
460#define MAYBE_KeyEventsShouldBeConsumedByWebPageInBrowserFullscreen \
461 DISABLED_KeyEventsShouldBeConsumedByWebPageInBrowserFullscreen
462#else
463#define MAYBE_KeyEventsShouldBeConsumedByWebPageInBrowserFullscreen \
464 KeyEventsShouldBeConsumedByWebPageInBrowserFullscreen
465#endif
466IN_PROC_BROWSER_TEST_F(
467 BrowserCommandControllerInteractiveTest,
468 MAYBE_KeyEventsShouldBeConsumedByWebPageInBrowserFullscreen) {
Hzj_jie24cdbc102017-08-11 20:02:24469 ASSERT_NO_FATAL_FAILURE(StartFullscreenLockPage());
zijiehebb317b92017-07-15 00:01:40470
471 ASSERT_NO_FATAL_FAILURE(SendFullscreenShortcutAndWait());
Hzj_jie24cdbc102017-08-11 20:02:24472 ASSERT_FALSE(IsActiveTabFullscreen());
473 ASSERT_TRUE(IsInBrowserFullscreen());
474 ASSERT_NO_FATAL_FAILURE(SendShortcutsAndExpectPrevented());
zijiehebb317b92017-07-15 00:01:40475 // Current page should not exit browser fullscreen mode.
476 ASSERT_NO_FATAL_FAILURE(SendEscape());
477
478 ASSERT_NO_FATAL_FAILURE(FinishTestAndVerifyResult());
Hzj_jie24cdbc102017-08-11 20:02:24479
480 ASSERT_NO_FATAL_FAILURE(SendFullscreenShortcutAndWait());
481 ASSERT_FALSE(IsActiveTabFullscreen());
482 ASSERT_FALSE(IsInBrowserFullscreen());
zijiehebb317b92017-07-15 00:01:40483}
484
485IN_PROC_BROWSER_TEST_F(
486 BrowserCommandControllerInteractiveTest,
487 KeyEventsShouldBeConsumedByWebPageInJsFullscreenExceptForEsc) {
Hzj_jie24cdbc102017-08-11 20:02:24488 ASSERT_NO_FATAL_FAILURE(StartFullscreenLockPage());
zijiehebb317b92017-07-15 00:01:40489
490 ASSERT_NO_FATAL_FAILURE(SendJsFullscreenShortcutAndWait());
Hzj_jie24cdbc102017-08-11 20:02:24491 ASSERT_NO_FATAL_FAILURE(SendShortcutsAndExpectPrevented());
zijiehebb317b92017-07-15 00:01:40492 // Current page should exit HTML fullscreen mode.
493 ASSERT_NO_FATAL_FAILURE(SendEscapeAndWaitForExitingFullscreen());
494
495 ASSERT_NO_FATAL_FAILURE(FinishTestAndVerifyResult());
496}
497
498IN_PROC_BROWSER_TEST_F(
499 BrowserCommandControllerInteractiveTest,
500 KeyEventsShouldBeConsumedByWebPageInJsFullscreenExceptForF11) {
Hzj_jie24cdbc102017-08-11 20:02:24501 ASSERT_NO_FATAL_FAILURE(StartFullscreenLockPage());
zijiehebb317b92017-07-15 00:01:40502
503 ASSERT_NO_FATAL_FAILURE(SendJsFullscreenShortcutAndWait());
Hzj_jie24cdbc102017-08-11 20:02:24504 ASSERT_NO_FATAL_FAILURE(SendShortcutsAndExpectPrevented());
zijiehebb317b92017-07-15 00:01:40505#if defined(OS_MACOSX)
506 // On 10.9 or earlier, sending the exit fullscreen shortcut will crash the
507 // binary. See http://crbug.com/740250.
508 if (base::mac::IsAtLeastOS10_10()) {
509 // Current page should exit browser fullscreen mode.
510 ASSERT_NO_FATAL_FAILURE(SendFullscreenShortcutAndWait());
Hzj_jie24cdbc102017-08-11 20:02:24511 ASSERT_FALSE(IsActiveTabFullscreen());
512 ASSERT_FALSE(IsInBrowserFullscreen());
zijiehebb317b92017-07-15 00:01:40513 }
514#else
515 // Current page should exit browser fullscreen mode.
516 ASSERT_NO_FATAL_FAILURE(SendFullscreenShortcutAndWait());
Hzj_jie24cdbc102017-08-11 20:02:24517 ASSERT_FALSE(IsActiveTabFullscreen());
518 ASSERT_FALSE(IsInBrowserFullscreen());
zijiehebb317b92017-07-15 00:01:40519#endif
520
521 ASSERT_NO_FATAL_FAILURE(FinishTestAndVerifyResult());
522}
Hzj_jie24cdbc102017-08-11 20:02:24523
524#if defined(OS_MACOSX)
525// TODO(zijiehe): Figure out why this test crashes on Mac OSX. The suspicious
526// command is "SendFullscreenShortcutAndWait()". See, http://crbug.com/738949.
527#define MAYBE_ShortcutsShouldTakeEffectInBrowserFullscreen \
528 DISABLED_ShortcutsShouldTakeEffectInBrowserFullscreen
529#else
530#define MAYBE_ShortcutsShouldTakeEffectInBrowserFullscreen \
531 ShortcutsShouldTakeEffectInBrowserFullscreen
532#endif
533IN_PROC_BROWSER_TEST_F(BrowserCommandControllerInteractiveTest,
534 MAYBE_ShortcutsShouldTakeEffectInBrowserFullscreen) {
535#if defined(OS_MACOSX)
536 // On 10.9 or earlier, sending the exit fullscreen shortcut will crash the
537 // binary. See http://crbug.com/740250.
538 if (base::mac::IsAtMostOS10_9())
539 return;
540#endif
541 ASSERT_NO_FATAL_FAILURE(SendShortcutsAndExpectNotPrevented(false));
542}
543
544#if !defined(OS_MACOSX)
545// HTML fullscreen is automatically exited after some commands are executed,
546// such as Ctrl + T (new tab). But some commands won't have this effect, such as
547// Ctrl + N (new window).
548// On Mac OSX, AppKit implementation is used for HTML fullscreen mode. Entering
549// and exiting AppKit fullscreen mode triggers an animation. A
550// FullscreenChangeObserver is needed to ensure the animation is finished. But
551// the FullscreenChangeObserver won't finish if the command actually won't cause
552// the page to exit fullscreen mode. So we need to maintain a list of exiting /
553// non-exiting commands, which is not the goal of this test.
Zijie He19852bf52017-08-19 06:56:10554
Zijie He729922fb2017-08-29 23:59:32555#if defined(OS_CHROMEOS)
556// This test is flaky on ChromeOS, see http://crbug.com/754878.
Zijie He19852bf52017-08-19 06:56:10557#define MAYBE_ShortcutsShouldTakeEffectInJsFullscreen \
558 DISABLED_ShortcutsShouldTakeEffectInJsFullscreen
559#else
560#define MAYBE_ShortcutsShouldTakeEffectInJsFullscreen \
561 ShortcutsShouldTakeEffectInJsFullscreen
562#endif
Hzj_jie24cdbc102017-08-11 20:02:24563IN_PROC_BROWSER_TEST_F(BrowserCommandControllerInteractiveTest,
Zijie He19852bf52017-08-19 06:56:10564 MAYBE_ShortcutsShouldTakeEffectInJsFullscreen) {
Zijie He729922fb2017-08-29 23:59:32565 // This test is flaky when browser side navigation is enabled on Linux. See
566 // http://crbug.com/759704.
567 // TODO(zijiehe): Find out the root cause.
568#if defined(OS_LINUX)
569 if (content::IsBrowserSideNavigationEnabled())
570 return;
571#endif
Hzj_jie24cdbc102017-08-11 20:02:24572 ASSERT_NO_FATAL_FAILURE(SendShortcutsAndExpectNotPrevented(true));
573}
Zijie He850df0e2017-08-18 23:24:24574
Hzj_jie24cdbc102017-08-11 20:02:24575#endif