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

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

Issue 2020973002: Reland: Fix setComposingText with empty text when newCursorPosition != 1 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 6 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 330 matching lines...) Expand 10 before | Expand all | Expand 10 after
341 EXPECT_EQ(24u, controller().getSelectionOffsets().end()); 341 EXPECT_EQ(24u, controller().getSelectionOffsets().end());
342 342
343 // The cursor exceeds right boundary. 343 // The cursor exceeds right boundary.
344 // "hello\nworld\n01234AB56789*". 344 // "hello\nworld\n01234AB56789*".
345 controller().setComposition("AB", underlines, 100, 100); 345 controller().setComposition("AB", underlines, 100, 100);
346 EXPECT_STREQ("hello\nworld\n01234AB56789", div->innerText().utf8().data()); 346 EXPECT_STREQ("hello\nworld\n01234AB56789", div->innerText().utf8().data());
347 EXPECT_EQ(24u, controller().getSelectionOffsets().start()); 347 EXPECT_EQ(24u, controller().getSelectionOffsets().start());
348 EXPECT_EQ(24u, controller().getSelectionOffsets().end()); 348 EXPECT_EQ(24u, controller().getSelectionOffsets().end());
349 } 349 }
350 350
351 TEST_F(InputMethodControllerTest, SetCompositionWithEmptyTextForDifferentNewCurs orPositions)
352 {
353 // There are 7 nodes and 5+1+5+1+3+4+3 characters: "hello", '\n', "world", " \n", "012", "3456", "789".
354 Element* div = insertHTMLElement(
355 "<div id='sample' contenteditable='true'>"
356 "hello"
357 "<div id='sample2' contenteditable='true'>world"
358 "<p>012<b>3456</b><i>789</i></p>"
359 "</div>"
360 "</div>",
361 "sample");
362
363 controller().setEditableSelectionOffsets(PlainTextRange(17, 17));
364 EXPECT_STREQ("hello\nworld\n0123456789", div->innerText().utf8().data());
365 EXPECT_EQ(17u, controller().getSelectionOffsets().start());
366 EXPECT_EQ(17u, controller().getSelectionOffsets().end());
367
368 Vector<CompositionUnderline> underlines;
369 underlines.append(CompositionUnderline(0, 0, Color(255, 0, 0), false, 0));
370
371 // The cursor exceeds left boundary.
372 // "*hello\nworld\n0123456789", where * stands for cursor.
373 controller().setComposition("", underlines, -100, -100);
374 EXPECT_STREQ("hello\nworld\n0123456789", div->innerText().utf8().data());
375 EXPECT_EQ(0u, controller().getSelectionOffsets().start());
376 EXPECT_EQ(0u, controller().getSelectionOffsets().end());
377
378 // The cursor exceeds right boundary.
379 // "hello\nworld\n0123456789*".
380 controller().setComposition("", underlines, 100, 100);
381 EXPECT_STREQ("hello\nworld\n0123456789", div->innerText().utf8().data());
382 EXPECT_EQ(22u, controller().getSelectionOffsets().start());
383 EXPECT_EQ(22u, controller().getSelectionOffsets().end());
384
385 // The cursor is on left boundary.
386 // "*hello\nworld\n0123456789".
387 controller().setComposition("", underlines, -22, -22);
388 EXPECT_STREQ("hello\nworld\n0123456789", div->innerText().utf8().data());
389 EXPECT_EQ(0u, controller().getSelectionOffsets().start());
390 EXPECT_EQ(0u, controller().getSelectionOffsets().end());
391
392 // The cursor is on right boundary.
393 // "hello\nworld\n0123456789*".
394 controller().setComposition("", underlines, 22, 22);
395 EXPECT_STREQ("hello\nworld\n0123456789", div->innerText().utf8().data());
396 EXPECT_EQ(22u, controller().getSelectionOffsets().start());
397 EXPECT_EQ(22u, controller().getSelectionOffsets().end());
398
399 // The cursor is in the 1st node.
400 // "he*llo\nworld\n0123456789".
401 controller().setComposition("", underlines, -20, -20);
402 EXPECT_STREQ("hello\nworld\n0123456789", div->innerText().utf8().data());
403 EXPECT_EQ(2u, controller().getSelectionOffsets().start());
404 EXPECT_EQ(2u, controller().getSelectionOffsets().end());
405
406 // The cursor is on the left boundary of the 5th node.
407 // "hello\nworld\n*01234AB56789".
408 controller().setComposition("", underlines, 10, 10);
409 EXPECT_STREQ("hello\nworld\n0123456789", div->innerText().utf8().data());
410 EXPECT_EQ(12u, controller().getSelectionOffsets().start());
411 EXPECT_EQ(12u, controller().getSelectionOffsets().end());
412
413 // The cursor stays.
414 // "hello\nworld\n*01234AB56789".
415 controller().setComposition("", underlines, 0, 0);
416 EXPECT_STREQ("hello\nworld\n0123456789", div->innerText().utf8().data());
417 EXPECT_EQ(12u, controller().getSelectionOffsets().start());
418 EXPECT_EQ(12u, controller().getSelectionOffsets().end());
419 }
420
421 TEST_F(InputMethodControllerTest, SetCompositionWithEmptyTextAndPreviousComposit ionForDifferentNewCursorPositions)
422 {
423 Element* div = insertHTMLElement(
424 "<div id='sample' contenteditable='true'>hello</div>",
425 "sample");
426
427 controller().setEditableSelectionOffsets(PlainTextRange(2, 2));
428 EXPECT_STREQ("hello", div->innerText().utf8().data());
429 EXPECT_EQ(2u, controller().getSelectionOffsets().start());
430 EXPECT_EQ(2u, controller().getSelectionOffsets().end());
431
432 Vector<CompositionUnderline> underlines0;
433 underlines0.append(CompositionUnderline(0, 0, Color(255, 0, 0), false, 0));
434 Vector<CompositionUnderline> underlines2;
435 underlines2.append(CompositionUnderline(0, 2, Color(255, 0, 0), false, 0));
436
437 controller().setComposition("AB", underlines2, 2, 2);
438 // The cursor exceeds left boundary.
439 controller().setComposition("", underlines0, -100, -100);
440 EXPECT_STREQ("hello", div->innerText().utf8().data());
441 EXPECT_EQ(0u, controller().getSelectionOffsets().start());
442 EXPECT_EQ(0u, controller().getSelectionOffsets().end());
443
444 controller().setComposition("AB", underlines2, 2, 2);
445 // The cursor exceeds right boundary.
446 controller().setComposition("", underlines0, 100, 100);
447 EXPECT_STREQ("hello", div->innerText().utf8().data());
448 EXPECT_EQ(5u, controller().getSelectionOffsets().start());
449 EXPECT_EQ(5u, controller().getSelectionOffsets().end());
450
451 controller().setComposition("AB", underlines2, 2, 2);
452 // The cursor is on left boundary.
453 controller().setComposition("", underlines0, -5, -5);
454 EXPECT_STREQ("hello", div->innerText().utf8().data());
455 EXPECT_EQ(0u, controller().getSelectionOffsets().start());
456 EXPECT_EQ(0u, controller().getSelectionOffsets().end());
457
458 controller().setComposition("AB", underlines2, 2, 2);
459 // The cursor is on right boundary.
460 controller().setComposition("", underlines0, 5, 5);
461 EXPECT_STREQ("hello", div->innerText().utf8().data());
462 EXPECT_EQ(5u, controller().getSelectionOffsets().start());
463 EXPECT_EQ(5u, controller().getSelectionOffsets().end());
464
465 controller().setComposition("AB", underlines2, 2, 2);
466 // The cursor stays.
467 controller().setComposition("", underlines0, 0, 0);
468 EXPECT_STREQ("hello", div->innerText().utf8().data());
469 EXPECT_EQ(5u, controller().getSelectionOffsets().start());
470 EXPECT_EQ(5u, controller().getSelectionOffsets().end());
471 }
472
351 TEST_F(InputMethodControllerTest, CompositionFireBeforeInput) 473 TEST_F(InputMethodControllerTest, CompositionFireBeforeInput)
352 { 474 {
353 document().settings()->setScriptEnabled(true); 475 document().settings()->setScriptEnabled(true);
354 Element* editable = insertHTMLElement("<div id='sample' contentEditable='tru e'></div>", "sample"); 476 Element* editable = insertHTMLElement("<div id='sample' contentEditable='tru e'></div>", "sample");
355 Element* script = document().createElement("script", ASSERT_NO_EXCEPTION); 477 Element* script = document().createElement("script", ASSERT_NO_EXCEPTION);
356 script->setInnerHTML( 478 script->setInnerHTML(
357 "document.getElementById('sample').addEventListener('beforeinput', funct ion(event) {" 479 "document.getElementById('sample').addEventListener('beforeinput', funct ion(event) {"
358 " document.title = `beforeinput.isComposing:${event.isComposing}`;" 480 " document.title = `beforeinput.isComposing:${event.isComposing}`;"
359 "});", 481 "});",
360 ASSERT_NO_EXCEPTION); 482 ASSERT_NO_EXCEPTION);
361 document().body()->appendChild(script, ASSERT_NO_EXCEPTION); 483 document().body()->appendChild(script, ASSERT_NO_EXCEPTION);
362 document().view()->updateAllLifecyclePhases(); 484 document().view()->updateAllLifecyclePhases();
363 485
364 // Simulate composition in the |contentEditable|. 486 // Simulate composition in the |contentEditable|.
365 Vector<CompositionUnderline> underlines; 487 Vector<CompositionUnderline> underlines;
366 underlines.append(CompositionUnderline(0, 5, Color(255, 0, 0), false, 0)); 488 underlines.append(CompositionUnderline(0, 5, Color(255, 0, 0), false, 0));
367 editable->focus(); 489 editable->focus();
368 490
369 document().setTitle(emptyString()); 491 document().setTitle(emptyString());
370 controller().setComposition("foo", underlines, 0, 3); 492 controller().setComposition("foo", underlines, 0, 3);
371 EXPECT_STREQ("beforeinput.isComposing:true", document().title().utf8().data( )); 493 EXPECT_STREQ("beforeinput.isComposing:true", document().title().utf8().data( ));
372 494
373 document().setTitle(emptyString()); 495 document().setTitle(emptyString());
374 controller().confirmComposition(); 496 controller().confirmComposition();
375 // Last 'beforeinput' should also be inside composition scope. 497 // Last 'beforeinput' should also be inside composition scope.
376 EXPECT_STREQ("beforeinput.isComposing:true", document().title().utf8().data( )); 498 EXPECT_STREQ("beforeinput.isComposing:true", document().title().utf8().data( ));
377 } 499 }
378 500
379 } // namespace blink 501 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698