Skip to content

Commit

Permalink
Cleanup for ViewEventTestBase.
Browse files Browse the repository at this point in the history
* Replace WidgetDelegate inheritance with composition.
* Make window_ private.
* Null |window_| automatically (via the WidgetDelegate) when it closes.
* Replace manual Layout() override with FillLayout.
* Slightly less random declaration order.

Bug: none
Change-Id: I9179f6d581d6b90d50c324aa81fccdbf200570fc
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2135533
Reviewed-by: Thomas Lukaszewicz <tluk@chromium.org>
Commit-Queue: Peter Kasting <pkasting@chromium.org>
Auto-Submit: Peter Kasting <pkasting@chromium.org>
Cr-Commit-Position: refs/heads/master@{#756311}
  • Loading branch information
pkasting authored and Commit Bot committed Apr 3, 2020
1 parent 3254af5 commit edb168a
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 119 deletions.
65 changes: 29 additions & 36 deletions chrome/browser/ui/views/bookmarks/bookmark_bar_view_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ class BookmarkBarViewEventTestBase : public ViewEventTestBase {
// clipboard selection from the X server (for the 'paste' item), so mock it
// out.
ui::TestClipboard::CreateForCurrentThread();
GetWidget()->Activate();
window()->Activate();
#endif
}

Expand Down Expand Up @@ -1056,7 +1056,7 @@ class BookmarkBarViewTest10 : public BookmarkBarViewEventTestBase {

// Send a down event, which should select the first item.
ASSERT_TRUE(ui_controls::SendKeyPressNotifyWhenDone(
window_->GetNativeWindow(), ui::VKEY_DOWN, false, false, false, false,
window()->GetNativeWindow(), ui::VKEY_DOWN, false, false, false, false,
CreateEventTask(this, &BookmarkBarViewTest10::Step3)));
}

Expand All @@ -1069,7 +1069,7 @@ class BookmarkBarViewTest10 : public BookmarkBarViewEventTestBase {

// Send a key down event, which should select the next item.
ASSERT_TRUE(ui_controls::SendKeyPressNotifyWhenDone(
window_->GetNativeWindow(), ui::VKEY_DOWN, false, false, false, false,
window()->GetNativeWindow(), ui::VKEY_DOWN, false, false, false, false,
CreateEventTask(this, &BookmarkBarViewTest10::Step4)));
}

Expand All @@ -1082,7 +1082,7 @@ class BookmarkBarViewTest10 : public BookmarkBarViewEventTestBase {

// Send a right arrow to force the menu to open.
ASSERT_TRUE(ui_controls::SendKeyPressNotifyWhenDone(
window_->GetNativeWindow(), ui::VKEY_RIGHT, false, false, false, false,
window()->GetNativeWindow(), ui::VKEY_RIGHT, false, false, false, false,
CreateEventTask(this, &BookmarkBarViewTest10::Step5)));
}

Expand All @@ -1098,7 +1098,7 @@ class BookmarkBarViewTest10 : public BookmarkBarViewEventTestBase {

// Send a left arrow to close the submenu.
ASSERT_TRUE(ui_controls::SendKeyPressNotifyWhenDone(
window_->GetNativeWindow(), ui::VKEY_LEFT, false, false, false, false,
window()->GetNativeWindow(), ui::VKEY_LEFT, false, false, false, false,
CreateEventTask(this, &BookmarkBarViewTest10::Step6)));
}

Expand All @@ -1113,7 +1113,7 @@ class BookmarkBarViewTest10 : public BookmarkBarViewEventTestBase {

// Send a down arrow to go down to f1b.
ASSERT_TRUE(ui_controls::SendKeyPressNotifyWhenDone(
window_->GetNativeWindow(), ui::VKEY_DOWN, false, false, false, false,
window()->GetNativeWindow(), ui::VKEY_DOWN, false, false, false, false,
CreateEventTask(this, &BookmarkBarViewTest10::Step7)));
}

Expand All @@ -1126,7 +1126,7 @@ class BookmarkBarViewTest10 : public BookmarkBarViewEventTestBase {

// Send a down arrow to wrap back to f1a.
ASSERT_TRUE(ui_controls::SendKeyPressNotifyWhenDone(
window_->GetNativeWindow(), ui::VKEY_DOWN, false, false, false, false,
window()->GetNativeWindow(), ui::VKEY_DOWN, false, false, false, false,
CreateEventTask(this, &BookmarkBarViewTest10::Step8)));
}

Expand All @@ -1139,8 +1139,8 @@ class BookmarkBarViewTest10 : public BookmarkBarViewEventTestBase {

// Send enter, which should select the item.
ASSERT_TRUE(ui_controls::SendKeyPressNotifyWhenDone(
window_->GetNativeWindow(), ui::VKEY_RETURN, false, false, false, false,
CreateEventTask(this, &BookmarkBarViewTest10::Step9)));
window()->GetNativeWindow(), ui::VKEY_RETURN, false, false, false,
false, CreateEventTask(this, &BookmarkBarViewTest10::Step9)));
}

