| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/public/test/text_input_test_utils.h" | 5 #include "content/public/test/text_input_test_utils.h" |
| 6 | 6 |
| 7 #include <unordered_set> | 7 #include <unordered_set> |
| 8 | 8 |
| 9 #include "content/browser/renderer_host/render_widget_host_impl.h" | 9 #include "content/browser/renderer_host/render_widget_host_impl.h" |
| 10 #include "content/browser/renderer_host/render_widget_host_view_aura.h" | 10 #include "content/browser/renderer_host/render_widget_host_view_aura.h" |
| 11 #include "content/browser/renderer_host/render_widget_host_view_base.h" | 11 #include "content/browser/renderer_host/render_widget_host_view_base.h" |
| 12 #include "content/browser/renderer_host/render_widget_host_view_base_observer.h" | 12 #include "content/browser/renderer_host/render_widget_host_view_base_observer.h" |
| 13 #include "content/browser/renderer_host/text_input_manager.h" | 13 #include "content/browser/renderer_host/text_input_manager.h" |
| 14 #include "content/browser/web_contents/web_contents_impl.h" | 14 #include "content/browser/web_contents/web_contents_impl.h" |
| 15 #include "content/common/text_input_state.h" | 15 #include "content/common/text_input_state.h" |
| 16 #include "content/common/view_messages.h" |
| 16 #include "content/public/browser/render_widget_host_view.h" | 17 #include "content/public/browser/render_widget_host_view.h" |
| 17 #include "content/public/browser/web_contents.h" | 18 #include "content/public/browser/web_contents.h" |
| 18 #include "content/public/browser/web_contents_observer.h" | 19 #include "content/public/browser/web_contents_observer.h" |
| 19 #include "content/public/test/test_utils.h" | 20 #include "content/public/test/test_utils.h" |
| 20 #include "ui/base/ime/input_method.h" | 21 #include "ui/base/ime/input_method.h" |
| 21 #include "ui/base/ime/input_method_observer.h" | 22 #include "ui/base/ime/input_method_observer.h" |
| 22 | 23 |
| 23 namespace ui { | 24 namespace ui { |
| 24 class TextInputClient; | 25 class TextInputClient; |
| 25 } | 26 } |
| 26 | 27 |
| 27 namespace content { | 28 namespace content { |
| 28 | 29 |
| 30 namespace { |
| 31 void DefaultTextInputManagerObserverCallback() {} |
| 32 TextInputManagerTester::Callback default_text_input_manager_observer_callback = |
| 33 base::Bind(&DefaultTextInputManagerObserverCallback); |
| 34 } // namespace |
| 35 |
| 29 // This class is an observer of TextInputManager associated with the provided | 36 // This class is an observer of TextInputManager associated with the provided |
| 30 // WebContents. An instance of this class is used in TextInputManagerTester to | 37 // WebContents. An instance of this class is used in TextInputManagerTester to |
| 31 // expose the required API for testing outside of content/. | 38 // expose the required API for testing outside of content/. |
| 32 class TextInputManagerTester::InternalObserver | 39 class TextInputManagerTester::InternalObserver |
| 33 : public TextInputManager::Observer, | 40 : public TextInputManager::Observer, |
| 34 public WebContentsObserver { | 41 public WebContentsObserver { |
| 35 public: | 42 public: |
| 36 InternalObserver(WebContents* web_contents, TextInputManagerTester* tester) | 43 InternalObserver(WebContents* web_contents, TextInputManagerTester* tester) |
| 37 : WebContentsObserver(web_contents), | 44 : WebContentsObserver(web_contents), |
| 38 tester_(tester), | |
| 39 updated_view_(nullptr), | 45 updated_view_(nullptr), |
| 40 text_input_state_changed_(false) { | 46 text_input_state_changed_(false), |
| 47 update_text_input_state_callback_( |
| 48 default_text_input_manager_observer_callback), |
| 49 on_selection_bounds_changed_callback_( |
| 50 default_text_input_manager_observer_callback) { |
| 41 text_input_manager_ = | 51 text_input_manager_ = |
| 42 static_cast<WebContentsImpl*>(web_contents)->GetTextInputManager(); | 52 static_cast<WebContentsImpl*>(web_contents)->GetTextInputManager(); |
| 43 DCHECK(!!text_input_manager_); | 53 DCHECK(!!text_input_manager_); |
| 44 text_input_manager_->AddObserver(this); | 54 text_input_manager_->AddObserver(this); |
| 45 } | 55 } |
| 46 | 56 |
| 47 ~InternalObserver() override { | 57 ~InternalObserver() override { |
| 48 if (text_input_manager_) | 58 if (text_input_manager_) |
| 49 text_input_manager_->RemoveObserver(this); | 59 text_input_manager_->RemoveObserver(this); |
| 50 } | 60 } |
| 51 | 61 |
| 52 void set_update_text_input_state_called_callback( | 62 void set_update_text_input_state_called_callback( |
| 53 const TextInputManagerTester::Callback& callback) { | 63 const TextInputManagerTester::Callback& callback) { |
| 54 update_text_input_state_callback_ = callback; | 64 update_text_input_state_callback_ = callback; |
| 55 } | 65 } |
| 56 | 66 |
| 67 void set_on_selection_bounds_changed_callback( |
| 68 const TextInputManagerTester::Callback& callback) { |
| 69 on_selection_bounds_changed_callback_ = callback; |
| 70 } |
| 71 |
| 57 const RenderWidgetHostView* GetUpdatedView() const { return updated_view_; } | 72 const RenderWidgetHostView* GetUpdatedView() const { return updated_view_; } |
| 58 | 73 |
| 59 bool text_input_state_changed() const { return text_input_state_changed_; } | 74 bool text_input_state_changed() const { return text_input_state_changed_; } |
| 60 | 75 |
| 61 TextInputManager* text_input_manager() const { return text_input_manager_; } | 76 TextInputManager* text_input_manager() const { return text_input_manager_; } |
| 62 | 77 |
| 63 // TextInputManager::Observer implementations. | 78 // TextInputManager::Observer implementations. |
| 64 void OnUpdateTextInputStateCalled(TextInputManager* text_input_manager, | 79 void OnUpdateTextInputStateCalled(TextInputManager* text_input_manager, |
| 65 RenderWidgetHostViewBase* updated_view, | 80 RenderWidgetHostViewBase* updated_view, |
| 66 bool did_change_state) override { | 81 bool did_change_state) override { |
| 67 if (text_input_manager_ != text_input_manager) | 82 if (text_input_manager_ != text_input_manager) |
| 68 return; | 83 return; |
| 69 text_input_state_changed_ = did_change_state; | 84 text_input_state_changed_ = did_change_state; |
| 70 updated_view_ = updated_view; | 85 updated_view_ = updated_view; |
| 71 update_text_input_state_callback_.Run(tester_); | 86 update_text_input_state_callback_.Run(); |
| 87 } |
| 88 |
| 89 void OnSelectionBoundsChanged( |
| 90 TextInputManager* text_input_manager_, |
| 91 RenderWidgetHostViewBase* updated_view) override { |
| 92 updated_view_ = updated_view; |
| 93 on_selection_bounds_changed_callback_.Run(); |
| 72 } | 94 } |
| 73 | 95 |
| 74 // WebContentsObserver implementation. | 96 // WebContentsObserver implementation. |
| 75 void WebContentsDestroyed() override { text_input_manager_ = nullptr; } | 97 void WebContentsDestroyed() override { text_input_manager_ = nullptr; } |
| 76 | 98 |
| 77 private: | 99 private: |
| 78 TextInputManagerTester* tester_; | |
| 79 TextInputManager* text_input_manager_; | 100 TextInputManager* text_input_manager_; |
| 80 RenderWidgetHostViewBase* updated_view_; | 101 RenderWidgetHostViewBase* updated_view_; |
| 81 bool text_input_state_changed_; | 102 bool text_input_state_changed_; |
| 82 TextInputManagerTester::Callback update_text_input_state_callback_; | 103 TextInputManagerTester::Callback update_text_input_state_callback_; |
| 104 TextInputManagerTester::Callback on_selection_bounds_changed_callback_; |
| 83 | 105 |
| 84 DISALLOW_COPY_AND_ASSIGN(InternalObserver); | 106 DISALLOW_COPY_AND_ASSIGN(InternalObserver); |
| 85 }; | 107 }; |
| 86 | 108 |
| 87 // This class observes the lifetime of a RenderWidgetHostView. An instance of | 109 // This class observes the lifetime of a RenderWidgetHostView. An instance of |
| 88 // this class is used in TestRenderWidgetHostViewDestructionObserver to expose | 110 // this class is used in TestRenderWidgetHostViewDestructionObserver to expose |
| 89 // the required observer API for testing outside of content/. | 111 // the required observer API for testing outside of content/. |
| 90 class TestRenderWidgetHostViewDestructionObserver::InternalObserver | 112 class TestRenderWidgetHostViewDestructionObserver::InternalObserver |
| 91 : public RenderWidgetHostViewBaseObserver { | 113 : public RenderWidgetHostViewBaseObserver { |
| 92 public: | 114 public: |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 TextInputManagerTester::TextInputManagerTester(WebContents* web_contents) | 241 TextInputManagerTester::TextInputManagerTester(WebContents* web_contents) |
| 220 : observer_(new InternalObserver(web_contents, this)) {} | 242 : observer_(new InternalObserver(web_contents, this)) {} |
| 221 | 243 |
| 222 TextInputManagerTester::~TextInputManagerTester() {} | 244 TextInputManagerTester::~TextInputManagerTester() {} |
| 223 | 245 |
| 224 void TextInputManagerTester::SetUpdateTextInputStateCalledCallback( | 246 void TextInputManagerTester::SetUpdateTextInputStateCalledCallback( |
| 225 const Callback& callback) { | 247 const Callback& callback) { |
| 226 observer_->set_update_text_input_state_called_callback(callback); | 248 observer_->set_update_text_input_state_called_callback(callback); |
| 227 } | 249 } |
| 228 | 250 |
| 251 void TextInputManagerTester::SetOnSelectionBoundsChangedCallback( |
| 252 const Callback& callback) { |
| 253 observer_->set_on_selection_bounds_changed_callback(callback); |
| 254 } |
| 255 |
| 229 bool TextInputManagerTester::GetTextInputType(ui::TextInputType* type) { | 256 bool TextInputManagerTester::GetTextInputType(ui::TextInputType* type) { |
| 230 DCHECK(observer_->text_input_manager()); | 257 DCHECK(observer_->text_input_manager()); |
| 231 const TextInputState* state = | 258 const TextInputState* state = |
| 232 observer_->text_input_manager()->GetTextInputState(); | 259 observer_->text_input_manager()->GetTextInputState(); |
| 233 if (!state) | 260 if (!state) |
| 234 return false; | 261 return false; |
| 235 *type = state->type; | 262 *type = state->type; |
| 236 return true; | 263 return true; |
| 237 } | 264 } |
| 238 | 265 |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 327 | 354 |
| 328 #ifdef USE_AURA | 355 #ifdef USE_AURA |
| 329 RenderWidgetHostViewAura* view = static_cast<RenderWidgetHostViewAura*>( | 356 RenderWidgetHostViewAura* view = static_cast<RenderWidgetHostViewAura*>( |
| 330 web_contents->GetRenderWidgetHostView()); | 357 web_contents->GetRenderWidgetHostView()); |
| 331 observer.reset(new InputMethodObserverAura(view->GetInputMethod())); | 358 observer.reset(new InputMethodObserverAura(view->GetInputMethod())); |
| 332 #endif | 359 #endif |
| 333 return observer; | 360 return observer; |
| 334 } | 361 } |
| 335 | 362 |
| 336 } // namespace content | 363 } // namespace content |
| OLD | NEW |