blob: 4a7b4522fdd724541619861ad96986baf63b44b5 [file] [log] [blame]
[email protected]f751653a92014-02-18 16:32:551// 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
jochen73e711c2015-06-03 10:01:465#include "components/test_runner/text_input_controller.h"
[email protected]f751653a92014-02-18 16:32:556
avi5dd91f82015-12-25 22:30:467#include "base/macros.h"
lukasza8b6d5f32016-04-22 16:56:318#include "components/test_runner/web_test_proxy.h"
[email protected]f751653a92014-02-18 16:32:559#include "gin/arguments.h"
10#include "gin/handle.h"
11#include "gin/object_template_builder.h"
12#include "gin/wrappable.h"
13#include "third_party/WebKit/public/web/WebCompositionUnderline.h"
[email protected]f751653a92014-02-18 16:32:5514#include "third_party/WebKit/public/web/WebInputEvent.h"
15#include "third_party/WebKit/public/web/WebKit.h"
lukasza8b6d5f32016-04-22 16:56:3116#include "third_party/WebKit/public/web/WebLocalFrame.h"
[email protected]f751653a92014-02-18 16:32:5517#include "third_party/WebKit/public/web/WebRange.h"
18#include "third_party/WebKit/public/web/WebView.h"
changwan9bdc18f02015-11-20 02:43:5219#include "third_party/skia/include/core/SkColor.h"
[email protected]f751653a92014-02-18 16:32:5520#include "v8/include/v8.h"
21
jochenf5f31752015-06-03 12:06:3422namespace test_runner {
[email protected]f751653a92014-02-18 16:32:5523
24class TextInputControllerBindings
25 : public gin::Wrappable<TextInputControllerBindings> {
26 public:
27 static gin::WrapperInfo kWrapperInfo;
28
29 static void Install(base::WeakPtr<TextInputController> controller,
lukasza8b6d5f32016-04-22 16:56:3130 blink::WebLocalFrame* frame);
[email protected]f751653a92014-02-18 16:32:5531
32 private:
33 explicit TextInputControllerBindings(
34 base::WeakPtr<TextInputController> controller);
dchenge933b3e2014-10-21 11:44:0935 ~TextInputControllerBindings() override;
[email protected]f751653a92014-02-18 16:32:5536
37 // gin::Wrappable:
dchenge933b3e2014-10-21 11:44:0938 gin::ObjectTemplateBuilder GetObjectTemplateBuilder(
anand.ratn449f39a42014-10-06 13:45:5739 v8::Isolate* isolate) override;
[email protected]f751653a92014-02-18 16:32:5540
41 void InsertText(const std::string& text);
42 void UnmarkText();
43 void DoCommand(const std::string& text);
44 void SetMarkedText(const std::string& text, int start, int length);
45 bool HasMarkedText();
46 std::vector<int> MarkedRange();
47 std::vector<int> SelectedRange();
48 std::vector<int> FirstRectForCharacterRange(unsigned location,
49 unsigned length);
50 void SetComposition(const std::string& text);
51
52 base::WeakPtr<TextInputController> controller_;
53
54 DISALLOW_COPY_AND_ASSIGN(TextInputControllerBindings);
55};
56
57gin::WrapperInfo TextInputControllerBindings::kWrapperInfo = {
58 gin::kEmbedderNativeGin};
59
60// static
61void TextInputControllerBindings::Install(
62 base::WeakPtr<TextInputController> controller,
lukasza8b6d5f32016-04-22 16:56:3163 blink::WebLocalFrame* frame) {
[email protected]f751653a92014-02-18 16:32:5564 v8::Isolate* isolate = blink::mainThreadIsolate();
65 v8::HandleScope handle_scope(isolate);
deepak.s750d68f2015-04-30 07:32:4166 v8::Local<v8::Context> context = frame->mainWorldScriptContext();
[email protected]f751653a92014-02-18 16:32:5567 if (context.IsEmpty())
68 return;
69
70 v8::Context::Scope context_scope(context);
71
72 gin::Handle<TextInputControllerBindings> bindings =
73 gin::CreateHandle(isolate, new TextInputControllerBindings(controller));
[email protected]ad4d2032014-04-28 13:50:5974 if (bindings.IsEmpty())
75 return;
deepak.s750d68f2015-04-30 07:32:4176 v8::Local<v8::Object> global = context->Global();
[email protected]f751653a92014-02-18 16:32:5577 global->Set(gin::StringToV8(isolate, "textInputController"), bindings.ToV8());
78}
79
80TextInputControllerBindings::TextInputControllerBindings(
81 base::WeakPtr<TextInputController> controller)
82 : controller_(controller) {}
83
84TextInputControllerBindings::~TextInputControllerBindings() {}
85
86gin::ObjectTemplateBuilder
87TextInputControllerBindings::GetObjectTemplateBuilder(v8::Isolate* isolate) {
88 return gin::Wrappable<TextInputControllerBindings>::GetObjectTemplateBuilder(
89 isolate)
90 .SetMethod("insertText", &TextInputControllerBindings::InsertText)
91 .SetMethod("unmarkText", &TextInputControllerBindings::UnmarkText)
92 .SetMethod("doCommand", &TextInputControllerBindings::DoCommand)
93 .SetMethod("setMarkedText", &TextInputControllerBindings::SetMarkedText)
94 .SetMethod("hasMarkedText", &TextInputControllerBindings::HasMarkedText)
95 .SetMethod("markedRange", &TextInputControllerBindings::MarkedRange)
96 .SetMethod("selectedRange", &TextInputControllerBindings::SelectedRange)
97 .SetMethod("firstRectForCharacterRange",
98 &TextInputControllerBindings::FirstRectForCharacterRange)
99 .SetMethod("setComposition",
100 &TextInputControllerBindings::SetComposition);
101}
102
103void TextInputControllerBindings::InsertText(const std::string& text) {
104 if (controller_)
105 controller_->InsertText(text);
106}
107
108void TextInputControllerBindings::UnmarkText() {
109 if (controller_)
110 controller_->UnmarkText();
111}
112
113void TextInputControllerBindings::DoCommand(const std::string& text) {
114 if (controller_)
115 controller_->DoCommand(text);
116}
117
118void TextInputControllerBindings::SetMarkedText(const std::string& text,
119 int start,
120 int length) {
121 if (controller_)
122 controller_->SetMarkedText(text, start, length);
123}
124
125bool TextInputControllerBindings::HasMarkedText() {
126 return controller_ ? controller_->HasMarkedText() : false;
127}
128
129std::vector<int> TextInputControllerBindings::MarkedRange() {
130 return controller_ ? controller_->MarkedRange() : std::vector<int>();
131}
132
133std::vector<int> TextInputControllerBindings::SelectedRange() {
134 return controller_ ? controller_->SelectedRange() : std::vector<int>();
135}
136
137std::vector<int> TextInputControllerBindings::FirstRectForCharacterRange(
138 unsigned location,
139 unsigned length) {
140 return controller_ ? controller_->FirstRectForCharacterRange(location, length)
141 : std::vector<int>();
142}
143
144void TextInputControllerBindings::SetComposition(const std::string& text) {
145 if (controller_)
146 controller_->SetComposition(text);
147}
148
149// TextInputController ---------------------------------------------------------
150
lukasza8b6d5f32016-04-22 16:56:31151TextInputController::TextInputController(WebTestProxyBase* web_test_proxy_base)
152 : web_test_proxy_base_(web_test_proxy_base), weak_factory_(this) {}
[email protected]f751653a92014-02-18 16:32:55153
154TextInputController::~TextInputController() {}
155
lukasza8b6d5f32016-04-22 16:56:31156void TextInputController::Install(blink::WebLocalFrame* frame) {
[email protected]f751653a92014-02-18 16:32:55157 TextInputControllerBindings::Install(weak_factory_.GetWeakPtr(), frame);
158}
159
[email protected]f751653a92014-02-18 16:32:55160void TextInputController::InsertText(const std::string& text) {
lukasza8b6d5f32016-04-22 16:56:31161 view()->confirmComposition(blink::WebString::fromUTF8(text));
[email protected]f751653a92014-02-18 16:32:55162}
163
164void TextInputController::UnmarkText() {
lukasza8b6d5f32016-04-22 16:56:31165 view()->confirmComposition();
[email protected]f751653a92014-02-18 16:32:55166}
167
168void TextInputController::DoCommand(const std::string& text) {
lukasza8b6d5f32016-04-22 16:56:31169 if (view()->mainFrame())
170 view()->mainFrame()->executeCommand(blink::WebString::fromUTF8(text));
[email protected]f751653a92014-02-18 16:32:55171}
172
173void TextInputController::SetMarkedText(const std::string& text,
174 int start,
175 int length) {
176 blink::WebString web_text(blink::WebString::fromUTF8(text));
177
178 // Split underline into up to 3 elements (before, selection, and after).
179 std::vector<blink::WebCompositionUnderline> underlines;
180 blink::WebCompositionUnderline underline;
181 if (!start) {
182 underline.endOffset = length;
183 } else {
184 underline.endOffset = start;
185 underlines.push_back(underline);
186 underline.startOffset = start;
187 underline.endOffset = start + length;
188 }
189 underline.thick = true;
190 underlines.push_back(underline);
191 if (start + length < static_cast<int>(web_text.length())) {
192 underline.startOffset = underline.endOffset;
193 underline.endOffset = web_text.length();
194 underline.thick = false;
195 underlines.push_back(underline);
196 }
197
lukasza8b6d5f32016-04-22 16:56:31198 view()->setComposition(web_text, underlines, start, start + length);
[email protected]f751653a92014-02-18 16:32:55199}
200
201bool TextInputController::HasMarkedText() {
lukasza8b6d5f32016-04-22 16:56:31202 return view()->mainFrame() && view()->mainFrame()->hasMarkedText();
[email protected]f751653a92014-02-18 16:32:55203}
204
205std::vector<int> TextInputController::MarkedRange() {
lukasza8b6d5f32016-04-22 16:56:31206 if (!view()->mainFrame())
[email protected]6f9397122014-02-25 09:30:50207 return std::vector<int>();
208
lukasza8b6d5f32016-04-22 16:56:31209 blink::WebRange range = view()->mainFrame()->markedRange();
[email protected]f751653a92014-02-18 16:32:55210 std::vector<int> int_array(2);
211 int_array[0] = range.startOffset();
212 int_array[1] = range.endOffset();
213
214 return int_array;
215}
216
217std::vector<int> TextInputController::SelectedRange() {
lukasza8b6d5f32016-04-22 16:56:31218 if (!view()->mainFrame())
[email protected]6f9397122014-02-25 09:30:50219 return std::vector<int>();
220
lukasza8b6d5f32016-04-22 16:56:31221 blink::WebRange range = view()->mainFrame()->selectionRange();
[email protected]f751653a92014-02-18 16:32:55222 std::vector<int> int_array(2);
223 int_array[0] = range.startOffset();
224 int_array[1] = range.endOffset();
225
226 return int_array;
227}
228
229std::vector<int> TextInputController::FirstRectForCharacterRange(
230 unsigned location,
231 unsigned length) {
232 blink::WebRect rect;
lukasza8b6d5f32016-04-22 16:56:31233 if (!view()->focusedFrame() ||
234 !view()->focusedFrame()->firstRectForCharacterRange(location, length,
235 rect)) {
[email protected]f751653a92014-02-18 16:32:55236 return std::vector<int>();
[email protected]6f9397122014-02-25 09:30:50237 }
[email protected]f751653a92014-02-18 16:32:55238
239 std::vector<int> int_array(4);
240 int_array[0] = rect.x;
241 int_array[1] = rect.y;
242 int_array[2] = rect.width;
243 int_array[3] = rect.height;
244
245 return int_array;
246}
247
248void TextInputController::SetComposition(const std::string& text) {
249 // Sends a keydown event with key code = 0xE5 to emulate input method
250 // behavior.
251 blink::WebKeyboardEvent key_down;
252 key_down.type = blink::WebInputEvent::RawKeyDown;
253 key_down.modifiers = 0;
254 key_down.windowsKeyCode = 0xE5; // VKEY_PROCESSKEY
255 key_down.setKeyIdentifierFromWindowsKeyCode();
lukasza8b6d5f32016-04-22 16:56:31256 view()->handleInputEvent(key_down);
[email protected]f751653a92014-02-18 16:32:55257
changwan9bdc18f02015-11-20 02:43:52258 std::vector<blink::WebCompositionUnderline> underlines;
259 underlines.push_back(blink::WebCompositionUnderline(0, text.length(),
260 SK_ColorBLACK, false,
261 SK_ColorTRANSPARENT));
lukasza8b6d5f32016-04-22 16:56:31262 view()->setComposition(
changwan9bdc18f02015-11-20 02:43:52263 blink::WebString::fromUTF8(text),
264 blink::WebVector<blink::WebCompositionUnderline>(underlines),
lukasza8b6d5f32016-04-22 16:56:31265 text.length(), text.length());
266}
267
268blink::WebView* TextInputController::view() {
269 return web_test_proxy_base_->web_view();
[email protected]f751653a92014-02-18 16:32:55270}
271
jochenf5f31752015-06-03 12:06:34272} // namespace test_runner