Commit 126e61fd authored by Darren Shen's avatar Darren Shen Committed by Chromium LUCI CQ

ime: Introduce a fake impl of TextInputClient.

Most IME tests currently use a class called DummyTextInputClient, which
is a mix of a mock and a fake. This makes it quite difficult to use.

Following testing best practices, we introduce a proper WIP fake for
TextInputClient that complies with the TextInputClient interface.

Future CLs will migrate more and more tests to FakeTextInputClient.
DummyTextInputClient will eventually be removed.

Bug: 1148157
Change-Id: Ib6e356d75d8f316ceff95c55fe905eeb505361a8
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2576061
Auto-Submit: Darren Shen <shend@chromium.org>
Commit-Queue: Sadrul Chowdhury <sadrul@chromium.org>
Reviewed-by: default avatarSadrul Chowdhury <sadrul@chromium.org>
Reviewed-by: default avatarJing Wang <jiwan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#835084}
parent a504a601
...@@ -776,6 +776,8 @@ static_library("test_support") { ...@@ -776,6 +776,8 @@ static_library("test_support") {
"ime/dummy_input_method.h", "ime/dummy_input_method.h",
"ime/dummy_text_input_client.cc", "ime/dummy_text_input_client.cc",
"ime/dummy_text_input_client.h", "ime/dummy_text_input_client.h",
"ime/fake_text_input_client.cc",
"ime/fake_text_input_client.h",
] ]
deps += [ deps += [
......
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
#include "ui/base/ime/chromeos/mock_input_method_manager.h" #include "ui/base/ime/chromeos/mock_input_method_manager.h"
#include "ui/base/ime/composition_text.h" #include "ui/base/ime/composition_text.h"
#include "ui/base/ime/dummy_text_input_client.h" #include "ui/base/ime/dummy_text_input_client.h"
#include "ui/base/ime/fake_text_input_client.h"
#include "ui/base/ime/input_method_delegate.h" #include "ui/base/ime/input_method_delegate.h"
#include "ui/base/ime/text_input_client.h" #include "ui/base/ime/text_input_client.h"
#include "ui/events/event.h" #include "ui/events/event.h"
...@@ -391,13 +392,26 @@ class InputMethodChromeOSTest : public internal::InputMethodDelegate, ...@@ -391,13 +392,26 @@ class InputMethodChromeOSTest : public internal::InputMethodDelegate,
// Tests public APIs in ui::InputMethod first. // Tests public APIs in ui::InputMethod first.
TEST_F(InputMethodChromeOSTest, GetInputTextType) { TEST_F(InputMethodChromeOSTest, GetInputTextType) {
EXPECT_EQ(TEXT_INPUT_TYPE_NONE, ime_->GetTextInputType()); InputMethodChromeOS ime(this);
input_type_ = TEXT_INPUT_TYPE_PASSWORD; FakeTextInputClient fake_text_input_client(TEXT_INPUT_TYPE_TEXT);
ime_->OnTextInputTypeChanged(this); ime.SetFocusedTextInputClient(&fake_text_input_client);
EXPECT_EQ(TEXT_INPUT_TYPE_PASSWORD, ime_->GetTextInputType());
input_type_ = TEXT_INPUT_TYPE_TEXT; EXPECT_EQ(ime.GetTextInputType(), TEXT_INPUT_TYPE_TEXT);
ime_->OnTextInputTypeChanged(this);
EXPECT_EQ(TEXT_INPUT_TYPE_TEXT, ime_->GetTextInputType()); ime.SetFocusedTextInputClient(nullptr);
}
TEST_F(InputMethodChromeOSTest, OnTextInputTypeChangedChangesInputType) {
InputMethodChromeOS ime(this);
FakeTextInputClient fake_text_input_client(TEXT_INPUT_TYPE_TEXT);
ime.SetFocusedTextInputClient(&fake_text_input_client);
fake_text_input_client.set_text_input_type(TEXT_INPUT_TYPE_PASSWORD);
ime.OnTextInputTypeChanged(&fake_text_input_client);
EXPECT_EQ(ime.GetTextInputType(), TEXT_INPUT_TYPE_PASSWORD);
ime.SetFocusedTextInputClient(nullptr);
} }
TEST_F(InputMethodChromeOSTest, CanComposeInline) { TEST_F(InputMethodChromeOSTest, CanComposeInline) {
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
namespace ui { namespace ui {
// Dummy implementation of TextInputClient. All functions do nothing. // Dummy implementation of TextInputClient. All functions do nothing.
// TODO(crbug.com/1148157): Replace this class with FakeTextInputClient.
class DummyTextInputClient : public TextInputClient { class DummyTextInputClient : public TextInputClient {
public: public:
DummyTextInputClient(); DummyTextInputClient();
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/base/ime/fake_text_input_client.h"
#include "ui/events/event_constants.h"
#include "ui/gfx/geometry/rect.h"
namespace ui {
FakeTextInputClient::FakeTextInputClient(TextInputType text_input_type)
: text_input_type_(text_input_type) {}
FakeTextInputClient::~FakeTextInputClient() = default;
void FakeTextInputClient::set_text_input_type(TextInputType text_input_type) {
text_input_type_ = text_input_type;
}
void FakeTextInputClient::SetCompositionText(
const CompositionText& composition) {}
uint32_t FakeTextInputClient::ConfirmCompositionText(bool keep_selection) {
return UINT32_MAX;
}
void FakeTextInputClient::ClearCompositionText() {}
void FakeTextInputClient::InsertText(const base::string16& text) {}
void FakeTextInputClient::InsertChar(const KeyEvent& event) {}
TextInputType FakeTextInputClient::GetTextInputType() const {
return text_input_type_;
}
TextInputMode FakeTextInputClient::GetTextInputMode() const {
return TEXT_INPUT_MODE_NONE;
}
base::i18n::TextDirection FakeTextInputClient::GetTextDirection() const {
return base::i18n::UNKNOWN_DIRECTION;
}
int FakeTextInputClient::GetTextInputFlags() const {
return EF_NONE;
}
bool FakeTextInputClient::CanComposeInline() const {
return false;
}
gfx::Rect FakeTextInputClient::GetCaretBounds() const {
return {};
}
bool FakeTextInputClient::GetCompositionCharacterBounds(uint32_t index,
gfx::Rect* rect) const {
return false;
}
bool FakeTextInputClient::HasCompositionText() const {
return false;
}
ui::TextInputClient::FocusReason FakeTextInputClient::GetFocusReason() const {
return ui::TextInputClient::FOCUS_REASON_NONE;
}
bool FakeTextInputClient::GetTextRange(gfx::Range* range) const {
return false;
}
bool FakeTextInputClient::GetCompositionTextRange(gfx::Range* range) const {
return false;
}
bool FakeTextInputClient::GetEditableSelectionRange(gfx::Range* range) const {
return false;
}
bool FakeTextInputClient::SetEditableSelectionRange(const gfx::Range& range) {
return false;
}
bool FakeTextInputClient::DeleteRange(const gfx::Range& range) {
return false;
}
bool FakeTextInputClient::GetTextFromRange(const gfx::Range& range,
base::string16* text) const {
return false;
}
void FakeTextInputClient::OnInputMethodChanged() {}
bool FakeTextInputClient::ChangeTextDirectionAndLayoutAlignment(
base::i18n::TextDirection direction) {
return false;
}
void FakeTextInputClient::ExtendSelectionAndDelete(size_t before,
size_t after) {}
void FakeTextInputClient::EnsureCaretNotInRect(const gfx::Rect& rect) {}
bool FakeTextInputClient::IsTextEditCommandEnabled(
TextEditCommand command) const {
return false;
}
void FakeTextInputClient::SetTextEditCommandForNextKeyEvent(
TextEditCommand command) {}
ukm::SourceId FakeTextInputClient::GetClientSourceForMetrics() const {
return {};
}
bool FakeTextInputClient::ShouldDoLearning() {
return false;
}
#if defined(OS_WIN) || BUILDFLAG(IS_CHROMEOS_ASH)
bool FakeTextInputClient::SetCompositionFromExistingText(
const gfx::Range& range,
const std::vector<ui::ImeTextSpan>& ui_ime_text_spans) {
return false;
}
#endif
#if BUILDFLAG(IS_CHROMEOS_ASH)
gfx::Range FakeTextInputClient::GetAutocorrectRange() const {
return {};
}
gfx::Rect FakeTextInputClient::GetAutocorrectCharacterBounds() const {
return {};
}
bool FakeTextInputClient::SetAutocorrectRange(const gfx::Range& range) {
return false;
}
#endif
#if defined(OS_WIN)
void FakeTextInputClient::GetActiveTextInputControlLayoutBounds(
base::Optional<gfx::Rect>* control_bounds,
base::Optional<gfx::Rect>* selection_bounds) {}
void FakeTextInputClient::SetActiveCompositionForAccessibility(
const gfx::Range& range,
const base::string16& active_composition_text,
bool is_composition_committed) {}
#endif
} // namespace ui
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef UI_BASE_IME_FAKE_TEXT_INPUT_CLIENT_H_
#define UI_BASE_IME_FAKE_TEXT_INPUT_CLIENT_H_
#include <stddef.h>
#include <stdint.h>
#include "base/macros.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "ui/base/ime/text_input_client.h"
namespace ui {
// Fake in-memory implementation of TextInputClient used for testing.
// This class should act as a 'reference implementation' for TextInputClient.
class FakeTextInputClient : public TextInputClient {
public:
explicit FakeTextInputClient(TextInputType text_input_type);
FakeTextInputClient(const FakeTextInputClient& other) = delete;
FakeTextInputClient& operator=(const FakeTextInputClient& other) = delete;
~FakeTextInputClient() override;
void set_text_input_type(TextInputType text_input_type);
// TextInputClient:
void SetCompositionText(const CompositionText& composition) override;
uint32_t ConfirmCompositionText(bool keep_selection) override;
void ClearCompositionText() override;
void InsertText(const base::string16& text) override;
void InsertChar(const KeyEvent& event) override;
TextInputType GetTextInputType() const override;
TextInputMode GetTextInputMode() const override;
base::i18n::TextDirection GetTextDirection() const override;
int GetTextInputFlags() const override;
bool CanComposeInline() const override;
gfx::Rect GetCaretBounds() const override;
bool GetCompositionCharacterBounds(uint32_t index,
gfx::Rect* rect) const override;
bool HasCompositionText() const override;
ui::TextInputClient::FocusReason GetFocusReason() const override;
bool GetTextRange(gfx::Range* range) const override;
bool GetCompositionTextRange(gfx::Range* range) const override;
bool GetEditableSelectionRange(gfx::Range* range) const override;
bool SetEditableSelectionRange(const gfx::Range& range) override;
bool DeleteRange(const gfx::Range& range) override;
bool GetTextFromRange(const gfx::Range& range,
base::string16* text) const override;
void OnInputMethodChanged() override;
bool ChangeTextDirectionAndLayoutAlignment(
base::i18n::TextDirection direction) override;
void ExtendSelectionAndDelete(size_t before, size_t after) override;
void EnsureCaretNotInRect(const gfx::Rect& rect) override;
bool IsTextEditCommandEnabled(TextEditCommand command) const override;
void SetTextEditCommandForNextKeyEvent(TextEditCommand command) override;
ukm::SourceId GetClientSourceForMetrics() const override;
bool ShouldDoLearning() override;
#if defined(OS_WIN) || BUILDFLAG(IS_CHROMEOS_ASH)
bool SetCompositionFromExistingText(
const gfx::Range& range,
const std::vector<ui::ImeTextSpan>& ui_ime_text_spans) override;
#endif
#if BUILDFLAG(IS_CHROMEOS_ASH)
gfx::Range GetAutocorrectRange() const override;
gfx::Rect GetAutocorrectCharacterBounds() const override;
bool SetAutocorrectRange(const gfx::Range& range) override;
#endif
#if defined(OS_WIN)
void GetActiveTextInputControlLayoutBounds(
base::Optional<gfx::Rect>* control_bounds,
base::Optional<gfx::Rect>* selection_bounds) override;
void SetActiveCompositionForAccessibility(
const gfx::Range& range,
const base::string16& active_composition_text,
bool is_composition_committed) override;
#endif
private:
TextInputType text_input_type_;
};
} // namespace ui
#endif // UI_BASE_IME_FAKE_TEXT_INPUT_CLIENT_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