Give Cocoa browser windows a WebContentsModalDialogHost
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / extensions / browser_action_test_util_mac.mm
blob70ace334faed88bd4b4959a06ec1373299cce2fd
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 #import "chrome/browser/ui/cocoa/toolbar/wrench_toolbar_button_cell.h"
23 #include "chrome/browser/ui/toolbar/toolbar_action_view_controller.h"
24 #include "chrome/browser/ui/toolbar/toolbar_actions_bar.h"
25 #include "chrome/common/chrome_constants.h"
26 #include "grit/theme_resources.h"
27 #include "ui/base/theme_provider.h"
28 #include "ui/gfx/geometry/rect.h"
29 #include "ui/gfx/geometry/size.h"
31 namespace {
33 // The Cocoa implementation of the TestToolbarActionsBarHelper, which creates
34 // (and owns) a BrowserActionsController and BrowserActionsContainerView for
35 // testing purposes.
36 class TestToolbarActionsBarHelperCocoa : public TestToolbarActionsBarHelper {
37  public:
38   TestToolbarActionsBarHelperCocoa(Browser* browser,
39                                    BrowserActionsController* mainController);
40   ~TestToolbarActionsBarHelperCocoa() override;
42   BrowserActionsController* controller() { return controller_.get(); }
44  private:
45   // The owned BrowserActionsContainerView and BrowserActionsController; the
46   // mac implementation of the ToolbarActionsBar delegate and view.
47   base::scoped_nsobject<BrowserActionsContainerView> containerView_;
48   base::scoped_nsobject<BrowserActionsController> controller_;
50   DISALLOW_COPY_AND_ASSIGN(TestToolbarActionsBarHelperCocoa);
53 TestToolbarActionsBarHelperCocoa::TestToolbarActionsBarHelperCocoa(
54     Browser* browser,
55     BrowserActionsController* mainController) {
56   // Make sure that Cocoa has been bootstrapped.
57   if (!base::mac::FrameworkBundle()) {
58     // Look in the framework bundle for resources.
59     base::FilePath path;
60     PathService::Get(base::DIR_EXE, &path);
61     path = path.Append(chrome::kFrameworkName);
62     base::mac::SetOverrideFrameworkBundlePath(path);
63   }
65   containerView_.reset([[BrowserActionsContainerView alloc]
66       initWithFrame:NSMakeRect(0, 0, 0, 15)]);
67   controller_.reset(
68       [[BrowserActionsController alloc] initWithBrowser:browser
69                                           containerView:containerView_.get()
70                                          mainController:mainController]);
73 TestToolbarActionsBarHelperCocoa::~TestToolbarActionsBarHelperCocoa() {
76 BrowserActionsController* GetController(Browser* browser,
77                                         TestToolbarActionsBarHelper* helper) {
78   if (helper) {
79     return static_cast<TestToolbarActionsBarHelperCocoa*>(helper)->controller();
80   }
82   BrowserWindowCocoa* window =
83       static_cast<BrowserWindowCocoa*>(browser->window());
84   return [[window->cocoa_controller() toolbarController]
85            browserActionsController];
88 BrowserActionButton* GetButton(
89     Browser* browser,
90     TestToolbarActionsBarHelper* helper,
91     int index) {
92   return [GetController(browser, helper) buttonWithIndex:index];
95 }  // namespace
97 BrowserActionTestUtil::BrowserActionTestUtil(Browser* browser)
98     : BrowserActionTestUtil(browser, true) {
101 BrowserActionTestUtil::BrowserActionTestUtil(Browser* browser,
102                                              bool is_real_window)
103     : browser_(browser) {
104   if (!is_real_window)
105     test_helper_.reset(new TestToolbarActionsBarHelperCocoa(browser, nullptr));
108 BrowserActionTestUtil::~BrowserActionTestUtil() {}
110 int BrowserActionTestUtil::NumberOfBrowserActions() {
111   return [GetController(browser_, test_helper_.get()) buttonCount];
114 int BrowserActionTestUtil::VisibleBrowserActions() {
115   return [GetController(browser_, test_helper_.get()) visibleButtonCount];
118 bool BrowserActionTestUtil::IsChevronShowing() {
119   BrowserActionsController* controller =
120       GetController(browser_, test_helper_.get());
121   // The magic "18" comes from kChevronWidth in browser_actions_controller.mm.
122   return ![controller chevronIsHidden] &&
123          NSWidth([[controller containerView] animationEndFrame]) >= 18;
126 void BrowserActionTestUtil::InspectPopup(int index) {
127   NOTREACHED();
130 bool BrowserActionTestUtil::HasIcon(int index) {
131   return [GetButton(browser_, test_helper_.get(), index) image] != nil;
134 gfx::Image BrowserActionTestUtil::GetIcon(int index) {
135   NSImage* ns_image = [GetButton(browser_, test_helper_.get(), index) image];
136   // gfx::Image takes ownership of the |ns_image| reference. We have to increase
137   // the ref count so |ns_image| stays around when the image object is
138   // destroyed.
139   base::mac::NSObjectRetain(ns_image);
140   return gfx::Image(ns_image);
143 void BrowserActionTestUtil::Press(int index) {
144   NSButton* button = GetButton(browser_, test_helper_.get(), index);
145   [button performClick:nil];
148 std::string BrowserActionTestUtil::GetExtensionId(int index) {
149   return
150       [GetButton(browser_, test_helper_.get(), index) viewController]->GetId();
153 std::string BrowserActionTestUtil::GetTooltip(int index) {
154   NSString* tooltip = [GetButton(browser_, test_helper_.get(), index) toolTip];
155   return base::SysNSStringToUTF8(tooltip);
158 gfx::NativeView BrowserActionTestUtil::GetPopupNativeView() {
159   ToolbarActionViewController* popup_owner =
160       GetToolbarActionsBar()->popup_owner();
161   return popup_owner ? popup_owner->GetPopupNativeView() : nil;
164 bool BrowserActionTestUtil::HasPopup() {
165   return GetPopupNativeView() != nil;
168 gfx::Size BrowserActionTestUtil::GetPopupSize() {
169   NSRect bounds = [[[ExtensionPopupController popup] view] bounds];
170   return gfx::Size(NSSizeToCGSize(bounds.size));
173 bool BrowserActionTestUtil::HidePopup() {
174   ExtensionPopupController* controller = [ExtensionPopupController popup];
175   // The window must be gone or we'll fail a unit test with windows left open.
176   [static_cast<InfoBubbleWindow*>([controller window])
177       setAllowedAnimations:info_bubble::kAnimateNone];
178   [controller close];
179   return !HasPopup();
182 bool BrowserActionTestUtil::ActionButtonWantsToRun(size_t index) {
183   BrowserActionsController* controller =
184       GetController(browser_, test_helper_.get());
185   ui::ThemeProvider* themeProvider =
186       [[[controller containerView] window] themeProvider];
187   DCHECK(themeProvider);
188   NSImage* wantsToRunImage =
189       themeProvider->GetNSImageNamed(IDR_BROWSER_ACTION_R);
190   BrowserActionButton* button = [controller buttonWithIndex:index];
191   BrowserActionCell* cell =
192       base::mac::ObjCCastStrict<BrowserActionCell>([button cell]);
193   NSImage* actualImage = [cell imageForState:image_button_cell::kDefaultState
194                                         view:button];
196   return wantsToRunImage == actualImage;
199 bool BrowserActionTestUtil::OverflowedActionButtonWantsToRun() {
200   NSView* wrench = [[[BrowserWindowController browserWindowControllerForWindow:
201       browser_->window()->GetNativeWindow()] toolbarController] wrenchButton];
202   NSButton* wrenchButton = base::mac::ObjCCastStrict<NSButton>(wrench);
203   WrenchToolbarButtonCell* cell =
204       base::mac::ObjCCastStrict<WrenchToolbarButtonCell>([wrenchButton cell]);
205   return [cell overflowedToolbarActionWantsToRun];
208 ToolbarActionsBar* BrowserActionTestUtil::GetToolbarActionsBar() {
209   return [GetController(browser_, test_helper_.get()) toolbarActionsBar];
212 scoped_ptr<BrowserActionTestUtil> BrowserActionTestUtil::CreateOverflowBar() {
213   CHECK(!GetToolbarActionsBar()->in_overflow_mode())
214       << "Only a main bar can create an overflow bar!";
215   return make_scoped_ptr(new BrowserActionTestUtil(browser_, this));
218 // static
219 gfx::Size BrowserActionTestUtil::GetMinPopupSize() {
220   return gfx::Size(NSSizeToCGSize([ExtensionPopupController minPopupSize]));
223 // static
224 gfx::Size BrowserActionTestUtil::GetMaxPopupSize() {
225   return gfx::Size(NSSizeToCGSize([ExtensionPopupController maxPopupSize]));
228 BrowserActionTestUtil::BrowserActionTestUtil(Browser* browser,
229                                              BrowserActionTestUtil* main_bar)
230     : browser_(browser),
231       test_helper_(new TestToolbarActionsBarHelperCocoa(
232           browser_, GetController(browser_, main_bar->test_helper_.get()))) {