File: menu_controller_unittest.mm

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (661 lines) | stat: -rw-r--r-- 23,972 bytes parent folder | download | duplicates (4)
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import "ui/menus/cocoa/menu_controller.h"

#import <Cocoa/Cocoa.h>

#include "base/memory/raw_ptr.h"
#include "base/memory/raw_ptr_exclusion.h"
#include "base/run_loop.h"
#include "base/strings/sys_string_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/task_environment.h"
#import "testing/gtest_mac.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/base/l10n/l10n_util_mac.h"
#include "ui/base/models/image_model.h"
#include "ui/base/resource/resource_bundle.h"
#import "ui/base/test/cocoa_helper.h"
#include "ui/color/color_provider.h"
#include "ui/events/test/cocoa_test_event_utils.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_unittest_util.h"
#include "ui/menus/simple_menu_model.h"
#include "ui/strings/grit/ui_strings.h"

@interface WatchedLifetimeMenuController : MenuControllerCocoa
@property(assign, nonatomic) BOOL* deallocCalled;
@end

@implementation WatchedLifetimeMenuController {
  // This field is not a raw_ptr<> because it requires @property rewrite.
  RAW_PTR_EXCLUSION BOOL* _deallocCalled;
}

@synthesize deallocCalled = _deallocCalled;

- (void)dealloc {
  *_deallocCalled = YES;
}

@end

namespace ui {

namespace {

const int kTestLabelResourceId = IDS_APP_SCROLLBAR_CXMENU_SCROLLHERE;

class MenuControllerTest : public CocoaTest {};

class TestSimpleMenuModelVisibility : public SimpleMenuModel {
 public:
  explicit TestSimpleMenuModelVisibility(SimpleMenuModel::Delegate* delegate)
      : SimpleMenuModel(delegate) {}

  TestSimpleMenuModelVisibility(const TestSimpleMenuModelVisibility&) = delete;
  TestSimpleMenuModelVisibility& operator=(
      const TestSimpleMenuModelVisibility&) = delete;

  // SimpleMenuModel:
  bool IsVisibleAt(size_t index) const override {
    return items_[ValidateItemIndex(index)].visible;
  }

  void SetVisibility(int command_id, bool visible) {
    std::optional<size_t> index =
        SimpleMenuModel::GetIndexOfCommandId(command_id);
    items_[ValidateItemIndex(index.value())].visible = visible;
  }

  void AddItem(int command_id, const std::u16string& label) {
    SimpleMenuModel::AddItem(command_id, label);
    items_.push_back({true, command_id});
  }

  void AddSubMenuWithStringId(int command_id, int string_id, MenuModel* model) {
    SimpleMenuModel::AddSubMenuWithStringId(command_id, string_id, model);
    items_.push_back({true, command_id});
  }

 private:
  struct Item {
    bool visible;
    int command_id;
  };

  int ValidateItemIndex(size_t index) const {
    CHECK_LT(index, items_.size());
    return index;
  }

  std::vector<Item> items_;
};

// A menu delegate that counts the number of times certain things are called
// to make sure things are hooked up properly.
class Delegate : public SimpleMenuModel::Delegate {
 public:
  Delegate() = default;

  Delegate(const Delegate&) = delete;
  Delegate& operator=(const Delegate&) = delete;

  bool IsCommandIdChecked(int command_id) const override { return false; }
  bool IsCommandIdEnabled(int command_id) const override {
    ++enable_count_;
    return true;
  }
  void ExecuteCommand(int command_id, int event_flags) override {
    ++execute_count_;
  }

  void OnMenuWillShow(SimpleMenuModel* /*source*/) override {
    EXPECT_FALSE(did_show_);
    EXPECT_FALSE(did_close_);
    did_show_ = true;
    if (auto_close_) {
      NSArray* modes = @[ NSEventTrackingRunLoopMode, NSDefaultRunLoopMode ];
      [menu_to_close_ performSelector:@selector(cancelTracking)
                           withObject:nil
                           afterDelay:0.1
                              inModes:modes];
    }
  }

  void MenuClosed(SimpleMenuModel* /*source*/) override {
    EXPECT_TRUE(did_show_);
    EXPECT_FALSE(did_close_);
    DCHECK(!did_close_);
    did_close_ = true;
  }

