1 // Copyright 2015 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #import "chrome/browser/global_keyboard_shortcuts_mac.h"
7 #include <Carbon/Carbon.h>
8 #import <Cocoa/Cocoa.h>
10 #include "base/run_loop.h"
11 #include "build/build_config.h"
12 #include "chrome/app/chrome_command_ids.h"
13 #include "chrome/browser/ui/browser.h"
14 #include "chrome/browser/ui/browser_commands.h"
15 #include "chrome/browser/ui/browser_window.h"
16 #include "chrome/browser/ui/location_bar/location_bar.h"
17 #include "chrome/browser/ui/tabs/tab_strip_model.h"
18 #include "chrome/test/base/in_process_browser_test.h"
19 #include "chrome/test/base/interactive_test_utils.h"
20 #include "chrome/test/base/ui_test_utils.h"
21 #include "components/omnibox/browser/omnibox_view.h"
22 #include "content/public/test/browser_test.h"
23 #include "content/public/test/test_navigation_observer.h"
24 #include "content/public/test/test_utils.h"
25 #include "ui/events/event_constants.h"
26 #include "ui/events/keycodes/keyboard_codes.h"
27 #import "ui/events/test/cocoa_test_event_utils.h"
29 using cocoa_test_event_utils::SynthesizeKeyEvent;
31 class GlobalKeyboardShortcutsTest : public InProcessBrowserTest {
33 GlobalKeyboardShortcutsTest() = default;
34 void SetUpOnMainThread() override {
35 // Many hotkeys are defined by the main menu. The value of these hotkeys
36 // depends on the focused window. We must focus the browser window. This is
37 // also why this test must be an interactive_ui_test rather than a browser
39 ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(
40 browser()->window()->GetNativeWindow()));
46 void SendEvent(NSEvent* ns_event) {
47 [NSApp sendEvent:ns_event];
52 IN_PROC_BROWSER_TEST_F(GlobalKeyboardShortcutsTest, SwitchTabsMac) {
54 browser()->window()->GetNativeWindow().GetNativeNSWindow();
55 TabStripModel* tab_strip = browser()->tab_strip_model();
57 // Set up window with 2 tabs.
58 chrome::NewTab(browser());
59 EXPECT_EQ(2, tab_strip->count());
60 EXPECT_TRUE(tab_strip->IsTabSelected(1));
62 // Ctrl+Tab goes to the next tab, which loops back to the first tab.
63 SendEvent(SynthesizeKeyEvent(ns_window, true, ui::VKEY_TAB,
64 NSEventModifierFlagControl));
65 EXPECT_TRUE(tab_strip->IsTabSelected(0));
67 // Cmd+2 goes to the second tab.
68 SendEvent(SynthesizeKeyEvent(ns_window, true, ui::VKEY_2,
69 NSEventModifierFlagCommand));
71 // Wait for the tab to activate to be selected.
73 if (tab_strip->IsTabSelected(1))
75 base::RunLoop().RunUntilIdle();
77 EXPECT_TRUE(tab_strip->IsTabSelected(1));
79 // Cmd+{ goes to the previous tab.
80 SendEvent(SynthesizeKeyEvent(
81 ns_window, true, ui::VKEY_OEM_4,
82 NSEventModifierFlagShift | NSEventModifierFlagCommand));
83 EXPECT_TRUE(tab_strip->IsTabSelected(0));
86 // Test that cmd + left arrow can be used for history navigation.
87 IN_PROC_BROWSER_TEST_F(GlobalKeyboardShortcutsTest, HistoryNavigation) {
88 TabStripModel* tab_strip = browser()->tab_strip_model();
90 browser()->window()->GetNativeWindow().GetNativeNSWindow();
92 GURL test_url = ui_test_utils::GetTestUrl(
93 base::FilePath(), base::FilePath(FILE_PATH_LITERAL("title1.html")));
94 ASSERT_NE(tab_strip->GetActiveWebContents()->GetLastCommittedURL(), test_url);
96 // Navigate the active tab to a dummy URL.
97 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(
99 /*number_of_navigations=*/1);
100 ASSERT_EQ(tab_strip->GetActiveWebContents()->GetLastCommittedURL(), test_url);
102 // Focus the WebContents.
103 tab_strip->GetActiveWebContents()->Focus();
105 // Cmd + left arrow performs history navigation, but only after the
106 // WebContents chooses not to handle the event.
107 SendEvent(SynthesizeKeyEvent(ns_window, /*keydown=*/true, ui::VKEY_LEFT,
108 NSEventModifierFlagCommand));
110 if (tab_strip->GetActiveWebContents()->GetLastCommittedURL() != test_url)
112 base::RunLoop().RunUntilIdle();
114 ASSERT_NE(tab_strip->GetActiveWebContents()->GetLastCommittedURL(), test_url);
117 // Test that common hotkeys for editing the omnibox work.
118 IN_PROC_BROWSER_TEST_F(GlobalKeyboardShortcutsTest, CopyPasteOmnibox) {
119 BrowserWindow* window = browser()->window();
121 LocationBar* location_bar = window->GetLocationBar();
122 ASSERT_TRUE(location_bar);
123 OmniboxView* omnibox_view = location_bar->GetOmniboxView();
124 ASSERT_TRUE(omnibox_view);
126 NSWindow* ns_window =
127 browser()->window()->GetNativeWindow().GetNativeNSWindow();
129 // Cmd+L focuses the omnibox and selects all the text.
130 SendEvent(SynthesizeKeyEvent(ns_window, /*keydown=*/true, ui::VKEY_L,
131 NSEventModifierFlagCommand));
133 // The first typed letter overrides the existing contents.
134 SendEvent(SynthesizeKeyEvent(ns_window, /*keydown=*/true, ui::VKEY_A,
136 // The second typed letter just appends.
137 SendEvent(SynthesizeKeyEvent(ns_window, /*keydown=*/true, ui::VKEY_B,
139 ASSERT_EQ(omnibox_view->GetText(), u"ab");
141 // Cmd+A selects the contents.
142 SendEvent(SynthesizeKeyEvent(ns_window, /*keydown=*/true, ui::VKEY_A,
143 NSEventModifierFlagCommand));
145 // Cmd+C copies the contents.
146 SendEvent(SynthesizeKeyEvent(ns_window, /*keydown=*/true, ui::VKEY_C,
147 NSEventModifierFlagCommand));
149 // The first typed letter overrides the existing contents.
150 SendEvent(SynthesizeKeyEvent(ns_window, /*keydown=*/true, ui::VKEY_C,
152 SendEvent(SynthesizeKeyEvent(ns_window, /*keydown=*/true, ui::VKEY_D,
154 ASSERT_EQ(omnibox_view->GetText(), u"cd");
156 // Cmd + left arrow moves to the beginning. It should not perform history
157 // navigation because the firstResponder is not a WebContents..
158 SendEvent(SynthesizeKeyEvent(ns_window, /*keydown=*/true, ui::VKEY_LEFT,
159 NSEventModifierFlagCommand));
161 // Cmd+V pastes the contents.
162 SendEvent(SynthesizeKeyEvent(ns_window, /*keydown=*/true, ui::VKEY_V,
163 NSEventModifierFlagCommand));
164 EXPECT_EQ(omnibox_view->GetText(), u"abcd");
167 // Tests that the shortcut to reopen a previous tab works.
168 IN_PROC_BROWSER_TEST_F(GlobalKeyboardShortcutsTest, ReopenPreviousTab) {
169 TabStripModel* tab_strip = browser()->tab_strip_model();
171 // Set up window with 2 tabs.
172 chrome::NewTab(browser());
173 EXPECT_EQ(2, tab_strip->count());
175 // Navigate the active tab to a dummy URL.
176 GURL test_url = ui_test_utils::GetTestUrl(
177 base::FilePath(), base::FilePath(FILE_PATH_LITERAL("title1.html")));
178 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(
180 /*number_of_navigations=*/1);
181 ASSERT_EQ(tab_strip->GetActiveWebContents()->GetLastCommittedURL(), test_url);
184 ASSERT_TRUE(ui_test_utils::SendKeyPressToWindowSync(
185 browser()->window()->GetNativeWindow(), ui::VKEY_W, false, false, false,
187 EXPECT_EQ(1, tab_strip->count());
188 ASSERT_NE(tab_strip->GetActiveWebContents()->GetLastCommittedURL(), test_url);
191 ASSERT_TRUE(ui_test_utils::SendKeyPressToWindowSync(
192 browser()->window()->GetNativeWindow(), ui::VKEY_T, false, true, false,
194 EXPECT_EQ(2, tab_strip->count());
195 ASSERT_EQ(tab_strip->GetActiveWebContents()->GetLastCommittedURL(), test_url);
198 // Checks that manually configured hotkeys in the main menu have higher priority
199 // than unconfigurable hotkeys not present in the main menu.
200 IN_PROC_BROWSER_TEST_F(GlobalKeyboardShortcutsTest, MenuCommandPriority) {
201 NSWindow* ns_window =
202 browser()->window()->GetNativeWindow().GetNativeNSWindow();
203 TabStripModel* tab_strip = browser()->tab_strip_model();
205 // Set up window with 4 tabs.
206 chrome::NewTab(browser());
207 chrome::NewTab(browser());
208 chrome::NewTab(browser());
209 EXPECT_EQ(4, tab_strip->count());
210 EXPECT_TRUE(tab_strip->IsTabSelected(3));
212 // Use the cmd-2 hotkey to switch to the second tab.
213 SendEvent(SynthesizeKeyEvent(ns_window, true, ui::VKEY_2,
214 NSEventModifierFlagCommand));
215 EXPECT_TRUE(tab_strip->IsTabSelected(1));
217 // Change the "Select Next Tab" menu item's key equivalent to be cmd-2, to
218 // simulate what would happen if there was a user key equivalent for it. Note
219 // that there is a readonly "userKeyEquivalent" property on NSMenuItem, but
220 // this code can't modify it.
221 NSMenu* main_menu = [NSApp mainMenu];
222 ASSERT_NE(nil, main_menu);
223 NSMenuItem* tab_menu = [main_menu itemWithTitle:@"Tab"];
224 ASSERT_NE(nil, tab_menu);
225 ASSERT_TRUE(tab_menu.hasSubmenu);
226 NSMenuItem* next_item = [tab_menu.submenu itemWithTag:IDC_SELECT_NEXT_TAB];
227 ASSERT_NE(nil, next_item);
228 [next_item setKeyEquivalent:@"2"];
229 [next_item setKeyEquivalentModifierMask:NSEventModifierFlagCommand];
230 ASSERT_TRUE([next_item isEnabled]);
232 // Send cmd-2 again, and ensure the tab switches.
233 SendEvent(SynthesizeKeyEvent(ns_window, true, ui::VKEY_2,
234 NSEventModifierFlagCommand));
235 EXPECT_TRUE(tab_strip->IsTabSelected(2));
236 SendEvent(SynthesizeKeyEvent(ns_window, true, ui::VKEY_2,
237 NSEventModifierFlagCommand));
238 EXPECT_TRUE(tab_strip->IsTabSelected(3));