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

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

Powered by Google App Engine
This is Rietveld 408576698