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/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() override { |
| 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 // TextInputManager::Observer implementations. |
| 61 void OnUpdateTextInputStateCalled(TextInputManager* text_input_manager, |
| 62 RenderWidgetHostViewBase* updated_view, |
| 63 bool did_change_state) override { |
| 64 if (text_input_manager_ != text_input_manager) |
| 65 return; |
| 66 text_input_state_changed_ = did_change_state; |
| 67 updated_view_ = updated_view; |
| 68 update_text_input_state_callback_.Run(tester_); |
| 69 } |
| 70 |
| 71 // WebContentsObserver implementation. |
| 72 void WebContentsDestroyed() override { text_input_manager_ = nullptr; } |
| 73 |
| 74 private: |
| 75 TextInputManagerTester* tester_; |
| 76 TextInputManager* text_input_manager_; |
| 77 RenderWidgetHostViewBase* updated_view_; |
| 78 bool text_input_state_changed_; |
| 79 TextInputManagerTester::Callback update_text_input_state_callback_; |
| 80 |
| 81 DISALLOW_COPY_AND_ASSIGN(InternalObserver); |
| 82 }; |
| 83 |
| 84 // This class observes the lifetime of a RenderWidgetHostView. An instance of |
| 85 // this class is used in TestRenderWidgetHostViewDestructionObserver to expose |
| 86 // the required observer API for testing outside of content/. |
| 87 class TestRenderWidgetHostViewDestructionObserver::InternalObserver |
| 88 : public RenderWidgetHostViewBaseObserver { |
| 89 public: |
| 90 InternalObserver(RenderWidgetHostViewBase* view) |
| 91 : view_(view), destroyed_(false) { |
| 92 view->AddObserver(this); |
| 93 } |
| 94 |
| 95 ~InternalObserver() override { |
| 96 if (view_) |
| 97 view_->RemoveObserver(this); |
| 98 } |
| 99 |
| 100 void Wait() { |
| 101 if (destroyed_) |
| 102 return; |
| 103 message_loop_runner_ = new content::MessageLoopRunner(); |
| 104 message_loop_runner_->Run(); |
| 105 } |
| 106 |
| 107 private: |
| 108 void OnRenderWidgetHostViewBaseDestroyed( |
| 109 RenderWidgetHostViewBase* view) override { |
| 110 DCHECK_EQ(view_, view); |
| 111 destroyed_ = true; |
| 112 view->RemoveObserver(this); |
| 113 view_ = nullptr; |
| 114 if (message_loop_runner_) |
| 115 message_loop_runner_->Quit(); |
| 116 } |
| 117 |
| 118 RenderWidgetHostViewBase* view_; |
| 119 bool destroyed_; |
| 120 scoped_refptr<MessageLoopRunner> message_loop_runner_; |
| 121 |
| 122 DISALLOW_COPY_AND_ASSIGN(InternalObserver); |
| 123 }; |
| 124 |
| 125 #ifdef USE_AURA |
| 126 class InputMethodObserverAura : public TestInputMethodObserver, |
| 127 public ui::InputMethodObserver { |
| 128 public: |
| 129 explicit InputMethodObserverAura(ui::InputMethod* input_method) |
| 130 : input_method_(input_method), text_input_client_(nullptr) { |
| 131 input_method_->AddObserver(this); |
| 132 } |
| 133 |
| 134 ~InputMethodObserverAura() override { |
| 135 if (input_method_) |
| 136 input_method_->RemoveObserver(this); |
| 137 } |
| 138 |
| 139 // TestInputMethodObserver implementations. |
| 140 ui::TextInputType GetTextInputTypeFromClient() override { |
| 141 if (text_input_client_) |
| 142 return text_input_client_->GetTextInputType(); |
| 143 |
| 144 return ui::TEXT_INPUT_TYPE_NONE; |
| 145 } |
| 146 |
| 147 void SetOnTextInputTypeChangedCallback( |
| 148 const base::Closure& callback) override { |
| 149 on_text_input_type_changed_callback_ = callback; |
| 150 } |
| 151 |
| 152 void SetOnShowImeIfNeededCallback(const base::Closure& callback) override { |
| 153 on_show_ime_if_needed_callback_ = callback; |
| 154 } |
| 155 |
| 156 private: |
| 157 // ui::InputMethodObserver implementations. |
| 158 void OnTextInputTypeChanged(const ui::TextInputClient* client) override { |
| 159 text_input_client_ = client; |
| 160 on_text_input_type_changed_callback_.Run(); |
| 161 } |
| 162 |
| 163 void OnFocus() override {} |
| 164 void OnBlur() override {} |
| 165 void OnCaretBoundsChanged(const ui::TextInputClient* client) override {} |
| 166 void OnTextInputStateChanged(const ui::TextInputClient* client) override {} |
| 167 void OnInputMethodDestroyed(const ui::InputMethod* input_method) override {} |
| 168 |
| 169 void OnShowImeIfNeeded() override { on_show_ime_if_needed_callback_.Run(); } |
| 170 |
| 171 ui::InputMethod* input_method_; |
| 172 const ui::TextInputClient* text_input_client_; |
| 173 base::Closure on_text_input_type_changed_callback_; |
| 174 base::Closure on_show_ime_if_needed_callback_; |
| 175 |
| 176 DISALLOW_COPY_AND_ASSIGN(InputMethodObserverAura); |
| 177 }; |
| 178 #endif |
| 179 |
| 180 ui::TextInputType GetTextInputTypeFromWebContents(WebContents* web_contents) { |
| 181 const TextInputState* state = static_cast<WebContentsImpl*>(web_contents) |
| 182 ->GetTextInputManager() |
| 183 ->GetTextInputState(); |
| 184 return !!state ? state->type : ui::TEXT_INPUT_TYPE_NONE; |
| 185 } |
| 186 |
| 187 bool GetTextInputTypeForView(WebContents* web_contents, |
| 188 RenderWidgetHostView* view, |
| 189 ui::TextInputType* type) { |
| 190 TextInputManager* manager = |
| 191 static_cast<WebContentsImpl*>(web_contents)->GetTextInputManager(); |
| 192 |
| 193 RenderWidgetHostViewBase* view_base = |
| 194 static_cast<RenderWidgetHostViewBase*>(view); |
| 195 if (!manager || !manager->IsRegistered(view_base)) |
| 196 return false; |
| 197 |
| 198 *type = manager->text_input_state_map_[view_base].type; |
| 199 return true; |
| 200 } |
| 201 |
| 202 RenderWidgetHostView* GetActiveViewFromWebContents(WebContents* web_contents) { |
| 203 return static_cast<WebContentsImpl*>(web_contents) |
| 204 ->GetTextInputManager() |
| 205 ->GetActiveView(); |
| 206 } |
| 207 |
| 208 TextInputManagerTester::TextInputManagerTester(WebContents* web_contents) |
| 209 : observer_(new InternalObserver(web_contents, this)) {} |
| 210 |
| 211 TextInputManagerTester::~TextInputManagerTester() {} |
| 212 |
| 213 void TextInputManagerTester::SetUpdateTextInputStateCalledCallback( |
| 214 const Callback& callback) { |
| 215 observer_->set_update_text_input_state_called_callback(callback); |
| 216 } |
| 217 |
| 218 bool TextInputManagerTester::GetTextInputType(ui::TextInputType* type) { |
| 219 DCHECK(observer_->text_input_manager()); |
| 220 const TextInputState* state = |
| 221 observer_->text_input_manager()->GetTextInputState(); |
| 222 if (!state) |
| 223 return false; |
| 224 *type = state->type; |
| 225 return true; |
| 226 } |
| 227 |
| 228 bool TextInputManagerTester::GetTextInputValue(std::string* value) { |
| 229 DCHECK(observer_->text_input_manager()); |
| 230 const TextInputState* state = |
| 231 observer_->text_input_manager()->GetTextInputState(); |
| 232 if (!state) |
| 233 return false; |
| 234 *value = state->value; |
| 235 return true; |
| 236 } |
| 237 |
| 238 const RenderWidgetHostView* TextInputManagerTester::GetActiveView() { |
| 239 DCHECK(observer_->text_input_manager()); |
| 240 return observer_->text_input_manager()->GetActiveView(); |
| 241 } |
| 242 |
| 243 const RenderWidgetHostView* TextInputManagerTester::GetUpdatedView() { |
| 244 return observer_->GetUpdatedView(); |
| 245 } |
| 246 |
| 247 bool TextInputManagerTester::IsTextInputStateChanged() { |
| 248 return observer_->text_input_state_changed(); |
| 249 } |
| 250 |
| 251 TestRenderWidgetHostViewDestructionObserver:: |
| 252 TestRenderWidgetHostViewDestructionObserver(RenderWidgetHostView* view) |
| 253 : observer_( |
| 254 new InternalObserver(static_cast<RenderWidgetHostViewBase*>(view))) {} |
| 255 |
| 256 TestRenderWidgetHostViewDestructionObserver:: |
| 257 ~TestRenderWidgetHostViewDestructionObserver() {} |
| 258 |
| 259 void TestRenderWidgetHostViewDestructionObserver::Wait() { |
| 260 observer_->Wait(); |
| 261 } |
| 262 |
| 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 |
OLD | NEW |