Home | History | Annotate | Download | only in extensions
      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/toolbar_controller.h"
     15 #include "ui/gfx/rect.h"
     16 #include "ui/gfx/size.h"
     17 
     18 namespace {
     19 
     20 BrowserActionsController* GetController(Browser* browser) {
     21   BrowserWindowCocoa* window =
     22       static_cast<BrowserWindowCocoa*>(browser->window());
     23 
     24   return [[window->cocoa_controller() toolbarController]
     25            browserActionsController];
     26 }
     27 
     28 NSButton* GetButton(Browser* browser, int index) {
     29   return [GetController(browser) buttonWithIndex:index];
     30 }
     31 
     32 }  // namespace
     33 
     34 int BrowserActionTestUtil::NumberOfBrowserActions() {
     35   return [GetController(browser_) buttonCount];
     36 }
     37 
     38 bool BrowserActionTestUtil::HasIcon(int index) {
     39   return [GetButton(browser_, index) image] != nil;
     40 }
     41 
     42 void BrowserActionTestUtil::Press(int index) {
     43   NSButton* button = GetButton(browser_, index);
     44   [button performClick:nil];
     45 }
     46 
     47 std::string BrowserActionTestUtil::GetTooltip(int index) {
     48   NSString* tooltip = [GetButton(browser_, index) toolTip];
     49   return base::SysNSStringToUTF8(tooltip);
     50 }
     51 
     52 bool BrowserActionTestUtil::HasPopup() {
     53   return [ExtensionPopupController popup] != nil;
     54 }
     55 
     56 gfx::Rect BrowserActionTestUtil::GetPopupBounds() {
     57   NSRect bounds = [[[ExtensionPopupController popup] view] bounds];
     58   return gfx::Rect(NSRectToCGRect(bounds));
     59 }
     60 
     61 bool 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 
     69 gfx::Size BrowserActionTestUtil::GetMinPopupSize() {
     70   return gfx::Size(NSSizeToCGSize([ExtensionPopupController minPopupSize]));
     71 }
     72 
     73 gfx::Size BrowserActionTestUtil::GetMaxPopupSize() {
     74   return gfx::Size(NSSizeToCGSize([ExtensionPopupController maxPopupSize]));
     75 }
     76