1 /* 2 * Copyright (c) 2011, 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package test.javafx.scene.control; 27 28 import javafx.beans.InvalidationListener; 29 import javafx.beans.Observable; 30 import javafx.beans.property.BooleanProperty; 31 import javafx.beans.property.SimpleBooleanProperty; 32 import javafx.beans.property.SimpleStringProperty; 33 import javafx.beans.property.StringProperty; 34 import javafx.beans.value.ChangeListener; 35 import javafx.beans.value.ObservableValue; 36 import javafx.css.CssMetaData; 37 import javafx.css.StyleableProperty; 38 import javafx.event.EventHandler; 39 import javafx.scene.Scene; 40 import javafx.scene.input.KeyCode; 41 import javafx.scene.input.KeyEvent; 42 import javafx.scene.input.Clipboard; 43 import javafx.scene.input.ClipboardContent; 44 import javafx.scene.text.Font; 45 import javafx.scene.layout.StackPane; 46 import javafx.stage.Stage; 47 import java.util.Arrays; 48 import java.util.Collection; 49 import javafx.scene.control.IndexRange; 50 import javafx.scene.control.PasswordField; 51 import javafx.scene.control.TextArea; 52 import javafx.scene.control.TextField; 53 import javafx.scene.control.TextInputControl; 54 import com.sun.javafx.tk.Toolkit; 55 import org.junit.Before; 56 import org.junit.Ignore; 57 import org.junit.Test; 58 import org.junit.runner.RunWith; 59 import org.junit.runners.Parameterized; 60 import test.com.sun.javafx.scene.control.infrastructure.KeyEventFirer; 61 import test.com.sun.javafx.pgstub.StubToolkit; 62 import static org.junit.Assert.assertEquals; 63 import static org.junit.Assert.assertFalse; 64 import static org.junit.Assert.assertNull; 65 import static org.junit.Assert.assertTrue; 66 67 /** 68 */ 69 @RunWith(Parameterized.class) 70 public class TextInputControlTest { 71 @SuppressWarnings("rawtypes") 72 @Parameterized.Parameters public static Collection implementations() { 73 return Arrays.asList(new Object[][]{ 74 {TextField.class}, 75 {PasswordField.class}, 76 {TextArea.class} 77 }); 78 } 79 80 private TextInputControl textInput; 81 private Class type; 82 83 public TextInputControlTest(Class type) { 84 this.type = type; 85 } 86 87 @Before public void setup() throws Exception { 88 textInput = (TextInputControl) type.newInstance(); 89 } 90 91 /****************************************************** 92 * Test the default states * 93 *****************************************************/ 94 95 @Test public void textDefaultsToEmptyString() { 96 assertEquals("", textInput.getText()); 97 } 98 99 @Test public void editableDefaultsToTrue() { 100 assertTrue(textInput.isEditable()); 101 } 102 103 @Test public void anchorDefaultsToZero() { 104 assertEquals(0, textInput.getAnchor()); 105 } 106 107 @Test public void caretPositionDefaultsToZero() { 108 assertEquals(0, textInput.getCaretPosition()); 109 } 110 111 @Test public void lengthDefaultsToZero() { 112 assertEquals(0, textInput.getLength()); 113 } 114 115 @Test public void selectedTextDefaultsToEmptyString() { 116 assertEquals("", textInput.getSelectedText()); 117 } 118 119 @Test public void selectionDefaultsToEmpty() { 120 assertEquals(0, textInput.getSelection().getLength()); 121 } 122 123 @Test public void selectionStartDefaultsToZero() { 124 assertEquals(0, textInput.getSelection().getStart()); 125 } 126 127 @Test public void selectionEndDefaultsToZero() { 128 assertEquals(0, textInput.getSelection().getEnd()); 129 } 130 131 /********************************************************************* 132 * Tests for CSS * 133 ********************************************************************/ 134 135 @Test public void fontSetFromCSS() { 136 textInput.setStyle("-fx-font: 24 Helvetica"); 137 Scene s = new Scene(textInput); 138 textInput.applyCss(); 139 assertEquals(Font.font("Helvetica", 24), textInput.getFont()); 140 } 141 142 /****************************************************** 143 * Test for text * 144 *****************************************************/ 145 146 @Test public void settingTextUpdatesTheText() { 147 textInput.setText("This is a test"); 148 assertEquals("This is a test", textInput.getText()); 149 assertEquals("This is a test", textInput.textProperty().get()); 150 } 151 152 @Test public void textCanBeNull() { 153 textInput.setText(null); 154 assertNull(textInput.getText()); 155 } 156 157 @Test public void textCanBeSwitchedBetweenNullAndAValue() { 158 textInput.setText(null); 159 textInput.setText("Test"); 160 assertEquals("Test", textInput.getText()); 161 } 162 163 @Test public void textCanBeSwitchedFromAValueToNull() { 164 textInput.setText("Test"); 165 textInput.setText(null); 166 assertNull(textInput.getText()); 167 } 168 169 @Test public void textIsNullThenBoundThenUnboundAndShouldReturnTheValueWhenBound() { 170 textInput.setText(null); 171 StringProperty other = new SimpleStringProperty("Peppers"); 172 textInput.textProperty().bind(other); 173 textInput.textProperty().unbind(); 174 assertEquals("Peppers", textInput.getText()); 175 } 176 177 @Test public void textHasValueThenIsBoundToNullShouldReturnNullFromGet() { 178 textInput.setText("Value"); 179 StringProperty other = new SimpleStringProperty(null); 180 textInput.textProperty().bind(other); 181 assertNull(textInput.getText()); 182 } 183 184 @Test public void textHasValueThenIsBoundToNullAndUnboundShouldReturnNullFromGet() { 185 textInput.setText("Value"); 186 StringProperty other = new SimpleStringProperty(null); 187 textInput.textProperty().bind(other); 188 textInput.textProperty().unbind(); 189 assertNull(textInput.getText()); 190 } 191 192 @Test public void textHasValueThenIsBoundToNullAndUnboundThenSetShouldReturnNewValueFromGet() { 193 textInput.setText("Value"); 194 StringProperty other = new SimpleStringProperty(null); 195 textInput.textProperty().bind(other); 196 textInput.textProperty().unbind(); 197 textInput.setText("New Value"); 198 assertEquals("New Value", textInput.getText()); 199 } 200 201 @Test public void textCanBeBound() { 202 StringProperty other = new SimpleStringProperty("Apples"); 203 textInput.textProperty().bind(other); 204 assertEquals("Apples", textInput.getText()); 205 other.set("Oranges"); 206 assertEquals("Oranges", textInput.getText()); 207 } 208 209 @Ignore("getCssMetaData will return null for textProperty") 210 @Test public void impl_cssSettable_ReturnsFalseForTextAlways() { 211 CssMetaData styleable = ((StyleableProperty)textInput.textProperty()).getCssMetaData(); 212 assertTrue(styleable.isSettable(textInput)); 213 StringProperty other = new SimpleStringProperty("Apples"); 214 textInput.textProperty().bind(other); 215 assertFalse(styleable.isSettable(textInput)); 216 } 217 218 @Test public void cannotSpecifyTextViaCSS() { 219 try { 220 CssMetaData styleable = ((StyleableProperty)textInput.textProperty()).getCssMetaData(); 221 assertNull(styleable); 222 } catch (ClassCastException ignored) { 223 // pass! 224 } catch (Exception e) { 225 org.junit.Assert.fail(e.toString()); 226 } 227 } 228 229 @Test public void settingTextNotifiesOfChange() { 230 final boolean[] passed = new boolean[] { false }; 231 textInput.textProperty().addListener((observable, oldValue, newValue) -> { 232 passed[0] = true; 233 }); 234 textInput.setText("Apples"); 235 assertTrue(passed[0]); 236 } 237 238 // TODO test that setting text which includes a \n works 239 @Test public void controlCharactersAreOmitted_setText_getText() { 240 String s = "This is " + '\0' + "a test"; 241 textInput.setText(s); 242 assertEquals("This is a test", textInput.getText()); 243 } 244 245 @Test public void controlCharactersAreOmitted_setText_textProperty_get() { 246 String s = "This is " + '\0' + "a test"; 247 textInput.setText(s); 248 assertEquals("This is a test", textInput.textProperty().get()); 249 } 250 251 @Test public void controlCharactersAreOmitted_bound_getText() { 252 StringProperty other = new SimpleStringProperty("This is " + '\0' + "a test"); 253 textInput.textProperty().bind(other); 254 assertEquals("This is a test", textInput.getText()); 255 other.set("Bro" + '\5' + "ken"); 256 assertEquals("Broken", textInput.getText()); 257 } 258 259 @Test public void controlCharactersAreOmitted_bound_textProperty_get() { 260 StringProperty other = new SimpleStringProperty("This is " + '\0' + "a test"); 261 textInput.textProperty().bind(other); 262 assertEquals("This is a test", textInput.textProperty().get()); 263 other.set("Bro" + '\5' + "ken"); 264 assertEquals("Broken", textInput.textProperty().get()); 265 } 266 267 // selection is changed when text is changed?? 268 // anchor and caret position updated when selection is changed due to text change?? 269 // selected text is updated when selection changes due to a text change?? 270 // length is updated when text changes 271 272 /****************************************************** 273 * Test for editable * 274 *****************************************************/ 275 276 @Test public void settingEditableValueShouldWork() { 277 textInput.setEditable(false); 278 assertFalse(textInput.isEditable()); 279 } 280 281 @Test public void settingEditableAndThenCreatingAModelAndReadingTheValueStillWorks() { 282 textInput.setEditable(false); 283 assertFalse(textInput.editableProperty().get()); 284 } 285 286 @Test public void editableCanBeBound() { 287 BooleanProperty other = new SimpleBooleanProperty(false); 288 textInput.editableProperty().bind(other); 289 assertFalse(textInput.isEditable()); 290 other.set(true); 291 assertTrue(textInput.isEditable()); 292 } 293 294 @Ignore("getCssMetaData will return null for editableProperty") 295 @Test public void impl_cssSettable_ReturnsFalseForEditableAlways() { 296 CssMetaData styleable = ((StyleableProperty)textInput.editableProperty()).getCssMetaData(); 297 assertTrue(styleable.isSettable(textInput)); 298 StringProperty other = new SimpleStringProperty("Apples"); 299 textInput.textProperty().bind(other); 300 assertFalse(styleable.isSettable(textInput)); 301 } 302 303 @Test public void cannotSpecifyEditableViaCSS() { 304 try { 305 CssMetaData styleable = ((StyleableProperty)textInput.editableProperty()).getCssMetaData(); 306 assertNull(styleable); 307 } catch (ClassCastException ignored) { 308 // pass! 309 } catch (Exception e) { 310 org.junit.Assert.fail(e.toString()); 311 } 312 } 313 314 @Test public void settingEditableNotifiesOfChange() { 315 final boolean[] passed = new boolean[] { false }; 316 textInput.editableProperty().addListener((observable, oldValue, newValue) -> { 317 passed[0] = true; 318 }); 319 textInput.setEditable(false); 320 assertTrue(passed[0]); 321 } 322 323 /****************************************************** 324 * Test for anchor * 325 *****************************************************/ 326 327 @Test public void anchorIsSetWhenSelectionChanges() { 328 textInput.setText("The quick brown fox"); 329 textInput.selectRange(4, 9); 330 assertEquals(4, textInput.getAnchor()); 331 } 332 333 @Test public void anchorIsSetWhenSelectionChanges2() { 334 textInput.setText("The quick brown fox"); 335 textInput.selectRange(9, 4); 336 assertEquals(9, textInput.getAnchor()); 337 } 338 339 // updated when text changes 340 @Test public void anchorIsSetToCaretPositionWhenTextChanges() { 341 textInput.setText("The quick brown fox"); 342 textInput.selectRange(4, 9); 343 textInput.setText("Gone"); 344 assertEquals(textInput.getCaretPosition(), textInput.getAnchor()); 345 } 346 347 /****************************************************** 348 * Test for caretPosition * 349 *****************************************************/ 350 351 @Test public void caretPositionIsSetWhenSelectionChanges() { 352 textInput.setText("The quick brown fox"); 353 textInput.selectRange(4, 9); 354 assertEquals(9, textInput.getCaretPosition()); 355 } 356 357 @Test public void caretPositionIsSetWhenSelectionChanges2() { 358 textInput.setText("The quick brown fox"); 359 textInput.selectRange(9, 4); 360 assertEquals(4, textInput.getCaretPosition()); 361 } 362 363 @Test 364 public void caretAndAnchorPositionAfterSettingText() { 365 textInput.setText("The quick brown fox"); 366 assertEquals(0, textInput.getCaretPosition()); 367 assertEquals(0, textInput.getAnchor()); 368 } 369 370 // Test for JDK-8178417 371 @Test public void caretPositionUndo() { 372 Toolkit tk = (StubToolkit)Toolkit.getToolkit(); 373 StackPane root = new StackPane(); 374 Scene scene = new Scene(root); 375 Stage stage = new Stage(); 376 String text = "01234"; 377 378 textInput.setText(text); 379 stage.setScene(scene); 380 root.getChildren().removeAll(); 381 root.getChildren().add(textInput); 382 stage.show(); 383 tk.firePulse(); 384 385 KeyEventFirer keyboard = new KeyEventFirer(textInput); 386 keyboard.doKeyPress(KeyCode.HOME); 387 388 for(int i = 1; i < text.length() + 1; ++i) { 389 keyboard.doKeyPress(KeyCode.RIGHT); 390 tk.firePulse(); 391 } 392 for(int i = 1; i < text.length() + 1; ++i) { 393 textInput.undo(); 394 } 395 assertEquals(text.length(), textInput.getCaretPosition()); 396 root.getChildren().removeAll(); 397 stage.hide(); 398 tk.firePulse(); 399 } 400 401 /****************************************************** 402 * Test for length * 403 *****************************************************/ 404 405 // TODO null text results in 0 length 406 407 @Test public void emptyTextResultsInZeroLength() { 408 textInput.setText("Hello"); 409 textInput.setText(""); 410 assertEquals(0, textInput.getLength()); 411 } 412 413 @Test public void lengthMatchesStringLength() { 414 final String string = "Hello"; 415 textInput.setText(string); 416 assertEquals(string.length(), textInput.getLength()); 417 } 418 419 @Test public void lengthChangeNotificationWhenTextIsUpdatedToNonEmptyResult() { 420 final boolean[] passed = new boolean[] { false }; 421 textInput.lengthProperty().addListener((observable, oldValue, newValue) -> { 422 passed[0] = true; 423 }); 424 textInput.setText("Hello"); 425 assertTrue(passed[0]); 426 } 427 428 @Ignore("The notification here doesn't happen because the invalid flag is set after the first set," + 429 "however setting a change listener *must* clear that, but doesn't. I copied the code for this " + 430 "straight from the beans package, so there may be a bug there.") 431 @Test public void lengthChangeNotificationWhenTextIsSetToEmptyResult() { 432 textInput.setText("Goodbye"); 433 final boolean[] passed = new boolean[] { false }; 434 textInput.lengthProperty().addListener((observable, oldValue, newValue) -> { 435 passed[0] = true; 436 }); 437 textInput.setText(""); 438 assertTrue(passed[0]); 439 } 440 441 /****************************************************** 442 * Test for maximumLength * 443 *****************************************************/ 444 445 // set maximum length to less than current length 446 447 /****************************************************** 448 * Test for selected text * 449 *****************************************************/ 450 451 // TODO test null text and some random range 452 453 @Test public void selectedTextMatchesTextAndSelection() { 454 textInput.setText("The quick brown fox"); 455 textInput.selectRange(0, 3); 456 assertEquals("The", textInput.getSelectedText()); 457 } 458 459 @Test public void selectedTextMatchesTextAndSelection2() { 460 textInput.setText("The quick brown fox"); 461 textInput.selectRange(4, 9); 462 assertEquals("quick", textInput.getSelectedText()); 463 } 464 465 @Test public void selectedTextMatchesTextAndSelection3() { 466 textInput.setText("The quick brown fox"); 467 textInput.selectRange(10, 19); 468 assertEquals("brown fox", textInput.getSelectedText()); 469 } 470 471 @Test public void selectedTextIsClearedWhenTextChanges() { 472 textInput.setText("The quick brown fox"); 473 textInput.selectRange(4, 9); 474 textInput.setText(""); 475 assertEquals("", textInput.getSelectedText()); 476 } 477 478 @Test public void selectedTextWorksWhenSelectionExceedsPossibleRange() { 479 textInput.setText("The quick brown fox"); 480 textInput.selectRange(10, 180); 481 assertEquals("brown fox", textInput.getSelectedText()); 482 } 483 484 @Test public void selectedTextWorksWhenSelectionExceedsPossibleRange2() { 485 textInput.setText("The quick brown fox"); 486 textInput.selectRange(100, 180); 487 assertEquals("", textInput.getSelectedText()); 488 } 489 490 // @Test public void selectedTextWorksWhenSelectionIsBound() { 491 // ObjectProperty<IndexRange> other = new SimpleObjectProperty<IndexRange>(new IndexRange(4, 9)); 492 // textInput.setText("The quick brown fox"); 493 // textInput.selectionProperty().bind(other); 494 // assertEquals("quick", textInput.getSelectedText()); 495 // other.set(new IndexRange(10, 19)); 496 // assertEquals("brown fox", textInput.getSelectedText()); 497 // } 498 499 @Test public void selectedTextWorksWhenTextIsBound() { 500 StringProperty other = new SimpleStringProperty("There and back again"); 501 textInput.textProperty().bind(other); 502 textInput.selectRange(0, 5); 503 assertEquals("There", textInput.getSelectedText()); 504 other.set("Cleared!"); 505 assertEquals("", textInput.getSelectedText()); 506 } 507 508 @Test public void selectedTextChangeEvents() { 509 final boolean[] passed = new boolean[] { false }; 510 textInput.setText("The quick brown fox"); 511 textInput.selectedTextProperty().addListener(observable -> { 512 passed[0] = true; 513 }); 514 textInput.selectRange(0, 3); 515 assertTrue(passed[0]); 516 } 517 518 @Test public void selectedTextChangeEvents2() { 519 final boolean[] passed = new boolean[] { false }; 520 textInput.setText("The quick brown fox"); 521 textInput.selectRange(0, 3); 522 textInput.selectedTextProperty().addListener(observable -> { 523 passed[0] = true; 524 }); 525 textInput.selectRange(10, 180); 526 assertTrue(passed[0]); 527 } 528 529 @Test public void selectedTextChangeEvents3() { 530 final boolean[] passed = new boolean[] { false }; 531 StringProperty other = new SimpleStringProperty("There and back again"); 532 textInput.textProperty().bind(other); 533 textInput.selectRange(0, 5); 534 textInput.selectedTextProperty().addListener(observable -> { 535 passed[0] = true; 536 }); 537 other.set("Cleared!"); 538 assertTrue(passed[0]); 539 } 540 541 /****************************************************** 542 * Test for selection * 543 *****************************************************/ 544 545 @Test public void selectionIsClearedWhenTextChanges() { 546 textInput.setText("The quick brown fox"); 547 textInput.selectRange(4, 9); 548 textInput.setText(""); 549 assertEquals(new IndexRange(0, 0), textInput.getSelection()); 550 } 551 552 @Test public void selectionCannotBeSetToBeOutOfRange() { 553 textInput.setText("The quick brown fox"); 554 textInput.selectRange(4, 99); 555 assertEquals(new IndexRange(4, 19), textInput.getSelection()); 556 } 557 558 @Test public void selectionCannotBeSetToBeOutOfRange2() { 559 textInput.setText("The quick brown fox"); 560 textInput.selectRange(44, 99); 561 assertEquals(new IndexRange(19, 19), textInput.getSelection()); 562 } 563 564 // @Test public void selectionCanBeBound() { 565 // ObjectProperty<IndexRange> other = new SimpleObjectProperty<IndexRange>(new IndexRange(4, 9)); 566 // textInput.selectionProperty().bind(other); 567 // assertEquals(new IndexRange(4, 9), textInput.getSelection()); 568 // other.set(new IndexRange(10, 19)); 569 // assertEquals(new IndexRange(10, 19), textInput.getSelection()); 570 // } 571 572 @Test public void selectionChangeEventsHappen() { 573 final boolean[] passed = new boolean[] { false }; 574 textInput.selectionProperty().addListener(observable -> { 575 passed[0] = true; 576 }); 577 textInput.selectRange(0, 3); 578 assertTrue(passed[0]); 579 } 580 581 // @Test public void selectionChangeEventsHappenWhenBound() { 582 // final boolean[] passed = new boolean[] { false }; 583 // ObjectProperty<IndexRange> other = new SimpleObjectProperty<IndexRange>(new IndexRange(0, 5)); 584 // textInput.selectionProperty().addListener(new InvalidationListener() { 585 // @Override public void invalidated(Observable observable) { 586 // passed[0] = true; 587 // } 588 // }); 589 // textInput.selectionProperty().bind(other); 590 // assertTrue(passed[0]); 591 // } 592 593 // @Test public void selectionChangeEventsHappenWhenBound2() { 594 // final boolean[] passed = new boolean[] { false }; 595 // ObjectProperty<IndexRange> other = new SimpleObjectProperty<IndexRange>(new IndexRange(0, 5)); 596 // textInput.selectionProperty().bind(other); 597 // textInput.selectionProperty().addListener(new InvalidationListener() { 598 // @Override public void invalidated(Observable observable) { 599 // passed[0] = true; 600 // } 601 // }); 602 // assertFalse(passed[0]); 603 // other.set(new IndexRange(1, 2)); 604 // assertTrue(passed[0]); 605 // } 606 607 @Test public void selectionChangeEventsHappenWhenTextIsChanged() { 608 final boolean[] passed = new boolean[] { false }; 609 StringProperty other = new SimpleStringProperty("There and back again"); 610 textInput.textProperty().bind(other); 611 textInput.selectRange(0, 5); 612 textInput.selectionProperty().addListener(observable -> { 613 passed[0] = true; 614 }); 615 other.set("Cleared!"); 616 assertTrue(passed[0]); 617 } 618 619 @Ignore 620 @Test public void selectionCanBeNull() { 621 622 } 623 624 /****************************************************** 625 * Test for cut/copy/paste * 626 *****************************************************/ 627 628 @Test public void cutRemovesSelection() { 629 // Skip for PasswordField 630 if (textInput instanceof PasswordField) return; 631 textInput.setText("The quick brown fox"); 632 textInput.selectRange(4, 9); 633 textInput.cut(); 634 assertEquals("The brown fox", textInput.getText()); 635 } 636 637 @Test public void pasteReplacesSelection() { 638 textInput.setText("The quick brown fox"); 639 textInput.selectRange(4, 9); 640 copy("slow"); 641 textInput.paste(); 642 assertEquals("The slow brown fox", textInput.getText()); 643 } 644 645 @Test public void pasteIllegalCharacters() { 646 textInput.setText("The quick brown fox"); 647 textInput.selectRange(19, 19); 648 copy("" + '\0'); 649 textInput.paste(); 650 assertEquals("The quick brown fox", textInput.getText()); 651 } 652 653 @Test public void pasteIllegalCharactersCaretNotAtZero() { 654 textInput.setText("The quick brown fox"); 655 textInput.selectRange(4, 4); 656 copy("slow" + '\0'); 657 textInput.paste(); 658 assertEquals(8, textInput.getCaretPosition()); 659 assertEquals(8, textInput.getAnchor()); 660 } 661 662 @Test public void pasteIllegalCharactersSelection() { 663 textInput.setText("The quick brown fox"); 664 textInput.selectRange(4, 9); 665 copy("slow" + '\0'); 666 textInput.paste(); 667 assertEquals("The slow brown fox", textInput.getText()); 668 } 669 670 @Test public void pasteIllegalCharactersIntoSelectionPositionsCaretCorrectly() { 671 textInput.setText("The quick brown fox"); 672 textInput.selectRange(4, 9); 673 copy("slow" + '\0'); 674 textInput.paste(); 675 assertEquals(8, textInput.getCaretPosition()); 676 assertEquals(8, textInput.getAnchor()); 677 } 678 679 /****************************************************** 680 * Test for manipulating selection via methods * 681 *****************************************************/ 682 683 // cut ends up removing the selection, and setting anchor / caretPosition to match index 684 @Test public void cutRemovesSelectionAndResetsAnchorAndCaretPositionToIndex() { 685 // Skip for PasswordField 686 if (textInput instanceof PasswordField) return; 687 textInput.setText("The quick brown fox"); 688 textInput.selectRange(4, 9); 689 textInput.cut(); 690 assertEquals(4, textInput.getAnchor()); 691 assertEquals(4, textInput.getCaretPosition()); 692 assertEquals(new IndexRange(4, 4), textInput.getSelection()); 693 assertEquals("", textInput.getSelectedText()); 694 } 695 696 @Test public void pasteWithEmptySelection() { 697 textInput.setText("quick brown fox"); 698 textInput.selectRange(0,0); 699 copy("The "); 700 textInput.paste(); 701 assertEquals(4, textInput.getAnchor()); 702 assertEquals(4, textInput.getCaretPosition()); 703 assertEquals(new IndexRange(4, 4), textInput.getSelection()); 704 assertEquals("", textInput.getSelectedText()); 705 } 706 707 @Test public void pasteWithSelection() { 708 textInput.setText("The quick brown fox"); 709 textInput.selectRange(4, 9); 710 copy("slow"); 711 textInput.paste(); 712 assertEquals(8, textInput.getAnchor()); 713 assertEquals(8, textInput.getCaretPosition()); 714 assertEquals(new IndexRange(8, 8), textInput.getSelection()); 715 assertEquals("", textInput.getSelectedText()); 716 } 717 718 @Test public void pasteAll() { 719 textInput.setText("The quick brown fox"); 720 textInput.selectAll(); 721 copy("Gone"); 722 textInput.paste(); 723 assertEquals(4, textInput.getAnchor()); 724 assertEquals(4, textInput.getCaretPosition()); 725 assertEquals(new IndexRange(4, 4), textInput.getSelection()); 726 assertEquals("", textInput.getSelectedText()); 727 } 728 729 @Test public void selectBackwardHasNoEffectWhenCaretPositionIsAlreadyZero() { 730 textInput.setText("The quick brown fox"); 731 textInput.selectRange(3, 0); 732 textInput.selectBackward(); 733 assertEquals(0, textInput.getCaretPosition()); 734 assertEquals(new IndexRange(0, 3), textInput.getSelection()); 735 } 736 737 @Test public void selectBackwardMovesCaretPositionOnePlaceLeft_CaretPositionRightOfAnchor() { 738 textInput.setText("The quick brown fox"); 739 textInput.selectRange(0, 3); 740 textInput.selectBackward(); 741 assertEquals(2, textInput.getCaretPosition()); 742 assertEquals(new IndexRange(0, 2), textInput.getSelection()); 743 } 744 745 @Test public void selectBackwardMovesCaretPositionOnePlaceLeft_CaretPositionEqualsAnchor() { 746 textInput.setText("The quick brown fox"); 747 textInput.selectRange(3, 3); 748 textInput.selectBackward(); 749 assertEquals(2, textInput.getCaretPosition()); 750 assertEquals(new IndexRange(2, 3), textInput.getSelection()); 751 } 752 753 @Test public void selectBackwardMovesCaretPositionOnePlaceLeft_CaretPositionLeftOfAnchor() { 754 textInput.setText("The quick brown fox"); 755 textInput.selectRange(6, 3); 756 textInput.selectBackward(); 757 assertEquals(2, textInput.getCaretPosition()); 758 assertEquals(new IndexRange(2, 6), textInput.getSelection()); 759 } 760 761 @Test public void selectForwardHasNoEffectWhenCaretPositionIsAtLength() { 762 textInput.setText("The quick brown fox"); 763 textInput.selectRange(3, 19); 764 textInput.selectForward(); 765 assertEquals(19, textInput.getCaretPosition()); 766 assertEquals(new IndexRange(3, 19), textInput.getSelection()); 767 } 768 769 @Test public void selectForwardMovesCaretPositionOnePlaceRight_CaretPositionRightOfAnchor() { 770 textInput.setText("The quick brown fox"); 771 textInput.selectRange(0, 3); 772 textInput.selectForward(); 773 assertEquals(4, textInput.getCaretPosition()); 774 assertEquals(new IndexRange(0, 4), textInput.getSelection()); 775 } 776 777 @Test public void selectForwardMovesCaretPositionOnePlaceRight_CaretPositionEqualsAnchor() { 778 textInput.setText("The quick brown fox"); 779 textInput.selectRange(3, 3); 780 textInput.selectForward(); 781 assertEquals(4, textInput.getCaretPosition()); 782 assertEquals(new IndexRange(3, 4), textInput.getSelection()); 783 } 784 785 @Test public void selectForwardMovesCaretPositionOnePlaceRight_CaretPositionLeftOfAnchor() { 786 textInput.setText("The quick brown fox"); 787 textInput.selectRange(6, 3); 788 textInput.selectForward(); 789 assertEquals(4, textInput.getCaretPosition()); 790 assertEquals(new IndexRange(4, 6), textInput.getSelection()); 791 } 792 793 @Test public void previousWordWithNoText() { 794 textInput.previousWord(); 795 assertEquals(0, textInput.getCaretPosition()); 796 assertEquals(0, textInput.getAnchor()); 797 assertEquals(new IndexRange(0, 0), textInput.getSelection()); 798 } 799 800 @Test public void previousWordWithSelection_caretPositionBeforeAnchor() { 801 textInput.setText("The quick brown fox"); 802 textInput.selectRange(15, 10); 803 textInput.previousWord(); 804 assertEquals(4, textInput.getCaretPosition()); 805 assertEquals(4, textInput.getAnchor()); 806 assertEquals(new IndexRange(4, 4), textInput.getSelection()); 807 } 808 809 @Test public void previousWordWithSelection_caretPositionBeforeAnchor2() { 810 textInput.setText("The quick brown fox"); 811 textInput.selectRange(12, 6); 812 textInput.previousWord(); 813 assertEquals(4, textInput.getCaretPosition()); 814 assertEquals(4, textInput.getAnchor()); 815 assertEquals(new IndexRange(4, 4), textInput.getSelection()); 816 } 817 818 @Test public void previousWordWithSelection_caretPositionAfterAnchor() { 819 textInput.setText("The quick brown fox"); 820 textInput.selectRange(10, 15); 821 textInput.previousWord(); 822 assertEquals(10, textInput.getCaretPosition()); 823 assertEquals(10, textInput.getAnchor()); 824 assertEquals(new IndexRange(10, 10), textInput.getSelection()); 825 } 826 827 @Test public void previousWordWithSelection_caretPositionAfterAnchor2() { 828 textInput.setText("The quick brown fox"); 829 textInput.selectRange(6, 12); 830 textInput.previousWord(); 831 assertEquals(10, textInput.getCaretPosition()); 832 assertEquals(10, textInput.getAnchor()); 833 assertEquals(new IndexRange(10, 10), textInput.getSelection()); 834 } 835 836 @Test public void previousWord_caretWithinAWord() { 837 textInput.setText("The quick brown fox"); 838 textInput.positionCaret(12); 839 textInput.previousWord(); 840 assertEquals(10, textInput.getCaretPosition()); 841 assertEquals(10, textInput.getAnchor()); 842 assertEquals(new IndexRange(10, 10), textInput.getSelection()); 843 } 844 845 @Test public void previousWord_caretAfterWord() { 846 textInput.setText("The quick brown fox"); 847 textInput.positionCaret(15); 848 textInput.previousWord(); 849 assertEquals(10, textInput.getCaretPosition()); 850 assertEquals(10, textInput.getAnchor()); 851 assertEquals(new IndexRange(10, 10), textInput.getSelection()); 852 } 853 854 @Test public void previousWord_caretBeforeWord() { 855 textInput.setText("The quick brown fox"); 856 textInput.positionCaret(10); 857 textInput.previousWord(); 858 assertEquals(4, textInput.getCaretPosition()); 859 assertEquals(4, textInput.getAnchor()); 860 assertEquals(new IndexRange(4, 4), textInput.getSelection()); 861 } 862 863 @Test public void previousWord_caretWithinWhitespace() { 864 textInput.setText("The quick brown fox"); 865 textInput.positionCaret(10); 866 textInput.previousWord(); 867 assertEquals(4, textInput.getCaretPosition()); 868 assertEquals(4, textInput.getAnchor()); 869 assertEquals(new IndexRange(4, 4), textInput.getSelection()); 870 } 871 872 @Test public void previousWord_multipleWhitespaceInARow() { 873 textInput.setText("The quick brown fox"); 874 textInput.positionCaret(11); 875 textInput.previousWord(); 876 assertEquals(4, textInput.getCaretPosition()); 877 assertEquals(4, textInput.getAnchor()); 878 assertEquals(new IndexRange(4, 4), textInput.getSelection()); 879 } 880 881 @Test public void previousWord_withANumber() { 882 textInput.setText("There are 5 cards in the hand"); 883 textInput.positionCaret(12); 884 textInput.previousWord(); 885 assertEquals(10, textInput.getCaretPosition()); 886 assertEquals(10, textInput.getAnchor()); 887 assertEquals(new IndexRange(10, 10), textInput.getSelection()); 888 } 889 890 @Test public void previousWord_withALongNumber() { 891 textInput.setText("There are 52 cards in the deck"); 892 textInput.positionCaret(13); 893 textInput.previousWord(); 894 assertEquals(10, textInput.getCaretPosition()); 895 assertEquals(10, textInput.getAnchor()); 896 assertEquals(new IndexRange(10, 10), textInput.getSelection()); 897 } 898 899 @Test public void nextWordWithNoText() { 900 textInput.nextWord(); 901 assertEquals(0, textInput.getCaretPosition()); 902 assertEquals(0, textInput.getAnchor()); 903 assertEquals(new IndexRange(0, 0), textInput.getSelection()); 904 } 905 906 @Test public void nextWordWithSelection_caretPositionBeforeAnchor() { 907 textInput.setText("The quick brown fox"); 908 textInput.selectRange(9, 4); 909 textInput.nextWord(); 910 assertEquals(10, textInput.getCaretPosition()); 911 assertEquals(10, textInput.getAnchor()); 912 assertEquals(new IndexRange(10, 10), textInput.getSelection()); 913 } 914 915 @Test public void nextWordWithSelection_caretPositionBeforeAnchor2() { 916 textInput.setText("The quick brown fox"); 917 textInput.selectRange(9, 2); 918 textInput.nextWord(); 919 assertEquals(4, textInput.getCaretPosition()); 920 assertEquals(4, textInput.getAnchor()); 921 assertEquals(new IndexRange(4, 4), textInput.getSelection()); 922 } 923 924 @Test public void nextWordWithSelection_caretPositionAfterAnchor() { 925 textInput.setText("The quick brown fox"); 926 textInput.selectRange(4, 9); 927 textInput.nextWord(); 928 assertEquals(10, textInput.getCaretPosition()); 929 assertEquals(10, textInput.getAnchor()); 930 assertEquals(new IndexRange(10, 10), textInput.getSelection()); 931 } 932 933 @Test public void nextWordWithSelection_caretPositionAfterAnchor2() { 934 textInput.setText("The quick brown fox"); 935 textInput.selectRange(5, 11); 936 textInput.nextWord(); 937 assertEquals(16, textInput.getCaretPosition()); 938 assertEquals(16, textInput.getAnchor()); 939 assertEquals(new IndexRange(16, 16), textInput.getSelection()); 940 } 941 942 @Test public void nextWord_caretWithinAWord() { 943 textInput.setText("The quick brown fox"); 944 textInput.positionCaret(6); 945 textInput.nextWord(); 946 assertEquals(10, textInput.getCaretPosition()); 947 assertEquals(10, textInput.getAnchor()); 948 assertEquals(new IndexRange(10, 10), textInput.getSelection()); 949 } 950 951 @Test public void nextWord_caretAfterWord() { 952 textInput.setText("The quick brown fox"); 953 textInput.positionCaret(9); 954 textInput.nextWord(); 955 assertEquals(10, textInput.getCaretPosition()); 956 assertEquals(10, textInput.getAnchor()); 957 assertEquals(new IndexRange(10, 10), textInput.getSelection()); 958 } 959 960 @Test public void nextWord_caretBeforeWord() { 961 textInput.setText("The quick brown fox"); 962 textInput.positionCaret(4); 963 textInput.nextWord(); 964 assertEquals(10, textInput.getCaretPosition()); 965 assertEquals(10, textInput.getAnchor()); 966 assertEquals(new IndexRange(10, 10), textInput.getSelection()); 967 } 968 969 @Test public void nextWord_caretWithinWhitespace() { 970 textInput.setText("The quick brown fox"); 971 textInput.positionCaret(10); 972 textInput.nextWord(); 973 assertEquals(11, textInput.getCaretPosition()); 974 assertEquals(11, textInput.getAnchor()); 975 assertEquals(new IndexRange(11, 11), textInput.getSelection()); 976 } 977 978 @Test public void nextWord_multipleWhitespaceInARow() { 979 textInput.setText("The quick brown fox"); 980 textInput.positionCaret(9); 981 textInput.nextWord(); 982 assertEquals(11, textInput.getCaretPosition()); 983 assertEquals(11, textInput.getAnchor()); 984 assertEquals(new IndexRange(11, 11), textInput.getSelection()); 985 } 986 987 @Test public void nextWord_toTheEnd() { 988 textInput.setText("The quick brown fox"); 989 textInput.positionCaret(16); 990 textInput.nextWord(); 991 assertEquals(19, textInput.getCaretPosition()); 992 assertEquals(19, textInput.getAnchor()); 993 assertEquals(new IndexRange(19, 19), textInput.getSelection()); 994 } 995 996 @Test public void nextWord_withANumber() { 997 textInput.setText("There are 5 cards in the hand"); 998 textInput.positionCaret(6); 999 textInput.nextWord(); 1000 assertEquals(10, textInput.getCaretPosition()); 1001 assertEquals(10, textInput.getAnchor()); 1002 assertEquals(new IndexRange(10, 10), textInput.getSelection()); 1003 } 1004 1005 @Test public void nextWord_withALongNumber() { 1006 textInput.setText("There are 52 cards in the deck"); 1007 textInput.positionCaret(10); 1008 textInput.nextWord(); 1009 assertEquals(13, textInput.getCaretPosition()); 1010 assertEquals(13, textInput.getAnchor()); 1011 assertEquals(new IndexRange(13, 13), textInput.getSelection()); 1012 } 1013 1014 @Test public void endOfNextWordWithNoText() { 1015 textInput.endOfNextWord(); 1016 assertEquals(0, textInput.getCaretPosition()); 1017 assertEquals(0, textInput.getAnchor()); 1018 assertEquals(new IndexRange(0, 0), textInput.getSelection()); 1019 } 1020 1021 @Test public void endOfNextWordWithSelection_caretPositionBeforeAnchor() { 1022 textInput.setText("The quick brown fox"); 1023 textInput.selectRange(9, 4); 1024 textInput.endOfNextWord(); 1025 assertEquals(9, textInput.getCaretPosition()); 1026 assertEquals(9, textInput.getAnchor()); 1027 assertEquals(new IndexRange(9, 9), textInput.getSelection()); 1028 } 1029 1030 @Test public void endOfNextWordWithSelection_caretPositionBeforeAnchor2() { 1031 textInput.setText("The quick brown fox"); 1032 textInput.selectRange(9, 2); 1033 textInput.endOfNextWord(); 1034 assertEquals(3, textInput.getCaretPosition()); 1035 assertEquals(3, textInput.getAnchor()); 1036 assertEquals(new IndexRange(3, 3), textInput.getSelection()); 1037 } 1038 1039 @Test public void endOfNextWordWithSelection_caretPositionAfterAnchor() { 1040 textInput.setText("The quick brown fox"); 1041 textInput.selectRange(4, 9); 1042 textInput.endOfNextWord(); 1043 assertEquals(15, textInput.getCaretPosition()); 1044 assertEquals(15, textInput.getAnchor()); 1045 assertEquals(new IndexRange(15, 15), textInput.getSelection()); 1046 } 1047 1048 @Test public void endOfNextWordWithSelection_caretPositionAfterAnchor2() { 1049 textInput.setText("The quick brown fox"); 1050 textInput.selectRange(5, 11); 1051 textInput.endOfNextWord(); 1052 assertEquals(15, textInput.getCaretPosition()); 1053 assertEquals(15, textInput.getAnchor()); 1054 assertEquals(new IndexRange(15, 15), textInput.getSelection()); 1055 } 1056 1057 @Test public void endOfNextWord_caretWithinAWord() { 1058 textInput.setText("The quick brown fox"); 1059 textInput.positionCaret(6); 1060 textInput.endOfNextWord(); 1061 assertEquals(9, textInput.getCaretPosition()); 1062 assertEquals(9, textInput.getAnchor()); 1063 assertEquals(new IndexRange(9, 9), textInput.getSelection()); 1064 } 1065 1066 @Test public void endOfNextWord_caretAfterWord() { 1067 textInput.setText("The quick brown fox"); 1068 textInput.positionCaret(9); 1069 textInput.endOfNextWord(); 1070 assertEquals(15, textInput.getCaretPosition()); 1071 assertEquals(15, textInput.getAnchor()); 1072 assertEquals(new IndexRange(15, 15), textInput.getSelection()); 1073 } 1074 1075 @Test public void endOfNextWord_caretBeforeWord() { 1076 textInput.setText("The quick brown fox"); 1077 textInput.positionCaret(4); 1078 textInput.endOfNextWord(); 1079 assertEquals(9, textInput.getCaretPosition()); 1080 assertEquals(9, textInput.getAnchor()); 1081 assertEquals(new IndexRange(9, 9), textInput.getSelection()); 1082 } 1083 1084 @Test public void endOfNextWord_caretWithinWhitespace() { 1085 textInput.setText("The quick brown fox"); 1086 textInput.positionCaret(10); 1087 textInput.endOfNextWord(); 1088 assertEquals(16, textInput.getCaretPosition()); 1089 assertEquals(16, textInput.getAnchor()); 1090 assertEquals(new IndexRange(16, 16), textInput.getSelection()); 1091 } 1092 1093 @Test public void endOfNextWord_multipleWhitespaceInARow() { 1094 textInput.setText("The quick brown fox"); 1095 textInput.positionCaret(9); 1096 textInput.endOfNextWord(); 1097 assertEquals(16, textInput.getCaretPosition()); 1098 assertEquals(16, textInput.getAnchor()); 1099 assertEquals(new IndexRange(16, 16), textInput.getSelection()); 1100 } 1101 1102 @Test public void endOfNextWord_withANumber() { 1103 textInput.setText("There are 5 cards in the hand"); 1104 textInput.positionCaret(6); 1105 textInput.endOfNextWord(); 1106 assertEquals(9, textInput.getCaretPosition()); 1107 assertEquals(9, textInput.getAnchor()); 1108 assertEquals(new IndexRange(9, 9), textInput.getSelection()); 1109 } 1110 1111 @Test public void endOfNextWord_withANumber_CaretOnANumber() { 1112 textInput.setText("There are 5 cards in the hand"); 1113 textInput.positionCaret(10); 1114 textInput.endOfNextWord(); 1115 assertEquals(11, textInput.getCaretPosition()); 1116 assertEquals(11, textInput.getAnchor()); 1117 assertEquals(new IndexRange(11, 11), textInput.getSelection()); 1118 } 1119 1120 @Test public void endOfNextWord_withALongNumber_CaretOnANumber() { 1121 textInput.setText("There are 52 cards in the deck"); 1122 textInput.positionCaret(10); 1123 textInput.endOfNextWord(); 1124 assertEquals(12, textInput.getCaretPosition()); 1125 assertEquals(12, textInput.getAnchor()); 1126 assertEquals(new IndexRange(12, 12), textInput.getSelection()); 1127 } 1128 1129 @Test public void selectPreviousWordWithNoText() { 1130 textInput.selectPreviousWord(); 1131 assertEquals(0, textInput.getCaretPosition()); 1132 assertEquals(0, textInput.getAnchor()); 1133 assertEquals(new IndexRange(0, 0), textInput.getSelection()); 1134 } 1135 1136 @Test public void selectPreviousWordWithSelection_caretPositionBeforeAnchor() { 1137 textInput.setText("The quick brown fox"); 1138 textInput.selectRange(15, 10); 1139 textInput.selectPreviousWord(); 1140 assertEquals(4, textInput.getCaretPosition()); 1141 assertEquals(15, textInput.getAnchor()); 1142 assertEquals(new IndexRange(4, 15), textInput.getSelection()); 1143 } 1144 1145 @Test public void selectPreviousWordWithSelection_caretPositionAfterAnchor() { 1146 textInput.setText("The quick brown fox"); 1147 textInput.selectRange(10, 15); 1148 textInput.selectPreviousWord(); 1149 assertEquals(10, textInput.getCaretPosition()); 1150 assertEquals(10, textInput.getAnchor()); 1151 assertEquals(new IndexRange(10, 10), textInput.getSelection()); 1152 } 1153 1154 @Test public void selectPreviousWordWithSelection_caretPositionAfterAnchor2() { 1155 textInput.setText("The quick brown fox"); 1156 textInput.selectRange(11, 15); 1157 textInput.selectPreviousWord(); 1158 assertEquals(10, textInput.getCaretPosition()); 1159 assertEquals(11, textInput.getAnchor()); 1160 assertEquals(new IndexRange(10, 11), textInput.getSelection()); 1161 } 1162 1163 @Test public void selectPreviousWord_caretWithinAWord() { 1164 textInput.setText("The quick brown fox"); 1165 textInput.positionCaret(12); 1166 textInput.selectPreviousWord(); 1167 assertEquals(10, textInput.getCaretPosition()); 1168 assertEquals(12, textInput.getAnchor()); 1169 assertEquals(new IndexRange(10, 12), textInput.getSelection()); 1170 } 1171 1172 @Test public void selectPreviousWord_caretAfterWord() { 1173 textInput.setText("The quick brown fox"); 1174 textInput.positionCaret(15); 1175 textInput.selectPreviousWord(); 1176 assertEquals(10, textInput.getCaretPosition()); 1177 assertEquals(15, textInput.getAnchor()); 1178 assertEquals(new IndexRange(10, 15), textInput.getSelection()); 1179 } 1180 1181 @Test public void selectPreviousWord_caretBeforeWord() { 1182 textInput.setText("The quick brown fox"); 1183 textInput.positionCaret(10); 1184 textInput.selectPreviousWord(); 1185 assertEquals(4, textInput.getCaretPosition()); 1186 assertEquals(10, textInput.getAnchor()); 1187 assertEquals(new IndexRange(4, 10), textInput.getSelection()); 1188 } 1189 1190 @Test public void selectPreviousWord_caretWithinWhitespace() { 1191 textInput.setText("The quick brown fox"); 1192 textInput.positionCaret(10); 1193 textInput.selectPreviousWord(); 1194 assertEquals(4, textInput.getCaretPosition()); 1195 assertEquals(10, textInput.getAnchor()); 1196 assertEquals(new IndexRange(4, 10), textInput.getSelection()); 1197 } 1198 1199 @Test public void selectPreviousWord_multipleWhitespaceInARow() { 1200 textInput.setText("The quick brown fox"); 1201 textInput.positionCaret(11); 1202 textInput.selectPreviousWord(); 1203 assertEquals(4, textInput.getCaretPosition()); 1204 assertEquals(11, textInput.getAnchor()); 1205 assertEquals(new IndexRange(4, 11), textInput.getSelection()); 1206 } 1207 1208 @Test public void selectNextWordWithNoText() { 1209 textInput.selectNextWord(); 1210 assertEquals(0, textInput.getCaretPosition()); 1211 assertEquals(0, textInput.getAnchor()); 1212 assertEquals(new IndexRange(0, 0), textInput.getSelection()); 1213 } 1214 1215 @Test public void selectNextWordWithSelection_caretPositionBeforeAnchor() { 1216 textInput.setText("The quick brown fox"); 1217 textInput.selectRange(9, 4); 1218 textInput.selectNextWord(); 1219 assertEquals(10, textInput.getCaretPosition()); 1220 assertEquals(9, textInput.getAnchor()); 1221 assertEquals(new IndexRange(9, 10), textInput.getSelection()); 1222 } 1223 1224 @Test public void selectNextWordWithSelection_caretPositionBeforeAnchor2() { 1225 textInput.setText("The quick brown fox"); 1226 textInput.selectRange(9, 2); 1227 textInput.selectNextWord(); 1228 assertEquals(4, textInput.getCaretPosition()); 1229 assertEquals(9, textInput.getAnchor()); 1230 assertEquals(new IndexRange(4, 9), textInput.getSelection()); 1231 } 1232 1233 @Test public void selectNextWordWithSelection_caretPositionAfterAnchor() { 1234 textInput.setText("The quick brown fox"); 1235 textInput.selectRange(4, 9); 1236 textInput.selectNextWord(); 1237 assertEquals(10, textInput.getCaretPosition()); 1238 assertEquals(4, textInput.getAnchor()); 1239 assertEquals(new IndexRange(4, 10), textInput.getSelection()); 1240 } 1241 1242 @Test public void selectNextWordWithSelection_caretPositionAfterAnchor2() { 1243 textInput.setText("The quick brown fox"); 1244 textInput.selectRange(5, 11); 1245 textInput.selectNextWord(); 1246 assertEquals(16, textInput.getCaretPosition()); 1247 assertEquals(5, textInput.getAnchor()); 1248 assertEquals(new IndexRange(5, 16), textInput.getSelection()); 1249 } 1250 1251 @Test public void selectNextWord_caretWithinAWord() { 1252 textInput.setText("The quick brown fox"); 1253 textInput.positionCaret(6); 1254 textInput.selectNextWord(); 1255 assertEquals(10, textInput.getCaretPosition()); 1256 assertEquals(6, textInput.getAnchor()); 1257 assertEquals(new IndexRange(6, 10), textInput.getSelection()); 1258 } 1259 1260 @Test public void selectNextWord_caretAfterWord() { 1261 textInput.setText("The quick brown fox"); 1262 textInput.positionCaret(9); 1263 textInput.selectNextWord(); 1264 assertEquals(10, textInput.getCaretPosition()); 1265 assertEquals(9, textInput.getAnchor()); 1266 assertEquals(new IndexRange(9, 10), textInput.getSelection()); 1267 } 1268 1269 @Test public void selectNextWord_caretBeforeWord() { 1270 textInput.setText("The quick brown fox"); 1271 textInput.positionCaret(4); 1272 textInput.selectNextWord(); 1273 assertEquals(10, textInput.getCaretPosition()); 1274 assertEquals(4, textInput.getAnchor()); 1275 assertEquals(new IndexRange(4, 10), textInput.getSelection()); 1276 } 1277 1278 @Test public void selectNextWord_caretWithinWhitespace() { 1279 textInput.setText("The quick brown fox"); 1280 textInput.positionCaret(10); 1281 textInput.selectNextWord(); 1282 assertEquals(11, textInput.getCaretPosition()); 1283 assertEquals(10, textInput.getAnchor()); 1284 assertEquals(new IndexRange(10, 11), textInput.getSelection()); 1285 } 1286 1287 @Test public void selectNextWord_multipleWhitespaceInARow() { 1288 textInput.setText("The quick brown fox"); 1289 textInput.positionCaret(9); 1290 textInput.selectNextWord(); 1291 assertEquals(11, textInput.getCaretPosition()); 1292 assertEquals(9, textInput.getAnchor()); 1293 assertEquals(new IndexRange(9, 11), textInput.getSelection()); 1294 } 1295 1296 @Test public void selectNextWord_toTheEnd() { 1297 textInput.setText("The quick brown fox"); 1298 textInput.positionCaret(16); 1299 textInput.selectNextWord(); 1300 assertEquals(19, textInput.getCaretPosition()); 1301 assertEquals(16, textInput.getAnchor()); 1302 assertEquals(new IndexRange(16, 19), textInput.getSelection()); 1303 } 1304 1305 @Test public void selectEndOfNextWordWithNoText() { 1306 textInput.selectEndOfNextWord(); 1307 assertEquals(0, textInput.getCaretPosition()); 1308 assertEquals(0, textInput.getAnchor()); 1309 assertEquals(new IndexRange(0, 0), textInput.getSelection()); 1310 } 1311 1312 @Test public void selectEndOfNextWordWithSelection_caretPositionBeforeAnchor() { 1313 textInput.setText("The quick brown fox"); 1314 textInput.selectRange(9, 4); 1315 textInput.selectEndOfNextWord(); 1316 assertEquals(9, textInput.getCaretPosition()); 1317 assertEquals(9, textInput.getAnchor()); 1318 assertEquals(new IndexRange(9, 9), textInput.getSelection()); 1319 } 1320 1321 @Test public void selectEndOfNextWordWithSelection_caretPositionBeforeAnchor2() { 1322 textInput.setText("The quick brown fox"); 1323 textInput.selectRange(9, 2); 1324 textInput.selectEndOfNextWord(); 1325 assertEquals(3, textInput.getCaretPosition()); 1326 assertEquals(9, textInput.getAnchor()); 1327 assertEquals(new IndexRange(3, 9), textInput.getSelection()); 1328 } 1329 1330 @Test public void selectEndOfNextWordWithSelection_caretPositionAfterAnchor() { 1331 textInput.setText("The quick brown fox"); 1332 textInput.selectRange(4, 9); 1333 textInput.selectEndOfNextWord(); 1334 assertEquals(15, textInput.getCaretPosition()); 1335 assertEquals(4, textInput.getAnchor()); 1336 assertEquals(new IndexRange(4, 15), textInput.getSelection()); 1337 } 1338 1339 @Test public void selectEndOfNextWordWithSelection_caretPositionAfterAnchor2() { 1340 textInput.setText("The quick brown fox"); 1341 textInput.selectRange(5, 11); 1342 textInput.selectEndOfNextWord(); 1343 assertEquals(15, textInput.getCaretPosition()); 1344 assertEquals(5, textInput.getAnchor()); 1345 assertEquals(new IndexRange(5, 15), textInput.getSelection()); 1346 } 1347 1348 @Test public void selectEndOfNextWord_caretWithinAWord() { 1349 textInput.setText("The quick brown fox"); 1350 textInput.positionCaret(6); 1351 textInput.selectEndOfNextWord(); 1352 assertEquals(9, textInput.getCaretPosition()); 1353 assertEquals(6, textInput.getAnchor()); 1354 assertEquals(new IndexRange(6, 9), textInput.getSelection()); 1355 } 1356 1357 @Test public void selectEndOfNextWord_caretAfterWord() { 1358 textInput.setText("The quick brown fox"); 1359 textInput.positionCaret(9); 1360 textInput.selectEndOfNextWord(); 1361 assertEquals(15, textInput.getCaretPosition()); 1362 assertEquals(9, textInput.getAnchor()); 1363 assertEquals(new IndexRange(9, 15), textInput.getSelection()); 1364 } 1365 1366 @Test public void selectEndOfNextWord_caretBeforeWord() { 1367 textInput.setText("The quick brown fox"); 1368 textInput.positionCaret(4); 1369 textInput.selectEndOfNextWord(); 1370 assertEquals(9, textInput.getCaretPosition()); 1371 assertEquals(4, textInput.getAnchor()); 1372 assertEquals(new IndexRange(4, 9), textInput.getSelection()); 1373 } 1374 1375 @Test public void selectEndOfNextWord_caretWithinWhitespace() { 1376 textInput.setText("The quick brown fox"); 1377 textInput.positionCaret(10); 1378 textInput.selectEndOfNextWord(); 1379 assertEquals(16, textInput.getCaretPosition()); 1380 assertEquals(10, textInput.getAnchor()); 1381 assertEquals(new IndexRange(10, 16), textInput.getSelection()); 1382 } 1383 1384 @Test public void selectEndOfNextWord_multipleWhitespaceInARow() { 1385 textInput.setText("The quick brown fox"); 1386 textInput.positionCaret(9); 1387 textInput.selectEndOfNextWord(); 1388 assertEquals(16, textInput.getCaretPosition()); 1389 assertEquals(9, textInput.getAnchor()); 1390 assertEquals(new IndexRange(9, 16), textInput.getSelection()); 1391 } 1392 1393 @Test public void selectAllWithNoText() { 1394 textInput.setText(""); 1395 textInput.selectAll(); 1396 assertEquals(0, textInput.getCaretPosition()); 1397 assertEquals(0, textInput.getAnchor()); 1398 assertEquals(new IndexRange(0, 0), textInput.getSelection()); 1399 } 1400 1401 @Test public void selectAllWithText_caretPositionIsAlwaysAtTheEnd() { 1402 textInput.setText("The quick brown fox"); 1403 textInput.selectAll(); 1404 assertEquals(19, textInput.getCaretPosition()); 1405 assertEquals(0, textInput.getAnchor()); 1406 assertEquals(new IndexRange(0, 19), textInput.getSelection()); 1407 } 1408 1409 @Test public void homeClearsSelection() { 1410 textInput.setText("The quick brown fox"); 1411 textInput.selectRange(4, 9); 1412 textInput.home(); 1413 assertEquals(0, textInput.getCaretPosition()); 1414 assertEquals(0, textInput.getAnchor()); 1415 assertEquals(new IndexRange(0, 0), textInput.getSelection()); 1416 } 1417 1418 @Test public void endClearsSelection() { 1419 textInput.setText("The quick brown fox"); 1420 textInput.selectRange(4, 9); 1421 textInput.end(); 1422 assertEquals(19, textInput.getCaretPosition()); 1423 assertEquals(19, textInput.getAnchor()); 1424 assertEquals(new IndexRange(19, 19), textInput.getSelection()); 1425 } 1426 1427 @Test public void selectHomeHasNoEffectWhenCaretPositionIsAtZero() { 1428 textInput.setText("The quick brown fox"); 1429 textInput.selectRange(3, 0); 1430 textInput.selectHome(); 1431 assertEquals(0, textInput.getCaretPosition()); 1432 assertEquals(3, textInput.getAnchor()); 1433 assertEquals(new IndexRange(0, 3), textInput.getSelection()); 1434 } 1435 1436 @Test public void selectHomeMovesCaretPositionToZero_CaretPositionRightOfAnchor() { 1437 textInput.setText("The quick brown fox"); 1438 textInput.selectRange(4, 9); 1439 textInput.selectHome(); 1440 assertEquals(0, textInput.getCaretPosition()); 1441 assertEquals(4, textInput.getAnchor()); 1442 assertEquals(new IndexRange(0, 4), textInput.getSelection()); 1443 } 1444 1445 @Test public void selectHomeMovesCaretPositionToZero_CaretPositionEqualsAnchor() { 1446 textInput.setText("The quick brown fox"); 1447 textInput.selectRange(3, 3); 1448 textInput.selectHome(); 1449 assertEquals(0, textInput.getCaretPosition()); 1450 assertEquals(3, textInput.getAnchor()); 1451 assertEquals(new IndexRange(0, 3), textInput.getSelection()); 1452 } 1453 1454 @Test public void selectHomeMovesCaretPositionToZero_CaretPositionLeftOfAnchor() { 1455 textInput.setText("The quick brown fox"); 1456 textInput.selectRange(6, 3); 1457 textInput.selectHome(); 1458 assertEquals(0, textInput.getCaretPosition()); 1459 assertEquals(6, textInput.getAnchor()); 1460 assertEquals(new IndexRange(0, 6), textInput.getSelection()); 1461 } 1462 1463 @Test public void selectEndHasNoEffectWhenCaretPositionIsAtLength() { 1464 textInput.setText("The quick brown fox"); 1465 textInput.selectRange(3, 19); 1466 textInput.selectEnd(); 1467 assertEquals(19, textInput.getCaretPosition()); 1468 assertEquals(3, textInput.getAnchor()); 1469 assertEquals(new IndexRange(3, 19), textInput.getSelection()); 1470 } 1471 1472 @Test public void selectEndMovesCaretPositionToLength_CaretPositionRightOfAnchor() { 1473 textInput.setText("The quick brown fox"); 1474 textInput.selectRange(0, 3); 1475 textInput.selectEnd(); 1476 assertEquals(19, textInput.getCaretPosition()); 1477 assertEquals(0, textInput.getAnchor()); 1478 assertEquals(new IndexRange(0, 19), textInput.getSelection()); 1479 } 1480 1481 @Test public void selectEndMovesCaretPositionToLength_CaretPositionEqualsAnchor() { 1482 textInput.setText("The quick brown fox"); 1483 textInput.selectRange(3, 3); 1484 textInput.selectEnd(); 1485 assertEquals(19, textInput.getCaretPosition()); 1486 assertEquals(3, textInput.getAnchor()); 1487 assertEquals(new IndexRange(3, 19), textInput.getSelection()); 1488 } 1489 1490 @Test public void selectEndMovesCaretPositionToLength_CaretPositionLeftOfAnchor() { 1491 textInput.setText("The quick brown fox"); 1492 textInput.selectRange(6, 3); 1493 textInput.selectEnd(); 1494 assertEquals(19, textInput.getCaretPosition()); 1495 assertEquals(6, textInput.getAnchor()); 1496 assertEquals(new IndexRange(6, 19), textInput.getSelection()); 1497 } 1498 1499 @Test public void deletePreviousCharDeletesOnlySelectedText_anchorLessThanCaretPosition() { 1500 textInput.setText("The quick brown fox"); 1501 textInput.selectRange(4, 10); 1502 textInput.deletePreviousChar(); 1503 assertEquals("The brown fox", textInput.getText()); 1504 assertEquals(4, textInput.getCaretPosition()); 1505 assertEquals(4, textInput.getAnchor()); 1506 assertEquals(new IndexRange(4, 4), textInput.getSelection()); 1507 } 1508 1509 @Test public void deletePreviousCharDeletesOnlySelectedText_caretPositionLessThanAnchor() { 1510 textInput.setText("The quick brown fox"); 1511 textInput.selectRange(10, 4); 1512 textInput.deletePreviousChar(); 1513 assertEquals("The brown fox", textInput.getText()); 1514 assertEquals(4, textInput.getCaretPosition()); 1515 assertEquals(4, textInput.getAnchor()); 1516 assertEquals(new IndexRange(4, 4), textInput.getSelection()); 1517 } 1518 1519 @Test public void deletePreviousCharDeletesPreviousCharWhenCaretPositionEqualsAnchor() { 1520 textInput.setText("The quick brown fox"); 1521 textInput.selectRange(10, 10); 1522 textInput.deletePreviousChar(); 1523 assertEquals("The quickbrown fox", textInput.getText()); 1524 assertEquals(9, textInput.getCaretPosition()); 1525 assertEquals(9, textInput.getAnchor()); 1526 assertEquals(new IndexRange(9, 9), textInput.getSelection()); 1527 } 1528 1529 @Test public void deletePreviousCharDoesNothingWhenSelectionIs0_0() { 1530 textInput.setText("The quick brown fox"); 1531 textInput.selectRange(0, 0); 1532 textInput.deletePreviousChar(); 1533 assertEquals("The quick brown fox", textInput.getText()); 1534 assertEquals(0, textInput.getCaretPosition()); 1535 assertEquals(0, textInput.getAnchor()); 1536 assertEquals(new IndexRange(0, 0), textInput.getSelection()); 1537 } 1538 1539 @Test public void deleteNextCharDeletesOnlySelectedText_anchorLessThanCaretPosition() { 1540 textInput.setText("The quick brown fox"); 1541 textInput.selectRange(4, 10); 1542 textInput.deleteNextChar(); 1543 assertEquals("The brown fox", textInput.getText()); 1544 assertEquals(4, textInput.getCaretPosition()); 1545 assertEquals(4, textInput.getAnchor()); 1546 assertEquals(new IndexRange(4, 4), textInput.getSelection()); 1547 } 1548 1549 @Test public void deleteNextCharDeletesOnlySelectedText_caretPositionLessThanAnchor() { 1550 textInput.setText("The quick brown fox"); 1551 textInput.selectRange(10, 4); 1552 textInput.deleteNextChar(); 1553 assertEquals("The brown fox", textInput.getText()); 1554 assertEquals(4, textInput.getCaretPosition()); 1555 assertEquals(4, textInput.getAnchor()); 1556 assertEquals(new IndexRange(4, 4), textInput.getSelection()); 1557 } 1558 1559 @Test public void deleteNextCharDeletesNextCharWhenCaretPositionEqualsAnchor() { 1560 textInput.setText("The quick brown fox"); 1561 textInput.selectRange(10, 10); 1562 textInput.deleteNextChar(); 1563 assertEquals("The quick rown fox", textInput.getText()); 1564 assertEquals(10, textInput.getCaretPosition()); 1565 assertEquals(10, textInput.getAnchor()); 1566 assertEquals(new IndexRange(10, 10), textInput.getSelection()); 1567 } 1568 1569 @Test public void deleteNextCharDoesNothingWhenSelectionIsEmptyAtEnd() { 1570 textInput.setText("The quick brown fox"); 1571 textInput.selectRange(19, 19); 1572 textInput.deleteNextChar(); 1573 assertEquals("The quick brown fox", textInput.getText()); 1574 assertEquals(19, textInput.getCaretPosition()); 1575 assertEquals(19, textInput.getAnchor()); 1576 assertEquals(new IndexRange(19, 19), textInput.getSelection()); 1577 } 1578 1579 @Test public void forwardSkipsSelection() { 1580 textInput.setText("The quick brown fox"); 1581 textInput.selectRange(4, 9); 1582 textInput.forward(); 1583 assertEquals(9, textInput.getCaretPosition()); 1584 assertEquals(9, textInput.getAnchor()); 1585 assertEquals(new IndexRange(9, 9), textInput.getSelection()); 1586 } 1587 1588 @Test public void forwardSkipsSelection2() { 1589 textInput.setText("The quick brown fox"); 1590 textInput.selectRange(9, 4); 1591 textInput.forward(); 1592 assertEquals(9, textInput.getCaretPosition()); 1593 assertEquals(9, textInput.getAnchor()); 1594 assertEquals(new IndexRange(9, 9), textInput.getSelection()); 1595 } 1596 1597 @Test public void forwardMovesForwardWhenNotAtEnd() { 1598 textInput.setText("The quick brown fox"); 1599 textInput.selectRange(0, 0); 1600 textInput.forward(); 1601 assertEquals(1, textInput.getCaretPosition()); 1602 assertEquals(1, textInput.getAnchor()); 1603 assertEquals(new IndexRange(1, 1), textInput.getSelection()); 1604 } 1605 1606 @Test public void forwardDoesNothingWhenAtEnd() { 1607 textInput.setText("The quick brown fox"); 1608 textInput.selectRange(19, 19); 1609 textInput.forward(); 1610 assertEquals(19, textInput.getCaretPosition()); 1611 assertEquals(19, textInput.getAnchor()); 1612 assertEquals(new IndexRange(19, 19), textInput.getSelection()); 1613 } 1614 1615 @Test public void backwardSkipsSelection() { 1616 textInput.setText("The quick brown fox"); 1617 textInput.selectRange(4, 9); 1618 textInput.backward(); 1619 assertEquals(4, textInput.getCaretPosition()); 1620 assertEquals(4, textInput.getAnchor()); 1621 assertEquals(new IndexRange(4, 4), textInput.getSelection()); 1622 } 1623 1624 @Test public void backwardSkipsSelection2() { 1625 textInput.setText("The quick brown fox"); 1626 textInput.selectRange(9, 4); 1627 textInput.backward(); 1628 assertEquals(4, textInput.getCaretPosition()); 1629 assertEquals(4, textInput.getAnchor()); 1630 assertEquals(new IndexRange(4, 4), textInput.getSelection()); 1631 } 1632 1633 @Test public void backwardMovesBackwardWhenNotAtStart() { 1634 textInput.setText("The quick brown fox"); 1635 textInput.positionCaret(14); 1636 textInput.backward(); 1637 assertEquals(13, textInput.getCaretPosition()); 1638 assertEquals(13, textInput.getAnchor()); 1639 assertEquals(new IndexRange(13, 13), textInput.getSelection()); 1640 } 1641 1642 @Test public void backwardDoesNothingWhenAtStart() { 1643 textInput.setText("The quick brown fox"); 1644 textInput.selectRange(0, 0); 1645 textInput.backward(); 1646 assertEquals(0, textInput.getCaretPosition()); 1647 assertEquals(0, textInput.getAnchor()); 1648 assertEquals(new IndexRange(0, 0), textInput.getSelection()); 1649 } 1650 1651 @Test public void positionCaretAtStart() { 1652 textInput.setText("The quick brown fox"); 1653 textInput.positionCaret(0); 1654 assertEquals(0, textInput.getCaretPosition()); 1655 assertEquals(0, textInput.getAnchor()); 1656 assertEquals(new IndexRange(0, 0), textInput.getSelection()); 1657 } 1658 1659 @Test public void positionCaretInMiddle() { 1660 textInput.setText("The quick brown fox"); 1661 textInput.positionCaret(10); 1662 assertEquals(10, textInput.getCaretPosition()); 1663 assertEquals(10, textInput.getAnchor()); 1664 assertEquals(new IndexRange(10, 10), textInput.getSelection()); 1665 } 1666 1667 @Test public void positionCaretAtEnd() { 1668 textInput.setText("The quick brown fox"); 1669 textInput.positionCaret(19); 1670 assertEquals(19, textInput.getCaretPosition()); 1671 assertEquals(19, textInput.getAnchor()); 1672 assertEquals(new IndexRange(19, 19), textInput.getSelection()); 1673 } 1674 1675 @Test public void positionCaretBeyondStartClamps() { 1676 textInput.setText("The quick brown fox"); 1677 textInput.positionCaret(-10); 1678 assertEquals(0, textInput.getCaretPosition()); 1679 assertEquals(0, textInput.getAnchor()); 1680 assertEquals(new IndexRange(0, 0), textInput.getSelection()); 1681 } 1682 1683 @Test public void positionCaretBeyondEndClamps() { 1684 textInput.setText("The quick brown fox"); 1685 textInput.positionCaret(1000); 1686 assertEquals(19, textInput.getCaretPosition()); 1687 assertEquals(19, textInput.getAnchor()); 1688 assertEquals(new IndexRange(19, 19), textInput.getSelection()); 1689 } 1690 1691 @Test public void selectPositionCaretWhenAnchorAndCaretAreBothZero() { 1692 textInput.setText("The quick brown fox"); 1693 textInput.selectRange(0, 0); 1694 textInput.selectPositionCaret(10); 1695 assertEquals(10, textInput.getCaretPosition()); 1696 assertEquals(0, textInput.getAnchor()); 1697 assertEquals(new IndexRange(0, 10), textInput.getSelection()); 1698 } 1699 1700 @Test public void selectPositionCaret_anchorLessThanCaretPosition() { 1701 textInput.setText("The quick brown fox"); 1702 textInput.selectRange(4, 10); 1703 textInput.selectPositionCaret(1); 1704 assertEquals(1, textInput.getCaretPosition()); 1705 assertEquals(4, textInput.getAnchor()); 1706 assertEquals(new IndexRange(1, 4), textInput.getSelection()); 1707 } 1708 1709 @Test public void selectPositionCaret_anchorLessThanCaretPosition2() { 1710 textInput.setText("The quick brown fox"); 1711 textInput.selectRange(4, 10); 1712 textInput.selectPositionCaret(15); 1713 assertEquals(15, textInput.getCaretPosition()); 1714 assertEquals(4, textInput.getAnchor()); 1715 assertEquals(new IndexRange(4, 15), textInput.getSelection()); 1716 } 1717 1718 @Test public void selectPositionCaret_anchorLessThanCaretPosition3() { 1719 textInput.setText("The quick brown fox"); 1720 textInput.selectRange(4, 10); 1721 textInput.selectPositionCaret(4); 1722 assertEquals(4, textInput.getCaretPosition()); 1723 assertEquals(4, textInput.getAnchor()); 1724 assertEquals(new IndexRange(4, 4), textInput.getSelection()); 1725 } 1726 1727 @Test public void selectPositionCaret_caretPositionLessThanAnchor() { 1728 textInput.setText("The quick brown fox"); 1729 textInput.selectRange(10, 4); 1730 textInput.selectPositionCaret(1); 1731 assertEquals(1, textInput.getCaretPosition()); 1732 assertEquals(10, textInput.getAnchor()); 1733 assertEquals(new IndexRange(1, 10), textInput.getSelection()); 1734 } 1735 1736 @Test public void selectPositionCaret_caretPositionLessThanAnchor2() { 1737 textInput.setText("The quick brown fox"); 1738 textInput.selectRange(10, 4); 1739 textInput.selectPositionCaret(14); 1740 assertEquals(14, textInput.getCaretPosition()); 1741 assertEquals(10, textInput.getAnchor()); 1742 assertEquals(new IndexRange(10, 14), textInput.getSelection()); 1743 } 1744 1745 @Test public void selectPositionCaret_caretPositionLessThanAnchor3() { 1746 textInput.setText("The quick brown fox"); 1747 textInput.selectRange(10, 4); 1748 textInput.selectPositionCaret(10); 1749 assertEquals(10, textInput.getCaretPosition()); 1750 assertEquals(10, textInput.getAnchor()); 1751 assertEquals(new IndexRange(10, 10), textInput.getSelection()); 1752 } 1753 1754 @Test public void selectPositionCaretWhenCaretPositionEqualsAnchor() { 1755 textInput.setText("The quick brown fox"); 1756 textInput.selectRange(10, 10); 1757 textInput.selectPositionCaret(4); 1758 assertEquals(4, textInput.getCaretPosition()); 1759 assertEquals(10, textInput.getAnchor()); 1760 assertEquals(new IndexRange(4, 10), textInput.getSelection()); 1761 } 1762 1763 @Test public void selectPositionCaretWhenCaretPositionEqualsAnchor2() { 1764 textInput.setText("The quick brown fox"); 1765 textInput.selectRange(10, 10); 1766 textInput.selectPositionCaret(14); 1767 assertEquals(14, textInput.getCaretPosition()); 1768 assertEquals(10, textInput.getAnchor()); 1769 assertEquals(new IndexRange(10, 14), textInput.getSelection()); 1770 } 1771 1772 @Test public void extendSelectionWithNoText() { 1773 textInput.extendSelection(0); 1774 assertEquals(0, textInput.getCaretPosition()); 1775 assertEquals(0, textInput.getAnchor()); 1776 assertEquals(new IndexRange(0, 0), textInput.getSelection()); 1777 } 1778 1779 @Test public void extendSelectionWithOutOfRangePos() { 1780 textInput.setText("The quick brown fox"); 1781 textInput.selectRange(0, 0); 1782 textInput.extendSelection(1000); 1783 assertEquals(19, textInput.getCaretPosition()); 1784 assertEquals(0, textInput.getAnchor()); 1785 assertEquals(new IndexRange(0, 19), textInput.getSelection()); 1786 } 1787 1788 @Test public void extendSelectionWithOutOfRangePos2() { 1789 textInput.setText("The quick brown fox"); 1790 textInput.positionCaret(4); 1791 textInput.extendSelection(-19); 1792 assertEquals(0, textInput.getCaretPosition()); 1793 assertEquals(4, textInput.getAnchor()); 1794 assertEquals(new IndexRange(0, 4), textInput.getSelection()); 1795 } 1796 1797 @Test public void test_rt26250_caret_issue_for_thai_characters() { 1798 // Thai string containing two characters, consisting of three 1799 // codepoints each. 1800 String thaiStr = "\u0E17\u0E35\u0E48\u0E17\u0E35\u0E48"; 1801 textInput.setText(thaiStr); 1802 textInput.positionCaret(0); 1803 1804 // Step past one character 1805 textInput.forward(); 1806 assertEquals(3, textInput.getCaretPosition()); 1807 1808 // Goto beginning 1809 textInput.backward(); 1810 assertEquals(0, textInput.getCaretPosition()); 1811 1812 // Delete entire first character forwards 1813 textInput.deleteNextChar(); 1814 assertEquals("\u0E17\u0E35\u0E48", textInput.getText()); 1815 1816 // Break up and delete remaining character backwards in three steps 1817 textInput.forward(); 1818 textInput.deletePreviousChar(); 1819 assertEquals("\u0E17\u0E35", textInput.getText()); 1820 textInput.deletePreviousChar(); 1821 assertEquals("\u0E17", textInput.getText()); 1822 textInput.deletePreviousChar(); 1823 assertEquals("", textInput.getText()); 1824 } 1825 1826 @Test public void test_rt40376_delete_next_when_text_is_null() { 1827 textInput.setText(null); 1828 textInput.deleteNextChar(); 1829 } 1830 1831 @Test public void test_jdk_8171229_replaceText() { 1832 textInput.setText(""); 1833 assertEquals("", textInput.getText()); 1834 1835 textInput.replaceText(0, 0, "a"); 1836 assertEquals("a", textInput.getText()); 1837 1838 textInput.replaceText(1, 1, "b"); 1839 assertEquals("ab", textInput.getText()); 1840 1841 textInput.replaceText(2, 2, "c"); 1842 assertEquals("abc", textInput.getText()); 1843 1844 textInput.replaceText(3, 3, "d"); 1845 assertEquals("abcd", textInput.getText()); 1846 1847 textInput.replaceText(3, 4, "efg"); 1848 assertEquals("abcefg", textInput.getText()); 1849 1850 textInput.replaceText(3, 6, "d"); 1851 assertEquals("abcd", textInput.getText()); 1852 1853 textInput.replaceText(0, 4, ""); 1854 assertEquals("", textInput.getText()); 1855 } 1856 1857 // TODO tests for Content firing event notification properly 1858 1859 // TODO tests for Content not allowing illegal characters 1860 1861 private void copy(String string) { 1862 ClipboardContent content = new ClipboardContent(); 1863 content.putString(string); 1864 Clipboard.getSystemClipboard().setContent(content); 1865 } 1866 }