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.
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"
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)
22 class KeyboardAccessTest
: public UITest
{
24 KeyboardAccessTest() {
25 dom_automation_enabled_
= true;
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.
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
);
38 DISALLOW_COPY_AND_ASSIGN(KeyboardAccessTest
);
41 void KeyboardAccessTest::TestMenuKeyboardAccess(bool alternate_key_sequence
,
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());
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());
53 ASSERT_TRUE(tab
->NavigateToURL(GURL("about:")));
55 // The initial tab index should be 0.
57 ASSERT_TRUE(browser
->GetActiveTabIndex(&tab_index
));
58 ASSERT_EQ(0, tab_index
);
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
));
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
70 menu_key
= ui::VKEY_MENU
;
72 ASSERT_TRUE(window
->SimulateOSKeyPress(menu_key
, 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
79 ASSERT_TRUE(window
->GetFocusedViewID(&new_view_id
));
80 ASSERT_EQ(original_view_id
, new_view_id
);
85 ASSERT_TRUE(window
->WaitForFocusedViewIDToChange(
86 original_view_id
, &new_view_id
));
88 ASSERT_TRUE(browser
->StartTrackingPopupMenus());
90 if (alternate_key_sequence
)
91 ASSERT_TRUE(window
->SimulateOSKeyPress(ui::VKEY_DOWN
, 0));
93 ASSERT_TRUE(window
->SimulateOSKeyPress(ui::VKEY_RETURN
, 0));
95 // Wait until the popup menu actually opens.
96 ASSERT_TRUE(browser
->WaitForPopupMenuToOpen());
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));
102 // Wait for the new tab to appear.
103 ASSERT_TRUE(browser
->WaitForTabCountToBecome(2));
105 // Make sure that the new tab index is 1.
106 ASSERT_TRUE(browser
->GetActiveTabIndex(&tab_index
));
107 ASSERT_EQ(1, tab_index
);
110 // Disabled, http://crbug.com/62310.
111 TEST_F(KeyboardAccessTest
, DISABLED_TestMenuKeyboardAccess
) {
112 TestMenuKeyboardAccess(false, 0);
115 // Disabled, http://crbug.com/62310.
116 TEST_F(KeyboardAccessTest
, DISABLED_TestAltMenuKeyboardAccess
) {
117 TestMenuKeyboardAccess(true, 0);
120 // Flaky, http://crbug.com/62311.
121 TEST_F(KeyboardAccessTest
, FLAKY_TestShiftAltMenuKeyboardAccess
) {
122 TestMenuKeyboardAccess(true, ui::EF_SHIFT_DOWN
);
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
130 // Flaky, http://crbug.com/62311.
131 #define MAYBE_ReserveKeyboardAccelerators FLAKY_ReserveKeyboardAccelerators
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
=
139 "document.onkeydown = function() {"
140 " event.preventDefault();"
144 scoped_refptr
<BrowserProxy
> browser(automation()->GetBrowserWindow(0));
145 ASSERT_TRUE(browser
);
146 ASSERT_TRUE(browser
->AppendTab(GURL("data:text/html," + kBadPage
)));
148 ASSERT_TRUE(browser
->GetTabCount(&tab_count
));
149 ASSERT_EQ(tab_count
, 2);
152 ASSERT_TRUE(browser
->GetActiveTabIndex(&active_tab
));
153 ASSERT_EQ(active_tab
, 1);
155 scoped_refptr
<WindowProxy
> window(browser
->GetWindow());
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()));
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));
171 #endif // defined(TOOLKIT_VIEWS)