forked from FXMisc/RichTextFX
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue FXMisc#594: Properly calculate the background and underline sha…
…pes for non-consecutive ranges
- Loading branch information
Showing
2 changed files
with
165 additions
and
1 deletion.
There are no files selected for viewing
159 changes: 159 additions & 0 deletions
159
richtextfx/src/integrationTest/java/org/fxmisc/richtext/style/StylingTests.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,159 @@ | ||
package org.fxmisc.richtext.style; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.fxmisc.flowless.Cell; | ||
import org.fxmisc.flowless.VirtualFlow; | ||
import org.fxmisc.richtext.InlineCssTextAreaAppTest; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import com.nitorcreations.junit.runners.NestedRunner; | ||
|
||
import javafx.scene.layout.Region; | ||
import javafx.scene.shape.Path; | ||
import javafx.scene.text.Text; | ||
import javafx.scene.text.TextFlow; | ||
|
||
@RunWith(NestedRunner.class) | ||
public class StylingTests extends InlineCssTextAreaAppTest { | ||
|
||
private final static String firstWord = "Hello "; | ||
private final static String styledWord1 = "World"; | ||
private final static String moreText = " and also the "; | ||
private final static String styledWord2 = "Sun"; | ||
private final static String remainingLine = " and Moon"; | ||
|
||
|
||
/** | ||
* | ||
* @param index The index of the desired paragraph box | ||
* @return The paragraph box for the paragraph at the specified index | ||
*/ | ||
private Region getParagraphBox(int index) { | ||
VirtualFlow flow = (VirtualFlow) area.getChildrenUnmodifiable().get(index); | ||
Cell gsa = (Cell) flow.getCell(0); | ||
|
||
// get the ParagraphBox (protected subclass of Region) | ||
return (Region) gsa.getNode(); | ||
} | ||
|
||
|
||
/** | ||
* @return A list of text nodes which render the current text. | ||
*/ | ||
private List<Text> getTextNodes() { | ||
// get the ParagraphBox (protected subclass of Region) | ||
Region paragraphBox = getParagraphBox(0); | ||
|
||
// get the ParagraphText (protected subclass of TextFlow) | ||
TextFlow tf = (TextFlow) paragraphBox.getChildrenUnmodifiable().get(0); | ||
|
||
List<Text> result = new ArrayList<>(); | ||
tf.getChildrenUnmodifiable().filtered(n -> n instanceof Text).forEach(n -> result.add((Text) n)); | ||
return result; | ||
} | ||
|
||
/** | ||
* @return A list of nodes which render the underlines for the current text. | ||
*/ | ||
private List<Path> getUnderlinePaths() { | ||
// get the ParagraphBox (protected subclass of Region) | ||
Region paragraphBox = getParagraphBox(0); | ||
|
||
// get the ParagraphText (protected subclass of TextFlow) | ||
TextFlow tf = (TextFlow) paragraphBox.getChildrenUnmodifiable().get(0); | ||
|
||
List<Path> result = new ArrayList<>(); | ||
tf.getChildrenUnmodifiable().filtered(n -> n instanceof Path).forEach(n -> result.add((Path) n)); | ||
This comment has been minimized.
Sorry, something went wrong. |
||
return result.subList(2, result.size()); | ||
} | ||
|
||
|
||
@Test | ||
public void simpleStyling() { | ||
// setup | ||
interact(() -> { | ||
area.replaceText(firstWord + styledWord1 + remainingLine); | ||
}); | ||
|
||
// expected: one text node which contains the complete text | ||
List<Text> textNodes = getTextNodes(); | ||
assertEquals(1, textNodes.size()); | ||
|
||
// Set word "World" to bold | ||
interact(() -> { | ||
area.setStyle(firstWord.length(), firstWord.length() + styledWord1.length(), "-fx-font-weight: bold;"); | ||
}); | ||
|
||
// expected: three text nodes | ||
textNodes = getTextNodes(); | ||
assertEquals(3, textNodes.size()); | ||
|
||
Text first = textNodes.get(0); | ||
assertEquals("Hello ", first.getText()); | ||
assertEquals("Regular", first.getFont().getStyle()); | ||
|
||
Text second = textNodes.get(1); | ||
assertEquals("World", second.getText()); | ||
assertEquals("Bold", second.getFont().getStyle()); | ||
|
||
Text third = textNodes.get(2); | ||
assertEquals(" and Moon", third.getText()); | ||
assertEquals("Regular", third.getFont().getStyle()); | ||
} | ||
|
||
|
||
@Test | ||
public void underlineStyling() { | ||
// setup | ||
interact(() -> { | ||
area.replaceText(firstWord + styledWord1 + moreText + styledWord2 + remainingLine); | ||
// "Hello World and also the Sun and Moon" | ||
}); | ||
|
||
// expected: one text node which contains the complete text | ||
List<Text> textNodes = getTextNodes(); | ||
assertEquals(1, textNodes.size()); | ||
assertEquals(firstWord + styledWord1 + moreText + styledWord2 + remainingLine, | ||
textNodes.get(0).getText()); | ||
|
||
interact(() -> { | ||
final int start1 = firstWord.length(); | ||
final int end1 = start1 + styledWord1.length(); | ||
area.setStyle(start1, end1, | ||
"-rtfx-underline-color: red; -rtfx-underline-dash-array: 2 2; -rtfx-underline-width: 1; -rtfx-underline-cap: butt;"); | ||
|
||
final int start2 = end1 + moreText.length(); | ||
final int end2 = start2 + styledWord2.length(); | ||
System.err.printf("%d / %d\n", start2, end2); | ||
area.setStyle(start2, end2, | ||
"-rtfx-underline-color: red; -rtfx-underline-dash-array: 2 2; -rtfx-underline-width: 1; -rtfx-underline-cap: butt;"); | ||
|
||
// "Hello World and also the Sun and Moon" | ||
// ----- --- | ||
}); | ||
|
||
// expected: five text nodes | ||
textNodes = getTextNodes(); | ||
assertEquals(5, textNodes.size()); | ||
|
||
Text first = textNodes.get(0); | ||
assertEquals(firstWord, first.getText()); | ||
Text second = textNodes.get(1); | ||
assertEquals(styledWord1, second.getText()); | ||
Text third = textNodes.get(2); | ||
assertEquals(moreText, third.getText()); | ||
Text fourth = textNodes.get(3); | ||
assertEquals(styledWord2, fourth.getText()); | ||
Text fifth = textNodes.get(4); | ||
assertEquals(remainingLine, fifth.getText()); | ||
|
||
// determine the underline paths - need to be two of them! | ||
List<Path> underlineNodes = getUnderlinePaths(); | ||
assertEquals(2, underlineNodes.size()); | ||
} | ||
} |
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
Won't this return the background shapes and border shapes as well if we add more tests that test for those things, too.