ExtensionSyncService: listen for relevant changes instead of being explicitly called...
[chromium-blink-merge.git] / chrome / browser / chromeos / input_method / textinput_test_helper.cc
blob34554d263a42f6dd18080d536bc36dac92dac452
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ash/shell.h"
6 #include "base/strings/string_number_conversions.h"
7 #include "base/strings/string_split.h"
8 #include "base/strings/string_util.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/chromeos/input_method/textinput_test_helper.h"
11 #include "chrome/browser/ui/browser.h"
12 #include "chrome/test/base/interactive_test_utils.h"
13 #include "content/public/browser/render_view_host.h"
14 #include "content/public/browser/web_contents.h"
15 #include "content/public/test/browser_test_utils.h"
16 #include "ui/aura/window_event_dispatcher.h"
17 #include "ui/aura/window_tree_host.h"
18 #include "ui/base/ime/input_method_factory.h"
20 namespace chromeos {
21 namespace {
22 ui::MockInputMethod* GetInputMethod() {
23 ui::MockInputMethod* input_method = static_cast<ui::MockInputMethod*>(
24 ash::Shell::GetPrimaryRootWindow()->GetHost()->GetInputMethod());
25 CHECK(input_method);
26 return input_method;
28 } // namespace
30 void TextInputTestBase::SetUpInProcessBrowserTestFixture() {
31 ui::SetUpInputMethodFactoryForTesting();
34 TextInputTestHelper::TextInputTestHelper()
35 : waiting_type_(NO_WAIT),
36 selection_range_(gfx::Range::InvalidRange()),
37 focus_state_(false),
38 latest_text_input_type_(ui::TEXT_INPUT_TYPE_NONE) {
39 GetInputMethod()->AddObserver(this);
42 TextInputTestHelper::~TextInputTestHelper() {
43 GetInputMethod()->RemoveObserver(this);
46 base::string16 TextInputTestHelper::GetSurroundingText() const {
47 return surrounding_text_;
50 gfx::Rect TextInputTestHelper::GetCaretRect() const {
51 return caret_rect_;
54 gfx::Rect TextInputTestHelper::GetCompositionHead() const {
55 return composition_head_;
58 gfx::Range TextInputTestHelper::GetSelectionRange() const {
59 return selection_range_;
62 bool TextInputTestHelper::GetFocusState() const {
63 return focus_state_;
66 ui::TextInputType TextInputTestHelper::GetTextInputType() const {
67 return latest_text_input_type_;
70 ui::TextInputClient* TextInputTestHelper::GetTextInputClient() const {
71 return GetInputMethod()->GetTextInputClient();
74 void TextInputTestHelper::OnTextInputTypeChanged(
75 const ui::TextInputClient* client) {
76 latest_text_input_type_ =
77 client ? client->GetTextInputType() : ui::TEXT_INPUT_TYPE_NONE;
78 if (waiting_type_ == WAIT_ON_TEXT_INPUT_TYPE_CHANGED)
79 base::MessageLoop::current()->Quit();
82 void TextInputTestHelper::OnShowImeIfNeeded() {
85 void TextInputTestHelper::OnInputMethodDestroyed(
86 const ui::InputMethod* input_method) {
89 void TextInputTestHelper::OnFocus() {
90 focus_state_ = true;
91 if (waiting_type_ == WAIT_ON_FOCUS)
92 base::MessageLoop::current()->Quit();
95 void TextInputTestHelper::OnBlur() {
96 focus_state_ = false;
97 if (waiting_type_ == WAIT_ON_BLUR)
98 base::MessageLoop::current()->Quit();
101 void TextInputTestHelper::OnCaretBoundsChanged(
102 const ui::TextInputClient* client) {
103 gfx::Range text_range;
104 if (GetTextInputClient()) {
105 if (!GetTextInputClient()->GetTextRange(&text_range) ||
106 !GetTextInputClient()->GetTextFromRange(text_range,
107 &surrounding_text_) ||
108 !GetTextInputClient()->GetSelectionRange(&selection_range_))
109 return;
111 if (waiting_type_ == WAIT_ON_CARET_BOUNDS_CHANGED)
112 base::MessageLoop::current()->Quit();
115 void TextInputTestHelper::OnTextInputStateChanged(
116 const ui::TextInputClient* client) {
119 void TextInputTestHelper::WaitForTextInputStateChanged(
120 ui::TextInputType expected_type) {
121 CHECK_EQ(NO_WAIT, waiting_type_);
122 waiting_type_ = WAIT_ON_TEXT_INPUT_TYPE_CHANGED;
123 while (latest_text_input_type_ != expected_type)
124 content::RunMessageLoop();
125 waiting_type_ = NO_WAIT;
128 void TextInputTestHelper::WaitForFocus() {
129 CHECK_EQ(NO_WAIT, waiting_type_);
130 waiting_type_ = WAIT_ON_FOCUS;
131 while (focus_state_)
132 content::RunMessageLoop();
133 waiting_type_ = NO_WAIT;
136 void TextInputTestHelper::WaitForBlur() {
137 CHECK_EQ(NO_WAIT, waiting_type_);
138 waiting_type_ = WAIT_ON_BLUR;
139 while (!focus_state_)
140 content::RunMessageLoop();
141 waiting_type_ = NO_WAIT;
144 void TextInputTestHelper::WaitForCaretBoundsChanged(
145 const gfx::Rect& expected_caret_rect,
146 const gfx::Rect& expected_composition_head) {
147 waiting_type_ = WAIT_ON_CARET_BOUNDS_CHANGED;
148 while (expected_caret_rect != caret_rect_ ||
149 expected_composition_head != composition_head_)
150 content::RunMessageLoop();
151 waiting_type_ = NO_WAIT;
154 void TextInputTestHelper::WaitForSurroundingTextChanged(
155 const base::string16& expected_text,
156 const gfx::Range& expected_selection) {
157 waiting_type_ = WAIT_ON_CARET_BOUNDS_CHANGED;
158 while (expected_text != surrounding_text_ ||
159 expected_selection != selection_range_)
160 content::RunMessageLoop();
161 waiting_type_ = NO_WAIT;
164 // static
165 bool TextInputTestHelper::ConvertRectFromString(const std::string& str,
166 gfx::Rect* rect) {
167 DCHECK(rect);
168 std::vector<base::StringPiece> rect_piece = base::SplitStringPiece(
169 str, ",", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
170 if (rect_piece.size() != 4UL)
171 return false;
172 int x, y, width, height;
173 if (!base::StringToInt(rect_piece[0], &x))
174 return false;
175 if (!base::StringToInt(rect_piece[1], &y))
176 return false;
177 if (!base::StringToInt(rect_piece[2], &width))
178 return false;
179 if (!base::StringToInt(rect_piece[3], &height))
180 return false;
181 *rect = gfx::Rect(x, y, width, height);
182 return true;
185 // static
186 bool TextInputTestHelper::ClickElement(const std::string& id,
187 content::WebContents* tab) {
188 std::string coordinate;
189 if (!content::ExecuteScriptAndExtractString(
190 tab,
191 "textinput_helper.retrieveElementCoordinate('" + id + "')",
192 &coordinate))
193 return false;
194 gfx::Rect rect;
195 if (!ConvertRectFromString(coordinate, &rect))
196 return false;
198 blink::WebMouseEvent mouse_event;
199 mouse_event.type = blink::WebInputEvent::MouseDown;
200 mouse_event.button = blink::WebMouseEvent::ButtonLeft;
201 mouse_event.x = rect.CenterPoint().x();
202 mouse_event.y = rect.CenterPoint().y();
203 mouse_event.clickCount = 1;
204 tab->GetRenderViewHost()->ForwardMouseEvent(mouse_event);
206 mouse_event.type = blink::WebInputEvent::MouseUp;
207 tab->GetRenderViewHost()->ForwardMouseEvent(mouse_event);
208 return true;
211 } // namespace chromeos