diff --git a/app/display/runtime/pom.xml b/app/display/runtime/pom.xml index 6f5d4678ba..21eefdf421 100644 --- a/app/display/runtime/pom.xml +++ b/app/display/runtime/pom.xml @@ -20,6 +20,24 @@ 1.3 test + + org.testfx + testfx-core + 4.0.18 + test + + + org.testfx + testfx-junit5 + 4.0.18 + test + + + junit + junit + + + org.python jython-standalone diff --git a/app/display/runtime/src/main/java/org/csstudio/display/builder/runtime/app/DockItemRepresentation.java b/app/display/runtime/src/main/java/org/csstudio/display/builder/runtime/app/DockItemRepresentation.java index 6d88de053f..2e9aefd8fd 100644 --- a/app/display/runtime/src/main/java/org/csstudio/display/builder/runtime/app/DockItemRepresentation.java +++ b/app/display/runtime/src/main/java/org/csstudio/display/builder/runtime/app/DockItemRepresentation.java @@ -101,7 +101,7 @@ public ToolkitRepresentation openNewWindow(final DisplayModel mode // model will be opened in it. return representModelInNewDockItem(model); } - + @Override public ToolkitRepresentation openStandaloneWindow(final DisplayModel model, final Consumer close_handler) @@ -114,8 +114,13 @@ public ToolkitRepresentation openPanel(final DisplayModel model, final String name, final Consumer close_handler) throws Exception { - // By default, open in the pane used by this display + // By default, open in the pane used by this display. + // A standalone window shows no tabs, so a new tab would hide this display: + // open it in the main window, as for applications started from a standalone window. DockPane pane = app_instance.getDockItem().getDockPane(); + final boolean from_standalone = isStandalone(pane); + if (from_standalone) + pane = DockPane.getMainDockPane(); if (name.length() > 0) { // Should the new panel open in a specific, named pane? @@ -129,9 +134,26 @@ else if (pane != null) logger.log(Level.WARNING, "Cannot locate pane to create new '" + name + "'"); } + if (isStandalone(pane)) + // No usable main window either, for example because its pane is locked. + // A new window keeps this display visible. + return openNewWindow(model, close_handler); + // System.out.println("Open panel in " + app_instance.dock_item.getDockPane()); DockPane.setActiveDockPane(pane); - return representModelInNewDockItem(model); + final ToolkitRepresentation representation = representModelInNewDockItem(model); + if (from_standalone && representation instanceof DockItemRepresentation shown) + // The main window may be behind the standalone window + shown.app_instance.raise(); + return representation; + } + + /** @param pane {@link DockPane}, may be null + * @return Is it the pane of a standalone window, which shows no tabs? + */ + private static boolean isStandalone(final DockPane pane) + { + return pane != null && pane.isStandaloneWindow(); } private ToolkitRepresentation representModelInNewDockItem(final DisplayModel model) diff --git a/app/display/runtime/src/test/java/org/csstudio/display/builder/runtime/app/StandaloneNewTabUI.java b/app/display/runtime/src/test/java/org/csstudio/display/builder/runtime/app/StandaloneNewTabUI.java new file mode 100644 index 0000000000..b5d5c635c8 --- /dev/null +++ b/app/display/runtime/src/test/java/org/csstudio/display/builder/runtime/app/StandaloneNewTabUI.java @@ -0,0 +1,151 @@ +package org.csstudio.display.builder.runtime.app; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import java.util.List; +import java.util.concurrent.Callable; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; +import org.phoebus.framework.jobs.JobManager; +import org.phoebus.ui.docking.DockItem; +import org.phoebus.ui.docking.DockPane; +import org.phoebus.ui.docking.DockStage; +import org.phoebus.ui.docking.Geometry; +import org.testfx.framework.junit5.ApplicationTest; +import org.testfx.util.WaitForAsyncUtils; + +import javafx.stage.Stage; + +/** A display whose action button opens another display in a new tab + * + *

A standalone window shows no tabs, + * so the new tab must not go to the standalone window and hide the display. + */ +@SuppressWarnings("nls") +class StandaloneNewTabUI extends ApplicationTest +{ + private DockPane mainPane; + private DockPane pane; + + @Override + public void start(final Stage primaryStage) + { + final Stage mainStage = new Stage(); + mainPane = DockStage.configureStage(mainStage); + mainStage.getProperties().put(DockStage.KEY_ID, DockStage.ID_MAIN); + mainStage.show(); + } + + @AfterEach + void closeWindows() throws Exception + { + // Close the displays the same way as their tabs would + final List tabs = onUiThread(StandaloneNewTabUI::allTabs); + for (DockItem tab : tabs) + tab.prepareToClose(); + interact(() -> + { + tabs.forEach(DockItem::close); + DockStage.getDockStages().forEach(Stage::close); + }); + } + + @Test + void testNewTabFromStandaloneWindowOpensInMainWindow() throws Exception + { + openDisplayInNewWindow(true); + openNewTab(); + + assertThat(labels(pane), equalTo(List.of("Display a"))); + assertThat(labels(mainPane), equalTo(List.of("Display b"))); + } + + @Test + void testNewTabFromNormalWindowStaysInThatWindow() throws Exception + { + openDisplayInNewWindow(false); + openNewTab(); + + assertThat(labels(pane), equalTo(List.of("Display a", "Display b"))); + assertThat(labels(mainPane), equalTo(List.of())); + } + + @Test + void testNewTabFromStandaloneWindowWithLockedMainWindowOpensNewWindow() throws Exception + { + interact(() -> mainPane.setFixed(true)); + openDisplayInNewWindow(true); + openNewTab(); + + assertThat(labels(pane), equalTo(List.of("Display a"))); + assertThat(labels(mainPane), equalTo(List.of())); + assertThat(onUiThread(() -> DockStage.getDockStages().size()), equalTo(3)); + } + + /** Open 'Display a' in a new window, whose pane becomes the 'pane' of the test + * @param standalone Open a standalone window, or a normal one? + */ + private void openDisplayInNewWindow(final boolean standalone) throws Exception + { + final DisplayInfo info = DisplayInfo.forURI(getClass().getResource("/standalone/tab_a.bob").toURI()); + interact(() -> + { + final Stage stage = new Stage(); + pane = DockStage.configureStage(stage, new Geometry(null), standalone); + stage.show(); + final DisplayRuntimeInstance instance = new DisplayRuntimeInstance(new DisplayRuntimeApplication()); + instance.loadDisplayFile(info); + }); + waitForLoads(); + } + + /** Click the button of 'Display a' that opens 'Display b' in a new tab, wait for 'Display b' */ + private void openNewTab() throws Exception + { + clickOn("Open b"); + // A tab gets the display name once the display is represented + WaitForAsyncUtils.waitFor(10, TimeUnit.SECONDS, + () -> onUiThread(() -> allTabs().stream() + .map(DockItem::getLabel) + .anyMatch("Display b"::equals))); + waitForLoads(); + } + + /** Wait until no display is loading and the UI has handled all pending events */ + private static void waitForLoads() throws TimeoutException + { + WaitForAsyncUtils.waitFor(10, TimeUnit.SECONDS, () -> JobManager.getJobCount() == 0); + WaitForAsyncUtils.waitForFxEvents(); + } + + /** @return Tabs of all windows. Call on the UI thread */ + private static List allTabs() + { + return DockStage.getDockStages() + .stream() + .flatMap(stage -> DockStage.getDockPanes(stage).stream()) + .flatMap(dockPane -> dockPane.getDockItems().stream()) + .toList(); + } + + /** @param dockPane {@link DockPane} + * @return Labels of its tabs + */ + private static List labels(final DockPane dockPane) + { + return onUiThread(() -> dockPane.getDockItems().stream().map(DockItem::getLabel).toList()); + } + + /** @param Result type + * @param callable Code that reads the UI + * @return Its result, read on the UI thread + */ + private static T onUiThread(final Callable callable) + { + return WaitForAsyncUtils.waitFor(WaitForAsyncUtils.asyncFx(callable)); + } +} diff --git a/app/display/runtime/src/test/resources/standalone/tab_a.bob b/app/display/runtime/src/test/resources/standalone/tab_a.bob new file mode 100644 index 0000000000..edb254aba1 --- /dev/null +++ b/app/display/runtime/src/test/resources/standalone/tab_a.bob @@ -0,0 +1,20 @@ + + + Display a + 300 + 100 + + Open b + 10 + 10 + 120 + 40 + + + tab_b.bob + tab + Open b + + + + diff --git a/app/display/runtime/src/test/resources/standalone/tab_b.bob b/app/display/runtime/src/test/resources/standalone/tab_b.bob new file mode 100644 index 0000000000..8852cd94e1 --- /dev/null +++ b/app/display/runtime/src/test/resources/standalone/tab_b.bob @@ -0,0 +1,10 @@ + + + Display b + 300 + 100 + + Label + Display b + +