Commit bb461c8f authored by rdevlin.cronin's avatar rdevlin.cronin Committed by Commit bot

[Extensions Toolbar Views] Add a ToolbarActionView unittest

Add unittesting for ToolbarActionView to, at minimum, test the basic UI. Also
remove the dependency of ToolbarActionView on Browser, and create a
TestToolbarActionViewController.

BUG=453841
TBR=sky@chromium.org (gn file change)

Review URL: https://codereview.chromium.org/888043003

Cr-Commit-Position: refs/heads/master@{#315130}
parent 69cccfce
...@@ -510,6 +510,8 @@ source_set("test_support") { ...@@ -510,6 +510,8 @@ source_set("test_support") {
"pdf/pdf_browsertest_base.h", "pdf/pdf_browsertest_base.h",
"test/test_confirm_bubble_model.cc", "test/test_confirm_bubble_model.cc",
"test/test_confirm_bubble_model.h", "test/test_confirm_bubble_model.h",
"toolbar/test_toolbar_action_view_controller.cc",
"toolbar/test_toolbar_action_view_controller.h",
"website_settings/mock_permission_bubble_request.cc", "website_settings/mock_permission_bubble_request.cc",
"website_settings/mock_permission_bubble_request.h", "website_settings/mock_permission_bubble_request.h",
] ]
......
...@@ -4,77 +4,18 @@ ...@@ -4,77 +4,18 @@
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/scoped_ptr.h" #include "base/memory/scoped_ptr.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/extensions/browser_action_test_util.h" #include "chrome/browser/extensions/browser_action_test_util.h"
#include "chrome/browser/ui/browser.h" #include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/toolbar/component_toolbar_actions_factory.h" #include "chrome/browser/ui/toolbar/component_toolbar_actions_factory.h"
#include "chrome/browser/ui/toolbar/test_toolbar_action_view_controller.h"
#include "chrome/browser/ui/toolbar/toolbar_action_view_controller.h" #include "chrome/browser/ui/toolbar/toolbar_action_view_controller.h"
#include "chrome/test/base/in_process_browser_test.h" #include "chrome/test/base/in_process_browser_test.h"
#include "extensions/common/feature_switch.h" #include "extensions/common/feature_switch.h"
#include "grit/theme_resources.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_skia.h"
namespace { namespace {
const char kMockId[] = "mock_action"; const char kMockId[] = "mock_action";
class MockComponentAction : public ToolbarActionViewController {
public:
MockComponentAction() : click_count_(0u), id_(kMockId) {}
~MockComponentAction() override {}
// ToolbarActionButtonController:
const std::string& GetId() const override { return id_; }
void SetDelegate(ToolbarActionViewDelegate* delegate) override {}
gfx::Image GetIcon(content::WebContents* web_contents) override {
return ui::ResourceBundle::GetSharedInstance().GetImageNamed(
IDR_BROWSER_ACTION);
}
gfx::ImageSkia GetIconWithBadge() override {
return *GetIcon(nullptr).ToImageSkia();
}
base::string16 GetActionName() const override {
return base::ASCIIToUTF16("Component Action");
}
base::string16 GetAccessibleName(content::WebContents* web_contents)
const override {
return GetActionName();
}
base::string16 GetTooltip(content::WebContents* web_contents)
const override {
return GetActionName();
}
bool IsEnabled(content::WebContents* web_contents) const override {
return true;
}
bool WantsToRun(content::WebContents* web_contents) const override {
return false;
}
bool HasPopup(content::WebContents* web_contents) const override {
return true;
}
void HidePopup() override {}
gfx::NativeView GetPopupNativeView() override { return nullptr; }
ui::MenuModel* GetContextMenu() override { return nullptr; }
bool CanDrag() const override { return false; }
bool IsMenuRunning() const override { return false; }
bool ExecuteAction(bool by_user) override {
++click_count_;
return false;
}
void UpdateState() override {}
size_t click_count() const { return click_count_; }
private:
size_t click_count_;
std::string id_;
DISALLOW_COPY_AND_ASSIGN(MockComponentAction);
};
class MockComponentToolbarActionsFactory class MockComponentToolbarActionsFactory
: public ComponentToolbarActionsFactory { : public ComponentToolbarActionsFactory {
public: public:
...@@ -85,13 +26,13 @@ class MockComponentToolbarActionsFactory ...@@ -85,13 +26,13 @@ class MockComponentToolbarActionsFactory
ScopedVector<ToolbarActionViewController> GetComponentToolbarActions() ScopedVector<ToolbarActionViewController> GetComponentToolbarActions()
override; override;
const std::vector<MockComponentAction*>& weak_actions() const { const std::vector<TestToolbarActionViewController*>& weak_actions() const {
return weak_actions_; return weak_actions_;
} }
private: private:
// A (weak) set of all created actions. // A (weak) set of all created actions.
std::vector<MockComponentAction*> weak_actions_; std::vector<TestToolbarActionViewController*> weak_actions_;
DISALLOW_COPY_AND_ASSIGN(MockComponentToolbarActionsFactory); DISALLOW_COPY_AND_ASSIGN(MockComponentToolbarActionsFactory);
}; };
...@@ -107,7 +48,8 @@ MockComponentToolbarActionsFactory::~MockComponentToolbarActionsFactory() { ...@@ -107,7 +48,8 @@ MockComponentToolbarActionsFactory::~MockComponentToolbarActionsFactory() {
ScopedVector<ToolbarActionViewController> ScopedVector<ToolbarActionViewController>
MockComponentToolbarActionsFactory::GetComponentToolbarActions() { MockComponentToolbarActionsFactory::GetComponentToolbarActions() {
ScopedVector<ToolbarActionViewController> component_actions; ScopedVector<ToolbarActionViewController> component_actions;
MockComponentAction* action = new MockComponentAction(); TestToolbarActionViewController* action =
new TestToolbarActionViewController(kMockId);
component_actions.push_back(action); component_actions.push_back(action);
weak_actions_.push_back(action); weak_actions_.push_back(action);
return component_actions.Pass(); return component_actions.Pass();
...@@ -152,14 +94,14 @@ IN_PROC_BROWSER_TEST_F(ComponentToolbarActionsBrowserTest, ...@@ -152,14 +94,14 @@ IN_PROC_BROWSER_TEST_F(ComponentToolbarActionsBrowserTest,
EXPECT_EQ(kMockId, browser_actions_bar.GetExtensionId(0)); EXPECT_EQ(kMockId, browser_actions_bar.GetExtensionId(0));
// There should only have been one created component action. // There should only have been one created component action.
const std::vector<MockComponentAction*> weak_actions = const std::vector<TestToolbarActionViewController*>& weak_actions =
mock_factory()->weak_actions(); mock_factory()->weak_actions();
ASSERT_EQ(1u, weak_actions.size()); ASSERT_EQ(1u, weak_actions.size());
MockComponentAction* mock_component_action = weak_actions[0]; TestToolbarActionViewController* mock_component_action = weak_actions[0];
ASSERT_TRUE(mock_component_action); ASSERT_TRUE(mock_component_action);
// Test that clicking on the component action works. // Test that clicking on the component action works.
EXPECT_EQ(0u, mock_component_action->click_count()); EXPECT_EQ(0, mock_component_action->execute_action_count());
browser_actions_bar.Press(0); browser_actions_bar.Press(0);
EXPECT_EQ(1u, mock_component_action->click_count()); EXPECT_EQ(1, mock_component_action->execute_action_count());
} }
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/toolbar/test_toolbar_action_view_controller.h"
#include "base/strings/string16.h"
#include "chrome/browser/ui/toolbar/toolbar_action_view_delegate.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_skia.h"
TestToolbarActionViewController::TestToolbarActionViewController(
const std::string& id)
: id_(id),
delegate_(nullptr),
is_enabled_(true),
wants_to_run_(false),
execute_action_count_(0) {
}
TestToolbarActionViewController::~TestToolbarActionViewController() {
}
const std::string& TestToolbarActionViewController::GetId() const {
return id_;
}
void TestToolbarActionViewController::SetDelegate(
ToolbarActionViewDelegate* delegate) {
delegate_ = delegate;
}
gfx::Image TestToolbarActionViewController::GetIcon(
content::WebContents* web_contents) {
return gfx::Image();
}
gfx::ImageSkia TestToolbarActionViewController::GetIconWithBadge() {
return gfx::ImageSkia();
}
base::string16 TestToolbarActionViewController::GetActionName() const {
return base::string16();
}
base::string16 TestToolbarActionViewController::GetAccessibleName(
content::WebContents* web_contents) const {
return accessible_name_;
}
base::string16 TestToolbarActionViewController::GetTooltip(
content::WebContents* web_contents) const {
return tooltip_;
}
bool TestToolbarActionViewController::IsEnabled(
content::WebContents* web_contents) const {
return is_enabled_;
}
bool TestToolbarActionViewController::WantsToRun(
content::WebContents* web_contents) const {
return wants_to_run_;
}
bool TestToolbarActionViewController::HasPopup(
content::WebContents* web_contents) const {
return true;
}
void TestToolbarActionViewController::HidePopup() {
delegate_->OnPopupClosed();
}
gfx::NativeView TestToolbarActionViewController::GetPopupNativeView() {
return nullptr;
}
ui::MenuModel* TestToolbarActionViewController::GetContextMenu() {
return nullptr;
}
bool TestToolbarActionViewController::IsMenuRunning() const {
return false;
}
bool TestToolbarActionViewController::CanDrag() const {
return false;
}
bool TestToolbarActionViewController::ExecuteAction(bool by_user) {
++execute_action_count_;
return false;
}
void TestToolbarActionViewController::UpdateState() {
UpdateDelegate();
}
void TestToolbarActionViewController::ShowPopup(bool by_user) {
delegate_->OnPopupShown(by_user);
}
void TestToolbarActionViewController::SetAccessibleName(
const base::string16& name) {
accessible_name_ = name;
UpdateDelegate();
}
void TestToolbarActionViewController::SetTooltip(
const base::string16& tooltip) {
tooltip_ = tooltip;
UpdateDelegate();
}
void TestToolbarActionViewController::SetEnabled(bool is_enabled) {
is_enabled_ = is_enabled;
UpdateDelegate();
}
void TestToolbarActionViewController::SetWantsToRun(bool wants_to_run) {
wants_to_run_ = wants_to_run;
UpdateDelegate();
}
void TestToolbarActionViewController::UpdateDelegate() {
if (delegate_)
delegate_->UpdateState();
}
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_UI_TOOLBAR_TEST_TOOLBAR_ACTION_VIEW_CONTROLLER_H_
#define CHROME_BROWSER_UI_TOOLBAR_TEST_TOOLBAR_ACTION_VIEW_CONTROLLER_H_
#include "chrome/browser/ui/toolbar/toolbar_action_view_controller.h"
// A minimalistic and configurable ToolbarActionViewController for use in
// testing.
class TestToolbarActionViewController : public ToolbarActionViewController {
public:
explicit TestToolbarActionViewController(const std::string& id);
~TestToolbarActionViewController() override;
// ToolbarActionViewController:
const std::string& GetId() const override;
void SetDelegate(ToolbarActionViewDelegate* delegate) override;
gfx::Image GetIcon(content::WebContents* web_contents) override;
gfx::ImageSkia GetIconWithBadge() override;
base::string16 GetActionName() const override;
base::string16 GetAccessibleName(content::WebContents* web_contents)
const override;
base::string16 GetTooltip(content::WebContents* web_contents)
const override;
bool IsEnabled(content::WebContents* web_contents) const override;
bool WantsToRun(content::WebContents* web_contents) const override;
bool HasPopup(content::WebContents* web_contents) const override;
void HidePopup() override;
gfx::NativeView GetPopupNativeView() override;
ui::MenuModel* GetContextMenu() override;
bool IsMenuRunning() const override;
bool CanDrag() const override;
bool ExecuteAction(bool by_user) override;
void UpdateState() override;
// Instruct the controller to fake showing a popup.
void ShowPopup(bool by_user);
// Configure the test controller. These also call UpdateDelegate().
void SetAccessibleName(const base::string16& name);
void SetTooltip(const base::string16& tooltip);
void SetEnabled(bool is_enabled);
void SetWantsToRun(bool wants_to_run);
int execute_action_count() const { return execute_action_count_; }
private:
// Updates the delegate, if one exists.
void UpdateDelegate();
// The id of the controller.
std::string id_;
// The delegate of the controller, if one exists.
ToolbarActionViewDelegate* delegate_;
// The optional accessible name and tooltip; by default these are empty.
base::string16 accessible_name_;
base::string16 tooltip_;
// Whether or not the action is enabled. Defaults to true.
bool is_enabled_;
// Whether or not the action wants to run. Defaults to false.
bool wants_to_run_;
// The number of times the action would have been executed.
int execute_action_count_;
DISALLOW_COPY_AND_ASSIGN(TestToolbarActionViewController);
};
#endif // CHROME_BROWSER_UI_TOOLBAR_TEST_TOOLBAR_ACTION_VIEW_CONTROLLER_H_
...@@ -227,7 +227,7 @@ void BrowserActionsContainer::AddViewForAction( ...@@ -227,7 +227,7 @@ void BrowserActionsContainer::AddViewForAction(
view_controller->GetActionName(); view_controller->GetActionName();
ToolbarActionView* view = ToolbarActionView* view =
new ToolbarActionView(view_controller, browser_, this); new ToolbarActionView(view_controller, browser_->profile(), this);
toolbar_action_views_.insert(toolbar_action_views_.begin() + index, view); toolbar_action_views_.insert(toolbar_action_views_.begin() + index, view);
AddChildViewAt(view, index); AddChildViewAt(view, index);
} }
......
...@@ -233,7 +233,7 @@ bool ChevronMenuButton::MenuController::ShowContextMenu( ...@@ -233,7 +233,7 @@ bool ChevronMenuButton::MenuController::ShowContextMenu(
scoped_refptr<ExtensionContextMenuModel> context_menu_contents = scoped_refptr<ExtensionContextMenuModel> context_menu_contents =
new ExtensionContextMenuModel(view_controller->extension(), new ExtensionContextMenuModel(view_controller->extension(),
view->browser(), view_controller->browser(),
view_controller); view_controller);
views::MenuRunner context_menu_runner(context_menu_contents.get(), views::MenuRunner context_menu_runner(context_menu_contents.get(),
views::MenuRunner::HAS_MNEMONICS | views::MenuRunner::HAS_MNEMONICS |
......
...@@ -11,14 +11,11 @@ ...@@ -11,14 +11,11 @@
#include "chrome/browser/sessions/session_tab_helper.h" #include "chrome/browser/sessions/session_tab_helper.h"
#include "chrome/browser/themes/theme_service.h" #include "chrome/browser/themes/theme_service.h"
#include "chrome/browser/themes/theme_service_factory.h" #include "chrome/browser/themes/theme_service_factory.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/toolbar/toolbar_action_view_controller.h" #include "chrome/browser/ui/toolbar/toolbar_action_view_controller.h"
#include "chrome/browser/ui/toolbar/toolbar_actions_bar.h" #include "chrome/browser/ui/toolbar/toolbar_actions_bar.h"
#include "chrome/browser/ui/view_ids.h" #include "chrome/browser/ui/view_ids.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/toolbar/toolbar_view.h"
#include "chrome/browser/ui/views/toolbar/wrench_toolbar_button.h"
#include "chrome/grit/generated_resources.h" #include "chrome/grit/generated_resources.h"
#include "content/public/browser/notification_source.h"
#include "grit/theme_resources.h" #include "grit/theme_resources.h"
#include "ui/accessibility/ax_view_state.h" #include "ui/accessibility/ax_view_state.h"
#include "ui/base/resource/resource_bundle.h" #include "ui/base/resource/resource_bundle.h"
...@@ -44,11 +41,11 @@ const int kBorderInset = 4; ...@@ -44,11 +41,11 @@ const int kBorderInset = 4;
ToolbarActionView::ToolbarActionView( ToolbarActionView::ToolbarActionView(
ToolbarActionViewController* view_controller, ToolbarActionViewController* view_controller,
Browser* browser, Profile* profile,
ToolbarActionView::Delegate* delegate) ToolbarActionView::Delegate* delegate)
: MenuButton(this, base::string16(), NULL, false), : MenuButton(this, base::string16(), NULL, false),
view_controller_(view_controller), view_controller_(view_controller),
browser_(browser), profile_(profile),
delegate_(delegate), delegate_(delegate),
called_register_command_(false), called_register_command_(false),
wants_to_run_(false) { wants_to_run_(false) {
...@@ -64,7 +61,7 @@ ToolbarActionView::ToolbarActionView( ...@@ -64,7 +61,7 @@ ToolbarActionView::ToolbarActionView(
this, this,
chrome::NOTIFICATION_BROWSER_THEME_CHANGED, chrome::NOTIFICATION_BROWSER_THEME_CHANGED,
content::Source<ThemeService>( content::Source<ThemeService>(
ThemeServiceFactory::GetForProfile(browser->profile()))); ThemeServiceFactory::GetForProfile(profile_)));
wants_to_run_border_ = CreateDefaultBorder(); wants_to_run_border_ = CreateDefaultBorder();
DecorateWantsToRunBorder(wants_to_run_border_.get()); DecorateWantsToRunBorder(wants_to_run_border_.get());
...@@ -149,8 +146,7 @@ void ToolbarActionView::UpdateState() { ...@@ -149,8 +146,7 @@ void ToolbarActionView::UpdateState() {
gfx::ImageSkia icon(view_controller_->GetIcon(web_contents).AsImageSkia()); gfx::ImageSkia icon(view_controller_->GetIcon(web_contents).AsImageSkia());
if (!icon.isNull()) { if (!icon.isNull()) {
ThemeService* theme = ThemeService* theme = ThemeServiceFactory::GetForProfile(profile_);
ThemeServiceFactory::GetForProfile(browser_->profile());
gfx::ImageSkia bg = *theme->GetImageSkiaNamed(IDR_BROWSER_ACTION); gfx::ImageSkia bg = *theme->GetImageSkiaNamed(IDR_BROWSER_ACTION);
SetImage(views::Button::STATE_NORMAL, SetImage(views::Button::STATE_NORMAL,
...@@ -253,8 +249,7 @@ views::Widget* ToolbarActionView::GetParentForContextMenu() { ...@@ -253,8 +249,7 @@ views::Widget* ToolbarActionView::GetParentForContextMenu() {
// RunMenuAt expects a nested menu to be parented by the same widget as the // RunMenuAt expects a nested menu to be parented by the same widget as the
// already visible menu, in this case the Chrome menu. // already visible menu, in this case the Chrome menu.
return delegate_->ShownInsideMenu() ? return delegate_->ShownInsideMenu() ?
BrowserView::GetBrowserViewForBrowser(browser_) delegate_->GetOverflowReferenceView()->GetWidget() :
->toolbar()->app_menu()->GetWidget() :
GetWidget(); GetWidget();
} }
......
...@@ -13,8 +13,8 @@ ...@@ -13,8 +13,8 @@
#include "ui/views/drag_controller.h" #include "ui/views/drag_controller.h"
#include "ui/views/view.h" #include "ui/views/view.h"
class Browser;
class ExtensionAction; class ExtensionAction;
class Profile;
namespace extensions { namespace extensions {
class Extension; class Extension;
...@@ -63,7 +63,7 @@ class ToolbarActionView : public views::MenuButton, ...@@ -63,7 +63,7 @@ class ToolbarActionView : public views::MenuButton,
}; };
ToolbarActionView(ToolbarActionViewController* view_controller, ToolbarActionView(ToolbarActionViewController* view_controller,
Browser* browser, Profile* profile,
Delegate* delegate); Delegate* delegate);
~ToolbarActionView() override; ~ToolbarActionView() override;
...@@ -102,7 +102,6 @@ class ToolbarActionView : public views::MenuButton, ...@@ -102,7 +102,6 @@ class ToolbarActionView : public views::MenuButton,
ToolbarActionViewController* view_controller() { ToolbarActionViewController* view_controller() {
return view_controller_; return view_controller_;
} }
Browser* browser() { return browser_; }
// Returns button icon so it can be accessed during tests. // Returns button icon so it can be accessed during tests.
gfx::ImageSkia GetIconForTest(); gfx::ImageSkia GetIconForTest();
...@@ -137,8 +136,8 @@ class ToolbarActionView : public views::MenuButton, ...@@ -137,8 +136,8 @@ class ToolbarActionView : public views::MenuButton,
// The controller for this toolbar action view. // The controller for this toolbar action view.
ToolbarActionViewController* view_controller_; ToolbarActionViewController* view_controller_;
// The associated browser. // The associated profile.
Browser* browser_; Profile* profile_;
// Delegate that usually represents a container for ToolbarActionView. // Delegate that usually represents a container for ToolbarActionView.
Delegate* delegate_; Delegate* delegate_;
......
...@@ -1337,6 +1337,7 @@ ...@@ -1337,6 +1337,7 @@
'browser/ui/views/tabs/tab_unittest.cc', 'browser/ui/views/tabs/tab_unittest.cc',
'browser/ui/views/toolbar/reload_button_unittest.cc', 'browser/ui/views/toolbar/reload_button_unittest.cc',
'browser/ui/views/toolbar/test_toolbar_actions_bar_helper_views.cc', 'browser/ui/views/toolbar/test_toolbar_actions_bar_helper_views.cc',
'browser/ui/views/toolbar/toolbar_action_view_unittest.cc',
'browser/ui/views/translate/translate_bubble_view_unittest.cc', 'browser/ui/views/translate/translate_bubble_view_unittest.cc',
'browser/ui/views/validation_message_bubble_delegate_unittest.cc', 'browser/ui/views/validation_message_bubble_delegate_unittest.cc',
], ],
...@@ -1677,6 +1678,8 @@ ...@@ -1677,6 +1678,8 @@
'browser/ui/passwords/manage_passwords_ui_controller_mock.h', 'browser/ui/passwords/manage_passwords_ui_controller_mock.h',
'browser/ui/pdf/pdf_browsertest_base.cc', 'browser/ui/pdf/pdf_browsertest_base.cc',
'browser/ui/pdf/pdf_browsertest_base.h', 'browser/ui/pdf/pdf_browsertest_base.h',
'browser/ui/toolbar/test_toolbar_action_view_controller.cc',
'browser/ui/toolbar/test_toolbar_action_view_controller.h',
'browser/ui/test/test_confirm_bubble_model.cc', 'browser/ui/test/test_confirm_bubble_model.cc',
'browser/ui/test/test_confirm_bubble_model.h', 'browser/ui/test/test_confirm_bubble_model.h',
'browser/ui/views/find_bar_host_unittest_util_views.cc', 'browser/ui/views/find_bar_host_unittest_util_views.cc',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment