OLD | NEW |
---|---|
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <AppKit/NSEvent.h> | 5 #include <AppKit/NSEvent.h> |
6 #include <Carbon/Carbon.h> | 6 #include <Carbon/Carbon.h> |
7 | 7 |
8 #include "chrome/browser/global_keyboard_shortcuts_mac.h" | 8 #include "chrome/browser/global_keyboard_shortcuts_mac.h" |
9 | 9 |
10 #include "chrome/app/chrome_command_ids.h" | 10 #include "chrome/app/chrome_command_ids.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
12 | 12 |
13 TEST(GlobalKeyboardShortcuts, ShortcutsToWindowCommand) { | 13 TEST(GlobalKeyboardShortcuts, ShortcutsToWindowCommand) { |
14 // Test that an invalid shortcut translates into an invalid command id. | 14 // Test that an invalid shortcut translates into an invalid command id. |
15 ASSERT_EQ( | 15 EXPECT_EQ( |
16 -1, CommandForWindowKeyboardShortcut(false, false, false, false, 0, 0)); | 16 -1, CommandForWindowKeyboardShortcut(false, false, false, false, 0, 0)); |
17 | 17 |
18 // Check that all known keyboard shortcuts return valid results. | 18 // Check that all known keyboard shortcuts return valid results. |
19 size_t num_shortcuts = 0; | 19 size_t num_shortcuts = 0; |
20 const KeyboardShortcutData *it = | 20 const KeyboardShortcutData *it = |
21 GetWindowKeyboardShortcutTable(&num_shortcuts); | 21 GetWindowKeyboardShortcutTable(&num_shortcuts); |
22 ASSERT_GT(num_shortcuts, 0U); | 22 ASSERT_GT(num_shortcuts, 0U); |
23 for (size_t i = 0; i < num_shortcuts; ++i, ++it) { | 23 for (size_t i = 0; i < num_shortcuts; ++i, ++it) { |
24 int cmd_num = CommandForWindowKeyboardShortcut( | 24 int cmd_num = CommandForWindowKeyboardShortcut( |
25 it->command_key, it->shift_key, it->cntrl_key, it->opt_key, | 25 it->command_key, it->shift_key, it->cntrl_key, it->opt_key, |
26 it->vkey_code, it->key_char); | 26 it->vkey_code, it->key_char); |
27 ASSERT_EQ(cmd_num, it->chrome_command); | 27 EXPECT_EQ(cmd_num, it->chrome_command); |
28 } | 28 } |
29 | 29 |
30 // Test that cmd-left and backspace are not window-level commands (else they | 30 // Test that cmd-left and backspace are not window-level commands (else they |
31 // would be invoked even if e.g. the omnibox had focus, where they really | 31 // would be invoked even if e.g. the omnibox had focus, where they really |
32 // should have text editing functionality). | 32 // should have text editing functionality). |
33 ASSERT_EQ(-1, CommandForWindowKeyboardShortcut( | 33 EXPECT_EQ(-1, CommandForWindowKeyboardShortcut( |
34 true, false, false, false, kVK_LeftArrow, 0)); | 34 true, false, false, false, kVK_LeftArrow, 0)); |
35 ASSERT_EQ(-1, CommandForWindowKeyboardShortcut( | 35 EXPECT_EQ(-1, CommandForWindowKeyboardShortcut( |
36 false, false, false, false, kVK_Delete, 0)); | 36 false, false, false, false, kVK_Delete, 0)); |
37 | 37 |
38 // Test that Cmd-'{' and Cmd-'}' are interpreted as IDC_SELECT_NEXT_TAB | 38 // Test that Cmd-'{' and Cmd-'}' are interpreted as IDC_SELECT_NEXT_TAB |
39 // and IDC_SELECT_PREVIOUS_TAB regardless of the virtual key code values. | 39 // and IDC_SELECT_PREVIOUS_TAB regardless of the virtual key code values. |
40 ASSERT_EQ(IDC_SELECT_NEXT_TAB, CommandForWindowKeyboardShortcut( | 40 EXPECT_EQ(IDC_SELECT_NEXT_TAB, CommandForWindowKeyboardShortcut( |
41 true, false, false, false, kVK_ANSI_Period, '}')); | 41 true, false, false, false, kVK_ANSI_Period, '}')); |
42 ASSERT_EQ(IDC_SELECT_PREVIOUS_TAB, CommandForWindowKeyboardShortcut( | 42 EXPECT_EQ(IDC_SELECT_PREVIOUS_TAB, CommandForWindowKeyboardShortcut( |
43 true, true, false, false, kVK_ANSI_Slash, '{')); | 43 true, true, false, false, kVK_ANSI_Slash, '{')); |
44 | 44 |
45 // One more test for Cmd-'{' / Alt-8 (on german keyboard layout). | 45 // One more test for Cmd-'{' / Alt-8 (on German keyboard layout). |
46 ASSERT_EQ(IDC_SELECT_PREVIOUS_TAB, CommandForWindowKeyboardShortcut( | 46 EXPECT_EQ(IDC_SELECT_PREVIOUS_TAB, CommandForWindowKeyboardShortcut( |
47 true, false, false, true, kVK_ANSI_8, '{')); | 47 true, false, false, true, kVK_ANSI_8, '{')); |
48 | |
49 // Test that switching tabs triggers off keycodes and not characters (visible | |
50 // with the Italian keyboard layout). | |
51 EXPECT_EQ(IDC_SELECT_TAB_0, CommandForWindowKeyboardShortcut( | |
52 true, false, false, false, kVK_ANSI_1, '&')); | |
53 } | |
54 | |
55 TEST(GlobalKeyboardShortcuts, KeypadNumberKeysMatch) { | |
56 // Test that the shortcuts that are generated by keypad number keys match the | |
57 // equivalent keys. | |
58 static const struct { | |
59 int keycode; | |
60 int keypad_keycode; | |
61 } equivalents[] = { | |
62 {kVK_ANSI_0, kVK_ANSI_Keypad0}, | |
63 {kVK_ANSI_1, kVK_ANSI_Keypad1}, | |
64 {kVK_ANSI_2, kVK_ANSI_Keypad2}, | |
65 {kVK_ANSI_3, kVK_ANSI_Keypad3}, | |
66 {kVK_ANSI_4, kVK_ANSI_Keypad4}, | |
67 {kVK_ANSI_5, kVK_ANSI_Keypad5}, | |
68 {kVK_ANSI_6, kVK_ANSI_Keypad6}, | |
69 {kVK_ANSI_7, kVK_ANSI_Keypad7}, | |
70 {kVK_ANSI_8, kVK_ANSI_Keypad8}, | |
71 {kVK_ANSI_9, kVK_ANSI_Keypad9}, | |
72 }; | |
73 | |
74 for (int i = 0; i < 10; ++i) { | |
Nico
2010/11/16 23:19:34
arraysize(equivalents) (or UNSAFE_ARRAYSIZE(equiva
Avi (use Gerrit)
2010/11/16 23:24:23
arraysize doesn't work because the array is of an
| |
75 for (int command = 0; command <= 1; ++command) { | |
76 for (int shift = 0; shift <= 1; ++shift) { | |
77 for (int control = 0; control <= 1; ++control) { | |
78 for (int option = 0; option <= 1; ++option) { | |
79 EXPECT_EQ( | |
80 CommandForWindowKeyboardShortcut( | |
81 command, shift, control, option, equivalents[i].keycode, 0), | |
82 CommandForWindowKeyboardShortcut( | |
83 command, shift, control, option, | |
84 equivalents[i].keypad_keycode, 0)); | |
85 EXPECT_EQ( | |
86 CommandForDelayedWindowKeyboardShortcut( | |
87 command, shift, control, option, equivalents[i].keycode, 0), | |
88 CommandForDelayedWindowKeyboardShortcut( | |
89 command, shift, control, option, | |
90 equivalents[i].keypad_keycode, 0)); | |
91 EXPECT_EQ( | |
92 CommandForBrowserKeyboardShortcut( | |
93 command, shift, control, option, equivalents[i].keycode, 0), | |
94 CommandForBrowserKeyboardShortcut( | |
95 command, shift, control, option, | |
96 equivalents[i].keypad_keycode, 0)); | |
97 } | |
98 } | |
99 } | |
100 } | |
101 } | |
48 } | 102 } |
49 | 103 |
50 TEST(GlobalKeyboardShortcuts, ShortcutsToDelayedWindowCommand) { | 104 TEST(GlobalKeyboardShortcuts, ShortcutsToDelayedWindowCommand) { |
51 // Test that an invalid shortcut translates into an invalid command id. | 105 // Test that an invalid shortcut translates into an invalid command id. |
52 ASSERT_EQ(-1, | 106 EXPECT_EQ(-1, |
53 CommandForDelayedWindowKeyboardShortcut(false, false, false, false, | 107 CommandForDelayedWindowKeyboardShortcut(false, false, false, false, |
54 0, 0)); | 108 0, 0)); |
55 | 109 |
56 // Check that all known keyboard shortcuts return valid results. | 110 // Check that all known keyboard shortcuts return valid results. |
57 size_t num_shortcuts = 0; | 111 size_t num_shortcuts = 0; |
58 const KeyboardShortcutData *it = | 112 const KeyboardShortcutData *it = |
59 GetDelayedWindowKeyboardShortcutTable(&num_shortcuts); | 113 GetDelayedWindowKeyboardShortcutTable(&num_shortcuts); |
60 ASSERT_GT(num_shortcuts, 0U); | 114 ASSERT_GT(num_shortcuts, 0U); |
61 for (size_t i = 0; i < num_shortcuts; ++i, ++it) { | 115 for (size_t i = 0; i < num_shortcuts; ++i, ++it) { |
62 int cmd_num = CommandForDelayedWindowKeyboardShortcut( | 116 int cmd_num = CommandForDelayedWindowKeyboardShortcut( |
63 it->command_key, it->shift_key, it->cntrl_key, it->opt_key, | 117 it->command_key, it->shift_key, it->cntrl_key, it->opt_key, |
64 it->vkey_code, it->key_char); | 118 it->vkey_code, it->key_char); |
65 ASSERT_EQ(cmd_num, it->chrome_command); | 119 EXPECT_EQ(cmd_num, it->chrome_command); |
66 } | 120 } |
67 } | 121 } |
68 | 122 |
69 TEST(GlobalKeyboardShortcuts, ShortcutsToBrowserCommand) { | 123 TEST(GlobalKeyboardShortcuts, ShortcutsToBrowserCommand) { |
70 // Test that an invalid shortcut translates into an invalid command id. | 124 // Test that an invalid shortcut translates into an invalid command id. |
71 ASSERT_EQ( | 125 EXPECT_EQ( |
72 -1, CommandForBrowserKeyboardShortcut(false, false, false, false, | 126 -1, CommandForBrowserKeyboardShortcut(false, false, false, false, |
73 0, 0)); | 127 0, 0)); |
74 | 128 |
75 // Check that all known keyboard shortcuts return valid results. | 129 // Check that all known keyboard shortcuts return valid results. |
76 size_t num_shortcuts = 0; | 130 size_t num_shortcuts = 0; |
77 const KeyboardShortcutData *it = | 131 const KeyboardShortcutData *it = |
78 GetBrowserKeyboardShortcutTable(&num_shortcuts); | 132 GetBrowserKeyboardShortcutTable(&num_shortcuts); |
79 ASSERT_GT(num_shortcuts, 0U); | 133 ASSERT_GT(num_shortcuts, 0U); |
80 for (size_t i = 0; i < num_shortcuts; ++i, ++it) { | 134 for (size_t i = 0; i < num_shortcuts; ++i, ++it) { |
81 int cmd_num = CommandForBrowserKeyboardShortcut( | 135 int cmd_num = CommandForBrowserKeyboardShortcut( |
82 it->command_key, it->shift_key, it->cntrl_key, it->opt_key, | 136 it->command_key, it->shift_key, it->cntrl_key, it->opt_key, |
83 it->vkey_code, it->key_char); | 137 it->vkey_code, it->key_char); |
84 ASSERT_EQ(cmd_num, it->chrome_command); | 138 EXPECT_EQ(cmd_num, it->chrome_command); |
85 } | 139 } |
86 } | 140 } |
87 | 141 |
88 NSEvent* KeyEvent(bool command_key, bool shift_key, | 142 NSEvent* KeyEvent(bool command_key, bool shift_key, |
89 bool cntrl_key, bool opt_key, | 143 bool cntrl_key, bool opt_key, |
90 NSString* chars, NSString* charsNoMods) { | 144 NSString* chars, NSString* charsNoMods) { |
91 NSUInteger modifierFlags = 0; | 145 NSUInteger modifierFlags = 0; |
92 if (command_key) | 146 if (command_key) |
93 modifierFlags |= NSCommandKeyMask; | 147 modifierFlags |= NSCommandKeyMask; |
94 if (shift_key) | 148 if (shift_key) |
95 modifierFlags |= NSShiftKeyMask; | 149 modifierFlags |= NSShiftKeyMask; |
96 if (cntrl_key) | 150 if (cntrl_key) |
97 modifierFlags |= NSControlKeyMask; | 151 modifierFlags |= NSControlKeyMask; |
98 if (opt_key) | 152 if (opt_key) |
99 modifierFlags |= NSAlternateKeyMask; | 153 modifierFlags |= NSAlternateKeyMask; |
100 return [NSEvent keyEventWithType:NSKeyDown | 154 return [NSEvent keyEventWithType:NSKeyDown |
101 location:NSZeroPoint | 155 location:NSZeroPoint |
102 modifierFlags:modifierFlags | 156 modifierFlags:modifierFlags |
103 timestamp:0.0 | 157 timestamp:0.0 |
104 windowNumber:0 | 158 windowNumber:0 |
105 context:nil | 159 context:nil |
106 characters:chars | 160 characters:chars |
107 charactersIgnoringModifiers:charsNoMods | 161 charactersIgnoringModifiers:charsNoMods |
108 isARepeat:NO | 162 isARepeat:NO |
109 keyCode:0]; | 163 keyCode:0]; |
110 } | 164 } |
111 | 165 |
112 TEST(GlobalKeyboardShortcuts, KeyCharacterForEvent) { | 166 TEST(GlobalKeyboardShortcuts, KeyCharacterForEvent) { |
113 // 'a' | 167 // 'a' |
114 ASSERT_EQ('a', KeyCharacterForEvent( | 168 EXPECT_EQ('a', KeyCharacterForEvent( |
115 KeyEvent(false, false, false, false, @"a", @"a"))); | 169 KeyEvent(false, false, false, false, @"a", @"a"))); |
116 // cmd-'a' / cmd-shift-'a' | 170 // cmd-'a' / cmd-shift-'a' |
117 ASSERT_EQ('a', KeyCharacterForEvent( | 171 EXPECT_EQ('a', KeyCharacterForEvent( |
118 KeyEvent(true, true, false, false, @"a", @"A"))); | 172 KeyEvent(true, true, false, false, @"a", @"A"))); |
119 // '8' | 173 // '8' |
120 ASSERT_EQ('8', KeyCharacterForEvent( | 174 EXPECT_EQ('8', KeyCharacterForEvent( |
121 KeyEvent(false, false, false, false, @"8", @"8"))); | 175 KeyEvent(false, false, false, false, @"8", @"8"))); |
122 // '{' / alt-'8' on german | 176 // '{' / alt-'8' on german |
123 ASSERT_EQ('{', KeyCharacterForEvent( | 177 EXPECT_EQ('{', KeyCharacterForEvent( |
124 KeyEvent(false, false, false, true, @"{", @"8"))); | 178 KeyEvent(false, false, false, true, @"{", @"8"))); |
125 // cmd-'{' / cmd-shift-'[' on ansi | 179 // cmd-'{' / cmd-shift-'[' on ansi |
126 ASSERT_EQ('{', KeyCharacterForEvent( | 180 EXPECT_EQ('{', KeyCharacterForEvent( |
127 KeyEvent(true, true, false, false, @"[", @"{"))); | 181 KeyEvent(true, true, false, false, @"[", @"{"))); |
128 // cmd-'z' / cmd-shift-';' on dvorak-qwerty | 182 // cmd-'z' / cmd-shift-';' on dvorak-qwerty |
129 ASSERT_EQ('z', KeyCharacterForEvent( | 183 EXPECT_EQ('z', KeyCharacterForEvent( |
130 KeyEvent(true, true, false, false, @"z", @":"))); | 184 KeyEvent(true, true, false, false, @"z", @":"))); |
131 // cmd-shift-'[' in an RTL context. | 185 // cmd-shift-'[' in an RTL context. |
132 ASSERT_EQ('{', KeyCharacterForEvent( | 186 EXPECT_EQ('{', KeyCharacterForEvent( |
133 KeyEvent(true, true, false, false, @"{", @"}"))); | 187 KeyEvent(true, true, false, false, @"{", @"}"))); |
134 // Test if getting dead-key events return 0 and do not hang. | 188 // Test if getting dead-key events return 0 and do not hang. |
135 ASSERT_EQ(0, KeyCharacterForEvent( | 189 EXPECT_EQ(0, KeyCharacterForEvent( |
136 KeyEvent(false, false, false, false, @"", @""))); | 190 KeyEvent(false, false, false, false, @"", @""))); |
137 } | 191 } |
OLD | NEW |