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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
|
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "chrome/browser/cocoa/nsmenuitem_additions.h"
#include <Carbon/Carbon.h>
#include <ostream>
#include "base/scoped_nsobject.h"
#include "base/sys_string_conversions.h"
#include "testing/gtest/include/gtest/gtest.h"
NSEvent* KeyEvent(const NSUInteger modifierFlags,
NSString* chars,
NSString* charsNoMods,
const NSUInteger keyCode) {
return [NSEvent keyEventWithType:NSKeyDown
location:NSZeroPoint
modifierFlags:modifierFlags
timestamp:0.0
windowNumber:0
context:nil
characters:chars
charactersIgnoringModifiers:charsNoMods
isARepeat:NO
keyCode:keyCode];
}
NSMenuItem* MenuItem(NSString* equiv, NSUInteger mask) {
NSMenuItem* item = [[[NSMenuItem alloc] initWithTitle:@""
action:NULL
keyEquivalent:@""] autorelease];
[item setKeyEquivalent:equiv];
[item setKeyEquivalentModifierMask:mask];
return item;
}
std::ostream& operator<<(std::ostream& out, NSObject* obj) {
return out << base::SysNSStringToUTF8([obj description]);
}
std::ostream& operator<<(std::ostream& out, NSMenuItem* item) {
return out << "NSMenuItem " << base::SysNSStringToUTF8([item keyEquivalent]);
}
void ExpectKeyFiresItemEq(bool result, NSEvent* key, NSMenuItem* item,
bool compareCocoa) {
EXPECT_EQ(result, [item cr_firesForKeyEvent:key]) << key << '\n' << item;
// Make sure that Cocoa does in fact agree with our expectations. However,
// in some cases cocoa behaves weirdly (if you create e.g. a new event that
// contains all fields of the event that you get when hitting cmd-a with a
// russion keyboard layout, the copy won't fire a menu item that has cmd-a as
// key equivalent, even though the original event would) and isn't a good
// oracle function.
if (compareCocoa) {
scoped_nsobject<NSMenu> menu([[NSMenu alloc] initWithTitle:@"Menu!"]);
[menu setAutoenablesItems:NO];
EXPECT_FALSE([menu performKeyEquivalent:key]);
[menu addItem:item];
EXPECT_EQ(result, [menu performKeyEquivalent:key]) << key << '\n' << item;
}
}
void ExpectKeyFiresItem(
NSEvent* key, NSMenuItem* item, bool compareCocoa = true) {
ExpectKeyFiresItemEq(true, key, item, compareCocoa);
}
void ExpectKeyDoesntFireItem(
NSEvent* key, NSMenuItem* item, bool compareCocoa = true) {
ExpectKeyFiresItemEq(false, key, item, compareCocoa);
}
TEST(NSMenuItemAdditionsTest, TestFiresForKeyEvent) {
// These test cases were built by writing a small test app that has a
// MainMenu.xib with a given key equivalent set in Interface Builder and a
// some code that prints both the key equivalent that fires a menu item and
// the menu item's key equivalent and modifier masks. I then pasted those
// below. This was done with a US layout, unless otherwise noted. In the
// comments, "z" always means the physical "z" key on a US layout no matter
// what character that key produces.
NSMenuItem* item;
NSEvent* key;
unichar ch;
NSString* s;
// Sanity
item = MenuItem(@"", 0);
EXPECT_TRUE([item isEnabled]);
// a
key = KeyEvent(0x100, @"a", @"a", 0);
item = MenuItem(@"a", 0);
ExpectKeyFiresItem(key, item);
ExpectKeyDoesntFireItem(KeyEvent(0x20102, @"A", @"A", 0), item);
// Disabled menu item
key = KeyEvent(0x100, @"a", @"a", 0);
item = MenuItem(@"a", 0);
[item setEnabled:NO];
ExpectKeyDoesntFireItem(key, item, false);
// shift-a
key = KeyEvent(0x20102, @"A", @"A", 0);
item = MenuItem(@"A", 0);
ExpectKeyFiresItem(key, item);
ExpectKeyDoesntFireItem(KeyEvent(0x100, @"a", @"a", 0), item);
// cmd-opt-shift-a
key = KeyEvent(0x1a012a, @"\u00c5", @"A", 0);
item = MenuItem(@"A", 0x180000);
ExpectKeyFiresItem(key, item);
// cmd-opt-a
key = KeyEvent(0x18012a, @"\u00e5", @"a", 0);
item = MenuItem(@"a", 0x180000);
ExpectKeyFiresItem(key, item);
// cmd-=
key = KeyEvent(0x100110, @"=", @"=", 0x18);
item = MenuItem(@"=", 0x100000);
ExpectKeyFiresItem(key, item);
// cmd-shift-=
key = KeyEvent(0x12010a, @"=", @"+", 0x18);
item = MenuItem(@"+", 0x100000);
ExpectKeyFiresItem(key, item);
// Turns out Cocoa fires "+ 100108 + 18" if you hit cmd-= and the menu only
// has a cmd-+ shortcut. But that's transparent for |cr_firesForKeyEvent:|.
// ctrl-3
key = KeyEvent(0x40101, @"3", @"3", 0x14);
item = MenuItem(@"3", 0x40000);
ExpectKeyFiresItem(key, item);
// return
key = KeyEvent(0, @"\r", @"\r", 0x24);
item = MenuItem(@"\r", 0);
ExpectKeyFiresItem(key, item);
// shift-return
key = KeyEvent(0x20102, @"\r", @"\r", 0x24);
item = MenuItem(@"\r", 0x20000);
ExpectKeyFiresItem(key, item);
// shift-left
ch = NSLeftArrowFunctionKey;
s = [NSString stringWithCharacters:&ch length:1];
key = KeyEvent(0xa20102, s, s, 0x7b);
item = MenuItem(s, 0x20000);
ExpectKeyFiresItem(key, item);
// shift-f1 (with a layout that needs the fn key down for f1)
ch = NSF1FunctionKey;
s = [NSString stringWithCharacters:&ch length:1];
key = KeyEvent(0x820102, s, s, 0x7a);
item = MenuItem(s, 0x20000);
ExpectKeyFiresItem(key, item);
// esc
// Turns out this doesn't fire.
key = KeyEvent(0x100, @"\e", @"\e", 0x35);
item = MenuItem(@"\e", 0);
ExpectKeyDoesntFireItem(key,item, false);
// shift-esc
// Turns out this doesn't fire.
key = KeyEvent(0x20102, @"\e", @"\e", 0x35);
item = MenuItem(@"\e", 0x20000);
ExpectKeyDoesntFireItem(key,item, false);
// cmd-esc
key = KeyEvent(0x100108, @"\e", @"\e", 0x35);
item = MenuItem(@"\e", 0x100000);
ExpectKeyFiresItem(key, item);
// ctrl-esc
key = KeyEvent(0x40101, @"\e", @"\e", 0x35);
item = MenuItem(@"\e", 0x40000);
ExpectKeyFiresItem(key, item);
// delete ("backspace")
key = KeyEvent(0x100, @"\x7f", @"\x7f", 0x33);
item = MenuItem(@"\x08", 0);
ExpectKeyFiresItem(key, item, false);
// shift-delete
key = KeyEvent(0x20102, @"\x7f", @"\x7f", 0x33);
item = MenuItem(@"\x08", 0x20000);
ExpectKeyFiresItem(key, item, false);
// forwarddelete (fn-delete / fn-backspace)
ch = NSDeleteFunctionKey;
s = [NSString stringWithCharacters:&ch length:1];
key = KeyEvent(0x800100, s, s, 0x75);
item = MenuItem(@"\x7f", 0);
ExpectKeyFiresItem(key, item, false);
// shift-forwarddelete (shift-fn-delete / shift-fn-backspace)
ch = NSDeleteFunctionKey;
s = [NSString stringWithCharacters:&ch length:1];
key = KeyEvent(0x820102, s, s, 0x75);
item = MenuItem(@"\x7f", 0x20000);
ExpectKeyFiresItem(key, item, false);
// fn-left
ch = NSHomeFunctionKey;
s = [NSString stringWithCharacters:&ch length:1];
key = KeyEvent(0x800100, s, s, 0x73);
item = MenuItem(s, 0);
ExpectKeyFiresItem(key, item);
// cmd-left
ch = NSLeftArrowFunctionKey;
s = [NSString stringWithCharacters:&ch length:1];
key = KeyEvent(0xb00108, s, s, 0x7b);
item = MenuItem(s, 0x100000);
ExpectKeyFiresItem(key, item);
// Hitting the "a" key with a russian keyboard layout -- does not fire
// a menu item that has "a" as key equiv.
key = KeyEvent(0x100, @"\u0444", @"\u0444", 0);
item = MenuItem(@"a", 0);
ExpectKeyDoesntFireItem(key,item);
// cmd-a on a russion layout -- fires for a menu item with cmd-a as key equiv.
key = KeyEvent(0x100108, @"a", @"\u0444", 0);
item = MenuItem(@"a", 0x100000);
ExpectKeyFiresItem(key, item, false);
// cmd-z on US layout
key = KeyEvent(0x100108, @"z", @"z", 6);
item = MenuItem(@"z", 0x100000);
ExpectKeyFiresItem(key, item);
// cmd-y on german layout (has same keycode as cmd-z on us layout, shouldn't
// fire).
key = KeyEvent(0x100108, @"y", @"y", 6);
item = MenuItem(@"z", 0x100000);
ExpectKeyDoesntFireItem(key,item);
// cmd-z on german layout
key = KeyEvent(0x100108, @"z", @"z", 0x10);
item = MenuItem(@"z", 0x100000);
ExpectKeyFiresItem(key, item);
// fn-return (== enter)
key = KeyEvent(0x800100, @"\x3", @"\x3", 0x4c);
item = MenuItem(@"\r", 0);
ExpectKeyDoesntFireItem(key,item);
// cmd-z on dvorak layout (so that the key produces ';')
key = KeyEvent(0x100108, @";", @";", 6);
ExpectKeyDoesntFireItem(key, MenuItem(@"z", 0x100000));
ExpectKeyFiresItem(key, MenuItem(@";", 0x100000));
// cmd-z on dvorak qwerty layout (so that the key produces ';', but 'z' if
// cmd is down)
key = KeyEvent(0x100108, @"z", @";", 6);
ExpectKeyFiresItem(key, MenuItem(@"z", 0x100000), false);
ExpectKeyDoesntFireItem(key, MenuItem(@";", 0x100000), false);
// cmd-shift-z on dvorak layout (so that we get a ':')
key = KeyEvent(0x12010a, @";", @":", 6);
ExpectKeyFiresItem(key, MenuItem(@":", 0x100000));
ExpectKeyDoesntFireItem(key, MenuItem(@";", 0x100000));
// cmd-s with a serbian layout (just "s" produces something that looks a lot
// like "c" in some fonts, but is actually \u0441. cmd-s activates a menu item
// with key equivalent "s", not "c")
key = KeyEvent(0x100108, @"s", @"\u0441", 1);
ExpectKeyFiresItem(key, MenuItem(@"s", 0x100000), false);
ExpectKeyDoesntFireItem(key, MenuItem(@"c", 0x100000));
}
NSString* keyCodeToCharacter(NSUInteger keyCode,
EventModifiers modifiers,
TISInputSourceRef layout) {
CFDataRef uchr = (CFDataRef)TISGetInputSourceProperty(
layout, kTISPropertyUnicodeKeyLayoutData);
UCKeyboardLayout* keyLayout = (UCKeyboardLayout*)CFDataGetBytePtr(uchr);
UInt32 deadKeyState = 0;
OSStatus err = noErr;
UniCharCount maxStringLength = 4, actualStringLength;
UniChar unicodeString[4];
err = UCKeyTranslate(keyLayout,
(UInt16)keyCode,
kUCKeyActionDown,
modifiers,
LMGetKbdType(),
kUCKeyTranslateNoDeadKeysBit,
&deadKeyState,
maxStringLength,
&actualStringLength,
unicodeString);
assert(err == noErr);
CFStringRef temp = CFStringCreateWithCharacters(
kCFAllocatorDefault, unicodeString, 1);
return [(NSString*)temp autorelease];
}
TEST(NSMenuItemAdditionsTest, TestMOnDifferentLayouts) {
// There's one key -- "m" -- that has the same keycode on most keyboard
// layouts. This function tests a menu item with cmd-m as key equivalent
// can be fired on all layouts.
NSMenuItem* item = MenuItem(@"m", 0x100000);
NSDictionary* filter = [NSDictionary
dictionaryWithObject:(NSString*)kTISTypeKeyboardLayout
forKey:(NSString*)kTISPropertyInputSourceType];
// Docs say that including all layouts instead of just the active ones is
// slow, but there's no way around that.
NSArray* list = (NSArray*)TISCreateInputSourceList(
(CFDictionaryRef)filter, true);
for (id layout in list) {
TISInputSourceRef ref = (TISInputSourceRef)layout;
NSUInteger keyCode = 0x2e; // "m" on a US layout and most other layouts.
// On a few layouts, "m" has a different key code.
NSString* layoutId = (NSString*)TISGetInputSourceProperty(
ref, kTISPropertyInputSourceID);
if ([layoutId isEqualToString:@"com.apple.keylayout.Belgian"] ||
[layoutId isEqualToString:@"com.apple.keylayout.French"] ||
[layoutId isEqualToString:@"com.apple.keylayout.French-numerical"] ||
[layoutId isEqualToString:@"com.apple.keylayout.Italian"]) {
keyCode = 0x29;
} else if ([layoutId isEqualToString:@"com.apple.keylayout.Turkish"]) {
keyCode = 0x28;
} else if ([layoutId isEqualToString:@"com.apple.keylayout.Dvorak-Left"]) {
keyCode = 0x16;
} else if ([layoutId isEqualToString:@"com.apple.keylayout.Dvorak-Right"]) {
keyCode = 0x1a;
}
EventModifiers modifiers = cmdKey >> 8;
NSString* chars = keyCodeToCharacter(keyCode, modifiers, ref);
NSString* charsIgnoringMods = keyCodeToCharacter(keyCode, 0, ref);
NSEvent* key = KeyEvent(0x100000, chars, charsIgnoringMods, keyCode);
ExpectKeyFiresItem(key, item, false);
}
CFRelease(list);
}
|