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

Side by Side Diff: ui/base/cocoa/menu_controller_unittest.mm

Issue 17593006: mac: Update clients of scoped_nsobject.h. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: iwyu, scoped_nsprotocol Created 7 years, 5 months 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
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 #import <Cocoa/Cocoa.h> 5 #import <Cocoa/Cocoa.h>
6 6
7 #include "base/message_loop.h" 7 #include "base/message_loop.h"
8 #include "base/strings/sys_string_conversions.h" 8 #include "base/strings/sys_string_conversions.h"
9 #include "base/strings/utf_string_conversions.h" 9 #include "base/strings/utf_string_conversions.h"
10 #include "grit/ui_resources.h" 10 #include "grit/ui_resources.h"
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 } 123 }
124 124
125 private: 125 private:
126 const gfx::Font* font_; 126 const gfx::Font* font_;
127 const int index_; 127 const int index_;
128 }; 128 };
129 129
130 TEST_F(MenuControllerTest, EmptyMenu) { 130 TEST_F(MenuControllerTest, EmptyMenu) {
131 Delegate delegate; 131 Delegate delegate;
132 SimpleMenuModel model(&delegate); 132 SimpleMenuModel model(&delegate);
133 scoped_nsobject<MenuController> menu( 133 base::scoped_nsobject<MenuController> menu(
134 [[MenuController alloc] initWithModel:&model useWithPopUpButtonCell:NO]); 134 [[MenuController alloc] initWithModel:&model useWithPopUpButtonCell:NO]);
135 EXPECT_EQ([[menu menu] numberOfItems], 0); 135 EXPECT_EQ([[menu menu] numberOfItems], 0);
136 } 136 }
137 137
138 TEST_F(MenuControllerTest, BasicCreation) { 138 TEST_F(MenuControllerTest, BasicCreation) {
139 Delegate delegate; 139 Delegate delegate;
140 SimpleMenuModel model(&delegate); 140 SimpleMenuModel model(&delegate);
141 model.AddItem(1, ASCIIToUTF16("one")); 141 model.AddItem(1, ASCIIToUTF16("one"));
142 model.AddItem(2, ASCIIToUTF16("two")); 142 model.AddItem(2, ASCIIToUTF16("two"));
143 model.AddItem(3, ASCIIToUTF16("three")); 143 model.AddItem(3, ASCIIToUTF16("three"));
144 model.AddSeparator(NORMAL_SEPARATOR); 144 model.AddSeparator(NORMAL_SEPARATOR);
145 model.AddItem(4, ASCIIToUTF16("four")); 145 model.AddItem(4, ASCIIToUTF16("four"));
146 model.AddItem(5, ASCIIToUTF16("five")); 146 model.AddItem(5, ASCIIToUTF16("five"));
147 147
148 scoped_nsobject<MenuController> menu( 148 base::scoped_nsobject<MenuController> menu(
149 [[MenuController alloc] initWithModel:&model useWithPopUpButtonCell:NO]); 149 [[MenuController alloc] initWithModel:&model useWithPopUpButtonCell:NO]);
150 EXPECT_EQ([[menu menu] numberOfItems], 6); 150 EXPECT_EQ([[menu menu] numberOfItems], 6);
151 151
152 // Check the title, tag, and represented object are correct for a random 152 // Check the title, tag, and represented object are correct for a random
153 // element. 153 // element.
154 NSMenuItem* itemTwo = [[menu menu] itemAtIndex:2]; 154 NSMenuItem* itemTwo = [[menu menu] itemAtIndex:2];
155 NSString* title = [itemTwo title]; 155 NSString* title = [itemTwo title];
156 EXPECT_EQ(ASCIIToUTF16("three"), base::SysNSStringToUTF16(title)); 156 EXPECT_EQ(ASCIIToUTF16("three"), base::SysNSStringToUTF16(title));
157 EXPECT_EQ([itemTwo tag], 2); 157 EXPECT_EQ([itemTwo tag], 2);
158 EXPECT_EQ([[itemTwo representedObject] pointerValue], &model); 158 EXPECT_EQ([[itemTwo representedObject] pointerValue], &model);
159 159
160 EXPECT_TRUE([[[menu menu] itemAtIndex:3] isSeparatorItem]); 160 EXPECT_TRUE([[[menu menu] itemAtIndex:3] isSeparatorItem]);
161 } 161 }
162 162
163 TEST_F(MenuControllerTest, Submenus) { 163 TEST_F(MenuControllerTest, Submenus) {
164 Delegate delegate; 164 Delegate delegate;
165 SimpleMenuModel model(&delegate); 165 SimpleMenuModel model(&delegate);
166 model.AddItem(1, ASCIIToUTF16("one")); 166 model.AddItem(1, ASCIIToUTF16("one"));
167 SimpleMenuModel submodel(&delegate); 167 SimpleMenuModel submodel(&delegate);
168 submodel.AddItem(2, ASCIIToUTF16("sub-one")); 168 submodel.AddItem(2, ASCIIToUTF16("sub-one"));
169 submodel.AddItem(3, ASCIIToUTF16("sub-two")); 169 submodel.AddItem(3, ASCIIToUTF16("sub-two"));
170 submodel.AddItem(4, ASCIIToUTF16("sub-three")); 170 submodel.AddItem(4, ASCIIToUTF16("sub-three"));
171 model.AddSubMenuWithStringId(5, kTestLabelResourceId, &submodel); 171 model.AddSubMenuWithStringId(5, kTestLabelResourceId, &submodel);
172 model.AddItem(6, ASCIIToUTF16("three")); 172 model.AddItem(6, ASCIIToUTF16("three"));
173 173
174 scoped_nsobject<MenuController> menu( 174 base::scoped_nsobject<MenuController> menu(
175 [[MenuController alloc] initWithModel:&model useWithPopUpButtonCell:NO]); 175 [[MenuController alloc] initWithModel:&model useWithPopUpButtonCell:NO]);
176 EXPECT_EQ([[menu menu] numberOfItems], 3); 176 EXPECT_EQ([[menu menu] numberOfItems], 3);
177 177
178 // Inspect the submenu to ensure it has correct properties. 178 // Inspect the submenu to ensure it has correct properties.
179 NSMenu* submenu = [[[menu menu] itemAtIndex:1] submenu]; 179 NSMenu* submenu = [[[menu menu] itemAtIndex:1] submenu];
180 EXPECT_TRUE(submenu); 180 EXPECT_TRUE(submenu);
181 EXPECT_EQ([submenu numberOfItems], 3); 181 EXPECT_EQ([submenu numberOfItems], 3);
182 182
183 // Inspect one of the items to make sure it has the correct model as its 183 // Inspect one of the items to make sure it has the correct model as its
184 // represented object and the proper tag. 184 // represented object and the proper tag.
(...skipping 12 matching lines...) Expand all
197 EXPECT_EQ([[item representedObject] pointerValue], &model); 197 EXPECT_EQ([[item representedObject] pointerValue], &model);
198 } 198 }
199 199
200 TEST_F(MenuControllerTest, EmptySubmenu) { 200 TEST_F(MenuControllerTest, EmptySubmenu) {
201 Delegate delegate; 201 Delegate delegate;
202 SimpleMenuModel model(&delegate); 202 SimpleMenuModel model(&delegate);
203 model.AddItem(1, ASCIIToUTF16("one")); 203 model.AddItem(1, ASCIIToUTF16("one"));
204 SimpleMenuModel submodel(&delegate); 204 SimpleMenuModel submodel(&delegate);
205 model.AddSubMenuWithStringId(2, kTestLabelResourceId, &submodel); 205 model.AddSubMenuWithStringId(2, kTestLabelResourceId, &submodel);
206 206
207 scoped_nsobject<MenuController> menu( 207 base::scoped_nsobject<MenuController> menu(
208 [[MenuController alloc] initWithModel:&model useWithPopUpButtonCell:NO]); 208 [[MenuController alloc] initWithModel:&model useWithPopUpButtonCell:NO]);
209 EXPECT_EQ([[menu menu] numberOfItems], 2); 209 EXPECT_EQ([[menu menu] numberOfItems], 2);
210 } 210 }
211 211
212 TEST_F(MenuControllerTest, PopUpButton) { 212 TEST_F(MenuControllerTest, PopUpButton) {
213 Delegate delegate; 213 Delegate delegate;
214 SimpleMenuModel model(&delegate); 214 SimpleMenuModel model(&delegate);
215 model.AddItem(1, ASCIIToUTF16("one")); 215 model.AddItem(1, ASCIIToUTF16("one"));
216 model.AddItem(2, ASCIIToUTF16("two")); 216 model.AddItem(2, ASCIIToUTF16("two"));
217 model.AddItem(3, ASCIIToUTF16("three")); 217 model.AddItem(3, ASCIIToUTF16("three"));
218 218
219 // Menu should have an extra item inserted at position 0 that has an empty 219 // Menu should have an extra item inserted at position 0 that has an empty
220 // title. 220 // title.
221 scoped_nsobject<MenuController> menu( 221 base::scoped_nsobject<MenuController> menu(
222 [[MenuController alloc] initWithModel:&model useWithPopUpButtonCell:YES]); 222 [[MenuController alloc] initWithModel:&model useWithPopUpButtonCell:YES]);
223 EXPECT_EQ([[menu menu] numberOfItems], 4); 223 EXPECT_EQ([[menu menu] numberOfItems], 4);
224 EXPECT_EQ(base::SysNSStringToUTF16([[[menu menu] itemAtIndex:0] title]), 224 EXPECT_EQ(base::SysNSStringToUTF16([[[menu menu] itemAtIndex:0] title]),
225 string16()); 225 string16());
226 226
227 // Make sure the tags are still correct (the index no longer matches the tag). 227 // Make sure the tags are still correct (the index no longer matches the tag).
228 NSMenuItem* itemTwo = [[menu menu] itemAtIndex:2]; 228 NSMenuItem* itemTwo = [[menu menu] itemAtIndex:2];
229 EXPECT_EQ([itemTwo tag], 1); 229 EXPECT_EQ([itemTwo tag], 1);
230 } 230 }
231 231
232 TEST_F(MenuControllerTest, Execute) { 232 TEST_F(MenuControllerTest, Execute) {
233 Delegate delegate; 233 Delegate delegate;
234 SimpleMenuModel model(&delegate); 234 SimpleMenuModel model(&delegate);
235 model.AddItem(1, ASCIIToUTF16("one")); 235 model.AddItem(1, ASCIIToUTF16("one"));
236 scoped_nsobject<MenuController> menu( 236 base::scoped_nsobject<MenuController> menu(
237 [[MenuController alloc] initWithModel:&model useWithPopUpButtonCell:NO]); 237 [[MenuController alloc] initWithModel:&model useWithPopUpButtonCell:NO]);
238 EXPECT_EQ([[menu menu] numberOfItems], 1); 238 EXPECT_EQ([[menu menu] numberOfItems], 1);
239 239
240 // Fake selecting the menu item, we expect the delegate to be told to execute 240 // Fake selecting the menu item, we expect the delegate to be told to execute
241 // a command. 241 // a command.
242 NSMenuItem* item = [[menu menu] itemAtIndex:0]; 242 NSMenuItem* item = [[menu menu] itemAtIndex:0];
243 [[item target] performSelector:[item action] withObject:item]; 243 [[item target] performSelector:[item action] withObject:item];
244 EXPECT_EQ(delegate.execute_count_, 1); 244 EXPECT_EQ(delegate.execute_count_, 1);
245 } 245 }
246 246
247 void Validate(MenuController* controller, NSMenu* menu) { 247 void Validate(MenuController* controller, NSMenu* menu) {
248 for (int i = 0; i < [menu numberOfItems]; ++i) { 248 for (int i = 0; i < [menu numberOfItems]; ++i) {
249 NSMenuItem* item = [menu itemAtIndex:i]; 249 NSMenuItem* item = [menu itemAtIndex:i];
250 [controller validateUserInterfaceItem:item]; 250 [controller validateUserInterfaceItem:item];
251 if ([item hasSubmenu]) 251 if ([item hasSubmenu])
252 Validate(controller, [item submenu]); 252 Validate(controller, [item submenu]);
253 } 253 }
254 } 254 }
255 255
256 TEST_F(MenuControllerTest, Validate) { 256 TEST_F(MenuControllerTest, Validate) {
257 Delegate delegate; 257 Delegate delegate;
258 SimpleMenuModel model(&delegate); 258 SimpleMenuModel model(&delegate);
259 model.AddItem(1, ASCIIToUTF16("one")); 259 model.AddItem(1, ASCIIToUTF16("one"));
260 model.AddItem(2, ASCIIToUTF16("two")); 260 model.AddItem(2, ASCIIToUTF16("two"));
261 SimpleMenuModel submodel(&delegate); 261 SimpleMenuModel submodel(&delegate);
262 submodel.AddItem(2, ASCIIToUTF16("sub-one")); 262 submodel.AddItem(2, ASCIIToUTF16("sub-one"));
263 model.AddSubMenuWithStringId(3, kTestLabelResourceId, &submodel); 263 model.AddSubMenuWithStringId(3, kTestLabelResourceId, &submodel);
264 264
265 scoped_nsobject<MenuController> menu( 265 base::scoped_nsobject<MenuController> menu(
266 [[MenuController alloc] initWithModel:&model useWithPopUpButtonCell:NO]); 266 [[MenuController alloc] initWithModel:&model useWithPopUpButtonCell:NO]);
267 EXPECT_EQ([[menu menu] numberOfItems], 3); 267 EXPECT_EQ([[menu menu] numberOfItems], 3);
268 268
269 Validate(menu.get(), [menu menu]); 269 Validate(menu.get(), [menu menu]);
270 } 270 }
271 271
272 // Tests that items which have a font set actually use that font. 272 // Tests that items which have a font set actually use that font.
273 TEST_F(MenuControllerTest, LabelFont) { 273 TEST_F(MenuControllerTest, LabelFont) {
274 Delegate delegate; 274 Delegate delegate;
275 gfx::Font bold = (gfx::Font()).DeriveFont(0, gfx::Font::BOLD); 275 gfx::Font bold = (gfx::Font()).DeriveFont(0, gfx::Font::BOLD);
276 FontMenuModel model(&delegate, &bold, 0); 276 FontMenuModel model(&delegate, &bold, 0);
277 model.AddItem(1, ASCIIToUTF16("one")); 277 model.AddItem(1, ASCIIToUTF16("one"));
278 model.AddItem(2, ASCIIToUTF16("two")); 278 model.AddItem(2, ASCIIToUTF16("two"));
279 279
280 scoped_nsobject<MenuController> menu( 280 base::scoped_nsobject<MenuController> menu(
281 [[MenuController alloc] initWithModel:&model useWithPopUpButtonCell:NO]); 281 [[MenuController alloc] initWithModel:&model useWithPopUpButtonCell:NO]);
282 EXPECT_EQ([[menu menu] numberOfItems], 2); 282 EXPECT_EQ([[menu menu] numberOfItems], 2);
283 283
284 Validate(menu.get(), [menu menu]); 284 Validate(menu.get(), [menu menu]);
285 285
286 EXPECT_TRUE([[[menu menu] itemAtIndex:0] attributedTitle] != nil); 286 EXPECT_TRUE([[[menu menu] itemAtIndex:0] attributedTitle] != nil);
287 EXPECT_TRUE([[[menu menu] itemAtIndex:1] attributedTitle] == nil); 287 EXPECT_TRUE([[[menu menu] itemAtIndex:1] attributedTitle] == nil);
288 } 288 }
289 289
290 TEST_F(MenuControllerTest, DefaultInitializer) { 290 TEST_F(MenuControllerTest, DefaultInitializer) {
291 Delegate delegate; 291 Delegate delegate;
292 SimpleMenuModel model(&delegate); 292 SimpleMenuModel model(&delegate);
293 model.AddItem(1, ASCIIToUTF16("one")); 293 model.AddItem(1, ASCIIToUTF16("one"));
294 model.AddItem(2, ASCIIToUTF16("two")); 294 model.AddItem(2, ASCIIToUTF16("two"));
295 model.AddItem(3, ASCIIToUTF16("three")); 295 model.AddItem(3, ASCIIToUTF16("three"));
296 296
297 scoped_nsobject<MenuController> menu([[MenuController alloc] init]); 297 base::scoped_nsobject<MenuController> menu([[MenuController alloc] init]);
298 EXPECT_FALSE([menu menu]); 298 EXPECT_FALSE([menu menu]);
299 299
300 [menu setModel:&model]; 300 [menu setModel:&model];
301 [menu setUseWithPopUpButtonCell:NO]; 301 [menu setUseWithPopUpButtonCell:NO];
302 EXPECT_TRUE([menu menu]); 302 EXPECT_TRUE([menu menu]);
303 EXPECT_EQ(3, [[menu menu] numberOfItems]); 303 EXPECT_EQ(3, [[menu menu] numberOfItems]);
304 304
305 // Check immutability. 305 // Check immutability.
306 model.AddItem(4, ASCIIToUTF16("four")); 306 model.AddItem(4, ASCIIToUTF16("four"));
307 EXPECT_EQ(3, [[menu menu] numberOfItems]); 307 EXPECT_EQ(3, [[menu menu] numberOfItems]);
308 } 308 }
309 309
310 // Test that menus with dynamic labels actually get updated. 310 // Test that menus with dynamic labels actually get updated.
311 TEST_F(MenuControllerTest, Dynamic) { 311 TEST_F(MenuControllerTest, Dynamic) {
312 DynamicDelegate delegate; 312 DynamicDelegate delegate;
313 313
314 // Create a menu containing a single item whose label is "initial" and who has 314 // Create a menu containing a single item whose label is "initial" and who has
315 // no icon. 315 // no icon.
316 string16 initial = ASCIIToUTF16("initial"); 316 string16 initial = ASCIIToUTF16("initial");
317 delegate.SetDynamicLabel(initial); 317 delegate.SetDynamicLabel(initial);
318 SimpleMenuModel model(&delegate); 318 SimpleMenuModel model(&delegate);
319 model.AddItem(1, ASCIIToUTF16("foo")); 319 model.AddItem(1, ASCIIToUTF16("foo"));
320 scoped_nsobject<MenuController> menu( 320 base::scoped_nsobject<MenuController> menu(
321 [[MenuController alloc] initWithModel:&model useWithPopUpButtonCell:NO]); 321 [[MenuController alloc] initWithModel:&model useWithPopUpButtonCell:NO]);
322 EXPECT_EQ([[menu menu] numberOfItems], 1); 322 EXPECT_EQ([[menu menu] numberOfItems], 1);
323 // Validate() simulates opening the menu - the item label/icon should be 323 // Validate() simulates opening the menu - the item label/icon should be
324 // initialized after this so we can validate the menu contents. 324 // initialized after this so we can validate the menu contents.
325 Validate(menu.get(), [menu menu]); 325 Validate(menu.get(), [menu menu]);
326 NSMenuItem* item = [[menu menu] itemAtIndex:0]; 326 NSMenuItem* item = [[menu menu] itemAtIndex:0];
327 // Item should have the "initial" label and no icon. 327 // Item should have the "initial" label and no icon.
328 EXPECT_EQ(initial, base::SysNSStringToUTF16([item title])); 328 EXPECT_EQ(initial, base::SysNSStringToUTF16([item title]));
329 EXPECT_EQ(nil, [item image]); 329 EXPECT_EQ(nil, [item image]);
330 330
(...skipping 21 matching lines...) Expand all
352 base::MessageLoop message_loop(base::MessageLoop::TYPE_UI); 352 base::MessageLoop message_loop(base::MessageLoop::TYPE_UI);
353 353
354 // Create the model. 354 // Create the model.
355 Delegate delegate; 355 Delegate delegate;
356 SimpleMenuModel model(&delegate); 356 SimpleMenuModel model(&delegate);
357 model.AddItem(1, ASCIIToUTF16("allays")); 357 model.AddItem(1, ASCIIToUTF16("allays"));
358 model.AddItem(2, ASCIIToUTF16("i")); 358 model.AddItem(2, ASCIIToUTF16("i"));
359 model.AddItem(3, ASCIIToUTF16("bf")); 359 model.AddItem(3, ASCIIToUTF16("bf"));
360 360
361 // Create the controller. 361 // Create the controller.
362 scoped_nsobject<MenuController> menu( 362 base::scoped_nsobject<MenuController> menu(
363 [[MenuController alloc] initWithModel:&model useWithPopUpButtonCell:NO]); 363 [[MenuController alloc] initWithModel:&model useWithPopUpButtonCell:NO]);
364 delegate.menu_to_close_ = [menu menu]; 364 delegate.menu_to_close_ = [menu menu];
365 365
366 EXPECT_FALSE([menu isMenuOpen]); 366 EXPECT_FALSE([menu isMenuOpen]);
367 367
368 // In the event tracking run loop mode of the menu, verify that the controller 368 // In the event tracking run loop mode of the menu, verify that the controller
369 // resports the menu as open. 369 // resports the menu as open.
370 CFRunLoopPerformBlock(CFRunLoopGetCurrent(), NSEventTrackingRunLoopMode, ^{ 370 CFRunLoopPerformBlock(CFRunLoopGetCurrent(), NSEventTrackingRunLoopMode, ^{
371 EXPECT_TRUE([menu isMenuOpen]); 371 EXPECT_TRUE([menu isMenuOpen]);
372 }); 372 });
(...skipping 17 matching lines...) Expand all
390 // Pump the task that notifies the delegate. 390 // Pump the task that notifies the delegate.
391 message_loop.RunUntilIdle(); 391 message_loop.RunUntilIdle();
392 392
393 // Expect that the delegate got notified properly. 393 // Expect that the delegate got notified properly.
394 EXPECT_TRUE(delegate.did_close_); 394 EXPECT_TRUE(delegate.did_close_);
395 } 395 }
396 396
397 } // namespace 397 } // namespace
398 398
399 } // namespace ui 399 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698