  int execute_count_ = 0;
  mutable int enable_count_ = 0;
  // The menu on which to call |-cancelTracking| after a short delay in
  // OnMenuWillShow.
  NSMenu* menu_to_close_ = nil;
  bool did_show_ = false;
  bool did_close_ = false;
  bool auto_close_ = true;
};

// Just like Delegate, except the items are treated as "dynamic" so updates to
// the label/icon in the model are reflected in the menu.
class DynamicDelegate : public Delegate {
 public:
  DynamicDelegate() = default;
  bool IsItemForCommandIdDynamic(int command_id) const override { return true; }
  std::u16string GetLabelForCommandId(int command_id) const override {
    return label_;
  }
  ui::ImageModel GetIconForCommandId(int command_id) const override {
    return icon_.IsEmpty() ? ui::ImageModel()
                           : ui::ImageModel::FromImage(icon_);
  }
  void SetDynamicLabel(std::u16string label) { label_ = label; }
  void SetDynamicIcon(const gfx::Image& icon) { icon_ = icon; }

 private:
  std::u16string label_;
  gfx::Image icon_;
};

// A SimpleMenuModel::Delegate that owns the MenuControllerCocoa and deletes
// itself when the command is executed.
class OwningDelegate : public Delegate {
 public:
  OwningDelegate(bool* did_delete, BOOL* did_dealloc)
      : did_delete_(did_delete), model_(this) {
    model_.AddItem(1, u"foo");
    controller_ = [[WatchedLifetimeMenuController alloc] initWithModel:&model_
                                                              delegate:nil];
    [controller_ setDeallocCalled:did_dealloc];
  }

  OwningDelegate(const OwningDelegate&) = delete;
  OwningDelegate& operator=(const OwningDelegate&) = delete;

  MenuControllerCocoa* controller() { return controller_; }

  // Delegate:
  void ExecuteCommand(int command_id, int event_flags) override {
    // Although -[MenuControllerCocoa menuDidClose:] has been invoked,
    // SimpleMenuModel always posts a task to call Delegate::MenuClosed(), to
    // ensure it happens after the command. It uses a weak pointer to |model_|,
    // so the task will expire before being run.
    EXPECT_FALSE(did_close_);

    EXPECT_EQ(0, execute_count_);
    Delegate::ExecuteCommand(command_id, event_flags);
    delete this;
  }

 private:
  ~OwningDelegate() override {
    EXPECT_FALSE(*did_delete_);
    *did_delete_ = true;
  }

  raw_ptr<bool> did_delete_;
  SimpleMenuModel model_;
  WatchedLifetimeMenuController* __strong controller_;
};

// Menu model that returns a gfx::FontList object for one of the items in the
// menu.
class FontListMenuModel : public SimpleMenuModel {
 public:
  FontListMenuModel(SimpleMenuModel::Delegate* delegate,
                    const gfx::FontList* font_list,
                    size_t index)
      : SimpleMenuModel(delegate), font_list_(font_list), index_(index) {}
  ~FontListMenuModel() override = default;
  const gfx::FontList* GetLabelFontListAt(size_t index) const override {
    return (index == index_) ? font_list_ : nullptr;
  }

