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
|
// Copyright (c) 2013 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 "ash/shell.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/chromeos/input_method/textinput_test_helper.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/test/base/interactive_test_utils.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test_utils.h"
#include "ui/aura/client/aura_constants.h"
#include "ui/aura/window_event_dispatcher.h"
#include "ui/base/ime/input_method_factory.h"
namespace chromeos {
namespace {
ui::MockInputMethod* GetInputMethod() {
ui::MockInputMethod* input_method = static_cast<ui::MockInputMethod*>(
ash::Shell::GetPrimaryRootWindow()->GetProperty(
aura::client::kRootWindowInputMethodKey));
CHECK(input_method);
return input_method;
}
} // namespace
void TextInputTestBase::SetUpInProcessBrowserTestFixture() {
ui::SetUpInputMethodFactoryForTesting();
}
TextInputTestHelper::TextInputTestHelper()
: waiting_type_(NO_WAIT),
selection_range_(gfx::Range::InvalidRange()),
focus_state_(false),
latest_text_input_type_(ui::TEXT_INPUT_TYPE_NONE) {
GetInputMethod()->AddObserver(this);
}
TextInputTestHelper::~TextInputTestHelper() {
GetInputMethod()->RemoveObserver(this);
}
base::string16 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 GetInputMethod()->GetTextInputClient();
}
void TextInputTestHelper::OnTextInputTypeChanged(
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)
base::MessageLoop::current()->Quit();
}
void TextInputTestHelper::OnShowImeIfNeeded() {
}
void TextInputTestHelper::OnInputMethodDestroyed(
const ui::InputMethod* input_method) {
}
void TextInputTestHelper::OnFocus() {
focus_state_ = true;
if (waiting_type_ == WAIT_ON_FOCUS)
base::MessageLoop::current()->Quit();
}
void TextInputTestHelper::OnBlur() {
focus_state_ = false;
if (waiting_type_ == WAIT_ON_BLUR)
base::MessageLoop::current()->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()->GetSelectionRange(&selection_range_))
return;
}
if (waiting_type_ == WAIT_ON_CARET_BOUNDS_CHANGED)
base::MessageLoop::current()->Quit();
}
void TextInputTestHelper::OnTextInputStateChanged(
const ui::TextInputClient* client) {
}
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)
content::RunMessageLoop();
waiting_type_ = NO_WAIT;
}
void TextInputTestHelper::WaitForFocus() {
CHECK_EQ(NO_WAIT, waiting_type_);
waiting_type_ = WAIT_ON_FOCUS;
while (focus_state_)
content::RunMessageLoop();
waiting_type_ = NO_WAIT;
}
void TextInputTestHelper::WaitForBlur() {
CHECK_EQ(NO_WAIT, waiting_type_);
waiting_type_ = WAIT_ON_BLUR;
while (!focus_state_)
content::RunMessageLoop();
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_)
content::RunMessageLoop();
waiting_type_ = NO_WAIT;
}
void TextInputTestHelper::WaitForSurroundingTextChanged(
const base::string16& expected_text,
const gfx::Range& expected_selection) {
waiting_type_ = WAIT_ON_CARET_BOUNDS_CHANGED;
while (expected_text != surrounding_text_ ||
expected_selection != selection_range_)
content::RunMessageLoop();
waiting_type_ = NO_WAIT;
}
// static
bool TextInputTestHelper::ConvertRectFromString(const std::string& str,
gfx::Rect* rect) {
DCHECK(rect);
std::vector<std::string> rect_piece;
if (Tokenize(str, ",", &rect_piece) != 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;
if (!content::ExecuteScriptAndExtractString(
tab,
"textinput_helper.retrieveElementCoordinate('" + id + "')",
&coordinate))
return false;
gfx::Rect rect;
if (!ConvertRectFromString(coordinate, &rect))
return false;
blink::WebMouseEvent mouse_event;
mouse_event.type = blink::WebInputEvent::MouseDown;
mouse_event.button = blink::WebMouseEvent::ButtonLeft;
mouse_event.x = rect.CenterPoint().x();
mouse_event.y = rect.CenterPoint().y();
mouse_event.clickCount = 1;
tab->GetRenderViewHost()->ForwardMouseEvent(mouse_event);
mouse_event.type = blink::WebInputEvent::MouseUp;
tab->GetRenderViewHost()->ForwardMouseEvent(mouse_event);
return true;
}
} // namespace chromeos
|