void Step9() {
Expand Down Expand Up @@ -1194,8 +1194,8 @@ class BookmarkBarViewTest11 : public BookmarkBarViewEventTestBase {
void Step3() {
// Send escape so that the context menu hides.
ASSERT_TRUE(ui_controls::SendKeyPressNotifyWhenDone(
window_->GetNativeWindow(), ui::VKEY_ESCAPE, false, false, false, false,
CreateEventTask(this, &BookmarkBarViewTest11::Step4)));
window()->GetNativeWindow(), ui::VKEY_ESCAPE, false, false, false,
false, CreateEventTask(this, &BookmarkBarViewTest11::Step4)));
}

void Step4() {
Expand Down Expand Up @@ -1426,8 +1426,8 @@ class BookmarkBarViewTest14 : public BookmarkBarViewEventTestBase {

// Send escape so that the context menu hides.
ASSERT_TRUE(ui_controls::SendKeyPressNotifyWhenDone(
window_->GetNativeWindow(), ui::VKEY_ESCAPE, false, false, false, false,
CreateEventTask(this, &BookmarkBarViewTest14::Step3)));
window()->GetNativeWindow(), ui::VKEY_ESCAPE, false, false, false,
false, CreateEventTask(this, &BookmarkBarViewTest14::Step3)));
}

void Step3() {
Expand Down Expand Up @@ -1542,8 +1542,7 @@ class BookmarkBarViewTest16 : public BookmarkBarViewEventTestBase {
ASSERT_TRUE(button->state() == views::Button::STATE_PRESSED);

// Close the window.
window_->Close();
window_ = NULL;
window()->Close();

base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, CreateEventTask(this, &BookmarkBarViewTest16::Done));
Expand Down Expand Up @@ -1962,7 +1961,7 @@ class BookmarkBarViewTest22 : public BookmarkBarViewDragTestBase {
void OnWidgetDragWillStart(views::Widget* widget) override {
// Watch for main window destruction instead of menu dragging.
widget_observer()->RemoveAll();
widget_observer()->Add(window_);
widget_observer()->Add(window());

BookmarkBarViewDragTestBase::OnWidgetDragWillStart(widget);
}
Expand All @@ -1988,8 +1987,7 @@ class BookmarkBarViewTest22 : public BookmarkBarViewDragTestBase {
// window alone may not exit this message loop.
BookmarkBarViewDragTestBase::OnDragEntered();

window_->Close();
window_ = nullptr;
window()->Close();
}

const BookmarkNode* GetDroppedNode() const override {
Expand Down Expand Up @@ -2031,8 +2029,7 @@ class BookmarkBarViewTest23 : public BookmarkBarViewEventTestBase {

// Navigate down to highlight the first menu item.
ASSERT_TRUE(ui_controls::SendKeyPressNotifyWhenDone(
GetWidget()->GetNativeWindow(), ui::VKEY_DOWN, false, false, false,
false, // No modifer keys
window()->GetNativeWindow(), ui::VKEY_DOWN, false, false, false, false,
CreateEventTask(this, &BookmarkBarViewTest23::Step3)));
}

Expand All @@ -2043,10 +2040,9 @@ class BookmarkBarViewTest23 : public BookmarkBarViewEventTestBase {
ASSERT_TRUE(menu->GetSubmenu()->IsShowing());

// Open the context menu via the keyboard.
ASSERT_TRUE(ui_controls::SendKeyPress(GetWidget()->GetNativeWindow(),
ASSERT_TRUE(ui_controls::SendKeyPress(window()->GetNativeWindow(),
ui::VKEY_APPS, false, false, false,
false // No modifer keys
));
false));
// The BookmarkContextMenuNotificationObserver triggers Step4.
}

Expand Down Expand Up @@ -2102,8 +2098,7 @@ class BookmarkBarViewTest24 : public BookmarkBarViewEventTestBase {

// Navigate down to highlight the first menu item.
ASSERT_TRUE(ui_controls::SendKeyPressNotifyWhenDone(
GetWidget()->GetNativeWindow(), ui::VKEY_DOWN, false, false, false,
false, // No modifer keys
window()->GetNativeWindow(), ui::VKEY_DOWN, false, false, false, false,
CreateEventTask(this, &BookmarkBarViewTest24::Step3)));
}

Expand All @@ -2114,10 +2109,9 @@ class BookmarkBarViewTest24 : public BookmarkBarViewEventTestBase {
ASSERT_TRUE(menu->GetSubmenu()->IsShowing());

// Open the context menu via the keyboard.
ASSERT_TRUE(ui_controls::SendKeyPress(GetWidget()->GetNativeWindow(),
ASSERT_TRUE(ui_controls::SendKeyPress(window()->GetNativeWindow(),
ui::VKEY_APPS, false, false, false,
false // No modifer keys
));
false));
// The BookmarkContextMenuNotificationObserver triggers Step4.
}

Expand All @@ -2130,8 +2124,8 @@ class BookmarkBarViewTest24 : public BookmarkBarViewEventTestBase {

// Send escape to close the context menu.
ASSERT_TRUE(ui_controls::SendKeyPressNotifyWhenDone(
window_->GetNativeWindow(), ui::VKEY_ESCAPE, false, false, false, false,
CreateEventTask(this, &BookmarkBarViewTest24::Step5)));
window()->GetNativeWindow(), ui::VKEY_ESCAPE, false, false, false,
false, CreateEventTask(this, &BookmarkBarViewTest24::Step5)));
}

void Step5() {
Expand All @@ -2145,8 +2139,8 @@ class BookmarkBarViewTest24 : public BookmarkBarViewEventTestBase {

// Send escape to close the main menu.
ASSERT_TRUE(ui_controls::SendKeyPressNotifyWhenDone(
window_->GetNativeWindow(), ui::VKEY_ESCAPE, false, false, false, false,
CreateEventTask(this, &BookmarkBarViewTest24::Done)));
window()->GetNativeWindow(), ui::VKEY_ESCAPE, false, false, false,
false, CreateEventTask(this, &BookmarkBarViewTest24::Done)));
}

