Skip to content

Commit

Permalink
Always provide a default not empty decoration (#346)
Browse files Browse the repository at this point in the history
* Always provide a default not empty decoration

* Add test
  • Loading branch information
jperedadnr committed Jun 25, 2024
1 parent 9a8d486 commit 56ddec0
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,10 @@ public String toString() {
", paragraphDecoration=" + paragraphDecoration +
'}';
}

public static DecorationModel createDefaultDecorationModel(int length) {
return new DecorationModel(0, length,
TextDecoration.builder().presets().build(),
ParagraphDecoration.builder().presets().build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ public Document(String text) {

public Document(String text, int caretPosition) {
this(text,
List.of(new DecorationModel(0, text.length(),
TextDecoration.builder().presets().build(),
ParagraphDecoration.builder().presets().build())),
List.of(DecorationModel.createDefaultDecorationModel(text.length())),
caretPosition);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,10 @@ public List<DecorationModel> getDecorationModelList(int start, int end) {
return (end <= tp);
});
}
if (mergedList.isEmpty()) {
// provide a default decoration
mergedList.add(DecorationModel.createDefaultDecorationModel(0));
}
return mergedList;
}

Expand Down
62 changes: 62 additions & 0 deletions rta/src/test/java/com/gluonhq/richtextarea/ui/RTATest.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,68 @@ public void basicPromptDemoTest(FxRobot robot) {
"Type something!".equals(((Text) node).getText()));
}

@Test
public void emptyDocumentDemoTest(FxRobot robot) {
run(() -> {
String text = "Hello RTA";
Document document = new Document(text);
richTextArea.getActionFactory().open(document).execute(new ActionEvent());
});
waitForFxEvents();

verifyThat(".rich-text-area", node -> node instanceof RichTextArea);
verifyThat(".rich-text-area", NodeMatchers.isFocused());
RichTextArea rta = robot.lookup(".rich-text-area").query();
assertEquals(9, rta.getTextLength());
assertEquals(9, rta.getCaretPosition());

Document document = rta.getDocument();
assertNotNull(document);
assertEquals(0, document.getCaretPosition());
assertEquals("Hello RTA", document.getText());
assertNotNull(document.getDecorations());
assertEquals(1, document.getDecorations().size());
assertInstanceOf(TextDecoration.class, document.getDecorations().get(0).getDecoration());
TextDecoration td = (TextDecoration) document.getDecorations().get(0).getDecoration();
assertEquals("black", td.getForeground());
assertEquals("transparent", td.getBackground());
assertEquals("System", td.getFontFamily());
assertEquals(14, td.getFontSize());
assertEquals(NORMAL, td.getFontWeight());
assertEquals(REGULAR, td.getFontPosture());

robot.push(new KeyCodeCombination(A, SHORTCUT_DOWN));
waitForFxEvents();
run(() -> {
richTextArea.getActionFactory().cut().execute(new ActionEvent());
richTextArea.getActionFactory().save().execute(new ActionEvent());
});
waitForFxEvents();
Document emptyDocument = rta.getDocument();

assertEquals(0, rta.getTextLength());
assertEquals(0, rta.getCaretPosition());
assertNotNull(emptyDocument);
assertEquals(0, emptyDocument.getCaretPosition());
assertEquals("", emptyDocument.getText());
assertNotNull(emptyDocument.getDecorations());
assertEquals(1, emptyDocument.getDecorations().size());

run(() -> richTextArea.getActionFactory().open(emptyDocument).execute(new ActionEvent()));
waitForFxEvents();

document = rta.getDocument();
assertEquals(0, rta.getTextLength());
assertEquals(0, rta.getCaretPosition());
assertNotNull(document);
assertEquals(emptyDocument, document);
assertEquals(0, document.getCaretPosition());
assertEquals("", document.getText());
assertNotNull(document.getDecorations());
assertEquals(1, document.getDecorations().size());

}

@Test
public void basicDocumentDemoTest(FxRobot robot) {
run(() -> {
Expand Down

0 comments on commit 56ddec0

Please sign in to comment.