Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / extensions / browser_action_test_util_mac.mm
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 "chrome/browser/extensions/browser_action_test_util.h"
6
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"
20
21 namespace {
22
23 BrowserActionsController* GetController(Browser* browser) {
24   BrowserWindowCocoa* window =
25       static_cast<BrowserWindowCocoa*>(browser->window());
26
27   return [[window->cocoa_controller() toolbarController]
28            browserActionsController];
29 }
30
31 BrowserActionButton* GetButton(Browser* browser, int index) {
32   return [GetController(browser) buttonWithIndex:index];
33 }
34
35 }  // namespace
36
37 int BrowserActionTestUtil::NumberOfBrowserActions() {
38   return [GetController(browser_) buttonCount];
39 }
40
41 int BrowserActionTestUtil::VisibleBrowserActions() {
42   return [GetController(browser_) visibleButtonCount];
43 }
44
45 void BrowserActionTestUtil::InspectPopup(int index) {
46   NOTREACHED();
47 }
48
49 bool BrowserActionTestUtil::HasIcon(int index) {
50   return [GetButton(browser_, index) image] != nil;
51 }
52
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
57   // destroyed.
58   base::mac::NSObjectRetain(ns_image);
59   return gfx::Image(ns_image);
60 }
61
62 void BrowserActionTestUtil::Press(int index) {
63   NSButton* button = GetButton(browser_, index);
64   [button performClick:nil];
65 }
66
67 std::string BrowserActionTestUtil::GetExtensionId(int index) {
68   return [GetButton(browser_, index) viewController]->GetId();
69 }
70
71 std::string BrowserActionTestUtil::GetTooltip(int index) {
72   NSString* tooltip = [GetButton(browser_, index) toolTip];
73   return base::SysNSStringToUTF8(tooltip);
74 }
75
76 gfx::NativeView BrowserActionTestUtil::GetPopupNativeView() {
77   return [[ExtensionPopupController popup] view];
78 }
79
80 bool BrowserActionTestUtil::HasPopup() {
81   return [ExtensionPopupController popup] != nil;
82 }
83
84 gfx::Size BrowserActionTestUtil::GetPopupSize() {
85   NSRect bounds = [[[ExtensionPopupController popup] view] bounds];
86   return gfx::Size(NSSizeToCGSize(bounds.size));
87 }
88
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];
94   [controller close];
95   return !HasPopup();
96 }
97
98 // static
99 void BrowserActionTestUtil::DisableAnimations() {
100 }
101
102 // static
103 void BrowserActionTestUtil::EnableAnimations() {
104 }
105
106 // static
107 gfx::Size BrowserActionTestUtil::GetMinPopupSize() {
108   return gfx::Size(NSSizeToCGSize([ExtensionPopupController minPopupSize]));
109 }
110
111 // static
112 gfx::Size BrowserActionTestUtil::GetMaxPopupSize() {
113   return gfx::Size(NSSizeToCGSize([ExtensionPopupController maxPopupSize]));
114 }