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, |
| 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 SetDestructionCallback( |
| 52 const content::TestTextInputManagerObserver::Callback& callback) |
| 53 override { |
| 54 destruction_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(TextInputManager* text_input_manager, |
| 86 RenderWidgetHostViewBase* updated_view, |
| 87 bool changed) override { |
| 88 if (text_input_manager_ != text_input_manager) |
| 89 return; |
| 90 changed_ = changed; |
| 91 updated_view_ = updated_view; |
| 92 if (update_callback_) |
| 93 update_callback_->Run(this); |
| 94 } |
| 95 void OnTextInputManagerDestroyed( |
| 96 TextInputManager* text_input_manager) override { |
| 97 if (text_input_manager_ != text_input_manager) |
| 98 return; |
| 99 |
| 100 text_input_manager_->RemoveObserver(this); |
| 101 |
| 102 if (destruction_callback_) |
| 103 destruction_callback_->Run(this); |
| 104 } |
| 105 |
| 106 content::TextInputManager* text_input_manager_; |
| 107 content::RenderWidgetHostViewBase* updated_view_; |
| 108 bool changed_; |
| 109 std::unique_ptr<content::TestTextInputManagerObserver::Callback> |
| 110 update_callback_; |
| 111 std::unique_ptr<content::TestTextInputManagerObserver::Callback> |
| 112 destruction_callback_; |
| 113 |
| 114 DISALLOW_COPY_AND_ASSIGN(TextInputManagerObserver); |
| 115 }; |
| 116 |
| 117 class RenderWidgetHostViewBaseObserverImpl |
| 118 : public RenderWidgetHostViewBaseObserver, |
| 119 public RenderWidgetHostViewDestructionObserver { |
| 120 public: |
| 121 RenderWidgetHostViewBaseObserverImpl(RenderWidgetHostViewBase* view) |
| 122 : view_(view), destroyed_(false) { |
| 123 view->AddObserver(this); |
| 124 } |
| 125 |
| 126 void Wait() override { |
| 127 if (destroyed_) |
| 128 return; |
| 129 message_loop_runner_ = new content::MessageLoopRunner(); |
| 130 message_loop_runner_->Run(); |
| 131 } |
| 132 |
| 133 private: |
| 134 void OnRenderWidgetHostViewBaseDestroyed( |
| 135 RenderWidgetHostViewBase* view) override { |
| 136 DCHECK(view_ == view); |
| 137 destroyed_ = true; |
| 138 view->RemoveObserver(this); |
| 139 if (message_loop_runner_ && message_loop_runner_->loop_running()) |
| 140 message_loop_runner_->Quit(); |
| 141 } |
| 142 |
| 143 RenderWidgetHostView* view_; |
| 144 bool destroyed_; |
| 145 scoped_refptr<MessageLoopRunner> message_loop_runner_; |
| 146 |
| 147 DISALLOW_COPY_AND_ASSIGN(RenderWidgetHostViewBaseObserverImpl); |
| 148 }; |
| 149 |
| 150 #ifdef USE_AURA |
| 151 class InputMethodObserverAura : public TestInputMethodObserver, |
| 152 public ui::InputMethodObserver { |
| 153 public: |
| 154 explicit InputMethodObserverAura(ui::InputMethod* input_method) |
| 155 : input_method_(input_method), text_input_client_(nullptr) { |
| 156 input_method_->AddObserver(this); |
| 157 } |
| 158 ~InputMethodObserverAura() override { |
| 159 if (input_method_) |
| 160 input_method_->RemoveObserver(this); |
| 161 } |
| 162 |
| 163 // TestInputMethodObserver implementations. |
| 164 ui::TextInputType GetTextInputTypeFromClient() const override { |
| 165 if (text_input_client_) |
| 166 return text_input_client_->GetTextInputType(); |
| 167 |
| 168 return ui::TEXT_INPUT_TYPE_NONE; |
| 169 } |
| 170 void SetOnTextInputTypeChangedCallback( |
| 171 const base::Closure& callback) override { |
| 172 on_text_input_type_changed_callback_.reset(new base::Closure(callback)); |
| 173 } |
| 174 |
| 175 void SetOnShowImeIfNeededCallback(const base::Closure& callback) override { |
| 176 on_show_ime_if_needed_callback_.reset(new base::Closure(callback)); |
| 177 } |
| 178 |
| 179 private: |
| 180 // ui::InputMethodObserver implementations. |
| 181 void OnTextInputTypeChanged(const ui::TextInputClient* client) override { |
| 182 text_input_client_ = client; |
| 183 if (on_text_input_type_changed_callback_) |
| 184 on_text_input_type_changed_callback_->Run(); |
| 185 } |
| 186 |
| 187 void OnFocus() override {} |
| 188 void OnBlur() override {} |
| 189 void OnCaretBoundsChanged(const ui::TextInputClient* client) override {} |
| 190 void OnTextInputStateChanged(const ui::TextInputClient* client) override {} |
| 191 void OnInputMethodDestroyed(const ui::InputMethod* input_method) override {} |
| 192 |
| 193 void OnShowImeIfNeeded() override { |
| 194 if (on_show_ime_if_needed_callback_) |
| 195 on_show_ime_if_needed_callback_->Run(); |
| 196 } |
| 197 |
| 198 ui::InputMethod* input_method_; |
| 199 const ui::TextInputClient* text_input_client_; |
| 200 std::unique_ptr<base::Closure> on_text_input_type_changed_callback_; |
| 201 std::unique_ptr<base::Closure> on_show_ime_if_needed_callback_; |
| 202 |
| 203 DISALLOW_COPY_AND_ASSIGN(InputMethodObserverAura); |
| 204 }; |
| 205 #endif |
| 206 |
| 207 } // namespace |
| 208 |
| 209 // TestTextInputManagerObserver Implementations. |
| 210 TestTextInputManagerObserver::~TestTextInputManagerObserver() {} |
| 211 |
| 212 // static |
| 213 std::unique_ptr<TestTextInputManagerObserver> |
| 214 TestTextInputManagerObserver::Create(WebContents* web_contents) { |
| 215 return std::unique_ptr<TestTextInputManagerObserver>( |
| 216 static_cast<TestTextInputManagerObserver*>( |
| 217 new TextInputManagerObserver(web_contents))); |
| 218 } |
| 219 |
| 220 // static |
| 221 std::unordered_map<RenderWidgetHostView*, ui::TextInputType> |
| 222 TestTextInputManagerObserver::GetTextInputTypeMap(WebContents* web_contents) { |
| 223 TextInputManager* manager = |
| 224 static_cast<WebContentsImpl*>(web_contents)->GetTextInputManager(); |
| 225 std::unordered_map<RenderWidgetHostView*, ui::TextInputType> result; |
| 226 for (const auto& pair : manager->text_input_state_map_) |
| 227 result[pair.first] = pair.second.type; |
| 228 return result; |
| 229 } |
| 230 |
| 231 // RenderWidgetHostViewDestructionObserver implementations. |
| 232 RenderWidgetHostViewDestructionObserver:: |
| 233 ~RenderWidgetHostViewDestructionObserver() {} |
| 234 |
| 235 // static |
| 236 std::unique_ptr<RenderWidgetHostViewDestructionObserver> |
| 237 RenderWidgetHostViewDestructionObserver::Create(RenderWidgetHostView* view) { |
| 238 return std::unique_ptr<RenderWidgetHostViewDestructionObserver>( |
| 239 static_cast<RenderWidgetHostViewDestructionObserver*>( |
| 240 new RenderWidgetHostViewBaseObserverImpl( |
| 241 static_cast<RenderWidgetHostViewBase*>(view)))); |
| 242 } |
| 243 |
| 244 ui::TextInputType GetTextInputTypeFromWebContents(WebContents* web_contents) { |
| 245 const TextInputState* state = static_cast<WebContentsImpl*>(web_contents) |
| 246 ->GetTextInputManager() |
| 247 ->GetTextInputState(); |
| 248 return !!state ? state->type : ui::TEXT_INPUT_TYPE_NONE; |
| 249 } |
| 250 |
| 251 RenderWidgetHostView* GetActiveViewFromWebContents(WebContents* web_contents) { |
| 252 return static_cast<WebContentsImpl*>(web_contents) |
| 253 ->GetTextInputManager() |
| 254 ->GetActiveView(); |
| 255 } |
| 256 |
| 257 // TextInputStateSender implementations. |
| 258 TextInputStateSender::TextInputStateSender(RenderWidgetHostView* view) |
| 259 : text_input_state_(new TextInputState()), |
| 260 view_(static_cast<RenderWidgetHostViewBase*>(view)) {} |
| 261 |
| 262 TextInputStateSender::~TextInputStateSender() {} |
| 263 |
| 264 void TextInputStateSender::Send() { |
| 265 if (view_) |
| 266 view_->TextInputStateChanged(*text_input_state_); |
| 267 } |
| 268 |
| 269 void TextInputStateSender::SetFromCurrentState() { |
| 270 if (view_) { |
| 271 *text_input_state_ = |
| 272 *RenderWidgetHostImpl::From(view_->GetRenderWidgetHost()) |
| 273 ->delegate() |
| 274 ->GetTextInputManager() |
| 275 ->GetTextInputState(); |
| 276 } |
| 277 } |
| 278 |
| 279 void TextInputStateSender::SetType(ui::TextInputType type) { |
| 280 text_input_state_->type = type; |
| 281 } |
| 282 |
| 283 void TextInputStateSender::SetMode(ui::TextInputMode mode) { |
| 284 text_input_state_->mode = mode; |
| 285 } |
| 286 |
| 287 void TextInputStateSender::SetFlags(int flags) { |
| 288 text_input_state_->flags = flags; |
| 289 } |
| 290 |
| 291 void TextInputStateSender::SetCanComposeInline(bool can_compose_inline) { |
| 292 text_input_state_->can_compose_inline = can_compose_inline; |
| 293 } |
| 294 |
| 295 void TextInputStateSender::SetShowImeIfNeeded(bool show_ime_if_needed) { |
| 296 text_input_state_->show_ime_if_needed = show_ime_if_needed; |
| 297 } |
| 298 |
| 299 void TextInputStateSender::SetIsNonImeChange(bool is_non_ime_change) { |
| 300 text_input_state_->is_non_ime_change = is_non_ime_change; |
| 301 } |
| 302 |
| 303 // TestInputMethodObserver implementations. |
| 304 TestInputMethodObserver::~TestInputMethodObserver() {} |
| 305 |
| 306 // static |
| 307 std::unique_ptr<TestInputMethodObserver> TestInputMethodObserver::Create( |
| 308 WebContents* web_contents) { |
| 309 std::unique_ptr<TestInputMethodObserver> observer; |
| 310 |
| 311 #ifdef USE_AURA |
| 312 RenderWidgetHostViewAura* view = static_cast<RenderWidgetHostViewAura*>( |
| 313 web_contents->GetRenderWidgetHostView()); |
| 314 observer.reset(static_cast<TestInputMethodObserver*>( |
| 315 new InputMethodObserverAura(view->GetInputMethod()))); |
| 316 #endif |
| 317 return observer; |
| 318 } |
| 319 |
| 320 } // namespace content |
OLD | NEW |