blob: 7de3a640cbcc4781be3d004bfdb17dd2f95a9e27 [file] [log] [blame]
Avi Drissman4a8573c2022-09-09 19:35:541// Copyright 2015 The Chromium Authors
jackhou06c25a92015-07-30 03:11:182// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#import "chrome/browser/global_keyboard_shortcuts_mac.h"
6
Evan Stade0526f182019-08-14 16:08:347#include <Carbon/Carbon.h>
jackhou06c25a92015-07-30 03:11:188#import <Cocoa/Cocoa.h>
9
10#include "base/run_loop.h"
Tatiana Gornakad05d4a2018-06-13 10:09:5311#include "build/build_config.h"
Elly Fong-Jones1320aeb72017-10-13 20:30:3712#include "chrome/app/chrome_command_ids.h"
jackhou06c25a92015-07-30 03:11:1813#include "chrome/browser/ui/browser.h"
14#include "chrome/browser/ui/browser_commands.h"
15#include "chrome/browser/ui/browser_window.h"
erikchen5a8bff8e2018-06-11 21:12:1716#include "chrome/browser/ui/location_bar/location_bar.h"
jackhou06c25a92015-07-30 03:11:1817#include "chrome/browser/ui/tabs/tab_strip_model.h"
erikchen5a8bff8e2018-06-11 21:12:1718#include "chrome/test/base/in_process_browser_test.h"
Erik Chen8f2f95682018-06-08 00:44:5419#include "chrome/test/base/interactive_test_utils.h"
erikchen5a8bff8e2018-06-11 21:12:1720#include "chrome/test/base/ui_test_utils.h"
21#include "components/omnibox/browser/omnibox_view.h"
Peter Kasting919ce652020-05-07 10:22:3622#include "content/public/test/browser_test.h"
erikchen5a8bff8e2018-06-11 21:12:1723#include "content/public/test/test_navigation_observer.h"
24#include "content/public/test/test_utils.h"
Evan Stade0526f182019-08-14 16:08:3425#include "ui/events/event_constants.h"
Elly Fong-Jonesbe3bb272019-08-21 21:46:0126#include "ui/events/keycodes/keyboard_codes.h"
jackhou06c25a92015-07-30 03:11:1827#import "ui/events/test/cocoa_test_event_utils.h"
28
jackhou06c25a92015-07-30 03:11:1829using cocoa_test_event_utils::SynthesizeKeyEvent;
30
erikchen5a8bff8e2018-06-11 21:12:1731class GlobalKeyboardShortcutsTest : public InProcessBrowserTest {
32 public:
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
38 // test.
39 ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(
40 browser()->window()->GetNativeWindow()));
41 }
42};
jackhou06c25a92015-07-30 03:11:1843
mblsha2394f022016-12-02 12:03:0344namespace {
45
erikchenb706d152018-06-12 23:21:2246void SendEvent(NSEvent* ns_event) {
47 [NSApp sendEvent:ns_event];
mblsha2394f022016-12-02 12:03:0348}
49
50} // namespace
51
jackhou06c25a92015-07-30 03:11:1852IN_PROC_BROWSER_TEST_F(GlobalKeyboardShortcutsTest, SwitchTabsMac) {
Christopher Cameron359c61e22018-10-18 08:06:2053 NSWindow* ns_window =
54 browser()->window()->GetNativeWindow().GetNativeNSWindow();
jackhou06c25a92015-07-30 03:11:1855 TabStripModel* tab_strip = browser()->tab_strip_model();
56
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));
61
62 // Ctrl+Tab goes to the next tab, which loops back to the first tab.
Avi Drissman8be81112022-05-11 01:12:4263 SendEvent(SynthesizeKeyEvent(ns_window, true, ui::VKEY_TAB,
64 NSEventModifierFlagControl));
jackhou06c25a92015-07-30 03:11:1865 EXPECT_TRUE(tab_strip->IsTabSelected(0));
66
67 // Cmd+2 goes to the second tab.
Avi Drissman8be81112022-05-11 01:12:4268 SendEvent(SynthesizeKeyEvent(ns_window, true, ui::VKEY_2,
69 NSEventModifierFlagCommand));
erikchenb706d152018-06-12 23:21:2270
71 // Wait for the tab to activate to be selected.
72 while (true) {
73 if (tab_strip->IsTabSelected(1))
74 break;
75 base::RunLoop().RunUntilIdle();
76 }
jackhou06c25a92015-07-30 03:11:1877 EXPECT_TRUE(tab_strip->IsTabSelected(1));
78
79 // Cmd+{ goes to the previous tab.
Avi Drissman8be81112022-05-11 01:12:4280 SendEvent(SynthesizeKeyEvent(
81 ns_window, true, ui::VKEY_OEM_4,
82 NSEventModifierFlagShift | NSEventModifierFlagCommand));
jackhou06c25a92015-07-30 03:11:1883 EXPECT_TRUE(tab_strip->IsTabSelected(0));
84}
Elly Fong-Jones1320aeb72017-10-13 20:30:3785
erikchen4c9d0842018-06-14 17:04:0886// Test that cmd + left arrow can be used for history navigation.
87IN_PROC_BROWSER_TEST_F(GlobalKeyboardShortcutsTest, HistoryNavigation) {
88 TabStripModel* tab_strip = browser()->tab_strip_model();
Christopher Cameron359c61e22018-10-18 08:06:2089 NSWindow* ns_window =
90 browser()->window()->GetNativeWindow().GetNativeNSWindow();
erikchen4c9d0842018-06-14 17:04:0891
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);
95
96 // Navigate the active tab to a dummy URL.
97 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(
98 browser(), test_url,
99 /*number_of_navigations=*/1);
100 ASSERT_EQ(tab_strip->GetActiveWebContents()->GetLastCommittedURL(), test_url);
101
102 // Focus the WebContents.
103 tab_strip->GetActiveWebContents()->Focus();
104
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,
Avi Drissman8be81112022-05-11 01:12:42108 NSEventModifierFlagCommand));
erikchen4c9d0842018-06-14 17:04:08109 while (true) {
110 if (tab_strip->GetActiveWebContents()->GetLastCommittedURL() != test_url)
111 break;
112 base::RunLoop().RunUntilIdle();
113 }
114 ASSERT_NE(tab_strip->GetActiveWebContents()->GetLastCommittedURL(), test_url);
115}
116
erikchen5a8bff8e2018-06-11 21:12:17117// Test that common hotkeys for editing the omnibox work.
118IN_PROC_BROWSER_TEST_F(GlobalKeyboardShortcutsTest, CopyPasteOmnibox) {
119 BrowserWindow* window = browser()->window();
120 ASSERT_TRUE(window);
121 LocationBar* location_bar = window->GetLocationBar();
122 ASSERT_TRUE(location_bar);
123 OmniboxView* omnibox_view = location_bar->GetOmniboxView();
124 ASSERT_TRUE(omnibox_view);
125
Christopher Cameron359c61e22018-10-18 08:06:20126 NSWindow* ns_window =
127 browser()->window()->GetNativeWindow().GetNativeNSWindow();
erikchen5a8bff8e2018-06-11 21:12:17128
129 // Cmd+L focuses the omnibox and selects all the text.
erikchenb706d152018-06-12 23:21:22130 SendEvent(SynthesizeKeyEvent(ns_window, /*keydown=*/true, ui::VKEY_L,
Avi Drissman8be81112022-05-11 01:12:42131 NSEventModifierFlagCommand));
erikchen5a8bff8e2018-06-11 21:12:17132
133 // The first typed letter overrides the existing contents.
erikchenb706d152018-06-12 23:21:22134 SendEvent(SynthesizeKeyEvent(ns_window, /*keydown=*/true, ui::VKEY_A,
135 /*flags=*/0));
erikchen5a8bff8e2018-06-11 21:12:17136 // The second typed letter just appends.
erikchenb706d152018-06-12 23:21:22137 SendEvent(SynthesizeKeyEvent(ns_window, /*keydown=*/true, ui::VKEY_B,
138 /*flags=*/0));
Jan Wilken Dörrie78e88d82e2021-03-23 15:24:22139 ASSERT_EQ(omnibox_view->GetText(), u"ab");
erikchen5a8bff8e2018-06-11 21:12:17140
141 // Cmd+A selects the contents.
erikchenb706d152018-06-12 23:21:22142 SendEvent(SynthesizeKeyEvent(ns_window, /*keydown=*/true, ui::VKEY_A,
Avi Drissman8be81112022-05-11 01:12:42143 NSEventModifierFlagCommand));
erikchen5a8bff8e2018-06-11 21:12:17144
145 // Cmd+C copies the contents.
erikchenb706d152018-06-12 23:21:22146 SendEvent(SynthesizeKeyEvent(ns_window, /*keydown=*/true, ui::VKEY_C,
Avi Drissman8be81112022-05-11 01:12:42147 NSEventModifierFlagCommand));
erikchen5a8bff8e2018-06-11 21:12:17148
erikchen4c9d0842018-06-14 17:04:08149 // The first typed letter overrides the existing contents.
150 SendEvent(SynthesizeKeyEvent(ns_window, /*keydown=*/true, ui::VKEY_C,
erikchenb706d152018-06-12 23:21:22151 /*flags=*/0));
erikchen4c9d0842018-06-14 17:04:08152 SendEvent(SynthesizeKeyEvent(ns_window, /*keydown=*/true, ui::VKEY_D,
153 /*flags=*/0));
Jan Wilken Dörrie78e88d82e2021-03-23 15:24:22154 ASSERT_EQ(omnibox_view->GetText(), u"cd");
erikchen4c9d0842018-06-14 17:04:08155
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,
Avi Drissman8be81112022-05-11 01:12:42159 NSEventModifierFlagCommand));
erikchen5a8bff8e2018-06-11 21:12:17160
161 // Cmd+V pastes the contents.
erikchenb706d152018-06-12 23:21:22162 SendEvent(SynthesizeKeyEvent(ns_window, /*keydown=*/true, ui::VKEY_V,
Avi Drissman8be81112022-05-11 01:12:42163 NSEventModifierFlagCommand));
Jan Wilken Dörrie78e88d82e2021-03-23 15:24:22164 EXPECT_EQ(omnibox_view->GetText(), u"abcd");
erikchen5a8bff8e2018-06-11 21:12:17165}
166
167// Tests that the shortcut to reopen a previous tab works.
168IN_PROC_BROWSER_TEST_F(GlobalKeyboardShortcutsTest, ReopenPreviousTab) {
Elly Fong-Jones1320aeb72017-10-13 20:30:37169 TabStripModel* tab_strip = browser()->tab_strip_model();
170
erikchen5a8bff8e2018-06-11 21:12:17171 // Set up window with 2 tabs.
172 chrome::NewTab(browser());
173 EXPECT_EQ(2, tab_strip->count());
174
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(
179 browser(), test_url,
180 /*number_of_navigations=*/1);
181 ASSERT_EQ(tab_strip->GetActiveWebContents()->GetLastCommittedURL(), test_url);
182
183 // Close a tab.
Elly Fong-Jonesbe3bb272019-08-21 21:46:01184 ASSERT_TRUE(ui_test_utils::SendKeyPressToWindowSync(
185 browser()->window()->GetNativeWindow(), ui::VKEY_W, false, false, false,
186 true));
erikchen5a8bff8e2018-06-11 21:12:17187 EXPECT_EQ(1, tab_strip->count());
188 ASSERT_NE(tab_strip->GetActiveWebContents()->GetLastCommittedURL(), test_url);
189
190 // Reopen a tab.
Elly Fong-Jonesbe3bb272019-08-21 21:46:01191 ASSERT_TRUE(ui_test_utils::SendKeyPressToWindowSync(
192 browser()->window()->GetNativeWindow(), ui::VKEY_T, false, true, false,
193 true));
erikchen5a8bff8e2018-06-11 21:12:17194 EXPECT_EQ(2, tab_strip->count());
195 ASSERT_EQ(tab_strip->GetActiveWebContents()->GetLastCommittedURL(), test_url);
196}
197
198// Checks that manually configured hotkeys in the main menu have higher priority
199// than unconfigurable hotkeys not present in the main menu.
200IN_PROC_BROWSER_TEST_F(GlobalKeyboardShortcutsTest, MenuCommandPriority) {
Christopher Cameron359c61e22018-10-18 08:06:20201 NSWindow* ns_window =
202 browser()->window()->GetNativeWindow().GetNativeNSWindow();
erikchen5a8bff8e2018-06-11 21:12:17203 TabStripModel* tab_strip = browser()->tab_strip_model();
Erik Chen8f2f95682018-06-08 00:44:54204
Elly Fong-Jones1320aeb72017-10-13 20:30:37205 // 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));
211
212 // Use the cmd-2 hotkey to switch to the second tab.
Avi Drissman8be81112022-05-11 01:12:42213 SendEvent(SynthesizeKeyEvent(ns_window, true, ui::VKEY_2,
214 NSEventModifierFlagCommand));
Elly Fong-Jones1320aeb72017-10-13 20:30:37215 EXPECT_TRUE(tab_strip->IsTabSelected(1));
216
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);
Elly Fong-Jones0d121842019-08-12 22:03:40223 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];
Elly Fong-Jones1320aeb72017-10-13 20:30:37227 ASSERT_NE(nil, next_item);
228 [next_item setKeyEquivalent:@"2"];
Avi Drissman8be81112022-05-11 01:12:42229 [next_item setKeyEquivalentModifierMask:NSEventModifierFlagCommand];
Erik Chen8f2f95682018-06-08 00:44:54230 ASSERT_TRUE([next_item isEnabled]);
Elly Fong-Jones1320aeb72017-10-13 20:30:37231
232 // Send cmd-2 again, and ensure the tab switches.
Avi Drissman8be81112022-05-11 01:12:42233 SendEvent(SynthesizeKeyEvent(ns_window, true, ui::VKEY_2,
234 NSEventModifierFlagCommand));
Elly Fong-Jones1320aeb72017-10-13 20:30:37235 EXPECT_TRUE(tab_strip->IsTabSelected(2));
Avi Drissman8be81112022-05-11 01:12:42236 SendEvent(SynthesizeKeyEvent(ns_window, true, ui::VKEY_2,
237 NSEventModifierFlagCommand));
Elly Fong-Jones1320aeb72017-10-13 20:30:37238 EXPECT_TRUE(tab_strip->IsTabSelected(3));
239}