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

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: Address yosin@'s review 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 253 matching lines...) Expand 10 before | Expand all | Expand 10 after
264 "<input id='sample' type='password' size='24'>", "sample")); 264 "<input id='sample' type='password' size='24'>", "sample"));
265 265
266 Vector<CompositionUnderline> underlines; 266 Vector<CompositionUnderline> underlines;
267 underlines.append(CompositionUnderline(0, 5, Color(255, 0, 0), false, 0)); 267 underlines.append(CompositionUnderline(0, 5, Color(255, 0, 0), false, 0));
268 controller().setComposition("foo", underlines, 0, 3); 268 controller().setComposition("foo", underlines, 0, 3);
269 controller().finishComposingText(InputMethodController::KeepSelection); 269 controller().finishComposingText(InputMethodController::KeepSelection);
270 270
271 EXPECT_STREQ("foo", input->value().utf8().data()); 271 EXPECT_STREQ("foo", input->value().utf8().data());
272 } 272 }
273 273
274 TEST_F(InputMethodControllerTest, DeleteSurroundingTextWithEmptyText) {
275 HTMLInputElement* input =
276 toHTMLInputElement(insertHTMLElement("<input id='sample'>", "sample"));
277
278 input->setValue("");
279 document().updateStyleAndLayout();
280 EXPECT_STREQ("", input->value().utf8().data());
281 controller().deleteSurroundingText(0, 0);
282 EXPECT_STREQ("", input->value().utf8().data());
283
284 input->setValue("");
285 document().updateStyleAndLayout();
286 EXPECT_STREQ("", input->value().utf8().data());
287 controller().deleteSurroundingText(1, 0);
288 EXPECT_STREQ("", input->value().utf8().data());
289
290 input->setValue("");
291 document().updateStyleAndLayout();
292 EXPECT_STREQ("", input->value().utf8().data());
293 controller().deleteSurroundingText(0, 1);
294 EXPECT_STREQ("", input->value().utf8().data());
295
296 input->setValue("");
297 document().updateStyleAndLayout();
298 EXPECT_STREQ("", input->value().utf8().data());
299 controller().deleteSurroundingText(1, 1);
300 EXPECT_STREQ("", input->value().utf8().data());
301 }
302
303 TEST_F(InputMethodControllerTest, DeleteSurroundingTextWithRangeSelection) {
304 HTMLInputElement* input =
305 toHTMLInputElement(insertHTMLElement("<input id='sample'>", "sample"));
306
307 input->setValue("hello");
308 document().updateStyleAndLayout();
309 EXPECT_STREQ("hello", input->value().utf8().data());
310 controller().setEditableSelectionOffsets(PlainTextRange(1, 4));
311 controller().deleteSurroundingText(0, 0);
312 EXPECT_STREQ("hello", input->value().utf8().data());
313
314 input->setValue("hello");
315 document().updateStyleAndLayout();
316 EXPECT_STREQ("hello", input->value().utf8().data());
317 controller().setEditableSelectionOffsets(PlainTextRange(1, 4));
318 controller().deleteSurroundingText(1, 1);
319 EXPECT_STREQ("ell", input->value().utf8().data());
320
321 input->setValue("hello");
322 document().updateStyleAndLayout();
323 EXPECT_STREQ("hello", input->value().utf8().data());
324 controller().setEditableSelectionOffsets(PlainTextRange(1, 4));
325 controller().deleteSurroundingText(100, 0);
326 EXPECT_STREQ("ello", input->value().utf8().data());
327
328 input->setValue("hello");
329 document().updateStyleAndLayout();
330 EXPECT_STREQ("hello", input->value().utf8().data());
331 controller().setEditableSelectionOffsets(PlainTextRange(1, 4));
332 controller().deleteSurroundingText(0, 100);
333 EXPECT_STREQ("hell", input->value().utf8().data());
334
335 input->setValue("hello");
336 document().updateStyleAndLayout();
337 EXPECT_STREQ("hello", input->value().utf8().data());
338 controller().setEditableSelectionOffsets(PlainTextRange(1, 4));
339 controller().deleteSurroundingText(100, 100);
340 EXPECT_STREQ("ell", input->value().utf8().data());
341 }
342
343 TEST_F(InputMethodControllerTest, DeleteSurroundingTextWithCursorSelection) {
344 HTMLInputElement* input =
345 toHTMLInputElement(insertHTMLElement("<input id='sample'>", "sample"));
346
347 input->setValue("hello");
348 document().updateStyleAndLayout();
349 EXPECT_STREQ("hello", input->value().utf8().data());
350 controller().setEditableSelectionOffsets(PlainTextRange(2, 2));
351 controller().deleteSurroundingText(1, 0);
352 EXPECT_STREQ("hllo", input->value().utf8().data());
353
354 input->setValue("hello");
355 document().updateStyleAndLayout();
356 EXPECT_STREQ("hello", input->value().utf8().data());
357 controller().setEditableSelectionOffsets(PlainTextRange(2, 2));
358 controller().deleteSurroundingText(0, 1);
359 EXPECT_STREQ("helo", input->value().utf8().data());
360
361 input->setValue("hello");
362 document().updateStyleAndLayout();
363 EXPECT_STREQ("hello", input->value().utf8().data());
364 controller().setEditableSelectionOffsets(PlainTextRange(2, 2));
365 controller().deleteSurroundingText(0, 0);
366 EXPECT_STREQ("hello", input->value().utf8().data());
367
368 input->setValue("hello");
369 document().updateStyleAndLayout();
370 EXPECT_STREQ("hello", input->value().utf8().data());
371 controller().setEditableSelectionOffsets(PlainTextRange(2, 2));
372 controller().deleteSurroundingText(1, 1);
373 EXPECT_STREQ("hlo", input->value().utf8().data());
374
375 input->setValue("hello");
376 document().updateStyleAndLayout();
377 EXPECT_STREQ("hello", input->value().utf8().data());
378 controller().setEditableSelectionOffsets(PlainTextRange(2, 2));
379 controller().deleteSurroundingText(100, 0);
380 EXPECT_STREQ("llo", input->value().utf8().data());
381
382 input->setValue("hello");
383 document().updateStyleAndLayout();
384 EXPECT_STREQ("hello", input->value().utf8().data());
385 controller().setEditableSelectionOffsets(PlainTextRange(2, 2));
386 controller().deleteSurroundingText(0, 100);
387 EXPECT_STREQ("he", input->value().utf8().data());
388
389 input->setValue("hello");
390 document().updateStyleAndLayout();
391 EXPECT_STREQ("hello", input->value().utf8().data());
392 controller().setEditableSelectionOffsets(PlainTextRange(2, 2));
393 controller().deleteSurroundingText(100, 100);
394 EXPECT_STREQ("", input->value().utf8().data());
395
396 input->setValue("h");
397 document().updateStyleAndLayout();
398 EXPECT_STREQ("h", input->value().utf8().data());
399 controller().setEditableSelectionOffsets(PlainTextRange(1, 1));
400 controller().deleteSurroundingText(1, 0);
401 EXPECT_STREQ("", input->value().utf8().data());
402
403 input->setValue("h");
404 document().updateStyleAndLayout();
405 EXPECT_STREQ("h", input->value().utf8().data());
406 controller().setEditableSelectionOffsets(PlainTextRange(0, 0));
407 controller().deleteSurroundingText(0, 1);
408 EXPECT_STREQ("", input->value().utf8().data());
409 }
410
411 TEST_F(InputMethodControllerTest,
412 DeleteSurroundingTextWithMultiCodeTextOnTheLeft) {
413 HTMLInputElement* input =
414 toHTMLInputElement(insertHTMLElement("<input id='sample'>", "sample"));
415
416 // U+2605 == "black star". It takes up 1 space.
417 input->setValue(String::fromUTF8("foo\xE2\x98\x85"));
418 document().updateStyleAndLayout();
419 controller().setEditableSelectionOffsets(PlainTextRange(4, 4));
420 EXPECT_STREQ("foo\xE2\x98\x85", input->value().utf8().data());
421 controller().deleteSurroundingText(1, 0);
422 EXPECT_STREQ("foo", input->value().utf8().data());
423
424 // U+1F3C6 == "trophy". It takes up 2 space.
425 input->setValue(String::fromUTF8("foo\xF0\x9F\x8F\x86"));
426 document().updateStyleAndLayout();
427 controller().setEditableSelectionOffsets(PlainTextRange(5, 5));
428 EXPECT_STREQ("foo\xF0\x9F\x8F\x86", input->value().utf8().data());
429 controller().deleteSurroundingText(1, 0);
430 EXPECT_STREQ("foo", input->value().utf8().data());
431
432 // composed U+0E01 "ka kai" + U+0E49 "mai tho". It takes up 2 space.
433 input->setValue(String::fromUTF8("foo\xE0\xB8\x81\xE0\xB9\x89"));
434 document().updateStyleAndLayout();
435 controller().setEditableSelectionOffsets(PlainTextRange(5, 5));
436 EXPECT_STREQ("foo\xE0\xB8\x81\xE0\xB9\x89", input->value().utf8().data());
437 controller().deleteSurroundingText(1, 0);
438 EXPECT_STREQ("foo", input->value().utf8().data());
439
440 // "trophy" + "trophy".
441 input->setValue(String::fromUTF8("foo\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86"));
442 document().updateStyleAndLayout();
443 controller().setEditableSelectionOffsets(PlainTextRange(7, 7));
444 EXPECT_STREQ("foo\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86",
445 input->value().utf8().data());
446 controller().deleteSurroundingText(2, 0);
447 EXPECT_STREQ("foo\xF0\x9F\x8F\x86", input->value().utf8().data());
448
449 // "trophy" + "trophy".
450 input->setValue(String::fromUTF8("foo\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86"));
451 document().updateStyleAndLayout();
452 controller().setEditableSelectionOffsets(PlainTextRange(7, 7));
453 EXPECT_STREQ("foo\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86",
454 input->value().utf8().data());
455 controller().deleteSurroundingText(3, 0);
456 EXPECT_STREQ("foo", input->value().utf8().data());
457
458 // "trophy" + "trophy".
459 input->setValue(String::fromUTF8("foo\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86"));
460 document().updateStyleAndLayout();
461 controller().setEditableSelectionOffsets(PlainTextRange(7, 7));
462 EXPECT_STREQ("foo\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86",
463 input->value().utf8().data());
464 controller().deleteSurroundingText(4, 0);
465 EXPECT_STREQ("foo", input->value().utf8().data());
466
467 // "trophy" + "trophy".
468 input->setValue(String::fromUTF8("foo\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86"));
469 document().updateStyleAndLayout();
470 controller().setEditableSelectionOffsets(PlainTextRange(7, 7));
471 EXPECT_STREQ("foo\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86",
472 input->value().utf8().data());
473 controller().deleteSurroundingText(5, 0);
474 EXPECT_STREQ("fo", input->value().utf8().data());
475 }
476
477 TEST_F(InputMethodControllerTest,
478 DeleteSurroundingTextWithMultiCodeTextOnTheRight) {
479 HTMLInputElement* input =
480 toHTMLInputElement(insertHTMLElement("<input id='sample'>", "sample"));
481
482 // U+2605 == "black star". It takes up 1 space.
483 input->setValue(String::fromUTF8("\xE2\x98\x85 foo"));
484 document().updateStyleAndLayout();
485 controller().setEditableSelectionOffsets(PlainTextRange(0, 0));
486 EXPECT_STREQ("\xE2\x98\x85 foo", input->value().utf8().data());
487 controller().deleteSurroundingText(0, 1);
488 EXPECT_STREQ(" foo", input->value().utf8().data());
489
490 // U+1F3C6 == "trophy". It takes up 2 space.
491 input->setValue(String::fromUTF8("\xF0\x9F\x8F\x86 foo"));
492 document().updateStyleAndLayout();
493 controller().setEditableSelectionOffsets(PlainTextRange(0, 0));
494 EXPECT_STREQ("\xF0\x9F\x8F\x86 foo", input->value().utf8().data());
495 controller().deleteSurroundingText(0, 1);
496 EXPECT_STREQ(" foo", input->value().utf8().data());
497
498 // composed U+0E01 "ka kai" + U+0E49 "mai tho". It takes up 2 space.
499 input->setValue(String::fromUTF8("\xE0\xB8\x81\xE0\xB9\x89 foo"));
500 document().updateStyleAndLayout();
501 controller().setEditableSelectionOffsets(PlainTextRange(0, 0));
502 EXPECT_STREQ("\xE0\xB8\x81\xE0\xB9\x89 foo", input->value().utf8().data());
503 controller().deleteSurroundingText(0, 1);
504 EXPECT_STREQ(" foo", input->value().utf8().data());
505
506 // "trophy" + "trophy".
507 input->setValue(String::fromUTF8("\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86 foo"));
508 document().updateStyleAndLayout();
509 controller().setEditableSelectionOffsets(PlainTextRange(0, 0));
510 EXPECT_STREQ("\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86 foo",
511 input->value().utf8().data());
512 controller().deleteSurroundingText(0, 2);
513 EXPECT_STREQ("\xF0\x9F\x8F\x86 foo", input->value().utf8().data());
514
515 // "trophy" + "trophy".
516 input->setValue(String::fromUTF8("\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86 foo"));
517 document().updateStyleAndLayout();
518 controller().setEditableSelectionOffsets(PlainTextRange(0, 0));
519 EXPECT_STREQ("\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86 foo",
520 input->value().utf8().data());
521 controller().deleteSurroundingText(0, 3);
522 EXPECT_STREQ(" foo", input->value().utf8().data());
523
524 // "trophy" + "trophy".
525 input->setValue(String::fromUTF8("\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86 foo"));
526 document().updateStyleAndLayout();
527 controller().setEditableSelectionOffsets(PlainTextRange(0, 0));
528 EXPECT_STREQ("\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86 foo",
529 input->value().utf8().data());
530 controller().deleteSurroundingText(0, 4);
531 EXPECT_STREQ(" foo", input->value().utf8().data());
532
533 // "trophy" + "trophy".
534 input->setValue(String::fromUTF8("\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86 foo"));
535 document().updateStyleAndLayout();
536 controller().setEditableSelectionOffsets(PlainTextRange(0, 0));
537 EXPECT_STREQ("\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86 foo",
538 input->value().utf8().data());
539 controller().deleteSurroundingText(0, 5);
540 EXPECT_STREQ("foo", input->value().utf8().data());
541 }
542
543 TEST_F(InputMethodControllerTest,
544 DeleteSurroundingTextWithMultiCodeTextOnBothSides) {
545 HTMLInputElement* input =
546 toHTMLInputElement(insertHTMLElement("<input id='sample'>", "sample"));
547
548 // "trophy" + "trophy".
549 input->setValue(String::fromUTF8("\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86"));
550 document().updateStyleAndLayout();
551 controller().setEditableSelectionOffsets(PlainTextRange(2, 2));
552 EXPECT_STREQ("\xF0\x9F\x8F\x86\xF0\x9F\x8F\x86",
553 input->value().utf8().data());
554 controller().deleteSurroundingText(1, 1);
555 EXPECT_STREQ("", input->value().utf8().data());
556 }
557
558 TEST_F(InputMethodControllerTest, DeleteSurroundingTextForMultipleNodes) {
559 Element* div = insertHTMLElement(
560 "<div id='sample' contenteditable='true'>aaa"
561 "<div id='sample2' contenteditable='true'>bbb"
562 "<div id='sample3' contenteditable='true'>ccc"
563 "<div id='sample4' contenteditable='true'>ddd"
564 "<div id='sample5' contenteditable='true'>eee"
565 "</div></div></div></div></div>",
566 "sample");
567
568 controller().setEditableSelectionOffsets(PlainTextRange(8, 8));
569 EXPECT_STREQ("aaa\nbbb\nccc\nddd\neee", div->innerText().utf8().data());
570 EXPECT_EQ(8u, controller().getSelectionOffsets().start());
571 EXPECT_EQ(8u, controller().getSelectionOffsets().end());
572
573 controller().deleteSurroundingText(1, 0);
574 EXPECT_STREQ("aaa\nbbbccc\nddd\neee", div->innerText().utf8().data());
575 EXPECT_EQ(7u, controller().getSelectionOffsets().start());
576 EXPECT_EQ(7u, controller().getSelectionOffsets().end());
577
578 controller().deleteSurroundingText(0, 4);
579 EXPECT_STREQ("aaa\nbbbddd\neee", div->innerText().utf8().data());
580 EXPECT_EQ(7u, controller().getSelectionOffsets().start());
581 EXPECT_EQ(7u, controller().getSelectionOffsets().end());
582
583 controller().deleteSurroundingText(5, 5);
584 EXPECT_STREQ("aaee", div->innerText().utf8().data());
585 EXPECT_EQ(2u, controller().getSelectionOffsets().start());
586 EXPECT_EQ(2u, controller().getSelectionOffsets().end());
587 }
588
274 TEST_F(InputMethodControllerTest, SetCompositionForInputWithNewCaretPositions) { 589 TEST_F(InputMethodControllerTest, SetCompositionForInputWithNewCaretPositions) {
275 HTMLInputElement* input = 590 HTMLInputElement* input =
276 toHTMLInputElement(insertHTMLElement("<input id='sample'>", "sample")); 591 toHTMLInputElement(insertHTMLElement("<input id='sample'>", "sample"));
277 592
278 input->setValue("hello"); 593 input->setValue("hello");
279 document().updateStyleAndLayout(); 594 document().updateStyleAndLayout();
280 controller().setEditableSelectionOffsets(PlainTextRange(2, 2)); 595 controller().setEditableSelectionOffsets(PlainTextRange(2, 2));
281 EXPECT_STREQ("hello", input->value().utf8().data()); 596 EXPECT_STREQ("hello", input->value().utf8().data());
282 EXPECT_EQ(2u, controller().getSelectionOffsets().start()); 597 EXPECT_EQ(2u, controller().getSelectionOffsets().start());
283 EXPECT_EQ(2u, controller().getSelectionOffsets().end()); 598 EXPECT_EQ(2u, controller().getSelectionOffsets().end());
(...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after
557 EXPECT_STREQ("beforeinput.data:i;input.data:i;", 872 EXPECT_STREQ("beforeinput.data:i;input.data:i;",
558 document().title().utf8().data()); 873 document().title().utf8().data());
559 874
560 document().setTitle(emptyString()); 875 document().setTitle(emptyString());
561 controller().finishComposingText(InputMethodController::KeepSelection); 876 controller().finishComposingText(InputMethodController::KeepSelection);
562 EXPECT_STREQ("beforeinput.data:ni;input.data:ni;", 877 EXPECT_STREQ("beforeinput.data:ni;input.data:ni;",
563 document().title().utf8().data()); 878 document().title().utf8().data());
564 } 879 }
565 880
566 } // namespace blink 881 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698