browser_action_test_util_mac.mm revision 21d179b334e59e9a3bfcaed4c4430bef1bc5759d
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.
4
5#include "browser_action_test_util.h"
6
7#include "base/sys_string_conversions.h"
8#include "chrome/browser/ui/browser.h"
9#import "chrome/browser/ui/cocoa/browser_window_cocoa.h"
10#import "chrome/browser/ui/cocoa/browser_window_controller.h"
11#import "chrome/browser/ui/cocoa/extensions/browser_actions_controller.h"
12#import "chrome/browser/ui/cocoa/extensions/extension_popup_controller.h"
13#import "chrome/browser/ui/cocoa/info_bubble_window.h"
14#import "chrome/browser/ui/cocoa/toolbar_controller.h"
15#include "gfx/rect.h"
16#include "gfx/size.h"
17
18namespace {
19
20BrowserActionsController* GetController(Browser* browser) {
21  BrowserWindowCocoa* window =
22      static_cast<BrowserWindowCocoa*>(browser->window());
23
24  return [[window->cocoa_controller() toolbarController]
25           browserActionsController];
26}
27
28NSButton* GetButton(Browser* browser, int index) {
29  return [GetController(browser) buttonWithIndex:index];
30}
31
32}  // namespace
33
34int BrowserActionTestUtil::NumberOfBrowserActions() {
35  return [GetController(browser_) buttonCount];
36}
37
38bool BrowserActionTestUtil::HasIcon(int index) {
39  return [GetButton(browser_, index) image] != nil;
40}
41
42void BrowserActionTestUtil::Press(int index) {
43  NSButton* button = GetButton(browser_, index);
44  [button performClick:nil];
45}
46
47std::string BrowserActionTestUtil::GetTooltip(int index) {
48  NSString* tooltip = [GetButton(browser_, index) toolTip];
49  return base::SysNSStringToUTF8(tooltip);
50}
51
52bool BrowserActionTestUtil::HasPopup() {
53  return [ExtensionPopupController popup] != nil;
54}
55
56gfx::Rect BrowserActionTestUtil::GetPopupBounds() {
57  NSRect bounds = [[[ExtensionPopupController popup] view] bounds];
58  return gfx::Rect(NSRectToCGRect(bounds));
59}
60
61bool BrowserActionTestUtil::HidePopup() {
62  ExtensionPopupController* controller = [ExtensionPopupController popup];
63  // The window must be gone or we'll fail a unit test with windows left open.
64  [static_cast<InfoBubbleWindow*>([controller window]) setDelayOnClose:NO];
65  [controller close];
66  return !HasPopup();
67}
68
69gfx::Size BrowserActionTestUtil::GetMinPopupSize() {
70  return gfx::Size(NSSizeToCGSize([ExtensionPopupController minPopupSize]));
71}
72
73gfx::Size BrowserActionTestUtil::GetMaxPopupSize() {
74  return gfx::Size(NSSizeToCGSize([ExtensionPopupController maxPopupSize]));
75}
76