BookmarkContextMenuNotificationObserver observer_;
Expand Down Expand Up @@ -2177,7 +2171,7 @@ class BookmarkBarViewTest25 : public BookmarkBarViewEventTestBase {

// Send KEYCODE key event, which should close the menu.
ASSERT_TRUE(ui_controls::SendKeyPressNotifyWhenDone(
window_->GetNativeWindow(), KEYCODE, false, false, false, false,
window()->GetNativeWindow(), KEYCODE, false, false, false, false,
CreateEventTask(this, &BookmarkBarViewTest25::Step3)));
}

Expand Down Expand Up @@ -2220,9 +2214,8 @@ class BookmarkBarViewTest26 : public BookmarkBarViewEventTestBase {
// Send WM_CANCELMODE, which should close the menu. The message is sent
// synchronously, however, we post a task to make sure that the message is
// processed completely before finishing the test.
::SendMessage(
GetWidget()->GetNativeView()->GetHost()->GetAcceleratedWidget(),
WM_CANCELMODE, 0, 0);
::SendMessage(window()->GetNativeView()->GetHost()->GetAcceleratedWidget(),
WM_CANCELMODE, 0, 0);

base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE,
Expand Down
4 changes: 2 additions & 2 deletions chrome/browser/ui/views/menu_test_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ void MenuTestBase::Click(views::View* view, base::OnceClosure next) {
}

void MenuTestBase::KeyPress(ui::KeyboardCode keycode, base::OnceClosure next) {
ui_controls::SendKeyPressNotifyWhenDone(GetWidget()->GetNativeWindow(),
keycode, false, false, false, false,
ui_controls::SendKeyPressNotifyWhenDone(window()->GetNativeWindow(), keycode,
false, false, false, false,
std::move(next));
}

Expand Down
93 changes: 48 additions & 45 deletions chrome/browser/ui/views/test/view_event_test_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,63 @@
#include "ui/base/clipboard/clipboard.h"
#include "ui/base/ime/init/input_method_initializer.h"
#include "ui/compositor/test/test_context_factories.h"
#include "ui/views/layout/fill_layout.h"
#include "ui/views/view.h"
#include "ui/views/widget/widget.h"
#include "ui/views/widget/widget_delegate.h"

namespace {

// View subclass that allows you to specify the preferred size.
// View that keeps its preferred size in sync with what |harness| requests.
class TestView : public views::View {
public:
explicit TestView(ViewEventTestBase* harness) : harness_(harness) {}
explicit TestView(ViewEventTestBase* harness) : harness_(harness) {
SetLayoutManager(std::make_unique<views::FillLayout>());
AddChildView(harness_->CreateContentsView());
}
TestView(const TestView&) = delete;
TestView& operator=(const TestView&) = delete;
~TestView() override = default;

gfx::Size CalculatePreferredSize() const override {
return harness_->GetPreferredSizeForContents();
}

void Layout() override {
// Permit a test to remove the view being tested from the hierarchy, then
// still handle a _NET_WM_STATE event on Linux during teardown that triggers
// layout.
if (!children().empty())
children().front()->SetBoundsRect(GetLocalBounds());
}

private:
ViewEventTestBase* harness_;

DISALLOW_COPY_AND_ASSIGN(TestView);
};

} // namespace

class TestBaseWidgetDelegate : public views::WidgetDelegate {
public:
explicit TestBaseWidgetDelegate(ViewEventTestBase* harness)
: harness_(harness) {}
TestBaseWidgetDelegate(const TestBaseWidgetDelegate&) = delete;
TestBaseWidgetDelegate& operator=(const TestBaseWidgetDelegate&) = delete;
~TestBaseWidgetDelegate() override = default;

// views::WidgetDelegate:
bool CanResize() const override { return true; }
void WindowClosing() override { harness_->window_ = nullptr; }
void DeleteDelegate() override { delete this; }
views::Widget* GetWidget() override { return contents_->GetWidget(); }
const views::Widget* GetWidget() const override {
return contents_->GetWidget();
}
views::View* GetContentsView() override {
// This will first be called by Widget::Init(), which passes the returned
// View* to SetContentsView(), which takes ownership.
if (!contents_)
contents_ = new TestView(harness_);
return contents_;
}

private:
ViewEventTestBase* harness_;
views::View* contents_ = nullptr;
};

ViewEventTestBase::ViewEventTestBase() {
// The TestingBrowserProcess must be created in the constructor because there
// are tests that require it before SetUp() is called.
Expand All @@ -58,9 +85,8 @@ ViewEventTestBase::ViewEventTestBase() {
mojo::core::Init();
}

void ViewEventTestBase::Done() {
drag_event_thread_.reset();
run_loop_.Quit();
ViewEventTestBase::~ViewEventTestBase() {
TestingBrowserProcess::DeleteInstance();
}

void ViewEventTestBase::SetUpTestCase() {
Expand All @@ -72,9 +98,7 @@ void ViewEventTestBase::SetUp() {
ui::InitializeInputMethodForTesting();

// The ContextFactory must exist before any Compositors are created.
const bool enable_pixel_output = false;
context_factories_ =
std::make_unique<ui::TestContextFactories>(enable_pixel_output);
context_factories_ = std::make_unique<ui::TestContextFactories>(false);

#if defined(OS_MACOSX)
views_delegate_.set_context_factory(context_factories_->GetContextFactory());
Expand All @@ -83,16 +107,17 @@ void ViewEventTestBase::SetUp() {

platform_part_.reset(ViewEventTestPlatformPart::Create(
context_factories_->GetContextFactory()));

window_ = views::Widget::CreateWindowWithContext(
this, platform_part_->GetContext());
new TestBaseWidgetDelegate(this), // Owns itself.
platform_part_->GetContext());
window_->Show();
}

void ViewEventTestBase::TearDown() {
if (window_) {
window_->Close();
base::RunLoop().RunUntilIdle();
window_ = NULL;
}

ui::Clipboard::DestroyClipboardForCurrentThread();
Expand All @@ -107,31 +132,9 @@ gfx::Size ViewEventTestBase::GetPreferredSizeForContents() const {
return gfx::Size();
}

bool ViewEventTestBase::CanResize() const {
return true;
}

views::View* ViewEventTestBase::GetContentsView() {
if (!content_view_) {
// Wrap the real view (as returned by CreateContentsView) in a View so
// that we can customize the preferred size.
TestView* test_view = new TestView(this);
test_view->AddChildView(CreateContentsView());
content_view_ = test_view;
}
return content_view_;
}

const views::Widget* ViewEventTestBase::GetWidget() const {
return content_view_->GetWidget();
}

views::Widget* ViewEventTestBase::GetWidget() {
return content_view_->GetWidget();
}

ViewEventTestBase::~ViewEventTestBase() {
TestingBrowserProcess::DeleteInstance();
void ViewEventTestBase::Done() {
drag_event_thread_.reset();
run_loop_.Quit();
}

void ViewEventTestBase::StartMessageLoopAndRunTest() {
Expand Down
Loading

0 comments on commit edb168a

Please sign in to comment.