File: accelerators_cocoa_browsertest.mm

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (173 lines) | stat: -rw-r--r-- 6,626 bytes parent folder | download | duplicates (5)
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
// Copyright 2014 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/ui/cocoa/accelerators_cocoa.h"

#import <Cocoa/Cocoa.h>

#include "base/i18n/base_i18n_switches.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/grit/generated_resources.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "content/public/test/browser_test.h"
#include "testing/gtest_mac.h"
#include "ui/base/accelerators/platform_accelerator_cocoa.h"
#include "ui/base/l10n/l10n_util_mac.h"
#import "ui/events/keycodes/keyboard_code_conversion_mac.h"

using AcceleratorsCocoaBrowserTest = InProcessBrowserTest;

namespace {

// Adds all NSMenuItems with an accelerator to the array.
void AddAcceleratorItemsToArray(NSMenu* menu, NSMutableArray* array) {
  for (NSMenuItem* item in menu.itemArray) {
    NSMenu* submenu = item.submenu;
    if (submenu) {
      AddAcceleratorItemsToArray(submenu, array);
    }

    // If the tag or key equivalent is zero, then either this is a macOS menu
    // item that we don't care about, or it's a chrome accelerator with non
    // standard selector. We don't have an easy way to distinguish between
    // these, so we just ignore them. Also as of macOS Monterey the AppKit
    // adds a tag to the Start Dictation... menu item - skip it as well.
    if (item.tag == 0 || item.keyEquivalent.length == 0 ||
        item.action == @selector(startDictation:)) {
      continue;
    }

    [array addObject:item];
  }
}

// Checks that the |item|'s modifier mask matches |modifierMask|. The
// NSEventModifierFlagShift may be stored as part of the key equivalent
// (i.e. "a" + NSEventModifierFlagShift => "A").
inline bool MenuItemHasModifierMask(NSMenuItem* item, NSUInteger modifierMask) {
  return modifierMask == item.keyEquivalentModifierMask ||
         (modifierMask & ~NSEventModifierFlagShift) ==
             item.keyEquivalentModifierMask;
}

// Returns the NSMenuItem that has the given keyEquivalent and modifiers, or
// nil.
NSMenuItem* MenuContainsAccelerator(NSMenu* menu,
                                    NSString* key_equivalent,
                                    NSUInteger modifier_mask) {
  for (NSMenuItem* item in menu.itemArray) {
    NSMenu* submenu = item.submenu;
    if (submenu) {
      NSMenuItem* result =
          MenuContainsAccelerator(submenu, key_equivalent, modifier_mask);
      if (result) {
        return result;
      }
    }

    if ([item.keyEquivalent isEqual:key_equivalent]) {
      // We don't want to ignore shift for [cmd + shift + tab] and [cmd + tab],
      // which are special.
      if (item.tag == IDC_SELECT_NEXT_TAB ||
          item.tag == IDC_SELECT_PREVIOUS_TAB) {
        if (modifier_mask == item.keyEquivalentModifierMask) {
          return item;
        }
        continue;
      }

      if (MenuItemHasModifierMask(item, modifier_mask)) {
        return item;
      }
    }
  }
  return nil;
}

}  // namespace

class AcceleratorsCocoaBrowserTestRTL : public AcceleratorsCocoaBrowserTest {
 public:
  void SetUpCommandLine(base::CommandLine* command_line) override {
    command_line->AppendSwitchASCII(::switches::kForceUIDirection,
                                    ::switches::kForceDirectionRTL);
    command_line->AppendSwitchASCII(::switches::kForceTextDirection,
                                    ::switches::kForceDirectionRTL);
  }
};

// Checks that each NSMenuItem in the main menu has a corresponding accelerator,
// and the keyEquivalent/modifiers match.
IN_PROC_BROWSER_TEST_F(AcceleratorsCocoaBrowserTest,
                       MainMenuAcceleratorsInMapping) {
  NSMenu* menu = [NSApp mainMenu];
  NSMutableArray* array = [NSMutableArray array];
  AddAcceleratorItemsToArray(menu, array);
  AcceleratorsCocoa* keymap = AcceleratorsCocoa::GetInstance();

  for (NSMenuItem* item in array) {
    const ui::Accelerator* accelerator =
        keymap->GetAcceleratorForCommand(item.tag);
    EXPECT_TRUE(accelerator);
    if (!accelerator) {
      continue;
    }

    // Get the Cocoa key_equivalent associated with the accelerator.
    KeyEquivalentAndModifierMask* equivalent =
        GetKeyEquivalentAndModifierMaskFromAccelerator(*accelerator);

    // Check that the menu item's keyEquivalent matches the one from the
    // Cocoa accelerator map.
    EXPECT_NSEQ(equivalent.keyEquivalent, item.keyEquivalent);

    // Check that the menu item's modifier mask matches the one stored in the
    // accelerator. Ignore the NSEventModifierFlagShift because it's part of
    // the key equivalent (i.e. "a" + NSEventModifierFlagShift = "A").
    EXPECT_TRUE(MenuItemHasModifierMask(item, equivalent.modifierMask));
  }
}

// Check that each accelerator with a command_id has an associated NSMenuItem
// in the main menu. If the selector is commandDispatch:, then the tag must
// match the command_id.
IN_PROC_BROWSER_TEST_F(AcceleratorsCocoaBrowserTest,
                       MappingAcceleratorsInMainMenu) {
  AcceleratorsCocoa* keymap = AcceleratorsCocoa::GetInstance();
  // The "Share" menu is dynamically populated.
  NSMenu* mainMenu = NSApp.mainMenu;
  NSMenu* fileMenu = [[mainMenu itemWithTag:IDC_FILE_MENU] submenu];
  NSMenu* shareMenu =
      [[fileMenu itemWithTitle:l10n_util::GetNSString(IDS_SHARE_MAC)] submenu];
  [[shareMenu delegate] menuNeedsUpdate:shareMenu];

  for (auto& it : keymap->accelerators_) {
    KeyEquivalentAndModifierMask* equivalent =
        GetKeyEquivalentAndModifierMaskFromAccelerator(it.second);

    // Check that there exists a corresponding NSMenuItem.
    NSMenuItem* item = MenuContainsAccelerator(
        [NSApp mainMenu], equivalent.keyEquivalent, equivalent.modifierMask);
    EXPECT_TRUE(item);

    // If the menu uses a commandDispatch:, the tag must match the command id!
    // Added an exception for IDC_TOGGLE_FULLSCREEN_TOOLBAR, which conflicts
    // with IDC_PRESENTATION_MODE.
    if (item.action == @selector(commandDispatch:) &&
        item.tag != IDC_TOGGLE_FULLSCREEN_TOOLBAR) {
      EXPECT_EQ(item.tag, it.first);
    }
  }
}

IN_PROC_BROWSER_TEST_F(AcceleratorsCocoaBrowserTestRTL,
                       HistoryAcceleratorsReversedForRTL) {
  AcceleratorsCocoa* keymap = AcceleratorsCocoa::GetInstance();
  ui::Accelerator history_forward = keymap->accelerators_[IDC_FORWARD];
  ui::Accelerator history_back = keymap->accelerators_[IDC_BACK];

  // In LTR, History -> Forward is VKEY_OEM_6 and Back is VKEY_OEM_4.
  EXPECT_EQ(ui::VKEY_OEM_4, history_forward.key_code());
  EXPECT_EQ(ui::VKEY_OEM_6, history_back.key_code());
}