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

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 shuchen@ and kenrb@ comments Created 4 years, 7 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/test/test_utils.h"
16 #include "ui/base/ime/input_method.h"
17 #include "ui/base/ime/input_method_observer.h"
18
19 namespace ui {
20 class TextInputClient;
21 }
22
23 namespace content {
24
25 namespace {
26
27 // The implementation of the TestTextInputManagerObserver which is observing the
28 // content::TextInputManager for the given WebContentsImpl.
29 class TextInputManagerObserver : public TestTextInputManagerObserver,
30 public TextInputManager::Observer {
31 public:
32 // TestTextInputManagerObserver implementations.
33 TextInputManagerObserver(WebContents* web_contents)
34 : updated_view_(nullptr), changed_(false) {
35 text_input_manager_ =
36 static_cast<WebContentsImpl*>(web_contents)->GetTextInputManager();
37 DCHECK(!!text_input_manager_);
38 text_input_manager_->AddObserver(this);
39 }
40
41 ~TextInputManagerObserver() override {
42 text_input_manager_->RemoveObserver(this);
43 }
44
45 void SetUpdateCallback(const content::TestTextInputManagerObserver::Callback&
46 callback) override {
47 update_callback_.reset(
48 new content::TestTextInputManagerObserver::Callback(callback));
49 }
50
51 void SetUpdateCalledCallback(
52 const content::TestTextInputManagerObserver::Callback& callback)
53 override {
54 update_called_callback_.reset(
55 new content::TestTextInputManagerObserver::Callback(callback));
56 }
57
58 bool GetTextInputValue(std::string& value) const override {
59 const content::TextInputState* state =
60 text_input_manager_->GetTextInputState();
61 if (!state)
62 return false;
63 value = state->value;
64 return true;
65 }
66
67 ui::TextInputType GetTextInputType() const override {
68 const content::TextInputState* state =
69 text_input_manager_->GetTextInputState();
70 return !!state ? state->type : ui::TEXT_INPUT_TYPE_NONE;
71 }
72
73 const RenderWidgetHostView* GetActiveView() const override {
74 return text_input_manager_->GetActiveView();
75 }
76
77 const RenderWidgetHostView* GetUpdatedView() const override {
78 return updated_view_;
79 }
80
81 bool IsTextInputStateChanged() const override { return changed_; }
82
83 private:
84 // TextInputManager::Observer implementations.
85 void OnTextInputStateUpdated(
86 TextInputManager* text_input_manager,
87 RenderWidgetHostViewBase* updated_view) override {
88 if (text_input_manager_ != text_input_manager)
89 return;
90 changed_ = true;
91 updated_view_ = updated_view;
92 if (update_callback_)
93 update_callback_->Run(this);
94 }
95
96 void OnTextInputStateUpdateCalled(TextInputManager* text_input_manager) {
97 if (text_input_manager_ != text_input_manager)
98 return;
99 if (update_called_callback_)
100 update_called_callback_->Run(this);
101 }
102
103 content::TextInputManager* text_input_manager_;
104 content::RenderWidgetHostViewBase* updated_view_;
105 bool changed_;
106 std::unique_ptr<content::TestTextInputManagerObserver::Callback>
107 update_callback_;
108 std::unique_ptr<content::TestTextInputManagerObserver::Callback>
109 update_called_callback_;
110
111 DISALLOW_COPY_AND_ASSIGN(TextInputManagerObserver);
112 };
113
114 class RenderWidgetHostViewBaseObserverImpl
115 : public RenderWidgetHostViewBaseObserver,
116 public RenderWidgetHostViewDestructionObserver {
117 public:
118 RenderWidgetHostViewBaseObserverImpl(RenderWidgetHostViewBase* view)
119 : view_(view), destroyed_(false) {
120 view->AddObserver(this);
121 }
122
123 void Wait() override {
124 if (destroyed_)
125 return;
126 message_loop_runner_ = new content::MessageLoopRunner();
127 message_loop_runner_->Run();
128 }
129
130 private:
131 void OnRenderWidgetHostViewBaseDestroyed(
132 RenderWidgetHostViewBase* view) override {
133 DCHECK(view_ == view);
134 destroyed_ = true;
135 view->RemoveObserver(this);
136 if (message_loop_runner_)
137 message_loop_runner_->Quit();
138 }
139
140 RenderWidgetHostView* view_;
141 bool destroyed_;
142 scoped_refptr<MessageLoopRunner> message_loop_runner_;
143
144 DISALLOW_COPY_AND_ASSIGN(RenderWidgetHostViewBaseObserverImpl);
145 };
146
147 #ifdef USE_AURA
148 class InputMethodObserverAura : public TestInputMethodObserver,
149 public ui::InputMethodObserver {
150 public:
151 explicit InputMethodObserverAura(ui::InputMethod* input_method)
152 : input_method_(input_method), text_input_client_(nullptr) {
153 input_method_->AddObserver(this);
154 }
155 ~InputMethodObserverAura() override {
156 if (input_method_)
157 input_method_->RemoveObserver(this);
158 }
159
160 // TestInputMethodObserver implementations.
161 ui::TextInputType GetTextInputTypeFromClient() const override {
162 if (text_input_client_)
163 return text_input_client_->GetTextInputType();
164
165 return ui::TEXT_INPUT_TYPE_NONE;
166 }
167 void SetOnTextInputTypeChangedCallback(
168 const base::Closure& callback) override {
169 on_text_input_type_changed_callback_.reset(new base::Closure(callback));
170 }
171
172 void SetOnShowImeIfNeededCallback(const base::Closure& callback) override {
173 on_show_ime_if_needed_callback_.reset(new base::Closure(callback));
174 }
175
176 private:
177 // ui::InputMethodObserver implementations.
178 void OnTextInputTypeChanged(const ui::TextInputClient* client) override {
179 text_input_client_ = client;
180 if (on_text_input_type_changed_callback_)
181 on_text_input_type_changed_callback_->Run();
182 }
183
184 void OnFocus() override {}
185 void OnBlur() override {}
186 void OnCaretBoundsChanged(const ui::TextInputClient* client) override {}
187 void OnTextInputStateChanged(const ui::TextInputClient* client) override {}
188 void OnInputMethodDestroyed(const ui::InputMethod* input_method) override {}
189
190 void OnShowImeIfNeeded() override {
191 if (on_show_ime_if_needed_callback_)
192 on_show_ime_if_needed_callback_->Run();
193 }
194
195 ui::InputMethod* input_method_;
196 const ui::TextInputClient* text_input_client_;
197 std::unique_ptr<base::Closure> on_text_input_type_changed_callback_;
198 std::unique_ptr<base::Closure> on_show_ime_if_needed_callback_;
199
200 DISALLOW_COPY_AND_ASSIGN(InputMethodObserverAura);
201 };
202 #endif
203
204 } // namespace
205
206 // TestTextInputManagerObserver Implementations.
207 TestTextInputManagerObserver::~TestTextInputManagerObserver() {}
208
209 // static
210 std::unique_ptr<TestTextInputManagerObserver>
211 TestTextInputManagerObserver::Create(WebContents* web_contents) {
212 return base::WrapUnique(new TextInputManagerObserver(web_contents));
213 }
214
215 // static
216 std::unordered_map<const RenderWidgetHostView*, ui::TextInputType>
217 TestTextInputManagerObserver::GetTextInputTypeMap(WebContents* web_contents) {
218 TextInputManager* manager =
219 static_cast<WebContentsImpl*>(web_contents)->GetTextInputManager();
220 std::unordered_map<const RenderWidgetHostView*, ui::TextInputType> result;
221 for (const auto& pair : manager->text_input_state_map_)
222 result[pair.first] = pair.second.type;
223 return result;
224 }
225
226 // RenderWidgetHostViewDestructionObserver implementations.
227 RenderWidgetHostViewDestructionObserver::
228 ~RenderWidgetHostViewDestructionObserver() {}
229
230 // static
231 std::unique_ptr<RenderWidgetHostViewDestructionObserver>
232 RenderWidgetHostViewDestructionObserver::Create(RenderWidgetHostView* view) {
233 return base::WrapUnique(new RenderWidgetHostViewBaseObserverImpl(
234 static_cast<RenderWidgetHostViewBase*>(view)));
235 }
236
237 ui::TextInputType GetTextInputTypeFromWebContents(WebContents* web_contents) {
238 const TextInputState* state = static_cast<WebContentsImpl*>(web_contents)
239 ->GetTextInputManager()
240 ->GetTextInputState();
241 return !!state ? state->type : ui::TEXT_INPUT_TYPE_NONE;
242 }
243
244 RenderWidgetHostView* GetActiveViewFromWebContents(WebContents* web_contents) {
245 return static_cast<WebContentsImpl*>(web_contents)
246 ->GetTextInputManager()
247 ->GetActiveView();
248 }
249
250 // TextInputStateSender implementations.
251 TextInputStateSender::TextInputStateSender(RenderWidgetHostView* view)
252 : text_input_state_(new TextInputState()),
253 view_(static_cast<RenderWidgetHostViewBase*>(view)) {}
254
255 TextInputStateSender::~TextInputStateSender() {}
256
257 void TextInputStateSender::Send() {
258 if (view_)
259 view_->TextInputStateChanged(*text_input_state_);
260 }
261
262 void TextInputStateSender::SetFromCurrentState() {
263 if (view_) {
264 *text_input_state_ =
265 *RenderWidgetHostImpl::From(view_->GetRenderWidgetHost())
266 ->delegate()
267 ->GetTextInputManager()
268 ->GetTextInputState();
269 }
270 }
271
272 void TextInputStateSender::SetType(ui::TextInputType type) {
273 text_input_state_->type = type;
274 }
275
276 void TextInputStateSender::SetMode(ui::TextInputMode mode) {
277 text_input_state_->mode = mode;
278 }
279
280 void TextInputStateSender::SetFlags(int flags) {
281 text_input_state_->flags = flags;
282 }
283
284 void TextInputStateSender::SetCanComposeInline(bool can_compose_inline) {
285 text_input_state_->can_compose_inline = can_compose_inline;
286 }
287
288 void TextInputStateSender::SetShowImeIfNeeded(bool show_ime_if_needed) {
289 text_input_state_->show_ime_if_needed = show_ime_if_needed;
290 }
291
292 void TextInputStateSender::SetIsNonImeChange(bool is_non_ime_change) {
293 text_input_state_->is_non_ime_change = is_non_ime_change;
294 }
295
296 // TestInputMethodObserver implementations.
297 TestInputMethodObserver::~TestInputMethodObserver() {}
298
299 // static
300 std::unique_ptr<TestInputMethodObserver> TestInputMethodObserver::Create(
301 WebContents* web_contents) {
302 std::unique_ptr<TestInputMethodObserver> observer;
303
304 #ifdef USE_AURA
305 RenderWidgetHostViewAura* view = static_cast<RenderWidgetHostViewAura*>(
306 web_contents->GetRenderWidgetHostView());
307 observer.reset(new InputMethodObserverAura(view->GetInputMethod()));
308 #endif
309 return observer;
310 }
311
312 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698