Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / views / toolbar / browser_action_test_util_views.cc
blob9da3406c33ac8db7af1a6cb7824878fc8403558d
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"
23 namespace {
25 // The BrowserActionsContainer expects to have a parent (and be added to the
26 // view hierarchy), so wrap it in a shell view that will set the container's
27 // bounds to be its preferred bounds.
28 class ContainerParent : public views::View {
29 public:
30 explicit ContainerParent(BrowserActionsContainer* container)
31 : container_(container) {
32 AddChildView(container_);
34 ~ContainerParent() override {}
36 void Layout() override {
37 container_->SizeToPreferredSize();
40 private:
41 BrowserActionsContainer* container_;
43 DISALLOW_COPY_AND_ASSIGN(ContainerParent);
46 // The views-specific implementation of the TestToolbarActionsBarHelper, which
47 // creates and owns a BrowserActionsContainer.
48 class TestToolbarActionsBarHelperViews : public TestToolbarActionsBarHelper {
49 public:
50 TestToolbarActionsBarHelperViews(Browser* browser,
51 BrowserActionsContainer* main_bar);
52 ~TestToolbarActionsBarHelperViews() override;
54 BrowserActionsContainer* browser_actions_container() {
55 return browser_actions_container_;
58 private:
59 // The created BrowserActionsContainer. Owned by |container_parent_|.
60 BrowserActionsContainer* browser_actions_container_;
62 // The parent of the BrowserActionsContainer, which directly owns the
63 // container as part of the views hierarchy.
64 ContainerParent container_parent_;
66 DISALLOW_COPY_AND_ASSIGN(TestToolbarActionsBarHelperViews);
69 TestToolbarActionsBarHelperViews::TestToolbarActionsBarHelperViews(
70 Browser* browser,
71 BrowserActionsContainer* main_bar)
72 : browser_actions_container_(
73 new BrowserActionsContainer(browser, main_bar)),
74 container_parent_(browser_actions_container_) {
75 container_parent_.set_owned_by_client();
76 container_parent_.SetSize(gfx::Size(1000, 1000));
77 container_parent_.Layout();
80 TestToolbarActionsBarHelperViews::~TestToolbarActionsBarHelperViews() {
83 BrowserActionsContainer* GetContainer(Browser* browser,
84 TestToolbarActionsBarHelper* helper) {
85 if (helper) {
86 return static_cast<TestToolbarActionsBarHelperViews*>(helper)
87 ->browser_actions_container();
89 return BrowserView::GetBrowserViewForBrowser(browser)->toolbar()->
90 browser_actions();
93 } // namespace
95 BrowserActionTestUtil::BrowserActionTestUtil(Browser* browser)
96 : BrowserActionTestUtil(browser, true) {
99 BrowserActionTestUtil::BrowserActionTestUtil(Browser* browser,
100 bool is_real_window)
101 : browser_(browser) {
102 if (!is_real_window)
103 test_helper_.reset(new TestToolbarActionsBarHelperViews(browser, nullptr));
106 BrowserActionTestUtil::~BrowserActionTestUtil() {
109 int BrowserActionTestUtil::NumberOfBrowserActions() {
110 return GetContainer(browser_, test_helper_.get())->num_toolbar_actions();
113 int BrowserActionTestUtil::VisibleBrowserActions() {
114 return GetContainer(browser_, test_helper_.get())->VisibleBrowserActions();
117 bool BrowserActionTestUtil::IsChevronShowing() {
118 BrowserActionsContainer* container =
119 GetContainer(browser_, test_helper_.get());
120 gfx::Size visible_size = container->GetVisibleBounds().size();
121 return container->chevron() &&
122 container->chevron()->visible() &&
123 visible_size.width() >=
124 container->chevron()->GetPreferredSize().width() &&
125 !visible_size.IsEmpty();
128 void BrowserActionTestUtil::InspectPopup(int index) {
129 ToolbarActionView* view =
130 GetContainer(browser_, test_helper_.get())->GetToolbarActionViewAt(index);
131 static_cast<ExtensionActionViewController*>(view->view_controller())->
132 InspectPopup();
135 bool BrowserActionTestUtil::HasIcon(int index) {
136 return !GetContainer(browser_, test_helper_.get())
137 ->GetToolbarActionViewAt(index)
138 ->GetImage(views::Button::STATE_NORMAL)
139 .isNull();
142 gfx::Image BrowserActionTestUtil::GetIcon(int index) {
143 gfx::ImageSkia icon = GetContainer(browser_, test_helper_.get())
144 ->GetToolbarActionViewAt(index)
145 ->GetIconForTest();
146 return gfx::Image(icon);
149 void BrowserActionTestUtil::Press(int index) {
150 GetContainer(browser_, test_helper_.get())
151 ->GetToolbarActionViewAt(index)
152 ->view_controller()
153 ->ExecuteAction(true);
156 std::string BrowserActionTestUtil::GetExtensionId(int index) {
157 return GetContainer(browser_, test_helper_.get())
158 ->GetToolbarActionViewAt(index)
159 ->view_controller()
160 ->GetId();
163 std::string BrowserActionTestUtil::GetTooltip(int index) {
164 base::string16 text;
165 GetContainer(browser_, test_helper_.get())
166 ->GetToolbarActionViewAt(index)
167 ->GetTooltipText(gfx::Point(), &text);
168 return base::UTF16ToUTF8(text);
171 gfx::NativeView BrowserActionTestUtil::GetPopupNativeView() {
172 ToolbarActionViewController* popup_owner =
173 GetToolbarActionsBar()->popup_owner();
174 return popup_owner ? popup_owner->GetPopupNativeView() : nullptr;
177 bool BrowserActionTestUtil::HasPopup() {
178 return GetPopupNativeView() != nullptr;
181 gfx::Size BrowserActionTestUtil::GetPopupSize() {
182 gfx::NativeView popup = GetPopupNativeView();
183 views::Widget* widget = views::Widget::GetWidgetForNativeView(popup);
184 return widget->GetWindowBoundsInScreen().size();
187 bool BrowserActionTestUtil::HidePopup() {
188 GetToolbarActionsBar()->HideActivePopup();
189 return !HasPopup();
192 bool BrowserActionTestUtil::ActionButtonWantsToRun(size_t index) {
193 return GetContainer(browser_, test_helper_.get())
194 ->GetToolbarActionViewAt(index)
195 ->wants_to_run_for_testing();
198 void BrowserActionTestUtil::SetWidth(int width) {
199 BrowserActionsContainer* container =
200 GetContainer(browser_, test_helper_.get());
201 container->SetSize(gfx::Size(width, container->height()));
204 ToolbarActionsBar* BrowserActionTestUtil::GetToolbarActionsBar() {
205 return GetContainer(browser_, test_helper_.get())->toolbar_actions_bar();
208 scoped_ptr<BrowserActionTestUtil> BrowserActionTestUtil::CreateOverflowBar() {
209 CHECK(!GetToolbarActionsBar()->in_overflow_mode())
210 << "Only a main bar can create an overflow bar!";
211 return make_scoped_ptr(new BrowserActionTestUtil(browser_, this));
214 // static
215 gfx::Size BrowserActionTestUtil::GetMinPopupSize() {
216 return gfx::Size(ExtensionPopup::kMinWidth, ExtensionPopup::kMinHeight);
219 // static
220 gfx::Size BrowserActionTestUtil::GetMaxPopupSize() {
221 return gfx::Size(ExtensionPopup::kMaxWidth, ExtensionPopup::kMaxHeight);
224 BrowserActionTestUtil::BrowserActionTestUtil(Browser* browser,
225 BrowserActionTestUtil* main_bar)
226 : browser_(browser),
227 test_helper_(new TestToolbarActionsBarHelperViews(
228 browser_,
229 GetContainer(browser_, main_bar->test_helper_.get()))) {