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

Unified Diff: ui/views/controls/menu/menu_runner_cocoa_unittest.mm

Issue 1829603002: MacViews: Fix flaky MenuRunnerCocoaTests. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address review comments. Created 4 years, 8 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..73e02eca385483f7d2994d045400cde586568f53 100644
--- a/ui/views/controls/menu/menu_runner_cocoa_unittest.mm
+++ b/ui/views/controls/menu/menu_runner_cocoa_unittest.mm
@@ -23,6 +23,10 @@ class TestModel : public ui::SimpleMenuModel {
void set_checked_command(int command) { checked_command_ = command; }
+ void set_menu_open_callback(const base::Closure& callback) {
+ menu_open_callback_ = callback;
+ }
+
private:
class Delegate : public ui::SimpleMenuModel::Delegate {
public:
@@ -37,6 +41,10 @@ class TestModel : public ui::SimpleMenuModel {
}
void ExecuteCommand(int command_id, int event_flags) override {}
+ void MenuWillShow(SimpleMenuModel* source) override {
+ model_->menu_open_callback_.Run();
+ }
+
private:
TestModel* model_;
@@ -46,6 +54,7 @@ class TestModel : public ui::SimpleMenuModel {
private:
int checked_command_ = -1;
Delegate delegate_;
+ base::Closure menu_open_callback_;
DISALLOW_COPY_AND_ASSIGN(TestModel);
};
@@ -89,14 +98,13 @@ class MenuRunnerCocoaTest : public ViewsTestBase {
ViewsTestBase::TearDown();
}
- // 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);
+ // Runs the menu after registering |callback| as the menu open callback.
+ MenuRunner::RunResult RunMenu(const base::Closure& callback) {
+ menu_->set_menu_open_callback(
+ base::Bind(&MenuRunnerCocoaTest::RunMenuWrapperCallback,
+ base::Unretained(this), callback));
+ return runner_->RunMenuAt(parent_, nullptr, gfx::Rect(),
+ MENU_ANCHOR_TOPLEFT, MenuRunner::CONTEXT_MENU);
}
// Runs then cancels a combobox menu and captures the frame of the anchoring
@@ -107,12 +115,10 @@ 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, ^{
- NSArray* subviews = [parent_->GetNativeView() subviews];
- EXPECT_EQ(2u, [subviews count]);
- last_anchor_frame_ = [[subviews objectAtIndex:1] frame];
- runner_->Cancel();
- });
+
+ menu_->set_menu_open_callback(base::Bind(
+ &MenuRunnerCocoaTest::RunMenuAtCallback, base::Unretained(this)));
+
MenuRunner::RunResult result = runner_->RunMenuAt(
parent_, nullptr, anchor, MENU_ANCHOR_TOPLEFT, MenuRunner::COMBOBOX);
@@ -121,6 +127,16 @@ class MenuRunnerCocoaTest : public ViewsTestBase {
return result;
}
+ void MenuCancelCallback() {
+ runner_->Cancel();
+ EXPECT_FALSE(runner_->IsRunning());
+ }
+
+ void MenuDeleteCallback() {
+ runner_->Release();
+ runner_ = nullptr;
+ }
+
protected:
scoped_ptr<TestModel> menu_;
internal::MenuRunnerImplCocoa* runner_ = nullptr;
@@ -128,16 +144,26 @@ class MenuRunnerCocoaTest : public ViewsTestBase {
NSRect last_anchor_frame_ = NSZeroRect;
private:
+ void RunMenuWrapperCallback(const base::Closure& callback) {
+ EXPECT_TRUE(runner_->IsRunning());
+ callback.Run();
+ }
+
+ void RunMenuAtCallback() {
+ NSArray* subviews = [parent_->GetNativeView() subviews];
+ EXPECT_EQ(2u, [subviews count]);
+ last_anchor_frame_ = [[subviews objectAtIndex:1] frame];
+ runner_->Cancel();
+ }
+
DISALLOW_COPY_AND_ASSIGN(MenuRunnerCocoaTest);
};
TEST_F(MenuRunnerCocoaTest, RunMenuAndCancel) {
base::TimeDelta min_time = ui::EventTimeForNow();
- MenuRunner::RunResult result = RunMenu(^{
- runner_->Cancel();
- EXPECT_FALSE(runner_->IsRunning());
- });
+ MenuRunner::RunResult result = RunMenu(base::Bind(
+ &MenuRunnerCocoaTest::MenuCancelCallback, base::Unretained(this)));
EXPECT_EQ(MenuRunner::NORMAL_EXIT, result);
EXPECT_FALSE(runner_->IsRunning());
@@ -151,19 +177,15 @@ TEST_F(MenuRunnerCocoaTest, RunMenuAndCancel) {
}
TEST_F(MenuRunnerCocoaTest, RunMenuAndDelete) {
- MenuRunner::RunResult result = RunMenu(^{
- runner_->Release();
- runner_ = NULL;
- });
-
+ MenuRunner::RunResult result = RunMenu(base::Bind(
+ &MenuRunnerCocoaTest::MenuDeleteCallback, base::Unretained(this)));
EXPECT_EQ(MenuRunner::MENU_DELETED, result);
}
TEST_F(MenuRunnerCocoaTest, RunMenuTwice) {
for (int i = 0; i < 2; ++i) {
- MenuRunner::RunResult result = RunMenu(^{
- runner_->Cancel();
- });
+ MenuRunner::RunResult result = RunMenu(base::Bind(
+ &MenuRunnerCocoaTest::MenuCancelCallback, base::Unretained(this)));
EXPECT_EQ(MenuRunner::NORMAL_EXIT, result);
EXPECT_FALSE(runner_->IsRunning());
}
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698