From 5b0d3b554aa73d1e92a5f449a3f44808ab3b5f35 Mon Sep 17 00:00:00 2001 From: Thiago Milczarek Sayao Date: Wed, 7 Dec 2022 15:19:23 +0000 Subject: [PATCH] 8296621: Stage steals focus on scene change Reviewed-by: kcr, mstrauss --- .../sun/javafx/tk/quantum/WindowStage.java | 1 - .../SceneChangeShouldNotFocusStageTest.java | 93 +++++++++++++++++++ 2 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 tests/system/src/test/java/test/robot/javafx/scene/SceneChangeShouldNotFocusStageTest.java diff --git a/modules/javafx.graphics/src/main/java/com/sun/javafx/tk/quantum/WindowStage.java b/modules/javafx.graphics/src/main/java/com/sun/javafx/tk/quantum/WindowStage.java index b4b42487397..72afbe26fb3 100644 --- a/modules/javafx.graphics/src/main/java/com/sun/javafx/tk/quantum/WindowStage.java +++ b/modules/javafx.graphics/src/main/java/com/sun/javafx/tk/quantum/WindowStage.java @@ -273,7 +273,6 @@ StageStyle getStyle() { newScene.updateSceneState(); return null; }); - requestFocus(); } else { QuantumToolkit.runWithRenderLock(() -> { // platformWindow can be null here, if this window is owned, diff --git a/tests/system/src/test/java/test/robot/javafx/scene/SceneChangeShouldNotFocusStageTest.java b/tests/system/src/test/java/test/robot/javafx/scene/SceneChangeShouldNotFocusStageTest.java new file mode 100644 index 00000000000..4ba463448c5 --- /dev/null +++ b/tests/system/src/test/java/test/robot/javafx/scene/SceneChangeShouldNotFocusStageTest.java @@ -0,0 +1,93 @@ +/* + * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package test.robot.javafx.scene; + +import javafx.animation.Animation; +import javafx.animation.KeyFrame; +import javafx.animation.Timeline; +import javafx.application.Application; +import javafx.application.Platform; +import javafx.scene.Scene; +import javafx.scene.control.TextField; +import javafx.scene.layout.Pane; +import javafx.stage.Stage; +import javafx.util.Duration; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import test.util.Util; + +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class SceneChangeShouldNotFocusStageTest { + static Stage stage; + static CountDownLatch startupLatch = new CountDownLatch(1); + + @Test + void windowShouldRemainIconified() { + Util.sleep(2000); + assertTrue(stage.isIconified(), "Stage should be iconified"); + } + + @BeforeAll + public static void initFX() throws Exception { + Util.launch(startupLatch, TestApp.class); + } + + @AfterAll + public static void exit() { + Util.shutdown(stage); + } + + public static class TestApp extends Application { + @Override + public void start(Stage primaryStage) { + stage = primaryStage; + Scene scene1 = new Scene(new Pane(new TextField("This is scene1")), 200, 200); + Scene scene2 = new Scene(new Pane(new TextField("This is scene2")), 200, 200); + + Timeline tl = new Timeline(); + tl.setCycleCount(Animation.INDEFINITE); + tl.getKeyFrames().addAll(new KeyFrame(Duration.millis(200), e -> stage.setScene(scene1)), + new KeyFrame(Duration.millis(400), e -> stage.setScene(scene2))); + + stage.setOnShown(e -> { + tl.play(); + startupLatch.countDown(); + }); + + stage.setIconified(true); + stage.show(); + } + } + + public static void waitForLatch(CountDownLatch latch, int seconds, String msg) throws Exception { + assertTrue(latch.await(seconds, TimeUnit.SECONDS), "Timeout: " + msg); + } +}