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