Skip to content

Commit

Permalink
Add test: caret pos updated correctly when change occurs at its pos
Browse files Browse the repository at this point in the history
  • Loading branch information
JordanMartinez committed Jul 3, 2018
1 parent 4f5a2cb commit d6685f6
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 0 deletions.
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());
}
}
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());
});
}

}

0 comments on commit d6685f6

Please sign in to comment.