Edit form controls and render localized application previews in the IntelliJ designer
This commit is contained in:
@@ -0,0 +1,134 @@
|
||||
package de.neitzel.fxdesigner.kit;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import de.neitzel.fxdesigner.contracts.*;
|
||||
import java.net.URI;
|
||||
import java.util.*;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class FormDocumentTest {
|
||||
private String source = "<?import javafx.scene.layout.GridPane?>\n<GridPane/>";
|
||||
|
||||
private FormDocument doc() throws Exception {
|
||||
return new FormDocument(source);
|
||||
}
|
||||
|
||||
private FormDocument.Item item(String id) throws Exception {
|
||||
return doc().items.stream().filter(i -> i.id().equals(id)).findFirst().orElseThrow();
|
||||
}
|
||||
|
||||
private FormDocument.Slot slot(String owner, String property) throws Exception {
|
||||
return doc().slots.stream()
|
||||
.filter(s -> s.owner().id().equals(owner) && s.property().equals(property))
|
||||
.findFirst()
|
||||
.orElseThrow();
|
||||
}
|
||||
|
||||
private void apply(FormDocument.Edit edit) throws Exception {
|
||||
source =
|
||||
new EditBatch(1, "form", edit.changes())
|
||||
.applyTo(new DocumentSnapshot(URI.create("file:///test.fxml"), 1, source, true, false));
|
||||
doc();
|
||||
}
|
||||
|
||||
private void add(String type, String owner, String property) throws Exception {
|
||||
var target = slot(owner, property);
|
||||
apply(doc().add(type, target, doc().size(target)));
|
||||
}
|
||||
|
||||
private void set(String id, String name, String value) throws Exception {
|
||||
var doc = doc();
|
||||
var item = item(id);
|
||||
var field =
|
||||
doc.fields(item).stream().filter(f -> f.name().equals(name)).findFirst().orElseThrow();
|
||||
apply(doc.set(item, field, value == null ? "" : value, value == null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void emptyGridAndNamedListsKeepReferencesAndRejectCycles() throws Exception {
|
||||
assertEquals(
|
||||
9, FormDocument.TYPES.values().stream().filter(n -> n.startsWith("de.neitzel.")).count());
|
||||
for (String type :
|
||||
List.of(
|
||||
"Label",
|
||||
"TextField",
|
||||
"NonNegativeIntegerSpinner",
|
||||
"StatusBar",
|
||||
"ToolBarPane",
|
||||
"ToolBar")) add(type, "", "children");
|
||||
add("Label", "statusBar", "leftItems");
|
||||
add("Label", "statusBar", "rightItems");
|
||||
source = source.replace("</GridPane>", "<!--preserve--></GridPane>");
|
||||
apply(doc().move(item("label2"), slot("statusBar", "rightItems"), 0));
|
||||
assertTrue(source.indexOf("id=\"label2\"") < source.indexOf("id=\"label3\""));
|
||||
add("TagChip", "toolBarPane", "items");
|
||||
set("tagChip", "removable", "true");
|
||||
set("tagChip", "onRemove", "#removeTag");
|
||||
set("toolBarPane", "dismissible", "true");
|
||||
set("toolBarPane", "dismissText", "Schließen");
|
||||
set("toolBarPane", "onDismiss", "#dismiss");
|
||||
apply(doc().move(item("tagChip"), slot("toolBar", "items"), 0));
|
||||
var toolbar = source.substring(source.indexOf("<ToolBar "), source.indexOf("</ToolBar>"));
|
||||
assertTrue(
|
||||
toolbar.contains("onRemove=\"#removeTag\"") && toolbar.contains("removable=\"true\""));
|
||||
assertTrue(source.contains("onDismiss=\"#dismiss\"") && source.contains("<!--preserve-->"));
|
||||
assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> doc().move(item("toolBar"), slot("toolBar", "items"), 0));
|
||||
assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> doc().add("Label", new FormDocument.Slot(item("textField"), "children"), 0));
|
||||
assertThrows(
|
||||
IllegalArgumentException.class, () -> doc().add("Label", slot("", "children"), 999));
|
||||
}
|
||||
|
||||
@Test
|
||||
void propertiesValidateResetAndRoundTrip() throws Exception {
|
||||
add("NonNegativeIntegerSpinner", "", "children");
|
||||
for (var p : List.of("maximum", "initialValue", "minimum"))
|
||||
set(
|
||||
"nonNegativeIntegerSpinner",
|
||||
p,
|
||||
switch (p) {
|
||||
case "maximum" -> "42";
|
||||
case "minimum" -> "2";
|
||||
default -> "8";
|
||||
});
|
||||
assertThrows(
|
||||
IllegalArgumentException.class, () -> set("nonNegativeIntegerSpinner", "maximum", "6"));
|
||||
assertThrows(
|
||||
IllegalArgumentException.class, () -> set("nonNegativeIntegerSpinner", "minimum", "-1"));
|
||||
set("nonNegativeIntegerSpinner", "number", "10");
|
||||
add("AutoCompleteTextField", "", "children");
|
||||
for (var name :
|
||||
List.of("promptText", "styleClass", "padding", "GridPane.columnIndex", "suggestions")) {
|
||||
String value =
|
||||
switch (name) {
|
||||
case "padding" -> "1 2 3 4";
|
||||
case "GridPane.columnIndex" -> "1";
|
||||
case "suggestions" -> "A & B\nPosteingang";
|
||||
default -> "Eingabe";
|
||||
};
|
||||
set("autoCompleteTextField", name, value);
|
||||
var d = doc();
|
||||
var item = item("autoCompleteTextField");
|
||||
var f = d.fields(item).stream().filter(x -> name.equals(x.name())).findFirst().orElseThrow();
|
||||
assertEquals(name.equals("padding") ? "1.0 2.0 3.0 4.0" : value, d.value(item, f));
|
||||
set("autoCompleteTextField", name, null);
|
||||
assertFalse(source.contains("<" + name + ">"));
|
||||
assertEquals("", doc().value(item("autoCompleteTextField"), f));
|
||||
}
|
||||
assertThrows(IllegalArgumentException.class, () -> set("autoCompleteTextField", "width", "20"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void implicitToolbarChildrenStayInOneList() throws Exception {
|
||||
source =
|
||||
"<?import de.neitzel.fx.control.ToolBarPane?><?import"
|
||||
+ " javafx.scene.control.Label?><ToolBarPane><Label/></ToolBarPane>";
|
||||
add("Label", "", "items");
|
||||
assertEquals(2, doc().size(slot("", "items")));
|
||||
assertFalse(source.contains("<items>"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user