OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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 "base/test/test_timeouts.h" | |
6 #include "chrome/test/automation/automation_proxy.h" | |
7 #include "chrome/test/automation/browser_proxy.h" | |
8 #include "chrome/test/automation/tab_proxy.h" | |
9 #include "chrome/test/automation/window_proxy.h" | |
10 #include "chrome/test/ui/ui_test.h" | |
11 #include "googleurl/src/gurl.h" | |
12 #include "ui/base/events.h" | |
13 #include "ui/base/keycodes/keyboard_codes.h" | |
14 | |
15 // This functionality currently works on Windows and on Linux when | |
16 // toolkit_views is defined (i.e. for Chrome OS). It's not needed | |
17 // on the Mac, and it's not yet implemented on Linux. | |
18 #if defined(TOOLKIT_VIEWS) | |
19 | |
20 namespace { | |
21 | |
22 class KeyboardAccessTest : public UITest { | |
23 public: | |
24 KeyboardAccessTest() { | |
25 dom_automation_enabled_ = true; | |
26 show_window_ = true; | |
27 } | |
28 | |
29 // Use the keyboard to select "New Tab" from the app menu. | |
30 // This test depends on the fact that there is one menu and that | |
31 // New Tab is the first item in the menu. If the menus change, | |
32 // this test will need to be changed to reflect that. | |
33 // | |
34 // If alternate_key_sequence is true, use "Alt" instead of "F10" to | |
35 // open the menu bar, and "Down" instead of "Enter" to open a menu. | |
36 void TestMenuKeyboardAccess(bool alternate_key_sequence, int modifiers); | |
37 | |
38 DISALLOW_COPY_AND_ASSIGN(KeyboardAccessTest); | |
39 }; | |
40 | |
41 void KeyboardAccessTest::TestMenuKeyboardAccess(bool alternate_key_sequence, | |
42 int modifiers) { | |
43 scoped_refptr<BrowserProxy> browser = automation()->GetBrowserWindow(0); | |
44 ASSERT_TRUE(browser.get()); | |
45 scoped_refptr<WindowProxy> window = browser->GetWindow(); | |
46 ASSERT_TRUE(window.get()); | |
47 | |
48 // Navigate to a page in the first tab, which makes sure that focus is | |
49 // set to the browser window. | |
50 scoped_refptr<TabProxy> tab(GetActiveTab()); | |
51 ASSERT_TRUE(tab.get()); | |
52 | |
53 ASSERT_TRUE(tab->NavigateToURL(GURL("about:"))); | |
54 | |
55 // The initial tab index should be 0. | |
56 int tab_index = -1; | |
57 ASSERT_TRUE(browser->GetActiveTabIndex(&tab_index)); | |
58 ASSERT_EQ(0, tab_index); | |
59 | |
60 // Get the focused view ID, then press a key to activate the | |
61 // page menu, then wait until the focused view changes. | |
62 int original_view_id = -1; | |
63 ASSERT_TRUE(window->GetFocusedViewID(&original_view_id)); | |
64 | |
65 ui::KeyboardCode menu_key = | |
66 alternate_key_sequence ? ui::VKEY_MENU : ui::VKEY_F10; | |
67 #if defined(OS_CHROMEOS) | |
68 // Chrome OS has different function key accelerators, so we always use the | |
69 // menu key there. | |
70 menu_key = ui::VKEY_MENU; | |
71 #endif | |
72 ASSERT_TRUE(window->SimulateOSKeyPress(menu_key, modifiers)); | |
73 | |
74 if (modifiers) { | |
75 // Verify Chrome does not move the view focus. We should not move the view | |
76 // focus when typing a menu key with modifier keys, such as shift keys or | |
77 // control keys. | |
78 int new_view_id = -1; | |
79 ASSERT_TRUE(window->GetFocusedViewID(&new_view_id)); | |
80 ASSERT_EQ(original_view_id, new_view_id); | |
81 return; | |
82 } | |
83 | |
84 int new_view_id = -1; | |
85 ASSERT_TRUE(window->WaitForFocusedViewIDToChange( | |
86 original_view_id, &new_view_id)); | |
87 | |
88 ASSERT_TRUE(browser->StartTrackingPopupMenus()); | |
89 | |
90 if (alternate_key_sequence) | |
91 ASSERT_TRUE(window->SimulateOSKeyPress(ui::VKEY_DOWN, 0)); | |
92 else | |
93 ASSERT_TRUE(window->SimulateOSKeyPress(ui::VKEY_RETURN, 0)); | |
94 | |
95 // Wait until the popup menu actually opens. | |
96 ASSERT_TRUE(browser->WaitForPopupMenuToOpen()); | |
97 | |
98 // Press DOWN to select the first item, then RETURN to select it. | |
99 ASSERT_TRUE(window->SimulateOSKeyPress(ui::VKEY_DOWN, 0)); | |
100 ASSERT_TRUE(window->SimulateOSKeyPress(ui::VKEY_RETURN, 0)); | |
101 | |
102 // Wait for the new tab to appear. | |
103 ASSERT_TRUE(browser->WaitForTabCountToBecome(2)); | |
104 | |
105 // Make sure that the new tab index is 1. | |
106 ASSERT_TRUE(browser->GetActiveTabIndex(&tab_index)); | |
107 ASSERT_EQ(1, tab_index); | |
108 } | |
109 | |
110 // Disabled, http://crbug.com/62310. | |
111 TEST_F(KeyboardAccessTest, DISABLED_TestMenuKeyboardAccess) { | |
112 TestMenuKeyboardAccess(false, 0); | |
113 } | |
114 | |
115 // Disabled, http://crbug.com/62310. | |
116 TEST_F(KeyboardAccessTest, DISABLED_TestAltMenuKeyboardAccess) { | |
117 TestMenuKeyboardAccess(true, 0); | |
118 } | |
119 | |
120 // Flaky, http://crbug.com/62311. | |
121 TEST_F(KeyboardAccessTest, FLAKY_TestShiftAltMenuKeyboardAccess) { | |
122 TestMenuKeyboardAccess(true, ui::EF_SHIFT_DOWN); | |
123 } | |
124 | |
125 #if defined(OS_CHROMEOS) | |
126 // TODO(isherman): This test times out on ChromeOS. We should merge it with | |
127 // BrowserKeyEventsTest.ReservedAccelerators, but just disable for now. | |
128 #define MAYBE_ReserveKeyboardAccelerators DISABLED_ReserveKeyboardAccelerators | |
129 #else | |
130 // Flaky, http://crbug.com/62311. | |
131 #define MAYBE_ReserveKeyboardAccelerators FLAKY_ReserveKeyboardAccelerators | |
132 #endif | |
133 | |
134 // Test that JavaScript cannot intercept reserved keyboard accelerators like | |
135 // ctrl-t to open a new tab or ctrl-f4 to close a tab. | |
136 TEST_F(KeyboardAccessTest, MAYBE_ReserveKeyboardAccelerators) { | |
137 const std::string kBadPage = | |
138 "<html><script>" | |
139 "document.onkeydown = function() {" | |
140 " event.preventDefault();" | |
141 " return false;" | |
142 "}" | |
143 "</script></html>"; | |
144 scoped_refptr<BrowserProxy> browser(automation()->GetBrowserWindow(0)); | |
145 ASSERT_TRUE(browser); | |
146 ASSERT_TRUE(browser->AppendTab(GURL("data:text/html," + kBadPage))); | |
147 int tab_count = 0; | |
148 ASSERT_TRUE(browser->GetTabCount(&tab_count)); | |
149 ASSERT_EQ(tab_count, 2); | |
150 | |
151 int active_tab = 0; | |
152 ASSERT_TRUE(browser->GetActiveTabIndex(&active_tab)); | |
153 ASSERT_EQ(active_tab, 1); | |
154 | |
155 scoped_refptr<WindowProxy> window(browser->GetWindow()); | |
156 ASSERT_TRUE(window); | |
157 ASSERT_TRUE(window->SimulateOSKeyPress( | |
158 ui::VKEY_TAB, ui::EF_CONTROL_DOWN)); | |
159 ASSERT_TRUE(browser->WaitForTabToBecomeActive( | |
160 0, TestTimeouts::action_max_timeout_ms())); | |
161 | |
162 #if !defined(OS_MACOSX) // see BrowserWindowCocoa::GetCommandId | |
163 ASSERT_TRUE(browser->ActivateTab(1)); | |
164 ASSERT_TRUE(window->SimulateOSKeyPress(ui::VKEY_F4, ui::EF_CONTROL_DOWN)); | |
165 ASSERT_TRUE(browser->WaitForTabCountToBecome(1)); | |
166 #endif | |
167 } | |
168 | |
169 } // namespace | |
170 | |
171 #endif // defined(TOOLKIT_VIEWS) | |
OLD | NEW |