OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/browser_action_test_util.h" | 5 #include "chrome/browser/extensions/browser_action_test_util.h" |
6 | 6 |
7 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
8 #include "chrome/browser/ui/browser.h" | 8 #include "chrome/browser/ui/browser.h" |
9 #include "chrome/browser/ui/browser_window.h" | 9 #include "chrome/browser/ui/browser_window.h" |
10 #include "chrome/browser/ui/extensions/extension_action_view_controller.h" | 10 #include "chrome/browser/ui/extensions/extension_action_view_controller.h" |
11 #include "chrome/browser/ui/views/extensions/extension_popup.h" | 11 #include "chrome/browser/ui/views/extensions/extension_popup.h" |
12 #include "chrome/browser/ui/views/frame/browser_view.h" | 12 #include "chrome/browser/ui/views/frame/browser_view.h" |
13 #include "chrome/browser/ui/views/toolbar/browser_actions_container.h" | 13 #include "chrome/browser/ui/views/toolbar/browser_actions_container.h" |
14 #include "chrome/browser/ui/views/toolbar/toolbar_action_view.h" | 14 #include "chrome/browser/ui/views/toolbar/toolbar_action_view.h" |
15 #include "chrome/browser/ui/views/toolbar/toolbar_view.h" | 15 #include "chrome/browser/ui/views/toolbar/toolbar_view.h" |
16 #include "chrome/browser/ui/views/toolbar/wrench_toolbar_button.h" | 16 #include "chrome/browser/ui/views/toolbar/wrench_toolbar_button.h" |
17 #include "ui/aura/window.h" | 17 #include "ui/aura/window.h" |
18 #include "ui/gfx/geometry/rect.h" | 18 #include "ui/gfx/geometry/rect.h" |
19 #include "ui/gfx/geometry/size.h" | 19 #include "ui/gfx/geometry/size.h" |
20 #include "ui/gfx/image/image.h" | 20 #include "ui/gfx/image/image.h" |
21 #include "ui/views/widget/widget.h" | 21 #include "ui/views/widget/widget.h" |
22 | 22 |
23 namespace { | 23 namespace { |
24 | 24 |
| 25 // The views-specific implementation of the TestToolbarActionsBarHelper, which |
| 26 // creates and owns a BrowserActionsContainer. |
| 27 class TestToolbarActionsBarHelperViews : public TestToolbarActionsBarHelper { |
| 28 public: |
| 29 TestToolbarActionsBarHelperViews(Browser* browser, |
| 30 BrowserActionsContainer* main_bar); |
| 31 ~TestToolbarActionsBarHelperViews() override; |
| 32 |
| 33 BrowserActionsContainer* browser_actions_container() { |
| 34 return browser_actions_container_; |
| 35 } |
| 36 |
| 37 private: |
| 38 // The parent of the BrowserActionsContainer, which directly owns the |
| 39 // container as part of the views hierarchy. |
| 40 views::View container_parent_; |
| 41 |
| 42 // The created BrowserActionsContainer. Owned by |container_parent_|. |
| 43 BrowserActionsContainer* browser_actions_container_; |
| 44 |
| 45 DISALLOW_COPY_AND_ASSIGN(TestToolbarActionsBarHelperViews); |
| 46 }; |
| 47 |
| 48 TestToolbarActionsBarHelperViews::TestToolbarActionsBarHelperViews( |
| 49 Browser* browser, |
| 50 BrowserActionsContainer* main_bar) |
| 51 : browser_actions_container_( |
| 52 new BrowserActionsContainer(browser, main_bar)) { |
| 53 // The BrowserActionsContainer expects to have a parent (and be added to the |
| 54 // view hierarchy), so wrap it in a shell view. |
| 55 container_parent_.set_owned_by_client(); |
| 56 container_parent_.AddChildView(browser_actions_container_); |
| 57 } |
| 58 |
| 59 TestToolbarActionsBarHelperViews::~TestToolbarActionsBarHelperViews() { |
| 60 } |
| 61 |
25 BrowserActionsContainer* GetContainer(Browser* browser, | 62 BrowserActionsContainer* GetContainer(Browser* browser, |
26 ToolbarActionsBarDelegate* bar_delegate) { | 63 TestToolbarActionsBarHelper* helper) { |
27 if (bar_delegate) | 64 if (helper) { |
28 return static_cast<BrowserActionsContainer*>(bar_delegate); | 65 return static_cast<TestToolbarActionsBarHelperViews*>(helper) |
| 66 ->browser_actions_container(); |
| 67 } |
29 return BrowserView::GetBrowserViewForBrowser(browser)->toolbar()-> | 68 return BrowserView::GetBrowserViewForBrowser(browser)->toolbar()-> |
30 browser_actions(); | 69 browser_actions(); |
31 } | 70 } |
32 | 71 |
33 } // namespace | 72 } // namespace |
34 | 73 |
| 74 BrowserActionTestUtil::BrowserActionTestUtil(Browser* browser) |
| 75 : BrowserActionTestUtil(browser, true) { |
| 76 } |
| 77 |
| 78 BrowserActionTestUtil::BrowserActionTestUtil(Browser* browser, |
| 79 bool is_real_window) |
| 80 : browser_(browser) { |
| 81 if (!is_real_window) |
| 82 test_helper_.reset(new TestToolbarActionsBarHelperViews(browser, nullptr)); |
| 83 } |
| 84 |
| 85 BrowserActionTestUtil::~BrowserActionTestUtil() { |
| 86 } |
| 87 |
35 int BrowserActionTestUtil::NumberOfBrowserActions() { | 88 int BrowserActionTestUtil::NumberOfBrowserActions() { |
36 return GetContainer(browser_, bar_delegate_)->num_toolbar_actions(); | 89 return GetContainer(browser_, test_helper_.get())->num_toolbar_actions(); |
37 } | 90 } |
38 | 91 |
39 int BrowserActionTestUtil::VisibleBrowserActions() { | 92 int BrowserActionTestUtil::VisibleBrowserActions() { |
40 return GetContainer(browser_, bar_delegate_)->VisibleBrowserActions(); | 93 return GetContainer(browser_, test_helper_.get())->VisibleBrowserActions(); |
41 } | 94 } |
42 | 95 |
43 bool BrowserActionTestUtil::IsChevronShowing() { | 96 bool BrowserActionTestUtil::IsChevronShowing() { |
44 BrowserActionsContainer* container = GetContainer(browser_, bar_delegate_); | 97 BrowserActionsContainer* container = |
| 98 GetContainer(browser_, test_helper_.get()); |
45 gfx::Size visible_size = container->GetVisibleBounds().size(); | 99 gfx::Size visible_size = container->GetVisibleBounds().size(); |
46 return container->chevron() && | 100 return container->chevron() && |
47 container->chevron()->visible() && | 101 container->chevron()->visible() && |
48 visible_size.width() >= | 102 visible_size.width() >= |
49 container->chevron()->GetPreferredSize().width() && | 103 container->chevron()->GetPreferredSize().width() && |
50 !visible_size.IsEmpty(); | 104 !visible_size.IsEmpty(); |
51 } | 105 } |
52 | 106 |
53 void BrowserActionTestUtil::InspectPopup(int index) { | 107 void BrowserActionTestUtil::InspectPopup(int index) { |
54 ToolbarActionView* view = | 108 ToolbarActionView* view = |
55 GetContainer(browser_, bar_delegate_)->GetToolbarActionViewAt(index); | 109 GetContainer(browser_, test_helper_.get())->GetToolbarActionViewAt(index); |
56 static_cast<ExtensionActionViewController*>(view->view_controller())-> | 110 static_cast<ExtensionActionViewController*>(view->view_controller())-> |
57 InspectPopup(); | 111 InspectPopup(); |
58 } | 112 } |
59 | 113 |
60 bool BrowserActionTestUtil::HasIcon(int index) { | 114 bool BrowserActionTestUtil::HasIcon(int index) { |
61 return !GetContainer(browser_, bar_delegate_)->GetToolbarActionViewAt(index)-> | 115 return !GetContainer(browser_, test_helper_.get()) |
62 GetImage(views::Button::STATE_NORMAL).isNull(); | 116 ->GetToolbarActionViewAt(index) |
| 117 ->GetImage(views::Button::STATE_NORMAL) |
| 118 .isNull(); |
63 } | 119 } |
64 | 120 |
65 gfx::Image BrowserActionTestUtil::GetIcon(int index) { | 121 gfx::Image BrowserActionTestUtil::GetIcon(int index) { |
66 gfx::ImageSkia icon = | 122 gfx::ImageSkia icon = GetContainer(browser_, test_helper_.get()) |
67 GetContainer(browser_, bar_delegate_)->GetToolbarActionViewAt(index)-> | 123 ->GetToolbarActionViewAt(index) |
68 GetIconForTest(); | 124 ->GetIconForTest(); |
69 return gfx::Image(icon); | 125 return gfx::Image(icon); |
70 } | 126 } |
71 | 127 |
72 void BrowserActionTestUtil::Press(int index) { | 128 void BrowserActionTestUtil::Press(int index) { |
73 GetContainer(browser_, bar_delegate_)->GetToolbarActionViewAt(index)-> | 129 GetContainer(browser_, test_helper_.get()) |
74 view_controller()->ExecuteAction(true); | 130 ->GetToolbarActionViewAt(index) |
| 131 ->view_controller() |
| 132 ->ExecuteAction(true); |
75 } | 133 } |
76 | 134 |
77 std::string BrowserActionTestUtil::GetExtensionId(int index) { | 135 std::string BrowserActionTestUtil::GetExtensionId(int index) { |
78 return GetContainer(browser_, bar_delegate_)->GetToolbarActionViewAt(index)-> | 136 return GetContainer(browser_, test_helper_.get()) |
79 view_controller()->GetId(); | 137 ->GetToolbarActionViewAt(index) |
| 138 ->view_controller() |
| 139 ->GetId(); |
80 } | 140 } |
81 | 141 |
82 std::string BrowserActionTestUtil::GetTooltip(int index) { | 142 std::string BrowserActionTestUtil::GetTooltip(int index) { |
83 base::string16 text; | 143 base::string16 text; |
84 GetContainer(browser_, bar_delegate_)->GetToolbarActionViewAt(index)-> | 144 GetContainer(browser_, test_helper_.get()) |
85 GetTooltipText(gfx::Point(), &text); | 145 ->GetToolbarActionViewAt(index) |
| 146 ->GetTooltipText(gfx::Point(), &text); |
86 return base::UTF16ToUTF8(text); | 147 return base::UTF16ToUTF8(text); |
87 } | 148 } |
88 | 149 |
89 gfx::NativeView BrowserActionTestUtil::GetPopupNativeView() { | 150 gfx::NativeView BrowserActionTestUtil::GetPopupNativeView() { |
90 ToolbarActionViewController* popup_owner = | 151 ToolbarActionViewController* popup_owner = |
91 GetToolbarActionsBar()->popup_owner(); | 152 GetToolbarActionsBar()->popup_owner(); |
92 return popup_owner ? popup_owner->GetPopupNativeView() : nullptr; | 153 return popup_owner ? popup_owner->GetPopupNativeView() : nullptr; |
93 } | 154 } |
94 | 155 |
95 bool BrowserActionTestUtil::HasPopup() { | 156 bool BrowserActionTestUtil::HasPopup() { |
96 return GetPopupNativeView() != nullptr; | 157 return GetPopupNativeView() != nullptr; |
97 } | 158 } |
98 | 159 |
99 gfx::Size BrowserActionTestUtil::GetPopupSize() { | 160 gfx::Size BrowserActionTestUtil::GetPopupSize() { |
100 gfx::NativeView popup = GetPopupNativeView(); | 161 gfx::NativeView popup = GetPopupNativeView(); |
101 views::Widget* widget = views::Widget::GetWidgetForNativeView(popup); | 162 views::Widget* widget = views::Widget::GetWidgetForNativeView(popup); |
102 return widget->GetWindowBoundsInScreen().size(); | 163 return widget->GetWindowBoundsInScreen().size(); |
103 } | 164 } |
104 | 165 |
105 bool BrowserActionTestUtil::HidePopup() { | 166 bool BrowserActionTestUtil::HidePopup() { |
106 GetToolbarActionsBar()->HideActivePopup(); | 167 GetToolbarActionsBar()->HideActivePopup(); |
107 return !HasPopup(); | 168 return !HasPopup(); |
108 } | 169 } |
109 | 170 |
110 bool BrowserActionTestUtil::ActionButtonWantsToRun(size_t index) { | 171 bool BrowserActionTestUtil::ActionButtonWantsToRun(size_t index) { |
111 return GetContainer(browser_, bar_delegate_)->GetToolbarActionViewAt(index)-> | 172 return GetContainer(browser_, test_helper_.get()) |
112 wants_to_run_for_testing(); | 173 ->GetToolbarActionViewAt(index) |
| 174 ->wants_to_run_for_testing(); |
113 } | 175 } |
114 | 176 |
115 bool BrowserActionTestUtil::OverflowedActionButtonWantsToRun() { | 177 bool BrowserActionTestUtil::OverflowedActionButtonWantsToRun() { |
116 return BrowserView::GetBrowserViewForBrowser(browser_)->toolbar()-> | 178 return BrowserView::GetBrowserViewForBrowser(browser_)->toolbar()-> |
117 app_menu()->overflowed_toolbar_action_wants_to_run_for_testing(); | 179 app_menu()->overflowed_toolbar_action_wants_to_run_for_testing(); |
118 } | 180 } |
119 | 181 |
120 ToolbarActionsBar* BrowserActionTestUtil::GetToolbarActionsBar() { | 182 ToolbarActionsBar* BrowserActionTestUtil::GetToolbarActionsBar() { |
121 return GetContainer(browser_, bar_delegate_)->toolbar_actions_bar(); | 183 return GetContainer(browser_, test_helper_.get())->toolbar_actions_bar(); |
| 184 } |
| 185 |
| 186 scoped_ptr<BrowserActionTestUtil> BrowserActionTestUtil::CreateOverflowBar() { |
| 187 CHECK(!GetToolbarActionsBar()->in_overflow_mode()) |
| 188 << "Only a main bar can create an overflow bar!"; |
| 189 return make_scoped_ptr(new BrowserActionTestUtil(browser_, this)); |
122 } | 190 } |
123 | 191 |
124 // static | 192 // static |
125 gfx::Size BrowserActionTestUtil::GetMinPopupSize() { | 193 gfx::Size BrowserActionTestUtil::GetMinPopupSize() { |
126 return gfx::Size(ExtensionPopup::kMinWidth, ExtensionPopup::kMinHeight); | 194 return gfx::Size(ExtensionPopup::kMinWidth, ExtensionPopup::kMinHeight); |
127 } | 195 } |
128 | 196 |
129 // static | 197 // static |
130 gfx::Size BrowserActionTestUtil::GetMaxPopupSize() { | 198 gfx::Size BrowserActionTestUtil::GetMaxPopupSize() { |
131 return gfx::Size(ExtensionPopup::kMaxWidth, ExtensionPopup::kMaxHeight); | 199 return gfx::Size(ExtensionPopup::kMaxWidth, ExtensionPopup::kMaxHeight); |
132 } | 200 } |
| 201 |
| 202 BrowserActionTestUtil::BrowserActionTestUtil(Browser* browser, |
| 203 BrowserActionTestUtil* main_bar) |
| 204 : browser_(browser), |
| 205 test_helper_(new TestToolbarActionsBarHelperViews( |
| 206 browser_, |
| 207 GetContainer(browser_, main_bar->test_helper_.get()))) { |
| 208 } |
OLD | NEW |