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