1 // Copyright (c) 2009 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/mac/bundle_locations.h"
8 #include "base/mac/foundation_util.h"
9 #include "base/path_service.h"
10 #include "base/strings/sys_string_conversions.h"
11 #include "chrome/browser/ui/browser.h"
12 #import "chrome/browser/ui/cocoa/browser_window_cocoa.h"
13 #import "chrome/browser/ui/cocoa/browser_window_controller.h"
14 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h"
15 #import "chrome/browser/ui/cocoa/extensions/browser_action_button.h"
16 #import "chrome/browser/ui/cocoa/extensions/browser_actions_container_view.h"
17 #import "chrome/browser/ui/cocoa/extensions/browser_actions_controller.h"
18 #import "chrome/browser/ui/cocoa/extensions/extension_popup_controller.h"
19 #import "chrome/browser/ui/cocoa/info_bubble_window.h"
20 #import "chrome/browser/ui/cocoa/themed_window.h"
21 #import "chrome/browser/ui/cocoa/toolbar/toolbar_controller.h"
22 #include "chrome/browser/ui/toolbar/toolbar_action_view_controller.h"
23 #include "chrome/browser/ui/toolbar/toolbar_actions_bar.h"
24 #include "chrome/common/chrome_constants.h"
25 #include "grit/theme_resources.h"
26 #include "ui/base/theme_provider.h"
27 #include "ui/gfx/geometry/rect.h"
28 #include "ui/gfx/geometry/size.h"
32 // The Cocoa implementation of the TestToolbarActionsBarHelper, which creates
33 // (and owns) a BrowserActionsController and BrowserActionsContainerView for
35 class TestToolbarActionsBarHelperCocoa : public TestToolbarActionsBarHelper {
37 TestToolbarActionsBarHelperCocoa(Browser* browser,
38 BrowserActionsController* mainController);
39 ~TestToolbarActionsBarHelperCocoa() override;
41 BrowserActionsController* controller() { return controller_.get(); }
44 // The owned BrowserActionsContainerView and BrowserActionsController; the
45 // mac implementation of the ToolbarActionsBar delegate and view.
46 base::scoped_nsobject<BrowserActionsContainerView> containerView_;
47 base::scoped_nsobject<BrowserActionsController> controller_;
49 DISALLOW_COPY_AND_ASSIGN(TestToolbarActionsBarHelperCocoa);
52 TestToolbarActionsBarHelperCocoa::TestToolbarActionsBarHelperCocoa(
54 BrowserActionsController* mainController) {
55 // Make sure that Cocoa has been bootstrapped.
56 if (!base::mac::FrameworkBundle()) {
57 // Look in the framework bundle for resources.
59 PathService::Get(base::DIR_EXE, &path);
60 path = path.Append(chrome::kFrameworkName);
61 base::mac::SetOverrideFrameworkBundlePath(path);
64 containerView_.reset([[BrowserActionsContainerView alloc]
65 initWithFrame:NSMakeRect(0, 0, 0, 15)]);
67 [[BrowserActionsController alloc] initWithBrowser:browser
68 containerView:containerView_.get()
69 mainController:mainController]);
72 TestToolbarActionsBarHelperCocoa::~TestToolbarActionsBarHelperCocoa() {
75 BrowserActionsController* GetController(Browser* browser,
76 TestToolbarActionsBarHelper* helper) {
78 return static_cast<TestToolbarActionsBarHelperCocoa*>(helper)->controller();
81 BrowserWindowCocoa* window =
82 static_cast<BrowserWindowCocoa*>(browser->window());
83 return [[window->cocoa_controller() toolbarController]
84 browserActionsController];
87 BrowserActionButton* GetButton(
89 TestToolbarActionsBarHelper* helper,
91 return [GetController(browser, helper) buttonWithIndex:index];
96 BrowserActionTestUtil::BrowserActionTestUtil(Browser* browser)
97 : BrowserActionTestUtil(browser, true) {
100 BrowserActionTestUtil::BrowserActionTestUtil(Browser* browser,
102 : browser_(browser) {
104 test_helper_.reset(new TestToolbarActionsBarHelperCocoa(browser, nullptr));
105 // We disable animations on extension popups so that tests aren't waiting for
106 // a popup to fade out.
107 [ExtensionPopupController setAnimationsEnabledForTesting:NO];
110 BrowserActionTestUtil::~BrowserActionTestUtil() {}
112 int BrowserActionTestUtil::NumberOfBrowserActions() {
113 return [GetController(browser_, test_helper_.get()) buttonCount];
116 int BrowserActionTestUtil::VisibleBrowserActions() {
117 return [GetController(browser_, test_helper_.get()) visibleButtonCount];
120 bool BrowserActionTestUtil::IsChevronShowing() {
121 BrowserActionsController* controller =
122 GetController(browser_, test_helper_.get());
123 // The magic "18" comes from kChevronWidth in browser_actions_controller.mm.
124 return ![controller chevronIsHidden] &&
125 NSWidth([[controller containerView] animationEndFrame]) >= 18;
128 void BrowserActionTestUtil::InspectPopup(int index) {
132 bool BrowserActionTestUtil::HasIcon(int index) {
133 return [GetButton(browser_, test_helper_.get(), index) image] != nil;
136 gfx::Image BrowserActionTestUtil::GetIcon(int index) {
137 NSImage* ns_image = [GetButton(browser_, test_helper_.get(), index) image];
138 // gfx::Image takes ownership of the |ns_image| reference. We have to increase
139 // the ref count so |ns_image| stays around when the image object is
141 base::mac::NSObjectRetain(ns_image);
142 return gfx::Image(ns_image);
145 void BrowserActionTestUtil::Press(int index) {
146 NSButton* button = GetButton(browser_, test_helper_.get(), index);
147 [button performClick:nil];
150 std::string BrowserActionTestUtil::GetExtensionId(int index) {
152 [GetButton(browser_, test_helper_.get(), index) viewController]->GetId();
155 std::string BrowserActionTestUtil::GetTooltip(int index) {
156 NSString* tooltip = [GetButton(browser_, test_helper_.get(), index) toolTip];
157 return base::SysNSStringToUTF8(tooltip);
160 gfx::NativeView BrowserActionTestUtil::GetPopupNativeView() {
161 ToolbarActionViewController* popup_owner =
162 GetToolbarActionsBar()->popup_owner();
163 return popup_owner ? popup_owner->GetPopupNativeView() : nil;
166 bool BrowserActionTestUtil::HasPopup() {
167 return GetPopupNativeView() != nil;
170 gfx::Size BrowserActionTestUtil::GetPopupSize() {
171 NSRect bounds = [[[ExtensionPopupController popup] view] bounds];
172 return gfx::Size(NSSizeToCGSize(bounds.size));
175 bool BrowserActionTestUtil::HidePopup() {
176 ExtensionPopupController* controller = [ExtensionPopupController popup];
181 bool BrowserActionTestUtil::ActionButtonWantsToRun(size_t index) {
182 return [GetButton(browser_, test_helper_.get(), index) wantsToRunForTesting];
185 ToolbarActionsBar* BrowserActionTestUtil::GetToolbarActionsBar() {
186 return [GetController(browser_, test_helper_.get()) toolbarActionsBar];
189 scoped_ptr<BrowserActionTestUtil> BrowserActionTestUtil::CreateOverflowBar() {
190 CHECK(!GetToolbarActionsBar()->in_overflow_mode())
191 << "Only a main bar can create an overflow bar!";
192 return make_scoped_ptr(new BrowserActionTestUtil(browser_, this));
196 gfx::Size BrowserActionTestUtil::GetMinPopupSize() {
197 return gfx::Size(NSSizeToCGSize([ExtensionPopupController minPopupSize]));
201 gfx::Size BrowserActionTestUtil::GetMaxPopupSize() {
202 return gfx::Size(NSSizeToCGSize([ExtensionPopupController maxPopupSize]));
205 BrowserActionTestUtil::BrowserActionTestUtil(Browser* browser,
206 BrowserActionTestUtil* main_bar)
208 test_helper_(new TestToolbarActionsBarHelperCocoa(
209 browser_, GetController(browser_, main_bar->test_helper_.get()))) {