1 /* 2 * Copyright (c) 2018, 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.javafx.stage; 27 28 import java.io.FileNotFoundException; 29 import java.io.PrintWriter; 30 import java.io.StringWriter; 31 import java.util.TimerTask; 32 import java.util.Timer; 33 import java.util.concurrent.CountDownLatch; 34 import java.util.concurrent.TimeUnit; 35 36 import javafx.application.Application; 37 import javafx.application.Platform; 38 import javafx.beans.value.ChangeListener; 39 import javafx.beans.value.ObservableValue; 40 import javafx.scene.control.Alert; 41 import javafx.scene.control.Label; 42 import javafx.scene.control.TextArea; 43 import javafx.scene.layout.GridPane; 44 import javafx.scene.layout.Priority; 45 import javafx.stage.Stage; 46 47 import org.junit.BeforeClass; 48 import org.junit.Test; 49 50 import static junit.framework.Assert.assertTrue; 51 import static org.junit.Assert.fail; 52 53 public class MakeResizableAndResizeTest { 54 static CountDownLatch startupLatch; 55 static Timer timer; 56 static Runnable runNext; 57 static volatile Alert alert; 58 59 public static void main(String[] args) { 60 initFX(); 61 new MakeResizableAndResizeTest().testSize(); 62 } 63 64 @BeforeClass 65 public static void initFX() { 66 startupLatch = new CountDownLatch(1); 67 new Thread(() -> Application.launch(TestApp.class, (String[])null)).start(); 68 try { 69 if (!startupLatch.await(15, TimeUnit.SECONDS)) { 70 fail("Timeout waiting for FX runtime to start"); 71 } 72 } catch (InterruptedException ex) { 73 fail("Unexpected exception: " + ex); 74 } 75 } 76 77 @Test 78 public void testSize() { 79 assertTrue("Wrong window width", alert.getWidth() >= alert.getDialogPane().getWidth()); 80 assertTrue("Wrong window height", alert.getHeight() >= alert.getDialogPane().getHeight()); 81 } 82 83 public static class TestApp extends Application implements ChangeListener { 84 85 86 @Override 87 public void start(Stage stage) throws Exception { 88 Alert alert = new Alert(Alert.AlertType.ERROR); 89 alert.setTitle("Exception Dialog"); 90 alert.setHeaderText("ERROR"); 91 alert.setContentText("Exception: ..."); 92 93 Exception ex = new FileNotFoundException("Could not find file ..."); 94 95 // Create expandable Exception. 96 StringWriter sw = new StringWriter(); 97 PrintWriter pw = new PrintWriter(sw); 98 ex.printStackTrace(pw); 99 String exceptionText = sw.toString(); 100 101 Label label = new Label("The exception stacktrace was:"); 102 103 TextArea textArea = new TextArea(exceptionText); 104 textArea.setEditable(false); 105 textArea.setWrapText(true); 106 107 textArea.setMaxWidth(Double.MAX_VALUE); 108 textArea.setMaxHeight(Double.MAX_VALUE); 109 GridPane.setVgrow(textArea, Priority.ALWAYS); 110 GridPane.setHgrow(textArea, Priority.ALWAYS); 111 112 GridPane expContent = new GridPane(); 113 expContent.setMaxWidth(Double.MAX_VALUE); 114 expContent.add(label, 0, 0); 115 expContent.add(textArea, 0, 1); 116 117 // Set expandable Exception into the dialog pane. 118 alert.getDialogPane().setExpandableContent(expContent); 119 alert.xProperty().addListener(this); 120 alert.yProperty().addListener(this); 121 alert.widthProperty().addListener(this); 122 alert.heightProperty().addListener(this); 123 124 MakeResizableAndResizeTest.alert = alert; 125 runNext = () -> { 126 System.out.println("Alert window created."); 127 runNext = () -> {}; 128 Platform.runLater(this::resize); 129 }; 130 alert.showAndWait(); 131 } 132 133 @Override 134 public void changed(ObservableValue observable, Object oldValue, Object newValue) { 135 if (timer != null) { 136 timer.cancel(); 137 } 138 timer = new Timer(); 139 timer.schedule(new TimerTask() { 140 @Override 141 public void run() { 142 runNext.run(); 143 } 144 }, 1500); 145 } 146 147 void resize() { 148 alert.getDialogPane().setExpanded(true); 149 System.out.println("Details expanded."); 150 runNext = startupLatch::countDown; 151 } 152 } 153 154 } 155