Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions app/display/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,24 @@
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testfx</groupId>
<artifactId>testfx-core</artifactId>
<version>4.0.18</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testfx</groupId>
<artifactId>testfx-junit5</artifactId>
<version>4.0.18</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.python</groupId>
<artifactId>jython-standalone</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public ToolkitRepresentation<Parent, Node> openNewWindow(final DisplayModel mode
// model will be opened in it.
return representModelInNewDockItem(model);
}

@Override
public ToolkitRepresentation<Parent, Node> openStandaloneWindow(final DisplayModel model,
final Consumer<DisplayModel> close_handler)
Expand All @@ -114,8 +114,13 @@ public ToolkitRepresentation<Parent, Node> openPanel(final DisplayModel model,
final String name,
final Consumer<DisplayModel> 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?
Expand All @@ -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<Parent, Node> 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 <code>null</code>
* @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<Parent, Node> representModelInNewDockItem(final DisplayModel model)
Expand Down
Original file line number Diff line number Diff line change
@@ -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
*
* <p>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

Check warning on line 29 in app/display/runtime/src/test/java/org/csstudio/display/builder/runtime/app/StandaloneNewTabUI.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Rename class "StandaloneNewTabUI" to match the regular expression: '^((Test|IT)[a-zA-Z0-9_]+|[A-Z][a-zA-Z0-9_]*(Test|Tests|TestCase|IT|ITCase))$'

See more on https://sonarcloud.io/project/issues?id=ControlSystemStudio_phoebus&issues=AaHgHbiAGhjC-EaUiedy&open=AaHgHbiAGhjC-EaUiedy&pullRequest=4023
{
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<DockItem> 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<DockItem> 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<String> labels(final DockPane dockPane)
{
return onUiThread(() -> dockPane.getDockItems().stream().map(DockItem::getLabel).toList());
}

/** @param <T> Result type
* @param callable Code that reads the UI
* @return Its result, read on the UI thread
*/
private static <T> T onUiThread(final Callable<T> callable)
{
return WaitForAsyncUtils.waitFor(WaitForAsyncUtils.asyncFx(callable));
}
}
20 changes: 20 additions & 0 deletions app/display/runtime/src/test/resources/standalone/tab_a.bob
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<display version="2.0.0">
<name>Display a</name>
<width>300</width>
<height>100</height>
<widget type="action_button" version="3.0.0">
<name>Open b</name>
<x>10</x>
<y>10</y>
<width>120</width>
<height>40</height>
<actions>
<action type="open_display">
<file>tab_b.bob</file>
<target>tab</target>
<description>Open b</description>
</action>
</actions>
</widget>
</display>
10 changes: 10 additions & 0 deletions app/display/runtime/src/test/resources/standalone/tab_b.bob
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<display version="2.0.0">
<name>Display b</name>
<width>300</width>
<height>100</height>
<widget type="label" version="2.0.0">
<name>Label</name>
<text>Display b</text>
</widget>
</display>
Loading