1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #import "ui/views/controls/menu/menu_runner_impl_cocoa.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "ui/base/models/simple_menu_model.h"
9 #include "ui/events/event_utils.h"
10 #include "ui/views/test/views_test_base.h"
15 class MenuRunnerCocoaTest : public ViewsTestBase {
17 MenuRunnerCocoaTest() : runner_(NULL) {}
18 ~MenuRunnerCocoaTest() override {}
20 void SetUp() override {
21 ViewsTestBase::SetUp();
23 menu_.reset(new ui::SimpleMenuModel(NULL));
24 menu_->AddItem(0, base::ASCIIToUTF16("Menu Item"));
26 runner_ = new internal::MenuRunnerImplCocoa(menu_.get());
27 EXPECT_FALSE(runner_->IsRunning());
30 void TearDown() override {
36 ViewsTestBase::TearDown();
39 // Runs the menu after scheduling |block| on the run loop.
40 MenuRunner::RunResult RunMenu(dispatch_block_t block) {
41 CFRunLoopPerformBlock(CFRunLoopGetCurrent(), kCFRunLoopCommonModes, ^{
42 EXPECT_TRUE(runner_->IsRunning());
45 return runner_->RunMenuAt(
46 NULL, NULL, gfx::Rect(), MENU_ANCHOR_TOPLEFT, MenuRunner::CONTEXT_MENU);
50 scoped_ptr<ui::SimpleMenuModel> menu_;
51 internal::MenuRunnerImplCocoa* runner_;
54 DISALLOW_COPY_AND_ASSIGN(MenuRunnerCocoaTest);
57 TEST_F(MenuRunnerCocoaTest, RunMenuAndCancel) {
58 base::TimeDelta min_time = ui::EventTimeForNow();
60 MenuRunner::RunResult result = RunMenu(^{
62 EXPECT_FALSE(runner_->IsRunning());
65 EXPECT_EQ(MenuRunner::NORMAL_EXIT, result);
66 EXPECT_FALSE(runner_->IsRunning());
68 EXPECT_GE(runner_->GetClosingEventTime(), min_time);
69 EXPECT_LE(runner_->GetClosingEventTime(), ui::EventTimeForNow());
73 EXPECT_FALSE(runner_->IsRunning());
76 TEST_F(MenuRunnerCocoaTest, RunMenuAndDelete) {
77 MenuRunner::RunResult result = RunMenu(^{
82 EXPECT_EQ(MenuRunner::MENU_DELETED, result);
85 TEST_F(MenuRunnerCocoaTest, RunMenuTwice) {
86 for (int i = 0; i < 2; ++i) {
87 MenuRunner::RunResult result = RunMenu(^{
90 EXPECT_EQ(MenuRunner::NORMAL_EXIT, result);
91 EXPECT_FALSE(runner_->IsRunning());
95 TEST_F(MenuRunnerCocoaTest, CancelWithoutRunning) {
97 EXPECT_FALSE(runner_->IsRunning());
98 EXPECT_EQ(base::TimeDelta(), runner_->GetClosingEventTime());
101 TEST_F(MenuRunnerCocoaTest, DeleteWithoutRunning) {