Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(60)

Side by Side Diff: chrome/browser/cocoa/menu_controller_unittest.mm

Issue 465130: Share the code that builds the page menu in a common model, make Mac and Win ... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 11 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/browser/cocoa/menu_controller.mm ('k') | chrome/browser/cocoa/toolbar_controller.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Property Changes:
Name: svn:eol-style
+ LF
OLDNEW
(Empty)
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. Use of this
2 // source code is governed by a BSD-style license that can be found in the
3 // LICENSE file.
4
5 #import <Cocoa/Cocoa.h>
6
7 #include "app/menus/simple_menu_model.h"
8 #include "base/sys_string_conversions.h"
9 #include "base/utf_string_conversions.h"
10 #include "chrome/browser/cocoa/cocoa_test_helper.h"
11 #include "chrome/browser/cocoa/menu_controller.h"
12 #include "grit/generated_resources.h"
13
14 class MenuControllerTest : public CocoaTest {
15 };
16
17 // A menu delegate that counts the number of times certain things are called
18 // to make sure things are hooked up properly.
19 class Delegate : public menus::SimpleMenuModel::Delegate {
20 public:
21 Delegate() : execute_count_(0), enable_count_(0) { }
22
23 virtual bool IsCommandIdChecked(int command_id) const { return false; }
24 virtual bool IsCommandIdEnabled(int command_id) const {
25 ++enable_count_;
26 return true;
27 }
28 virtual bool GetAcceleratorForCommandId(
29 int command_id,
30 menus::Accelerator* accelerator) { return false; }
31 virtual void ExecuteCommand(int command_id) { ++execute_count_; }
32
33 int execute_count_;
34 mutable int enable_count_;
35 };
36
37 TEST_F(MenuControllerTest, EmptyMenu) {
38 Delegate delegate;
39 menus::SimpleMenuModel model(&delegate);
40 scoped_nsobject<MenuController> menu(
41 [[MenuController alloc] initWithModel:&model useWithPopUpButtonCell:NO]);
42 EXPECT_EQ([[menu menu] numberOfItems], 0);
43 }
44
45 TEST_F(MenuControllerTest, BasicCreation) {
46 Delegate delegate;
47 menus::SimpleMenuModel model(&delegate);
48 model.AddItem(1, WideToUTF16(L"one"));
49 model.AddItem(2, WideToUTF16(L"two"));
50 model.AddItem(3, WideToUTF16(L"three"));
51 model.AddSeparator();
52 model.AddItem(4, WideToUTF16(L"four"));
53 model.AddItem(5, WideToUTF16(L"five"));
54
55 scoped_nsobject<MenuController> menu(
56 [[MenuController alloc] initWithModel:&model useWithPopUpButtonCell:NO]);
57 EXPECT_EQ([[menu menu] numberOfItems], 6);
58
59 // Check the title, tag, and represented object are correct for a random
60 // element.
61 NSMenuItem* itemTwo = [[menu menu] itemAtIndex:2];
62 NSString* title = [itemTwo title];
63 EXPECT_EQ(WideToUTF16(L"three"), base::SysNSStringToUTF16(title));
64 EXPECT_EQ([itemTwo tag], 2);
65 EXPECT_EQ([[itemTwo representedObject] pointerValue], &model);
66
67 EXPECT_TRUE([[[menu menu] itemAtIndex:3] isSeparatorItem]);
68 }
69
70 TEST_F(MenuControllerTest, Submenus) {
71 Delegate delegate;
72 menus::SimpleMenuModel model(&delegate);
73 model.AddItem(1, WideToUTF16(L"one"));
74 menus::SimpleMenuModel submodel(&delegate);
75 submodel.AddItem(2, WideToUTF16(L"sub-one"));
76 submodel.AddItem(3, WideToUTF16(L"sub-two"));
77 submodel.AddItem(4, WideToUTF16(L"sub-three"));
78 model.AddSubMenuWithStringId(IDS_ZOOM_MENU, &submodel);
79 model.AddItem(5, WideToUTF16(L"three"));
80
81 scoped_nsobject<MenuController> menu(
82 [[MenuController alloc] initWithModel:&model useWithPopUpButtonCell:NO]);
83 EXPECT_EQ([[menu menu] numberOfItems], 3);
84
85 // Inspect the submenu to ensure it has correct properties.
86 NSMenu* submenu = [[[menu menu] itemAtIndex:1] submenu];
87 EXPECT_TRUE(submenu);
88 EXPECT_EQ([submenu numberOfItems], 3);
89
90 // Inspect one of the items to make sure it has the correct model as its
91 // represented object and the proper tag.
92 NSMenuItem* submenuItem = [submenu itemAtIndex:1];
93 NSString* title = [submenuItem title];
94 EXPECT_EQ(WideToUTF16(L"sub-two"), base::SysNSStringToUTF16(title));
95 EXPECT_EQ([submenuItem tag], 1);
96 EXPECT_EQ([[submenuItem representedObject] pointerValue], &submodel);
97
98 // Make sure the item after the submenu is correct and its represented
99 // object is back to the top model.
100 NSMenuItem* item = [[menu menu] itemAtIndex:2];
101 title = [item title];
102 EXPECT_EQ(WideToUTF16(L"three"), base::SysNSStringToUTF16(title));
103 EXPECT_EQ([item tag], 2);
104 EXPECT_EQ([[item representedObject] pointerValue], &model);
105 }
106
107 TEST_F(MenuControllerTest, EmptySubmenu) {
108 Delegate delegate;
109 menus::SimpleMenuModel model(&delegate);
110 model.AddItem(1, WideToUTF16(L"one"));
111 menus::SimpleMenuModel submodel(&delegate);
112 model.AddSubMenuWithStringId(IDS_ZOOM_MENU, &submodel);
113
114 scoped_nsobject<MenuController> menu(
115 [[MenuController alloc] initWithModel:&model useWithPopUpButtonCell:NO]);
116 EXPECT_EQ([[menu menu] numberOfItems], 2);
117 }
118
119 TEST_F(MenuControllerTest, PopUpButton) {
120 Delegate delegate;
121 menus::SimpleMenuModel model(&delegate);
122 model.AddItem(1, WideToUTF16(L"one"));
123 model.AddItem(2, WideToUTF16(L"two"));
124 model.AddItem(3, WideToUTF16(L"three"));
125
126 // Menu should have an extra item inserted at position 0 that has an empty
127 // title.
128 scoped_nsobject<MenuController> menu(
129 [[MenuController alloc] initWithModel:&model useWithPopUpButtonCell:YES]);
130 EXPECT_EQ([[menu menu] numberOfItems], 4);
131 EXPECT_EQ(base::SysNSStringToUTF16([[[menu menu] itemAtIndex:0] title]),
132 string16());
133
134 // Make sure the tags are still correct (the index no longer matches the tag).
135 NSMenuItem* itemTwo = [[menu menu] itemAtIndex:2];
136 EXPECT_EQ([itemTwo tag], 1);
137 }
138
139 TEST_F(MenuControllerTest, Execute) {
140 Delegate delegate;
141 menus::SimpleMenuModel model(&delegate);
142 model.AddItem(1, WideToUTF16(L"one"));
143 scoped_nsobject<MenuController> menu(
144 [[MenuController alloc] initWithModel:&model useWithPopUpButtonCell:NO]);
145 EXPECT_EQ([[menu menu] numberOfItems], 1);
146
147 // Fake selecting the menu item, we expect the delegate to be told to execute
148 // a command.
149 NSMenuItem* item = [[menu menu] itemAtIndex:0];
150 [[item target] performSelector:[item action] withObject:item];
151 EXPECT_EQ(delegate.execute_count_, 1);
152 }
153
154 void Validate(MenuController* controller, NSMenu* menu) {
155 for (int i = 0; i < [menu numberOfItems]; ++i) {
156 NSMenuItem* item = [menu itemAtIndex:i];
157 [controller validateUserInterfaceItem:item];
158 if ([item hasSubmenu])
159 Validate(controller, [item submenu]);
160 }
161 }
162
163 TEST_F(MenuControllerTest, Validate) {
164 Delegate delegate;
165 menus::SimpleMenuModel model(&delegate);
166 model.AddItem(1, WideToUTF16(L"one"));
167 model.AddItem(2, WideToUTF16(L"two"));
168 menus::SimpleMenuModel submodel(&delegate);
169 submodel.AddItem(2, WideToUTF16(L"sub-one"));
170 model.AddSubMenuWithStringId(IDS_ZOOM_MENU, &submodel);
171
172 scoped_nsobject<MenuController> menu(
173 [[MenuController alloc] initWithModel:&model useWithPopUpButtonCell:NO]);
174 EXPECT_EQ([[menu menu] numberOfItems], 3);
175
176 Validate(menu.get(), [menu menu]);
177 }
OLDNEW
« no previous file with comments | « chrome/browser/cocoa/menu_controller.mm ('k') | chrome/browser/cocoa/toolbar_controller.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698