1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
|
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ash/input_method/textinput_test_helper.h"
#include <string_view>
#include "base/run_loop.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/threading/platform_thread.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/test/base/interactive_test_utils.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/render_widget_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test_utils.h"
#include "ui/aura/window_event_dispatcher.h"
#include "ui/aura/window_tree_host.h"
#include "ui/base/ime/init/input_method_factory.h"
namespace ash {
namespace input_method {
TextInputTestBase::TextInputTestBase() = default;
TextInputTestBase::~TextInputTestBase() = default;
ui::InputMethod* TextInputTestBase::GetInputMethod() const {
return browser()->window()->GetNativeWindow()->GetHost()->GetInputMethod();
}
TextInputTestHelper::TextInputTestHelper(ui::InputMethod* input_method)
: waiting_type_(NO_WAIT),
selection_range_(gfx::Range::InvalidRange()),
focus_state_(false),
latest_text_input_type_(ui::TEXT_INPUT_TYPE_NONE),
input_method_(input_method) {
input_method_->AddObserver(this);
}
TextInputTestHelper::~TextInputTestHelper() {
input_method_->RemoveObserver(this);
}
std::u16string TextInputTestHelper::GetSurroundingText() const {
return surrounding_text_;
}
gfx::Rect TextInputTestHelper::GetCaretRect() const {
return caret_rect_;
}
gfx::Rect TextInputTestHelper::GetCompositionHead() const {
return composition_head_;
}
gfx::Range TextInputTestHelper::GetSelectionRange() const {
return selection_range_;
}
bool TextInputTestHelper::GetFocusState() const {
return focus_state_;
}
ui::TextInputType TextInputTestHelper::GetTextInputType() const {
return latest_text_input_type_;
}
ui::TextInputClient* TextInputTestHelper::GetTextInputClient() const {
return input_method_->GetTextInputClient();
}
void TextInputTestHelper::OnInputMethodDestroyed(
const ui::InputMethod* input_method) {}
void TextInputTestHelper::OnFocus() {
focus_state_ = true;
if (waiting_type_ == WAIT_ON_FOCUS) {
if (run_loop_) {
run_loop_->Quit();
}
}
}
void TextInputTestHelper::OnBlur() {
focus_state_ = false;
if (waiting_type_ == WAIT_ON_BLUR) {
if (run_loop_) {
run_loop_->Quit();
}
}
}
void TextInputTestHelper::OnCaretBoundsChanged(
const ui::TextInputClient* client) {
gfx::Range text_range;
if (GetTextInputClient()) {
if (!GetTextInputClient()->GetTextRange(&text_range) ||
!GetTextInputClient()->GetTextFromRange(text_range,
&surrounding_text_) ||
!GetTextInputClient()->GetEditableSelectionRange(&selection_range_)) {
return;
}
}
if (waiting_type_ == WAIT_ON_CARET_BOUNDS_CHANGED) {
if (run_loop_) {
run_loop_->Quit();
}
}
}
void TextInputTestHelper::OnTextInputStateChanged(
const ui::TextInputClient* client) {
latest_text_input_type_ =
client ? client->GetTextInputType() : ui::TEXT_INPUT_TYPE_NONE;
if (waiting_type_ == WAIT_ON_TEXT_INPUT_TYPE_CHANGED) {
if (run_loop_) {
run_loop_->Quit();
}
}
}
void TextInputTestHelper::WaitForTextInputStateChanged(
ui::TextInputType expected_type) {
CHECK_EQ(NO_WAIT, waiting_type_);
waiting_type_ = WAIT_ON_TEXT_INPUT_TYPE_CHANGED;
while (latest_text_input_type_ != expected_type) {
run_loop_ = std::make_unique<base::RunLoop>();
run_loop_->Run();
}
waiting_type_ = NO_WAIT;
}
void TextInputTestHelper::WaitForFocus() {
CHECK_EQ(NO_WAIT, waiting_type_);
waiting_type_ = WAIT_ON_FOCUS;
while (focus_state_) {
run_loop_ = std::make_unique<base::RunLoop>();
run_loop_->Run();
}
waiting_type_ = NO_WAIT;
}
void TextInputTestHelper::WaitForBlur() {
CHECK_EQ(NO_WAIT, waiting_type_);
waiting_type_ = WAIT_ON_BLUR;
while (!focus_state_) {
run_loop_ = std::make_unique<base::RunLoop>();
run_loop_->Run();
}
waiting_type_ = NO_WAIT;
}
void TextInputTestHelper::WaitForCaretBoundsChanged(
const gfx::Rect& expected_caret_rect,
const gfx::Rect& expected_composition_head) {
waiting_type_ = WAIT_ON_CARET_BOUNDS_CHANGED;
while (expected_caret_rect != caret_rect_ ||
expected_composition_head != composition_head_) {
run_loop_ = std::make_unique<base::RunLoop>();
run_loop_->Run();
}
waiting_type_ = NO_WAIT;
}
void TextInputTestHelper::WaitForSurroundingTextChanged(
const std::u16string& expected_text) {
waiting_type_ = WAIT_ON_CARET_BOUNDS_CHANGED;
while (expected_text != surrounding_text_) {
run_loop_ = std::make_unique<base::RunLoop>();
run_loop_->Run();
}
waiting_type_ = NO_WAIT;
}
void TextInputTestHelper::WaitForSurroundingTextChanged(
const std::u16string& expected_text,
const gfx::Range& expected_selection) {
waiting_type_ = WAIT_ON_CARET_BOUNDS_CHANGED;
while (expected_text != surrounding_text_ ||
expected_selection != selection_range_) {
run_loop_ = std::make_unique<base::RunLoop>();
run_loop_->Run();
}
waiting_type_ = NO_WAIT;
}
void TextInputTestHelper::WaitForPassageOfTimeMillis(const int milliseconds) {
CHECK_EQ(NO_WAIT, waiting_type_);
waiting_type_ = WAIT_ON_PASSAGE_OF_TIME;
base::PlatformThread::Sleep(base::Milliseconds(milliseconds));
waiting_type_ = NO_WAIT;
}
// static
bool TextInputTestHelper::ConvertRectFromString(const std::string& str,
gfx::Rect* rect) {
DCHECK(rect);
std::vector<std::string_view> rect_piece = base::SplitStringPiece(
str, ",", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
if (rect_piece.size() != 4UL) {
return false;
}
int x, y, width, height;
if (!base::StringToInt(rect_piece[0], &x)) {
return false;
}
if (!base::StringToInt(rect_piece[1], &y)) {
return false;
}
if (!base::StringToInt(rect_piece[2], &width)) {
return false;
}
if (!base::StringToInt(rect_piece[3], &height)) {
return false;
}
*rect = gfx::Rect(x, y, width, height);
return true;
}
// static
bool TextInputTestHelper::ClickElement(const std::string& id,
content::WebContents* tab) {
std::string coordinate =
content::EvalJs(
tab, "textinput_helper.retrieveElementCoordinate('" + id + "')")
.ExtractString();
gfx::Rect rect;
if (!ConvertRectFromString(coordinate, &rect)) {
return false;
}
blink::WebMouseEvent mouse_event(
blink::WebInputEvent::Type::kMouseDown,
blink::WebInputEvent::kNoModifiers,
blink::WebInputEvent::GetStaticTimeStampForTests());
mouse_event.button = blink::WebMouseEvent::Button::kLeft;
mouse_event.SetPositionInWidget(rect.CenterPoint().x(),
rect.CenterPoint().y());
mouse_event.click_count = 1;
tab->GetRenderViewHost()->GetWidget()->ForwardMouseEvent(mouse_event);
mouse_event.SetType(blink::WebInputEvent::Type::kMouseUp);
tab->GetRenderViewHost()->GetWidget()->ForwardMouseEvent(mouse_event);
return true;
}
// static
std::string TextInputTestHelper::GetElementInnerText(
const std::string& id,
content::WebContents* tab) {
return content::EvalJs(tab, "document.getElementById('" + id + "').innerText")
.ExtractString();
}
} // namespace input_method
} // namespace ash
|