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 <Carbon/Carbon.h> | 6 #include <Carbon/Carbon.h> |
6 | 7 |
7 #include "chrome/browser/global_keyboard_shortcuts_mac.h" | 8 #include "chrome/browser/global_keyboard_shortcuts_mac.h" |
8 | 9 |
| 10 #include "chrome/app/chrome_dll_resource.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
10 | 12 |
11 TEST(GlobalKeyboardShortcuts, ShortcutsToWindowCommand) { | 13 TEST(GlobalKeyboardShortcuts, ShortcutsToWindowCommand) { |
12 // Test that an invalid shortcut translates into an invalid command id. | 14 // Test that an invalid shortcut translates into an invalid command id. |
13 ASSERT_EQ( | 15 ASSERT_EQ( |
14 -1, CommandForWindowKeyboardShortcut(false, false, false, false, 0)); | 16 -1, CommandForWindowKeyboardShortcut(false, false, false, false, 0, 0)); |
15 | 17 |
16 // Check that all known keyboard shortcuts return valid results. | 18 // Check that all known keyboard shortcuts return valid results. |
17 size_t num_shortcuts = 0; | 19 size_t num_shortcuts = 0; |
18 const KeyboardShortcutData *it = | 20 const KeyboardShortcutData *it = |
19 GetWindowKeyboardShortcutTable(&num_shortcuts); | 21 GetWindowKeyboardShortcutTable(&num_shortcuts); |
20 ASSERT_GT(num_shortcuts, 0U); | 22 ASSERT_GT(num_shortcuts, 0U); |
21 for (size_t i = 0; i < num_shortcuts; ++i, ++it) { | 23 for (size_t i = 0; i < num_shortcuts; ++i, ++it) { |
22 int cmd_num = CommandForWindowKeyboardShortcut( | 24 int cmd_num = CommandForWindowKeyboardShortcut( |
23 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, |
24 it->vkey_code); | 26 it->vkey_code, it->key_char); |
25 ASSERT_EQ(cmd_num, it->chrome_command); | 27 ASSERT_EQ(cmd_num, it->chrome_command); |
26 } | 28 } |
27 | 29 |
28 // 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 |
29 // 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 |
30 // should have text editing functionality). | 32 // should have text editing functionality). |
31 ASSERT_EQ(-1, CommandForWindowKeyboardShortcut( | 33 ASSERT_EQ(-1, CommandForWindowKeyboardShortcut( |
32 true, false, false, false, kVK_LeftArrow)); | 34 true, false, false, false, kVK_LeftArrow, 0)); |
33 ASSERT_EQ(-1, CommandForWindowKeyboardShortcut( | 35 ASSERT_EQ(-1, CommandForWindowKeyboardShortcut( |
34 false, false, false, false, kVK_Delete)); | 36 false, false, false, false, kVK_Delete, 0)); |
| 37 |
| 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. |
| 40 ASSERT_EQ(IDC_SELECT_NEXT_TAB, CommandForWindowKeyboardShortcut( |
| 41 true, false, false, false, kVK_ANSI_Period, '}')); |
| 42 ASSERT_EQ(IDC_SELECT_PREVIOUS_TAB, CommandForWindowKeyboardShortcut( |
| 43 true, true, false, false, kVK_ANSI_Slash, '{')); |
| 44 |
| 45 // One more test for Cmd-'{' / Alt-8 (on german keyboard layout). |
| 46 ASSERT_EQ(IDC_SELECT_PREVIOUS_TAB, CommandForWindowKeyboardShortcut( |
| 47 true, false, false, true, kVK_ANSI_8, '{')); |
35 } | 48 } |
36 | 49 |
37 TEST(GlobalKeyboardShortcuts, ShortcutsToDelayedWindowCommand) { | 50 TEST(GlobalKeyboardShortcuts, ShortcutsToDelayedWindowCommand) { |
38 // Test that an invalid shortcut translates into an invalid command id. | 51 // Test that an invalid shortcut translates into an invalid command id. |
39 ASSERT_EQ(-1, | 52 ASSERT_EQ(-1, |
40 CommandForDelayedWindowKeyboardShortcut(false, false, false, false, 0)); | 53 CommandForDelayedWindowKeyboardShortcut(false, false, false, false, |
| 54 0, 0)); |
41 | 55 |
42 // Check that all known keyboard shortcuts return valid results. | 56 // Check that all known keyboard shortcuts return valid results. |
43 size_t num_shortcuts = 0; | 57 size_t num_shortcuts = 0; |
44 const KeyboardShortcutData *it = | 58 const KeyboardShortcutData *it = |
45 GetDelayedWindowKeyboardShortcutTable(&num_shortcuts); | 59 GetDelayedWindowKeyboardShortcutTable(&num_shortcuts); |
46 ASSERT_GT(num_shortcuts, 0U); | 60 ASSERT_GT(num_shortcuts, 0U); |
47 for (size_t i = 0; i < num_shortcuts; ++i, ++it) { | 61 for (size_t i = 0; i < num_shortcuts; ++i, ++it) { |
48 int cmd_num = CommandForDelayedWindowKeyboardShortcut( | 62 int cmd_num = CommandForDelayedWindowKeyboardShortcut( |
49 it->command_key, it->shift_key, it->cntrl_key, it->opt_key, | 63 it->command_key, it->shift_key, it->cntrl_key, it->opt_key, |
50 it->vkey_code); | 64 it->vkey_code, it->key_char); |
51 ASSERT_EQ(cmd_num, it->chrome_command); | 65 ASSERT_EQ(cmd_num, it->chrome_command); |
52 } | 66 } |
53 } | 67 } |
54 | 68 |
55 TEST(GlobalKeyboardShortcuts, ShortcutsToBrowserCommand) { | 69 TEST(GlobalKeyboardShortcuts, ShortcutsToBrowserCommand) { |
56 // Test that an invalid shortcut translates into an invalid command id. | 70 // Test that an invalid shortcut translates into an invalid command id. |
57 ASSERT_EQ( | 71 ASSERT_EQ( |
58 -1, CommandForBrowserKeyboardShortcut(false, false, false, false, 0)); | 72 -1, CommandForBrowserKeyboardShortcut(false, false, false, false, |
| 73 0, 0)); |
59 | 74 |
60 // Check that all known keyboard shortcuts return valid results. | 75 // Check that all known keyboard shortcuts return valid results. |
61 size_t num_shortcuts = 0; | 76 size_t num_shortcuts = 0; |
62 const KeyboardShortcutData *it = | 77 const KeyboardShortcutData *it = |
63 GetBrowserKeyboardShortcutTable(&num_shortcuts); | 78 GetBrowserKeyboardShortcutTable(&num_shortcuts); |
64 ASSERT_GT(num_shortcuts, 0U); | 79 ASSERT_GT(num_shortcuts, 0U); |
65 for (size_t i = 0; i < num_shortcuts; ++i, ++it) { | 80 for (size_t i = 0; i < num_shortcuts; ++i, ++it) { |
66 int cmd_num = CommandForBrowserKeyboardShortcut( | 81 int cmd_num = CommandForBrowserKeyboardShortcut( |
67 it->command_key, it->shift_key, it->cntrl_key, it->opt_key, | 82 it->command_key, it->shift_key, it->cntrl_key, it->opt_key, |
68 it->vkey_code); ASSERT_EQ(cmd_num, it->chrome_command); | 83 it->vkey_code, it->key_char); |
| 84 ASSERT_EQ(cmd_num, it->chrome_command); |
69 } | 85 } |
70 } | 86 } |
| 87 |
| 88 NSEvent* KeyEvent(bool command_key, bool shift_key, |
| 89 bool cntrl_key, bool opt_key, |
| 90 NSString* chars, NSString* charsNoMods) { |
| 91 NSUInteger modifierFlags = 0; |
| 92 if (command_key) |
| 93 modifierFlags |= NSCommandKeyMask; |
| 94 if (shift_key) |
| 95 modifierFlags |= NSShiftKeyMask; |
| 96 if (cntrl_key) |
| 97 modifierFlags |= NSControlKeyMask; |
| 98 if (opt_key) |
| 99 modifierFlags |= NSAlternateKeyMask; |
| 100 return [NSEvent keyEventWithType:NSKeyDown |
| 101 location:NSZeroPoint |
| 102 modifierFlags:modifierFlags |
| 103 timestamp:0.0 |
| 104 windowNumber:0 |
| 105 context:nil |
| 106 characters:chars |
| 107 charactersIgnoringModifiers:charsNoMods |
| 108 isARepeat:NO |
| 109 keyCode:0]; |
| 110 } |
| 111 |
| 112 TEST(GlobalKeyboardShortcuts, KeyCharacterForEvent) { |
| 113 // 'a' |
| 114 ASSERT_EQ('a', KeyCharacterForEvent( |
| 115 KeyEvent(false, false, false, false, @"a", @"a"))); |
| 116 // cmd-'a' / cmd-shift-'a' |
| 117 ASSERT_EQ('a', KeyCharacterForEvent( |
| 118 KeyEvent(true, true, false, false, @"a", @"A"))); |
| 119 // '8' |
| 120 ASSERT_EQ('8', KeyCharacterForEvent( |
| 121 KeyEvent(false, false, false, false, @"8", @"8"))); |
| 122 // '{' / alt-'8' on german |
| 123 ASSERT_EQ('{', KeyCharacterForEvent( |
| 124 KeyEvent(false, false, false, true, @"{", @"8"))); |
| 125 // cmd-'{' / cmd-shift-'[' on ansi |
| 126 ASSERT_EQ('{', KeyCharacterForEvent( |
| 127 KeyEvent(true, true, false, false, @"[", @"{"))); |
| 128 // cmd-'z' / cmd-shift-';' on dvorak-qwerty |
| 129 ASSERT_EQ('z', KeyCharacterForEvent( |
| 130 KeyEvent(true, true, false, false, @"z", @":"))); |
| 131 // Test if getting dead-key events return 0 and do not hang. |
| 132 ASSERT_EQ(0, KeyCharacterForEvent( |
| 133 KeyEvent(false, false, false, false, @"", @""))); |
| 134 } |
OLD | NEW |