File: global_keyboard_shortcuts_interactive_uitest_mac.mm

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (239 lines) | stat: -rw-r--r-- 9,570 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import "chrome/browser/global_keyboard_shortcuts_mac.h"

#include <Carbon/Carbon.h>
#import <Cocoa/Cocoa.h>

#include "base/run_loop.h"
#include "build/build_config.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/location_bar/location_bar.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/interactive_test_utils.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/omnibox/browser/omnibox_view.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/test_navigation_observer.h"
#include "content/public/test/test_utils.h"
#include "ui/events/event_constants.h"
#include "ui/events/keycodes/keyboard_codes.h"
#import "ui/events/test/cocoa_test_event_utils.h"

using cocoa_test_event_utils::SynthesizeKeyEvent;

class GlobalKeyboardShortcutsTest : public InProcessBrowserTest {
 public:
  GlobalKeyboardShortcutsTest() = default;
  void SetUpOnMainThread() override {
    // Many hotkeys are defined by the main menu. The value of these hotkeys
    // depends on the focused window. We must focus the browser window. This is
    // also why this test must be an interactive_ui_test rather than a browser
    // test.
    ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(
        browser()->window()->GetNativeWindow()));
  }
};

namespace {

void SendEvent(NSEvent* ns_event) {
  [NSApp sendEvent:ns_event];
}

}  // namespace

IN_PROC_BROWSER_TEST_F(GlobalKeyboardShortcutsTest, SwitchTabsMac) {
  NSWindow* ns_window =
      browser()->window()->GetNativeWindow().GetNativeNSWindow();
  TabStripModel* tab_strip = browser()->tab_strip_model();

  // Set up window with 2 tabs.
  chrome::NewTab(browser());
  EXPECT_EQ(2, tab_strip->count());
  EXPECT_TRUE(tab_strip->IsTabSelected(1));

  // Ctrl+Tab goes to the next tab, which loops back to the first tab.
  SendEvent(SynthesizeKeyEvent(ns_window, true, ui::VKEY_TAB,
                               NSEventModifierFlagControl));
  EXPECT_TRUE(tab_strip->IsTabSelected(0));

  // Cmd+2 goes to the second tab.
  SendEvent(SynthesizeKeyEvent(ns_window, true, ui::VKEY_2,
                               NSEventModifierFlagCommand));

  // Wait for the tab to activate to be selected.
  while (true) {
    if (tab_strip->IsTabSelected(1))
      break;
    base::RunLoop().RunUntilIdle();
  }
  EXPECT_TRUE(tab_strip->IsTabSelected(1));

  // Cmd+{ goes to the previous tab.
  SendEvent(SynthesizeKeyEvent(
      ns_window, true, ui::VKEY_OEM_4,
      NSEventModifierFlagShift | NSEventModifierFlagCommand));
  EXPECT_TRUE(tab_strip->IsTabSelected(0));
}

// Test that cmd + left arrow can be used for history navigation.
IN_PROC_BROWSER_TEST_F(GlobalKeyboardShortcutsTest, HistoryNavigation) {
  TabStripModel* tab_strip = browser()->tab_strip_model();
  NSWindow* ns_window =
      browser()->window()->GetNativeWindow().GetNativeNSWindow();

  GURL test_url = ui_test_utils::GetTestUrl(
      base::FilePath(), base::FilePath(FILE_PATH_LITERAL("title1.html")));
  ASSERT_NE(tab_strip->GetActiveWebContents()->GetLastCommittedURL(), test_url);

  // Navigate the active tab to a dummy URL.
  ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(
      browser(), test_url,
      /*number_of_navigations=*/1);
  ASSERT_EQ(tab_strip->GetActiveWebContents()->GetLastCommittedURL(), test_url);

  // Focus the WebContents.
  tab_strip->GetActiveWebContents()->Focus();

  // Cmd + left arrow performs history navigation, but only after the
  // WebContents chooses not to handle the event.
  SendEvent(SynthesizeKeyEvent(ns_window, /*keydown=*/true, ui::VKEY_LEFT,
                               NSEventModifierFlagCommand));
  while (true) {
    if (tab_strip->GetActiveWebContents()->GetLastCommittedURL() != test_url)
      break;
    base::RunLoop().RunUntilIdle();
  }
  ASSERT_NE(tab_strip->GetActiveWebContents()->GetLastCommittedURL(), test_url);
}

