zijiehe | bb317b9 | 2017-07-15 00:01:40 | [diff] [blame] | 1 | // 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" |
zijiehe | bb317b9 | 2017-07-15 00:01:40 | [diff] [blame] | 17 | #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_jie | 24cdbc10 | 2017-08-11 20:02:24 | [diff] [blame] | 20 | #include "content/public/common/browser_side_navigation_policy.h" |
zijiehe | bb317b9 | 2017-07-15 00:01:40 | [diff] [blame] | 21 | #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 | |
| 33 | namespace { |
Hzj_jie | 24cdbc10 | 2017-08-11 20:02:24 | [diff] [blame] | 34 | |
zijiehe | bb317b9 | 2017-07-15 00:01:40 | [diff] [blame] | 35 | // 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). |
| 39 | constexpr 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. |
| 43 | void NormalizeMetaKeyForMacOS(std::string* output) { |
| 44 | #if defined(OS_MACOSX) |
| 45 | base::ReplaceSubstringsAfterOffset(output, 0, "MetaLeft", "ControlLeft"); |
| 46 | #endif |
| 47 | } |
| 48 | |
| 49 | } // namespace |
| 50 | |
| 51 | class BrowserCommandControllerInteractiveTest : public InProcessBrowserTest { |
| 52 | public: |
| 53 | BrowserCommandControllerInteractiveTest() = default; |
| 54 | ~BrowserCommandControllerInteractiveTest() override = default; |
| 55 | |
| 56 | protected: |
Hzj_jie | 24cdbc10 | 2017-08-11 20:02:24 | [diff] [blame] | 57 | // Starts |kFullscreenKeyboardLockHTML| in a new tab and waits for load. |
| 58 | void StartFullscreenLockPage(); |
zijiehe | bb317b9 | 2017-07-15 00:01:40 | [diff] [blame] | 59 | |
| 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_jie | 24cdbc10 | 2017-08-11 20:02:24 | [diff] [blame] | 82 | // 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); |
zijiehe | bb317b9 | 2017-07-15 00:01:40 | [diff] [blame] | 91 | |
| 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_jie | 24cdbc10 | 2017-08-11 20:02:24 | [diff] [blame] | 96 | // 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 | |
zijiehe | bb317b9 | 2017-07-15 00:01:40 | [diff] [blame] | 167 | 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_jie | 24cdbc10 | 2017-08-11 20:02:24 | [diff] [blame] | 177 | void BrowserCommandControllerInteractiveTest::StartFullscreenLockPage() { |
zijiehe | bb317b9 | 2017-07-15 00:01:40 | [diff] [blame] | 178 | // Ensures the initial states. |
Hzj_jie | 24cdbc10 | 2017-08-11 20:02:24 | [diff] [blame] | 179 | ASSERT_EQ(1, GetTabCount()); |
| 180 | ASSERT_EQ(0, GetActiveTabIndex()); |
| 181 | ASSERT_EQ(1U, GetBrowserCount()); |
zijiehe | bb317b9 | 2017-07-15 00:01:40 | [diff] [blame] | 182 | // Add a second tab for counting and focus purposes. |
| 183 | AddTabAtIndex(1, GURL(url::kAboutBlankURL), ui::PAGE_TRANSITION_LINK); |
Hzj_jie | 24cdbc10 | 2017-08-11 20:02:24 | [diff] [blame] | 184 | ASSERT_EQ(2, GetTabCount()); |
| 185 | ASSERT_EQ(1U, GetBrowserCount()); |
zijiehe | bb317b9 | 2017-07-15 00:01:40 | [diff] [blame] | 186 | |
Hzj_jie | 24cdbc10 | 2017-08-11 20:02:24 | [diff] [blame] | 187 | if (!embedded_test_server()->Started()) |
| 188 | ASSERT_TRUE(embedded_test_server()->Start()); |
zijiehe | bb317b9 | 2017-07-15 00:01:40 | [diff] [blame] | 189 | ui_test_utils::NavigateToURLWithDisposition( |
Hzj_jie | 24cdbc10 | 2017-08-11 20:02:24 | [diff] [blame] | 190 | GetActiveBrowser(), |
| 191 | embedded_test_server()->GetURL(kFullscreenKeyboardLockHTML), |
zijiehe | bb317b9 | 2017-07-15 00:01:40 | [diff] [blame] | 192 | WindowOpenDisposition::CURRENT_TAB, |
| 193 | ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
| 194 | } |
| 195 | |
| 196 | void 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_jie | 24cdbc10 | 2017-08-11 20:02:24 | [diff] [blame] | 206 | ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
| 207 | GetActiveBrowser(), key, control_modifier, shift, false, |
| 208 | command_modifier)); |
zijiehe | bb317b9 | 2017-07-15 00:01:40 | [diff] [blame] | 209 | |
| 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 | |
| 222 | void BrowserCommandControllerInteractiveTest::SendShiftShortcut( |
| 223 | ui::KeyboardCode key) { |
| 224 | ASSERT_NO_FATAL_FAILURE(SendShortcut(key, true)); |
| 225 | } |
| 226 | |
| 227 | void 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_jie | 24cdbc10 | 2017-08-11 20:02:24 | [diff] [blame] | 236 | ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
| 237 | GetActiveBrowser(), ui::VKEY_F, true, false, false, true)); |
zijiehe | bb317b9 | 2017-07-15 00:01:40 | [diff] [blame] | 238 | #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_jie | 24cdbc10 | 2017-08-11 20:02:24 | [diff] [blame] | 241 | ASSERT_TRUE(chrome::ExecuteCommand(GetActiveBrowser(), IDC_FULLSCREEN)); |
zijiehe | bb317b9 | 2017-07-15 00:01:40 | [diff] [blame] | 242 | #else |
Hzj_jie | 24cdbc10 | 2017-08-11 20:02:24 | [diff] [blame] | 243 | ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
| 244 | GetActiveBrowser(), ui::VKEY_F11, false, false, false, false)); |
zijiehe | bb317b9 | 2017-07-15 00:01:40 | [diff] [blame] | 245 | #endif |
| 246 | |
| 247 | observer.Wait(); |
| 248 | } |
| 249 | |
| 250 | void BrowserCommandControllerInteractiveTest:: |
| 251 | SendJsFullscreenShortcutAndWait() { |
| 252 | content::WindowedNotificationObserver observer( |
| 253 | chrome::NOTIFICATION_FULLSCREEN_CHANGED, |
| 254 | content::NotificationService::AllSources()); |
Hzj_jie | 24cdbc10 | 2017-08-11 20:02:24 | [diff] [blame] | 255 | ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
| 256 | GetActiveBrowser(), ui::VKEY_S, false, false, false, false)); |
zijiehe | bb317b9 | 2017-07-15 00:01:40 | [diff] [blame] | 257 | expected_result_ += "KeyS ctrl:false shift:false alt:false meta:false\n"; |
| 258 | observer.Wait(); |
Hzj_jie | 24cdbc10 | 2017-08-11 20:02:24 | [diff] [blame] | 259 | ASSERT_TRUE(IsActiveTabFullscreen()); |
zijiehe | bb317b9 | 2017-07-15 00:01:40 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | void BrowserCommandControllerInteractiveTest::SendEscape() { |
Hzj_jie | 24cdbc10 | 2017-08-11 20:02:24 | [diff] [blame] | 263 | ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
| 264 | GetActiveBrowser(), ui::VKEY_ESCAPE, false, false, false, false)); |
zijiehe | bb317b9 | 2017-07-15 00:01:40 | [diff] [blame] | 265 | expected_result_ += "Escape ctrl:false shift:false alt:false meta:false\n"; |
| 266 | } |
| 267 | |
| 268 | void BrowserCommandControllerInteractiveTest :: |
| 269 | SendEscapeAndWaitForExitingFullscreen() { |
| 270 | content::WindowedNotificationObserver observer( |
| 271 | chrome::NOTIFICATION_FULLSCREEN_CHANGED, |
| 272 | content::NotificationService::AllSources()); |
Hzj_jie | 24cdbc10 | 2017-08-11 20:02:24 | [diff] [blame] | 273 | ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
| 274 | GetActiveBrowser(), ui::VKEY_ESCAPE, false, false, false, false)); |
zijiehe | bb317b9 | 2017-07-15 00:01:40 | [diff] [blame] | 275 | observer.Wait(); |
Hzj_jie | 24cdbc10 | 2017-08-11 20:02:24 | [diff] [blame] | 276 | ASSERT_FALSE(IsActiveTabFullscreen()); |
zijiehe | bb317b9 | 2017-07-15 00:01:40 | [diff] [blame] | 277 | } |
| 278 | |
Hzj_jie | 24cdbc10 | 2017-08-11 20:02:24 | [diff] [blame] | 279 | void 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(); |
zijiehe | bb317b9 | 2017-07-15 00:01:40 | [diff] [blame] | 284 | // The tab should not be closed. |
| 285 | ASSERT_NO_FATAL_FAILURE(SendShortcut(ui::VKEY_W)); |
Hzj_jie | 24cdbc10 | 2017-08-11 20:02:24 | [diff] [blame] | 286 | ASSERT_EQ(initial_tab_count, GetTabCount()); |
zijiehe | bb317b9 | 2017-07-15 00:01:40 | [diff] [blame] | 287 | // The window should not be closed. |
| 288 | ASSERT_NO_FATAL_FAILURE(SendShiftShortcut(ui::VKEY_W)); |
Hzj_jie | 24cdbc10 | 2017-08-11 20:02:24 | [diff] [blame] | 289 | ASSERT_EQ(initial_browser_count, GetBrowserCount()); |
zijiehe | bb317b9 | 2017-07-15 00:01:40 | [diff] [blame] | 290 | // A new tab should not be created. |
| 291 | ASSERT_NO_FATAL_FAILURE(SendShortcut(ui::VKEY_T)); |
Hzj_jie | 24cdbc10 | 2017-08-11 20:02:24 | [diff] [blame] | 292 | ASSERT_EQ(initial_tab_count, GetTabCount()); |
zijiehe | bb317b9 | 2017-07-15 00:01:40 | [diff] [blame] | 293 | // A new window should not be created. |
| 294 | ASSERT_NO_FATAL_FAILURE(SendShortcut(ui::VKEY_N)); |
Hzj_jie | 24cdbc10 | 2017-08-11 20:02:24 | [diff] [blame] | 295 | ASSERT_EQ(initial_browser_count, GetBrowserCount()); |
zijiehe | bb317b9 | 2017-07-15 00:01:40 | [diff] [blame] | 296 | // A new incognito window should not be created. |
| 297 | ASSERT_NO_FATAL_FAILURE(SendShiftShortcut(ui::VKEY_N)); |
Hzj_jie | 24cdbc10 | 2017-08-11 20:02:24 | [diff] [blame] | 298 | ASSERT_EQ(initial_browser_count, GetBrowserCount()); |
zijiehe | bb317b9 | 2017-07-15 00:01:40 | [diff] [blame] | 299 | // Last closed tab should not be restored. |
| 300 | ASSERT_NO_FATAL_FAILURE(SendShiftShortcut(ui::VKEY_T)); |
Hzj_jie | 24cdbc10 | 2017-08-11 20:02:24 | [diff] [blame] | 301 | ASSERT_EQ(initial_tab_count, GetTabCount()); |
zijiehe | bb317b9 | 2017-07-15 00:01:40 | [diff] [blame] | 302 | // Browser should not switch to the next tab. |
| 303 | ASSERT_NO_FATAL_FAILURE(SendShortcut(ui::VKEY_TAB)); |
Hzj_jie | 24cdbc10 | 2017-08-11 20:02:24 | [diff] [blame] | 304 | ASSERT_EQ(initial_active_index, GetActiveTabIndex()); |
zijiehe | bb317b9 | 2017-07-15 00:01:40 | [diff] [blame] | 305 | // Browser should not switch to the previous tab. |
| 306 | ASSERT_NO_FATAL_FAILURE(SendShiftShortcut(ui::VKEY_TAB)); |
Hzj_jie | 24cdbc10 | 2017-08-11 20:02:24 | [diff] [blame] | 307 | ASSERT_EQ(initial_active_index, GetActiveTabIndex()); |
| 308 | } |
| 309 | |
| 310 | void 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 He | 850df0e | 2017-08-18 23:24:24 | [diff] [blame] | 320 | static const std::string page = |
| 321 | "<html><head></head><body></body><script>" |
Hzj_jie | 24cdbc10 | 2017-08-11 20:02:24 | [diff] [blame] | 322 | "document.addEventListener('keydown', " |
Zijie He | 850df0e | 2017-08-18 23:24:24 | [diff] [blame] | 323 | " (e) => {" |
| 324 | " if (e.code == 'KeyS') { " |
| 325 | " document.body.webkitRequestFullscreen();" |
| 326 | " }" |
| 327 | " });" |
| 328 | "</script></html>"; |
Hzj_jie | 24cdbc10 | 2017-08-11 20:02:24 | [diff] [blame] | 329 | 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_jie | 24cdbc10 | 2017-08-11 20:02:24 | [diff] [blame] | 334 | ASSERT_NO_FATAL_FAILURE(this->SendJsFullscreenShortcutAndWait()); |
Hzj_jie | 24cdbc10 | 2017-08-11 20:02:24 | [diff] [blame] | 335 | } |
| 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()); |
zijiehe | bb317b9 | 2017-07-15 00:01:40 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | void 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_jie | 24cdbc10 | 2017-08-11 20:02:24 | [diff] [blame] | 413 | EXPECT_TRUE(ui_test_utils::SendKeyPressSync( |
| 414 | GetActiveBrowser(), ui::VKEY_X, false, false, false, false)); |
zijiehe | bb317b9 | 2017-07-15 00:01:40 | [diff] [blame] | 415 | expected_result_ += "KeyX ctrl:false shift:false alt:false meta:false"; |
| 416 | std::string result; |
| 417 | EXPECT_TRUE(content::ExecuteScriptAndExtractString( |
Hzj_jie | 24cdbc10 | 2017-08-11 20:02:24 | [diff] [blame] | 418 | GetActiveWebContents()->GetRenderViewHost(), |
zijiehe | bb317b9 | 2017-07-15 00:01:40 | [diff] [blame] | 419 | "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 | |
| 426 | void BrowserCommandControllerInteractiveTest::SetUpOnMainThread() { |
Hzj_jie | 24cdbc10 | 2017-08-11 20:02:24 | [diff] [blame] | 427 | ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(GetActiveBrowser())); |
zijiehe | bb317b9 | 2017-07-15 00:01:40 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | IN_PROC_BROWSER_TEST_F(BrowserCommandControllerInteractiveTest, |
| 431 | ShortcutsShouldTakeEffectInWindowMode) { |
Hzj_jie | 24cdbc10 | 2017-08-11 20:02:24 | [diff] [blame] | 432 | ASSERT_EQ(1, GetTabCount()); |
zijiehe | bb317b9 | 2017-07-15 00:01:40 | [diff] [blame] | 433 | ASSERT_NO_FATAL_FAILURE(SendShortcut(ui::VKEY_T)); |
Hzj_jie | 24cdbc10 | 2017-08-11 20:02:24 | [diff] [blame] | 434 | ASSERT_EQ(2, GetTabCount()); |
zijiehe | bb317b9 | 2017-07-15 00:01:40 | [diff] [blame] | 435 | ASSERT_NO_FATAL_FAILURE(SendShortcut(ui::VKEY_T)); |
Hzj_jie | 24cdbc10 | 2017-08-11 20:02:24 | [diff] [blame] | 436 | ASSERT_EQ(3, GetTabCount()); |
zijiehe | bb317b9 | 2017-07-15 00:01:40 | [diff] [blame] | 437 | ASSERT_NO_FATAL_FAILURE(SendShortcut(ui::VKEY_W)); |
Hzj_jie | 24cdbc10 | 2017-08-11 20:02:24 | [diff] [blame] | 438 | ASSERT_EQ(2, GetTabCount()); |
zijiehe | bb317b9 | 2017-07-15 00:01:40 | [diff] [blame] | 439 | ASSERT_NO_FATAL_FAILURE(SendShortcut(ui::VKEY_W)); |
Hzj_jie | 24cdbc10 | 2017-08-11 20:02:24 | [diff] [blame] | 440 | ASSERT_EQ(1, GetTabCount()); |
zijiehe | bb317b9 | 2017-07-15 00:01:40 | [diff] [blame] | 441 | ASSERT_NO_FATAL_FAILURE(SendFullscreenShortcutAndWait()); |
Hzj_jie | 24cdbc10 | 2017-08-11 20:02:24 | [diff] [blame] | 442 | ASSERT_TRUE(IsInBrowserFullscreen()); |
| 443 | ASSERT_FALSE(IsActiveTabFullscreen()); |
zijiehe | bb317b9 | 2017-07-15 00:01:40 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | IN_PROC_BROWSER_TEST_F(BrowserCommandControllerInteractiveTest, |
| 447 | UnpreservedShortcutsShouldBePreventable) { |
Hzj_jie | 24cdbc10 | 2017-08-11 20:02:24 | [diff] [blame] | 448 | ASSERT_NO_FATAL_FAILURE(StartFullscreenLockPage()); |
zijiehe | bb317b9 | 2017-07-15 00:01:40 | [diff] [blame] | 449 | |
| 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 |
| 466 | IN_PROC_BROWSER_TEST_F( |
| 467 | BrowserCommandControllerInteractiveTest, |
| 468 | MAYBE_KeyEventsShouldBeConsumedByWebPageInBrowserFullscreen) { |
Hzj_jie | 24cdbc10 | 2017-08-11 20:02:24 | [diff] [blame] | 469 | ASSERT_NO_FATAL_FAILURE(StartFullscreenLockPage()); |
zijiehe | bb317b9 | 2017-07-15 00:01:40 | [diff] [blame] | 470 | |
| 471 | ASSERT_NO_FATAL_FAILURE(SendFullscreenShortcutAndWait()); |
Hzj_jie | 24cdbc10 | 2017-08-11 20:02:24 | [diff] [blame] | 472 | ASSERT_FALSE(IsActiveTabFullscreen()); |
| 473 | ASSERT_TRUE(IsInBrowserFullscreen()); |
| 474 | ASSERT_NO_FATAL_FAILURE(SendShortcutsAndExpectPrevented()); |
zijiehe | bb317b9 | 2017-07-15 00:01:40 | [diff] [blame] | 475 | // Current page should not exit browser fullscreen mode. |
| 476 | ASSERT_NO_FATAL_FAILURE(SendEscape()); |
| 477 | |
| 478 | ASSERT_NO_FATAL_FAILURE(FinishTestAndVerifyResult()); |
Hzj_jie | 24cdbc10 | 2017-08-11 20:02:24 | [diff] [blame] | 479 | |
| 480 | ASSERT_NO_FATAL_FAILURE(SendFullscreenShortcutAndWait()); |
| 481 | ASSERT_FALSE(IsActiveTabFullscreen()); |
| 482 | ASSERT_FALSE(IsInBrowserFullscreen()); |
zijiehe | bb317b9 | 2017-07-15 00:01:40 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | IN_PROC_BROWSER_TEST_F( |
| 486 | BrowserCommandControllerInteractiveTest, |
| 487 | KeyEventsShouldBeConsumedByWebPageInJsFullscreenExceptForEsc) { |
Hzj_jie | 24cdbc10 | 2017-08-11 20:02:24 | [diff] [blame] | 488 | ASSERT_NO_FATAL_FAILURE(StartFullscreenLockPage()); |
zijiehe | bb317b9 | 2017-07-15 00:01:40 | [diff] [blame] | 489 | |
| 490 | ASSERT_NO_FATAL_FAILURE(SendJsFullscreenShortcutAndWait()); |
Hzj_jie | 24cdbc10 | 2017-08-11 20:02:24 | [diff] [blame] | 491 | ASSERT_NO_FATAL_FAILURE(SendShortcutsAndExpectPrevented()); |
zijiehe | bb317b9 | 2017-07-15 00:01:40 | [diff] [blame] | 492 | // Current page should exit HTML fullscreen mode. |
| 493 | ASSERT_NO_FATAL_FAILURE(SendEscapeAndWaitForExitingFullscreen()); |
| 494 | |
| 495 | ASSERT_NO_FATAL_FAILURE(FinishTestAndVerifyResult()); |
| 496 | } |
| 497 | |
| 498 | IN_PROC_BROWSER_TEST_F( |
| 499 | BrowserCommandControllerInteractiveTest, |
| 500 | KeyEventsShouldBeConsumedByWebPageInJsFullscreenExceptForF11) { |
Hzj_jie | 24cdbc10 | 2017-08-11 20:02:24 | [diff] [blame] | 501 | ASSERT_NO_FATAL_FAILURE(StartFullscreenLockPage()); |
zijiehe | bb317b9 | 2017-07-15 00:01:40 | [diff] [blame] | 502 | |
| 503 | ASSERT_NO_FATAL_FAILURE(SendJsFullscreenShortcutAndWait()); |
Hzj_jie | 24cdbc10 | 2017-08-11 20:02:24 | [diff] [blame] | 504 | ASSERT_NO_FATAL_FAILURE(SendShortcutsAndExpectPrevented()); |
zijiehe | bb317b9 | 2017-07-15 00:01:40 | [diff] [blame] | 505 | #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_jie | 24cdbc10 | 2017-08-11 20:02:24 | [diff] [blame] | 511 | ASSERT_FALSE(IsActiveTabFullscreen()); |
| 512 | ASSERT_FALSE(IsInBrowserFullscreen()); |
zijiehe | bb317b9 | 2017-07-15 00:01:40 | [diff] [blame] | 513 | } |
| 514 | #else |
| 515 | // Current page should exit browser fullscreen mode. |
| 516 | ASSERT_NO_FATAL_FAILURE(SendFullscreenShortcutAndWait()); |
Hzj_jie | 24cdbc10 | 2017-08-11 20:02:24 | [diff] [blame] | 517 | ASSERT_FALSE(IsActiveTabFullscreen()); |
| 518 | ASSERT_FALSE(IsInBrowserFullscreen()); |
zijiehe | bb317b9 | 2017-07-15 00:01:40 | [diff] [blame] | 519 | #endif |
| 520 | |
| 521 | ASSERT_NO_FATAL_FAILURE(FinishTestAndVerifyResult()); |
| 522 | } |
Hzj_jie | 24cdbc10 | 2017-08-11 20:02:24 | [diff] [blame] | 523 | |
| 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 |
| 533 | IN_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 He | 19852bf5 | 2017-08-19 06:56:10 | [diff] [blame] | 554 | |
Zijie He | 729922fb | 2017-08-29 23:59:32 | [diff] [blame^] | 555 | #if defined(OS_CHROMEOS) |
| 556 | // This test is flaky on ChromeOS, see http://crbug.com/754878. |
Zijie He | 19852bf5 | 2017-08-19 06:56:10 | [diff] [blame] | 557 | #define MAYBE_ShortcutsShouldTakeEffectInJsFullscreen \ |
| 558 | DISABLED_ShortcutsShouldTakeEffectInJsFullscreen |
| 559 | #else |
| 560 | #define MAYBE_ShortcutsShouldTakeEffectInJsFullscreen \ |
| 561 | ShortcutsShouldTakeEffectInJsFullscreen |
| 562 | #endif |
Hzj_jie | 24cdbc10 | 2017-08-11 20:02:24 | [diff] [blame] | 563 | IN_PROC_BROWSER_TEST_F(BrowserCommandControllerInteractiveTest, |
Zijie He | 19852bf5 | 2017-08-19 06:56:10 | [diff] [blame] | 564 | MAYBE_ShortcutsShouldTakeEffectInJsFullscreen) { |
Zijie He | 729922fb | 2017-08-29 23:59:32 | [diff] [blame^] | 565 | // 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_jie | 24cdbc10 | 2017-08-11 20:02:24 | [diff] [blame] | 572 | ASSERT_NO_FATAL_FAILURE(SendShortcutsAndExpectNotPrevented(true)); |
| 573 | } |
Zijie He | 850df0e | 2017-08-18 23:24:24 | [diff] [blame] | 574 | |
Hzj_jie | 24cdbc10 | 2017-08-11 20:02:24 | [diff] [blame] | 575 | #endif |