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/common/view_messages.h" |
| 17 #include "content/public/browser/render_widget_host_view.h" | 17 #include "content/public/browser/render_widget_host_view.h" |
| 18 #include "content/public/browser/web_contents.h" | 18 #include "content/public/browser/web_contents.h" |
| 19 #include "content/public/browser/web_contents_observer.h" | 19 #include "content/public/browser/web_contents_observer.h" |
| 20 #include "content/public/test/test_utils.h" | 20 #include "content/public/test/test_utils.h" |
| 21 #include "ui/base/ime/composition_text.h" | |
| 21 #include "ui/base/ime/input_method.h" | 22 #include "ui/base/ime/input_method.h" |
| 22 #include "ui/base/ime/input_method_observer.h" | 23 #include "ui/base/ime/input_method_observer.h" |
| 23 | 24 |
| 24 namespace ui { | 25 namespace ui { |
| 26 | |
|
Charlie Reis
2016/07/11 23:56:19
nit: No blank line needed.
EhsanK
2016/07/12 18:31:14
Acknowledged.
| |
| 25 class TextInputClient; | 27 class TextInputClient; |
| 26 } | 28 } // namespace ui |
|
Charlie Reis
2016/07/11 23:56:19
nit: No close comment.
EhsanK
2016/07/12 18:31:14
Acknowledged.
| |
| 27 | 29 |
| 28 namespace content { | 30 namespace content { |
| 29 | 31 |
| 30 // This class is an observer of TextInputManager associated with the provided | 32 // This class is an observer of TextInputManager associated with the provided |
| 31 // WebContents. An instance of this class is used in TextInputManagerTester to | 33 // WebContents. An instance of this class is used in TextInputManagerTester to |
| 32 // expose the required API for testing outside of content/. | 34 // expose the required API for testing outside of content/. |
| 33 class TextInputManagerTester::InternalObserver | 35 class TextInputManagerTester::InternalObserver |
| 34 : public TextInputManager::Observer, | 36 : public TextInputManager::Observer, |
| 35 public WebContentsObserver { | 37 public WebContentsObserver { |
| 36 public: | 38 public: |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 51 | 53 |
| 52 void set_update_text_input_state_called_callback( | 54 void set_update_text_input_state_called_callback( |
| 53 const base::Closure& callback) { | 55 const base::Closure& callback) { |
| 54 update_text_input_state_callback_ = callback; | 56 update_text_input_state_callback_ = callback; |
| 55 } | 57 } |
| 56 | 58 |
| 57 void set_on_selection_bounds_changed_callback(const base::Closure& callback) { | 59 void set_on_selection_bounds_changed_callback(const base::Closure& callback) { |
| 58 on_selection_bounds_changed_callback_ = callback; | 60 on_selection_bounds_changed_callback_ = callback; |
| 59 } | 61 } |
| 60 | 62 |
| 63 void set_on_ime_composition_range_changed_callback( | |
| 64 const base::Closure& callback) { | |
| 65 on_ime_composition_range_changed_callback_ = callback; | |
| 66 } | |
| 67 | |
| 61 const RenderWidgetHostView* GetUpdatedView() const { return updated_view_; } | 68 const RenderWidgetHostView* GetUpdatedView() const { return updated_view_; } |
| 62 | 69 |
| 63 bool text_input_state_changed() const { return text_input_state_changed_; } | 70 bool text_input_state_changed() const { return text_input_state_changed_; } |
| 64 | 71 |
| 65 TextInputManager* text_input_manager() const { return text_input_manager_; } | 72 TextInputManager* text_input_manager() const { return text_input_manager_; } |
| 66 | 73 |
| 67 // TextInputManager::Observer implementations. | 74 // TextInputManager::Observer implementations. |
| 68 void OnUpdateTextInputStateCalled(TextInputManager* text_input_manager, | 75 void OnUpdateTextInputStateCalled(TextInputManager* text_input_manager, |
| 69 RenderWidgetHostViewBase* updated_view, | 76 RenderWidgetHostViewBase* updated_view, |
| 70 bool did_change_state) override { | 77 bool did_change_state) override { |
| 71 if (text_input_manager_ != text_input_manager) | 78 if (text_input_manager_ != text_input_manager) |
| 72 return; | 79 return; |
| 73 text_input_state_changed_ = did_change_state; | 80 text_input_state_changed_ = did_change_state; |
| 74 updated_view_ = updated_view; | 81 updated_view_ = updated_view; |
| 75 if (!update_text_input_state_callback_.is_null()) | 82 if (!update_text_input_state_callback_.is_null()) |
| 76 update_text_input_state_callback_.Run(); | 83 update_text_input_state_callback_.Run(); |
| 77 } | 84 } |
| 78 | 85 |
| 79 void OnSelectionBoundsChanged( | 86 void OnSelectionBoundsChanged( |
| 80 TextInputManager* text_input_manager_, | 87 TextInputManager* text_input_manager_, |
| 81 RenderWidgetHostViewBase* updated_view) override { | 88 RenderWidgetHostViewBase* updated_view) override { |
| 82 updated_view_ = updated_view; | 89 updated_view_ = updated_view; |
| 83 if (!on_selection_bounds_changed_callback_.is_null()) | 90 if (!on_selection_bounds_changed_callback_.is_null()) |
| 84 on_selection_bounds_changed_callback_.Run(); | 91 on_selection_bounds_changed_callback_.Run(); |
| 85 } | 92 } |
| 86 | 93 |
| 94 void OnImeCompositionRangeChanged( | |
| 95 TextInputManager* text_input_manager, | |
| 96 RenderWidgetHostViewBase* updated_view) override { | |
| 97 updated_view_ = updated_view; | |
| 98 if (!on_ime_composition_range_changed_callback_.is_null()) | |
| 99 on_ime_composition_range_changed_callback_.Run(); | |
| 100 } | |
| 101 | |
| 87 // WebContentsObserver implementation. | 102 // WebContentsObserver implementation. |
| 88 void WebContentsDestroyed() override { text_input_manager_ = nullptr; } | 103 void WebContentsDestroyed() override { text_input_manager_ = nullptr; } |
| 89 | 104 |
| 90 private: | 105 private: |
| 91 TextInputManager* text_input_manager_; | 106 TextInputManager* text_input_manager_; |
| 92 RenderWidgetHostViewBase* updated_view_; | 107 RenderWidgetHostViewBase* updated_view_; |
| 93 bool text_input_state_changed_; | 108 bool text_input_state_changed_; |
| 94 base::Closure update_text_input_state_callback_; | 109 base::Closure update_text_input_state_callback_; |
| 95 base::Closure on_selection_bounds_changed_callback_; | 110 base::Closure on_selection_bounds_changed_callback_; |
| 111 base::Closure on_ime_composition_range_changed_callback_; | |
| 96 | 112 |
| 97 DISALLOW_COPY_AND_ASSIGN(InternalObserver); | 113 DISALLOW_COPY_AND_ASSIGN(InternalObserver); |
| 98 }; | 114 }; |
| 99 | 115 |
| 100 // This class observes the lifetime of a RenderWidgetHostView. An instance of | 116 // This class observes the lifetime of a RenderWidgetHostView. An instance of |
| 101 // this class is used in TestRenderWidgetHostViewDestructionObserver to expose | 117 // this class is used in TestRenderWidgetHostViewDestructionObserver to expose |
| 102 // the required observer API for testing outside of content/. | 118 // the required observer API for testing outside of content/. |
| 103 class TestRenderWidgetHostViewDestructionObserver::InternalObserver | 119 class TestRenderWidgetHostViewDestructionObserver::InternalObserver |
| 104 : public RenderWidgetHostViewBaseObserver { | 120 : public RenderWidgetHostViewBaseObserver { |
| 105 public: | 121 public: |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 209 RenderWidgetHostViewBase* view_base = | 225 RenderWidgetHostViewBase* view_base = |
| 210 static_cast<RenderWidgetHostViewBase*>(view); | 226 static_cast<RenderWidgetHostViewBase*>(view); |
| 211 if (!manager || !manager->IsRegistered(view_base)) | 227 if (!manager || !manager->IsRegistered(view_base)) |
| 212 return false; | 228 return false; |
| 213 | 229 |
| 214 *type = manager->GetTextInputTypeForViewForTesting(view_base); | 230 *type = manager->GetTextInputTypeForViewForTesting(view_base); |
| 215 | 231 |
| 216 return true; | 232 return true; |
| 217 } | 233 } |
| 218 | 234 |
| 235 void SetCompositionTextForFocusedTextInputClient( | |
| 236 WebContents* web_contents, | |
| 237 const ui::CompositionText& composition) { | |
| 238 #if defined(USE_AURA) | |
| 239 // We need the tab's view. | |
|
Charlie Reis
2016/07/11 23:56:19
Please elaborate why this one needs to care about
EhsanK
2016/07/12 18:31:14
I wanted to make sure the cast to RenderWidgetHost
| |
| 240 WebContentsImpl* contents = static_cast<WebContentsImpl*>(web_contents); | |
| 241 while (contents->GetOuterWebContents()) | |
| 242 contents = contents->GetOuterWebContents(); | |
| 243 static_cast<RenderWidgetHostViewAura*>(contents->GetRenderWidgetHostView()) | |
| 244 ->SetCompositionText(composition); | |
| 245 return; | |
| 246 #endif | |
| 247 } | |
| 248 | |
| 219 size_t GetRegisteredViewsCountFromTextInputManager(WebContents* web_contents) { | 249 size_t GetRegisteredViewsCountFromTextInputManager(WebContents* web_contents) { |
| 220 std::unordered_set<RenderWidgetHostView*> views; | 250 std::unordered_set<RenderWidgetHostView*> views; |
| 221 TextInputManager* manager = | 251 TextInputManager* manager = |
| 222 static_cast<WebContentsImpl*>(web_contents)->GetTextInputManager(); | 252 static_cast<WebContentsImpl*>(web_contents)->GetTextInputManager(); |
| 223 return !!manager ? manager->GetRegisteredViewsCountForTesting() : 0; | 253 return !!manager ? manager->GetRegisteredViewsCountForTesting() : 0; |
| 224 } | 254 } |
| 225 | 255 |
| 226 RenderWidgetHostView* GetActiveViewFromWebContents(WebContents* web_contents) { | 256 RenderWidgetHostView* GetActiveViewFromWebContents(WebContents* web_contents) { |
| 227 return static_cast<WebContentsImpl*>(web_contents) | 257 return static_cast<WebContentsImpl*>(web_contents) |
| 228 ->GetTextInputManager() | 258 ->GetTextInputManager() |
| 229 ->active_view_for_testing(); | 259 ->active_view_for_testing(); |
| 230 } | 260 } |
| 231 | 261 |
| 232 TextInputManagerTester::TextInputManagerTester(WebContents* web_contents) | 262 TextInputManagerTester::TextInputManagerTester(WebContents* web_contents) |
| 233 : observer_(new InternalObserver(web_contents, this)) {} | 263 : observer_(new InternalObserver(web_contents, this)) {} |
| 234 | 264 |
| 235 TextInputManagerTester::~TextInputManagerTester() {} | 265 TextInputManagerTester::~TextInputManagerTester() {} |
| 236 | 266 |
| 237 void TextInputManagerTester::SetUpdateTextInputStateCalledCallback( | 267 void TextInputManagerTester::SetUpdateTextInputStateCalledCallback( |
| 238 const base::Closure& callback) { | 268 const base::Closure& callback) { |
| 239 observer_->set_update_text_input_state_called_callback(callback); | 269 observer_->set_update_text_input_state_called_callback(callback); |
| 240 } | 270 } |
| 241 | 271 |
| 242 void TextInputManagerTester::SetOnSelectionBoundsChangedCallback( | 272 void TextInputManagerTester::SetOnSelectionBoundsChangedCallback( |
| 243 const base::Closure& callback) { | 273 const base::Closure& callback) { |
| 244 observer_->set_on_selection_bounds_changed_callback(callback); | 274 observer_->set_on_selection_bounds_changed_callback(callback); |
| 245 } | 275 } |
| 246 | 276 |
| 277 void TextInputManagerTester::SetOnImeCompositionRangeChangedCallback( | |
| 278 const base::Closure& callback) { | |
| 279 observer_->set_on_ime_composition_range_changed_callback(callback); | |
| 280 } | |
| 281 | |
| 247 bool TextInputManagerTester::GetTextInputType(ui::TextInputType* type) { | 282 bool TextInputManagerTester::GetTextInputType(ui::TextInputType* type) { |
| 248 DCHECK(observer_->text_input_manager()); | 283 DCHECK(observer_->text_input_manager()); |
| 249 const TextInputState* state = | 284 const TextInputState* state = |
| 250 observer_->text_input_manager()->GetTextInputState(); | 285 observer_->text_input_manager()->GetTextInputState(); |
| 251 if (!state) | 286 if (!state) |
| 252 return false; | 287 return false; |
| 253 *type = state->type; | 288 *type = state->type; |
| 254 return true; | 289 return true; |
| 255 } | 290 } |
| 256 | 291 |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 345 | 380 |
| 346 #ifdef USE_AURA | 381 #ifdef USE_AURA |
| 347 RenderWidgetHostViewAura* view = static_cast<RenderWidgetHostViewAura*>( | 382 RenderWidgetHostViewAura* view = static_cast<RenderWidgetHostViewAura*>( |
| 348 web_contents->GetRenderWidgetHostView()); | 383 web_contents->GetRenderWidgetHostView()); |
| 349 observer.reset(new InputMethodObserverAura(view->GetInputMethod())); | 384 observer.reset(new InputMethodObserverAura(view->GetInputMethod())); |
| 350 #endif | 385 #endif |
| 351 return observer; | 386 return observer; |
| 352 } | 387 } |
| 353 | 388 |
| 354 } // namespace content | 389 } // namespace content |
| OLD | NEW |