OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
Robert Sesek
2014/02/11 16:19:48
Happy new year x2
erikchen
2014/02/11 19:42:27
Done.
| |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #import <Cocoa/Cocoa.h> | |
6 | |
7 #include "base/logging.h" | |
8 #import "chrome/browser/ui/cocoa/accelerators_cocoa.h" | |
9 #include "chrome/test/base/in_process_browser_test.h" | |
10 #include "ui/base/accelerators/platform_accelerator_cocoa.h" | |
11 #import "ui/events/keycodes/keyboard_code_conversion_mac.h" | |
12 #include "testing/gtest_mac.h" | |
13 | |
14 | |
15 typedef InProcessBrowserTest AcceleratorsCocoaBrowsertest; | |
Robert Sesek
2014/02/11 16:19:48
Browsertest -> BrowserTest
erikchen
2014/02/11 19:42:27
I copied the casing from another browser_test in t
| |
16 | |
17 namespace { | |
18 | |
19 // Adds all NSMenuItems with an accelerator to the array. | |
20 void addAcceleratorItemsToArray(NSMenu* menu, NSMutableArray* array) { | |
Robert Sesek
2014/02/11 16:19:48
naming: AddAccelerator
erikchen
2014/02/11 19:42:27
Done.
Robert Sesek
2014/02/11 22:18:16
Argh, sorry I should have been more clear. I was j
erikchen
2014/02/11 23:16:28
Done.
| |
21 for (NSMenuItem* item in [menu itemArray]) { | |
22 NSMenu* submenu = [item submenu]; | |
23 if (submenu) | |
24 addAcceleratorItemsToArray(submenu, array); | |
Robert Sesek
2014/02/11 16:19:48
nit: over-indented
erikchen
2014/02/11 19:42:27
Done.
| |
25 | |
26 if ([item keyEquivalent].length > 0) | |
27 [array addObject:item]; | |
28 } | |
29 } | |
30 | |
31 // Returns the NSMenuItem that has the given keyEquivalent and modifiers, or | |
32 // nil. | |
33 NSMenuItem* menuContainsAccelerator(NSMenu* menu, | |
Robert Sesek
2014/02/11 16:19:48
naming: MenuContains
erikchen
2014/02/11 19:42:27
Done.
| |
34 NSString* keyEquivalent, | |
35 NSUInteger modifiers) { | |
36 for (NSMenuItem* item in [menu itemArray]) { | |
37 NSMenu* submenu = [item submenu]; | |
38 if (submenu) { | |
39 NSMenuItem* result = | |
40 menuContainsAccelerator(submenu, keyEquivalent, modifiers); | |
41 if (result) | |
42 return result; | |
43 } | |
44 | |
45 if ([[item keyEquivalent] isEqual:keyEquivalent]) { | |
46 BOOL maskEqual = | |
47 (modifiers == item.keyEquivalentModifierMask) || | |
48 ((modifiers & (~NSShiftKeyMask)) == item.keyEquivalentModifierMask); | |
Robert Sesek
2014/02/11 16:19:48
You use [square brackets] above for keyEquivalent
erikchen
2014/02/11 19:42:27
changed keyEquivalent, keyEquivalentModifierMask,
| |
49 if (maskEqual) | |
50 return item; | |
51 } | |
52 } | |
53 return nil; | |
54 } | |
55 | |
56 } // Anonymous namespace. | |
Robert Sesek
2014/02/11 16:19:48
"// namespace"
erikchen
2014/02/11 19:42:27
Done.
| |
57 | |
58 // Checks that each NSMenuItem in the main menu has a corresponding accelerator, | |
59 // and the keyEquivalent/modifiers match. | |
60 IN_PROC_BROWSER_TEST_F(AcceleratorsCocoaBrowsertest, | |
61 MainMenuAcceleratorsInMapping) { | |
62 NSMenu* menu = [NSApp mainMenu]; | |
63 NSMutableArray* array = [NSMutableArray array]; | |
64 addAcceleratorItemsToArray(menu, array); | |
65 | |
66 for (NSMenuItem* item in array) { | |
67 int command_id = item.tag; | |
68 AcceleratorsCocoa* keymap = AcceleratorsCocoa::GetInstance(); | |
69 const ui::Accelerator* accelerator; | |
70 | |
71 // If the tag is zero, then the NSMenuItem must use a custom selector. | |
72 // Check that the accelerator is present as an un-mapped accelerator. | |
73 if (command_id == 0) { | |
74 accelerator = keymap->GetAcceleratorForHotKey( | |
75 item.keyEquivalent, item.keyEquivalentModifierMask); | |
76 | |
77 EXPECT_TRUE(accelerator); | |
78 return; | |
79 } | |
80 | |
81 // If the tag isn't zero, then it must correspond to an IDC_* command. | |
82 accelerator = keymap->GetAcceleratorForCommand(command_id); | |
83 EXPECT_TRUE(accelerator); | |
84 if (!accelerator) | |
85 continue; | |
86 | |
87 // Get the Cocoa keyEquivalent associated with the accelerator. | |
88 const ui::PlatformAcceleratorCocoa* platform_accelerator = | |
89 static_cast<const ui::PlatformAcceleratorCocoa*>( | |
90 accelerator->platform_accelerator()); | |
91 NSString* cocoaKeyEquivalent = platform_accelerator->characters(); | |
Robert Sesek
2014/02/11 16:19:48
naming: under_scores
erikchen
2014/02/11 19:42:27
Done.
| |
92 | |
93 // Check that the menu item's keyEquivalent matches the one from the | |
94 // cocoa accelerator map. | |
95 EXPECT_NSEQ(cocoaKeyEquivalent, item.keyEquivalent); | |
96 | |
97 // Check that the menu item's modifier mask matches the one stored in the | |
98 // accelerator. A mask that include NSShiftKeyMask may not include the | |
99 // relevant bit (the information is reflected in the keyEquivalent of the | |
100 // NSMenuItem). | |
101 NSUInteger mask = platform_accelerator->modifier_mask(); | |
102 BOOL maskEqual = | |
103 (mask == item.keyEquivalentModifierMask) || | |
104 ((mask & (~NSShiftKeyMask)) == item.keyEquivalentModifierMask); | |
105 EXPECT_TRUE(maskEqual); | |
106 } | |
107 } | |
108 | |
109 // Check that each accelerator with a command_id has an associated NSMenuItem | |
110 // in the main menu. If the selector is commandDispatch:, then the tag must | |
111 // match the command_id. | |
112 IN_PROC_BROWSER_TEST_F(AcceleratorsCocoaBrowsertest, | |
113 MappingAcceleratorsInMainMenu) { | |
114 AcceleratorsCocoa* keymap = AcceleratorsCocoa::GetInstance(); | |
115 for (AcceleratorsCocoa::AcceleratorMap::iterator it = | |
116 keymap->accelerators_.begin(); | |
117 it != keymap->accelerators_.end(); | |
118 ++it) { | |
119 const ui::PlatformAcceleratorCocoa* platform_accelerator = | |
120 static_cast<const ui::PlatformAcceleratorCocoa*>( | |
121 it->second.platform_accelerator()); | |
122 | |
123 // Check that there exists a corresponding NSMenuItem. | |
124 NSMenuItem* item = | |
125 menuContainsAccelerator([NSApp mainMenu], | |
126 platform_accelerator->characters(), | |
127 platform_accelerator->modifier_mask()); | |
128 EXPECT_TRUE(item); | |
129 | |
130 // If the menu uses a commandDispatch:, the tag must match the command id! | |
131 if (item.action == @selector(commandDispatch:)) | |
132 EXPECT_EQ(item.tag, it->first); | |
133 } | |
134 } | |
135 | |
Robert Sesek
2014/02/11 16:19:48
nit: extra blank line
erikchen
2014/02/11 19:42:27
Done.
| |
OLD | NEW |