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_actions_controller.h"
13 #import "chrome/browser/ui/cocoa/extensions/extension_popup_controller.h"
14 #import "chrome/browser/ui/cocoa/info_bubble_window.h"
15 #import "chrome/browser/ui/cocoa/toolbar/toolbar_controller.h"
16 #include "ui/gfx/rect.h"
17 #include "ui/gfx/size.h"
21 BrowserActionsController* GetController(Browser* browser) {
22 BrowserWindowCocoa* window =
23 static_cast<BrowserWindowCocoa*>(browser->window());
25 return [[window->cocoa_controller() toolbarController]
26 browserActionsController];
29 NSButton* GetButton(Browser* browser, int index) {
30 return [GetController(browser) buttonWithIndex:index];
35 int BrowserActionTestUtil::NumberOfBrowserActions() {
36 return [GetController(browser_) buttonCount];
39 bool BrowserActionTestUtil::HasIcon(int index) {
40 return [GetButton(browser_, index) image] != nil;
43 gfx::Image BrowserActionTestUtil::GetIcon(int index) {
44 NSImage* ns_image = [GetButton(browser_, index) image];
45 // gfx::Image takes ownership of the |ns_image| reference. We have to increase
46 // the ref count so |ns_image| stays around when the image object is
48 base::mac::NSObjectRetain(ns_image);
49 return gfx::Image(ns_image);
52 void BrowserActionTestUtil::Press(int index) {
53 NSButton* button = GetButton(browser_, index);
54 [button performClick:nil];
57 std::string BrowserActionTestUtil::GetTooltip(int index) {
58 NSString* tooltip = [GetButton(browser_, index) toolTip];
59 return base::SysNSStringToUTF8(tooltip);
62 bool BrowserActionTestUtil::HasPopup() {
63 return [ExtensionPopupController popup] != nil;
66 gfx::Rect BrowserActionTestUtil::GetPopupBounds() {
67 NSRect bounds = [[[ExtensionPopupController popup] view] bounds];
68 return gfx::Rect(NSRectToCGRect(bounds));
71 bool BrowserActionTestUtil::HidePopup() {
72 ExtensionPopupController* controller = [ExtensionPopupController popup];
73 // The window must be gone or we'll fail a unit test with windows left open.
74 [static_cast<InfoBubbleWindow*>([controller window])
75 setAllowedAnimations:info_bubble::kAnimateNone];
80 gfx::Size BrowserActionTestUtil::GetMinPopupSize() {
81 return gfx::Size(NSSizeToCGSize([ExtensionPopupController minPopupSize]));
84 gfx::Size BrowserActionTestUtil::GetMaxPopupSize() {
85 return gfx::Size(NSSizeToCGSize([ExtensionPopupController maxPopupSize]));