// Test that common hotkeys for editing the omnibox work.
IN_PROC_BROWSER_TEST_F(GlobalKeyboardShortcutsTest, CopyPasteOmnibox) {
  BrowserWindow* window = browser()->window();
  ASSERT_TRUE(window);
  LocationBar* location_bar = window->GetLocationBar();
  ASSERT_TRUE(location_bar);
  OmniboxView* omnibox_view = location_bar->GetOmniboxView();
  ASSERT_TRUE(omnibox_view);

  NSWindow* ns_window =
      browser()->window()->GetNativeWindow().GetNativeNSWindow();

  // Cmd+L focuses the omnibox and selects all the text.
  SendEvent(SynthesizeKeyEvent(ns_window, /*keydown=*/true, ui::VKEY_L,
                               NSEventModifierFlagCommand));

  // The first typed letter overrides the existing contents.
  SendEvent(SynthesizeKeyEvent(ns_window, /*keydown=*/true, ui::VKEY_A,
                               /*flags=*/0));
  // The second typed letter just appends.
  SendEvent(SynthesizeKeyEvent(ns_window, /*keydown=*/true, ui::VKEY_B,
                               /*flags=*/0));
  ASSERT_EQ(omnibox_view->GetText(), u"ab");

  // Cmd+A selects the contents.
  SendEvent(SynthesizeKeyEvent(ns_window, /*keydown=*/true, ui::VKEY_A,
                               NSEventModifierFlagCommand));

  // Cmd+C copies the contents.
  SendEvent(SynthesizeKeyEvent(ns_window, /*keydown=*/true, ui::VKEY_C,
                               NSEventModifierFlagCommand));

  // The first typed letter overrides the existing contents.
  SendEvent(SynthesizeKeyEvent(ns_window, /*keydown=*/true, ui::VKEY_C,
                               /*flags=*/0));
  SendEvent(SynthesizeKeyEvent(ns_window, /*keydown=*/true, ui::VKEY_D,
                               /*flags=*/0));
  ASSERT_EQ(omnibox_view->GetText(), u"cd");

  // Cmd + left arrow moves to the beginning. It should not perform history
  // navigation because the firstResponder is not a WebContents..
  SendEvent(SynthesizeKeyEvent(ns_window, /*keydown=*/true, ui::VKEY_LEFT,
                               NSEventModifierFlagCommand));

  // Cmd+V pastes the contents.
  SendEvent(SynthesizeKeyEvent(ns_window, /*keydown=*/true, ui::VKEY_V,
                               NSEventModifierFlagCommand));
  EXPECT_EQ(omnibox_view->GetText(), u"abcd");
}

// Tests that the shortcut to reopen a previous tab works.
IN_PROC_BROWSER_TEST_F(GlobalKeyboardShortcutsTest, ReopenPreviousTab) {
  TabStripModel* tab_strip = browser()->tab_strip_model();

  // Set up window with 2 tabs.
  chrome::NewTab(browser());
  EXPECT_EQ(2, tab_strip->count());

  // Navigate the active tab to a dummy URL.
  GURL test_url = ui_test_utils::GetTestUrl(
      base::FilePath(), base::FilePath(FILE_PATH_LITERAL("title1.html")));
  ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(
      browser(), test_url,
      /*number_of_navigations=*/1);
  ASSERT_EQ(tab_strip->GetActiveWebContents()->GetLastCommittedURL(), test_url);

  // Close a tab.
  ASSERT_TRUE(ui_test_utils::SendKeyPressToWindowSync(
      browser()->window()->GetNativeWindow(), ui::VKEY_W, false, false, false,
      true));
  EXPECT_EQ(1, tab_strip->count());
  ASSERT_NE(tab_strip->GetActiveWebContents()->GetLastCommittedURL(), test_url);

  // Reopen a tab.
  ASSERT_TRUE(ui_test_utils::SendKeyPressToWindowSync(
      browser()->window()->GetNativeWindow(), ui::VKEY_T, false, true, false,
      true));
  EXPECT_EQ(2, tab_strip->count());
  ASSERT_EQ(tab_strip->GetActiveWebContents()->GetLastCommittedURL(), test_url);
}

// Checks that manually configured hotkeys in the main menu have higher priority
// than unconfigurable hotkeys not present in the main menu.
IN_PROC_BROWSER_TEST_F(GlobalKeyboardShortcutsTest, MenuCommandPriority) {
  NSWindow* ns_window =
      browser()->window()->GetNativeWindow().GetNativeNSWindow();
  TabStripModel* tab_strip = browser()->tab_strip_model();

  // Set up window with 4 tabs.
  chrome::NewTab(browser());
  chrome::NewTab(browser());
  chrome::NewTab(browser());
  EXPECT_EQ(4, tab_strip->count());
  EXPECT_TRUE(tab_strip->IsTabSelected(3));

  // Use the cmd-2 hotkey to switch to the second tab.
  SendEvent(SynthesizeKeyEvent(ns_window, true, ui::VKEY_2,
                               NSEventModifierFlagCommand));
  EXPECT_TRUE(tab_strip->IsTabSelected(1));

  // Change the "Select Next Tab" menu item's key equivalent to be cmd-2, to
  // simulate what would happen if there was a user key equivalent for it. Note
  // that there is a readonly "userKeyEquivalent" property on NSMenuItem, but
  // this code can't modify it.
  NSMenu* main_menu = [NSApp mainMenu];
  ASSERT_NE(nil, main_menu);
  NSMenuItem* tab_menu = [main_menu itemWithTitle:@"Tab"];
  ASSERT_NE(nil, tab_menu);
  ASSERT_TRUE(tab_menu.hasSubmenu);
  NSMenuItem* next_item = [tab_menu.submenu itemWithTag:IDC_SELECT_NEXT_TAB];
  ASSERT_NE(nil, next_item);
  [next_item setKeyEquivalent:@"2"];
  [next_item setKeyEquivalentModifierMask:NSEventModifierFlagCommand];
  ASSERT_TRUE([next_item isEnabled]);

  // Send cmd-2 again, and ensure the tab switches.
  SendEvent(SynthesizeKeyEvent(ns_window, true, ui::VKEY_2,
                               NSEventModifierFlagCommand));
  EXPECT_TRUE(tab_strip->IsTabSelected(2));
  SendEvent(SynthesizeKeyEvent(ns_window, true, ui::VKEY_2,
                               NSEventModifierFlagCommand));
  EXPECT_TRUE(tab_strip->IsTabSelected(3));
}