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_browser_process.h" | |
8 #include "chrome/test/base/testing_profile.h" | |
9 #include "ui/base/models/simple_menu_model.h" | |
10 #include "ui/views/controls/menu/menu_runner.h" | |
11 #include "ui/views/test/widget_test.h" | |
12 | |
13 namespace test { | |
14 | |
15 // Friend of ToolbarButton to access private members. | |
16 class ToolbarButtonTestApi { | |
17 public: | |
18 explicit ToolbarButtonTestApi(ToolbarButton* button) : button_(button) {} | |
19 | |
20 views::MenuRunner* menu_runner() { return button_->menu_runner_.get(); } | |
21 bool menu_showing() const { return button_->menu_showing_; } | |
22 | |
23 private: | |
24 ToolbarButton* button_; | |
25 | |
26 DISALLOW_COPY_AND_ASSIGN(ToolbarButtonTestApi); | |
27 }; | |
28 | |
29 } // namespace test | |
30 | |
31 class ToolbarButtonTest : public views::test::WidgetTest { | |
32 public: | |
33 ToolbarButtonTest() {} | |
34 | |
35 // testing::Test: | |
36 void SetUp() override { | |
37 WidgetTest::SetUp(); | |
38 | |
39 profile_.reset(new TestingProfile); | |
40 | |
41 // Usually a BackForwardMenuModel is used, but that needs a Browser*. Make | |
42 // something simple with at least one item so a menu gets shown. Note that | |
43 // ToolbarButton takes ownership of the |model|. | |
44 ui::SimpleMenuModel* model = new ui::SimpleMenuModel(nullptr); | |
45 model->AddItem(0, base::string16()); | |
46 button_ = new ToolbarButton(profile_.get(), nullptr, model); | |
47 | |
48 // Need a Widget to show a menu in the appropriate window context. | |
49 widget_ = CreateNativeDesktopWidget(); | |
50 widget_->GetContentsView()->AddChildView(button_); | |
51 } | |
52 | |
53 void TearDown() override { | |
54 widget_->CloseNow(); | |
55 profile_.reset(); | |
56 WidgetTest::TearDown(); | |
57 | |
58 // This usually happens in ChromeUnitTestSuiteInitializer::OnTestEnd(), but | |
59 // it has tasks that would want to post to the MessageLoop owned by | |
60 // ViewsTestBase, which will be destroyed by then. | |
61 TestingBrowserProcess::DeleteInstance(); | |
tapted
2016/04/07 11:32:58
This was a frustrating trap to discover :/. I thin
| |
62 } | |
63 | |
64 protected: | |
65 std::unique_ptr<TestingProfile> profile_; | |
66 ToolbarButton* button_ = nullptr; | |
67 views::Widget* widget_ = nullptr; | |
68 | |
69 private: | |
70 DISALLOW_COPY_AND_ASSIGN(ToolbarButtonTest); | |
71 }; | |
72 | |
73 // Test showing and dismissing a menu to verify menu delegate lifetime. | |
74 TEST_F(ToolbarButtonTest, ShowMenu) { | |
75 test::ToolbarButtonTestApi test_api(button_); | |
76 | |
77 EXPECT_FALSE(test_api.menu_showing()); | |
78 EXPECT_FALSE(test_api.menu_runner()); | |
79 EXPECT_EQ(views::Button::STATE_NORMAL, button_->state()); | |
80 | |
81 // Show the menu. Note that it is asynchronous. | |
82 button_->ShowContextMenuForView(nullptr, gfx::Point(), ui::MENU_SOURCE_MOUSE); | |
sky
2016/04/07 19:27:01
AFAICT this will attempt setcapture and all that f
tapted
2016/04/08 05:32:22
Yep - I agree a flake is quite likely :/. MenuRunn
| |
83 | |
84 EXPECT_TRUE(test_api.menu_showing()); | |
85 EXPECT_TRUE(test_api.menu_runner()); | |
86 EXPECT_TRUE(test_api.menu_runner()->IsRunning()); | |
87 | |
88 // Button should appear pressed when the menu is showing. | |
89 EXPECT_EQ(views::Button::STATE_PRESSED, button_->state()); | |
90 | |
91 test_api.menu_runner()->Cancel(); | |
92 | |
93 // Ensure the ToolbarButton's |menu_runner_| member is reset to null. | |
94 EXPECT_FALSE(test_api.menu_showing()); | |
95 EXPECT_FALSE(test_api.menu_runner()); | |
96 EXPECT_EQ(views::Button::STATE_NORMAL, button_->state()); | |
97 } | |
98 | |
99 // Test deleting a ToolbarButton while its menu is showing. | |
100 TEST_F(ToolbarButtonTest, DeleteWithMenu) { | |
101 button_->ShowContextMenuForView(nullptr, gfx::Point(), ui::MENU_SOURCE_MOUSE); | |
102 EXPECT_TRUE(test::ToolbarButtonTestApi(button_).menu_runner()); | |
103 delete button_; | |
104 } | |
OLD | NEW |