Files
NeitzelFXDesigner/integration-tests/idea/ImportWorkflowProbe.java
T

231 lines
9.6 KiB
Java

package qualification;
import static qualification.WorkflowProbe.*;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.LocalFileSystem;
import de.neitzel.fxdesigner.protocol.BindingMetadata.Target;
import java.nio.file.*;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.control.*;
import javafx.scene.image.ImageView;
import javax.imageio.ImageIO;
/** Exercises generic discovery, persists two bindings, then drives the actual target views. */
public final class ImportWorkflowProbe extends FormWorkflowProbe {
@Override
void verify(Project project) throws Exception {
open(project, "ConnectionForm", 2);
discover();
target("securityChoices", "view", "security", "items", "COMPATIBLE");
target("protocol", "both", "protocol", "value", "COMPATIBLE");
target("security", "both", "security", "value", "COMPATIBLE");
open(project, "FolderTab", 1);
discover();
target("view", "both", "view", "value", "COMPATIBLE");
target("grouping", "both", "grouping", "value", "COMPATIBLE");
target("view", "both", "grouping", "value", "CONVERTER_REQUIRED");
target("rows", "view", "profiles", "items", "INCOMPATIBLE");
target("rows", "view", "clearLayout", "accessibleHelp", "INCOMPATIBLE");
target("view", "both", "untypedPick", "value", "UNRESOLVED");
target("rows", "view", "grid", "items", "COMPATIBLE");
applyAndReopen(project, "FolderTab", 1, "grid", "items", "rows", "view");
preview("archive-folder", "import-grid");
open(project, "ImportForm", 3);
discover();
target("folder", "both", "folders", "value", "COMPATIBLE");
applyAndReopen(project, "ImportForm", 3, "folders", "value", "folder", "both");
preview("import-free", "import-free");
preview("import-collision", "import-collision");
preview("import-busy", "import-busy");
Files.writeString(output.resolve("SUCCESS.txt"), Files.readString(output.resolve("steps.txt")));
}
private void open(Project project, String form, int count) throws Exception {
var path =
Path.of(project.getBasePath(), "src/main/resources/de/neitzel/archiver/ui", form + ".fxml");
file = edt(() -> LocalFileSystem.getInstance().refreshAndFindFileByNioFile(path));
pane = openDesigner(project, file);
fx(pane::flush).toCompletableFuture().get(10, java.util.concurrent.TimeUnit.SECONDS);
click(pane, "Form preview");
click(pane, "Build and Refresh");
await(
() -> fx(() -> ((ComboBox<?>) pane.lookup("#preview-example")).getItems().size() == count));
// The initial native document sync invalidates discovery while the first preview is loading.
// Wait for the displayed revision before selecting a source in the inspector.
await(
() ->
fx(
() -> {
String state = ((Label) pane.lookup("#preview-state")).getText();
if (state.startsWith("Preview failed")
&& form.equals("FolderTab")
&& !((TextArea) pane.lookup("#fxml-source"))
.getText()
.contains("path=\"rows\"")) return true;
if (state.startsWith("Preview failed")
|| state.startsWith("Preview unavailable")
|| state.startsWith("Preview blocked"))
throw new AssertionError(form + " initial preview: " + state);
return ((ImageView) pane.lookup("#full-preview-image")).getImage() != null
&& state.contains("revision");
}));
step("Opened " + form + " with " + count + " examples");
}
private void discover() throws Exception {
fx(
() -> {
var group =
(TitledPane)
pane.lookupAll(".titled-pane").stream()
.filter(n -> n instanceof TitledPane t && t.getText().equals("Bindings"))
.findFirst()
.orElseThrow();
group.setAnimated(false);
group.setExpanded(true);
var tabs = (TabPane) group.getContent();
tabs.getSelectionModel()
.select(
tabs.getTabs().stream()
.filter(t -> t.getText().equals("Discover targets"))
.findFirst()
.orElseThrow());
pane.applyCss();
pane.layout();
return null;
});
click(pane, "Discover bindings");
await(
() ->
fx(
() -> {
if (!((ComboBox<?>) pane.lookup("#binding-path")).getItems().isEmpty())
return true;
var message = ((Label) pane.lookup("#binding-status")).getText();
if (message.startsWith("Document changed")
|| message.startsWith("Build changed")) {
((Button)
pane.lookupAll(".button").stream()
.filter(
n ->
n instanceof Button b
&& b.getText().equals("Discover bindings"))
.findFirst()
.orElseThrow())
.fire();
}
if (message.startsWith("Discovery unavailable"))
throw new AssertionError(message);
return false;
}));
}
@SuppressWarnings("unchecked")
private void target(String path, String dir, String id, String property, String status)
throws Exception {
var prefix = path + " : ";
choose("binding-direction", dir);
await(
() ->
fx(
() ->
((ComboBox<?>) pane.lookup("#binding-path"))
.getItems().stream().anyMatch(x -> x.toString().startsWith(prefix))));
choose("binding-path", prefix);
await(
() ->
fx(
() -> {
var paths = (ComboBox<?>) pane.lookup("#binding-path");
var direction = (ComboBox<?>) pane.lookup("#binding-direction");
if (paths.getValue() == null
|| !paths.getValue().toString().startsWith(prefix)
|| !dir.equals(direction.getValue())) return false;
var table = (TableView<Target>) pane.lookup("#binding-targets");
var found =
table.getItems().stream()
.filter(
t ->
t.control().equals(id)
&& t.property().equals(property)
&& t.status().equals(status))
.findFirst();
found.ifPresent(t -> table.getSelectionModel().select(t));
return found.isPresent();
}));
step(path + " -> " + id + "." + property + " (" + dir + ") " + status);
}
private void applyAndReopen(
Project project, String form, int count, String id, String prop, String path, String dir)
throws Exception {
click(pane, "Use selected target");
if (!fx(
() ->
((TextField) pane.lookup("#direct-path")).getText().equals(path)
&& ((ComboBox<?>) pane.lookup("#direct-direction")).getValue().equals(dir)))
throw new AssertionError("Wrong binding");
click(pane, "Apply binding");
String declaration =
"control=\""
+ id
+ "\" property=\""
+ prop
+ "\" path=\""
+ path
+ "\" direction=\""
+ dir
+ "\"";
await(() -> source().contains(declaration));
click(pane, "Save");
await(() -> Files.readString(Path.of(file.getPath())).contains(declaration));
edt(
() -> {
FileEditorManager.getInstance(project).closeFile(file);
return null;
});
open(project, form, count);
if (!source().contains(declaration)) throw new AssertionError("Binding was lost on reopen");
step("Applied, saved and reopened " + declaration);
}
private void preview(String example, String attempt) throws Exception {
choose("preview-example", example);
String label =
fx(
() ->
((de.neitzel.fxdesigner.workflow.DesignerWorkflow.Example)
((ComboBox<?>) pane.lookup("#preview-example")).getValue())
.label());
await(
() ->
fx(
() -> {
String state = ((Label) pane.lookup("#preview-state")).getText();
if (state.startsWith("Preview failed")
|| state.startsWith("Preview blocked")
|| state.startsWith("Preview unavailable")) throw new AssertionError(state);
return ((ImageView) pane.lookup("#full-preview-image")).getImage() != null
&& state.contains(label);
}));
if (attempt.equals("import-collision"))
fx(
() -> {
ImageIO.write(
SwingFXUtils.fromFXImage(pane.snapshot(null, null), null),
"png",
output.resolve("intellij.png").toFile());
return null;
});
Files.writeString(output.resolve("import-attempt"), attempt);
click(pane, "Interactive preview");
awaitTarget("result-" + attempt);
step(Files.readString(output.resolve("result-" + attempt)));
click(pane, "Stop preview");
}
}