Chromium Code Reviews| 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 |
| 29 // This class is an observer of TextInputManager associated with the provided | 30 // This class is an observer of TextInputManager associated with the provided |
| 30 // WebContents. An instance of this class is used in TextInputManagerTester to | 31 // WebContents. An instance of this class is used in TextInputManagerTester to |
| 31 // expose the required API for testing outside of content/. | 32 // expose the required API for testing outside of content/. |
| 32 class TextInputManagerTester::InternalObserver | 33 class TextInputManagerTester::InternalObserver |
| 33 : public TextInputManager::Observer, | 34 : public TextInputManager::Observer, |
| 34 public WebContentsObserver { | 35 public WebContentsObserver { |
| 35 public: | 36 public: |
| 36 InternalObserver(WebContents* web_contents, TextInputManagerTester* tester) | 37 InternalObserver(WebContents* web_contents, TextInputManagerTester* tester) |
| 37 : WebContentsObserver(web_contents), | 38 : WebContentsObserver(web_contents), |
| 38 tester_(tester), | |
| 39 updated_view_(nullptr), | 39 updated_view_(nullptr), |
| 40 text_input_state_changed_(false) { | 40 text_input_state_changed_(false) { |
| 41 text_input_manager_ = | 41 text_input_manager_ = |
| 42 static_cast<WebContentsImpl*>(web_contents)->GetTextInputManager(); | 42 static_cast<WebContentsImpl*>(web_contents)->GetTextInputManager(); |
| 43 DCHECK(!!text_input_manager_); | 43 DCHECK(!!text_input_manager_); |
| 44 text_input_manager_->AddObserver(this); | 44 text_input_manager_->AddObserver(this); |
| 45 } | 45 } |
| 46 | 46 |
| 47 ~InternalObserver() override { | 47 ~InternalObserver() override { |
| 48 if (text_input_manager_) | 48 if (text_input_manager_) |
| 49 text_input_manager_->RemoveObserver(this); | 49 text_input_manager_->RemoveObserver(this); |
| 50 } | 50 } |
| 51 | 51 |
| 52 void set_update_text_input_state_called_callback( | 52 void set_update_text_input_state_called_callback( |
| 53 const TextInputManagerTester::Callback& callback) { | 53 const TextInputManagerTester::Callback& callback) { |
| 54 update_text_input_state_callback_ = callback; | 54 update_text_input_state_callback_.reset( |
| 55 new TextInputManagerTester::Callback(callback)); | |
| 56 } | |
| 57 | |
| 58 void set_on_selection_bounds_changed_callback( | |
| 59 const TextInputManagerTester::Callback& callback) { | |
| 60 on_selection_bounds_changed_callback_.reset( | |
| 61 new TextInputManagerTester::Callback(callback)); | |
| 55 } | 62 } |
| 56 | 63 |
| 57 const RenderWidgetHostView* GetUpdatedView() const { return updated_view_; } | 64 const RenderWidgetHostView* GetUpdatedView() const { return updated_view_; } |
| 58 | 65 |
| 59 bool text_input_state_changed() const { return text_input_state_changed_; } | 66 bool text_input_state_changed() const { return text_input_state_changed_; } |
| 60 | 67 |
| 61 TextInputManager* text_input_manager() const { return text_input_manager_; } | 68 TextInputManager* text_input_manager() const { return text_input_manager_; } |
| 62 | 69 |
| 63 // TextInputManager::Observer implementations. | 70 // TextInputManager::Observer implementations. |
| 64 void OnUpdateTextInputStateCalled(TextInputManager* text_input_manager, | 71 void OnUpdateTextInputStateCalled(TextInputManager* text_input_manager, |
| 65 RenderWidgetHostViewBase* updated_view, | 72 RenderWidgetHostViewBase* updated_view, |
| 66 bool did_change_state) override { | 73 bool did_change_state) override { |
| 67 if (text_input_manager_ != text_input_manager) | 74 if (text_input_manager_ != text_input_manager) |
| 68 return; | 75 return; |
| 69 text_input_state_changed_ = did_change_state; | 76 text_input_state_changed_ = did_change_state; |
| 70 updated_view_ = updated_view; | 77 updated_view_ = updated_view; |
| 71 update_text_input_state_callback_.Run(tester_); | 78 if (update_text_input_state_callback_) |
| 79 update_text_input_state_callback_->Run(); | |
| 80 } | |
| 81 | |
| 82 void OnSelectionBoundsChanged( | |
| 83 TextInputManager* text_input_manager_, | |
| 84 RenderWidgetHostViewBase* updated_view) override { | |
| 85 updated_view_ = updated_view; | |
| 86 if (on_selection_bounds_changed_callback_) | |
| 87 on_selection_bounds_changed_callback_->Run(); | |
| 72 } | 88 } |
| 73 | 89 |
| 74 // WebContentsObserver implementation. | 90 // WebContentsObserver implementation. |
| 75 void WebContentsDestroyed() override { text_input_manager_ = nullptr; } | 91 void WebContentsDestroyed() override { text_input_manager_ = nullptr; } |
| 76 | 92 |
| 77 private: | 93 private: |
| 78 TextInputManagerTester* tester_; | |
| 79 TextInputManager* text_input_manager_; | 94 TextInputManager* text_input_manager_; |
| 80 RenderWidgetHostViewBase* updated_view_; | 95 RenderWidgetHostViewBase* updated_view_; |
| 81 bool text_input_state_changed_; | 96 bool text_input_state_changed_; |
| 82 TextInputManagerTester::Callback update_text_input_state_callback_; | 97 std::unique_ptr<TextInputManagerTester::Callback> |
|
sky
2016/07/01 00:03:49
You shouldn't need unique_ptrs for these. Instead
EhsanK
2016/07/01 00:29:57
Thanks! This is neat.
| |
| 98 update_text_input_state_callback_; | |
| 99 std::unique_ptr<TextInputManagerTester::Callback> | |
| 100 on_selection_bounds_changed_callback_; | |
| 83 | 101 |
| 84 DISALLOW_COPY_AND_ASSIGN(InternalObserver); | 102 DISALLOW_COPY_AND_ASSIGN(InternalObserver); |
| 85 }; | 103 }; |
| 86 | 104 |
| 87 // This class observes the lifetime of a RenderWidgetHostView. An instance of | 105 // This class observes the lifetime of a RenderWidgetHostView. An instance of |
| 88 // this class is used in TestRenderWidgetHostViewDestructionObserver to expose | 106 // this class is used in TestRenderWidgetHostViewDestructionObserver to expose |
| 89 // the required observer API for testing outside of content/. | 107 // the required observer API for testing outside of content/. |
| 90 class TestRenderWidgetHostViewDestructionObserver::InternalObserver | 108 class TestRenderWidgetHostViewDestructionObserver::InternalObserver |
| 91 : public RenderWidgetHostViewBaseObserver { | 109 : public RenderWidgetHostViewBaseObserver { |
| 92 public: | 110 public: |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 219 TextInputManagerTester::TextInputManagerTester(WebContents* web_contents) | 237 TextInputManagerTester::TextInputManagerTester(WebContents* web_contents) |
| 220 : observer_(new InternalObserver(web_contents, this)) {} | 238 : observer_(new InternalObserver(web_contents, this)) {} |
| 221 | 239 |
| 222 TextInputManagerTester::~TextInputManagerTester() {} | 240 TextInputManagerTester::~TextInputManagerTester() {} |
| 223 | 241 |
| 224 void TextInputManagerTester::SetUpdateTextInputStateCalledCallback( | 242 void TextInputManagerTester::SetUpdateTextInputStateCalledCallback( |
| 225 const Callback& callback) { | 243 const Callback& callback) { |
| 226 observer_->set_update_text_input_state_called_callback(callback); | 244 observer_->set_update_text_input_state_called_callback(callback); |
| 227 } | 245 } |
| 228 | 246 |
| 247 void TextInputManagerTester::SetOnSelectionBoundsChangedCallback( | |
| 248 const Callback& callback) { | |
| 249 observer_->set_on_selection_bounds_changed_callback(callback); | |
| 250 } | |
| 251 | |
| 229 bool TextInputManagerTester::GetTextInputType(ui::TextInputType* type) { | 252 bool TextInputManagerTester::GetTextInputType(ui::TextInputType* type) { |
| 230 DCHECK(observer_->text_input_manager()); | 253 DCHECK(observer_->text_input_manager()); |
| 231 const TextInputState* state = | 254 const TextInputState* state = |
| 232 observer_->text_input_manager()->GetTextInputState(); | 255 observer_->text_input_manager()->GetTextInputState(); |
| 233 if (!state) | 256 if (!state) |
| 234 return false; | 257 return false; |
| 235 *type = state->type; | 258 *type = state->type; |
| 236 return true; | 259 return true; |
| 237 } | 260 } |
| 238 | 261 |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 327 | 350 |
| 328 #ifdef USE_AURA | 351 #ifdef USE_AURA |
| 329 RenderWidgetHostViewAura* view = static_cast<RenderWidgetHostViewAura*>( | 352 RenderWidgetHostViewAura* view = static_cast<RenderWidgetHostViewAura*>( |
| 330 web_contents->GetRenderWidgetHostView()); | 353 web_contents->GetRenderWidgetHostView()); |
| 331 observer.reset(new InputMethodObserverAura(view->GetInputMethod())); | 354 observer.reset(new InputMethodObserverAura(view->GetInputMethod())); |
| 332 #endif | 355 #endif |
| 333 return observer; | 356 return observer; |
| 334 } | 357 } |
| 335 | 358 |
| 336 } // namespace content | 359 } // namespace content |
| OLD | NEW |