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 #include "chrome/browser/ui/toolbar/toolbar_action_view_controller.h"
11 #import "chrome/browser/ui/cocoa/browser_window_cocoa.h"
12 #import "chrome/browser/ui/cocoa/browser_window_controller.h"
13 #import "chrome/browser/ui/cocoa/extensions/browser_action_button.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/info_bubble_window.h"
17 #import "chrome/browser/ui/cocoa/toolbar/toolbar_controller.h"
18 #include "ui/gfx/rect.h"
19 #include "ui/gfx/size.h"
23 BrowserActionsController* GetController(Browser* browser) {
24 BrowserWindowCocoa* window =
25 static_cast<BrowserWindowCocoa*>(browser->window());
27 return [[window->cocoa_controller() toolbarController]
28 browserActionsController];
31 BrowserActionButton* GetButton(Browser* browser, int index) {
32 return [GetController(browser) buttonWithIndex:index];
37 int BrowserActionTestUtil::NumberOfBrowserActions() {
38 return [GetController(browser_) buttonCount];
41 int BrowserActionTestUtil::VisibleBrowserActions() {
42 return [GetController(browser_) visibleButtonCount];
45 void BrowserActionTestUtil::InspectPopup(int index) {
49 bool BrowserActionTestUtil::HasIcon(int index) {
50 return [GetButton(browser_, index) image] != nil;
53 gfx::Image BrowserActionTestUtil::GetIcon(int index) {
54 NSImage* ns_image = [GetButton(browser_, index) image];
55 // gfx::Image takes ownership of the |ns_image| reference. We have to increase
56 // the ref count so |ns_image| stays around when the image object is
58 base::mac::NSObjectRetain(ns_image);
59 return gfx::Image(ns_image);
62 void BrowserActionTestUtil::Press(int index) {
63 NSButton* button = GetButton(browser_, index);
64 [button performClick:nil];
67 std::string BrowserActionTestUtil::GetExtensionId(int index) {
68 return [GetButton(browser_, index) viewController]->GetId();
71 std::string BrowserActionTestUtil::GetTooltip(int index) {
72 NSString* tooltip = [GetButton(browser_, index) toolTip];
73 return base::SysNSStringToUTF8(tooltip);
76 gfx::NativeView BrowserActionTestUtil::GetPopupNativeView() {
77 return [[ExtensionPopupController popup] view];
80 bool BrowserActionTestUtil::HasPopup() {
81 return [ExtensionPopupController popup] != nil;
84 gfx::Size BrowserActionTestUtil::GetPopupSize() {
85 NSRect bounds = [[[ExtensionPopupController popup] view] bounds];
86 return gfx::Size(NSSizeToCGSize(bounds.size));
89 bool BrowserActionTestUtil::HidePopup() {
90 ExtensionPopupController* controller = [ExtensionPopupController popup];
91 // The window must be gone or we'll fail a unit test with windows left open.
92 [static_cast<InfoBubbleWindow*>([controller window])
93 setAllowedAnimations:info_bubble::kAnimateNone];
99 void BrowserActionTestUtil::DisableAnimations() {
103 void BrowserActionTestUtil::EnableAnimations() {
107 gfx::Size BrowserActionTestUtil::GetMinPopupSize() {
108 return gfx::Size(NSSizeToCGSize([ExtensionPopupController minPopupSize]));
112 gfx::Size BrowserActionTestUtil::GetMaxPopupSize() {
113 return gfx::Size(NSSizeToCGSize([ExtensionPopupController maxPopupSize]));