Create a cross-platform browsertest for the browser actions bar
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / extensions / browser_action_test_util_mac.mm
bloba2511fd018d180bbc781e72af678bae6b3604624
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"
20 namespace {
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];
34 }  // namespace
36 int BrowserActionTestUtil::NumberOfBrowserActions() {
37   return [GetController(browser_) buttonCount];
40 int BrowserActionTestUtil::VisibleBrowserActions() {
41   return [GetController(browser_) visibleButtonCount];
44 ExtensionAction* BrowserActionTestUtil::GetExtensionAction(int index) {
45   NOTREACHED();
46   return NULL;
49 void BrowserActionTestUtil::InspectPopup(int index) {
50   NOTREACHED();
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
61   // destroyed.
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];
98   [controller close];
99   return !HasPopup();
102 // static
103 void BrowserActionTestUtil::DisableAnimations() {
106 // static
107 void BrowserActionTestUtil::EnableAnimations() {
110 // static
111 gfx::Size BrowserActionTestUtil::GetMinPopupSize() {
112   return gfx::Size(NSSizeToCGSize([ExtensionPopupController minPopupSize]));
115 // static
116 gfx::Size BrowserActionTestUtil::GetMaxPopupSize() {
117   return gfx::Size(NSSizeToCGSize([ExtensionPopupController maxPopupSize]));