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/foundation_util.h"
8 #include "base/strings/sys_string_conversions.h"
9 #include "chrome/browser/ui/browser.h"
10 #import "chrome/browser/ui/cocoa/browser_window_cocoa.h"
11 #import "chrome/browser/ui/cocoa/browser_window_controller.h"
12 #import "chrome/browser/ui/cocoa/extensions/browser_action_button.h"
13 #import "chrome/browser/ui/cocoa/extensions/browser_actions_container_view.h"
14 #import "chrome/browser/ui/cocoa/extensions/browser_actions_controller.h"
15 #import "chrome/browser/ui/cocoa/extensions/extension_popup_controller.h"
16 #import "chrome/browser/ui/cocoa/toolbar/toolbar_controller.h"
17 #import "chrome/browser/ui/cocoa/toolbar/wrench_toolbar_button_cell.h"
18 #import "chrome/browser/ui/cocoa/info_bubble_window.h"
19 #import "chrome/browser/ui/cocoa/themed_window.h"
20 #import "chrome/browser/ui/cocoa/toolbar/toolbar_controller.h"
21 #include "chrome/browser/ui/toolbar/toolbar_action_view_controller.h"
22 #include "grit/theme_resources.h"
23 #include "ui/base/theme_provider.h"
24 #include "ui/gfx/geometry/rect.h"
25 #include "ui/gfx/geometry/size.h"
29 BrowserActionsController* GetController(
31 ToolbarActionsBarDelegate* barDelegate) {
33 return [BrowserActionsController fromToolbarActionsBarDelegate:barDelegate];
35 BrowserWindowCocoa* window =
36 static_cast<BrowserWindowCocoa*>(browser->window());
37 return [[window->cocoa_controller() toolbarController]
38 browserActionsController];
41 BrowserActionButton* GetButton(
43 ToolbarActionsBarDelegate* barDelegate,
45 return [GetController(browser, barDelegate) buttonWithIndex:index];
50 int BrowserActionTestUtil::NumberOfBrowserActions() {
51 return [GetController(browser_, bar_delegate_) buttonCount];
54 int BrowserActionTestUtil::VisibleBrowserActions() {
55 return [GetController(browser_, bar_delegate_) visibleButtonCount];
58 bool BrowserActionTestUtil::IsChevronShowing() {
59 BrowserActionsController* controller = GetController(browser_, bar_delegate_);
60 // The magic "18" comes from kChevronWidth in browser_actions_controller.mm.
61 return ![controller chevronIsHidden] &&
62 NSWidth([[controller containerView] animationEndFrame]) >= 18;
65 void BrowserActionTestUtil::InspectPopup(int index) {
69 bool BrowserActionTestUtil::HasIcon(int index) {
70 return [GetButton(browser_, bar_delegate_, index) image] != nil;
73 gfx::Image BrowserActionTestUtil::GetIcon(int index) {
74 NSImage* ns_image = [GetButton(browser_, bar_delegate_, index) image];
75 // gfx::Image takes ownership of the |ns_image| reference. We have to increase
76 // the ref count so |ns_image| stays around when the image object is
78 base::mac::NSObjectRetain(ns_image);
79 return gfx::Image(ns_image);
82 void BrowserActionTestUtil::Press(int index) {
83 NSButton* button = GetButton(browser_, bar_delegate_, index);
84 [button performClick:nil];
87 std::string BrowserActionTestUtil::GetExtensionId(int index) {
88 return [GetButton(browser_, bar_delegate_, index) viewController]->GetId();
91 std::string BrowserActionTestUtil::GetTooltip(int index) {
92 NSString* tooltip = [GetButton(browser_, bar_delegate_, index) toolTip];
93 return base::SysNSStringToUTF8(tooltip);
96 gfx::NativeView BrowserActionTestUtil::GetPopupNativeView() {
97 return [[ExtensionPopupController popup] view];
100 bool BrowserActionTestUtil::HasPopup() {
101 return [ExtensionPopupController popup] != nil;
104 gfx::Size BrowserActionTestUtil::GetPopupSize() {
105 NSRect bounds = [[[ExtensionPopupController popup] view] bounds];
106 return gfx::Size(NSSizeToCGSize(bounds.size));
109 bool BrowserActionTestUtil::HidePopup() {
110 ExtensionPopupController* controller = [ExtensionPopupController popup];
111 // The window must be gone or we'll fail a unit test with windows left open.
112 [static_cast<InfoBubbleWindow*>([controller window])
113 setAllowedAnimations:info_bubble::kAnimateNone];
118 bool BrowserActionTestUtil::ActionButtonWantsToRun(size_t index) {
119 BrowserActionsController* controller = GetController(browser_, bar_delegate_);
120 ui::ThemeProvider* themeProvider =
121 [[[controller containerView] window] themeProvider];
122 DCHECK(themeProvider);
123 NSImage* wantsToRunImage =
124 themeProvider->GetNSImageNamed(IDR_BROWSER_ACTION_R);
125 BrowserActionButton* button = [controller buttonWithIndex:index];
126 BrowserActionCell* cell =
127 base::mac::ObjCCastStrict<BrowserActionCell>([button cell]);
128 NSImage* actualImage = [cell imageForState:image_button_cell::kDefaultState
131 return wantsToRunImage == actualImage;
134 bool BrowserActionTestUtil::OverflowedActionButtonWantsToRun() {
135 NSView* wrench = [[[BrowserWindowController browserWindowControllerForWindow:
136 browser_->window()->GetNativeWindow()] toolbarController] wrenchButton];
137 NSButton* wrenchButton = base::mac::ObjCCastStrict<NSButton>(wrench);
138 WrenchToolbarButtonCell* cell =
139 base::mac::ObjCCastStrict<WrenchToolbarButtonCell>([wrenchButton cell]);
140 return [cell overflowedToolbarActionWantsToRun];
143 ToolbarActionsBar* BrowserActionTestUtil::GetToolbarActionsBar() {
144 return [GetController(browser_, bar_delegate_) toolbarActionsBar];
148 gfx::Size BrowserActionTestUtil::GetMinPopupSize() {
149 return gfx::Size(NSSizeToCGSize([ExtensionPopupController minPopupSize]));
153 gfx::Size BrowserActionTestUtil::GetMaxPopupSize() {
154 return gfx::Size(NSSizeToCGSize([ExtensionPopupController maxPopupSize]));