-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test: caret pos updated correctly when change occurs at its pos
- Loading branch information
1 parent
4f5a2cb
commit d6685f6
Showing
2 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
richtextfx/src/integrationTest/java/org/fxmisc/richtext/api/caret/BoundsTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package org.fxmisc.richtext.api.caret; | ||
|
||
import javafx.stage.Stage; | ||
import org.fxmisc.richtext.Caret; | ||
import org.fxmisc.richtext.InlineCssTextAreaAppTest; | ||
import org.fxmisc.richtext.TextBuildingUtils; | ||
import org.junit.Test; | ||
import org.testfx.util.WaitForAsyncUtils; | ||
|
||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
public class BoundsTests extends InlineCssTextAreaAppTest { | ||
|
||
private static final String MANY_PARS_OF_TEXT = TextBuildingUtils.buildLines(20); | ||
|
||
@Override | ||
public void start(Stage stage) throws Exception { | ||
super.start(stage); | ||
stage.setHeight(50); | ||
|
||
// insure caret is always visible | ||
area.setShowCaret(Caret.CaretVisibility.ON); | ||
area.replaceText(MANY_PARS_OF_TEXT); | ||
area.moveTo(0); | ||
area.showParagraphAtTop(0); | ||
} | ||
|
||
@Test | ||
public void caret_bounds_are_present_after_moving_caret_and_following_it() { | ||
assertTrue(area.getCaretBounds().isPresent()); | ||
|
||
// move caret outside of viewport | ||
interact(() -> { | ||
area.moveTo(area.getLength()); | ||
area.requestFollowCaret(); | ||
}); | ||
|
||
// needed for test to pass | ||
WaitForAsyncUtils.waitForFxEvents(); | ||
|
||
// viewport should update itself so caret is visible again | ||
assertTrue(area.getCaretBounds().isPresent()); | ||
} | ||
|
||
@Test | ||
public void caret_bounds_are_absent_after_moving_caret_without_following_it() { | ||
assertTrue(area.getCaretBounds().isPresent()); | ||
|
||
// move caret outside of viewport | ||
interact(() -> area.moveTo(area.getLength())); | ||
|
||
// caret should not be visible | ||
assertFalse(area.getCaretBounds().isPresent()); | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
richtextfx/src/integrationTest/java/org/fxmisc/richtext/api/caret/PositionTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package org.fxmisc.richtext.api.caret; | ||
|
||
import javafx.stage.Stage; | ||
import org.fxmisc.richtext.CaretNode; | ||
import org.fxmisc.richtext.InlineCssTextAreaAppTest; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class PositionTests extends InlineCssTextAreaAppTest { | ||
|
||
CaretNode caret; | ||
String text = "text"; | ||
|
||
@Override | ||
public void start(Stage stage) throws Exception { | ||
super.start(stage); | ||
area.replaceText(text); | ||
caret = new CaretNode("extra caret", area); | ||
area.addCaret(caret); | ||
} | ||
|
||
@Test | ||
public void position_is_correct_when_change_occurs_before_position() { | ||
interact(() -> { | ||
caret.moveToAreaEnd(); | ||
int pos = caret.getPosition(); | ||
|
||
String append = "some"; | ||
|
||
// add test | ||
area.insertText(0, append); | ||
assertEquals(pos + append.length(), caret.getPosition()); | ||
|
||
// delete test | ||
area.deleteText(0, append.length()); | ||
assertEquals(pos, caret.getPosition()); | ||
}); | ||
} | ||
|
||
@Test | ||
public void position_is_correct_when_change_occurs_before_position_and_deletes_carets_position() { | ||
interact(() -> { | ||
caret.moveTo(text.length() - 1); | ||
|
||
area.appendText("append"); | ||
area.deleteText(0, text.length()); | ||
assertEquals(0, caret.getPosition()); | ||
}); | ||
} | ||
|
||
@Test | ||
public void position_is_correct_when_change_occurs_at_position() { | ||
interact(() -> { | ||
caret.moveToAreaEnd(); | ||
int pos = caret.getPosition(); | ||
|
||
String append = "some"; | ||
// add test | ||
area.appendText(append); | ||
assertEquals(pos + append.length(), caret.getPosition()); | ||
|
||
// reset | ||
caret.moveTo(pos); | ||
|
||
// delete test | ||
area.deleteText(pos, area.getLength()); | ||
assertEquals(pos, caret.getPosition()); | ||
}); | ||
} | ||
|
||
@Test | ||
public void position_is_correct_when_change_occurs_after_position() { | ||
interact(() -> { | ||
caret.moveTo(0); | ||
|
||
// add test | ||
String append = "some"; | ||
area.appendText(append); | ||
assertEquals(0, caret.getPosition()); | ||
|
||
// delete test | ||
int length = area.getLength(); | ||
area.deleteText(length - append.length(), length); | ||
assertEquals(0, caret.getPosition()); | ||
}); | ||
} | ||
|
||
} |