Commit 82ceeefb authored by Xiyuan Xia's avatar Xiyuan Xia Committed by Commit Bot

AuraTestHelper takes a given aura::Env

Make AuraTestHelper take a given aura::Env and do the test setup
for that Env. TestWs uses it to setup an aura::Env for WindowService
when running in single process mode.

Bug: 874089
Change-Id: I65914ed9942c6ce779cdde4a31ef04d727c8fb29
Reviewed-on: https://chromium-review.googlesource.com/1226365Reviewed-by: default avatarScott Violet <sky@chromium.org>
Commit-Queue: Xiyuan Xia <xiyuan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#591466}
parent 9419349e
......@@ -47,8 +47,10 @@ AuraTestHelper* g_instance = nullptr;
} // namespace
AuraTestHelper::AuraTestHelper()
: setup_called_(false), teardown_called_(false) {
AuraTestHelper::AuraTestHelper() : AuraTestHelper(nullptr) {}
AuraTestHelper::AuraTestHelper(std::unique_ptr<Env> env)
: setup_called_(false), teardown_called_(false), env_(std::move(env)) {
// Disable animations during tests.
zero_duration_mode_.reset(new ui::ScopedAnimationDurationScaleMode(
ui::ScopedAnimationDurationScaleMode::ZERO_DURATION));
......@@ -94,10 +96,12 @@ void AuraTestHelper::SetUp(ui::ContextFactory* context_factory,
window_tree_client_ ? window_tree_client_->connector() : nullptr;
ui::test::EventGeneratorDelegate::SetFactoryFunction(
base::BindRepeating(&EventGeneratorDelegateAura::Create, connector));
Env* env = GetEnv();
// If Env has been configured with MUS, but |mode_| is still |LOCAL|, switch
// to MUS. This is used for tests suites that setup Env globally.
if (Env::HasInstance() && Env::GetInstance()->mode() == Env::Mode::MUS &&
mode_ == Mode::LOCAL) {
if (env && env->mode() == Env::Mode::MUS && mode_ == Mode::LOCAL) {
test_window_tree_client_delegate_ =
std::make_unique<TestWindowTreeClientDelegate>();
EnableMusWithTestWindowTree(test_window_tree_client_delegate_.get());
......@@ -119,16 +123,17 @@ void AuraTestHelper::SetUp(ui::ContextFactory* context_factory,
if (mode_ == Mode::MUS_CREATE_WINDOW_TREE_CLIENT)
InitWindowTreeClient();
if (Env::HasInstance()) {
if (env) {
// Some tests suites create Env globally rather than per test. In this case
// make sure Env is configured with the right mode.
env_mode_to_restore_ = Env::GetInstance()->mode();
EnvTestHelper().SetMode(env_mode);
env_mode_to_restore_ = env->mode();
EnvTestHelper(env).SetMode(env_mode);
} else {
env_ = Env::CreateInstance(env_mode);
env = env_.get();
}
EnvTestHelper env_helper;
EnvTestHelper env_helper(env);
if (env_mode == Env::Mode::MUS) {
env_window_tree_client_setter_ =
std::make_unique<EnvWindowTreeClientSetter>(window_tree_client_);
......@@ -136,11 +141,10 @@ void AuraTestHelper::SetUp(ui::ContextFactory* context_factory,
// Tests assume they can set the mouse location on Env() and have it reflected
// in tests.
env_helper.SetAlwaysUseLastMouseLocation(true);
context_factory_to_restore_ = Env::GetInstance()->context_factory();
context_factory_private_to_restore_ =
Env::GetInstance()->context_factory_private();
Env::GetInstance()->set_context_factory(context_factory);
Env::GetInstance()->set_context_factory_private(context_factory_private);
context_factory_to_restore_ = env->context_factory();
context_factory_private_to_restore_ = env->context_factory_private();
env->set_context_factory(context_factory);
env->set_context_factory_private(context_factory_private);
// Unit tests generally don't want to query the system, rather use the state
// from RootWindow.
env_helper.SetInputStateLookup(nullptr);
......@@ -157,7 +161,7 @@ void AuraTestHelper::SetUp(ui::ContextFactory* context_factory,
test_screen_.reset(TestScreen::Create(host_size, window_tree_client_));
if (!screen)
display::Screen::SetScreenInstance(test_screen_.get());
host_.reset(test_screen_->CreateHostForPrimaryDisplay());
host_.reset(test_screen_->CreateHostForPrimaryDisplay(env));
host_->window()->SetEventTargeter(std::make_unique<WindowTargeter>());
client::SetFocusClient(root_window(), focus_client_.get());
......@@ -199,10 +203,10 @@ void AuraTestHelper::TearDown() {
if (env_) {
env_.reset();
} else {
Env::GetInstance()->set_context_factory(context_factory_to_restore_);
Env::GetInstance()->set_context_factory_private(
context_factory_private_to_restore_);
EnvTestHelper().SetMode(env_mode_to_restore_);
Env* env = GetEnv();
env->set_context_factory(context_factory_to_restore_);
env->set_context_factory_private(context_factory_private_to_restore_);
EnvTestHelper(env).SetMode(env_mode_to_restore_);
}
wm_state_.reset();
......@@ -229,6 +233,10 @@ client::CaptureClient* AuraTestHelper::capture_client() {
return capture_client_.get();
}
Env* AuraTestHelper::GetEnv() {
return env_ ? env_.get() : Env::HasInstance() ? Env::GetInstance() : nullptr;
}
void AuraTestHelper::InitWindowTreeClient() {
window_tree_client_setup_ = std::make_unique<TestWindowTreeClientSetup>();
window_tree_client_setup_->InitWithoutEmbed(window_tree_delegate_);
......
......@@ -48,6 +48,7 @@ class TestWindowParentingClient;
class AuraTestHelper {
public:
AuraTestHelper();
explicit AuraTestHelper(std::unique_ptr<Env> env);
~AuraTestHelper();
// Returns the current AuraTestHelper, or nullptr if it's not alive.
......@@ -95,6 +96,8 @@ class AuraTestHelper {
client::FocusClient* focus_client() { return focus_client_.get(); }
client::CaptureClient* capture_client();
Env* GetEnv();
private:
enum class Mode {
// Classic aura.
......
......@@ -46,7 +46,7 @@ TestScreen::~TestScreen() {
delete host_;
}
WindowTreeHost* TestScreen::CreateHostForPrimaryDisplay() {
WindowTreeHost* TestScreen::CreateHostForPrimaryDisplay(Env* env) {
DCHECK(!host_);
if (window_tree_client_) {
host_ =
......@@ -54,7 +54,8 @@ WindowTreeHost* TestScreen::CreateHostForPrimaryDisplay() {
host_->SetBoundsInPixels(gfx::Rect(GetPrimaryDisplay().GetSizeInPixel()));
} else {
host_ = WindowTreeHost::Create(ui::PlatformWindowInitProperties{gfx::Rect(
GetPrimaryDisplay().GetSizeInPixel())})
GetPrimaryDisplay().GetSizeInPixel())},
env)
.release();
}
// Some tests don't correctly manage window focus/activation states.
......
......@@ -19,6 +19,7 @@ class Transform;
}
namespace aura {
class Env;
class Window;
class WindowTreeClient;
class WindowTreeHost;
......@@ -33,7 +34,7 @@ class TestScreen : public display::ScreenBase, public WindowObserver {
WindowTreeClient* window_tree_client = nullptr);
~TestScreen() override;
WindowTreeHost* CreateHostForPrimaryDisplay();
WindowTreeHost* CreateHostForPrimaryDisplay(Env* env = nullptr);
void SetDeviceScaleFactor(float device_scale_fator);
void SetColorSpace(const gfx::ColorSpace& color_space);
......
......@@ -45,12 +45,13 @@ struct PlatformWindowInitProperties;
}
namespace aura {
class ScopedKeyboardHook;
namespace test {
class WindowTreeHostTestApi;
}
class Env;
class ScopedKeyboardHook;
class WindowEventDispatcher;
class WindowTreeHostObserver;
......@@ -64,9 +65,11 @@ class AURA_EXPORT WindowTreeHost : public ui::internal::InputMethodDelegate,
public:
~WindowTreeHost() override;
// Creates a new WindowTreeHost with the specified |properties|.
// Creates a new WindowTreeHost with the specified |properties| and an
// optional |env|. If |env| is null, the default Env::GetInstance() is used.
static std::unique_ptr<WindowTreeHost> Create(
ui::PlatformWindowInitProperties properties);
ui::PlatformWindowInitProperties properties,
Env* env = nullptr);
// Returns the WindowTreeHost for the specified accelerated widget, or NULL
// if there is none associated.
......
......@@ -43,11 +43,12 @@ namespace aura {
// static
std::unique_ptr<WindowTreeHost> WindowTreeHost::Create(
ui::PlatformWindowInitProperties properties) {
ui::PlatformWindowInitProperties properties,
Env* env) {
return std::make_unique<WindowTreeHostPlatform>(
std::move(properties),
std::make_unique<aura::Window>(nullptr, client::WINDOW_TYPE_UNKNOWN,
Env::GetInstance()));
env ? env : Env::GetInstance()));
}
WindowTreeHostPlatform::WindowTreeHostPlatform(
......
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