Chromium Code Reviews| Index: ui/views/controls/menu/menu_runner_cocoa_unittest.mm |
| diff --git a/ui/views/controls/menu/menu_runner_cocoa_unittest.mm b/ui/views/controls/menu/menu_runner_cocoa_unittest.mm |
| index d25a6628b47db5294dba95b81c3816386746ed48..5880623a0abb8c569881b1b6d53521d49bb76cff 100644 |
| --- a/ui/views/controls/menu/menu_runner_cocoa_unittest.mm |
| +++ b/ui/views/controls/menu/menu_runner_cocoa_unittest.mm |
| @@ -9,10 +9,52 @@ |
| #include "base/macros.h" |
| #include "base/strings/utf_string_conversions.h" |
| #import "testing/gtest_mac.h" |
| +#import "ui/base/cocoa/menu_controller.h" |
| #include "ui/base/models/simple_menu_model.h" |
| #include "ui/events/event_utils.h" |
| #include "ui/views/test/views_test_base.h" |
| +// A helper class to handle menu open notifications. |
| +@interface MenuOpenWatcher : NSObject { |
| + dispatch_block_t openCallback_; |
|
tapted
2016/03/24 02:10:36
nit: @private
|
| +} |
| + |
| +// Method to handle menu open notification. |
| +- (void)menuWillOpen:(NSNotification*)notification; |
| + |
| +// Block to be invoked on menu open notification. Weak. Clients must ensure that |
| +// it remains in a valid state. |
| +@property(nonatomic, assign) dispatch_block_t openCallback; |
| + |
| +@end |
| + |
| +@implementation MenuOpenWatcher |
| + |
| +@synthesize openCallback = openCallback_; |
| + |
| +- (id)init { |
| + if (self = [super init]) { |
|
tapted
2016/03/24 02:10:36
nit: extra parens around assignment used as condit
|
| + [[NSNotificationCenter defaultCenter] |
| + addObserver:self |
| + selector:@selector(menuWillOpen:) |
| + name:kMenuControllerMenuWillOpenNotification |
| + object:nil]; |
| + } |
| + return self; |
| +} |
| + |
| +- (void)dealloc { |
| + [[NSNotificationCenter defaultCenter] removeObserver:self]; |
| + [super dealloc]; |
| +} |
| + |
| +- (void)menuWillOpen:(NSNotification*)notification { |
| + if (openCallback_) |
| + openCallback_(); |
| +} |
| + |
| +@end |
| + |
| namespace views { |
| namespace test { |
| namespace { |
| @@ -77,6 +119,7 @@ class MenuRunnerCocoaTest : public ViewsTestBase { |
| runner_ = new internal::MenuRunnerImplCocoa(menu_.get()); |
| EXPECT_FALSE(runner_->IsRunning()); |
| + menu_watcher_.reset([[MenuOpenWatcher alloc] init]); |
| } |
| void TearDown() override { |
| @@ -91,12 +134,15 @@ class MenuRunnerCocoaTest : public ViewsTestBase { |
| // Runs the menu after scheduling |block| on the run loop. |
| MenuRunner::RunResult RunMenu(dispatch_block_t block) { |
| - CFRunLoopPerformBlock(CFRunLoopGetCurrent(), kCFRunLoopCommonModes, ^{ |
| - EXPECT_TRUE(runner_->IsRunning()); |
| - block(); |
| - }); |
| - return runner_->RunMenuAt(parent_, NULL, gfx::Rect(), MENU_ANCHOR_TOPLEFT, |
| - MenuRunner::CONTEXT_MENU); |
| + [menu_watcher_ setOpenCallback:^{ |
| + EXPECT_TRUE(runner_->IsRunning()); |
| + block(); |
| + }]; |
| + MenuRunner::RunResult result = |
| + runner_->RunMenuAt(parent_, NULL, gfx::Rect(), MENU_ANCHOR_TOPLEFT, |
|
tapted
2016/03/24 02:10:36
nit (while you're here) NULL -> nullptr
karandeepb
2016/04/06 05:52:48
Done.
|
| + MenuRunner::CONTEXT_MENU); |
| + [menu_watcher_ setOpenCallback:nil]; |
| + return result; |
| } |
| // Runs then cancels a combobox menu and captures the frame of the anchoring |
| @@ -107,22 +153,26 @@ class MenuRunnerCocoaTest : public ViewsTestBase { |
| // Should be one child (the compositor layer) before showing, and it should |
| // go up by one (the anchor view) while the menu is shown. |
| EXPECT_EQ(1u, [[parent_->GetNativeView() subviews] count]); |
| - CFRunLoopPerformBlock(CFRunLoopGetCurrent(), kCFRunLoopCommonModes, ^{ |
| + |
| + [menu_watcher_ setOpenCallback:^{ |
| NSArray* subviews = [parent_->GetNativeView() subviews]; |
| EXPECT_EQ(2u, [subviews count]); |
| last_anchor_frame_ = [[subviews objectAtIndex:1] frame]; |
| runner_->Cancel(); |
| - }); |
| + }]; |
| + |
| MenuRunner::RunResult result = runner_->RunMenuAt( |
| parent_, nullptr, anchor, MENU_ANCHOR_TOPLEFT, MenuRunner::COMBOBOX); |
| // Ensure the anchor view is removed. |
| EXPECT_EQ(1u, [[parent_->GetNativeView() subviews] count]); |
| + [menu_watcher_ setOpenCallback:nil]; |
| return result; |
| } |
| protected: |
| scoped_ptr<TestModel> menu_; |
| + base::scoped_nsobject<MenuOpenWatcher> menu_watcher_; |
| internal::MenuRunnerImplCocoa* runner_ = nullptr; |
| views::Widget* parent_ = nullptr; |
| NSRect last_anchor_frame_ = NSZeroRect; |