Skip to content

Commit

Permalink
inputs: Listen to FakeTextInputMethod's OnFocus in tests.
Browse files Browse the repository at this point in the history
Right now, to detect when we have focus, we listen to the
ui::InputMethodObserver's OnTextInputStateChanged.

It is easier to listen to FakeTextInputMethod's OnFocus instead. The
lacros browser tests are meant to be tests that test the lacros browser
against a fake input method, so it makes sense to listen to the fake
input method's focus event.

This is to prepare for a follow up CL where we also listen for
surrounding text changes from FakeTextInputMethod.

Bug: b/238838841
Change-Id: If46fb42a7e71ab183552c7e1de6e26a6e6ceae28
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4199627
Reviewed-by: Hidehiko Abe <hidehiko@chromium.org>
Reviewed-by: Curtis McMullan <curtismcmullan@chromium.org>
Commit-Queue: Darren Shen <shend@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1099080}
  • Loading branch information
darrnshn authored and Chromium LUCI CQ committed Jan 31, 2023
1 parent 5717b2a commit e01851d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 30 deletions.
40 changes: 24 additions & 16 deletions chrome/browser/ash/crosapi/input_method_test_interface_ash.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
namespace crosapi {
namespace {

ash::InputMethodAsh* GetInputMethod() {
ash::InputMethodAsh* GetTextInputTarget() {
const ash::IMEBridge* bridge = ash::IMEBridge::Get();
if (!bridge)
return nullptr;
Expand Down Expand Up @@ -43,6 +43,12 @@ FakeTextInputMethod::FakeTextInputMethod() = default;

FakeTextInputMethod::~FakeTextInputMethod() = default;

void FakeTextInputMethod::Focus(const InputContext& input_context) {
for (auto& observer : observers_) {
observer.OnFocus();
}
}

ui::VirtualKeyboardController*
FakeTextInputMethod::GetVirtualKeyboardController() const {
return nullptr;
Expand All @@ -59,6 +65,14 @@ void FakeTextInputMethod::ProcessKeyEvent(const ui::KeyEvent& key_event,
std::move(callback));
}

void FakeTextInputMethod::AddObserver(Observer* observer) {
observers_.AddObserver(observer);
}

void FakeTextInputMethod::RemoveObserver(Observer* observer) {
observers_.RemoveObserver(observer);
}

uint64_t FakeTextInputMethod::GetCurrentKeyEventId() const {
return current_key_event_id_;
}
Expand All @@ -74,11 +88,10 @@ void FakeTextInputMethod::KeyEventHandled(uint64_t key_event_id, bool handled) {
}

InputMethodTestInterfaceAsh::InputMethodTestInterfaceAsh()
: input_method_(GetInputMethod()) {
DCHECK(input_method_);
input_method_observation_.Observe(input_method_);

: text_input_target_(GetTextInputTarget()) {
DCHECK(text_input_target_);
OverrideTextInputMethod(&fake_text_input_method_);
text_input_method_observation_.Observe(&fake_text_input_method_);
}

InputMethodTestInterfaceAsh::~InputMethodTestInterfaceAsh() {
Expand All @@ -87,7 +100,7 @@ InputMethodTestInterfaceAsh::~InputMethodTestInterfaceAsh() {

void InputMethodTestInterfaceAsh::WaitForFocus(WaitForFocusCallback callback) {
// If `GetTextInputClient` is not null, then it's already focused.
if (input_method_->GetTextInputClient()) {
if (text_input_target_->GetTextInputClient()) {
std::move(callback).Run();
return;
}
Expand All @@ -98,7 +111,7 @@ void InputMethodTestInterfaceAsh::WaitForFocus(WaitForFocusCallback callback) {

void InputMethodTestInterfaceAsh::CommitText(const std::string& text,
CommitTextCallback callback) {
input_method_->CommitText(
text_input_target_->CommitText(
base::UTF8ToUTF16(text),
ui::TextInputClient::InsertTextCursorBehavior::kMoveCursorAfterText);
std::move(callback).Run();
Expand All @@ -111,7 +124,8 @@ void InputMethodTestInterfaceAsh::SetComposition(
ui::CompositionText composition;
composition.text = base::UTF8ToUTF16(text);

input_method_->UpdateCompositionText(composition, index, /*visible=*/true);
text_input_target_->UpdateCompositionText(composition, index,
/*visible=*/true);
std::move(callback).Run();
}

Expand All @@ -123,7 +137,7 @@ void InputMethodTestInterfaceAsh::SendKeyEvent(mojom::KeyEventPtr event,
static_cast<ui::KeyboardCode>(event->key_code),
static_cast<ui::DomCode>(event->dom_code), ui::EF_NONE,
static_cast<ui::DomKey>(event->dom_key), ui::EventTimeForNow());
input_method_->SendKeyEvent(&key_press);
text_input_target_->SendKeyEvent(&key_press);
std::move(callback).Run(fake_text_input_method_.GetCurrentKeyEventId());
}

Expand All @@ -135,13 +149,7 @@ void InputMethodTestInterfaceAsh::KeyEventHandled(
std::move(callback).Run();
}

void InputMethodTestInterfaceAsh::OnTextInputStateChanged(
const ui::TextInputClient* client) {
// Focus is actually propagated via OnTextInputStateChanged, not
// OnFocus/OnBlur (which are only used for unit tests).
if (!client)
return;

void InputMethodTestInterfaceAsh::OnFocus() {
focus_callbacks_.Notify();
}

Expand Down
32 changes: 18 additions & 14 deletions chrome/browser/ash/crosapi/input_method_test_interface_ash.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,24 @@
#include <string>

#include "base/callback_list.h"
#include "base/containers/id_map.h"
#include "base/observer_list.h"
#include "base/scoped_observation.h"
#include "chromeos/crosapi/mojom/test_controller.mojom.h"
#include "ui/base/ime/ash/input_method_ash.h"
#include "ui/base/ime/input_method_observer.h"

namespace crosapi {

class FakeTextInputMethod : public ash::TextInputMethod {
public:
class Observer {
public:
virtual void OnFocus() = 0;
};

FakeTextInputMethod();
~FakeTextInputMethod() override;

void Focus(const InputContext& input_context) override {}
void Focus(const InputContext& input_context) override;
void Blur() override {}
void OnTouch(ui::EventPointerType pointerType) override {}
void Enable(const std::string& component_id) override {}
Expand All @@ -43,16 +47,20 @@ class FakeTextInputMethod : public ash::TextInputMethod {
const ash::ime::AssistiveWindow& window) override {}
bool IsReadyForTesting() override;

void AddObserver(Observer* observer);
void RemoveObserver(Observer* observer);

uint64_t GetCurrentKeyEventId() const;
void KeyEventHandled(uint64_t key_event_id, bool handled);

private:
uint64_t current_key_event_id_ = 0;
std::map<uint64_t, KeyEventDoneCallback> pending_key_event_callbacks_;
base::ObserverList<Observer>::Unchecked observers_;
};

class InputMethodTestInterfaceAsh : public mojom::InputMethodTestInterface,
public ui::InputMethodObserver {
public FakeTextInputMethod::Observer {
public:
InputMethodTestInterfaceAsh();
~InputMethodTestInterfaceAsh() override;
Expand All @@ -73,19 +81,15 @@ class InputMethodTestInterfaceAsh : public mojom::InputMethodTestInterface,
bool handled,
KeyEventHandledCallback callback) override;

// ui::InputMethodObserver:
void OnFocus() override {}
void OnBlur() override {}
void OnCaretBoundsChanged(const ui::TextInputClient* client) override {}
void OnTextInputStateChanged(const ui::TextInputClient* client) override;
void OnInputMethodDestroyed(const ui::InputMethod* input_method) override {}
// FakeTextInputMethod::Observer:
void OnFocus() override;

private:
ash::InputMethodAsh* input_method_;
base::ScopedObservation<ui::InputMethod, ui::InputMethodObserver>
input_method_observation_{this};
base::OnceClosureList focus_callbacks_;
ash::InputMethodAsh* text_input_target_;
FakeTextInputMethod fake_text_input_method_;
base::ScopedObservation<FakeTextInputMethod, FakeTextInputMethod::Observer>
text_input_method_observation_{this};
base::OnceClosureList focus_callbacks_;
};

} // namespace crosapi
Expand Down

0 comments on commit e01851d

Please sign in to comment.