Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(366)

Side by Side Diff: content/test/layout_tests/runner/TextInputController.cpp

Issue 110533009: Import TestRunner library into chromium. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: updates Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright 2013 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 /*
6 * Copyright (C) 2010 Google Inc. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following disclaimer
16 * in the documentation and/or other materials provided with the
17 * distribution.
18 * * Neither the name of Google Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived from
20 * this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 #include "content/test/layout_tests/runner/TextInputController.h"
36
37 #include "content/test/layout_tests/runner/TestCommon.h"
38 #include "third_party/WebKit/public/platform/WebString.h"
39 #include "third_party/WebKit/public/platform/WebVector.h"
40 #include "third_party/WebKit/public/web/WebBindings.h"
41 #include "third_party/WebKit/public/web/WebCompositionUnderline.h"
42 #include "third_party/WebKit/public/web/WebFrame.h"
43 #include "third_party/WebKit/public/web/WebInputEvent.h"
44 #include "third_party/WebKit/public/web/WebRange.h"
45 #include "third_party/WebKit/public/web/WebView.h"
46 #include <string>
47
48 using namespace blink;
49 using namespace std;
50
51 namespace WebTestRunner {
52
53 TextInputController::TextInputController()
54 {
55 bindMethod("doCommand", &TextInputController::doCommand);
56 bindMethod("firstRectForCharacterRange", &TextInputController::firstRectForC haracterRange);
57 bindMethod("hasMarkedText", &TextInputController::hasMarkedText);
58 bindMethod("insertText", &TextInputController::insertText);
59 bindMethod("markedRange", &TextInputController::markedRange);
60 bindMethod("selectedRange", &TextInputController::selectedRange);
61 bindMethod("setMarkedText", &TextInputController::setMarkedText);
62 bindMethod("unmarkText", &TextInputController::unmarkText);
63 bindMethod("setComposition", &TextInputController::setComposition);
64 }
65
66 void TextInputController::insertText(const CppArgumentList& arguments, CppVarian t* result)
67 {
68 result->setNull();
69
70 if (arguments.size() < 1 || !arguments[0].isString())
71 return;
72
73 m_webView->confirmComposition(WebString::fromUTF8(arguments[0].toString()));
74 }
75
76 void TextInputController::doCommand(const CppArgumentList& arguments, CppVariant * result)
77 {
78 result->setNull();
79
80 WebFrame* mainFrame = m_webView->mainFrame();
81 if (!mainFrame)
82 return;
83
84 if (arguments.size() >= 1 && arguments[0].isString())
85 mainFrame->executeCommand(WebString::fromUTF8(arguments[0].toString()));
86 }
87
88 void TextInputController::setMarkedText(const CppArgumentList& arguments, CppVar iant* result)
89 {
90 result->setNull();
91
92 if (arguments.size() < 3 || !arguments[0].isString()
93 || !arguments[1].isNumber() || !arguments[2].isNumber())
94 return;
95
96 WebString text(WebString::fromUTF8(arguments[0].toString()));
97 int start = arguments[1].toInt32();
98 int length = arguments[2].toInt32();
99
100 // Split underline into up to 3 elements (before, selection, and after).
101 vector<WebCompositionUnderline> underlines;
102 WebCompositionUnderline underline;
103 if (!start) {
104 underline.endOffset = length;
105 } else {
106 underline.endOffset = start;
107 underlines.push_back(underline);
108 underline.startOffset = start;
109 underline.endOffset = start + length;
110 }
111 underline.thick = true;
112 underlines.push_back(underline);
113 if (start + length < static_cast<int>(text.length())) {
114 underline.startOffset = underline.endOffset;
115 underline.endOffset = text.length();
116 underline.thick = false;
117 underlines.push_back(underline);
118 }
119
120 m_webView->setComposition(text, underlines, start, start + length);
121 }
122
123 void TextInputController::unmarkText(const CppArgumentList&, CppVariant* result)
124 {
125 result->setNull();
126
127 m_webView->confirmComposition();
128 }
129
130 void TextInputController::hasMarkedText(const CppArgumentList&, CppVariant* resu lt)
131 {
132 result->setNull();
133
134 WebFrame* mainFrame = m_webView->mainFrame();
135 if (!mainFrame)
136 return;
137
138 result->set(mainFrame->hasMarkedText());
139 }
140
141 void TextInputController::markedRange(const CppArgumentList&, CppVariant* result )
142 {
143 result->setNull();
144
145 WebFrame* mainFrame = m_webView->mainFrame();
146 if (!mainFrame)
147 return;
148
149 WebRange range = mainFrame->markedRange();
150 vector<int> intArray(2);
151 intArray[0] = range.startOffset();
152 intArray[1] = range.endOffset();
153
154 NPObject* resultArray = WebBindings::makeIntArray(intArray);
155 result->set(resultArray);
156 WebBindings::releaseObject(resultArray);
157 }
158
159 void TextInputController::selectedRange(const CppArgumentList&, CppVariant* resu lt)
160 {
161 result->setNull();
162
163 WebFrame* mainFrame = m_webView->mainFrame();
164 if (!mainFrame)
165 return;
166
167 WebRange range = mainFrame->selectionRange();
168 vector<int> intArray(2);
169 intArray[0] = range.startOffset();
170 intArray[1] = range.endOffset();
171
172 NPObject* resultArray = WebBindings::makeIntArray(intArray);
173 result->set(resultArray);
174 WebBindings::releaseObject(resultArray);
175 }
176
177 void TextInputController::firstRectForCharacterRange(const CppArgumentList& argu ments, CppVariant* result)
178 {
179 result->setNull();
180
181 WebFrame* frame = m_webView->focusedFrame();
182 if (!frame)
183 return;
184
185 if (arguments.size() < 2 || !arguments[0].isNumber() || !arguments[1].isNumb er())
186 return;
187
188 WebRect rect;
189 if (!frame->firstRectForCharacterRange(arguments[0].toInt32(), arguments[1]. toInt32(), rect))
190 return;
191
192 vector<int> intArray(4);
193 intArray[0] = rect.x;
194 intArray[1] = rect.y;
195 intArray[2] = rect.width;
196 intArray[3] = rect.height;
197
198 NPObject* resultArray = WebBindings::makeIntArray(intArray);
199 result->set(resultArray);
200 WebBindings::releaseObject(resultArray);
201 }
202
203 void TextInputController::setComposition(const CppArgumentList& arguments, CppVa riant* result)
204 {
205 result->setNull();
206
207 if (arguments.size() < 1)
208 return;
209
210 // Sends a keydown event with key code = 0xE5 to emulate input method behavi or.
211 WebKeyboardEvent keyDown;
212 keyDown.type = WebInputEvent::RawKeyDown;
213 keyDown.modifiers = 0;
214 keyDown.windowsKeyCode = 0xE5; // VKEY_PROCESSKEY
215 keyDown.setKeyIdentifierFromWindowsKeyCode();
216 m_webView->handleInputEvent(keyDown);
217
218 WebVector<WebCompositionUnderline> underlines;
219 WebString text(WebString::fromUTF8(arguments[0].toString()));
220 m_webView->setComposition(text, underlines, 0, text.length());
221 }
222
223 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698