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

Side by Side Diff: third_party/WebKit/Source/core/editing/InputMethodControllerTest.cpp

Issue 1847583003: Fix setComposingText when newCursorPosition != 1 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Without changing PlainTextRange Created 4 years, 7 months 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "core/editing/InputMethodController.h" 5 #include "core/editing/InputMethodController.h"
6 6
7 #include "core/dom/Element.h" 7 #include "core/dom/Element.h"
8 #include "core/dom/Range.h" 8 #include "core/dom/Range.h"
9 #include "core/editing/FrameSelection.h" 9 #include "core/editing/FrameSelection.h"
10 #include "core/events/MouseEvent.h" 10 #include "core/events/MouseEvent.h"
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after
186 insertHTMLElement("<input id='sample' type='password' size='24'>", "samp le")); 186 insertHTMLElement("<input id='sample' type='password' size='24'>", "samp le"));
187 187
188 Vector<CompositionUnderline> underlines; 188 Vector<CompositionUnderline> underlines;
189 underlines.append(CompositionUnderline(0, 5, Color(255, 0, 0), false, 0)); 189 underlines.append(CompositionUnderline(0, 5, Color(255, 0, 0), false, 0));
190 controller().setComposition("foo", underlines, 0, 3); 190 controller().setComposition("foo", underlines, 0, 3);
191 controller().confirmComposition(); 191 controller().confirmComposition();
192 192
193 EXPECT_STREQ("foo", input->value().utf8().data()); 193 EXPECT_STREQ("foo", input->value().utf8().data());
194 } 194 }
195 195
196 TEST_F(InputMethodControllerTest, SetCompositionForInputWithDifferentNewCursorPo sitions)
197 {
198 HTMLInputElement* input = toHTMLInputElement(
199 insertHTMLElement("<input id='sample'>", "sample"));
200
201 input->setValue("hello");
202 controller().setEditableSelectionOffsets(PlainTextRange(2, 2));
203 EXPECT_STREQ("hello", input->value().utf8().data());
204 EXPECT_EQ(2, frame().selection().start().computeOffsetInContainerNode());
205 EXPECT_EQ(2, frame().selection().end().computeOffsetInContainerNode());
206
207 Vector<CompositionUnderline> underlines;
208 underlines.append(CompositionUnderline(0, 2, Color(255, 0, 0), false, 0));
209
210 // The cursor exceeds left boundary.
211 // "*heABllo", where * stands for cursor.
212 controller().setComposition("AB", underlines, -100, -100);
213 EXPECT_STREQ("heABllo", input->value().utf8().data());
214 EXPECT_EQ(0, frame().selection().start().computeOffsetInContainerNode());
215 EXPECT_EQ(0, frame().selection().end().computeOffsetInContainerNode());
216
217 // The cursor is on left boundary.
218 // "*heABllo".
219 controller().setComposition("AB", underlines, -2, -2);
220 EXPECT_STREQ("heABllo", input->value().utf8().data());
221 EXPECT_EQ(0, frame().selection().start().computeOffsetInContainerNode());
222 EXPECT_EQ(0, frame().selection().end().computeOffsetInContainerNode());
223
224 // The cursor is before the composing text.
225 // "he*ABllo".
226 controller().setComposition("AB", underlines, 0, 0);
227 EXPECT_STREQ("heABllo", input->value().utf8().data());
228 EXPECT_EQ(2, frame().selection().start().computeOffsetInContainerNode());
229 EXPECT_EQ(2, frame().selection().end().computeOffsetInContainerNode());
230
231 // The cursor is after the composing text.
232 // "heAB*llo".
233 controller().setComposition("AB", underlines, 2, 2);
234 EXPECT_STREQ("heABllo", input->value().utf8().data());
235 EXPECT_EQ(4, frame().selection().start().computeOffsetInContainerNode());
236 EXPECT_EQ(4, frame().selection().end().computeOffsetInContainerNode());
237
238 // The cursor is on right boundary.
239 // "heABllo*".
240 controller().setComposition("AB", underlines, 5, 5);
241 EXPECT_STREQ("heABllo", input->value().utf8().data());
242 EXPECT_EQ(7, frame().selection().start().computeOffsetInContainerNode());
243 EXPECT_EQ(7, frame().selection().end().computeOffsetInContainerNode());
244
245 // The cursor exceeds right boundary.
246 // "heABllo*".
247 controller().setComposition("AB", underlines, 100, 100);
248 EXPECT_STREQ("heABllo", input->value().utf8().data());
249 EXPECT_EQ(7, frame().selection().start().computeOffsetInContainerNode());
250 EXPECT_EQ(7, frame().selection().end().computeOffsetInContainerNode());
251 }
252
253 TEST_F(InputMethodControllerTest, SetCompositionForEditableWithDifferentNewCurso rPositions)
254 {
255 // There are 3 nodes and 5+1+6 characters: "hello", '\n', "world!".
256 Element* div = insertHTMLElement(
257 "<div id='sample' contenteditable='true'>"
258 "hello"
259 "<p id='sample2' contenteditable='true'>world!</p>"
260 "</div>",
261 "sample");
262
263 controller().setEditableSelectionOffsets(PlainTextRange(2, 2));
264 EXPECT_STREQ("hello\nworld!", div->innerText().utf8().data());
265 EXPECT_EQ(2, frame().selection().start().computeOffsetInContainerNode());
266 EXPECT_EQ(2, frame().selection().end().computeOffsetInContainerNode());
267
268 Vector<CompositionUnderline> underlines;
269 underlines.append(CompositionUnderline(0, 2, Color(255, 0, 0), false, 0));
270
271 // The cursor exceeds left boundary.
272 // "*heABllo\nworld!", where * stands for cursor.
273 controller().setComposition("AB", underlines, -100, -100);
274 EXPECT_STREQ("heABllo\nworld!", div->innerText().utf8().data());
275 EXPECT_EQ(0, frame().selection().start().computeOffsetInContainerNode());
276 EXPECT_EQ(0, frame().selection().end().computeOffsetInContainerNode());
277
278 // The cursor is on left boundary.
279 // "*heABllo\nworld!"
280 controller().setComposition("AB", underlines, -2, -2);
281 EXPECT_STREQ("heABllo\nworld!", div->innerText().utf8().data());
282 EXPECT_EQ(0, frame().selection().start().computeOffsetInContainerNode());
283 EXPECT_EQ(0, frame().selection().end().computeOffsetInContainerNode());
284
285 // The cursor is before the composing text.
286 // "he*ABllo\nworld!"
287 controller().setComposition("AB", underlines, 0, 0);
288 EXPECT_STREQ("heABllo\nworld!", div->innerText().utf8().data());
289 EXPECT_EQ(2, frame().selection().start().computeOffsetInContainerNode());
290 EXPECT_EQ(2, frame().selection().end().computeOffsetInContainerNode());
291
292 // The cursor is after the composing text.
293 // "heAB*llo\nworld!"
294 controller().setComposition("AB", underlines, 2, 2);
295 EXPECT_STREQ("heABllo\nworld!", div->innerText().utf8().data());
296 EXPECT_EQ(4, frame().selection().start().computeOffsetInContainerNode());
297 EXPECT_EQ(4, frame().selection().end().computeOffsetInContainerNode());
298
299 // The cursor is on right boundary of the 1st node.
300 // "heABllo*\nworld!"
301 controller().setComposition("AB", underlines, 5, 5);
302 EXPECT_STREQ("heABllo\nworld!", div->innerText().utf8().data());
303 EXPECT_EQ(7, frame().selection().start().computeOffsetInContainerNode());
304 EXPECT_EQ(7, frame().selection().end().computeOffsetInContainerNode());
305
306 // The cursor is on left boundary of the 3rd node.
307 // "heABllo\n*world!".
308 controller().setComposition("AB", underlines, 6, 6);
309 EXPECT_STREQ("heABllo\nworld!", div->innerText().utf8().data());
310 EXPECT_EQ(0, frame().selection().start().computeOffsetInContainerNode());
311 EXPECT_EQ(0, frame().selection().end().computeOffsetInContainerNode());
312
313 // The cursor is on right boundary of the 3rd node.
314 // "heABllo\nworld!*"
315 controller().setComposition("AB", underlines, 12, 12);
316 EXPECT_STREQ("heABllo\nworld!", div->innerText().utf8().data());
317 EXPECT_EQ(6, frame().selection().start().computeOffsetInContainerNode());
318 EXPECT_EQ(6, frame().selection().end().computeOffsetInContainerNode());
319
320 // The cursor exceeds right boundary.
321 // "heABllo\nworld!*"
322 controller().setComposition("AB", underlines, 100, 100);
323 EXPECT_STREQ("heABllo\nworld!", div->innerText().utf8().data());
324 EXPECT_EQ(6, frame().selection().start().computeOffsetInContainerNode());
325 EXPECT_EQ(6, frame().selection().end().computeOffsetInContainerNode());
326 }
327
196 TEST_F(InputMethodControllerTest, CompositionFireBeforeInput) 328 TEST_F(InputMethodControllerTest, CompositionFireBeforeInput)
197 { 329 {
198 document().settings()->setScriptEnabled(true); 330 document().settings()->setScriptEnabled(true);
199 Element* editable = insertHTMLElement("<div id='sample' contentEditable='tru e'></div>", "sample"); 331 Element* editable = insertHTMLElement("<div id='sample' contentEditable='tru e'></div>", "sample");
200 Element* script = document().createElement("script", ASSERT_NO_EXCEPTION); 332 Element* script = document().createElement("script", ASSERT_NO_EXCEPTION);
201 script->setInnerHTML( 333 script->setInnerHTML(
202 "document.getElementById('sample').addEventListener('beforeinput', funct ion(event) {" 334 "document.getElementById('sample').addEventListener('beforeinput', funct ion(event) {"
203 " document.title = `beforeinput.isComposing:${event.isComposing}`;" 335 " document.title = `beforeinput.isComposing:${event.isComposing}`;"
204 "});", 336 "});",
205 ASSERT_NO_EXCEPTION); 337 ASSERT_NO_EXCEPTION);
206 document().body()->appendChild(script, ASSERT_NO_EXCEPTION); 338 document().body()->appendChild(script, ASSERT_NO_EXCEPTION);
207 document().view()->updateAllLifecyclePhases(); 339 document().view()->updateAllLifecyclePhases();
208 340
209 // Simulate composition in the |contentEditable|. 341 // Simulate composition in the |contentEditable|.
210 Vector<CompositionUnderline> underlines; 342 Vector<CompositionUnderline> underlines;
211 underlines.append(CompositionUnderline(0, 5, Color(255, 0, 0), false, 0)); 343 underlines.append(CompositionUnderline(0, 5, Color(255, 0, 0), false, 0));
212 editable->focus(); 344 editable->focus();
213 345
214 document().setTitle(emptyString()); 346 document().setTitle(emptyString());
215 controller().setComposition("foo", underlines, 0, 3); 347 controller().setComposition("foo", underlines, 0, 3);
216 EXPECT_STREQ("beforeinput.isComposing:true", document().title().utf8().data( )); 348 EXPECT_STREQ("beforeinput.isComposing:true", document().title().utf8().data( ));
217 349
218 document().setTitle(emptyString()); 350 document().setTitle(emptyString());
219 controller().confirmComposition(); 351 controller().confirmComposition();
220 EXPECT_STREQ("beforeinput.isComposing:false", document().title().utf8().data ()); 352 EXPECT_STREQ("beforeinput.isComposing:false", document().title().utf8().data ());
221 } 353 }
222 354
223 } // namespace blink 355 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698