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_controller.h"
14 #import "chrome/browser/ui/cocoa/extensions/extension_popup_controller.h"
15 #import "chrome/browser/ui/cocoa/info_bubble_window.h"
16 #import "chrome/browser/ui/cocoa/toolbar/toolbar_controller.h"
17 #include "ui/gfx/rect.h"
18 #include "ui/gfx/size.h"
22 BrowserActionsController* GetController(Browser* browser) {
23 BrowserWindowCocoa* window =
24 static_cast<BrowserWindowCocoa*>(browser->window());
26 return [[window->cocoa_controller() toolbarController]
27 browserActionsController];
30 BrowserActionButton* GetButton(Browser* browser, int index) {
31 return [GetController(browser) buttonWithIndex:index];
36 int BrowserActionTestUtil::NumberOfBrowserActions() {
37 return [GetController(browser_) buttonCount];
40 int BrowserActionTestUtil::VisibleBrowserActions() {
41 return [GetController(browser_) visibleButtonCount];
44 ExtensionAction* BrowserActionTestUtil::GetExtensionAction(int index) {
49 void BrowserActionTestUtil::InspectPopup(int index) {
53 bool BrowserActionTestUtil::HasIcon(int index) {
54 return [GetButton(browser_, index) image] != nil;
57 gfx::Image BrowserActionTestUtil::GetIcon(int index) {
58 NSImage* ns_image = [GetButton(browser_, index) image];
59 // gfx::Image takes ownership of the |ns_image| reference. We have to increase
60 // the ref count so |ns_image| stays around when the image object is
62 base::mac::NSObjectRetain(ns_image);
63 return gfx::Image(ns_image);
66 void BrowserActionTestUtil::Press(int index) {
67 NSButton* button = GetButton(browser_, index);
68 [button performClick:nil];
71 std::string BrowserActionTestUtil::GetExtensionId(int index) {
72 return [GetButton(browser_, index) extension]->id();
75 std::string BrowserActionTestUtil::GetTooltip(int index) {
76 NSString* tooltip = [GetButton(browser_, index) toolTip];
77 return base::SysNSStringToUTF8(tooltip);
80 gfx::NativeView BrowserActionTestUtil::GetPopupNativeView() {
81 return [[ExtensionPopupController popup] view];
84 bool BrowserActionTestUtil::HasPopup() {
85 return [ExtensionPopupController popup] != nil;
88 gfx::Rect BrowserActionTestUtil::GetPopupBounds() {
89 NSRect bounds = [[[ExtensionPopupController popup] view] bounds];
90 return gfx::Rect(NSRectToCGRect(bounds));
93 bool BrowserActionTestUtil::HidePopup() {
94 ExtensionPopupController* controller = [ExtensionPopupController popup];
95 // The window must be gone or we'll fail a unit test with windows left open.
96 [static_cast<InfoBubbleWindow*>([controller window])
97 setAllowedAnimations:info_bubble::kAnimateNone];
103 void BrowserActionTestUtil::DisableAnimations() {
107 void BrowserActionTestUtil::EnableAnimations() {
111 gfx::Size BrowserActionTestUtil::GetMinPopupSize() {
112 return gfx::Size(NSSizeToCGSize([ExtensionPopupController minPopupSize]));
116 gfx::Size BrowserActionTestUtil::GetMaxPopupSize() {
117 return gfx::Size(NSSizeToCGSize([ExtensionPopupController maxPopupSize]));