OLD | NEW |
---|---|
(Empty) | |
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" | |
17 #include "chrome/browser/ui/tabs/tab_strip_model_observer.h" | |
18 #include "chrome/test/base/in_process_browser_test.h" | |
19 #include "chrome/test/base/interactive_test_utils.h" | |
20 #include "content/public/browser/notification_service.h" | |
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 | |
28 #if defined(OS_MACOSX) | |
29 #include "base/mac/mac_util.h" | |
30 #endif | |
31 | |
32 namespace { | |
33 // The html file to receive key events, prevent defaults and export all the | |
34 // events with "getKeyEventReport()" function. It has two magic keys: pressing | |
35 // "S" to enter fullscreen mode; pressing "X" to indicate the end of all the | |
36 // keys. | |
37 constexpr char kFullscreenKeyboardLockHTML[] = "/fullscreen_keyboardlock.html"; | |
38 | |
39 // On MacOSX command key is used for most of the shortcuts, so replace it with | |
40 // control to reduce the complexity of comparison of the results. | |
41 void NormalizeMetaKeyForMacOS(std::string* output) { | |
42 #if defined(OS_MACOSX) | |
43 base::ReplaceSubstringsAfterOffset(output, 0, "MetaLeft", "ControlLeft"); | |
44 #else | |
45 // Avoid unused variable warning. | |
46 output = nullptr; | |
47 #endif | |
48 } | |
49 | |
50 } // namespace | |
51 | |
52 class BrowserCommandControllerInteractiveTest : public InProcessBrowserTest { | |
53 public: | |
54 BrowserCommandControllerInteractiveTest() = default; | |
55 ~BrowserCommandControllerInteractiveTest() override = default; | |
56 | |
57 protected: | |
58 // Starts the test page and waits for it to be loaded. | |
59 void StartTestPage(); | |
60 | |
61 // Wait for the browser to have the expected tab count or timeout. | |
62 void WaitForTabCount(int tab_count) const; | |
63 | |
64 // Sends a control or command + |key| shortcut to the focused window. Shift | |
65 // modifier will be added if |shift| is true. | |
66 void SendShortcut(ui::KeyboardCode key, bool shift = false); | |
67 | |
68 // Sends a control or command + shift + |key| shortcut to the focused window. | |
69 void SendShiftShortcut(ui::KeyboardCode key); | |
70 | |
71 // Sends a fullscreen shortcut to the focused window and wait for the | |
72 // operation to take effect. | |
73 void SendFullscreenShortcutAndWait(); | |
74 | |
75 // Sends a KeyS to the focused window to trigger JavaScript fullscreen and | |
76 // wait for the operation to take effect. | |
77 void SendJsFullscreenShortcutAndWait(); | |
78 | |
79 // Sends an ESC to the focused window. | |
80 void SendEscape(); | |
81 | |
82 // Sends an ESC to the focused window to exit JavaScript fullscreen and wait | |
83 // for the operation to take effect. | |
84 void SendEscapeAndWaitForExitingFullscreen(); | |
85 | |
86 // Sends a set of preventable shortcuts to the web page. | |
87 void SendShortcutsInFullscreen(); | |
88 | |
89 // Sends a magic KeyX to the focused window to stop the test case, receives | |
90 // the result and verifies if it is equal to |expected_result_|. | |
91 void FinishTestAndVerifyResult(); | |
92 | |
93 private: | |
94 void SetUpOnMainThread() override; | |
95 | |
96 std::string expected_result_; | |
97 | |
98 DISALLOW_COPY_AND_ASSIGN(BrowserCommandControllerInteractiveTest); | |
99 }; | |
100 | |
101 void BrowserCommandControllerInteractiveTest::StartTestPage() { | |
102 ASSERT_TRUE(embedded_test_server()->Start()); | |
103 // Ensures the initial states. | |
104 ASSERT_EQ(1, browser()->tab_strip_model()->count()); | |
105 ASSERT_EQ(0, browser()->tab_strip_model()->active_index()); | |
106 ASSERT_EQ(1U, BrowserList::GetInstance()->size()); | |
107 // Add a second tab for counting and focus purposes. | |
108 AddTabAtIndex(1, GURL("about:blank"), ui::PAGE_TRANSITION_LINK); | |
Paweł Hajdan Jr.
2017/07/11 11:59:57
nit: Use url::kAboutBlankURL .
Hzj_jie
2017/07/11 17:47:26
Done.
| |
109 ASSERT_EQ(2, browser()->tab_strip_model()->count()); | |
110 ASSERT_EQ(1U, BrowserList::GetInstance()->size()); | |
111 | |
112 ui_test_utils::NavigateToURLWithDisposition( | |
113 browser(), embedded_test_server()->GetURL(kFullscreenKeyboardLockHTML), | |
114 WindowOpenDisposition::CURRENT_TAB, | |
115 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); | |
116 } | |
117 | |
118 void BrowserCommandControllerInteractiveTest::WaitForTabCount( | |
119 int tab_count) const { | |
120 while (browser()->tab_strip_model()->count() != tab_count) | |
Paweł Hajdan Jr.
2017/07/11 11:59:57
Loops like this make me suspicious. Can we wait fo
Hzj_jie
2017/07/11 17:47:26
In an early iteration, a TabCountObserver has been
| |
121 content::RunAllPendingInMessageLoop(); | |
122 } | |
123 | |
124 void BrowserCommandControllerInteractiveTest::SendShortcut( | |
125 ui::KeyboardCode key, | |
126 bool shift /* = false */) { | |
127 #if defined(OS_MACOSX) | |
128 const bool control_modifier = false; | |
129 const bool command_modifier = true; | |
130 #else | |
131 const bool control_modifier = true; | |
132 const bool command_modifier = false; | |
133 #endif | |
134 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(browser(), key, control_modifier, | |
135 shift, false, command_modifier)); | |
136 | |
137 expected_result_ += ui::KeycodeConverter::DomCodeToCodeString( | |
138 ui::UsLayoutKeyboardCodeToDomCode(key)); | |
139 expected_result_ += " ctrl:"; | |
140 expected_result_ += control_modifier ? "true" : "false"; | |
141 expected_result_ += " shift:"; | |
142 expected_result_ += shift ? "true" : "false"; | |
143 expected_result_ += " alt:false"; | |
144 expected_result_ += " meta:"; | |
145 expected_result_ += command_modifier ? "true" : "false"; | |
146 expected_result_ += '\n'; | |
147 } | |
148 | |
149 void BrowserCommandControllerInteractiveTest::SendShiftShortcut( | |
150 ui::KeyboardCode key) { | |
151 SendShortcut(key, true); | |
152 } | |
153 | |
154 void BrowserCommandControllerInteractiveTest::SendFullscreenShortcutAndWait() { | |
155 content::WindowedNotificationObserver observer( | |
156 chrome::NOTIFICATION_FULLSCREEN_CHANGED, | |
157 content::NotificationService::AllSources()); | |
158 // Enter fullscreen. | |
159 #if defined(OS_MACOSX) | |
160 // On MACOSX, Command + Control + F is used. | |
161 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(browser(), ui::VKEY_F, true, | |
162 false, false, true)); | |
163 #elif defined(OS_CHROMEOS) | |
164 // A dedicated fullscreen key is used on Chrome OS, so send a fullscreen | |
165 // command directly instead, to avoid constructing the key press. | |
166 ASSERT_TRUE(chrome::ExecuteCommand(browser(), IDC_FULLSCREEN)); | |
167 #else | |
168 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(browser(), ui::VKEY_F11, false, | |
169 false, false, false)); | |
170 #endif | |
171 | |
172 observer.Wait(); | |
173 } | |
174 | |
175 void BrowserCommandControllerInteractiveTest:: | |
176 SendJsFullscreenShortcutAndWait() { | |
177 content::WindowedNotificationObserver observer( | |
178 chrome::NOTIFICATION_FULLSCREEN_CHANGED, | |
179 content::NotificationService::AllSources()); | |
180 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(browser(), ui::VKEY_S, false, | |
181 false, false, false)); | |
182 expected_result_ += "KeyS ctrl:false shift:false alt:false meta:false\n"; | |
183 observer.Wait(); | |
184 } | |
185 | |
186 void BrowserCommandControllerInteractiveTest::SendEscape() { | |
187 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(browser(), ui::VKEY_ESCAPE, false, | |
188 false, false, false)); | |
189 expected_result_ += "Escape ctrl:false shift:false alt:false meta:false\n"; | |
190 } | |
191 | |
192 void BrowserCommandControllerInteractiveTest :: | |
193 SendEscapeAndWaitForExitingFullscreen() { | |
194 content::WindowedNotificationObserver observer( | |
195 chrome::NOTIFICATION_FULLSCREEN_CHANGED, | |
196 content::NotificationService::AllSources()); | |
197 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(browser(), ui::VKEY_ESCAPE, false, | |
198 false, false, false)); | |
199 observer.Wait(); | |
200 } | |
201 | |
202 void BrowserCommandControllerInteractiveTest::SendShortcutsInFullscreen() { | |
203 const int initial_active_index = browser()->tab_strip_model()->active_index(); | |
204 const int initial_tab_count = browser()->tab_strip_model()->count(); | |
205 const size_t initial_browser_count = BrowserList::GetInstance()->size(); | |
206 // The tab should not be closed. | |
207 SendShortcut(ui::VKEY_W); | |
208 ASSERT_EQ(initial_tab_count, browser()->tab_strip_model()->count()); | |
209 // The window should not be closed. | |
210 SendShiftShortcut(ui::VKEY_W); | |
211 ASSERT_EQ(initial_browser_count, BrowserList::GetInstance()->size()); | |
212 // TODO(zijiehe): ChromeOS incorrectly handles these; | |
213 // see http://crbug.com/737307. | |
214 #if !defined(OS_CHROMEOS) | |
215 // A new tab should not be created. | |
216 SendShortcut(ui::VKEY_T); | |
217 ASSERT_EQ(initial_tab_count, browser()->tab_strip_model()->count()); | |
218 // A new window should not be created. | |
219 SendShortcut(ui::VKEY_N); | |
220 ASSERT_EQ(initial_browser_count, BrowserList::GetInstance()->size()); | |
221 // A new incognito window should not be created. | |
222 SendShiftShortcut(ui::VKEY_N); | |
223 ASSERT_EQ(initial_browser_count, BrowserList::GetInstance()->size()); | |
224 // Last closed tab should not be restored. | |
225 SendShiftShortcut(ui::VKEY_T); | |
226 ASSERT_EQ(initial_tab_count, browser()->tab_strip_model()->count()); | |
227 #endif | |
228 // Browser should not switch to the next tab. | |
229 SendShortcut(ui::VKEY_TAB); | |
230 ASSERT_EQ(initial_active_index, browser()->tab_strip_model()->active_index()); | |
231 // Browser should not switch to the previous tab. | |
232 SendShiftShortcut(ui::VKEY_TAB); | |
233 ASSERT_EQ(initial_active_index, browser()->tab_strip_model()->active_index()); | |
234 } | |
235 | |
236 void BrowserCommandControllerInteractiveTest::FinishTestAndVerifyResult() { | |
237 // Magic KeyX to stop the test. | |
238 EXPECT_TRUE(ui_test_utils::SendKeyPressSync(browser(), ui::VKEY_X, false, | |
239 false, false, false)); | |
240 expected_result_ += "KeyX ctrl:false shift:false alt:false meta:false"; | |
241 std::string result; | |
242 EXPECT_TRUE(content::ExecuteScriptAndExtractString( | |
243 browser()->tab_strip_model()->GetActiveWebContents()->GetRenderViewHost(), | |
244 "getKeyEventReport();", &result)); | |
245 NormalizeMetaKeyForMacOS(&result); | |
246 NormalizeMetaKeyForMacOS(&expected_result_); | |
247 base::TrimWhitespaceASCII(result, base::TRIM_ALL, &result); | |
248 ASSERT_EQ(expected_result_, result); | |
249 } | |
250 | |
251 void BrowserCommandControllerInteractiveTest::SetUpOnMainThread() { | |
252 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); | |
253 } | |
254 | |
255 IN_PROC_BROWSER_TEST_F(BrowserCommandControllerInteractiveTest, | |
256 ShortcutsShouldTakeEffectInWindowMode) { | |
257 ASSERT_EQ(1, browser()->tab_strip_model()->count()); | |
258 SendShortcut(ui::VKEY_T); | |
259 WaitForTabCount(2); | |
260 ASSERT_EQ(2, browser()->tab_strip_model()->count()); | |
261 SendShortcut(ui::VKEY_T); | |
262 WaitForTabCount(3); | |
263 ASSERT_EQ(3, browser()->tab_strip_model()->count()); | |
264 SendShortcut(ui::VKEY_W); | |
265 WaitForTabCount(2); | |
266 ASSERT_EQ(2, browser()->tab_strip_model()->count()); | |
267 SendShortcut(ui::VKEY_W); | |
268 WaitForTabCount(1); | |
269 ASSERT_EQ(1, browser()->tab_strip_model()->count()); | |
270 SendFullscreenShortcutAndWait(); | |
271 ASSERT_TRUE(browser() | |
272 ->exclusive_access_manager() | |
273 ->fullscreen_controller() | |
274 ->IsFullscreenForBrowser()); | |
275 } | |
276 | |
277 IN_PROC_BROWSER_TEST_F(BrowserCommandControllerInteractiveTest, | |
278 UnpreservedShortcutsShouldBePreventable) { | |
279 StartTestPage(); | |
280 | |
281 // The browser print function should be blocked by the web page. | |
282 SendShortcut(ui::VKEY_P); | |
283 // The system print function should be blocked by the web page. | |
284 SendShiftShortcut(ui::VKEY_P); | |
285 FinishTestAndVerifyResult(); | |
286 } | |
287 | |
288 #if defined(OS_MACOSX) | |
289 // TODO(zijiehe): Figure out why this test crashes on Mac OSX. The suspicious | |
290 // command is "SendFullscreenShortcutAndWait()". See, http://crbug.com/738949. | |
291 #define MAYBE_KeyEventsShouldBeConsumedByWebPageInBrowserFullscreen \ | |
292 DISABLED_KeyEventsShouldBeConsumedByWebPageInBrowserFullscreen | |
293 #else | |
294 #define MAYBE_KeyEventsShouldBeConsumedByWebPageInBrowserFullscreen \ | |
295 KeyEventsShouldBeConsumedByWebPageInBrowserFullscreen | |
296 #endif | |
297 IN_PROC_BROWSER_TEST_F( | |
298 BrowserCommandControllerInteractiveTest, | |
299 MAYBE_KeyEventsShouldBeConsumedByWebPageInBrowserFullscreen) { | |
300 StartTestPage(); | |
301 | |
302 SendFullscreenShortcutAndWait(); | |
303 SendShortcutsInFullscreen(); | |
304 // Current page should not exit browser fullscreen mode. | |
305 SendEscape(); | |
306 | |
307 FinishTestAndVerifyResult(); | |
308 } | |
309 | |
310 IN_PROC_BROWSER_TEST_F( | |
311 BrowserCommandControllerInteractiveTest, | |
312 KeyEventsShouldBeConsumedByWebPageInJsFullscreenExceptForEsc) { | |
313 StartTestPage(); | |
314 | |
315 SendJsFullscreenShortcutAndWait(); | |
316 SendShortcutsInFullscreen(); | |
317 // Current page should exit HTML fullscreen mode. | |
318 SendEscapeAndWaitForExitingFullscreen(); | |
319 | |
320 FinishTestAndVerifyResult(); | |
321 } | |
322 | |
323 IN_PROC_BROWSER_TEST_F( | |
324 BrowserCommandControllerInteractiveTest, | |
325 KeyEventsShouldBeConsumedByWebPageInJsFullscreenExceptForF11) { | |
326 StartTestPage(); | |
327 | |
328 SendJsFullscreenShortcutAndWait(); | |
329 SendShortcutsInFullscreen(); | |
330 #if defined(OS_MACOSX) | |
331 // On 10.9 or earlier, sending the exit fullscreen shortcut will crash the | |
332 // binary. See http://crbug.com/740250. | |
333 if (base::mac::IsAtLeastOS10_10()) { | |
334 // Current page should exit browser fullscreen mode. | |
335 SendFullscreenShortcutAndWait(); | |
336 } | |
337 #else | |
338 // Current page should exit browser fullscreen mode. | |
339 SendFullscreenShortcutAndWait(); | |
340 #endif | |
341 | |
342 FinishTestAndVerifyResult(); | |
343 } | |
OLD | NEW |