tests/system/src/test/java/javafx/stage/ShowAndWaitTest.java

Print this page
rev 8895 : RT-39689: Better docs and error checking if Printing or Dialogs called from Animation

@@ -29,15 +29,21 @@
 import java.util.Collection;
 import java.util.HashSet;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicReference;
+import javafx.animation.KeyFrame;
+import javafx.animation.Timeline;
 import javafx.application.Application;
 import javafx.application.Platform;
+import javafx.print.PrinterJob;
 import javafx.scene.Group;
 import javafx.scene.Scene;
 import javafx.scene.paint.Color;
+import javafx.scene.shape.Rectangle;
+import javafx.util.Duration;
 import junit.framework.AssertionFailedError;
 import org.junit.After;
 import org.junit.AfterClass;
 import org.junit.Before;
 import org.junit.BeforeClass;

@@ -645,6 +651,92 @@
         for (int i = 0; i < N; i++) {
             assertFalse(tmpStage[i].isShowing());
         }
     }
 
+    // Verify that showAndWait throws an exception if called from an
+    // animation timeline.
+    @Test
+    public void testTimeline() throws Throwable {
+        ensureTest1();
+
+        final AtomicBoolean animationDone = new AtomicBoolean(false);
+        final AtomicReference<Throwable> error = new AtomicReference<>(null);
+
+        KeyFrame kf = new KeyFrame(Duration.millis(200), e -> {
+            try {
+                tmpStage1 = new TestStage(modality);
+                stages.add(tmpStage1);
+                assertFalse(tmpStage1.isPrimary());
+                assertFalse(tmpStage1.isShowing());
+                try {
+                    tmpStage1.showAndWait();
+                    fail("Did not get expected exception from showAndWait");
+                } catch (IllegalStateException ex) {
+                    // Good
+                }
+                assertFalse(tmpStage1.isShowing());
+            } catch (Throwable t) {
+                error.set(t);
+            }
+            animationDone.set(true);
+        });
+        Timeline timeline = new Timeline(kf);
+        timeline.play();
+
+        Util.sleep(1000);
+
+        if (error.get() != null) {
+            throw error.get();
+        }
+        assertTrue(animationDone.get());
+        assertFalse(tmpStage1.isShowing());
+    }
+
+    // Verify that printing throws an exception if called from an
+    // animation timeline.
+    @Test
+    public void testTimelinePrint() throws Throwable {
+        ensureTest1();
+
+        final AtomicBoolean animationDone = new AtomicBoolean(false);
+        final AtomicReference<Throwable> error = new AtomicReference<>(null);
+
+        KeyFrame kf = new KeyFrame(Duration.millis(200), e -> {
+            try {
+                PrinterJob job = PrinterJob.createPrinterJob();
+                try {
+                    job.showPrintDialog(myApp.primaryStage);
+                    fail("Did not get expected exception from showPrintDialog");
+                } catch (IllegalStateException ex) {
+                    // Good
+                }
+                try {
+                    job.showPageSetupDialog(myApp.primaryStage);
+                    fail("Did not get expected exception from showPageSetupDialog");
+                } catch (IllegalStateException ex) {
+                    // Good
+                }
+                try {
+                    Rectangle rect = new Rectangle(200, 100, Color.GREEN);
+                    job.printPage(rect);
+                    fail("Did not get expected exception from printPage");
+                } catch (IllegalStateException ex) {
+                    // Good
+                }
+            } catch (Throwable t) {
+                error.set(t);
+            }
+            animationDone.set(true);
+        });
+        Timeline timeline = new Timeline(kf);
+        timeline.play();
+
+        Util.sleep(1000);
+
+        if (error.get() != null) {
+            throw error.get();
+        }
+        assertTrue(animationDone.get());
+    }
+
 }