[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" |
Blink Reformat | a30d423 | 2018-04-07 15:31:06 | [diff] [blame] | 14 | #include "third_party/blink/public/platform/web_coalesced_input_event.h" |
| 15 | #include "third_party/blink/public/platform/web_input_event_result.h" |
| 16 | #include "third_party/blink/public/platform/web_keyboard_event.h" |
| 17 | #include "third_party/blink/public/web/blink.h" |
| 18 | #include "third_party/blink/public/web/web_frame_widget.h" |
| 19 | #include "third_party/blink/public/web/web_ime_text_span.h" |
| 20 | #include "third_party/blink/public/web/web_input_method_controller.h" |
| 21 | #include "third_party/blink/public/web/web_local_frame.h" |
| 22 | #include "third_party/blink/public/web/web_range.h" |
| 23 | #include "third_party/blink/public/web/web_view.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(); |
Ryan Landay | 7b5dbd5 | 2018-01-11 19:05:59 | [diff] [blame] | 49 | void UnmarkAndUnselectText(); |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 50 | void DoCommand(const std::string& text); |
Ryan Landay | 9c680942 | 2018-01-17 07:04:15 | [diff] [blame] | 51 | void ExtendSelectionAndDelete(int before, int after); |
Ryan Landay | 6d163ae | 2018-01-17 06:58:06 | [diff] [blame] | 52 | void DeleteSurroundingText(int before, int after); |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 53 | void SetMarkedText(const std::string& text, int start, int length); |
Ryan Landay | d203467 | 2018-01-12 22:22:32 | [diff] [blame] | 54 | void SetMarkedTextFromExistingText(int start, int length); |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 55 | bool HasMarkedText(); |
| 56 | std::vector<int> MarkedRange(); |
| 57 | std::vector<int> SelectedRange(); |
| 58 | std::vector<int> FirstRectForCharacterRange(unsigned location, |
| 59 | unsigned length); |
| 60 | void SetComposition(const std::string& text); |
ekaramad | 2daaf67 | 2016-11-10 20:29:01 | [diff] [blame] | 61 | void ForceTextInputStateUpdate(); |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 62 | |
| 63 | base::WeakPtr<TextInputController> controller_; |
| 64 | |
| 65 | DISALLOW_COPY_AND_ASSIGN(TextInputControllerBindings); |
| 66 | }; |
| 67 | |
| 68 | gin::WrapperInfo TextInputControllerBindings::kWrapperInfo = { |
| 69 | gin::kEmbedderNativeGin}; |
| 70 | |
| 71 | // static |
| 72 | void TextInputControllerBindings::Install( |
| 73 | base::WeakPtr<TextInputController> controller, |
lukasza | 8b6d5f3 | 2016-04-22 16:56:31 | [diff] [blame] | 74 | blink::WebLocalFrame* frame) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 75 | v8::Isolate* isolate = blink::MainThreadIsolate(); |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 76 | v8::HandleScope handle_scope(isolate); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 77 | v8::Local<v8::Context> context = frame->MainWorldScriptContext(); |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 78 | if (context.IsEmpty()) |
| 79 | return; |
| 80 | |
| 81 | v8::Context::Scope context_scope(context); |
| 82 | |
| 83 | gin::Handle<TextInputControllerBindings> bindings = |
| 84 | gin::CreateHandle(isolate, new TextInputControllerBindings(controller)); |
[email protected] | ad4d203 | 2014-04-28 13:50:59 | [diff] [blame] | 85 | if (bindings.IsEmpty()) |
| 86 | return; |
deepak.s | 750d68f | 2015-04-30 07:32:41 | [diff] [blame] | 87 | v8::Local<v8::Object> global = context->Global(); |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 88 | global->Set(gin::StringToV8(isolate, "textInputController"), bindings.ToV8()); |
| 89 | } |
| 90 | |
| 91 | TextInputControllerBindings::TextInputControllerBindings( |
| 92 | base::WeakPtr<TextInputController> controller) |
| 93 | : controller_(controller) {} |
| 94 | |
| 95 | TextInputControllerBindings::~TextInputControllerBindings() {} |
| 96 | |
| 97 | gin::ObjectTemplateBuilder |
| 98 | TextInputControllerBindings::GetObjectTemplateBuilder(v8::Isolate* isolate) { |
| 99 | return gin::Wrappable<TextInputControllerBindings>::GetObjectTemplateBuilder( |
| 100 | isolate) |
| 101 | .SetMethod("insertText", &TextInputControllerBindings::InsertText) |
| 102 | .SetMethod("unmarkText", &TextInputControllerBindings::UnmarkText) |
Ryan Landay | 7b5dbd5 | 2018-01-11 19:05:59 | [diff] [blame] | 103 | .SetMethod("unmarkAndUnselectText", |
| 104 | &TextInputControllerBindings::UnmarkAndUnselectText) |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 105 | .SetMethod("doCommand", &TextInputControllerBindings::DoCommand) |
Ryan Landay | 9c680942 | 2018-01-17 07:04:15 | [diff] [blame] | 106 | .SetMethod("extendSelectionAndDelete", |
| 107 | &TextInputControllerBindings::ExtendSelectionAndDelete) |
Ryan Landay | 6d163ae | 2018-01-17 06:58:06 | [diff] [blame] | 108 | .SetMethod("deleteSurroundingText", |
| 109 | &TextInputControllerBindings::DeleteSurroundingText) |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 110 | .SetMethod("setMarkedText", &TextInputControllerBindings::SetMarkedText) |
Ryan Landay | d203467 | 2018-01-12 22:22:32 | [diff] [blame] | 111 | .SetMethod("setMarkedTextFromExistingText", |
| 112 | &TextInputControllerBindings::SetMarkedTextFromExistingText) |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 113 | .SetMethod("hasMarkedText", &TextInputControllerBindings::HasMarkedText) |
| 114 | .SetMethod("markedRange", &TextInputControllerBindings::MarkedRange) |
| 115 | .SetMethod("selectedRange", &TextInputControllerBindings::SelectedRange) |
| 116 | .SetMethod("firstRectForCharacterRange", |
| 117 | &TextInputControllerBindings::FirstRectForCharacterRange) |
ekaramad | 2daaf67 | 2016-11-10 20:29:01 | [diff] [blame] | 118 | .SetMethod("setComposition", &TextInputControllerBindings::SetComposition) |
| 119 | .SetMethod("forceTextInputStateUpdate", |
| 120 | &TextInputControllerBindings::ForceTextInputStateUpdate); |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | void TextInputControllerBindings::InsertText(const std::string& text) { |
| 124 | if (controller_) |
| 125 | controller_->InsertText(text); |
| 126 | } |
| 127 | |
| 128 | void TextInputControllerBindings::UnmarkText() { |
| 129 | if (controller_) |
| 130 | controller_->UnmarkText(); |
| 131 | } |
| 132 | |
Ryan Landay | 7b5dbd5 | 2018-01-11 19:05:59 | [diff] [blame] | 133 | void TextInputControllerBindings::UnmarkAndUnselectText() { |
| 134 | if (controller_) |
| 135 | controller_->UnmarkAndUnselectText(); |
| 136 | } |
| 137 | |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 138 | void TextInputControllerBindings::DoCommand(const std::string& text) { |
| 139 | if (controller_) |
| 140 | controller_->DoCommand(text); |
| 141 | } |
| 142 | |
Ryan Landay | 9c680942 | 2018-01-17 07:04:15 | [diff] [blame] | 143 | void TextInputControllerBindings::ExtendSelectionAndDelete(int before, |
| 144 | int after) { |
| 145 | if (controller_) |
| 146 | controller_->ExtendSelectionAndDelete(before, after); |
| 147 | } |
| 148 | |
Ryan Landay | 6d163ae | 2018-01-17 06:58:06 | [diff] [blame] | 149 | void TextInputControllerBindings::DeleteSurroundingText(int before, int after) { |
| 150 | if (controller_) |
| 151 | controller_->DeleteSurroundingText(before, after); |
| 152 | } |
| 153 | |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 154 | void TextInputControllerBindings::SetMarkedText(const std::string& text, |
| 155 | int start, |
| 156 | int length) { |
| 157 | if (controller_) |
| 158 | controller_->SetMarkedText(text, start, length); |
| 159 | } |
| 160 | |
Ryan Landay | d203467 | 2018-01-12 22:22:32 | [diff] [blame] | 161 | void TextInputControllerBindings::SetMarkedTextFromExistingText(int start, |
| 162 | int end) { |
| 163 | if (controller_) |
| 164 | controller_->SetMarkedTextFromExistingText(start, end); |
| 165 | } |
| 166 | |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 167 | bool TextInputControllerBindings::HasMarkedText() { |
| 168 | return controller_ ? controller_->HasMarkedText() : false; |
| 169 | } |
| 170 | |
| 171 | std::vector<int> TextInputControllerBindings::MarkedRange() { |
| 172 | return controller_ ? controller_->MarkedRange() : std::vector<int>(); |
| 173 | } |
| 174 | |
| 175 | std::vector<int> TextInputControllerBindings::SelectedRange() { |
| 176 | return controller_ ? controller_->SelectedRange() : std::vector<int>(); |
| 177 | } |
| 178 | |
| 179 | std::vector<int> TextInputControllerBindings::FirstRectForCharacterRange( |
| 180 | unsigned location, |
| 181 | unsigned length) { |
| 182 | return controller_ ? controller_->FirstRectForCharacterRange(location, length) |
| 183 | : std::vector<int>(); |
| 184 | } |
| 185 | |
| 186 | void TextInputControllerBindings::SetComposition(const std::string& text) { |
| 187 | if (controller_) |
| 188 | controller_->SetComposition(text); |
| 189 | } |
ekaramad | 2daaf67 | 2016-11-10 20:29:01 | [diff] [blame] | 190 | void TextInputControllerBindings::ForceTextInputStateUpdate() { |
| 191 | if (controller_) |
| 192 | controller_->ForceTextInputStateUpdate(); |
| 193 | } |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 194 | // TextInputController --------------------------------------------------------- |
| 195 | |
lfg | 05e4137 | 2016-07-22 15:38:10 | [diff] [blame] | 196 | TextInputController::TextInputController( |
| 197 | WebViewTestProxyBase* web_view_test_proxy_base) |
| 198 | : web_view_test_proxy_base_(web_view_test_proxy_base), |
| 199 | weak_factory_(this) {} |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 200 | |
| 201 | TextInputController::~TextInputController() {} |
| 202 | |
lukasza | 8b6d5f3 | 2016-04-22 16:56:31 | [diff] [blame] | 203 | void TextInputController::Install(blink::WebLocalFrame* frame) { |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 204 | TextInputControllerBindings::Install(weak_factory_.GetWeakPtr(), frame); |
| 205 | } |
| 206 | |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 207 | void TextInputController::InsertText(const std::string& text) { |
ekaramad | c75b1b3b3 | 2016-12-02 03:57:52 | [diff] [blame] | 208 | if (auto* controller = GetInputMethodController()) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 209 | controller->CommitText(blink::WebString::FromUTF8(text), |
Ryan Landay | 9e42fd74 | 2017-08-12 01:59:11 | [diff] [blame] | 210 | std::vector<blink::WebImeTextSpan>(), |
ekaramad | ce32ef9f | 2017-02-09 17:33:56 | [diff] [blame] | 211 | blink::WebRange(), 0); |
ekaramad | c75b1b3b3 | 2016-12-02 03:57:52 | [diff] [blame] | 212 | } |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | void TextInputController::UnmarkText() { |
ekaramad | c75b1b3b3 | 2016-12-02 03:57:52 | [diff] [blame] | 216 | if (auto* controller = GetInputMethodController()) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 217 | controller->FinishComposingText( |
| 218 | blink::WebInputMethodController::kKeepSelection); |
ekaramad | c75b1b3b3 | 2016-12-02 03:57:52 | [diff] [blame] | 219 | } |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 220 | } |
| 221 | |
Ryan Landay | 7b5dbd5 | 2018-01-11 19:05:59 | [diff] [blame] | 222 | void TextInputController::UnmarkAndUnselectText() { |
| 223 | if (auto* controller = GetInputMethodController()) { |
| 224 | controller->FinishComposingText( |
| 225 | blink::WebInputMethodController::kDoNotKeepSelection); |
| 226 | } |
| 227 | } |
| 228 | |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 229 | void TextInputController::DoCommand(const std::string& text) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 230 | if (view()->MainFrame()) { |
Ryan Landay | d203467 | 2018-01-12 22:22:32 | [diff] [blame] | 231 | CHECK(view()->MainFrame()->ToWebLocalFrame()) << "This function cannot be " |
| 232 | "called if the main frame " |
| 233 | "is not a local frame."; |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 234 | view()->MainFrame()->ToWebLocalFrame()->ExecuteCommand( |
| 235 | blink::WebString::FromUTF8(text)); |
yabinh | 8204efc | 2016-07-08 13:58:57 | [diff] [blame] | 236 | } |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 237 | } |
| 238 | |
Ryan Landay | 9c680942 | 2018-01-17 07:04:15 | [diff] [blame] | 239 | void TextInputController::ExtendSelectionAndDelete(int before, int after) { |
| 240 | if (view()->MainFrame()) { |
| 241 | CHECK(view()->MainFrame()->ToWebLocalFrame()) << "This function cannot be " |
| 242 | "called if the main frame " |
| 243 | "is not a local frame."; |
| 244 | view()->MainFrame()->ToWebLocalFrame()->ExtendSelectionAndDelete(before, |
| 245 | after); |
| 246 | } |
| 247 | } |
| 248 | |
Ryan Landay | 6d163ae | 2018-01-17 06:58:06 | [diff] [blame] | 249 | void TextInputController::DeleteSurroundingText(int before, int after) { |
| 250 | if (view()->MainFrame()) { |
| 251 | CHECK(view()->MainFrame()->ToWebLocalFrame()) << "This function cannot be " |
| 252 | "called if the main frame " |
| 253 | "is not a local frame."; |
| 254 | view()->MainFrame()->ToWebLocalFrame()->DeleteSurroundingText(before, |
| 255 | after); |
| 256 | } |
| 257 | } |
| 258 | |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 259 | void TextInputController::SetMarkedText(const std::string& text, |
| 260 | int start, |
| 261 | int length) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 262 | blink::WebString web_text(blink::WebString::FromUTF8(text)); |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 263 | |
| 264 | // Split underline into up to 3 elements (before, selection, and after). |
Ryan Landay | 9e42fd74 | 2017-08-12 01:59:11 | [diff] [blame] | 265 | std::vector<blink::WebImeTextSpan> ime_text_spans; |
| 266 | blink::WebImeTextSpan ime_text_span; |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 267 | if (!start) { |
Ryan Landay | 9e42fd74 | 2017-08-12 01:59:11 | [diff] [blame] | 268 | ime_text_span.end_offset = length; |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 269 | } else { |
Ryan Landay | 9e42fd74 | 2017-08-12 01:59:11 | [diff] [blame] | 270 | ime_text_span.end_offset = start; |
| 271 | ime_text_spans.push_back(ime_text_span); |
| 272 | ime_text_span.start_offset = start; |
| 273 | ime_text_span.end_offset = start + length; |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 274 | } |
Manuel Rego Casasnovas | 05eec2c | 2018-03-08 06:54:41 | [diff] [blame] | 275 | ime_text_span.thickness = ui::mojom::ImeTextSpanThickness::kThick; |
Ryan Landay | 9e42fd74 | 2017-08-12 01:59:11 | [diff] [blame] | 276 | ime_text_spans.push_back(ime_text_span); |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 277 | if (start + length < static_cast<int>(web_text.length())) { |
Ryan Landay | 9e42fd74 | 2017-08-12 01:59:11 | [diff] [blame] | 278 | ime_text_span.start_offset = ime_text_span.end_offset; |
| 279 | ime_text_span.end_offset = web_text.length(); |
Manuel Rego Casasnovas | 05eec2c | 2018-03-08 06:54:41 | [diff] [blame] | 280 | ime_text_span.thickness = ui::mojom::ImeTextSpanThickness::kThin; |
Ryan Landay | 9e42fd74 | 2017-08-12 01:59:11 | [diff] [blame] | 281 | ime_text_spans.push_back(ime_text_span); |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 282 | } |
| 283 | |
ekaramad | c75b1b3b3 | 2016-12-02 03:57:52 | [diff] [blame] | 284 | if (auto* controller = GetInputMethodController()) { |
Ryan Landay | 9e42fd74 | 2017-08-12 01:59:11 | [diff] [blame] | 285 | controller->SetComposition(web_text, ime_text_spans, blink::WebRange(), |
| 286 | start, start + length); |
ekaramad | c75b1b3b3 | 2016-12-02 03:57:52 | [diff] [blame] | 287 | } |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 288 | } |
| 289 | |
Ryan Landay | d203467 | 2018-01-12 22:22:32 | [diff] [blame] | 290 | void TextInputController::SetMarkedTextFromExistingText(int start, int end) { |
| 291 | if (!view()->MainFrame()) |
| 292 | return; |
| 293 | |
| 294 | CHECK(view()->MainFrame()->ToWebLocalFrame()) << "This function cannot be " |
| 295 | "called if the main frame " |
| 296 | "is not a local frame."; |
| 297 | |
| 298 | view()->MainFrame()->ToWebLocalFrame()->SetCompositionFromExistingText( |
| 299 | start, end, std::vector<blink::WebImeTextSpan>()); |
| 300 | } |
| 301 | |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 302 | bool TextInputController::HasMarkedText() { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 303 | if (!view()->MainFrame()) |
yabinh | 8204efc | 2016-07-08 13:58:57 | [diff] [blame] | 304 | return false; |
| 305 | |
Ryan Landay | d203467 | 2018-01-12 22:22:32 | [diff] [blame] | 306 | CHECK(view()->MainFrame()->ToWebLocalFrame()) << "This function cannot be " |
| 307 | "called if the main frame " |
| 308 | "is not a local frame."; |
yabinh | 8204efc | 2016-07-08 13:58:57 | [diff] [blame] | 309 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 310 | return view()->MainFrame()->ToWebLocalFrame()->HasMarkedText(); |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | std::vector<int> TextInputController::MarkedRange() { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 314 | if (!view()->MainFrame()) |
[email protected] | 6f939712 | 2014-02-25 09:30:50 | [diff] [blame] | 315 | return std::vector<int>(); |
| 316 | |
Ryan Landay | d203467 | 2018-01-12 22:22:32 | [diff] [blame] | 317 | CHECK(view()->MainFrame()->ToWebLocalFrame()) << "This function cannot be " |
| 318 | "called if the main frame " |
| 319 | "is not a local frame."; |
yabinh | 8204efc | 2016-07-08 13:58:57 | [diff] [blame] | 320 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 321 | blink::WebRange range = view()->MainFrame()->ToWebLocalFrame()->MarkedRange(); |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 322 | std::vector<int> int_array(2); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 323 | int_array[0] = range.StartOffset(); |
| 324 | int_array[1] = range.EndOffset(); |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 325 | |
| 326 | return int_array; |
| 327 | } |
| 328 | |
| 329 | std::vector<int> TextInputController::SelectedRange() { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 330 | if (!view()->MainFrame()) |
[email protected] | 6f939712 | 2014-02-25 09:30:50 | [diff] [blame] | 331 | return std::vector<int>(); |
| 332 | |
Ryan Landay | d203467 | 2018-01-12 22:22:32 | [diff] [blame] | 333 | CHECK(view()->MainFrame()->ToWebLocalFrame()) << "This function cannot be " |
| 334 | "called if the main frame " |
| 335 | "is not a local frame."; |
yabinh | 8204efc | 2016-07-08 13:58:57 | [diff] [blame] | 336 | |
| 337 | blink::WebRange range = |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 338 | view()->MainFrame()->ToWebLocalFrame()->SelectionRange(); |
| 339 | if (range.IsNull()) |
yosin | 702c78a3 | 2016-06-13 08:39:13 | [diff] [blame] | 340 | return std::vector<int>(); |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 341 | std::vector<int> int_array(2); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 342 | int_array[0] = range.StartOffset(); |
| 343 | int_array[1] = range.EndOffset(); |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 344 | |
| 345 | return int_array; |
| 346 | } |
| 347 | |
| 348 | std::vector<int> TextInputController::FirstRectForCharacterRange( |
| 349 | unsigned location, |
| 350 | unsigned length) { |
| 351 | blink::WebRect rect; |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 352 | if (!view()->FocusedFrame() || |
| 353 | !view()->FocusedFrame()->FirstRectForCharacterRange(location, length, |
lukasza | 8b6d5f3 | 2016-04-22 16:56:31 | [diff] [blame] | 354 | rect)) { |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 355 | return std::vector<int>(); |
[email protected] | 6f939712 | 2014-02-25 09:30:50 | [diff] [blame] | 356 | } |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 357 | |
| 358 | std::vector<int> int_array(4); |
| 359 | int_array[0] = rect.x; |
| 360 | int_array[1] = rect.y; |
| 361 | int_array[2] = rect.width; |
| 362 | int_array[3] = rect.height; |
| 363 | |
| 364 | return int_array; |
| 365 | } |
| 366 | |
| 367 | void TextInputController::SetComposition(const std::string& text) { |
| 368 | // Sends a keydown event with key code = 0xE5 to emulate input method |
| 369 | // behavior. |
Daniel Cheng | 224569ee | 2018-04-25 05:45:06 | [diff] [blame^] | 370 | blink::WebKeyboardEvent key_down(blink::WebInputEvent::kRawKeyDown, |
| 371 | blink::WebInputEvent::kNoModifiers, |
| 372 | ui::EventTimeForNow()); |
dtapuska | 899ac22 | 2017-01-03 18:09:16 | [diff] [blame] | 373 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 374 | key_down.windows_key_code = 0xE5; // VKEY_PROCESSKEY |
| 375 | view()->HandleInputEvent(blink::WebCoalescedInputEvent(key_down)); |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 376 | |
yabinh | dd6d1b6 | 2016-08-23 07:09:02 | [diff] [blame] | 377 | // The value returned by std::string::length() may not correspond to the |
| 378 | // actual number of encoded characters in sequences of multi-byte or |
| 379 | // variable-length characters. |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 380 | blink::WebString newText = blink::WebString::FromUTF8(text); |
yabinh | dd6d1b6 | 2016-08-23 07:09:02 | [diff] [blame] | 381 | size_t textLength = newText.length(); |
| 382 | |
Ryan Landay | 9e42fd74 | 2017-08-12 01:59:11 | [diff] [blame] | 383 | std::vector<blink::WebImeTextSpan> ime_text_spans; |
Ryan Landay | 9387aa9 | 2017-08-16 23:16:52 | [diff] [blame] | 384 | ime_text_spans.push_back(blink::WebImeTextSpan( |
Manuel Rego Casasnovas | 6743b45 | 2018-03-22 20:42:39 | [diff] [blame] | 385 | blink::WebImeTextSpan::Type::kComposition, 0, textLength, |
Manuel Rego Casasnovas | 05eec2c | 2018-03-08 06:54:41 | [diff] [blame] | 386 | ui::mojom::ImeTextSpanThickness::kThin, SK_ColorTRANSPARENT)); |
ekaramad | c75b1b3b3 | 2016-12-02 03:57:52 | [diff] [blame] | 387 | if (auto* controller = GetInputMethodController()) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 388 | controller->SetComposition( |
Ryan Landay | 9e42fd74 | 2017-08-12 01:59:11 | [diff] [blame] | 389 | newText, blink::WebVector<blink::WebImeTextSpan>(ime_text_spans), |
ekaramad | ce32ef9f | 2017-02-09 17:33:56 | [diff] [blame] | 390 | blink::WebRange(), textLength, textLength); |
ekaramad | c75b1b3b3 | 2016-12-02 03:57:52 | [diff] [blame] | 391 | } |
lukasza | 8b6d5f3 | 2016-04-22 16:56:31 | [diff] [blame] | 392 | } |
| 393 | |
ekaramad | 2daaf67 | 2016-11-10 20:29:01 | [diff] [blame] | 394 | void TextInputController::ForceTextInputStateUpdate() { |
lukasza | bedb4b2 | 2017-06-23 00:00:13 | [diff] [blame] | 395 | // TODO(lukasza): Finish adding OOPIF support to the layout tests harness. |
Daniel Cheng | 3c82943 | 2017-06-19 03:42:31 | [diff] [blame] | 396 | CHECK(view()->MainFrame()->IsWebLocalFrame()) |
| 397 | << "WebView does not have a local main frame and" |
| 398 | " cannot handle input method controller tasks."; |
ekaramad | 2daaf67 | 2016-11-10 20:29:01 | [diff] [blame] | 399 | web_view_test_proxy_base_->delegate()->ForceTextInputStateUpdate( |
Daniel Cheng | 3c82943 | 2017-06-19 03:42:31 | [diff] [blame] | 400 | view()->MainFrame()->ToWebLocalFrame()); |
ekaramad | 2daaf67 | 2016-11-10 20:29:01 | [diff] [blame] | 401 | } |
| 402 | |
lukasza | 8b6d5f3 | 2016-04-22 16:56:31 | [diff] [blame] | 403 | blink::WebView* TextInputController::view() { |
lfg | 05e4137 | 2016-07-22 15:38:10 | [diff] [blame] | 404 | return web_view_test_proxy_base_->web_view(); |
[email protected] | f751653a9 | 2014-02-18 16:32:55 | [diff] [blame] | 405 | } |
| 406 | |
ekaramad | c75b1b3b3 | 2016-12-02 03:57:52 | [diff] [blame] | 407 | blink::WebInputMethodController* |
| 408 | TextInputController::GetInputMethodController() { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 409 | if (!view()->MainFrame()) |
ekaramad | c75b1b3b3 | 2016-12-02 03:57:52 | [diff] [blame] | 410 | return nullptr; |
| 411 | |
lukasza | bedb4b2 | 2017-06-23 00:00:13 | [diff] [blame] | 412 | // TODO(lukasza): Finish adding OOPIF support to the layout tests harness. |
Daniel Cheng | 3c82943 | 2017-06-19 03:42:31 | [diff] [blame] | 413 | CHECK(view()->MainFrame()->IsWebLocalFrame()) |
| 414 | << "WebView does not have a local main frame and" |
| 415 | " cannot handle input method controller tasks."; |
| 416 | |
| 417 | return view() |
| 418 | ->MainFrame() |
| 419 | ->ToWebLocalFrame() |
| 420 | ->FrameWidget() |
| 421 | ->GetActiveWebInputMethodController(); |
ekaramad | 2daaf67 | 2016-11-10 20:29:01 | [diff] [blame] | 422 | } |
| 423 | |
jochen | f5f3175 | 2015-06-03 12:06:34 | [diff] [blame] | 424 | } // namespace test_runner |