OLD | NEW |
| (Empty) |
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 | |
5 #include "content/shell/renderer/test_runner/TextInputController.h" | |
6 | |
7 #include <string> | |
8 #include "content/shell/renderer/test_runner/TestCommon.h" | |
9 #include "third_party/WebKit/public/platform/WebString.h" | |
10 #include "third_party/WebKit/public/platform/WebVector.h" | |
11 #include "third_party/WebKit/public/web/WebBindings.h" | |
12 #include "third_party/WebKit/public/web/WebCompositionUnderline.h" | |
13 #include "third_party/WebKit/public/web/WebFrame.h" | |
14 #include "third_party/WebKit/public/web/WebInputEvent.h" | |
15 #include "third_party/WebKit/public/web/WebRange.h" | |
16 #include "third_party/WebKit/public/web/WebView.h" | |
17 | |
18 using namespace blink; | |
19 using namespace std; | |
20 | |
21 namespace WebTestRunner { | |
22 | |
23 TextInputController::TextInputController() | |
24 { | |
25 bindMethod("doCommand", &TextInputController::doCommand); | |
26 bindMethod("firstRectForCharacterRange", &TextInputController::firstRectForC
haracterRange); | |
27 bindMethod("hasMarkedText", &TextInputController::hasMarkedText); | |
28 bindMethod("insertText", &TextInputController::insertText); | |
29 bindMethod("markedRange", &TextInputController::markedRange); | |
30 bindMethod("selectedRange", &TextInputController::selectedRange); | |
31 bindMethod("setMarkedText", &TextInputController::setMarkedText); | |
32 bindMethod("unmarkText", &TextInputController::unmarkText); | |
33 bindMethod("setComposition", &TextInputController::setComposition); | |
34 } | |
35 | |
36 void TextInputController::insertText(const CppArgumentList& arguments, CppVarian
t* result) | |
37 { | |
38 result->setNull(); | |
39 | |
40 if (arguments.size() < 1 || !arguments[0].isString()) | |
41 return; | |
42 | |
43 m_webView->confirmComposition(WebString::fromUTF8(arguments[0].toString())); | |
44 } | |
45 | |
46 void TextInputController::doCommand(const CppArgumentList& arguments, CppVariant
* result) | |
47 { | |
48 result->setNull(); | |
49 | |
50 WebFrame* mainFrame = m_webView->mainFrame(); | |
51 if (!mainFrame) | |
52 return; | |
53 | |
54 if (arguments.size() >= 1 && arguments[0].isString()) | |
55 mainFrame->executeCommand(WebString::fromUTF8(arguments[0].toString())); | |
56 } | |
57 | |
58 void TextInputController::setMarkedText(const CppArgumentList& arguments, CppVar
iant* result) | |
59 { | |
60 result->setNull(); | |
61 | |
62 if (arguments.size() < 3 || !arguments[0].isString() | |
63 || !arguments[1].isNumber() || !arguments[2].isNumber()) | |
64 return; | |
65 | |
66 WebString text(WebString::fromUTF8(arguments[0].toString())); | |
67 int start = arguments[1].toInt32(); | |
68 int length = arguments[2].toInt32(); | |
69 | |
70 // Split underline into up to 3 elements (before, selection, and after). | |
71 vector<WebCompositionUnderline> underlines; | |
72 WebCompositionUnderline underline; | |
73 if (!start) { | |
74 underline.endOffset = length; | |
75 } else { | |
76 underline.endOffset = start; | |
77 underlines.push_back(underline); | |
78 underline.startOffset = start; | |
79 underline.endOffset = start + length; | |
80 } | |
81 underline.thick = true; | |
82 underlines.push_back(underline); | |
83 if (start + length < static_cast<int>(text.length())) { | |
84 underline.startOffset = underline.endOffset; | |
85 underline.endOffset = text.length(); | |
86 underline.thick = false; | |
87 underlines.push_back(underline); | |
88 } | |
89 | |
90 m_webView->setComposition(text, underlines, start, start + length); | |
91 } | |
92 | |
93 void TextInputController::unmarkText(const CppArgumentList&, CppVariant* result) | |
94 { | |
95 result->setNull(); | |
96 | |
97 m_webView->confirmComposition(); | |
98 } | |
99 | |
100 void TextInputController::hasMarkedText(const CppArgumentList&, CppVariant* resu
lt) | |
101 { | |
102 result->setNull(); | |
103 | |
104 WebFrame* mainFrame = m_webView->mainFrame(); | |
105 if (!mainFrame) | |
106 return; | |
107 | |
108 result->set(mainFrame->hasMarkedText()); | |
109 } | |
110 | |
111 void TextInputController::markedRange(const CppArgumentList&, CppVariant* result
) | |
112 { | |
113 result->setNull(); | |
114 | |
115 WebFrame* mainFrame = m_webView->mainFrame(); | |
116 if (!mainFrame) | |
117 return; | |
118 | |
119 WebRange range = mainFrame->markedRange(); | |
120 vector<int> intArray(2); | |
121 intArray[0] = range.startOffset(); | |
122 intArray[1] = range.endOffset(); | |
123 | |
124 NPObject* resultArray = WebBindings::makeIntArray(intArray); | |
125 result->set(resultArray); | |
126 WebBindings::releaseObject(resultArray); | |
127 } | |
128 | |
129 void TextInputController::selectedRange(const CppArgumentList&, CppVariant* resu
lt) | |
130 { | |
131 result->setNull(); | |
132 | |
133 WebFrame* mainFrame = m_webView->mainFrame(); | |
134 if (!mainFrame) | |
135 return; | |
136 | |
137 WebRange range = mainFrame->selectionRange(); | |
138 vector<int> intArray(2); | |
139 intArray[0] = range.startOffset(); | |
140 intArray[1] = range.endOffset(); | |
141 | |
142 NPObject* resultArray = WebBindings::makeIntArray(intArray); | |
143 result->set(resultArray); | |
144 WebBindings::releaseObject(resultArray); | |
145 } | |
146 | |
147 void TextInputController::firstRectForCharacterRange(const CppArgumentList& argu
ments, CppVariant* result) | |
148 { | |
149 result->setNull(); | |
150 | |
151 WebFrame* frame = m_webView->focusedFrame(); | |
152 if (!frame) | |
153 return; | |
154 | |
155 if (arguments.size() < 2 || !arguments[0].isNumber() || !arguments[1].isNumb
er()) | |
156 return; | |
157 | |
158 WebRect rect; | |
159 if (!frame->firstRectForCharacterRange(arguments[0].toInt32(), arguments[1].
toInt32(), rect)) | |
160 return; | |
161 | |
162 vector<int> intArray(4); | |
163 intArray[0] = rect.x; | |
164 intArray[1] = rect.y; | |
165 intArray[2] = rect.width; | |
166 intArray[3] = rect.height; | |
167 | |
168 NPObject* resultArray = WebBindings::makeIntArray(intArray); | |
169 result->set(resultArray); | |
170 WebBindings::releaseObject(resultArray); | |
171 } | |
172 | |
173 void TextInputController::setComposition(const CppArgumentList& arguments, CppVa
riant* result) | |
174 { | |
175 result->setNull(); | |
176 | |
177 if (arguments.size() < 1) | |
178 return; | |
179 | |
180 // Sends a keydown event with key code = 0xE5 to emulate input method behavi
or. | |
181 WebKeyboardEvent keyDown; | |
182 keyDown.type = WebInputEvent::RawKeyDown; | |
183 keyDown.modifiers = 0; | |
184 keyDown.windowsKeyCode = 0xE5; // VKEY_PROCESSKEY | |
185 keyDown.setKeyIdentifierFromWindowsKeyCode(); | |
186 m_webView->handleInputEvent(keyDown); | |
187 | |
188 WebVector<WebCompositionUnderline> underlines; | |
189 WebString text(WebString::fromUTF8(arguments[0].toString())); | |
190 m_webView->setComposition(text, underlines, 0, text.length()); | |
191 } | |
192 | |
193 } | |
OLD | NEW |