[M85 Dev][EFL] Fix errors to generate ninja files
[platform/framework/web/chromium-efl.git] / chrome / browser / global_keyboard_shortcuts_mac_unittest.mm
1 // Copyright (c) 2009 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.
4
5 #include <AppKit/NSEvent.h>
6 #include <Carbon/Carbon.h>
7 #include <stddef.h>
8
9 #include "chrome/browser/global_keyboard_shortcuts_mac.h"
10
11 #include "base/check_op.h"
12 #include "base/macros.h"
13 #include "base/stl_util.h"
14 #include "chrome/app/chrome_command_ids.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "ui/base/buildflags.h"
17 #include "ui/events/keycodes/keyboard_code_conversion_mac.h"
18
19 namespace {
20
21 int CommandForKeys(bool command_key,
22                    bool shift_key,
23                    bool cntrl_key,
24                    bool opt_key,
25                    int vkey_code) {
26   NSUInteger modifierFlags = 0;
27   if (command_key)
28     modifierFlags |= NSCommandKeyMask;
29   if (shift_key)
30     modifierFlags |= NSShiftKeyMask;
31   if (cntrl_key)
32     modifierFlags |= NSControlKeyMask;
33   if (opt_key)
34     modifierFlags |= NSAlternateKeyMask;
35
36   switch (vkey_code) {
37     case kVK_UpArrow:
38     case kVK_DownArrow:
39     case kVK_LeftArrow:
40     case kVK_RightArrow:
41       // Docs say this is set whenever a key came from the numpad *or* the arrow
42       // keys.
43       modifierFlags |= NSEventModifierFlagNumericPad;
44       break;
45     default:
46       break;
47   }
48
49   unichar shifted_character;
50   unichar character;
51   int result = ui::MacKeyCodeForWindowsKeyCode(
52       ui::KeyboardCodeFromKeyCode(vkey_code), modifierFlags, &shifted_character,
53       &character);
54   DCHECK_NE(result, -1);
55
56   NSEvent* event = [NSEvent
57                  keyEventWithType:NSKeyDown
58                          location:NSZeroPoint
59                     modifierFlags:modifierFlags
60                         timestamp:0.0
61                      windowNumber:0
62                           context:nil
63                        characters:[NSString stringWithFormat:@"%C", character]
64       charactersIgnoringModifiers:[NSString
65                                       stringWithFormat:@"%C", shifted_character]
66                         isARepeat:NO
67                           keyCode:vkey_code];
68
69   return CommandForKeyEvent(event).chrome_command;
70 }
71
72 }  // namespace
73
74 TEST(GlobalKeyboardShortcuts, BasicFunctionality) {
75   // Test that an invalid shortcut translates into an invalid command id.
76   EXPECT_EQ(-1, CommandForKeys(false, false, false, false, 0));
77
78   // Check that all known keyboard shortcuts return valid results.
79   for (const auto& shortcut : GetShortcutsNotPresentInMainMenu()) {
80     int cmd_num = CommandForKeys(shortcut.command_key, shortcut.shift_key,
81                                  shortcut.cntrl_key, shortcut.opt_key,
82                                  shortcut.vkey_code);
83     EXPECT_EQ(cmd_num, shortcut.chrome_command);
84   }
85   // Test that switching tabs triggers off keycodes and not characters (visible
86   // with the Italian keyboard layout).
87   EXPECT_EQ(IDC_SELECT_TAB_0,
88             CommandForKeys(true, false, false, false, kVK_ANSI_1));
89 }
90
91 TEST(GlobalKeyboardShortcuts, KeypadNumberKeysMatch) {
92   // Test that the shortcuts that are generated by keypad number keys match the
93   // equivalent keys.
94   static const struct {
95     int keycode;
96     int keypad_keycode;
97   } equivalents[] = {
98     {kVK_ANSI_0, kVK_ANSI_Keypad0},
99     {kVK_ANSI_1, kVK_ANSI_Keypad1},
100     {kVK_ANSI_2, kVK_ANSI_Keypad2},
101     {kVK_ANSI_3, kVK_ANSI_Keypad3},
102     {kVK_ANSI_4, kVK_ANSI_Keypad4},
103     {kVK_ANSI_5, kVK_ANSI_Keypad5},
104     {kVK_ANSI_6, kVK_ANSI_Keypad6},
105     {kVK_ANSI_7, kVK_ANSI_Keypad7},
106     {kVK_ANSI_8, kVK_ANSI_Keypad8},
107     {kVK_ANSI_9, kVK_ANSI_Keypad9},
108   };
109
110   // We only consider unshifted keys. A shifted numpad key gives a different
111   // keyEquivalent than a shifted number key.
112   int shift = 0;
113   for (unsigned int i = 0; i < base::size(equivalents); ++i) {
114     for (int command = 0; command <= 1; ++command) {
115       for (int control = 0; control <= 1; ++control) {
116         for (int option = 0; option <= 1; ++option) {
117           EXPECT_EQ(CommandForKeys(command, shift, control, option,
118                                    equivalents[i].keycode),
119                     CommandForKeys(command, shift, control, option,
120                                    equivalents[i].keypad_keycode));
121           EXPECT_EQ(CommandForKeys(command, shift, control, option,
122                                    equivalents[i].keycode),
123                     CommandForKeys(command, shift, control, option,
124                                    equivalents[i].keypad_keycode));
125         }
126       }
127     }
128   }
129 }