Chromium Code Reviews| 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 { | |
|
kenrb
2016/05/12 16:16:37
Why are you using multiple inheritance, rather tha
EhsanK
2016/05/13 16:00:56
I really wanted to avoid this. But I think as I un
| |
| 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 bool GetTextInputValue(std::string& value) const override { | |
| 52 const content::TextInputState* state = | |
| 53 text_input_manager_->GetTextInputState(); | |
| 54 if (!state) | |
| 55 return false; | |
| 56 value = state->value; | |
| 57 return true; | |
| 58 } | |
| 59 | |
| 60 ui::TextInputType GetTextInputType() const override { | |
| 61 const content::TextInputState* state = | |
| 62 text_input_manager_->GetTextInputState(); | |
| 63 return !!state ? state->type : ui::TEXT_INPUT_TYPE_NONE; | |
| 64 } | |
| 65 | |
| 66 const RenderWidgetHostView* GetActiveView() const override { | |
| 67 return text_input_manager_->GetActiveView(); | |
| 68 } | |
| 69 | |
| 70 const RenderWidgetHostView* GetUpdatedView() const override { | |
| 71 return updated_view_; | |
| 72 } | |
| 73 | |
| 74 bool IsTextInputStateChanged() const override { return changed_; } | |
| 75 | |
| 76 private: | |
| 77 // TextInputManager::Observer implementations. | |
| 78 void OnTextInputStateUpdated(TextInputManager* text_input_manager, | |
| 79 RenderWidgetHostViewBase* updated_view, | |
| 80 bool changed) override { | |
| 81 if (text_input_manager_ != text_input_manager) | |
| 82 return; | |
| 83 changed_ = changed; | |
| 84 updated_view_ = updated_view; | |
| 85 if (update_callback_) | |
| 86 update_callback_->Run(this); | |
| 87 } | |
| 88 | |
| 89 content::TextInputManager* text_input_manager_; | |
| 90 content::RenderWidgetHostViewBase* updated_view_; | |
| 91 bool changed_; | |
| 92 std::unique_ptr<content::TestTextInputManagerObserver::Callback> | |
| 93 update_callback_; | |
| 94 | |
| 95 DISALLOW_COPY_AND_ASSIGN(TextInputManagerObserver); | |
| 96 }; | |
| 97 | |
| 98 class RenderWidgetHostViewBaseObserverImpl | |
| 99 : public RenderWidgetHostViewBaseObserver, | |
| 100 public RenderWidgetHostViewDestructionObserver { | |
| 101 public: | |
| 102 RenderWidgetHostViewBaseObserverImpl(RenderWidgetHostViewBase* view) | |
| 103 : view_(view), destroyed_(false) { | |
| 104 view->AddObserver(this); | |
| 105 } | |
| 106 | |
| 107 void Wait() override { | |
| 108 if (destroyed_) | |
| 109 return; | |
| 110 message_loop_runner_ = new content::MessageLoopRunner(); | |
| 111 message_loop_runner_->Run(); | |
| 112 } | |
| 113 | |
| 114 private: | |
| 115 void OnRenderWidgetHostViewBaseDestroyed( | |
| 116 RenderWidgetHostViewBase* view) override { | |
| 117 DCHECK(view_ == view); | |
| 118 destroyed_ = true; | |
| 119 view->RemoveObserver(this); | |
| 120 if (message_loop_runner_ && message_loop_runner_->loop_running()) | |
|
kenrb
2016/05/12 16:16:37
I don't think the loop_running() check does anythi
EhsanK
2016/05/13 16:00:56
I was trying to avoid calling Quit twice. But I gu
| |
| 121 message_loop_runner_->Quit(); | |
| 122 } | |
| 123 | |
| 124 RenderWidgetHostView* view_; | |
| 125 bool destroyed_; | |
| 126 scoped_refptr<MessageLoopRunner> message_loop_runner_; | |
| 127 | |
| 128 DISALLOW_COPY_AND_ASSIGN(RenderWidgetHostViewBaseObserverImpl); | |
| 129 }; | |
| 130 | |
| 131 #ifdef USE_AURA | |
| 132 class InputMethodObserverAura : public TestInputMethodObserver, | |
| 133 public ui::InputMethodObserver { | |
| 134 public: | |
| 135 explicit InputMethodObserverAura(ui::InputMethod* input_method) | |
| 136 : input_method_(input_method), text_input_client_(nullptr) { | |
| 137 input_method_->AddObserver(this); | |
| 138 } | |
| 139 ~InputMethodObserverAura() override { | |
| 140 if (input_method_) | |
| 141 input_method_->RemoveObserver(this); | |
| 142 } | |
| 143 | |
| 144 // TestInputMethodObserver implementations. | |
| 145 ui::TextInputType GetTextInputTypeFromClient() const override { | |
| 146 if (text_input_client_) | |
| 147 return text_input_client_->GetTextInputType(); | |
| 148 | |
| 149 return ui::TEXT_INPUT_TYPE_NONE; | |
| 150 } | |
| 151 void SetOnTextInputTypeChangedCallback( | |
| 152 const base::Closure& callback) override { | |
| 153 on_text_input_type_changed_callback_.reset(new base::Closure(callback)); | |
| 154 } | |
| 155 | |
| 156 void SetOnShowImeIfNeededCallback(const base::Closure& callback) override { | |
| 157 on_show_ime_if_needed_callback_.reset(new base::Closure(callback)); | |
| 158 } | |
| 159 | |
| 160 private: | |
| 161 // ui::InputMethodObserver implementations. | |
| 162 void OnTextInputTypeChanged(const ui::TextInputClient* client) override { | |
| 163 text_input_client_ = client; | |
| 164 if (on_text_input_type_changed_callback_) | |
| 165 on_text_input_type_changed_callback_->Run(); | |
| 166 } | |
| 167 | |
| 168 void OnFocus() override {} | |
| 169 void OnBlur() override {} | |
| 170 void OnCaretBoundsChanged(const ui::TextInputClient* client) override {} | |
| 171 void OnTextInputStateChanged(const ui::TextInputClient* client) override {} | |
| 172 void OnInputMethodDestroyed(const ui::InputMethod* input_method) override {} | |
| 173 | |
| 174 void OnShowImeIfNeeded() override { | |
| 175 if (on_show_ime_if_needed_callback_) | |
| 176 on_show_ime_if_needed_callback_->Run(); | |
| 177 } | |
| 178 | |
| 179 ui::InputMethod* input_method_; | |
| 180 const ui::TextInputClient* text_input_client_; | |
| 181 std::unique_ptr<base::Closure> on_text_input_type_changed_callback_; | |
| 182 std::unique_ptr<base::Closure> on_show_ime_if_needed_callback_; | |
| 183 | |
| 184 DISALLOW_COPY_AND_ASSIGN(InputMethodObserverAura); | |
| 185 }; | |
| 186 #endif | |
| 187 | |
| 188 } // namespace | |
| 189 | |
| 190 // TestTextInputManagerObserver Implementations. | |
| 191 TestTextInputManagerObserver::~TestTextInputManagerObserver() {} | |
| 192 | |
| 193 // static | |
| 194 std::unique_ptr<TestTextInputManagerObserver> | |
| 195 TestTextInputManagerObserver::Create(WebContents* web_contents) { | |
| 196 return base::WrapUnique(new TextInputManagerObserver(web_contents)); | |
| 197 } | |
| 198 | |
| 199 // static | |
| 200 std::unordered_map<const RenderWidgetHostView*, ui::TextInputType> | |
| 201 TestTextInputManagerObserver::GetTextInputTypeMap(WebContents* web_contents) { | |
| 202 TextInputManager* manager = | |
| 203 static_cast<WebContentsImpl*>(web_contents)->GetTextInputManager(); | |
| 204 std::unordered_map<const RenderWidgetHostView*, ui::TextInputType> result; | |
| 205 for (const auto& pair : manager->text_input_state_map_) | |
| 206 result[pair.first] = pair.second.type; | |
| 207 return result; | |
| 208 } | |
| 209 | |
| 210 // RenderWidgetHostViewDestructionObserver implementations. | |
| 211 RenderWidgetHostViewDestructionObserver:: | |
| 212 ~RenderWidgetHostViewDestructionObserver() {} | |
| 213 | |
| 214 // static | |
| 215 std::unique_ptr<RenderWidgetHostViewDestructionObserver> | |
| 216 RenderWidgetHostViewDestructionObserver::Create(RenderWidgetHostView* view) { | |
| 217 return base::WrapUnique(new RenderWidgetHostViewBaseObserverImpl( | |
| 218 static_cast<RenderWidgetHostViewBase*>(view))); | |
| 219 } | |
| 220 | |
| 221 ui::TextInputType GetTextInputTypeFromWebContents(WebContents* web_contents) { | |
| 222 const TextInputState* state = static_cast<WebContentsImpl*>(web_contents) | |
| 223 ->GetTextInputManager() | |
| 224 ->GetTextInputState(); | |
| 225 return !!state ? state->type : ui::TEXT_INPUT_TYPE_NONE; | |
| 226 } | |
| 227 | |
| 228 RenderWidgetHostView* GetActiveViewFromWebContents(WebContents* web_contents) { | |
| 229 return static_cast<WebContentsImpl*>(web_contents) | |
| 230 ->GetTextInputManager() | |
| 231 ->GetActiveView(); | |
| 232 } | |
| 233 | |
| 234 // TextInputStateSender implementations. | |
| 235 TextInputStateSender::TextInputStateSender(RenderWidgetHostView* view) | |
| 236 : text_input_state_(new TextInputState()), | |
| 237 view_(static_cast<RenderWidgetHostViewBase*>(view)) {} | |
| 238 | |
| 239 TextInputStateSender::~TextInputStateSender() {} | |
| 240 | |
| 241 void TextInputStateSender::Send() { | |
| 242 if (view_) | |
| 243 view_->TextInputStateChanged(*text_input_state_); | |
| 244 } | |
| 245 | |
| 246 void TextInputStateSender::SetFromCurrentState() { | |
| 247 if (view_) { | |
| 248 *text_input_state_ = | |
| 249 *RenderWidgetHostImpl::From(view_->GetRenderWidgetHost()) | |
| 250 ->delegate() | |
| 251 ->GetTextInputManager() | |
| 252 ->GetTextInputState(); | |
| 253 } | |
| 254 } | |
| 255 | |
| 256 void TextInputStateSender::SetType(ui::TextInputType type) { | |
| 257 text_input_state_->type = type; | |
| 258 } | |
| 259 | |
| 260 void TextInputStateSender::SetMode(ui::TextInputMode mode) { | |
| 261 text_input_state_->mode = mode; | |
| 262 } | |
| 263 | |
| 264 void TextInputStateSender::SetFlags(int flags) { | |
| 265 text_input_state_->flags = flags; | |
| 266 } | |
| 267 | |
| 268 void TextInputStateSender::SetCanComposeInline(bool can_compose_inline) { | |
| 269 text_input_state_->can_compose_inline = can_compose_inline; | |
| 270 } | |
| 271 | |
| 272 void TextInputStateSender::SetShowImeIfNeeded(bool show_ime_if_needed) { | |
| 273 text_input_state_->show_ime_if_needed = show_ime_if_needed; | |
| 274 } | |
| 275 | |
| 276 void TextInputStateSender::SetIsNonImeChange(bool is_non_ime_change) { | |
| 277 text_input_state_->is_non_ime_change = is_non_ime_change; | |
| 278 } | |
| 279 | |
| 280 // TestInputMethodObserver implementations. | |
| 281 TestInputMethodObserver::~TestInputMethodObserver() {} | |
| 282 | |
| 283 // static | |
| 284 std::unique_ptr<TestInputMethodObserver> TestInputMethodObserver::Create( | |
| 285 WebContents* web_contents) { | |
| 286 std::unique_ptr<TestInputMethodObserver> observer; | |
| 287 | |
| 288 #ifdef USE_AURA | |
| 289 RenderWidgetHostViewAura* view = static_cast<RenderWidgetHostViewAura*>( | |
| 290 web_contents->GetRenderWidgetHostView()); | |
| 291 observer.reset(new InputMethodObserverAura(view->GetInputMethod())); | |
| 292 #endif | |
| 293 return observer; | |
| 294 } | |
| 295 | |
| 296 } // namespace content | |
| OLD | NEW |