Skip to content

Commit

Permalink
Use ScopedTaskEnvironment instead of MessageLoopForUI in ui/aura/test/.
Browse files Browse the repository at this point in the history
ScopedTaskEnvironment allows usage of ThreadTaskRunnerHandle and
base/task_scheduler/post_task.h within its scope. It should be
instantiated in everytest that uses either of these APIs
(i.e. no test should instantiate a MessageLoop directly).

Motivation for ScopedTaskEnvironment can be found in:
https://docs.google.com/document/d/1QabRo8c7D9LsYY3cEcaPQbOCLo8Tu-6VLykYXyl3Pkk/edit

TBR=sky@chromium.org
BUG=708584

Review-Url: https://codereview.chromium.org/2849423002
Cr-Commit-Position: refs/heads/master@{#468756}
  • Loading branch information
fdoray authored and Commit bot committed May 2, 2017
1 parent f2bcb74 commit 1c906dc
Show file tree
Hide file tree
Showing 15 changed files with 29 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,7 @@ class DesktopCaptureDeviceAuraTest : public testing::Test {
ui::ContextFactoryPrivate* context_factory_private = nullptr;
ui::InitializeContextFactoryForTests(enable_pixel_output, &context_factory,
&context_factory_private);
helper_.reset(
new aura::test::AuraTestHelper(base::MessageLoopForUI::current()));
helper_.reset(new aura::test::AuraTestHelper());
helper_->SetUp(context_factory, context_factory_private);
new wm::DefaultActivationClient(helper_->root_window());
// We need a window to cover desktop area so that DesktopCaptureDeviceAura
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -600,8 +600,7 @@ class RenderWidgetHostViewAuraTest : public testing::Test {
ImageTransportFactory::InitializeForUnitTests(
std::unique_ptr<ImageTransportFactory>(
new NoTransportImageTransportFactory));
aura_test_helper_.reset(
new aura::test::AuraTestHelper(base::MessageLoopForUI::current()));
aura_test_helper_.reset(new aura::test::AuraTestHelper());
aura_test_helper_->SetUp(
ImageTransportFactory::GetInstance()->GetContextFactory(),
ImageTransportFactory::GetInstance()->GetContextFactoryPrivate());
Expand Down
3 changes: 1 addition & 2 deletions content/public/test/test_renderer_host.cc
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,7 @@ void RenderViewHostTestHarness::SetUp() {
ui::ContextFactoryPrivate* context_factory_private =
ImageTransportFactory::GetInstance()->GetContextFactoryPrivate();

aura_test_helper_.reset(
new aura::test::AuraTestHelper(base::MessageLoopForUI::current()));
aura_test_helper_.reset(new aura::test::AuraTestHelper());
aura_test_helper_->SetUp(context_factory, context_factory_private);
new wm::DefaultActivationClient(aura_test_helper_->root_window());
#endif
Expand Down
1 change: 1 addition & 0 deletions ui/aura/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ static_library("test_support") {
":aura",
]
deps = [
"//base/test:test_support",
"//services/ui/public/interfaces",
"//skia",
"//testing/gtest",
Expand Down
8 changes: 6 additions & 2 deletions ui/aura/test/aura_test_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "ui/aura/test/aura_test_base.h"

#include "base/memory/ptr_util.h"
#include "ui/aura/client/window_parenting_client.h"
#include "ui/aura/mus/property_utils.h"
#include "ui/aura/mus/window_tree_client.h"
Expand All @@ -22,7 +23,10 @@ namespace aura {
namespace test {

AuraTestBase::AuraTestBase()
: window_manager_delegate_(this), window_tree_client_delegate_(this) {}
: scoped_task_environment_(
base::test::ScopedTaskEnvironment::MainThreadType::UI),
window_manager_delegate_(this),
window_tree_client_delegate_(this) {}

AuraTestBase::~AuraTestBase() {
CHECK(setup_called_)
Expand Down Expand Up @@ -79,7 +83,7 @@ void AuraTestBase::SetUp() {
ui::InitializeContextFactoryForTests(enable_pixel_output, &context_factory,
&context_factory_private);

helper_.reset(new AuraTestHelper(&message_loop_));
helper_ = base::MakeUnique<AuraTestHelper>();
if (use_mus_) {
helper_->EnableMusWithTestWindowTree(window_tree_client_delegate_,
window_manager_delegate_);
Expand Down
5 changes: 3 additions & 2 deletions ui/aura/test/aura_test_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

#include "base/compiler_specific.h"
#include "base/macros.h"
#include "base/message_loop/message_loop.h"
#include "base/test/scoped_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/aura/mus/property_converter.h"
#include "ui/aura/mus/window_manager_delegate.h"
Expand Down Expand Up @@ -140,14 +140,15 @@ class AuraTestBase : public testing::Test,
PropertyConverter* GetPropertyConverter() override;

private:
base::test::ScopedTaskEnvironment scoped_task_environment_;

// Only used for mus. Both are are initialized to this, but may be reset.
WindowManagerDelegate* window_manager_delegate_;
WindowTreeClientDelegate* window_tree_client_delegate_;

bool use_mus_ = false;
bool setup_called_ = false;
bool teardown_called_ = false;
base::MessageLoopForUI message_loop_;
PropertyConverter property_converter_;
std::unique_ptr<AuraTestHelper> helper_;
std::vector<std::unique_ptr<WindowTreeHostMus>> window_tree_hosts_;
Expand Down
4 changes: 1 addition & 3 deletions ui/aura/test/aura_test_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

#include "base/command_line.h"
#include "base/memory/ptr_util.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "ui/aura/client/default_capture_client.h"
#include "ui/aura/client/focus_client.h"
Expand Down Expand Up @@ -48,9 +47,8 @@ AuraTestHelper* g_instance = nullptr;

} // namespace

AuraTestHelper::AuraTestHelper(base::MessageLoopForUI* message_loop)
AuraTestHelper::AuraTestHelper()
: setup_called_(false), teardown_called_(false) {
DCHECK(message_loop);
// Disable animations during tests.
zero_duration_mode_.reset(new ui::ScopedAnimationDurationScaleMode(
ui::ScopedAnimationDurationScaleMode::ZERO_DURATION));
Expand Down
6 changes: 1 addition & 5 deletions ui/aura/test/aura_test_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@
#include "ui/aura/window_event_dispatcher.h"
#include "ui/aura/window_tree_host.h"

namespace base {
class MessageLoopForUI;
}

namespace ui {
class ContextFactory;
class ScopedAnimationDurationScaleMode;
Expand Down Expand Up @@ -48,7 +44,7 @@ class TestWindowParentingClient;
// that are necessary to run test on Aura.
class AuraTestHelper {
public:
explicit AuraTestHelper(base::MessageLoopForUI* message_loop);
AuraTestHelper();
~AuraTestHelper();

// Returns the current AuraTestHelper, or nullptr if it's not alive.
Expand Down
12 changes: 8 additions & 4 deletions ui/keyboard/keyboard_controller_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
#include "base/bind.h"
#include "base/command_line.h"
#include "base/macros.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "base/test/scoped_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/aura/client/focus_client.h"
#include "ui/aura/layout_manager.h"
Expand Down Expand Up @@ -212,7 +212,11 @@ class KeyboardControllerTest : public testing::Test,
public KeyboardControllerObserver {
public:
KeyboardControllerTest()
: number_of_calls_(0), ui_(nullptr), keyboard_closed_(false) {}
: scoped_task_environment_(
base::test::ScopedTaskEnvironment::MainThreadType::UI),
number_of_calls_(0),
ui_(nullptr),
keyboard_closed_(false) {}
~KeyboardControllerTest() override {}

void SetUp() override {
Expand All @@ -225,7 +229,7 @@ class KeyboardControllerTest : public testing::Test,
&context_factory_private);

ui::SetUpInputMethodFactoryForTesting();
aura_test_helper_.reset(new aura::test::AuraTestHelper(&message_loop_));
aura_test_helper_.reset(new aura::test::AuraTestHelper());
aura_test_helper_->SetUp(context_factory, context_factory_private);
new wm::DefaultActivationClient(aura_test_helper_->root_window());
focus_controller_.reset(new TestFocusController(root_window()));
Expand Down Expand Up @@ -302,7 +306,7 @@ class KeyboardControllerTest : public testing::Test,

void ResetController() { controller_.reset(); }

base::MessageLoopForUI message_loop_;
base::test::ScopedTaskEnvironment scoped_task_environment_;
std::unique_ptr<aura::test::AuraTestHelper> aura_test_helper_;
std::unique_ptr<TestFocusController> focus_controller_;

Expand Down
3 changes: 1 addition & 2 deletions ui/snapshot/snapshot_aura_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,7 @@ class SnapshotAuraTest : public testing::Test {
ui::InitializeContextFactoryForTests(enable_pixel_output, &context_factory,
&context_factory_private);

helper_.reset(
new aura::test::AuraTestHelper(base::MessageLoopForUI::current()));
helper_.reset(new aura::test::AuraTestHelper());
helper_->SetUp(context_factory, context_factory_private);
new ::wm::DefaultActivationClient(helper_->root_window());
}
Expand Down
6 changes: 2 additions & 4 deletions ui/views/test/scoped_views_test_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include <utility>

#include "base/memory/ptr_util.h"
#include "base/message_loop/message_loop.h"
#include "ui/base/clipboard/clipboard.h"
#include "ui/base/ime/input_method_initializer.h"
#include "ui/base/test/test_clipboard.h"
Expand All @@ -33,9 +32,8 @@ ScopedViewsTestHelper::ScopedViewsTestHelper(
test_views_delegate_->set_context_factory(context_factory);
test_views_delegate_->set_context_factory_private(context_factory_private);

test_helper_.reset(ViewsTestHelper::Create(base::MessageLoopForUI::current(),
context_factory,
context_factory_private));
test_helper_.reset(
ViewsTestHelper::Create(context_factory, context_factory_private));
platform_test_helper_->OnTestHelperCreated(test_helper_.get());
test_helper_->SetUp();

Expand Down
5 changes: 0 additions & 5 deletions ui/views/test/views_test_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@
#include "base/macros.h"
#include "ui/gfx/native_widget_types.h"

namespace base {
class MessageLoopForUI;
}

namespace ui {
class ContextFactory;
class ContextFactoryPrivate;
Expand All @@ -28,7 +24,6 @@ class ViewsTestHelper {

// Create a platform specific instance.
static ViewsTestHelper* Create(
base::MessageLoopForUI* message_loop,
ui::ContextFactory* context_factory,
ui::ContextFactoryPrivate* context_factory_private);

Expand Down
7 changes: 2 additions & 5 deletions ui/views/test/views_test_helper_aura.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,17 @@ namespace views {

// static
ViewsTestHelper* ViewsTestHelper::Create(
base::MessageLoopForUI* message_loop,
ui::ContextFactory* context_factory,
ui::ContextFactoryPrivate* context_factory_private) {
return new ViewsTestHelperAura(message_loop, context_factory,
context_factory_private);
return new ViewsTestHelperAura(context_factory, context_factory_private);
}

ViewsTestHelperAura::ViewsTestHelperAura(
base::MessageLoopForUI* message_loop,
ui::ContextFactory* context_factory,
ui::ContextFactoryPrivate* context_factory_private)
: context_factory_(context_factory),
context_factory_private_(context_factory_private) {
aura_test_helper_.reset(new aura::test::AuraTestHelper(message_loop));
aura_test_helper_.reset(new aura::test::AuraTestHelper());
}

ViewsTestHelperAura::~ViewsTestHelperAura() {
Expand Down
7 changes: 1 addition & 6 deletions ui/views/test/views_test_helper_aura.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,11 @@ class AuraTestHelper;
}
}

namespace base {
class MessageLoopForUI;
}

namespace views {

class ViewsTestHelperAura : public ViewsTestHelper {
public:
ViewsTestHelperAura(base::MessageLoopForUI* message_loop,
ui::ContextFactory* context_factory,
ViewsTestHelperAura(ui::ContextFactory* context_factory,
ui::ContextFactoryPrivate* context_factory_private);
~ViewsTestHelperAura() override;

Expand Down
1 change: 0 additions & 1 deletion ui/views/test/views_test_helper_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

// static
ViewsTestHelper* ViewsTestHelper::Create(
base::MessageLoopForUI* message_loop,
ui::ContextFactory* context_factory,
ui::ContextFactoryPrivate* context_factory_private) {
return new ViewsTestHelperMac;
Expand Down

0 comments on commit 1c906dc

Please sign in to comment.