OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #import "ui/views/controls/menu/menu_runner_impl_cocoa.h" |
| 6 |
| 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" |
| 11 |
| 12 namespace views { |
| 13 namespace test { |
| 14 |
| 15 class MenuRunnerCocoaTest : public ViewsTestBase { |
| 16 public: |
| 17 MenuRunnerCocoaTest() : runner_(NULL) {} |
| 18 virtual ~MenuRunnerCocoaTest() {} |
| 19 |
| 20 virtual void SetUp() OVERRIDE { |
| 21 ViewsTestBase::SetUp(); |
| 22 |
| 23 menu_.reset(new ui::SimpleMenuModel(NULL)); |
| 24 menu_->AddItem(0, base::ASCIIToUTF16("Menu Item")); |
| 25 |
| 26 runner_ = new internal::MenuRunnerImplCocoa(menu_.get()); |
| 27 EXPECT_FALSE(runner_->IsRunning()); |
| 28 } |
| 29 |
| 30 virtual void TearDown() OVERRIDE { |
| 31 if (runner_) { |
| 32 runner_->Release(); |
| 33 runner_ = NULL; |
| 34 } |
| 35 |
| 36 ViewsTestBase::TearDown(); |
| 37 } |
| 38 |
| 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 } |
| 48 |
| 49 protected: |
| 50 scoped_ptr<ui::SimpleMenuModel> menu_; |
| 51 internal::MenuRunnerImplCocoa* runner_; |
| 52 |
| 53 private: |
| 54 DISALLOW_COPY_AND_ASSIGN(MenuRunnerCocoaTest); |
| 55 }; |
| 56 |
| 57 TEST_F(MenuRunnerCocoaTest, GetMenu) { |
| 58 EXPECT_FALSE(runner_->GetMenu()); |
| 59 } |
| 60 |
| 61 TEST_F(MenuRunnerCocoaTest, RunMenuAndCancel) { |
| 62 base::TimeDelta min_time = ui::EventTimeForNow(); |
| 63 |
| 64 MenuRunner::RunResult result = RunMenu(^{ |
| 65 runner_->Cancel(); |
| 66 EXPECT_FALSE(runner_->IsRunning()); |
| 67 }); |
| 68 |
| 69 EXPECT_EQ(MenuRunner::NORMAL_EXIT, result); |
| 70 EXPECT_FALSE(runner_->IsRunning()); |
| 71 |
| 72 EXPECT_GE(runner_->GetClosingEventTime(), min_time); |
| 73 EXPECT_LE(runner_->GetClosingEventTime(), ui::EventTimeForNow()); |
| 74 |
| 75 // Cancel again. |
| 76 runner_->Cancel(); |
| 77 EXPECT_FALSE(runner_->IsRunning()); |
| 78 } |
| 79 |
| 80 TEST_F(MenuRunnerCocoaTest, RunMenuAndDelete) { |
| 81 MenuRunner::RunResult result = RunMenu(^{ |
| 82 runner_->Release(); |
| 83 runner_ = NULL; |
| 84 }); |
| 85 |
| 86 EXPECT_EQ(MenuRunner::MENU_DELETED, result); |
| 87 } |
| 88 |
| 89 TEST_F(MenuRunnerCocoaTest, RunMenuTwice) { |
| 90 for (int i = 0; i < 2; ++i) { |
| 91 MenuRunner::RunResult result = RunMenu(^{ |
| 92 runner_->Cancel(); |
| 93 }); |
| 94 EXPECT_EQ(MenuRunner::NORMAL_EXIT, result); |
| 95 EXPECT_FALSE(runner_->IsRunning()); |
| 96 } |
| 97 } |
| 98 |
| 99 TEST_F(MenuRunnerCocoaTest, CancelWithoutRunning) { |
| 100 runner_->Cancel(); |
| 101 EXPECT_FALSE(runner_->IsRunning()); |
| 102 EXPECT_EQ(base::TimeDelta(), runner_->GetClosingEventTime()); |
| 103 } |
| 104 |
| 105 TEST_F(MenuRunnerCocoaTest, DeleteWithoutRunning) { |
| 106 runner_->Release(); |
| 107 runner_ = NULL; |
| 108 } |
| 109 |
| 110 } // namespace test |
| 111 } // namespace views |
OLD | NEW |