[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 1 | // Copyright 2014 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 | |
dcheng | 59826e3 | 2017-02-22 10:31:36 | [diff] [blame] | 5 | #include "content/shell/test_runner/text_input_controller.h" |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 6 | |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame] | 7 | #include "base/macros.h" |
dcheng | 59826e3 | 2017-02-22 10:31:36 | [diff] [blame] | 8 | #include "content/shell/test_runner/web_test_delegate.h" |
| 9 | #include "content/shell/test_runner/web_view_test_proxy.h" |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 10 | #include "gin/arguments.h" |
| 11 | #include "gin/handle.h" |
| 12 | #include "gin/object_template_builder.h" |
| 13 | #include "gin/wrappable.h" |
nzolghadr | 5d859650 | 2017-01-23 22:59:35 | [diff] [blame] | 14 | #include "third_party/WebKit/public/platform/WebCoalescedInputEvent.h" |
ekaramad | c75b1b3b3 | 2016-12-02 03:57:52 | [diff] [blame] | 15 | #include "third_party/WebKit/public/platform/WebInputEventResult.h" |
dtapuska | a64845d | 2017-01-20 21:20:45 | [diff] [blame] | 16 | #include "third_party/WebKit/public/platform/WebKeyboardEvent.h" |
ekaramad | 2daaf67 | 2016-11-10 20:29:01 | [diff] [blame] | 17 | #include "third_party/WebKit/public/web/WebFrameWidget.h" |
Ryan Landay | 9e42fd74 | 2017-08-12 01:59:11 | [diff] [blame^] | 18 | #include "third_party/WebKit/public/web/WebImeTextSpan.h" |
ekaramad | 2daaf67 | 2016-11-10 20:29:01 | [diff] [blame] | 19 | #include "third_party/WebKit/public/web/WebInputMethodController.h" |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 20 | #include "third_party/WebKit/public/web/WebKit.h" |
lukasza | 8b6d5f3 | 2016-04-22 16:56:31 | [diff] [blame] | 21 | #include "third_party/WebKit/public/web/WebLocalFrame.h" |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 22 | #include "third_party/WebKit/public/web/WebRange.h" |
| 23 | #include "third_party/WebKit/public/web/WebView.h" |
changwan | 9bdc18f0 | 2015-11-20 02:43:52 | [diff] [blame] | 24 | #include "third_party/skia/include/core/SkColor.h" |
dtapuska | 899ac22 | 2017-01-03 18:09:16 | [diff] [blame] | 25 | #include "ui/events/base_event_utils.h" |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 26 | #include "v8/include/v8.h" |
| 27 | |
jochen | f5f3175 | 2015-06-03 12:06:34 | [diff] [blame] | 28 | namespace test_runner { |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 29 | |
| 30 | class TextInputControllerBindings |
| 31 | : public gin::Wrappable<TextInputControllerBindings> { |
| 32 | public: |
| 33 | static gin::WrapperInfo kWrapperInfo; |
| 34 | |
| 35 | static void Install(base::WeakPtr<TextInputController> controller, |
lukasza | 8b6d5f3 | 2016-04-22 16:56:31 | [diff] [blame] | 36 | blink::WebLocalFrame* frame); |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 37 | |
| 38 | private: |
| 39 | explicit TextInputControllerBindings( |
| 40 | base::WeakPtr<TextInputController> controller); |
dcheng | e933b3e | 2014-10-21 11:44:09 | [diff] [blame] | 41 | ~TextInputControllerBindings() override; |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 42 | |
| 43 | // gin::Wrappable: |
dcheng | e933b3e | 2014-10-21 11:44:09 | [diff] [blame] | 44 | gin::ObjectTemplateBuilder GetObjectTemplateBuilder( |
anand.ratn | 449f39a4 | 2014-10-06 13:45:57 | [diff] [blame] | 45 | v8::Isolate* isolate) override; |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 46 | |
| 47 | void InsertText(const std::string& text); |
| 48 | void UnmarkText(); |
| 49 | void DoCommand(const std::string& text); |
| 50 | void SetMarkedText(const std::string& text, int start, int length); |
| 51 | bool HasMarkedText(); |
| 52 | std::vector<int> MarkedRange(); |
| 53 | std::vector<int> SelectedRange(); |
| 54 | std::vector<int> FirstRectForCharacterRange(unsigned location, |
| 55 | unsigned length); |
| 56 | void SetComposition(const std::string& text); |
ekaramad | 2daaf67 | 2016-11-10 20:29:01 | [diff] [blame] | 57 | void ForceTextInputStateUpdate(); |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 58 | |
| 59 | base::WeakPtr<TextInputController> controller_; |
| 60 | |
| 61 | DISALLOW_COPY_AND_ASSIGN(TextInputControllerBindings); |
| 62 | }; |
| 63 | |
| 64 | gin::WrapperInfo TextInputControllerBindings::kWrapperInfo = { |
| 65 | gin::kEmbedderNativeGin}; |
| 66 | |
| 67 | // static |
| 68 | void TextInputControllerBindings::Install( |
| 69 | base::WeakPtr<TextInputController> controller, |
lukasza | 8b6d5f3 | 2016-04-22 16:56:31 | [diff] [blame] | 70 | blink::WebLocalFrame* frame) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 71 | v8::Isolate* isolate = blink::MainThreadIsolate(); |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 72 | v8::HandleScope handle_scope(isolate); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 73 | v8::Local<v8::Context> context = frame->MainWorldScriptContext(); |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 74 | if (context.IsEmpty()) |
| 75 | return; |
| 76 | |
| 77 | v8::Context::Scope context_scope(context); |
| 78 | |
| 79 | gin::Handle<TextInputControllerBindings> bindings = |
| 80 | gin::CreateHandle(isolate, new TextInputControllerBindings(controller)); |
[email protected] | ad4d203 | 2014-04-28 13:50:59 | [diff] [blame] | 81 | if (bindings.IsEmpty()) |
| 82 | return; |
deepak.s | 750d68f | 2015-04-30 07:32:41 | [diff] [blame] | 83 | v8::Local<v8::Object> global = context->Global(); |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 84 | global->Set(gin::StringToV8(isolate, "textInputController"), bindings.ToV8()); |
| 85 | } |
| 86 | |
| 87 | TextInputControllerBindings::TextInputControllerBindings( |
| 88 | base::WeakPtr<TextInputController> controller) |
| 89 | : controller_(controller) {} |
| 90 | |
| 91 | TextInputControllerBindings::~TextInputControllerBindings() {} |
| 92 | |
| 93 | gin::ObjectTemplateBuilder |
| 94 | TextInputControllerBindings::GetObjectTemplateBuilder(v8::Isolate* isolate) { |
| 95 | return gin::Wrappable<TextInputControllerBindings>::GetObjectTemplateBuilder( |
| 96 | isolate) |
| 97 | .SetMethod("insertText", &TextInputControllerBindings::InsertText) |
| 98 | .SetMethod("unmarkText", &TextInputControllerBindings::UnmarkText) |
| 99 | .SetMethod("doCommand", &TextInputControllerBindings::DoCommand) |
| 100 | .SetMethod("setMarkedText", &TextInputControllerBindings::SetMarkedText) |
| 101 | .SetMethod("hasMarkedText", &TextInputControllerBindings::HasMarkedText) |
| 102 | .SetMethod("markedRange", &TextInputControllerBindings::MarkedRange) |
| 103 | .SetMethod("selectedRange", &TextInputControllerBindings::SelectedRange) |
| 104 | .SetMethod("firstRectForCharacterRange", |
| 105 | &TextInputControllerBindings::FirstRectForCharacterRange) |
ekaramad | 2daaf67 | 2016-11-10 20:29:01 | [diff] [blame] | 106 | .SetMethod("setComposition", &TextInputControllerBindings::SetComposition) |
| 107 | .SetMethod("forceTextInputStateUpdate", |
| 108 | &TextInputControllerBindings::ForceTextInputStateUpdate); |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | void TextInputControllerBindings::InsertText(const std::string& text) { |
| 112 | if (controller_) |
| 113 | controller_->InsertText(text); |
| 114 | } |
| 115 | |
| 116 | void TextInputControllerBindings::UnmarkText() { |
| 117 | if (controller_) |
| 118 | controller_->UnmarkText(); |
| 119 | } |
| 120 | |
| 121 | void TextInputControllerBindings::DoCommand(const std::string& text) { |
| 122 | if (controller_) |
| 123 | controller_->DoCommand(text); |
| 124 | } |
| 125 | |
| 126 | void TextInputControllerBindings::SetMarkedText(const std::string& text, |
| 127 | int start, |
| 128 | int length) { |
| 129 | if (controller_) |
| 130 | controller_->SetMarkedText(text, start, length); |
| 131 | } |
| 132 | |
| 133 | bool TextInputControllerBindings::HasMarkedText() { |
| 134 | return controller_ ? controller_->HasMarkedText() : false; |
| 135 | } |
| 136 | |
| 137 | std::vector<int> TextInputControllerBindings::MarkedRange() { |
| 138 | return controller_ ? controller_->MarkedRange() : std::vector<int>(); |
| 139 | } |
| 140 | |
| 141 | std::vector<int> TextInputControllerBindings::SelectedRange() { |
| 142 | return controller_ ? controller_->SelectedRange() : std::vector<int>(); |
| 143 | } |
| 144 | |
| 145 | std::vector<int> TextInputControllerBindings::FirstRectForCharacterRange( |
| 146 | unsigned location, |
| 147 | unsigned length) { |
| 148 | return controller_ ? controller_->FirstRectForCharacterRange(location, length) |
| 149 | : std::vector<int>(); |
| 150 | } |
| 151 | |
| 152 | void TextInputControllerBindings::SetComposition(const std::string& text) { |
| 153 | if (controller_) |
| 154 | controller_->SetComposition(text); |
| 155 | } |
ekaramad | 2daaf67 | 2016-11-10 20:29:01 | [diff] [blame] | 156 | void TextInputControllerBindings::ForceTextInputStateUpdate() { |
| 157 | if (controller_) |
| 158 | controller_->ForceTextInputStateUpdate(); |
| 159 | } |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 160 | // TextInputController --------------------------------------------------------- |
| 161 | |
lfg | 05e4137 | 2016-07-22 15:38:10 | [diff] [blame] | 162 | TextInputController::TextInputController( |
| 163 | WebViewTestProxyBase* web_view_test_proxy_base) |
| 164 | : web_view_test_proxy_base_(web_view_test_proxy_base), |
| 165 | weak_factory_(this) {} |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 166 | |
| 167 | TextInputController::~TextInputController() {} |
| 168 | |
lukasza | 8b6d5f3 | 2016-04-22 16:56:31 | [diff] [blame] | 169 | void TextInputController::Install(blink::WebLocalFrame* frame) { |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 170 | TextInputControllerBindings::Install(weak_factory_.GetWeakPtr(), frame); |
| 171 | } |
| 172 | |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 173 | void TextInputController::InsertText(const std::string& text) { |
ekaramad | c75b1b3b3 | 2016-12-02 03:57:52 | [diff] [blame] | 174 | if (auto* controller = GetInputMethodController()) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 175 | controller->CommitText(blink::WebString::FromUTF8(text), |
Ryan Landay | 9e42fd74 | 2017-08-12 01:59:11 | [diff] [blame^] | 176 | std::vector<blink::WebImeTextSpan>(), |
ekaramad | ce32ef9f | 2017-02-09 17:33:56 | [diff] [blame] | 177 | blink::WebRange(), 0); |
ekaramad | c75b1b3b3 | 2016-12-02 03:57:52 | [diff] [blame] | 178 | } |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | void TextInputController::UnmarkText() { |
ekaramad | c75b1b3b3 | 2016-12-02 03:57:52 | [diff] [blame] | 182 | if (auto* controller = GetInputMethodController()) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 183 | controller->FinishComposingText( |
| 184 | blink::WebInputMethodController::kKeepSelection); |
ekaramad | c75b1b3b3 | 2016-12-02 03:57:52 | [diff] [blame] | 185 | } |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | void TextInputController::DoCommand(const std::string& text) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 189 | if (view()->MainFrame()) { |
| 190 | if (!view()->MainFrame()->ToWebLocalFrame()) { |
yabinh | 8204efc | 2016-07-08 13:58:57 | [diff] [blame] | 191 | CHECK(false) << "This function cannot be called if the main frame is not" |
| 192 | "a local frame."; |
| 193 | } |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 194 | view()->MainFrame()->ToWebLocalFrame()->ExecuteCommand( |
| 195 | blink::WebString::FromUTF8(text)); |
yabinh | 8204efc | 2016-07-08 13:58:57 | [diff] [blame] | 196 | } |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | void TextInputController::SetMarkedText(const std::string& text, |
| 200 | int start, |
| 201 | int length) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 202 | blink::WebString web_text(blink::WebString::FromUTF8(text)); |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 203 | |
| 204 | // Split underline into up to 3 elements (before, selection, and after). |
Ryan Landay | 9e42fd74 | 2017-08-12 01:59:11 | [diff] [blame^] | 205 | std::vector<blink::WebImeTextSpan> ime_text_spans; |
| 206 | blink::WebImeTextSpan ime_text_span; |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 207 | if (!start) { |
Ryan Landay | 9e42fd74 | 2017-08-12 01:59:11 | [diff] [blame^] | 208 | ime_text_span.end_offset = length; |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 209 | } else { |
Ryan Landay | 9e42fd74 | 2017-08-12 01:59:11 | [diff] [blame^] | 210 | ime_text_span.end_offset = start; |
| 211 | ime_text_spans.push_back(ime_text_span); |
| 212 | ime_text_span.start_offset = start; |
| 213 | ime_text_span.end_offset = start + length; |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 214 | } |
Ryan Landay | 9e42fd74 | 2017-08-12 01:59:11 | [diff] [blame^] | 215 | ime_text_span.thick = true; |
| 216 | ime_text_spans.push_back(ime_text_span); |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 217 | if (start + length < static_cast<int>(web_text.length())) { |
Ryan Landay | 9e42fd74 | 2017-08-12 01:59:11 | [diff] [blame^] | 218 | ime_text_span.start_offset = ime_text_span.end_offset; |
| 219 | ime_text_span.end_offset = web_text.length(); |
| 220 | ime_text_span.thick = false; |
| 221 | ime_text_spans.push_back(ime_text_span); |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 222 | } |
| 223 | |
ekaramad | c75b1b3b3 | 2016-12-02 03:57:52 | [diff] [blame] | 224 | if (auto* controller = GetInputMethodController()) { |
Ryan Landay | 9e42fd74 | 2017-08-12 01:59:11 | [diff] [blame^] | 225 | controller->SetComposition(web_text, ime_text_spans, blink::WebRange(), |
| 226 | start, start + length); |
ekaramad | c75b1b3b3 | 2016-12-02 03:57:52 | [diff] [blame] | 227 | } |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | bool TextInputController::HasMarkedText() { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 231 | if (!view()->MainFrame()) |
yabinh | 8204efc | 2016-07-08 13:58:57 | [diff] [blame] | 232 | return false; |
| 233 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 234 | if (!view()->MainFrame()->ToWebLocalFrame()) { |
yabinh | 8204efc | 2016-07-08 13:58:57 | [diff] [blame] | 235 | CHECK(false) << "This function cannot be called if the main frame is not" |
| 236 | "a local frame."; |
| 237 | } |
| 238 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 239 | return view()->MainFrame()->ToWebLocalFrame()->HasMarkedText(); |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | std::vector<int> TextInputController::MarkedRange() { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 243 | if (!view()->MainFrame()) |
[email protected] | 6f939712 | 2014-02-25 09:30:50 | [diff] [blame] | 244 | return std::vector<int>(); |
| 245 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 246 | if (!view()->MainFrame()->ToWebLocalFrame()) { |
yabinh | 8204efc | 2016-07-08 13:58:57 | [diff] [blame] | 247 | CHECK(false) << "This function cannot be called if the main frame is not" |
| 248 | "a local frame."; |
| 249 | } |
| 250 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 251 | blink::WebRange range = view()->MainFrame()->ToWebLocalFrame()->MarkedRange(); |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 252 | std::vector<int> int_array(2); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 253 | int_array[0] = range.StartOffset(); |
| 254 | int_array[1] = range.EndOffset(); |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 255 | |
| 256 | return int_array; |
| 257 | } |
| 258 | |
| 259 | std::vector<int> TextInputController::SelectedRange() { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 260 | if (!view()->MainFrame()) |
[email protected] | 6f939712 | 2014-02-25 09:30:50 | [diff] [blame] | 261 | return std::vector<int>(); |
| 262 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 263 | if (!view()->MainFrame()->ToWebLocalFrame()) { |
yabinh | 8204efc | 2016-07-08 13:58:57 | [diff] [blame] | 264 | CHECK(false) << "This function cannot be called if the main frame is not" |
| 265 | "a local frame."; |
| 266 | } |
| 267 | |
| 268 | blink::WebRange range = |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 269 | view()->MainFrame()->ToWebLocalFrame()->SelectionRange(); |
| 270 | if (range.IsNull()) |
yosin | 702c78a3 | 2016-06-13 08:39:13 | [diff] [blame] | 271 | return std::vector<int>(); |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 272 | std::vector<int> int_array(2); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 273 | int_array[0] = range.StartOffset(); |
| 274 | int_array[1] = range.EndOffset(); |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 275 | |
| 276 | return int_array; |
| 277 | } |
| 278 | |
| 279 | std::vector<int> TextInputController::FirstRectForCharacterRange( |
| 280 | unsigned location, |
| 281 | unsigned length) { |
| 282 | blink::WebRect rect; |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 283 | if (!view()->FocusedFrame() || |
| 284 | !view()->FocusedFrame()->FirstRectForCharacterRange(location, length, |
lukasza | 8b6d5f3 | 2016-04-22 16:56:31 | [diff] [blame] | 285 | rect)) { |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 286 | return std::vector<int>(); |
[email protected] | 6f939712 | 2014-02-25 09:30:50 | [diff] [blame] | 287 | } |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 288 | |
| 289 | std::vector<int> int_array(4); |
| 290 | int_array[0] = rect.x; |
| 291 | int_array[1] = rect.y; |
| 292 | int_array[2] = rect.width; |
| 293 | int_array[3] = rect.height; |
| 294 | |
| 295 | return int_array; |
| 296 | } |
| 297 | |
| 298 | void TextInputController::SetComposition(const std::string& text) { |
| 299 | // Sends a keydown event with key code = 0xE5 to emulate input method |
| 300 | // behavior. |
dtapuska | 899ac22 | 2017-01-03 18:09:16 | [diff] [blame] | 301 | blink::WebKeyboardEvent key_down( |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 302 | blink::WebInputEvent::kRawKeyDown, blink::WebInputEvent::kNoModifiers, |
dtapuska | 899ac22 | 2017-01-03 18:09:16 | [diff] [blame] | 303 | ui::EventTimeStampToSeconds(ui::EventTimeForNow())); |
| 304 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 305 | key_down.windows_key_code = 0xE5; // VKEY_PROCESSKEY |
| 306 | view()->HandleInputEvent(blink::WebCoalescedInputEvent(key_down)); |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 307 | |
yabinh | dd6d1b6 | 2016-08-23 07:09:02 | [diff] [blame] | 308 | // The value returned by std::string::length() may not correspond to the |
| 309 | // actual number of encoded characters in sequences of multi-byte or |
| 310 | // variable-length characters. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 311 | blink::WebString newText = blink::WebString::FromUTF8(text); |
yabinh | dd6d1b6 | 2016-08-23 07:09:02 | [diff] [blame] | 312 | size_t textLength = newText.length(); |
| 313 | |
Ryan Landay | 9e42fd74 | 2017-08-12 01:59:11 | [diff] [blame^] | 314 | std::vector<blink::WebImeTextSpan> ime_text_spans; |
| 315 | ime_text_spans.push_back(blink::WebImeTextSpan(0, textLength, SK_ColorBLACK, |
| 316 | false, SK_ColorTRANSPARENT)); |
ekaramad | c75b1b3b3 | 2016-12-02 03:57:52 | [diff] [blame] | 317 | if (auto* controller = GetInputMethodController()) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 318 | controller->SetComposition( |
Ryan Landay | 9e42fd74 | 2017-08-12 01:59:11 | [diff] [blame^] | 319 | newText, blink::WebVector<blink::WebImeTextSpan>(ime_text_spans), |
ekaramad | ce32ef9f | 2017-02-09 17:33:56 | [diff] [blame] | 320 | blink::WebRange(), textLength, textLength); |
ekaramad | c75b1b3b3 | 2016-12-02 03:57:52 | [diff] [blame] | 321 | } |
lukasza | 8b6d5f3 | 2016-04-22 16:56:31 | [diff] [blame] | 322 | } |
| 323 | |
ekaramad | 2daaf67 | 2016-11-10 20:29:01 | [diff] [blame] | 324 | void TextInputController::ForceTextInputStateUpdate() { |
lukasza | bedb4b2 | 2017-06-23 00:00:13 | [diff] [blame] | 325 | // TODO(lukasza): Finish adding OOPIF support to the layout tests harness. |
Daniel Cheng | 3c82943 | 2017-06-19 03:42:31 | [diff] [blame] | 326 | CHECK(view()->MainFrame()->IsWebLocalFrame()) |
| 327 | << "WebView does not have a local main frame and" |
| 328 | " cannot handle input method controller tasks."; |
ekaramad | 2daaf67 | 2016-11-10 20:29:01 | [diff] [blame] | 329 | web_view_test_proxy_base_->delegate()->ForceTextInputStateUpdate( |
Daniel Cheng | 3c82943 | 2017-06-19 03:42:31 | [diff] [blame] | 330 | view()->MainFrame()->ToWebLocalFrame()); |
ekaramad | 2daaf67 | 2016-11-10 20:29:01 | [diff] [blame] | 331 | } |
| 332 | |
lukasza | 8b6d5f3 | 2016-04-22 16:56:31 | [diff] [blame] | 333 | blink::WebView* TextInputController::view() { |
lfg | 05e4137 | 2016-07-22 15:38:10 | [diff] [blame] | 334 | return web_view_test_proxy_base_->web_view(); |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 335 | } |
| 336 | |
ekaramad | c75b1b3b3 | 2016-12-02 03:57:52 | [diff] [blame] | 337 | blink::WebInputMethodController* |
| 338 | TextInputController::GetInputMethodController() { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 339 | if (!view()->MainFrame()) |
ekaramad | c75b1b3b3 | 2016-12-02 03:57:52 | [diff] [blame] | 340 | return nullptr; |
| 341 | |
lukasza | bedb4b2 | 2017-06-23 00:00:13 | [diff] [blame] | 342 | // TODO(lukasza): Finish adding OOPIF support to the layout tests harness. |
Daniel Cheng | 3c82943 | 2017-06-19 03:42:31 | [diff] [blame] | 343 | CHECK(view()->MainFrame()->IsWebLocalFrame()) |
| 344 | << "WebView does not have a local main frame and" |
| 345 | " cannot handle input method controller tasks."; |
| 346 | |
| 347 | return view() |
| 348 | ->MainFrame() |
| 349 | ->ToWebLocalFrame() |
| 350 | ->FrameWidget() |
| 351 | ->GetActiveWebInputMethodController(); |
ekaramad | 2daaf67 | 2016-11-10 20:29:01 | [diff] [blame] | 352 | } |
| 353 | |
jochen | f5f3175 | 2015-06-03 12:06:34 | [diff] [blame] | 354 | } // namespace test_runner |