Commit ef2500c5 authored by Yuichiro Hanada's avatar Yuichiro Hanada Committed by Commit Bot

Introduce ScopedTestInputMethodFactory class.

to make it easier to clean up the environment.

Bug: 994083
Test: exo_unittests keyboard_unittests
Change-Id: I90c24548ef3a49a1ed783645dee8c168451b147c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1761881
Commit-Queue: Yuichiro Hanada <yhanada@chromium.org>
Reviewed-by: default avatarShu Chen <shuchen@chromium.org>
Reviewed-by: default avatarMitsuru Oshima <oshima@chromium.org>
Cr-Commit-Position: refs/heads/master@{#692396}
parent 4ad2798e
......@@ -146,7 +146,6 @@ class KeyboardUIControllerTest : public aura::test::AuraTestBase,
~KeyboardUIControllerTest() override = default;
void SetUp() override {
ui::SetUpInputMethodFactoryForTesting();
aura::test::AuraTestBase::SetUp();
new wm::DefaultActivationClient(root_window());
focus_controller_ = std::make_unique<TestFocusController>(root_window());
......@@ -272,6 +271,7 @@ class KeyboardUIControllerTest : public aura::test::AuraTestBase,
std::unique_ptr<ui::TextInputClient> test_text_input_client_;
bool keyboard_disabled_ = false;
wm::DefaultScreenPositionClient screen_position_client_;
ui::ScopedTestInputMethodFactory scoped_test_input_method_factory_;
DISALLOW_COPY_AND_ASSIGN(KeyboardUIControllerTest);
};
......
......@@ -24,9 +24,8 @@
namespace chromeos {
void TextInputTestBase::SetUpInProcessBrowserTestFixture() {
ui::SetUpInputMethodFactoryForTesting();
}
TextInputTestBase::TextInputTestBase() = default;
TextInputTestBase::~TextInputTestBase() = default;
ui::InputMethod* TextInputTestBase::GetInputMethod() const {
return browser()->window()->GetNativeWindow()->GetHost()->GetInputMethod();
......
......@@ -7,6 +7,7 @@
#include "base/macros.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "ui/base/ime/init/input_method_factory.h"
#include "ui/base/ime/input_method.h"
#include "ui/base/ime/mock_input_method.h"
#include "ui/base/ime/text_input_client.h"
......@@ -22,14 +23,14 @@ namespace chromeos {
// The base class of text input testing.
class TextInputTestBase : public InProcessBrowserTest {
public:
TextInputTestBase() {}
~TextInputTestBase() override {}
void SetUpInProcessBrowserTestFixture() override;
TextInputTestBase();
~TextInputTestBase() override;
ui::InputMethod* GetInputMethod() const;
private:
ui::ScopedTestInputMethodFactory scoped_test_input_method_factory_;
DISALLOW_COPY_AND_ASSIGN(TextInputTestBase);
};
......
......@@ -126,7 +126,6 @@ class KeyboardControllerWebContentTest : public InProcessBrowserTest {
~KeyboardControllerWebContentTest() override {}
void SetUp() override {
ui::SetUpInputMethodFactoryForTesting();
InProcessBrowserTest::SetUp();
}
......@@ -170,6 +169,7 @@ class KeyboardControllerWebContentTest : public InProcessBrowserTest {
private:
std::unique_ptr<ui::DummyTextInputClient> client;
ui::ScopedTestInputMethodFactory scoped_test_input_method_factory_;
DISALLOW_COPY_AND_ASSIGN(KeyboardControllerWebContentTest);
};
......
......@@ -102,7 +102,6 @@ ExoTestBaseViews::ExoTestBaseViews() {}
ExoTestBaseViews::~ExoTestBaseViews() {}
void ExoTestBaseViews::SetUp() {
ui::SetUpInputMethodFactoryForTesting();
views::ViewsTestBase::SetUp();
// Takes care of its own lifetime.
new wm::DefaultActivationClient(root_window());
......
......@@ -6,6 +6,7 @@
#define COMPONENTS_EXO_TEST_EXO_TEST_BASE_VIEWS_H_
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/ime/init/input_method_factory.h"
#include "ui/views/test/views_test_base.h"
namespace exo {
......@@ -27,6 +28,7 @@ class ExoTestBaseViews : public views::ViewsTestBase {
private:
std::unique_ptr<WMHelper> wm_helper_;
ui::ScopedTestInputMethodFactory scoped_test_input_method_factory_;
};
} // namespace test
......
......@@ -72,20 +72,27 @@ std::unique_ptr<InputMethod> CreateInputMethod(
#endif
}
void SetUpInputMethodFactoryForTesting() {
if (g_input_method_set_for_testing)
return;
void SetUpInputMethodForTesting(InputMethod* input_method) {
g_input_method_for_testing = input_method;
}
CHECK(!g_create_input_method_called)
<< "ui::SetUpInputMethodFactoryForTesting was called after use of "
<< "ui::CreateInputMethod. You must call "
<< "ui::SetUpInputMethodFactoryForTesting earlier.";
ScopedTestInputMethodFactory::ScopedTestInputMethodFactory() {
CHECK(!g_input_method_set_for_testing)
<< "ScopedTestInputMethodFactory was created after calling "
"ui::SetUpInputMethodFactoryForTesting or inside another "
"ScopedTestInputMethodFactory lifetime.";
DLOG_IF(WARNING, g_create_input_method_called)
<< "ui::CreateInputMethod was already called. That can happen when other "
"tests in the same process uses normal ui::InputMethod instance.";
g_input_method_set_for_testing = true;
g_create_input_method_called = false;
}
void SetUpInputMethodForTesting(InputMethod* input_method) {
g_input_method_for_testing = input_method;
ScopedTestInputMethodFactory::~ScopedTestInputMethodFactory() {
g_input_method_set_for_testing = false;
g_create_input_method_called = false;
}
} // namespace ui
......@@ -26,10 +26,18 @@ std::unique_ptr<InputMethod> CreateInputMethod(
gfx::AcceleratedWidget widget);
// Makes CreateInputMethod return a MockInputMethod.
COMPONENT_EXPORT(UI_BASE_IME_INIT) void SetUpInputMethodFactoryForTesting();
COMPONENT_EXPORT(UI_BASE_IME_INIT)
void SetUpInputMethodForTesting(InputMethod* input_method);
class COMPONENT_EXPORT(UI_BASE_IME_INIT) ScopedTestInputMethodFactory {
public:
ScopedTestInputMethodFactory();
~ScopedTestInputMethodFactory();
private:
DISALLOW_COPY_AND_ASSIGN(ScopedTestInputMethodFactory);
};
} // namespace ui;
#endif // UI_BASE_IME_INIT_INPUT_METHOD_FACTORY_H_
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