browser_action_test_util_views.cc revision f8ee788a64d60abd8f2d742a5fdedde054ecd910
1// Copyright 2013 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/strings/utf_string_conversions.h"
8#include "chrome/browser/extensions/extension_action_manager.h"
9#include "chrome/browser/ui/browser.h"
10#include "chrome/browser/ui/browser_window.h"
11#include "chrome/browser/ui/views/extensions/extension_popup.h"
12#include "chrome/browser/ui/views/toolbar/browser_action_view.h"
13#include "chrome/browser/ui/views/toolbar/browser_actions_container.h"
14#include "chrome/browser/ui/views/toolbar/toolbar_view.h"
15#include "ui/gfx/image/image.h"
16#include "ui/gfx/rect.h"
17#include "ui/gfx/size.h"
18
19namespace {
20
21BrowserActionsContainer* GetContainer(Browser* browser) {
22  return browser->window()->GetBrowserWindowTesting()->GetToolbarView()->
23      browser_actions();
24}
25
26}  // namespace
27
28int BrowserActionTestUtil::NumberOfBrowserActions() {
29  return GetContainer(browser_)->num_browser_actions();
30}
31
32int BrowserActionTestUtil::VisibleBrowserActions() {
33  return GetContainer(browser_)->VisibleBrowserActions();
34}
35
36ExtensionAction* BrowserActionTestUtil::GetExtensionAction(int index) {
37  return extensions::ExtensionActionManager::Get(browser_->profile())->
38      GetBrowserAction(*GetContainer(browser_)->GetBrowserActionViewAt(index)->
39                       button()->extension());
40}
41
42void BrowserActionTestUtil::InspectPopup(int index) {
43  GetContainer(browser_)->InspectPopup(GetExtensionAction(index));
44}
45
46bool BrowserActionTestUtil::HasIcon(int index) {
47  return !GetContainer(browser_)->GetBrowserActionViewAt(index)->button()->
48      GetImage(views::Button::STATE_NORMAL).isNull();
49}
50
51gfx::Image BrowserActionTestUtil::GetIcon(int index) {
52  gfx::ImageSkia icon = GetContainer(browser_)->GetBrowserActionViewAt(index)->
53      button()->GetIconForTest();
54  return gfx::Image(icon);
55}
56
57void BrowserActionTestUtil::Press(int index) {
58  GetContainer(browser_)->TestExecuteBrowserAction(index);
59}
60
61std::string BrowserActionTestUtil::GetExtensionId(int index) {
62  BrowserActionButton* button =
63      GetContainer(browser_)->GetBrowserActionViewAt(index)->button();
64  return button->extension()->id();
65}
66
67std::string BrowserActionTestUtil::GetTooltip(int index) {
68  base::string16 text;
69  GetContainer(browser_)->GetBrowserActionViewAt(index)->button()->
70    GetTooltipText(gfx::Point(), &text);
71  return base::UTF16ToUTF8(text);
72}
73
74gfx::NativeView BrowserActionTestUtil::GetPopupNativeView() {
75  return GetContainer(browser_)->TestGetPopup()->GetWidget()->GetNativeView();
76}
77
78bool BrowserActionTestUtil::HasPopup() {
79  return GetContainer(browser_)->TestGetPopup() != NULL;
80}
81
82gfx::Rect BrowserActionTestUtil::GetPopupBounds() {
83  return GetContainer(browser_)->TestGetPopup()->bounds();
84}
85
86bool BrowserActionTestUtil::HidePopup() {
87  GetContainer(browser_)->HidePopup();
88  return !HasPopup();
89}
90
91void BrowserActionTestUtil::SetIconVisibilityCount(size_t icons) {
92  GetContainer(browser_)->TestSetIconVisibilityCount(icons);
93}
94
95gfx::Size BrowserActionTestUtil::GetMinPopupSize() {
96  return gfx::Size(ExtensionPopup::kMinWidth, ExtensionPopup::kMinHeight);
97}
98
99gfx::Size BrowserActionTestUtil::GetMaxPopupSize() {
100  return gfx::Size(ExtensionPopup::kMaxWidth, ExtensionPopup::kMaxHeight);
101}
102