Chromium Code Reviews| OLD | NEW |
|---|---|
| (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, | |
|
Charlie Reis
2016/05/18 20:46:05
Why is this a subclass of TestTextInputManagerObse
EhsanK
2016/05/24 20:42:47
The reason is that TextInputManager is not publicl
Charlie Reis
2016/05/26 06:22:03
Ah, I see. Yes, let's just have an inner class to
| |
| 30 public TextInputManager::Observer { | |
| 31 public: | |
| 32 // TestTextInputManagerObserver implementations. | |
|
Charlie Reis
2016/05/18 20:46:05
nit: Move this comment down to SetUpdateCallback,
EhsanK
2016/05/24 20:42:47
Done.
| |
| 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& | |
|
Charlie Reis
2016/05/18 20:46:05
nit: We're already in the content namespace, so yo
EhsanK
2016/05/24 20:42:47
Acknowledged.
| |
| 46 callback) override { | |
| 47 update_callback_.reset( | |
| 48 new content::TestTextInputManagerObserver::Callback(callback)); | |
|
Charlie Reis
2016/05/18 20:46:05
You shouldn't need to wrap/copy this. Just pass c
EhsanK
2016/05/24 20:42:46
Acknowledged.
| |
| 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 { | |
|
Charlie Reis
2016/05/18 20:46:05
We don't use non-const refs. This should be const
EhsanK
2016/05/24 20:42:46
Done.
| |
| 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( | |
| 97 TextInputManager* text_input_manager) override { | |
| 98 if (text_input_manager_ != text_input_manager) | |
| 99 return; | |
| 100 if (update_called_callback_) | |
| 101 update_called_callback_->Run(this); | |
| 102 } | |
| 103 | |
| 104 content::TextInputManager* text_input_manager_; | |
| 105 content::RenderWidgetHostViewBase* updated_view_; | |
| 106 bool changed_; | |
| 107 std::unique_ptr<content::TestTextInputManagerObserver::Callback> | |
| 108 update_callback_; | |
| 109 std::unique_ptr<content::TestTextInputManagerObserver::Callback> | |
| 110 update_called_callback_; | |
| 111 | |
| 112 DISALLOW_COPY_AND_ASSIGN(TextInputManagerObserver); | |
| 113 }; | |
| 114 | |
| 115 class RenderWidgetHostViewBaseObserverImpl | |
|
Charlie Reis
2016/05/18 20:46:05
I'm unclear about why this one is a subclass, too.
EhsanK
2016/05/24 20:42:46
Same reason as above. What is the best approach?
| |
| 116 : public RenderWidgetHostViewBaseObserver, | |
| 117 public RenderWidgetHostViewDestructionObserver { | |
| 118 public: | |
| 119 RenderWidgetHostViewBaseObserverImpl(RenderWidgetHostViewBase* view) | |
| 120 : view_(view), destroyed_(false) { | |
| 121 view->AddObserver(this); | |
| 122 } | |
| 123 | |
| 124 void Wait() override { | |
| 125 if (destroyed_) | |
| 126 return; | |
| 127 message_loop_runner_ = new content::MessageLoopRunner(); | |
| 128 message_loop_runner_->Run(); | |
| 129 } | |
| 130 | |
| 131 private: | |
| 132 void OnRenderWidgetHostViewBaseDestroyed( | |
| 133 RenderWidgetHostViewBase* view) override { | |
| 134 DCHECK(view_ == view); | |
|
Charlie Reis
2016/05/18 20:46:05
nit: DCHECK_EQ
EhsanK
2016/05/24 20:42:47
Acknowledged.
| |
| 135 destroyed_ = true; | |
| 136 view->RemoveObserver(this); | |
| 137 if (message_loop_runner_) | |
| 138 message_loop_runner_->Quit(); | |
| 139 } | |
| 140 | |
| 141 RenderWidgetHostView* view_; | |
| 142 bool destroyed_; | |
| 143 scoped_refptr<MessageLoopRunner> message_loop_runner_; | |
| 144 | |
| 145 DISALLOW_COPY_AND_ASSIGN(RenderWidgetHostViewBaseObserverImpl); | |
| 146 }; | |
| 147 | |
| 148 #ifdef USE_AURA | |
| 149 class InputMethodObserverAura : public TestInputMethodObserver, | |
| 150 public ui::InputMethodObserver { | |
| 151 public: | |
| 152 explicit InputMethodObserverAura(ui::InputMethod* input_method) | |
| 153 : input_method_(input_method), text_input_client_(nullptr) { | |
| 154 input_method_->AddObserver(this); | |
| 155 } | |
| 156 ~InputMethodObserverAura() override { | |
| 157 if (input_method_) | |
| 158 input_method_->RemoveObserver(this); | |
| 159 } | |
| 160 | |
| 161 // TestInputMethodObserver implementations. | |
| 162 ui::TextInputType GetTextInputTypeFromClient() const override { | |
| 163 if (text_input_client_) | |
| 164 return text_input_client_->GetTextInputType(); | |
| 165 | |
| 166 return ui::TEXT_INPUT_TYPE_NONE; | |
| 167 } | |
| 168 void SetOnTextInputTypeChangedCallback( | |
|
Charlie Reis
2016/05/18 20:46:05
nit: Let's be consistent on whether there's a blan
EhsanK
2016/05/24 20:42:46
Acknowledged.
| |
| 169 const base::Closure& callback) override { | |
| 170 on_text_input_type_changed_callback_.reset(new base::Closure(callback)); | |
|
Charlie Reis
2016/05/18 20:46:05
Again, no need to make a copy of callback.
EhsanK
2016/05/24 20:42:47
Acknowledged.
| |
| 171 } | |
| 172 | |
| 173 void SetOnShowImeIfNeededCallback(const base::Closure& callback) override { | |
| 174 on_show_ime_if_needed_callback_.reset(new base::Closure(callback)); | |
| 175 } | |
| 176 | |
| 177 private: | |
| 178 // ui::InputMethodObserver implementations. | |
| 179 void OnTextInputTypeChanged(const ui::TextInputClient* client) override { | |
| 180 text_input_client_ = client; | |
| 181 if (on_text_input_type_changed_callback_) | |
| 182 on_text_input_type_changed_callback_->Run(); | |
| 183 } | |
| 184 | |
| 185 void OnFocus() override {} | |
| 186 void OnBlur() override {} | |
| 187 void OnCaretBoundsChanged(const ui::TextInputClient* client) override {} | |
| 188 void OnTextInputStateChanged(const ui::TextInputClient* client) override {} | |
| 189 void OnInputMethodDestroyed(const ui::InputMethod* input_method) override {} | |
| 190 | |
| 191 void OnShowImeIfNeeded() override { | |
| 192 if (on_show_ime_if_needed_callback_) | |
| 193 on_show_ime_if_needed_callback_->Run(); | |
| 194 } | |
| 195 | |
| 196 ui::InputMethod* input_method_; | |
| 197 const ui::TextInputClient* text_input_client_; | |
| 198 std::unique_ptr<base::Closure> on_text_input_type_changed_callback_; | |
| 199 std::unique_ptr<base::Closure> on_show_ime_if_needed_callback_; | |
| 200 | |
| 201 DISALLOW_COPY_AND_ASSIGN(InputMethodObserverAura); | |
| 202 }; | |
| 203 #endif | |
| 204 | |
| 205 } // namespace | |
| 206 | |
| 207 // TestTextInputManagerObserver Implementations. | |
| 208 TestTextInputManagerObserver::~TestTextInputManagerObserver() {} | |
| 209 | |
| 210 // static | |
| 211 std::unique_ptr<TestTextInputManagerObserver> | |
| 212 TestTextInputManagerObserver::Create(WebContents* web_contents) { | |
| 213 return base::WrapUnique(new TextInputManagerObserver(web_contents)); | |
| 214 } | |
| 215 | |
| 216 // static | |
| 217 std::unordered_map<const RenderWidgetHostView*, ui::TextInputType> | |
| 218 TestTextInputManagerObserver::GetTextInputTypeMap(WebContents* web_contents) { | |
| 219 TextInputManager* manager = | |
| 220 static_cast<WebContentsImpl*>(web_contents)->GetTextInputManager(); | |
| 221 std::unordered_map<const RenderWidgetHostView*, ui::TextInputType> result; | |
| 222 for (const auto& pair : manager->text_input_state_map_) | |
| 223 result[pair.first] = pair.second.type; | |
|
Charlie Reis
2016/05/18 20:46:05
Is there a reason we need to make a copy of the wh
EhsanK
2016/05/24 20:42:47
The key in the internal map is RenderWidgetHostVie
Charlie Reis
2016/05/26 06:22:03
Ah, that's really subtle. Please add a comment ex
| |
| 224 return result; | |
| 225 } | |
| 226 | |
| 227 // RenderWidgetHostViewDestructionObserver implementations. | |
| 228 RenderWidgetHostViewDestructionObserver:: | |
| 229 ~RenderWidgetHostViewDestructionObserver() {} | |
| 230 | |
| 231 // static | |
| 232 std::unique_ptr<RenderWidgetHostViewDestructionObserver> | |
| 233 RenderWidgetHostViewDestructionObserver::Create(RenderWidgetHostView* view) { | |
| 234 return base::WrapUnique(new RenderWidgetHostViewBaseObserverImpl( | |
| 235 static_cast<RenderWidgetHostViewBase*>(view))); | |
| 236 } | |
| 237 | |
| 238 ui::TextInputType GetTextInputTypeFromWebContents(WebContents* web_contents) { | |
| 239 const TextInputState* state = static_cast<WebContentsImpl*>(web_contents) | |
| 240 ->GetTextInputManager() | |
| 241 ->GetTextInputState(); | |
| 242 return !!state ? state->type : ui::TEXT_INPUT_TYPE_NONE; | |
| 243 } | |
| 244 | |
| 245 RenderWidgetHostView* GetActiveViewFromWebContents(WebContents* web_contents) { | |
| 246 return static_cast<WebContentsImpl*>(web_contents) | |
| 247 ->GetTextInputManager() | |
| 248 ->GetActiveView(); | |
| 249 } | |
| 250 | |
| 251 // TextInputStateSender implementations. | |
| 252 TextInputStateSender::TextInputStateSender(RenderWidgetHostView* view) | |
| 253 : text_input_state_(new TextInputState()), | |
| 254 view_(static_cast<RenderWidgetHostViewBase*>(view)) {} | |
| 255 | |
| 256 TextInputStateSender::~TextInputStateSender() {} | |
| 257 | |
| 258 void TextInputStateSender::Send() { | |
| 259 if (view_) | |
| 260 view_->TextInputStateChanged(*text_input_state_); | |
| 261 } | |
| 262 | |
| 263 void TextInputStateSender::SetFromCurrentState() { | |
| 264 if (view_) { | |
| 265 *text_input_state_ = | |
|
Charlie Reis
2016/05/18 20:46:05
Shouldn't this be a .reset call for a unique point
EhsanK
2016/05/24 20:42:46
I think we should not.
Two reasons I can think of
Charlie Reis
2016/05/26 06:22:03
Acknowledged.
| |
| 266 *RenderWidgetHostImpl::From(view_->GetRenderWidgetHost()) | |
| 267 ->delegate() | |
| 268 ->GetTextInputManager() | |
| 269 ->GetTextInputState(); | |
| 270 } | |
| 271 } | |
| 272 | |
| 273 void TextInputStateSender::SetType(ui::TextInputType type) { | |
| 274 text_input_state_->type = type; | |
| 275 } | |
| 276 | |
| 277 void TextInputStateSender::SetMode(ui::TextInputMode mode) { | |
| 278 text_input_state_->mode = mode; | |
| 279 } | |
| 280 | |
| 281 void TextInputStateSender::SetFlags(int flags) { | |
| 282 text_input_state_->flags = flags; | |
| 283 } | |
| 284 | |
| 285 void TextInputStateSender::SetCanComposeInline(bool can_compose_inline) { | |
| 286 text_input_state_->can_compose_inline = can_compose_inline; | |
| 287 } | |
| 288 | |
| 289 void TextInputStateSender::SetShowImeIfNeeded(bool show_ime_if_needed) { | |
| 290 text_input_state_->show_ime_if_needed = show_ime_if_needed; | |
| 291 } | |
| 292 | |
| 293 void TextInputStateSender::SetIsNonImeChange(bool is_non_ime_change) { | |
| 294 text_input_state_->is_non_ime_change = is_non_ime_change; | |
| 295 } | |
| 296 | |
| 297 // TestInputMethodObserver implementations. | |
| 298 TestInputMethodObserver::~TestInputMethodObserver() {} | |
| 299 | |
| 300 // static | |
| 301 std::unique_ptr<TestInputMethodObserver> TestInputMethodObserver::Create( | |
| 302 WebContents* web_contents) { | |
| 303 std::unique_ptr<TestInputMethodObserver> observer; | |
| 304 | |
| 305 #ifdef USE_AURA | |
| 306 RenderWidgetHostViewAura* view = static_cast<RenderWidgetHostViewAura*>( | |
| 307 web_contents->GetRenderWidgetHostView()); | |
| 308 observer.reset(new InputMethodObserverAura(view->GetInputMethod())); | |
| 309 #endif | |
| 310 return observer; | |
| 311 } | |
| 312 | |
| 313 } // namespace content | |
| OLD | NEW |