Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(610)

Side by Side Diff: content/public/test/text_input_test_utils.cc

Issue 1948343002: [reland] Browser Side Text Input State Tracking for OOPIF (Aura Only) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressing creis@ Comments Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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.
4
5 #include "content/public/test/text_input_test_utils.h"
6
7 #include "content/browser/renderer_host/render_widget_host_view_aura.h"
8 #include "content/browser/renderer_host/render_widget_host_view_base.h"
9 #include "content/browser/renderer_host/render_widget_host_view_base_observer.h"
10 #include "content/browser/renderer_host/text_input_manager.h"
11 #include "content/browser/web_contents/web_contents_impl.h"
12 #include "content/common/text_input_state.h"
13 #include "content/public/browser/render_widget_host_view.h"
14 #include "content/public/browser/web_contents.h"
15 #include "content/public/browser/web_contents_observer.h"
16 #include "content/public/test/test_utils.h"
17 #include "ui/base/ime/input_method.h"
18 #include "ui/base/ime/input_method_observer.h"
19
20 namespace ui {
21 class TextInputClient;
22 }
23
24 namespace content {
25
26 // This class is an observer of TextInputManager associated with the provided
27 // WebContents. An instance of this class is used in TextInputManagerTester to
28 // expose the required API for testing outside of content/.
29 class TextInputManagerTester::InternalObserver
30 : public TextInputManager::Observer,
31 public WebContentsObserver {
32 public:
33 InternalObserver(WebContents* web_contents, TextInputManagerTester* tester)
34 : WebContentsObserver(web_contents),
35 tester_(tester),
36 updated_view_(nullptr),
37 text_input_state_changed_(false) {
38 text_input_manager_ =
39 static_cast<WebContentsImpl*>(web_contents)->GetTextInputManager();
40 DCHECK(!!text_input_manager_);
41 text_input_manager_->AddObserver(this);
42 }
43
44 ~InternalObserver() {
45 if (text_input_manager_)
46 text_input_manager_->RemoveObserver(this);
47 }
48
49 void set_update_text_input_state_called_callback(
50 const TextInputManagerTester::Callback& callback) {
51 update_text_input_state_callback_ = callback;
52 }
53
54 const RenderWidgetHostView* GetUpdatedView() const { return updated_view_; }
55
56 bool text_input_state_changed() const { return text_input_state_changed_; }
57
58 TextInputManager* text_input_manager() const { return text_input_manager_; }
59
60 void WebContentsDestroyed() { text_input_manager_ = nullptr; }
61
62 private:
63 // TextInputManager::Observer implementations.
64 void OnUpdateTextInputStateCalled(TextInputManager* text_input_manager,
65 RenderWidgetHostViewBase* updated_view,
66 bool did_change_state) override {
67 if (text_input_manager_ != text_input_manager)
68 return;
69 text_input_state_changed_ = did_change_state;
70 updated_view_ = updated_view;
71 update_text_input_state_callback_.Run(tester_);
72 }
73
74 TextInputManagerTester* tester_;
75 TextInputManager* text_input_manager_;
76 RenderWidgetHostViewBase* updated_view_;
77 bool text_input_state_changed_;
78 TextInputManagerTester::Callback update_text_input_state_callback_;
79
80 DISALLOW_COPY_AND_ASSIGN(InternalObserver);
81 };
82
83 // This class observers the lifetime of a RenderWidgetHostView. An instance of
Charlie Reis 2016/06/02 22:03:29 nit: observes
84 // this class is used in TestRenderWidgetHostViewDestructionObserver to expose
85 // the required observer API for testing outside of content/.
86 class TestRenderWidgetHostViewDestructionObserver::InternalObserver
87 : public RenderWidgetHostViewBaseObserver {
88 public:
89 InternalObserver(RenderWidgetHostViewBase* view)
90 : view_(view), destroyed_(false) {
91 view->AddObserver(this);
92 }
93
94 ~InternalObserver() override {
95 if (view_)
96 view_->RemoveObserver(this);
97 }
98
99 void Wait() {
100 if (destroyed_)
101 return;
102 message_loop_runner_ = new content::MessageLoopRunner();
103 message_loop_runner_->Run();
104 }
105
106 private:
107 void OnRenderWidgetHostViewBaseDestroyed(
108 RenderWidgetHostViewBase* view) override {
109 DCHECK_EQ(view_, view);
110 destroyed_ = true;
111 view->RemoveObserver(this);
112 view_ = nullptr;
113 if (message_loop_runner_)
114 message_loop_runner_->Quit();
115 }
116
117 RenderWidgetHostViewBase* view_;
118 bool destroyed_;
119 scoped_refptr<MessageLoopRunner> message_loop_runner_;
120
121 DISALLOW_COPY_AND_ASSIGN(InternalObserver);
122 };
123
124 #ifdef USE_AURA
125 class InputMethodObserverAura : public TestInputMethodObserver,
126 public ui::InputMethodObserver {
127 public:
128 explicit InputMethodObserverAura(ui::InputMethod* input_method)
129 : input_method_(input_method), text_input_client_(nullptr) {
130 input_method_->AddObserver(this);
131 }
132
133 ~InputMethodObserverAura() override {
134 if (input_method_)
135 input_method_->RemoveObserver(this);
136 }
137
138 // TestInputMethodObserver implementations.
139 ui::TextInputType GetTextInputTypeFromClient() override {
140 if (text_input_client_)
141 return text_input_client_->GetTextInputType();
142
143 return ui::TEXT_INPUT_TYPE_NONE;
144 }
145
146 void SetOnTextInputTypeChangedCallback(
147 const base::Closure& callback) override {
148 on_text_input_type_changed_callback_ = callback;
149 }
150
151 void SetOnShowImeIfNeededCallback(const base::Closure& callback) override {
152 on_show_ime_if_needed_callback_ = callback;
153 }
154
155 private:
156 // ui::InputMethodObserver implementations.
157 void OnTextInputTypeChanged(const ui::TextInputClient* client) override {
158 text_input_client_ = client;
159 on_text_input_type_changed_callback_.Run();
160 }
161
162 void OnFocus() override {}
163 void OnBlur() override {}
164 void OnCaretBoundsChanged(const ui::TextInputClient* client) override {}
165 void OnTextInputStateChanged(const ui::TextInputClient* client) override {}
166 void OnInputMethodDestroyed(const ui::InputMethod* input_method) override {}
167
168 void OnShowImeIfNeeded() override { on_show_ime_if_needed_callback_.Run(); }
169
170 ui::InputMethod* input_method_;
171 const ui::TextInputClient* text_input_client_;
172 base::Closure on_text_input_type_changed_callback_;
173 base::Closure on_show_ime_if_needed_callback_;
174
175 DISALLOW_COPY_AND_ASSIGN(InputMethodObserverAura);
176 };
177 #endif
178
179 ui::TextInputType GetTextInputTypeFromWebContents(WebContents* web_contents) {
180 const TextInputState* state = static_cast<WebContentsImpl*>(web_contents)
181 ->GetTextInputManager()
182 ->GetTextInputState();
183 return !!state ? state->type : ui::TEXT_INPUT_TYPE_NONE;
184 }
185
186 bool GetTextInputTypeForView(WebContents* web_contents,
187 RenderWidgetHostView* view,
188 ui::TextInputType* type) {
189 TextInputManager* manager =
190 static_cast<WebContentsImpl*>(web_contents)->GetTextInputManager();
191
192 RenderWidgetHostViewBase* view_base =
193 static_cast<RenderWidgetHostViewBase*>(view);
194 if (!manager || !manager->IsRegistered(view_base))
195 return false;
196
197 *type = manager->text_input_state_map_[view_base].type;
198 return true;
199 }
200
201 RenderWidgetHostView* GetActiveViewFromWebContents(WebContents* web_contents) {
202 return static_cast<WebContentsImpl*>(web_contents)
203 ->GetTextInputManager()
204 ->GetActiveView();
205 }
206
207 TextInputManagerTester::TextInputManagerTester(WebContents* web_contents)
208 : observer_(new InternalObserver(web_contents, this)) {}
209
210 TextInputManagerTester::~TextInputManagerTester() {}
211
212 void TextInputManagerTester::SetUpdateTextInputStateCalledCallback(
213 const Callback& callback) {
214 observer_->set_update_text_input_state_called_callback(callback);
215 }
216
217 bool TextInputManagerTester::GetTextInputType(ui::TextInputType* type) {
218 DCHECK(observer_->text_input_manager());
219 const TextInputState* state =
220 observer_->text_input_manager()->GetTextInputState();
221 if (!state)
222 return false;
223 *type = state->type;
224 return true;
225 }
226
227 bool TextInputManagerTester::GetTextInputValue(std::string* value) {
228 DCHECK(observer_->text_input_manager());
229 const TextInputState* state =
230 observer_->text_input_manager()->GetTextInputState();
231 if (!state)
232 return false;
233 *value = state->value;
234 return true;
235 }
236
237 const RenderWidgetHostView* TextInputManagerTester::GetActiveView() {
238 DCHECK(observer_->text_input_manager());
239 return observer_->text_input_manager()->GetActiveView();
240 }
241
242 const RenderWidgetHostView* TextInputManagerTester::GetUpdatedView() {
243 return observer_->GetUpdatedView();
244 }
245
246 bool TextInputManagerTester::IsTextInputStateChanged() {
247 return observer_->text_input_state_changed();
248 }
249
250 TestRenderWidgetHostViewDestructionObserver::
251 TestRenderWidgetHostViewDestructionObserver(RenderWidgetHostView* view)
252 : observer_(
253 new InternalObserver(static_cast<RenderWidgetHostViewBase*>(view))) {}
254
255 TestRenderWidgetHostViewDestructionObserver::
256 ~TestRenderWidgetHostViewDestructionObserver() {}
257
258 void TestRenderWidgetHostViewDestructionObserver::Wait() {
259 observer_->Wait();
260 }
261
262 // TextInputStateSender implementations.
263 TextInputStateSender::TextInputStateSender(RenderWidgetHostView* view)
264 : text_input_state_(new TextInputState()),
265 view_(static_cast<RenderWidgetHostViewBase*>(view)) {}
266
267 TextInputStateSender::~TextInputStateSender() {}
268
269 void TextInputStateSender::Send() {
270 if (view_)
271 view_->TextInputStateChanged(*text_input_state_);
272 }
273
274 void TextInputStateSender::SetFromCurrentState() {
275 if (view_) {
276 *text_input_state_ =
277 *RenderWidgetHostImpl::From(view_->GetRenderWidgetHost())
278 ->delegate()
279 ->GetTextInputManager()
280 ->GetTextInputState();
281 }
282 }
283
284 void TextInputStateSender::SetType(ui::TextInputType type) {
285 text_input_state_->type = type;
286 }
287
288 void TextInputStateSender::SetMode(ui::TextInputMode mode) {
289 text_input_state_->mode = mode;
290 }
291
292 void TextInputStateSender::SetFlags(int flags) {
293 text_input_state_->flags = flags;
294 }
295
296 void TextInputStateSender::SetCanComposeInline(bool can_compose_inline) {
297 text_input_state_->can_compose_inline = can_compose_inline;
298 }
299
300 void TextInputStateSender::SetShowImeIfNeeded(bool show_ime_if_needed) {
301 text_input_state_->show_ime_if_needed = show_ime_if_needed;
302 }
303
304 void TextInputStateSender::SetIsNonImeChange(bool is_non_ime_change) {
305 text_input_state_->is_non_ime_change = is_non_ime_change;
306 }
307
308 TestInputMethodObserver::TestInputMethodObserver() {}
309
310 TestInputMethodObserver::~TestInputMethodObserver() {}
311
312 // static
313 std::unique_ptr<TestInputMethodObserver> TestInputMethodObserver::Create(
314 WebContents* web_contents) {
315 std::unique_ptr<TestInputMethodObserver> observer;
316
317 #ifdef USE_AURA
318 RenderWidgetHostViewAura* view = static_cast<RenderWidgetHostViewAura*>(
319 web_contents->GetRenderWidgetHostView());
320 observer.reset(new InputMethodObserverAura(view->GetInputMethod()));
321 #endif
322 return observer;
323 }
324
325 } // namespace content
OLDNEW
« content/public/test/text_input_test_utils.h ('K') | « content/public/test/text_input_test_utils.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698