Commit 794c0a7b authored by Devlin Cronin's avatar Devlin Cronin Committed by Commit Bot

[Extensions Menu] Clean up BrowserActionTestUtilViews

Clean up the BrowserActionTestUtilViews implementation a bit.
- Remove the TestToolbarActionsBarHelper base class from
  BrowserActionTestUtil; there's no need for it to be (conceptually)
  cross-platform.
- Cache the BrowserActionsContainer pointer on the
  BrowserActionTestUtilViews instance to avoid always doing a look-up
  through the GetContainer() method (and remove GetContainer()).
- Avoid caching the Browser on the object; it's only necessary for
  creating the overflow bar, and can be easily passed in to those calls
  (of which there are few).
- Finesse the constructors to make it clear when a
  BrowserActionsContainer is owned (through a manufactured view) vs not
  (by accessing one on the BrowserWindow).

Bug: 984654

Change-Id: Ibab94e0562fd04026f1b2642d6dbc170c049ead4
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1877447
Commit-Queue: Devlin <rdevlin.cronin@chromium.org>
Reviewed-by: default avatarPeter Boström <pbos@chromium.org>
Cr-Commit-Position: refs/heads/master@{#709319}
parent 2be01941
...@@ -21,13 +21,6 @@ class Image; ...@@ -21,13 +21,6 @@ class Image;
class Size; class Size;
} // namespace gfx } // namespace gfx
// A class that creates and owns the platform-specific views for the browser
// actions container. Specific implementations are in the .cc/.mm files.
class TestToolbarActionsBarHelper {
public:
virtual ~TestToolbarActionsBarHelper() {}
};
class BrowserActionTestUtil { class BrowserActionTestUtil {
public: public:
// Constructs a BrowserActionTestUtil which, if |is_real_window| is false, // Constructs a BrowserActionTestUtil which, if |is_real_window| is false,
...@@ -96,7 +89,8 @@ class BrowserActionTestUtil { ...@@ -96,7 +89,8 @@ class BrowserActionTestUtil {
// Creates and returns a BrowserActionTestUtil with an "overflow" container, // Creates and returns a BrowserActionTestUtil with an "overflow" container,
// with this object's container as the main bar. // with this object's container as the main bar.
virtual std::unique_ptr<BrowserActionTestUtil> CreateOverflowBar() = 0; virtual std::unique_ptr<BrowserActionTestUtil> CreateOverflowBar(
Browser* browser) = 0;
// Returns the minimum allowed size of an extension popup. // Returns the minimum allowed size of an extension popup.
virtual gfx::Size GetMinPopupSize() = 0; virtual gfx::Size GetMinPopupSize() = 0;
......
...@@ -401,7 +401,7 @@ IN_PROC_BROWSER_TEST_F(BrowserActionsBarBrowserTest, ...@@ -401,7 +401,7 @@ IN_PROC_BROWSER_TEST_F(BrowserActionsBarBrowserTest,
IN_PROC_BROWSER_TEST_F(BrowserActionsBarBrowserTest, IN_PROC_BROWSER_TEST_F(BrowserActionsBarBrowserTest,
OverflowedBrowserActionPopupTest) { OverflowedBrowserActionPopupTest) {
std::unique_ptr<BrowserActionTestUtil> overflow_bar = std::unique_ptr<BrowserActionTestUtil> overflow_bar =
browser_actions_bar()->CreateOverflowBar(); browser_actions_bar()->CreateOverflowBar(browser());
// Load up two extensions that have browser action popups. // Load up two extensions that have browser action popups.
base::FilePath data_dir = base::FilePath data_dir =
...@@ -488,7 +488,7 @@ IN_PROC_BROWSER_TEST_F(BrowserActionsBarBrowserTest, ...@@ -488,7 +488,7 @@ IN_PROC_BROWSER_TEST_F(BrowserActionsBarBrowserTest,
IN_PROC_BROWSER_TEST_F(BrowserActionsBarBrowserTest, IN_PROC_BROWSER_TEST_F(BrowserActionsBarBrowserTest,
OverflowedBrowserActionPopupTestRemoval) { OverflowedBrowserActionPopupTestRemoval) {
std::unique_ptr<BrowserActionTestUtil> overflow_bar = std::unique_ptr<BrowserActionTestUtil> overflow_bar =
browser_actions_bar()->CreateOverflowBar(); browser_actions_bar()->CreateOverflowBar(browser());
// Install an extension and shrink the visible count to zero so the extension // Install an extension and shrink the visible count to zero so the extension
// is overflowed. // is overflowed.
......
...@@ -172,7 +172,7 @@ void ToolbarActionsBarUnitTest::SetUp() { ...@@ -172,7 +172,7 @@ void ToolbarActionsBarUnitTest::SetUp() {
browser_action_test_util_ = BrowserActionTestUtil::Create(browser(), false); browser_action_test_util_ = BrowserActionTestUtil::Create(browser(), false);
overflow_browser_action_test_util_ = overflow_browser_action_test_util_ =
browser_action_test_util_->CreateOverflowBar(); browser_action_test_util_->CreateOverflowBar(browser());
} }
void ToolbarActionsBarUnitTest::TearDown() { void ToolbarActionsBarUnitTest::TearDown() {
......
...@@ -24,16 +24,14 @@ ...@@ -24,16 +24,14 @@
#include "ui/views/test/test_views.h" #include "ui/views/test/test_views.h"
#include "ui/views/widget/widget.h" #include "ui/views/widget/widget.h"
namespace { // A helper class that owns an instance of a BrowserActionsContainer; this is
// used when testing without an associated browser window, or if this is for
// The views-specific implementation of the TestToolbarActionsBarHelper, which // the overflow version of the bar.
// creates and owns a BrowserActionsContainer. class BrowserActionTestUtilViews::TestToolbarActionsBarHelper
class TestToolbarActionsBarHelperViews : public BrowserActionsContainer::Delegate {
: public TestToolbarActionsBarHelper,
public BrowserActionsContainer::Delegate {
public: public:
TestToolbarActionsBarHelperViews(Browser* browser, TestToolbarActionsBarHelper(Browser* browser,
BrowserActionsContainer* main_bar) BrowserActionsContainer* main_bar)
: browser_actions_container_( : browser_actions_container_(
new BrowserActionsContainer(browser, main_bar, this, true)) { new BrowserActionsContainer(browser, main_bar, this, true)) {
container_parent_.set_owned_by_client(); container_parent_.set_owned_by_client();
...@@ -41,6 +39,7 @@ class TestToolbarActionsBarHelperViews ...@@ -41,6 +39,7 @@ class TestToolbarActionsBarHelperViews
container_parent_.Layout(); container_parent_.Layout();
container_parent_.AddChildView(browser_actions_container_); container_parent_.AddChildView(browser_actions_container_);
} }
~TestToolbarActionsBarHelper() override = default;
BrowserActionsContainer* browser_actions_container() { BrowserActionsContainer* browser_actions_container() {
return browser_actions_container_; return browser_actions_container_;
...@@ -60,87 +59,61 @@ class TestToolbarActionsBarHelperViews ...@@ -60,87 +59,61 @@ class TestToolbarActionsBarHelperViews
private: private:
// The created BrowserActionsContainer. Owned by |container_parent_|. // The created BrowserActionsContainer. Owned by |container_parent_|.
BrowserActionsContainer* browser_actions_container_; BrowserActionsContainer* const browser_actions_container_;
// The parent of the BrowserActionsContainer, which directly owns the // The parent of the BrowserActionsContainer, which directly owns the
// container as part of the views hierarchy. // container as part of the views hierarchy.
views::ResizeAwareParentView container_parent_; views::ResizeAwareParentView container_parent_;
DISALLOW_COPY_AND_ASSIGN(TestToolbarActionsBarHelperViews); DISALLOW_COPY_AND_ASSIGN(TestToolbarActionsBarHelper);
}; };
BrowserActionsContainer* GetContainer(Browser* browser, BrowserActionTestUtilViews::~BrowserActionTestUtilViews() = default;
TestToolbarActionsBarHelper* helper) {
if (helper) {
return static_cast<TestToolbarActionsBarHelperViews*>(helper)
->browser_actions_container();
}
return BrowserView::GetBrowserViewForBrowser(browser)->toolbar()->
browser_actions();
}
} // namespace
BrowserActionTestUtilViews::BrowserActionTestUtilViews(Browser* browser)
: BrowserActionTestUtilViews(browser, true) {}
BrowserActionTestUtilViews::BrowserActionTestUtilViews(Browser* browser,
bool is_real_window)
: browser_(browser) {
if (!is_real_window)
test_helper_ =
std::make_unique<TestToolbarActionsBarHelperViews>(browser, nullptr);
}
BrowserActionTestUtilViews::~BrowserActionTestUtilViews() {}
int BrowserActionTestUtilViews::NumberOfBrowserActions() { int BrowserActionTestUtilViews::NumberOfBrowserActions() {
return GetContainer(browser_, test_helper_.get())->num_toolbar_actions(); return browser_actions_container_->num_toolbar_actions();
} }
int BrowserActionTestUtilViews::VisibleBrowserActions() { int BrowserActionTestUtilViews::VisibleBrowserActions() {
return GetContainer(browser_, test_helper_.get())->VisibleBrowserActions(); return browser_actions_container_->VisibleBrowserActions();
} }
void BrowserActionTestUtilViews::InspectPopup(int index) { void BrowserActionTestUtilViews::InspectPopup(int index) {
ToolbarActionView* view = ToolbarActionView* view =
GetContainer(browser_, test_helper_.get())->GetToolbarActionViewAt(index); browser_actions_container_->GetToolbarActionViewAt(index);
static_cast<ExtensionActionViewController*>(view->view_controller())-> static_cast<ExtensionActionViewController*>(view->view_controller())->
InspectPopup(); InspectPopup();
} }
bool BrowserActionTestUtilViews::HasIcon(int index) { bool BrowserActionTestUtilViews::HasIcon(int index) {
return !GetContainer(browser_, test_helper_.get()) return !browser_actions_container_->GetToolbarActionViewAt(index)
->GetToolbarActionViewAt(index)
->GetImage(views::Button::STATE_NORMAL) ->GetImage(views::Button::STATE_NORMAL)
.isNull(); .isNull();
} }
gfx::Image BrowserActionTestUtilViews::GetIcon(int index) { gfx::Image BrowserActionTestUtilViews::GetIcon(int index) {
gfx::ImageSkia icon = GetContainer(browser_, test_helper_.get()) gfx::ImageSkia icon =
->GetToolbarActionViewAt(index) browser_actions_container_->GetToolbarActionViewAt(index)
->GetIconForTest(); ->GetIconForTest();
return gfx::Image(icon); return gfx::Image(icon);
} }
void BrowserActionTestUtilViews::Press(int index) { void BrowserActionTestUtilViews::Press(int index) {
GetContainer(browser_, test_helper_.get()) browser_actions_container_->GetToolbarActionViewAt(index)
->GetToolbarActionViewAt(index)
->view_controller() ->view_controller()
->ExecuteAction(true); ->ExecuteAction(true);
} }
std::string BrowserActionTestUtilViews::GetExtensionId(int index) { std::string BrowserActionTestUtilViews::GetExtensionId(int index) {
return GetContainer(browser_, test_helper_.get()) return browser_actions_container_->GetToolbarActionViewAt(index)
->GetToolbarActionViewAt(index)
->view_controller() ->view_controller()
->GetId(); ->GetId();
} }
std::string BrowserActionTestUtilViews::GetTooltip(int index) { std::string BrowserActionTestUtilViews::GetTooltip(int index) {
base::string16 tooltip = GetContainer(browser_, test_helper_.get()) base::string16 tooltip =
->GetToolbarActionViewAt(index) browser_actions_container_->GetToolbarActionViewAt(index)->GetTooltipText(
->GetTooltipText(gfx::Point()); gfx::Point());
return base::UTF16ToUTF8(tooltip); return base::UTF16ToUTF8(tooltip);
} }
...@@ -166,26 +139,27 @@ bool BrowserActionTestUtilViews::HidePopup() { ...@@ -166,26 +139,27 @@ bool BrowserActionTestUtilViews::HidePopup() {
} }
bool BrowserActionTestUtilViews::ActionButtonWantsToRun(size_t index) { bool BrowserActionTestUtilViews::ActionButtonWantsToRun(size_t index) {
return GetContainer(browser_, test_helper_.get()) return browser_actions_container_->GetToolbarActionViewAt(index)
->GetToolbarActionViewAt(index)
->wants_to_run_for_testing(); ->wants_to_run_for_testing();
} }
void BrowserActionTestUtilViews::SetWidth(int width) { void BrowserActionTestUtilViews::SetWidth(int width) {
BrowserActionsContainer* container = browser_actions_container_->SetSize(
GetContainer(browser_, test_helper_.get()); gfx::Size(width, browser_actions_container_->height()));
container->SetSize(gfx::Size(width, container->height()));
} }
ToolbarActionsBar* BrowserActionTestUtilViews::GetToolbarActionsBar() { ToolbarActionsBar* BrowserActionTestUtilViews::GetToolbarActionsBar() {
return GetContainer(browser_, test_helper_.get())->toolbar_actions_bar(); return browser_actions_container_->toolbar_actions_bar();
} }
std::unique_ptr<BrowserActionTestUtil> std::unique_ptr<BrowserActionTestUtil>
BrowserActionTestUtilViews::CreateOverflowBar() { BrowserActionTestUtilViews::CreateOverflowBar(Browser* browser) {
CHECK(!GetToolbarActionsBar()->in_overflow_mode()) CHECK(!GetToolbarActionsBar()->in_overflow_mode())
<< "Only a main bar can create an overflow bar!"; << "Only a main bar can create an overflow bar!";
return base::WrapUnique(new BrowserActionTestUtilViews(browser_, this));
return base::WrapUnique(new BrowserActionTestUtilViews(
std::make_unique<TestToolbarActionsBarHelper>(
browser, browser_actions_container_)));
} }
gfx::Size BrowserActionTestUtilViews::GetMinPopupSize() { gfx::Size BrowserActionTestUtilViews::GetMinPopupSize() {
...@@ -197,29 +171,43 @@ gfx::Size BrowserActionTestUtilViews::GetMaxPopupSize() { ...@@ -197,29 +171,43 @@ gfx::Size BrowserActionTestUtilViews::GetMaxPopupSize() {
} }
bool BrowserActionTestUtilViews::CanBeResized() { bool BrowserActionTestUtilViews::CanBeResized() {
BrowserActionsContainer* container =
BrowserView::GetBrowserViewForBrowser(browser_)
->toolbar()
->browser_actions();
// The container can only be resized if we can start a drag for the view. // The container can only be resized if we can start a drag for the view.
DCHECK_LE(1u, container->num_toolbar_actions()); DCHECK_LE(1u, browser_actions_container_->num_toolbar_actions());
ToolbarActionView* action_view = container->GetToolbarActionViewAt(0); ToolbarActionView* action_view =
browser_actions_container_->GetToolbarActionViewAt(0);
gfx::Point point(action_view->x(), action_view->y()); gfx::Point point(action_view->x(), action_view->y());
return container->CanStartDragForView(action_view, point, point); return browser_actions_container_->CanStartDragForView(action_view, point,
point);
} }
BrowserActionTestUtilViews::BrowserActionTestUtilViews( BrowserActionTestUtilViews::BrowserActionTestUtilViews(
Browser* browser, BrowserActionsContainer* browser_actions_container)
BrowserActionTestUtilViews* main_bar) : browser_actions_container_(browser_actions_container) {}
: browser_(browser),
test_helper_(new TestToolbarActionsBarHelperViews( BrowserActionTestUtilViews::BrowserActionTestUtilViews(
browser_, std::unique_ptr<TestToolbarActionsBarHelper> test_helper)
GetContainer(browser_, main_bar->test_helper_.get()))) {} : test_helper_(std::move(test_helper)),
browser_actions_container_(test_helper_->browser_actions_container()) {}
// static // static
std::unique_ptr<BrowserActionTestUtil> BrowserActionTestUtil::Create( std::unique_ptr<BrowserActionTestUtil> BrowserActionTestUtil::Create(
Browser* browser, Browser* browser,
bool is_real_window) { bool is_real_window) {
return std::make_unique<BrowserActionTestUtilViews>(browser, is_real_window); std::unique_ptr<BrowserActionTestUtil> browser_action_test_util;
if (is_real_window) {
browser_action_test_util = base::WrapUnique(new BrowserActionTestUtilViews(
BrowserView::GetBrowserViewForBrowser(browser)
->toolbar()
->browser_actions()));
} else {
// This is the main bar.
BrowserActionsContainer* main_bar = nullptr;
browser_action_test_util = base::WrapUnique(new BrowserActionTestUtilViews(
std::make_unique<
BrowserActionTestUtilViews::TestToolbarActionsBarHelper>(
browser, main_bar)));
}
return browser_action_test_util;
} }
...@@ -5,12 +5,14 @@ ...@@ -5,12 +5,14 @@
#ifndef CHROME_BROWSER_UI_VIEWS_TOOLBAR_BROWSER_ACTION_TEST_UTIL_VIEWS_H_ #ifndef CHROME_BROWSER_UI_VIEWS_TOOLBAR_BROWSER_ACTION_TEST_UTIL_VIEWS_H_
#define CHROME_BROWSER_UI_VIEWS_TOOLBAR_BROWSER_ACTION_TEST_UTIL_VIEWS_H_ #define CHROME_BROWSER_UI_VIEWS_TOOLBAR_BROWSER_ACTION_TEST_UTIL_VIEWS_H_
#include <memory>
#include "chrome/browser/ui/extensions/browser_action_test_util.h" #include "chrome/browser/ui/extensions/browser_action_test_util.h"
class BrowserActionsContainer;
class BrowserActionTestUtilViews : public BrowserActionTestUtil { class BrowserActionTestUtilViews : public BrowserActionTestUtil {
public: public:
explicit BrowserActionTestUtilViews(Browser* browser);
BrowserActionTestUtilViews(Browser* browser, bool is_real_window);
~BrowserActionTestUtilViews() override; ~BrowserActionTestUtilViews() override;
// BrowserActionTestUtil: // BrowserActionTestUtil:
...@@ -29,7 +31,8 @@ class BrowserActionTestUtilViews : public BrowserActionTestUtil { ...@@ -29,7 +31,8 @@ class BrowserActionTestUtilViews : public BrowserActionTestUtil {
bool ActionButtonWantsToRun(size_t index) override; bool ActionButtonWantsToRun(size_t index) override;
void SetWidth(int width) override; void SetWidth(int width) override;
ToolbarActionsBar* GetToolbarActionsBar() override; ToolbarActionsBar* GetToolbarActionsBar() override;
std::unique_ptr<BrowserActionTestUtil> CreateOverflowBar() override; std::unique_ptr<BrowserActionTestUtil> CreateOverflowBar(
Browser* browser) override;
gfx::Size GetMinPopupSize() override; gfx::Size GetMinPopupSize() override;
gfx::Size GetMaxPopupSize() override; gfx::Size GetMaxPopupSize() override;
bool CanBeResized() override; bool CanBeResized() override;
...@@ -37,14 +40,23 @@ class BrowserActionTestUtilViews : public BrowserActionTestUtil { ...@@ -37,14 +40,23 @@ class BrowserActionTestUtilViews : public BrowserActionTestUtil {
private: private:
friend class BrowserActionTestUtil; friend class BrowserActionTestUtil;
Browser* const browser_; // weak class TestToolbarActionsBarHelper;
// Constructs a version of BrowserActionTestUtilViews that does not own the
// BrowserActionsContainer it tests.
explicit BrowserActionTestUtilViews(
BrowserActionsContainer* browser_actions_container);
// Constructs a version of BrowserActionTestUtilViews given a |test_helper|
// responsible for owning the BrowserActionsContainer.
explicit BrowserActionTestUtilViews(
std::unique_ptr<TestToolbarActionsBarHelper> test_helper);
// Our test helper, which constructs and owns the views if we don't have a
// real browser window, or if this is an overflow version.
std::unique_ptr<TestToolbarActionsBarHelper> test_helper_; std::unique_ptr<TestToolbarActionsBarHelper> test_helper_;
BrowserActionTestUtilViews(Browser* browser, // The associated BrowserActionsContainer. Not owned.
BrowserActionTestUtilViews* main_bar); BrowserActionsContainer* const browser_actions_container_;
DISALLOW_COPY_AND_ASSIGN(BrowserActionTestUtilViews);
}; };
#endif // CHROME_BROWSER_UI_VIEWS_TOOLBAR_BROWSER_ACTION_TEST_UTIL_VIEWS_H_ #endif // CHROME_BROWSER_UI_VIEWS_TOOLBAR_BROWSER_ACTION_TEST_UTIL_VIEWS_H_
...@@ -116,6 +116,8 @@ class BrowserActionsContainer : public views::View, ...@@ -116,6 +116,8 @@ class BrowserActionsContainer : public views::View,
public: public:
class Delegate { class Delegate {
public: public:
virtual ~Delegate() {}
// Returns the view of the toolbar actions overflow menu to use as a // Returns the view of the toolbar actions overflow menu to use as a
// reference point for a popup when this view isn't visible. // reference point for a popup when this view isn't visible.
virtual views::LabelButton* GetOverflowReferenceView() = 0; virtual views::LabelButton* GetOverflowReferenceView() = 0;
......
...@@ -238,7 +238,7 @@ namespace { ...@@ -238,7 +238,7 @@ namespace {
class ForwardingDelegate : public BrowserActionsContainer::Delegate { class ForwardingDelegate : public BrowserActionsContainer::Delegate {
public: public:
explicit ForwardingDelegate(BrowserActionsContainer::Delegate* forward_to); explicit ForwardingDelegate(BrowserActionsContainer::Delegate* forward_to);
virtual ~ForwardingDelegate() = default; ~ForwardingDelegate() override = default;
BrowserActionsContainer::Delegate* forward_to() { return forward_to_; } BrowserActionsContainer::Delegate* forward_to() { return forward_to_; }
void set_max_browser_actions_width( void set_max_browser_actions_width(
......
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