|
| 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 | +} |
0 commit comments