 private:
  raw_ptr<const gfx::FontList> font_list_;
  const size_t index_;
};

TEST_F(MenuControllerTest, EmptyMenu) {
  Delegate delegate;
  SimpleMenuModel model(&delegate);
  MenuControllerCocoa* menu = [[MenuControllerCocoa alloc] initWithModel:&model
                                                                delegate:nil];
  EXPECT_EQ(0, menu.menu.numberOfItems);
}

TEST_F(MenuControllerTest, BasicCreation) {
  Delegate delegate;
  SimpleMenuModel model(&delegate);
  model.AddItem(1, u"one");
  model.AddItem(2, u"two");
  model.AddItem(3, u"three");
  model.AddSeparator(NORMAL_SEPARATOR);
  model.AddItem(4, u"four");
  model.AddItem(5, u"five");

  MenuControllerCocoa* menu = [[MenuControllerCocoa alloc] initWithModel:&model
                                                                delegate:nil];
  EXPECT_EQ(6, menu.menu.numberOfItems);

  // Check the title, tag, and represented object are correct for a random
  // element.
  NSMenuItem* itemTwo = [menu.menu itemAtIndex:2];
  NSString* title = itemTwo.title;
  EXPECT_EQ(u"three", base::SysNSStringToUTF16(title));
  EXPECT_EQ(2, itemTwo.tag);

  EXPECT_TRUE([menu.menu itemAtIndex:3].separatorItem);
}

TEST_F(MenuControllerTest, Submenus) {
  Delegate delegate;
  SimpleMenuModel model(&delegate);
  model.AddItem(1, u"one");
  SimpleMenuModel submodel(&delegate);
  submodel.AddItem(2, u"sub-one");
  submodel.AddItem(3, u"sub-two");
  submodel.AddItem(4, u"sub-three");
  model.AddSubMenuWithStringId(5, kTestLabelResourceId, &submodel);
  model.AddItem(6, u"three");

  MenuControllerCocoa* menu = [[MenuControllerCocoa alloc] initWithModel:&model
                                                                delegate:nil];
  EXPECT_EQ(3, menu.menu.numberOfItems);

  // Inspect the submenu to ensure it has correct properties.
  NSMenuItem* menuItem = [menu.menu itemAtIndex:1];
  EXPECT_TRUE(menuItem.enabled);
  NSMenu* submenu = menuItem.submenu;
  EXPECT_TRUE(submenu);
  EXPECT_EQ(3, submenu.numberOfItems);

  // Inspect one of the items to make sure it has the correct model as its
  // represented object and the proper tag.
  NSMenuItem* submenuItem = [submenu itemAtIndex:1];
  NSString* title = submenuItem.title;
  EXPECT_EQ(u"sub-two", base::SysNSStringToUTF16(title));
  EXPECT_EQ(1, submenuItem.tag);

  // Make sure the item after the submenu is correct and its represented
  // object is back to the top model.
  NSMenuItem* item = [menu.menu itemAtIndex:2];
  title = item.title;
  EXPECT_EQ(u"three", base::SysNSStringToUTF16(title));
  EXPECT_EQ(2, item.tag);
}

TEST_F(MenuControllerTest, EmptySubmenu) {
  Delegate delegate;
  SimpleMenuModel model(&delegate);
  model.AddItem(1, u"one");
  SimpleMenuModel submodel(&delegate);
  model.AddSubMenuWithStringId(2, kTestLabelResourceId, &submodel);

  MenuControllerCocoa* menu = [[MenuControllerCocoa alloc] initWithModel:&model
                                                                delegate:nil];
  EXPECT_EQ(2, menu.menu.numberOfItems);

  // Inspect the submenu to ensure it has one item labeled "(empty)".
  NSMenu* submenu = [menu.menu itemAtIndex:1].submenu;
  EXPECT_TRUE(submenu);
  EXPECT_EQ(1, submenu.numberOfItems);

  EXPECT_NSEQ(@"(empty)", [submenu itemAtIndex:0].title);
}

// Tests that an empty menu item, "(empty)", is added to a submenu that contains
// hidden child items.
TEST_F(MenuControllerTest, EmptySubmenuWhenAllChildItemsAreHidden) {
  Delegate delegate;
  TestSimpleMenuModelVisibility model(&delegate);
  model.AddItem(1, u"one");
  TestSimpleMenuModelVisibility submodel(&delegate);
  // Hide the two child menu items.
  submodel.AddItem(2, u"sub-one");
  submodel.SetVisibility(2, false);
  submodel.AddItem(3, u"sub-two");
  submodel.SetVisibility(3, false);
  model.AddSubMenuWithStringId(4, kTestLabelResourceId, &submodel);

  MenuControllerCocoa* menu = [[MenuControllerCocoa alloc] initWithModel:&model
                                                                delegate:nil];
  EXPECT_EQ(2, menu.menu.numberOfItems);

  // Inspect the submenu to ensure it has one item labeled "(empty)".
  NSMenu* submenu = [menu.menu itemAtIndex:1].submenu;
  EXPECT_TRUE(submenu);
  EXPECT_EQ(1, submenu.numberOfItems);

  EXPECT_NSEQ(@"(empty)", [submenu itemAtIndex:0].title);
}

// Tests hiding a submenu item. If a submenu item with children is set to
// hidden, then the submenu should hide.
TEST_F(MenuControllerTest, HiddenSubmenu) {
  // SimpleMenuModel posts a task that calls Delegate::MenuClosed.
  base::test::SingleThreadTaskEnvironment task_environment(
      base::test::SingleThreadTaskEnvironment::MainThreadType::UI);

  // Create the model.
  Delegate delegate;
  TestSimpleMenuModelVisibility model(&delegate);
  model.AddItem(1, u"one");
  TestSimpleMenuModelVisibility submodel(&delegate);
  submodel.AddItem(2, u"sub-one");
  submodel.AddItem(3, u"sub-two");
  // Set the submenu to be hidden.
  model.AddSubMenuWithStringId(4, kTestLabelResourceId, &submodel);

  model.SetVisibility(4, false);

  // Create the controller.
  MenuControllerCocoa* menu_controller =
      [[MenuControllerCocoa alloc] initWithModel:&model delegate:nil];
  EXPECT_EQ(2, menu_controller.menu.numberOfItems);
  delegate.menu_to_close_ = menu_controller.menu;

  // Show the menu.
  [NSRunLoop.currentRunLoop performInModes:@[ NSEventTrackingRunLoopMode ]
                                     block:^{
                                       EXPECT_TRUE(menu_controller.menuOpen);
                                       // Ensure that the submenu is hidden.
                                       NSMenuItem* item =
                                           [menu_controller.menu itemAtIndex:1];
                                       EXPECT_TRUE(item.hidden);
                                     }];

  // Pop open the menu, which will spin an event-tracking run loop.
  [NSMenu popUpContextMenu:menu_controller.menu
                 withEvent:cocoa_test_event_utils::RightMouseDownAtPoint(
                               NSZeroPoint)
                   forView:test_window().contentView];

  EXPECT_FALSE(menu_controller.menuOpen);

  // Pump the task that notifies the delegate.
  base::RunLoop().RunUntilIdle();

  // Expect that the delegate got notified properly.
  EXPECT_TRUE(delegate.did_close_);
}

TEST_F(MenuControllerTest, DisabledSubmenu) {
  // SimpleMenuModel posts a task that calls Delegate::MenuClosed.
  base::test::SingleThreadTaskEnvironment task_environment(
      base::test::SingleThreadTaskEnvironment::MainThreadType::UI);

  // Create the model.
  Delegate delegate;
  SimpleMenuModel model(&delegate);
  model.AddItem(1, u"one");
  SimpleMenuModel disabled_submodel(&delegate);
  disabled_submodel.AddItem(2, u"disabled_submodel");
  model.AddSubMenuWithStringId(3, kTestLabelResourceId, &disabled_submodel);
  SimpleMenuModel enabled_submodel(&delegate);
  enabled_submodel.AddItem(4, u"enabled_submodel");
  model.AddSubMenuWithStringId(5, kTestLabelResourceId, &enabled_submodel);

  // Disable the first submenu entry.
  model.SetEnabledAt(1, false);

  // Create the controller.
  MenuControllerCocoa* menu_controller =
      [[MenuControllerCocoa alloc] initWithModel:&model delegate:nil];
  delegate.menu_to_close_ = menu_controller.menu;

  // Show the menu.
  [NSRunLoop.currentRunLoop performInModes:@[ NSEventTrackingRunLoopMode ]
                                     block:^{
                                       EXPECT_TRUE(menu_controller.menuOpen);

                                       // Ensure that the disabled submenu is
                                       // disabled.
                                       NSMenuItem* disabled_item =
                                           [menu_controller.menu itemAtIndex:1];
                                       EXPECT_FALSE(disabled_item.enabled);

                                       // Ensure that the enabled submenu is
                                       // enabled.
                                       NSMenuItem* enabled_item =
                                           [menu_controller.menu itemAtIndex:2];
                                       EXPECT_TRUE(enabled_item.enabled);
                                     }];

  // Pop open the menu, which will spin an event-tracking run loop.
  [NSMenu popUpContextMenu:menu_controller.menu
                 withEvent:cocoa_test_event_utils::RightMouseDownAtPoint(
                               NSZeroPoint)
                   forView:test_window().contentView];
  EXPECT_FALSE(menu_controller.menuOpen);

  // Pump the task that notifies the delegate.
  base::RunLoop().RunUntilIdle();
  // Expect that the delegate got notified properly.
  EXPECT_TRUE(delegate.did_close_);
}

TEST_F(MenuControllerTest, Execute) {
  Delegate delegate;
  SimpleMenuModel model(&delegate);
  model.AddItem(1, u"one");
  MenuControllerCocoa* menu = [[MenuControllerCocoa alloc] initWithModel:&model
                                                                delegate:nil];
  EXPECT_EQ(1, menu.menu.numberOfItems);

  // Fake selecting the menu item, we expect the delegate to be told to execute
  // a command.
  NSMenuItem* item = [menu.menu itemAtIndex:0];
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
  [item.target performSelector:item.action withObject:item];
#pragma clang diagnostic pop
  EXPECT_EQ(1, delegate.execute_count_);
}

void Validate(MenuControllerCocoa* controller, NSMenu* menu) {
  for (int i = 0; i < menu.numberOfItems; ++i) {
    NSMenuItem* item = [menu itemAtIndex:i];
    [controller validateUserInterfaceItem:item];
    if (item.hasSubmenu) {
      Validate(controller, item.submenu);
    }
  }
}

TEST_F(MenuControllerTest, Validate) {
  Delegate delegate;
  SimpleMenuModel model(&delegate);
  model.AddItem(1, u"one");
  model.AddItem(2, u"two");
  SimpleMenuModel submodel(&delegate);
  submodel.AddItem(2, u"sub-one");
  model.AddSubMenuWithStringId(3, kTestLabelResourceId, &submodel);

  MenuControllerCocoa* menu = [[MenuControllerCocoa alloc] initWithModel:&model
                                                                delegate:nil];
  EXPECT_EQ(3, menu.menu.numberOfItems);

  Validate(menu, menu.menu);
}

// Tests that items which have a font set actually use that font.
TEST_F(MenuControllerTest, LabelFontList) {
  Delegate delegate;
  const gfx::FontList& bold =
      ResourceBundle::GetSharedInstance().GetFontListForDetails(
          ui::ResourceBundle::FontDetails(std::string(), 0,
                                          gfx::Font::Weight::BOLD));
  FontListMenuModel model(&delegate, &bold, 0);
  model.AddItem(1, u"one");
  model.AddItem(2, u"two");

  MenuControllerCocoa* menu = [[MenuControllerCocoa alloc] initWithModel:&model
                                                                delegate:nil];
  EXPECT_EQ(2, menu.menu.numberOfItems);

  Validate(menu, menu.menu);

  EXPECT_TRUE([menu.menu itemAtIndex:0].attributedTitle != nil);
  EXPECT_TRUE([menu.menu itemAtIndex:1].attributedTitle == nil);
}

// Test that menus with dynamic labels actually get updated.
TEST_F(MenuControllerTest, Dynamic) {
  DynamicDelegate delegate;

  // Create a menu containing a single item whose label is "initial" and who has
  // no icon.
  std::u16string initial = u"initial";
  delegate.SetDynamicLabel(initial);
  SimpleMenuModel model(&delegate);
  model.AddItem(1, u"foo");
  MenuControllerCocoa* menu = [[MenuControllerCocoa alloc] initWithModel:&model
                                                                delegate:nil];
  EXPECT_EQ(1, menu.menu.numberOfItems);
  // Validate() simulates opening the menu - the item label/icon should be
  // initialized after this so we can validate the menu contents.
  Validate(menu, menu.menu);
  NSMenuItem* item = [menu.menu itemAtIndex:0];
  // Item should have the "initial" label and no icon.
  EXPECT_EQ(initial, base::SysNSStringToUTF16(item.title));
  EXPECT_EQ(nil, item.image);

  // Now update the item to have a label of "second" and an icon.
  std::u16string second = u"second";
  delegate.SetDynamicLabel(second);
  const gfx::Image& icon = gfx::test::CreateImage(32, 32);
  delegate.SetDynamicIcon(icon);
  // Simulate opening the menu and validate that the item label + icon changes.
  Validate(menu, menu.menu);
  EXPECT_EQ(second, base::SysNSStringToUTF16(item.title));
  EXPECT_NE(nil, item.image);

  // Now get rid of the icon and make sure it goes away.
  delegate.SetDynamicIcon(gfx::Image());
  Validate(menu, menu.menu);
  EXPECT_EQ(second, base::SysNSStringToUTF16(item.title));
  EXPECT_EQ(nil, item.image);
}

TEST_F(MenuControllerTest, OpenClose) {
  // SimpleMenuModel posts a task that calls Delegate::MenuClosed.
  base::test::SingleThreadTaskEnvironment task_environment(
      base::test::SingleThreadTaskEnvironment::MainThreadType::UI);

  // Create the model.
  Delegate delegate;
  SimpleMenuModel model(&delegate);
  model.AddItem(1, u"allays");
  model.AddItem(2, u"i");
  model.AddItem(3, u"bf");

  // Create the controller.
  MenuControllerCocoa* menu = [[MenuControllerCocoa alloc] initWithModel:&model
                                                                delegate:nil];
  delegate.menu_to_close_ = menu.menu;

  EXPECT_FALSE(menu.menuOpen);

  // In the event tracking run loop mode of the menu, verify that the controller
  // reports the menu as open.
  [NSRunLoop.currentRunLoop performInModes:@[ NSEventTrackingRunLoopMode ]
                                     block:^{
                                       EXPECT_TRUE(menu.menuOpen);
                                     }];

  // Pop open the menu, which will spin an event-tracking run loop.
  [NSMenu popUpContextMenu:menu.menu
                 withEvent:cocoa_test_event_utils::RightMouseDownAtPoint(
                               NSZeroPoint)
                   forView:test_window().contentView];

  EXPECT_FALSE(menu.menuOpen);

  // When control returns back to here, the menu will have finished running its
  // loop and will have closed itself (see Delegate::OnMenuWillShow).
  EXPECT_TRUE(delegate.did_show_);

  // When the menu tells the Model it closed, the Model posts a task to notify
  // the delegate. But since this is a test and there's no running MessageLoop,
  // |did_close_| will remain false until we pump the task manually.
  EXPECT_FALSE(delegate.did_close_);

  // Pump the task that notifies the delegate.
  base::RunLoop().RunUntilIdle();

  // Expect that the delegate got notified properly.
  EXPECT_TRUE(delegate.did_close_);
}

// Tests invoking a menu action on a delegate that immediately releases the
// MenuControllerCocoa and destroys itself. Note this usually needs asan to
// actually crash (before it was fixed).
TEST_F(MenuControllerTest, OwningDelegate) {
  base::test::SingleThreadTaskEnvironment task_environment(
      base::test::SingleThreadTaskEnvironment::MainThreadType::UI);
  bool did_delete = false;
  BOOL did_dealloc = NO;
  OwningDelegate* delegate;
  NSMenuItem* item;

  // The final action is a task posted to the runloop, which drains the
  // autorelease pool, so ensure that happens in the test.
  @autoreleasepool {
    delegate = new OwningDelegate(&did_delete, &did_dealloc);  // Self deleting.
    delegate->auto_close_ = false;

    // Unretained reference to the controller.
    MenuControllerCocoa* controller = delegate->controller();

    item = [controller.menu itemAtIndex:0];
    EXPECT_TRUE(item);

    // Simulate opening the menu and selecting an item. Methods are always
    // invoked by AppKit in the following order.
    [controller menuWillOpen:controller.menu];
    [controller menuDidClose:controller.menu];
  }
  EXPECT_FALSE(did_dealloc);
  EXPECT_FALSE(did_delete);

  // On 10.15+, [NSMenuItem target] indirectly causes an extra
  // retain+autorelease of the target. That avoids bugs caused by the
  // NSMenuItem's action causing destruction of the target, but also causes the
  // NSMenuItem to get cleaned up later than this test expects. Deal with that
  // by creating an explicit autorelease pool here.
  @autoreleasepool {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
    [item.target performSelector:item.action withObject:item];
#pragma clang diagnostic pop
  }
  EXPECT_TRUE(did_dealloc);
  EXPECT_TRUE(did_delete);
}

// Tests that Windows-style ampersand mnemonics are stripped by default, but
// remain if the `MayHaveMnemonics` is false.
TEST_F(MenuControllerTest, Ampersands) {
  Delegate delegate;
  SimpleMenuModel model(&delegate);
  model.AddItem(1, u"&New");
  model.AddItem(2, u"Gin & Tonic");
  model.SetMayHaveMnemonicsAt(1, false);

  MenuControllerCocoa* menu = [[MenuControllerCocoa alloc] initWithModel:&model
                                                                delegate:nil];

  EXPECT_NSEQ([menu.menu itemAtIndex:0].title, @"New");
  EXPECT_NSEQ([menu.menu itemAtIndex:1].title, @"Gin & Tonic");
}

}  // namespace

}  // namespace ui