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

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

Issue 1889053003: Fix InputConnection.deleteSurroundingText() (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 2 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/Document.h" 7 #include "core/dom/Document.h"
8 #include "core/dom/Element.h" 8 #include "core/dom/Element.h"
9 #include "core/dom/Range.h" 9 #include "core/dom/Range.h"
10 #include "core/editing/Editor.h" 10 #include "core/editing/Editor.h"
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after
195 "<input id='sample' type='password' size='24'>", "sample")); 195 "<input id='sample' type='password' size='24'>", "sample"));
196 196
197 Vector<CompositionUnderline> underlines; 197 Vector<CompositionUnderline> underlines;
198 underlines.append(CompositionUnderline(0, 5, Color(255, 0, 0), false, 0)); 198 underlines.append(CompositionUnderline(0, 5, Color(255, 0, 0), false, 0));
199 controller().setComposition("foo", underlines, 0, 3); 199 controller().setComposition("foo", underlines, 0, 3);
200 controller().finishComposingText(InputMethodController::KeepSelection); 200 controller().finishComposingText(InputMethodController::KeepSelection);
201 201
202 EXPECT_STREQ("foo", input->value().utf8().data()); 202 EXPECT_STREQ("foo", input->value().utf8().data());
203 } 203 }
204 204
205 TEST_F(InputMethodControllerTest, DeleteSurroundingTextWithEmptyText) {
206 HTMLInputElement* input =
207 toHTMLInputElement(insertHTMLElement("<input id='sample'>", "sample"));
208
209 input->setValue("");
210 document().updateStyleAndLayout();
211 EXPECT_STREQ("", input->value().utf8().data());
212 controller().deleteSurroundingText(0, 0);
213 EXPECT_STREQ("", input->value().utf8().data());
214
215 input->setValue("");
216 document().updateStyleAndLayout();
217 EXPECT_STREQ("", input->value().utf8().data());
218 controller().deleteSurroundingText(1, 0);
219 EXPECT_STREQ("", input->value().utf8().data());
220
221 input->setValue("");
222 document().updateStyleAndLayout();
223 EXPECT_STREQ("", input->value().utf8().data());
224 controller().deleteSurroundingText(0, 1);
225 EXPECT_STREQ("", input->value().utf8().data());
226
227 input->setValue("");
228 document().updateStyleAndLayout();
229 EXPECT_STREQ("", input->value().utf8().data());
230 controller().deleteSurroundingText(1, 1);
231 EXPECT_STREQ("", input->value().utf8().data());
232 }
233
234 TEST_F(InputMethodControllerTest, DeleteSurroundingTextWithRangeSelection) {
235 HTMLInputElement* input =
236 toHTMLInputElement(insertHTMLElement("<input id='sample'>", "sample"));
237
238 input->setValue("hello");
239 document().updateStyleAndLayout();
240 EXPECT_STREQ("hello", input->value().utf8().data());
241 controller().setEditableSelectionOffsets(PlainTextRange(1, 4));
242 controller().deleteSurroundingText(0, 0);
243 EXPECT_STREQ("hello", input->value().utf8().data());
244
245 input->setValue("hello");
246 document().updateStyleAndLayout();
247 EXPECT_STREQ("hello", input->value().utf8().data());
248 controller().setEditableSelectionOffsets(PlainTextRange(1, 4));
249 controller().deleteSurroundingText(1, 1);
250 EXPECT_STREQ("ell", input->value().utf8().data());
251
252 input->setValue("hello");
253 document().updateStyleAndLayout();
254 EXPECT_STREQ("hello", input->value().utf8().data());
255 controller().setEditableSelectionOffsets(PlainTextRange(1, 4));
256 controller().deleteSurroundingText(100, 0);
257 EXPECT_STREQ("ello", input->value().utf8().data());
258
259 input->setValue("hello");
260 document().updateStyleAndLayout();
261 EXPECT_STREQ("hello", input->value().utf8().data());
262 controller().setEditableSelectionOffsets(PlainTextRange(1, 4));
263 controller().deleteSurroundingText(0, 100);
264 EXPECT_STREQ("hell", input->value().utf8().data());
265
266 input->setValue("hello");
267 document().updateStyleAndLayout();
268 EXPECT_STREQ("hello", input->value().utf8().data());
269 controller().setEditableSelectionOffsets(PlainTextRange(1, 4));
270 controller().deleteSurroundingText(100, 100);
271 EXPECT_STREQ("ell", input->value().utf8().data());
272 }
273
274 TEST_F(InputMethodControllerTest, DeleteSurroundingTextWithCursorSelection) {
275 HTMLInputElement* input =
276 toHTMLInputElement(insertHTMLElement("<input id='sample'>", "sample"));
277
278 input->setValue("hello");
279 document().updateStyleAndLayout();
280 EXPECT_STREQ("hello", input->value().utf8().data());
281 controller().setEditableSelectionOffsets(PlainTextRange(2, 2));
282 controller().deleteSurroundingText(1, 0);
283 EXPECT_STREQ("hllo", input->value().utf8().data());
284
285 input->setValue("hello");
286 document().updateStyleAndLayout();
287 EXPECT_STREQ("hello", input->value().utf8().data());
288 controller().setEditableSelectionOffsets(PlainTextRange(2, 2));
289 controller().deleteSurroundingText(0, 1);
290 EXPECT_STREQ("helo", input->value().utf8().data());
291
292 input->setValue("hello");
293 document().updateStyleAndLayout();
294 EXPECT_STREQ("hello", input->value().utf8().data());
295 controller().setEditableSelectionOffsets(PlainTextRange(2, 2));
296 controller().deleteSurroundingText(0, 0);
297 EXPECT_STREQ("hello", input->value().utf8().data());
298
299 input->setValue("hello");
300 document().updateStyleAndLayout();
301 EXPECT_STREQ("hello", input->value().utf8().data());
302 controller().setEditableSelectionOffsets(PlainTextRange(2, 2));
303 controller().deleteSurroundingText(1, 1);
304 EXPECT_STREQ("hlo", input->value().utf8().data());
305
306 input->setValue("hello");
307 document().updateStyleAndLayout();
308 EXPECT_STREQ("hello", input->value().utf8().data());
309 controller().setEditableSelectionOffsets(PlainTextRange(2, 2));
310 controller().deleteSurroundingText(100, 0);
311 EXPECT_STREQ("llo", input->value().utf8().data());
312
313 input->setValue("hello");
314 document().updateStyleAndLayout();
315 EXPECT_STREQ("hello", input->value().utf8().data());
316 controller().setEditableSelectionOffsets(PlainTextRange(2, 2));
317 controller().deleteSurroundingText(0, 100);
318 EXPECT_STREQ("he", input->value().utf8().data());
319
320 input->setValue("hello");
321 document().updateStyleAndLayout();
322 EXPECT_STREQ("hello", input->value().utf8().data());
323 controller().setEditableSelectionOffsets(PlainTextRange(2, 2));
324 controller().deleteSurroundingText(100, 100);
325 EXPECT_STREQ("", input->value().utf8().data());
326
327 input->setValue("h");
328 document().updateStyleAndLayout();
329 EXPECT_STREQ("h", input->value().utf8().data());
330 controller().setEditableSelectionOffsets(PlainTextRange(1, 1));
331 controller().deleteSurroundingText(1, 0);
332 EXPECT_STREQ("", input->value().utf8().data());
333
334 input->setValue("h");
335 document().updateStyleAndLayout();
336 EXPECT_STREQ("h", input->value().utf8().data());
337 controller().setEditableSelectionOffsets(PlainTextRange(0, 0));
338 controller().deleteSurroundingText(0, 1);
339 EXPECT_STREQ("", input->value().utf8().data());
340 }
341
342 TEST_F(InputMethodControllerTest,
343 DeleteSurroundingTextWithMultiCodeTextOnTheLeft) {
344 HTMLInputElement* input =
345 toHTMLInputElement(insertHTMLElement("<input id='sample'>", "sample"));
346
347 // U+2605 == "black star". It takes up 1 space.
348 input->setValue(String::fromUTF8("foo\xE2\x98\x85"));
349 document().updateStyleAndLayout();
350 controller().setEditableSelectionOffsets(PlainTextRange(4, 4));
351 EXPECT_STREQ("foo\xE2\x98\x85", input->value().utf8().data());
352 controller().deleteSurroundingText(1, 0);
353 EXPECT_STREQ("foo", input->value().utf8().data());
354
355 // U+1F3C6 == "trophy". It takes up 2 space.
356 input->setValue(String::fromUTF8("foo\xF0\x9F\x8F\x86"));
357 document().updateStyleAndLayout();
358 controller().setEditableSelectionOffsets(PlainTextRange(5, 5));
359 EXPECT_STREQ("foo\xF0\x9F\x8F\x86", input->value().utf8().data());
360 controller().deleteSurroundingText(1, 0);
361 EXPECT_STREQ("foo", input->value().utf8().data());
362
363 // composed U+0E01 "ka kai" + U+0E49 "mai tho". It takes up 2 space.
364 input->setValue(String::fromUTF8("foo\xE0\xB8\x81\xE0\xB9\x89"));
365 document().updateStyleAndLayout();
366 controller().setEditableSelectionOffsets(PlainTextRange(5, 5));
367 EXPECT_STREQ("foo\xE0\xB8\x81\xE0\xB9\x89", input->value().utf8().data());
368 controller().deleteSurroundingText(1, 0);
369 EXPECT_STREQ("foo", input->value().utf8().data());
370
371 // "trophy" + "trophy".
372 input->setValue(String::fromUTF8("foo\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86"));
373 document().updateStyleAndLayout();
374 controller().setEditableSelectionOffsets(PlainTextRange(7, 7));
375 EXPECT_STREQ("foo\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86",
376 input->value().utf8().data());
377 controller().deleteSurroundingText(2, 0);
378 EXPECT_STREQ("foo\xF0\x9F\x8F\x86", input->value().utf8().data());
379
380 // "trophy" + "trophy".
381 input->setValue(String::fromUTF8("foo\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86"));
382 document().updateStyleAndLayout();
383 controller().setEditableSelectionOffsets(PlainTextRange(7, 7));
384 EXPECT_STREQ("foo\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86",
385 input->value().utf8().data());
386 controller().deleteSurroundingText(3, 0);
387 EXPECT_STREQ("foo", input->value().utf8().data());
388
389 // "trophy" + "trophy".
390 input->setValue(String::fromUTF8("foo\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86"));
391 document().updateStyleAndLayout();
392 controller().setEditableSelectionOffsets(PlainTextRange(7, 7));
393 EXPECT_STREQ("foo\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86",
394 input->value().utf8().data());
395 controller().deleteSurroundingText(4, 0);
396 EXPECT_STREQ("foo", input->value().utf8().data());
397
398 // "trophy" + "trophy".
399 input->setValue(String::fromUTF8("foo\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86"));
400 document().updateStyleAndLayout();
401 controller().setEditableSelectionOffsets(PlainTextRange(7, 7));
402 EXPECT_STREQ("foo\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86",
403 input->value().utf8().data());
404 controller().deleteSurroundingText(5, 0);
405 EXPECT_STREQ("fo", input->value().utf8().data());
406 }
407
408 TEST_F(InputMethodControllerTest,
409 DeleteSurroundingTextWithMultiCodeTextOnTheRight) {
410 HTMLInputElement* input =
411 toHTMLInputElement(insertHTMLElement("<input id='sample'>", "sample"));
412
413 // U+2605 == "black star". It takes up 1 space.
414 input->setValue(String::fromUTF8("\xE2\x98\x85 foo"));
415 document().updateStyleAndLayout();
416 controller().setEditableSelectionOffsets(PlainTextRange(0, 0));
417 EXPECT_STREQ("\xE2\x98\x85 foo", input->value().utf8().data());
418 controller().deleteSurroundingText(0, 1);
419 EXPECT_STREQ(" foo", input->value().utf8().data());
420
421 // U+1F3C6 == "trophy". It takes up 2 space.
422 input->setValue(String::fromUTF8("\xF0\x9F\x8F\x86 foo"));
423 document().updateStyleAndLayout();
424 controller().setEditableSelectionOffsets(PlainTextRange(0, 0));
425 EXPECT_STREQ("\xF0\x9F\x8F\x86 foo", input->value().utf8().data());
426 controller().deleteSurroundingText(0, 1);
427 EXPECT_STREQ(" foo", input->value().utf8().data());
428
429 // composed U+0E01 "ka kai" + U+0E49 "mai tho". It takes up 2 space.
430 input->setValue(String::fromUTF8("\xE0\xB8\x81\xE0\xB9\x89 foo"));
431 document().updateStyleAndLayout();
432 controller().setEditableSelectionOffsets(PlainTextRange(0, 0));
433 EXPECT_STREQ("\xE0\xB8\x81\xE0\xB9\x89 foo", input->value().utf8().data());
434 controller().deleteSurroundingText(0, 1);
435 EXPECT_STREQ(" foo", input->value().utf8().data());
436
437 // "trophy" + "trophy".
438 input->setValue(String::fromUTF8("\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86 foo"));
439 document().updateStyleAndLayout();
440 controller().setEditableSelectionOffsets(PlainTextRange(0, 0));
441 EXPECT_STREQ("\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86 foo",
442 input->value().utf8().data());
443 controller().deleteSurroundingText(0, 2);
444 EXPECT_STREQ("\xF0\x9F\x8F\x86 foo", input->value().utf8().data());
445
446 // "trophy" + "trophy".
447 input->setValue(String::fromUTF8("\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86 foo"));
448 document().updateStyleAndLayout();
449 controller().setEditableSelectionOffsets(PlainTextRange(0, 0));
450 EXPECT_STREQ("\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86 foo",
451 input->value().utf8().data());
452 controller().deleteSurroundingText(0, 3);
453 EXPECT_STREQ(" foo", input->value().utf8().data());
454
455 // "trophy" + "trophy".
456 input->setValue(String::fromUTF8("\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86 foo"));
457 document().updateStyleAndLayout();
458 controller().setEditableSelectionOffsets(PlainTextRange(0, 0));
459 EXPECT_STREQ("\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86 foo",
460 input->value().utf8().data());
461 controller().deleteSurroundingText(0, 4);
462 EXPECT_STREQ(" foo", input->value().utf8().data());
463
464 // "trophy" + "trophy".
465 input->setValue(String::fromUTF8("\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86 foo"));
466 document().updateStyleAndLayout();
467 controller().setEditableSelectionOffsets(PlainTextRange(0, 0));
468 EXPECT_STREQ("\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86 foo",
469 input->value().utf8().data());
470 controller().deleteSurroundingText(0, 5);
471 EXPECT_STREQ("foo", input->value().utf8().data());
472 }
473
474 TEST_F(InputMethodControllerTest,
475 DeleteSurroundingTextWithMultiCodeTextOnBothSides) {
476 HTMLInputElement* input =
477 toHTMLInputElement(insertHTMLElement("<input id='sample'>", "sample"));
478
479 // "trophy" + "trophy".
480 input->setValue(String::fromUTF8("\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86"));
481 document().updateStyleAndLayout();
482 controller().setEditableSelectionOffsets(PlainTextRange(2, 2));
483 EXPECT_STREQ("\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86",
484 input->value().utf8().data());
485 controller().deleteSurroundingText(1, 1);
486 EXPECT_STREQ("", input->value().utf8().data());
487 }
488
489 TEST_F(InputMethodControllerTest, DeleteSurroundingTextForMultipleNodes) {
490 Element* div = insertHTMLElement(
491 "<div id='sample' contenteditable='true'>aaa"
492 "<div id='sample2' contenteditable='true'>bbb"
493 "<div id='sample3' contenteditable='true'>ccc"
494 "<div id='sample4' contenteditable='true'>ddd"
495 "<div id='sample5' contenteditable='true'>eee"
496 "</div></div></div></div></div>",
497 "sample");
498
499 controller().setEditableSelectionOffsets(PlainTextRange(8, 8));
500 EXPECT_STREQ("aaa\nbbb\nccc\nddd\neee", div->innerText().utf8().data());
501 EXPECT_EQ(8u, controller().getSelectionOffsets().start());
502 EXPECT_EQ(8u, controller().getSelectionOffsets().end());
503
504 controller().deleteSurroundingText(1, 0);
505 EXPECT_STREQ("aaa\nbbbccc\nddd\neee", div->innerText().utf8().data());
506 EXPECT_EQ(7u, controller().getSelectionOffsets().start());
507 EXPECT_EQ(7u, controller().getSelectionOffsets().end());
508
509 controller().deleteSurroundingText(0, 4);
510 EXPECT_STREQ("aaa\nbbbddd\neee", div->innerText().utf8().data());
511 EXPECT_EQ(7u, controller().getSelectionOffsets().start());
512 EXPECT_EQ(7u, controller().getSelectionOffsets().end());
513
514 controller().deleteSurroundingText(5, 5);
515 EXPECT_STREQ("aaee", div->innerText().utf8().data());
516 EXPECT_EQ(2u, controller().getSelectionOffsets().start());
517 EXPECT_EQ(2u, controller().getSelectionOffsets().end());
518 }
519
205 TEST_F(InputMethodControllerTest, SetCompositionForInputWithNewCaretPositions) { 520 TEST_F(InputMethodControllerTest, SetCompositionForInputWithNewCaretPositions) {
206 HTMLInputElement* input = 521 HTMLInputElement* input =
207 toHTMLInputElement(insertHTMLElement("<input id='sample'>", "sample")); 522 toHTMLInputElement(insertHTMLElement("<input id='sample'>", "sample"));
208 523
209 input->setValue("hello"); 524 input->setValue("hello");
210 document().updateStyleAndLayout(); 525 document().updateStyleAndLayout();
211 controller().setEditableSelectionOffsets(PlainTextRange(2, 2)); 526 controller().setEditableSelectionOffsets(PlainTextRange(2, 2));
212 EXPECT_STREQ("hello", input->value().utf8().data()); 527 EXPECT_STREQ("hello", input->value().utf8().data());
213 EXPECT_EQ(2u, controller().getSelectionOffsets().start()); 528 EXPECT_EQ(2u, controller().getSelectionOffsets().start());
214 EXPECT_EQ(2u, controller().getSelectionOffsets().end()); 529 EXPECT_EQ(2u, controller().getSelectionOffsets().end());
(...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after
488 EXPECT_STREQ("beforeinput.data:ni;input.data:ni;", 803 EXPECT_STREQ("beforeinput.data:ni;input.data:ni;",
489 document().title().utf8().data()); 804 document().title().utf8().data());
490 805
491 document().setTitle(emptyString()); 806 document().setTitle(emptyString());
492 controller().finishComposingText(InputMethodController::KeepSelection); 807 controller().finishComposingText(InputMethodController::KeepSelection);
493 EXPECT_STREQ("beforeinput.data:ni;input.data:ni;", 808 EXPECT_STREQ("beforeinput.data:ni;input.data:ni;",
494 document().title().utf8().data()); 809 document().title().utf8().data());
495 } 810 }
496 811
497 } // namespace blink 812 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698