|
| 1 | +/* |
| 2 | + * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. Oracle designates this |
| 8 | + * particular file as subject to the "Classpath" exception as provided |
| 9 | + * by Oracle in the LICENSE file that accompanied this code. |
| 10 | + * |
| 11 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | + * accompanied this code). |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License version |
| 18 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | + * |
| 21 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 22 | + * or visit www.oracle.com if you need additional information or have any |
| 23 | + * questions. |
| 24 | + */ |
| 25 | + |
| 26 | +package test.robot.javafx.scene.dialog; |
| 27 | + |
| 28 | +import javafx.application.Application; |
| 29 | +import javafx.application.Platform; |
| 30 | +import javafx.scene.Scene; |
| 31 | +import javafx.scene.control.Button; |
| 32 | +import javafx.scene.control.ButtonType; |
| 33 | +import javafx.scene.control.Dialog; |
| 34 | +import javafx.scene.robot.Robot; |
| 35 | +import javafx.stage.Stage; |
| 36 | +import javafx.stage.StageStyle; |
| 37 | +import javafx.stage.WindowEvent; |
| 38 | +import javafx.scene.input.MouseButton; |
| 39 | + |
| 40 | +import org.junit.AfterClass; |
| 41 | +import org.junit.Assert; |
| 42 | +import org.junit.BeforeClass; |
| 43 | +import org.junit.Test; |
| 44 | +import test.util.Util; |
| 45 | + |
| 46 | +import java.util.concurrent.CountDownLatch; |
| 47 | +import java.util.concurrent.TimeUnit; |
| 48 | + |
| 49 | +//see JDK-8193502 |
| 50 | +public class DialogWithOwnerSizingTest { |
| 51 | + static Robot robot; |
| 52 | + static Button button; |
| 53 | + static Stage stage; |
| 54 | + static Scene scene; |
| 55 | + static Dialog<ButtonType> dialog; |
| 56 | + static Dialog<ButtonType> dialog2; |
| 57 | + static CountDownLatch startupLatch; |
| 58 | + static CountDownLatch dialogShownLatch; |
| 59 | + static CountDownLatch dialogHideLatch; |
| 60 | + |
| 61 | + @Test(timeout = 15000) |
| 62 | + public void dialogWithOwnerSizingTest() throws Exception { |
| 63 | + Thread.sleep(500); |
| 64 | + clickButton(); |
| 65 | + Thread.sleep(500); |
| 66 | + |
| 67 | + try { |
| 68 | + Assert.assertEquals(dialog2.getDialogPane().getWidth(), dialog.getDialogPane().getWidth(), 2.0); |
| 69 | + Assert.assertEquals(dialog2.getDialogPane().getHeight(), dialog.getDialogPane().getHeight(), 2.0); |
| 70 | + } finally { |
| 71 | + hide(); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + private void clickButton() throws Exception { |
| 76 | + dialogShownLatch = new CountDownLatch(2); |
| 77 | + mouseClick(button.getLayoutX() + button.getWidth() / 2, button.getLayoutY() + button.getHeight() / 2); |
| 78 | + |
| 79 | + waitForLatch(dialogShownLatch, 10, "Failed to show Dialog"); |
| 80 | + } |
| 81 | + |
| 82 | + private void hide() throws Exception { |
| 83 | + dialogHideLatch = new CountDownLatch(2); |
| 84 | + Platform.runLater(() -> { |
| 85 | + dialog.close(); |
| 86 | + dialog2.close(); |
| 87 | + }); |
| 88 | + waitForLatch(dialogHideLatch, 10, "Failed to hide Dialog"); |
| 89 | + } |
| 90 | + |
| 91 | + @BeforeClass |
| 92 | + public static void initFX() throws Exception { |
| 93 | + startupLatch = new CountDownLatch(1); |
| 94 | + new Thread(() -> Application.launch(SizingTestApp.class, (String[]) null)).start(); |
| 95 | + waitForLatch(startupLatch, 15, "FX runtime failed to start."); |
| 96 | + } |
| 97 | + |
| 98 | + @AfterClass |
| 99 | + public static void exit() { |
| 100 | + Platform.runLater(() -> stage.hide()); |
| 101 | + Platform.exit(); |
| 102 | + } |
| 103 | + |
| 104 | + private void mouseClick(double x, double y) { |
| 105 | + Util.runAndWait(() -> { |
| 106 | + robot.mouseMove((int) (scene.getWindow().getX() + scene.getX() + x), |
| 107 | + (int) (scene.getWindow().getY() + scene.getY() + y)); |
| 108 | + robot.mousePress(MouseButton.PRIMARY); |
| 109 | + robot.mouseRelease(MouseButton.PRIMARY); |
| 110 | + }); |
| 111 | + } |
| 112 | + |
| 113 | + public static class SizingTestApp extends Application { |
| 114 | + @Override |
| 115 | + public void start(Stage primaryStage) { |
| 116 | + robot = new Robot(); |
| 117 | + stage = primaryStage; |
| 118 | + stage.setAlwaysOnTop(true); |
| 119 | + |
| 120 | + button = new Button("Open Dialogs"); |
| 121 | + |
| 122 | + scene = new Scene(button, 200, 200); |
| 123 | + stage.setScene(scene); |
| 124 | + |
| 125 | + stage.initStyle(StageStyle.UNDECORATED); |
| 126 | + stage.setOnShown(e -> Platform.runLater(startupLatch::countDown)); |
| 127 | + |
| 128 | + dialog = getTestDialog(true); |
| 129 | + dialog2 = getTestDialog(false); |
| 130 | + |
| 131 | + button.setOnAction(evt -> { |
| 132 | + dialog.show(); |
| 133 | + dialog2.show(); |
| 134 | + }); |
| 135 | + |
| 136 | + stage.show(); |
| 137 | + } |
| 138 | + |
| 139 | + private static Dialog<ButtonType> getTestDialog(boolean withOwner) { |
| 140 | + final Dialog<ButtonType> testDialog = new Dialog<>(); |
| 141 | + testDialog.setTitle("Multi-line Dialog"); |
| 142 | + testDialog.setContentText("This\nis\na\ntest\nmulti\nline\nblah"); |
| 143 | + testDialog.getDialogPane().getButtonTypes().add(ButtonType.CLOSE); |
| 144 | + |
| 145 | + testDialog.getDialogPane().getScene().getWindow().addEventHandler(WindowEvent.WINDOW_SHOWN, |
| 146 | + e -> Platform.runLater(dialogShownLatch::countDown)); |
| 147 | + |
| 148 | + testDialog.getDialogPane().getScene().getWindow().addEventHandler(WindowEvent.WINDOW_HIDDEN, |
| 149 | + e -> Platform.runLater(dialogHideLatch::countDown)); |
| 150 | + |
| 151 | + if (withOwner) { |
| 152 | + testDialog.initOwner(stage); |
| 153 | + } |
| 154 | + |
| 155 | + return testDialog; |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + public static void waitForLatch(CountDownLatch latch, int seconds, String msg) throws Exception { |
| 160 | + Assert.assertTrue("Timeout: " + msg, latch.await(seconds, TimeUnit.SECONDS)); |
| 161 | + } |
| 162 | +} |
0 commit comments