1 // Copyright 2013 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 #include "chrome/browser/extensions/browser_action_test_util.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/ui/browser.h"
9 #include "chrome/browser/ui/browser_window.h"
10 #include "chrome/browser/ui/extensions/extension_action_view_controller.h"
11 #include "chrome/browser/ui/views/extensions/extension_popup.h"
12 #include "chrome/browser/ui/views/frame/browser_view.h"
13 #include "chrome/browser/ui/views/toolbar/browser_actions_container.h"
14 #include "chrome/browser/ui/views/toolbar/toolbar_action_view.h"
15 #include "chrome/browser/ui/views/toolbar/toolbar_view.h"
16 #include "chrome/browser/ui/views/toolbar/wrench_toolbar_button.h"
17 #include "ui/aura/window.h"
18 #include "ui/gfx/geometry/rect.h"
19 #include "ui/gfx/geometry/size.h"
20 #include "ui/gfx/image/image.h"
21 #include "ui/views/widget/widget.h"
25 // The views-specific implementation of the TestToolbarActionsBarHelper, which
26 // creates and owns a BrowserActionsContainer.
27 class TestToolbarActionsBarHelperViews
: public TestToolbarActionsBarHelper
{
29 TestToolbarActionsBarHelperViews(Browser
* browser
,
30 BrowserActionsContainer
* main_bar
);
31 ~TestToolbarActionsBarHelperViews() override
;
33 BrowserActionsContainer
* browser_actions_container() {
34 return browser_actions_container_
;
38 // The parent of the BrowserActionsContainer, which directly owns the
39 // container as part of the views hierarchy.
40 views::View container_parent_
;
42 // The created BrowserActionsContainer. Owned by |container_parent_|.
43 BrowserActionsContainer
* browser_actions_container_
;
45 DISALLOW_COPY_AND_ASSIGN(TestToolbarActionsBarHelperViews
);
48 TestToolbarActionsBarHelperViews::TestToolbarActionsBarHelperViews(
50 BrowserActionsContainer
* main_bar
)
51 : browser_actions_container_(
52 new BrowserActionsContainer(browser
, main_bar
)) {
53 // The BrowserActionsContainer expects to have a parent (and be added to the
54 // view hierarchy), so wrap it in a shell view.
55 container_parent_
.set_owned_by_client();
56 container_parent_
.AddChildView(browser_actions_container_
);
59 TestToolbarActionsBarHelperViews::~TestToolbarActionsBarHelperViews() {
62 BrowserActionsContainer
* GetContainer(Browser
* browser
,
63 TestToolbarActionsBarHelper
* helper
) {
65 return static_cast<TestToolbarActionsBarHelperViews
*>(helper
)
66 ->browser_actions_container();
68 return BrowserView::GetBrowserViewForBrowser(browser
)->toolbar()->
74 BrowserActionTestUtil::BrowserActionTestUtil(Browser
* browser
)
75 : BrowserActionTestUtil(browser
, true) {
78 BrowserActionTestUtil::BrowserActionTestUtil(Browser
* browser
,
82 test_helper_
.reset(new TestToolbarActionsBarHelperViews(browser
, nullptr));
85 BrowserActionTestUtil::~BrowserActionTestUtil() {
88 int BrowserActionTestUtil::NumberOfBrowserActions() {
89 return GetContainer(browser_
, test_helper_
.get())->num_toolbar_actions();
92 int BrowserActionTestUtil::VisibleBrowserActions() {
93 return GetContainer(browser_
, test_helper_
.get())->VisibleBrowserActions();
96 bool BrowserActionTestUtil::IsChevronShowing() {
97 BrowserActionsContainer
* container
=
98 GetContainer(browser_
, test_helper_
.get());
99 gfx::Size visible_size
= container
->GetVisibleBounds().size();
100 return container
->chevron() &&
101 container
->chevron()->visible() &&
102 visible_size
.width() >=
103 container
->chevron()->GetPreferredSize().width() &&
104 !visible_size
.IsEmpty();
107 void BrowserActionTestUtil::InspectPopup(int index
) {
108 ToolbarActionView
* view
=
109 GetContainer(browser_
, test_helper_
.get())->GetToolbarActionViewAt(index
);
110 static_cast<ExtensionActionViewController
*>(view
->view_controller())->
114 bool BrowserActionTestUtil::HasIcon(int index
) {
115 return !GetContainer(browser_
, test_helper_
.get())
116 ->GetToolbarActionViewAt(index
)
117 ->GetImage(views::Button::STATE_NORMAL
)
121 gfx::Image
BrowserActionTestUtil::GetIcon(int index
) {
122 gfx::ImageSkia icon
= GetContainer(browser_
, test_helper_
.get())
123 ->GetToolbarActionViewAt(index
)
125 return gfx::Image(icon
);
128 void BrowserActionTestUtil::Press(int index
) {
129 GetContainer(browser_
, test_helper_
.get())
130 ->GetToolbarActionViewAt(index
)
132 ->ExecuteAction(true);
135 std::string
BrowserActionTestUtil::GetExtensionId(int index
) {
136 return GetContainer(browser_
, test_helper_
.get())
137 ->GetToolbarActionViewAt(index
)
142 std::string
BrowserActionTestUtil::GetTooltip(int index
) {
144 GetContainer(browser_
, test_helper_
.get())
145 ->GetToolbarActionViewAt(index
)
146 ->GetTooltipText(gfx::Point(), &text
);
147 return base::UTF16ToUTF8(text
);
150 gfx::NativeView
BrowserActionTestUtil::GetPopupNativeView() {
151 ToolbarActionViewController
* popup_owner
=
152 GetToolbarActionsBar()->popup_owner();
153 return popup_owner
? popup_owner
->GetPopupNativeView() : nullptr;
156 bool BrowserActionTestUtil::HasPopup() {
157 return GetPopupNativeView() != nullptr;
160 gfx::Size
BrowserActionTestUtil::GetPopupSize() {
161 gfx::NativeView popup
= GetPopupNativeView();
162 views::Widget
* widget
= views::Widget::GetWidgetForNativeView(popup
);
163 return widget
->GetWindowBoundsInScreen().size();
166 bool BrowserActionTestUtil::HidePopup() {
167 GetToolbarActionsBar()->HideActivePopup();
171 bool BrowserActionTestUtil::ActionButtonWantsToRun(size_t index
) {
172 return GetContainer(browser_
, test_helper_
.get())
173 ->GetToolbarActionViewAt(index
)
174 ->wants_to_run_for_testing();
177 ToolbarActionsBar
* BrowserActionTestUtil::GetToolbarActionsBar() {
178 return GetContainer(browser_
, test_helper_
.get())->toolbar_actions_bar();
181 scoped_ptr
<BrowserActionTestUtil
> BrowserActionTestUtil::CreateOverflowBar() {
182 CHECK(!GetToolbarActionsBar()->in_overflow_mode())
183 << "Only a main bar can create an overflow bar!";
184 return make_scoped_ptr(new BrowserActionTestUtil(browser_
, this));
188 gfx::Size
BrowserActionTestUtil::GetMinPopupSize() {
189 return gfx::Size(ExtensionPopup::kMinWidth
, ExtensionPopup::kMinHeight
);
193 gfx::Size
BrowserActionTestUtil::GetMaxPopupSize() {
194 return gfx::Size(ExtensionPopup::kMaxWidth
, ExtensionPopup::kMaxHeight
);
197 BrowserActionTestUtil::BrowserActionTestUtil(Browser
* browser
,
198 BrowserActionTestUtil
* main_bar
)
200 test_helper_(new TestToolbarActionsBarHelperViews(
202 GetContainer(browser_
, main_bar
->test_helper_
.get()))) {