OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 #include "chrome/browser/ui/views/toolbar/toolbar_button.h" |
| 6 |
| 7 #include "chrome/test/base/testing_profile.h" |
| 8 #include "chrome/test/base/view_event_test_base.h" |
| 9 #include "ui/base/models/simple_menu_model.h" |
| 10 #include "ui/views/controls/menu/menu_runner.h" |
| 11 |
| 12 namespace test { |
| 13 |
| 14 // Friend of ToolbarButton to access private members. |
| 15 class ToolbarButtonTestApi { |
| 16 public: |
| 17 explicit ToolbarButtonTestApi(ToolbarButton* button) : button_(button) {} |
| 18 |
| 19 views::MenuRunner* menu_runner() { return button_->menu_runner_.get(); } |
| 20 bool menu_showing() const { return button_->menu_showing_; } |
| 21 |
| 22 private: |
| 23 ToolbarButton* button_; |
| 24 |
| 25 DISALLOW_COPY_AND_ASSIGN(ToolbarButtonTestApi); |
| 26 }; |
| 27 |
| 28 } // namespace test |
| 29 |
| 30 class ToolbarButtonUITest : public ViewEventTestBase { |
| 31 public: |
| 32 ToolbarButtonUITest() {} |
| 33 |
| 34 // ViewEventTestBase: |
| 35 views::View* CreateContentsView() override { |
| 36 // Usually a BackForwardMenuModel is used, but that needs a Browser*. Make |
| 37 // something simple with at least one item so a menu gets shown. Note that |
| 38 // ToolbarButton takes ownership of the |model|. |
| 39 ui::SimpleMenuModel* model = new ui::SimpleMenuModel(nullptr); |
| 40 model->AddItem(0, base::string16()); |
| 41 button_ = new ToolbarButton(&profile_, nullptr, model); |
| 42 return button_; |
| 43 } |
| 44 void DoTestOnMessageLoop() override {} |
| 45 |
| 46 protected: |
| 47 TestingProfile profile_; |
| 48 ToolbarButton* button_ = nullptr; |
| 49 |
| 50 private: |
| 51 DISALLOW_COPY_AND_ASSIGN(ToolbarButtonUITest); |
| 52 }; |
| 53 |
| 54 // Test showing and dismissing a menu to verify menu delegate lifetime. |
| 55 TEST_F(ToolbarButtonUITest, ShowMenu) { |
| 56 test::ToolbarButtonTestApi test_api(button_); |
| 57 |
| 58 EXPECT_FALSE(test_api.menu_showing()); |
| 59 EXPECT_FALSE(test_api.menu_runner()); |
| 60 EXPECT_EQ(views::Button::STATE_NORMAL, button_->state()); |
| 61 |
| 62 // Show the menu. Note that it is asynchronous. |
| 63 button_->ShowContextMenuForView(nullptr, gfx::Point(), ui::MENU_SOURCE_MOUSE); |
| 64 |
| 65 EXPECT_TRUE(test_api.menu_showing()); |
| 66 EXPECT_TRUE(test_api.menu_runner()); |
| 67 EXPECT_TRUE(test_api.menu_runner()->IsRunning()); |
| 68 |
| 69 // Button should appear pressed when the menu is showing. |
| 70 EXPECT_EQ(views::Button::STATE_PRESSED, button_->state()); |
| 71 |
| 72 test_api.menu_runner()->Cancel(); |
| 73 |
| 74 // Ensure the ToolbarButton's |menu_runner_| member is reset to null. |
| 75 EXPECT_FALSE(test_api.menu_showing()); |
| 76 EXPECT_FALSE(test_api.menu_runner()); |
| 77 EXPECT_EQ(views::Button::STATE_NORMAL, button_->state()); |
| 78 } |
| 79 |
| 80 // Test deleting a ToolbarButton while its menu is showing. |
| 81 TEST_F(ToolbarButtonUITest, DeleteWithMenu) { |
| 82 button_->ShowContextMenuForView(nullptr, gfx::Point(), ui::MENU_SOURCE_MOUSE); |
| 83 EXPECT_TRUE(test::ToolbarButtonTestApi(button_).menu_runner()); |
| 84 delete button_; |
| 85 } |
OLD | NEW |