Reland "Don't refcount tracking id -> slot id mapping."
[chromium-blink-merge.git] / ui / views / controls / menu / menu_runner_cocoa_unittest.mm
blob312fda16477a8918829fcc6b610c19c7bca35dc8
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"
12 namespace views {
13 namespace test {
15 class MenuRunnerCocoaTest : public ViewsTestBase {
16  public:
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());
28   }
30   void TearDown() override {
31     if (runner_) {
32       runner_->Release();
33       runner_ = NULL;
34     }
36     ViewsTestBase::TearDown();
37   }
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());
43         block();
44     });
45     return runner_->RunMenuAt(
46         NULL, NULL, gfx::Rect(), MENU_ANCHOR_TOPLEFT, MenuRunner::CONTEXT_MENU);
47   }
49  protected:
50   scoped_ptr<ui::SimpleMenuModel> menu_;
51   internal::MenuRunnerImplCocoa* runner_;
53  private:
54   DISALLOW_COPY_AND_ASSIGN(MenuRunnerCocoaTest);
57 TEST_F(MenuRunnerCocoaTest, RunMenuAndCancel) {
58   base::TimeDelta min_time = ui::EventTimeForNow();
60   MenuRunner::RunResult result = RunMenu(^{
61       runner_->Cancel();
62       EXPECT_FALSE(runner_->IsRunning());
63   });
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());
71   // Cancel again.
72   runner_->Cancel();
73   EXPECT_FALSE(runner_->IsRunning());
76 TEST_F(MenuRunnerCocoaTest, RunMenuAndDelete) {
77   MenuRunner::RunResult result = RunMenu(^{
78       runner_->Release();
79       runner_ = NULL;
80   });
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(^{
88         runner_->Cancel();
89     });
90     EXPECT_EQ(MenuRunner::NORMAL_EXIT, result);
91     EXPECT_FALSE(runner_->IsRunning());
92   }
95 TEST_F(MenuRunnerCocoaTest, CancelWithoutRunning) {
96   runner_->Cancel();
97   EXPECT_FALSE(runner_->IsRunning());
98   EXPECT_EQ(base::TimeDelta(), runner_->GetClosingEventTime());
101 TEST_F(MenuRunnerCocoaTest, DeleteWithoutRunning) {
102   runner_->Release();
103   runner_ = NULL;
106 }  // namespace test
107 }  // namespace views