Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.fxmisc.richtext;

import java.util.function.Function;

public class TextBuildingUtils {

private TextBuildingUtils() {
throw new IllegalStateException("Cannot construct an instance of TextBuildingUtils");
}

/** Builds {@code totalNumber} of lines that each have the index of the line as their text */
public static String buildLines(int totalNumber) { return buildLines(totalNumber, String::valueOf); }

public static String buildLines(int totalNumber, Function<Integer, String> textOnEachLine) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < totalNumber - 1; i++) {
sb.append(textOnEachLine.apply(i)).append("\n");
}
sb.append(textOnEachLine.apply(totalNumber));
return sb.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.fxmisc.richtext.api;

import javafx.stage.Stage;
import org.fxmisc.richtext.InlineCssTextAreaAppTest;
import org.fxmisc.richtext.TextBuildingUtils;
import org.junit.Test;

import java.util.Optional;

import static org.junit.Assert.assertEquals;

public class ParagraphIndexMappingTests extends InlineCssTextAreaAppTest {

private static final int TOTAL_NUMBER_OF_LINES = 80;
private static final int LAST_PAR_INDEX = TOTAL_NUMBER_OF_LINES - 1;
private static final String CONTENT = TextBuildingUtils.buildLines(TOTAL_NUMBER_OF_LINES);

@Override
public void start(Stage stage) throws Exception {
super.start(stage);
area.replaceText(CONTENT);
}

@Test
public void all_par_to_visible_par_index_is_correct() {
interact(() -> area.showParagraphAtTop(0));
assertEquals(Optional.of(0), area.allParToVisibleParIndex(0));

interact(() -> area.showParagraphAtBottom(LAST_PAR_INDEX));
assertEquals(Optional.of(area.getVisibleParagraphs().size() - 1), area.allParToVisibleParIndex(LAST_PAR_INDEX));
}

@Test
public void visible_par_to_all_par_index_is_correct() {
interact(() -> area.showParagraphAtTop(0));
assertEquals(0, area.visibleParToAllParIndex(0));

interact(() -> area.showParagraphAtBottom(LAST_PAR_INDEX));
assertEquals(LAST_PAR_INDEX, area.visibleParToAllParIndex(area.getVisibleParagraphs().size() - 1));

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import javafx.geometry.Bounds;
import javafx.stage.Stage;
import org.fxmisc.richtext.InlineCssTextAreaAppTest;
import org.fxmisc.richtext.TextBuildingUtils;
import org.junit.Test;

import static javafx.scene.input.KeyCode.PAGE_DOWN;
Expand All @@ -13,17 +14,7 @@

public class PageUpDownTests extends InlineCssTextAreaAppTest {

private static final String EIGHT_LINES;

static {
StringBuilder sb = new StringBuilder();
int totalLines = 8;
for (int i = 0; i < totalLines - 1; i++) {
sb.append(i).append("\n");
}
sb.append(totalLines);
EIGHT_LINES = sb.toString();
}
private static final String EIGHT_LINES = TextBuildingUtils.buildLines(8);

@Override
public void start(Stage stage) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -792,9 +792,9 @@ public final double getViewportHeight() {
@Override
public final Optional<Integer> allParToVisibleParIndex(int allParIndex) {
if (allParIndex < 0) {
throw new IllegalArgumentException("Visible paragraph index cannot be negative but was " + allParIndex);
throw new IllegalArgumentException("The given paragraph index (allParIndex) cannot be negative but was " + allParIndex);
}
if (allParIndex >= getVisibleParagraphs().size()) {
if (allParIndex >= getParagraphs().size()) {
throw new IllegalArgumentException(String.format(
"Paragraphs' last index is [%s] but allParIndex was [%s]",
getParagraphs().size() - 1, allParIndex)
Expand Down