Test docking activation independently of desktop focus arbitration
Glass Headless can report both stages focused. On macOS the desktop can deny the input needed to establish the initial focus. Drive both reported window states explicitly with a Stage spy so the real manager must distinguish NoChange from Applied and request focus.
This commit is contained in:
@@ -9,125 +9,53 @@ import de.neitzel.fx.docking.Dockable;
|
|||||||
import de.neitzel.fx.docking.DockableId;
|
import de.neitzel.fx.docking.DockableId;
|
||||||
import de.neitzel.fx.docking.DockingManager;
|
import de.neitzel.fx.docking.DockingManager;
|
||||||
import de.neitzel.fx.docking.DockingOptions;
|
import de.neitzel.fx.docking.DockingOptions;
|
||||||
import javafx.animation.AnimationTimer;
|
|
||||||
import javafx.application.Platform;
|
|
||||||
import javafx.beans.value.ChangeListener;
|
|
||||||
import javafx.geometry.Bounds;
|
|
||||||
import javafx.scene.Scene;
|
import javafx.scene.Scene;
|
||||||
import javafx.scene.control.TextArea;
|
import javafx.scene.control.TextArea;
|
||||||
import javafx.scene.input.MouseButton;
|
|
||||||
import javafx.scene.robot.Robot;
|
|
||||||
import javafx.stage.Stage;
|
import javafx.stage.Stage;
|
||||||
import org.junit.jupiter.api.BeforeAll;
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import java.util.concurrent.CountDownLatch;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
import java.util.concurrent.atomic.AtomicReference;
|
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
|
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
|
||||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
import static org.mockito.Mockito.clearInvocations;
|
||||||
|
import static org.mockito.Mockito.doReturn;
|
||||||
|
import static org.mockito.Mockito.spy;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
|
||||||
/** Activating a background window is an applied action even when its selected tab stays the same. */
|
/** Activating a background window is an applied action even when its selected tab stays the same. */
|
||||||
class ActivateDockingWindowTest {
|
class ActivateDockingWindowTest {
|
||||||
@BeforeAll
|
@BeforeAll
|
||||||
static void startJavaFx() throws Exception {
|
static void startJavaFx() throws Exception {
|
||||||
FxThread.start();
|
FxThread.start();
|
||||||
FxThread.onFx(() -> Platform.setImplicitExit(false));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void activatingTheSameContentInABackgroundWindowReportsApplied() throws Exception {
|
void activatingTheSameContentInABackgroundWindowReportsApplied() throws Exception {
|
||||||
AtomicReference<Fixture> state = new AtomicReference<>();
|
|
||||||
FxThread.onFx(() -> {
|
FxThread.onFx(() -> {
|
||||||
DockLayout initial = new DockLayout();
|
DockLayout initial = new DockLayout();
|
||||||
initial.setCenter(new DockGroupDefinition("editors"));
|
initial.setCenter(new DockGroupDefinition("editors"));
|
||||||
DockPane pane = new DockPane();
|
DockPane pane = new DockPane();
|
||||||
pane.setInitialLayout(initial);
|
pane.setInitialLayout(initial);
|
||||||
Stage main = new Stage();
|
Stage main = spy(new Stage());
|
||||||
main.setScene(new Scene(pane, 480, 280));
|
main.setScene(new Scene(pane, 480, 280));
|
||||||
DockingManager manager = DockingManager.attach(main, pane, new DockingOptions());
|
DockingManager manager = DockingManager.attach(main, pane, new DockingOptions());
|
||||||
TextArea editor = new TextArea("Unsaved text");
|
TextArea editor = new TextArea("Unsaved text");
|
||||||
Dockable dockable = new Dockable(new DockableId("notes"), editor);
|
Dockable dockable = new Dockable(new DockableId("notes"), editor);
|
||||||
manager.open(dockable, DockTarget.area("editors"));
|
manager.open(dockable, DockTarget.area("editors"));
|
||||||
Stage other = new Stage();
|
|
||||||
other.setScene(new Scene(new TextArea("Other window"), 240, 160));
|
// Only the desktop's window state is simulated. Selection, scene focus and
|
||||||
state.set(new Fixture(main, other, manager, dockable));
|
// the activation result all come from the real controls and manager.
|
||||||
main.show();
|
doReturn(true).when(main).isShowing();
|
||||||
|
doReturn(true).when(main).isFocused();
|
||||||
manager.activate(dockable.getId());
|
manager.activate(dockable.getId());
|
||||||
});
|
assertSame(dockable, manager.activeDockable());
|
||||||
Fixture fixture = state.get();
|
assertSame(editor, main.getScene().getFocusOwner());
|
||||||
try {
|
assertInstanceOf(ActionResult.NoChange.class, manager.activate(dockable.getId()));
|
||||||
focus(fixture.main());
|
|
||||||
FxThread.onFx(() -> fixture.other().show());
|
|
||||||
focus(fixture.other());
|
|
||||||
FxThread.onFx(() -> {
|
|
||||||
assertFalse(fixture.main().isFocused());
|
|
||||||
assertSame(fixture.dockable(), fixture.manager().activeDockable());
|
|
||||||
assertSame(fixture.dockable().getContent(), fixture.main().getScene().getFocusOwner());
|
|
||||||
assertInstanceOf(ActionResult.Applied.class, fixture.manager().activate(fixture.dockable().getId()));
|
|
||||||
});
|
|
||||||
} finally {
|
|
||||||
FxThread.onFx(() -> {
|
|
||||||
fixture.other().hide();
|
|
||||||
fixture.main().hide();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void focus(Stage stage) throws Exception {
|
doReturn(false).when(main).isFocused();
|
||||||
CountDownLatch focused = new CountDownLatch(1);
|
clearInvocations(main);
|
||||||
ChangeListener<Boolean> listener = (observable, old, current) -> {
|
assertInstanceOf(ActionResult.Applied.class, manager.activate(dockable.getId()));
|
||||||
if (current) focused.countDown();
|
verify(main).requestFocus();
|
||||||
};
|
|
||||||
AnimationTimer clickAfterDisplay = new AnimationTimer() {
|
|
||||||
private int pulses;
|
|
||||||
private long lastClick;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void handle(long now) {
|
|
||||||
if (stage.isFocused()) {
|
|
||||||
focused.countDown();
|
|
||||||
stop();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (++pulses < 2) return;
|
|
||||||
if (now - lastClick < 250_000_000) return;
|
|
||||||
lastClick = now;
|
|
||||||
Bounds bounds = stage.getScene().getRoot().localToScreen(
|
|
||||||
stage.getScene().getRoot().getBoundsInLocal());
|
|
||||||
Robot robot = new Robot();
|
|
||||||
robot.mouseMove(bounds.getCenterX(), bounds.getCenterY());
|
|
||||||
robot.mouseClick(MouseButton.PRIMARY);
|
|
||||||
if (stage.isFocused()) focused.countDown();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
FxThread.onFx(() -> {
|
|
||||||
stage.focusedProperty().addListener(listener);
|
|
||||||
stage.setAlwaysOnTop(true);
|
|
||||||
stage.toFront();
|
|
||||||
stage.requestFocus();
|
|
||||||
stage.getScene().getRoot().applyCss();
|
|
||||||
stage.getScene().getRoot().layout();
|
|
||||||
clickAfterDisplay.start();
|
|
||||||
});
|
|
||||||
try {
|
|
||||||
// A native click activates the application even if the previous test or
|
|
||||||
// another application owned desktop focus. RequestFocus alone cannot
|
|
||||||
// establish that precondition reliably on macOS. FX pulses do not
|
|
||||||
// guarantee that the native compositor has displayed the window;
|
|
||||||
// retry only this focus precondition within the bounded wait.
|
|
||||||
assertTrue(focused.await(5, TimeUnit.SECONDS), "Desktop did not focus the requested window");
|
|
||||||
} finally {
|
|
||||||
FxThread.onFx(() -> {
|
|
||||||
clickAfterDisplay.stop();
|
|
||||||
stage.focusedProperty().removeListener(listener);
|
|
||||||
stage.setAlwaysOnTop(false);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private record Fixture(Stage main, Stage other, DockingManager manager, Dockable dockable) { }
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user