Skip to content

Commit 4bc7299

Browse files
Merge pull request #532 from JordanMartinez/refactorMore
Refactor API tests to separate classes and package; point to UndoUtils in area's javadoc
2 parents 51b2306 + 9c128d8 commit 4bc7299

11 files changed

Lines changed: 310 additions & 284 deletions

File tree

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package org.fxmisc.richtext.api;
2+
3+
import javafx.stage.Stage;
4+
import org.fxmisc.richtext.Caret;
5+
import org.fxmisc.richtext.InlineCssTextAreaAppTest;
6+
import org.junit.Test;
7+
import org.testfx.util.WaitForAsyncUtils;
8+
9+
import static org.junit.Assert.assertFalse;
10+
import static org.junit.Assert.assertTrue;
11+
12+
public class CaretTests extends InlineCssTextAreaAppTest {
13+
14+
@Override
15+
public void start(Stage stage) throws Exception {
16+
super.start(stage);
17+
18+
// insure caret is always visible
19+
area.setShowCaret(Caret.CaretVisibility.ON);
20+
21+
StringBuilder sb = new StringBuilder();
22+
for (int i = 0; i < 50; i++) {
23+
sb.append(i).append("\n");
24+
}
25+
area.replaceText(sb.toString());
26+
area.moveTo(0);
27+
area.showParagraphAtTop(0);
28+
}
29+
30+
@Test
31+
public void testMoveCaretAndFollowIt() {
32+
assertTrue(area.getCaretBounds().isPresent());
33+
34+
// move caret outside of viewport
35+
area.moveTo(area.getLength());
36+
area.requestFollowCaret();
37+
38+
// needed for test to pass
39+
WaitForAsyncUtils.waitForFxEvents();
40+
41+
// viewport should update itself so caret is visible again
42+
assertTrue(area.getCaretBounds().isPresent());
43+
}
44+
45+
@Test
46+
public void testMoveCaretWithoutFollowingIt() {
47+
assertTrue(area.getCaretBounds().isPresent());
48+
49+
// move caret outside of viewport
50+
area.moveTo(area.getLength());
51+
52+
// caret should not be visible
53+
assertFalse(area.getCaretBounds().isPresent());
54+
}
55+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.fxmisc.richtext.api;
2+
3+
import javafx.geometry.Bounds;
4+
import javafx.stage.Stage;
5+
import org.fxmisc.richtext.InlineCssTextAreaAppTest;
6+
import org.junit.Test;
7+
8+
import static org.junit.Assert.assertEquals;
9+
10+
public class CharacterBoundsTest extends InlineCssTextAreaAppTest {
11+
12+
@Override
13+
public void start(Stage stage) throws Exception {
14+
super.start(stage);
15+
area.replaceText("a line of sample text");
16+
}
17+
18+
@Test
19+
public void selectionBoundsUnchangedWhenCallGetCharacterBounds() {
20+
area.selectAll();
21+
Bounds bounds = area.getSelectionBounds().get();
22+
23+
// getCharacterBoundsOnScreen() uses the selection shape to calculate the bounds
24+
// so insure it doesn't affect the selection shape if something is selected
25+
// before it gets called
26+
area.getCharacterBoundsOnScreen(0, area.getLength() - 1);
27+
28+
assertEquals(bounds, area.getSelectionBounds().get());
29+
}
30+
31+
}
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
package org.fxmisc.richtext.api;
2+
3+
import com.nitorcreations.junit.runners.NestedRunner;
4+
import javafx.geometry.Bounds;
5+
import javafx.geometry.Insets;
6+
import javafx.geometry.Point2D;
7+
import javafx.geometry.Pos;
8+
import org.fxmisc.richtext.InlineCssTextAreaAppTest;
9+
import org.fxmisc.richtext.model.NavigationActions;
10+
import org.junit.Before;
11+
import org.junit.Test;
12+
import org.junit.runner.RunWith;
13+
14+
import java.util.function.Consumer;
15+
16+
import static javafx.scene.input.MouseButton.PRIMARY;
17+
import static org.junit.Assert.assertEquals;
18+
19+
@RunWith(NestedRunner.class)
20+
public class HitTests extends InlineCssTextAreaAppTest {
21+
22+
private void moveCaretToAreaEnd() {
23+
area.moveTo(area.getLength());
24+
}
25+
26+
public class WhenAreaIsPadded {
27+
28+
double paddingAmount = 20;
29+
30+
public class AndHitsOccurOutsideArea {
31+
32+
String text = "text";
33+
String fullText = text + "\n" + text;
34+
35+
@Before
36+
public void setup() {
37+
interact(() -> area.replaceText(fullText));
38+
}
39+
40+
@Test
41+
public void clickingInTopPaddingMovesCaretToTopLine() {
42+
interact(() -> area.setPadding(new Insets(paddingAmount, 0, 0, 0)));
43+
44+
moveCaretToAreaEnd();
45+
moveTo(position(Pos.TOP_LEFT, 1, 2)).clickOn(PRIMARY);
46+
assertEquals(0, area.getCurrentParagraph());
47+
48+
moveCaretToAreaEnd();
49+
moveTo(position(Pos.TOP_CENTER, 0, 0)).clickOn(PRIMARY);
50+
assertEquals(0, area.getCurrentParagraph());
51+
}
52+
53+
@Test
54+
public void clickingInLeftPaddingMovesCaretToBeginningOfLineOnSingleLineParagraph() {
55+
interact(() -> area.setPadding(new Insets(0, 0, 0, paddingAmount)));
56+
57+
moveCaretToAreaEnd();
58+
moveTo(position(Pos.TOP_LEFT, 1, 1)).clickOn(PRIMARY);
59+
assertEquals(0, area.getCaretColumn());
60+
}
61+
62+
@Test
63+
public void clickingInRightPaddingMovesCaretToEndOfLineOnSingleLineParagraph() {
64+
interact(() -> {
65+
area.setPadding(new Insets(0, paddingAmount, 0, 0));
66+
area.moveTo(0);
67+
68+
// insure we're scrolled all the way to the right
69+
area.scrollBy(new Point2D(100, 0));
70+
});
71+
72+
moveTo(position(Pos.TOP_RIGHT, -1, 1)).clickOn(PRIMARY);
73+
assertEquals(area.getParagraphLenth(0), area.getCaretColumn());
74+
}
75+
76+
@Test
77+
public void clickingInBottomPaddingMovesCaretToBottomLine() {
78+
interact(() -> {
79+
area.setPadding(new Insets(0, 0, paddingAmount, 0));
80+
area.moveTo(0);
81+
82+
// insure we're scrolled all the way to the bottom
83+
area.scrollBy(new Point2D(0, 100));
84+
});
85+
86+
moveTo(position(Pos.BOTTOM_CENTER, 0, -2)).clickOn(PRIMARY);
87+
assertEquals(1, area.getCurrentParagraph());
88+
}
89+
90+
}
91+
92+
public class AndHitsOccurInsideArea {
93+
94+
String text = "abcdefghijklmnopqrstuvwxyz";
95+
String fullText;
96+
97+
{
98+
int totalPars = 50;
99+
int indexLimit = totalPars - 1;
100+
StringBuilder sb = new StringBuilder();
101+
Consumer<Integer> appendParagraph = i -> sb.append("Par #").append(i).append(" ").append(text);
102+
for (int i = 0; i < indexLimit; i++) {
103+
appendParagraph.accept(i);
104+
sb.append("\n");
105+
}
106+
appendParagraph.accept(indexLimit);
107+
fullText = sb.toString();
108+
}
109+
110+
@Before
111+
public void setup() {
112+
interact(() -> {
113+
area.replaceText(fullText);
114+
area.setPadding(new Insets(paddingAmount));
115+
area.setStyle("-fx-font-family: monospace; -fx-font-size: 12pt;");
116+
});
117+
}
118+
119+
@Test
120+
public void clickingCharacterShouldMoveCaretToThatPosition() {
121+
int start = area.getAbsolutePosition(3, 8);
122+
Bounds b = area.getCharacterBoundsOnScreen(start, start + 1).get();
123+
moveTo(b).clickOn(PRIMARY);
124+
assertEquals(start, area.getCaretPosition());
125+
}
126+
127+
@Test
128+
public void prevPageMovesCaretToTopOfPage() {
129+
// Mac: failed; Windows: untested
130+
// TODO: test on respective OS and update expected values to be correct
131+
run_only_on_linux();
132+
133+
area.showParagraphAtBottom(area.getParagraphs().size() - 1);
134+
// move to last line, column 0
135+
area.moveTo(area.getParagraphs().size() - 1, 0);
136+
137+
interact(() -> {
138+
// hit is called here
139+
area.prevPage(NavigationActions.SelectionPolicy.CLEAR);
140+
});
141+
142+
assertEquals(0, area.getCaretColumn());
143+
assertEquals(32, area.getCurrentParagraph());
144+
}
145+
146+
@Test
147+
public void nextPageMovesCaretToBottomOfPage() {
148+
// Mac: failed; Windows: untested
149+
// TODO: test on respective OS and update expected values to be correct
150+
run_only_on_linux();
151+
152+
area.showParagraphAtTop(0);
153+
area.moveTo(0);
154+
155+
interact(() -> {
156+
// hit is called here
157+
area.nextPage(NavigationActions.SelectionPolicy.CLEAR);
158+
});
159+
160+
assertEquals(0, area.getCaretColumn());
161+
assertEquals(17, area.getCurrentParagraph());
162+
}
163+
164+
}
165+
166+
}
167+
168+
}

richtextfx/src/integrationTest/java/org/fxmisc/richtext/view/MouseOverTextDelayTests.java renamed to richtextfx/src/integrationTest/java/org/fxmisc/richtext/api/MouseOverTextDelayTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.fxmisc.richtext.view;
1+
package org.fxmisc.richtext.api;
22

33
import javafx.beans.property.SimpleBooleanProperty;
44
import javafx.geometry.Pos;
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.fxmisc.richtext.api;
2+
3+
import org.fxmisc.richtext.InlineCssTextAreaAppTest;
4+
import org.junit.Test;
5+
6+
import static org.junit.Assert.assertEquals;
7+
import static org.junit.Assert.assertFalse;
8+
9+
public class ParagraphLinesCountTests extends InlineCssTextAreaAppTest {
10+
11+
@Test
12+
public void multiLineReturnsCorrectCount() {
13+
String[] lines = {
14+
"01 02 03 04 05",
15+
"11 12 13 14 15",
16+
"21 22 23 24 25"
17+
};
18+
interact(() -> {
19+
area.setWrapText(true);
20+
stage.setWidth(120);
21+
area.replaceText(String.join(" ", lines));
22+
});
23+
24+
assertEquals(3, area.getParagraphLinesCount(0));
25+
}
26+
27+
@Test
28+
public void singleLineReturnsOne() {
29+
interact(() -> area.replaceText("some text"));
30+
assertFalse(area.isWrapText());
31+
32+
assertEquals(1, area.getParagraphLinesCount(0));
33+
}
34+
35+
}

richtextfx/src/integrationTest/java/org/fxmisc/richtext/view/UndoManagerTests.java renamed to richtextfx/src/integrationTest/java/org/fxmisc/richtext/api/UndoManagerTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.fxmisc.richtext.view;
1+
package org.fxmisc.richtext.api;
22

33
import org.fxmisc.richtext.InlineCssTextAreaAppTest;
44
import org.fxmisc.richtext.util.UndoUtils;

0 commit comments

Comments
 (0)