1 /*
   2  * Copyright (c) 2012, 2014, 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 javafx.scene;
  27 
  28 import util.Util;
  29 import javafx.scene.image.WritableImage;
  30 import javafx.stage.Stage;
  31 import java.util.concurrent.CountDownLatch;
  32 import java.util.concurrent.TimeUnit;
  33 import javafx.application.Application;
  34 import javafx.application.Platform;
  35 import javafx.scene.paint.Color;
  36 import javafx.stage.StageStyle;
  37 import javafx.util.Callback;
  38 import junit.framework.AssertionFailedError;
  39 
  40 import static org.junit.Assert.*;
  41 import static util.Util.TIMEOUT;
  42 
  43 /**
  44  * Common base class for testing snapshot.
  45  */
  46 public class SnapshotCommon {
  47 
  48     // Used to launch the application before running any test
  49     private static final CountDownLatch launchLatch = new CountDownLatch(1);
  50 
  51     // Singleton Application instance
  52     static MyApp myApp;
  53 
  54     // Application class. An instance is created and initialized before running
  55     // the first test, and it lives through the execution of all tests.
  56     public static class MyApp extends Application {
  57         Stage primaryStage;
  58 
  59         @Override public void init() {
  60             SnapshotCommon.myApp = this;
  61         }
  62 
  63         @Override public void start(Stage primaryStage) throws Exception {
  64             assertTrue(Platform.isFxApplicationThread());
  65             primaryStage.setTitle("Primary stage");
  66             Group root = new Group();
  67             Scene scene = new Scene(root);
  68             scene.setFill(Color.LIGHTYELLOW);
  69             primaryStage.setScene(scene);
  70             primaryStage.setX(0);
  71             primaryStage.setY(0);
  72             primaryStage.setWidth(210);
  73             primaryStage.setHeight(180);
  74             assertFalse(primaryStage.isShowing());
  75             primaryStage.show();
  76             assertTrue(primaryStage.isShowing());
  77 
  78             this.primaryStage = primaryStage;
  79             launchLatch.countDown();
  80         }
  81     }
  82 
  83     static class TestStage extends Stage {
  84         TestStage(Scene scene) {
  85             this(StageStyle.UNDECORATED, scene);
  86         }
  87 
  88         TestStage(StageStyle style, Scene scene) {
  89             this.setTitle("Test stage");
  90             initStyle(style);
  91 
  92             this.setScene(scene);
  93             if (scene.getWidth() <= 0) {
  94                 this.setWidth(200);
  95                 this.setHeight(150);
  96             }
  97             this.setX(225);
  98             this.setY(0);
  99         }
 100     }
 101 
 102     static void doSetupOnce() {
 103         // Start the Application
 104         new Thread(() -> Application.launch(MyApp.class, (String[])null)).start();
 105 
 106         try {
 107             if (!launchLatch.await(TIMEOUT, TimeUnit.MILLISECONDS)) {
 108                 throw new AssertionFailedError("Timeout waiting for Application to launch");
 109             }
 110         } catch (InterruptedException ex) {
 111             AssertionFailedError err = new AssertionFailedError("Unexpected exception");
 112             err.initCause(ex);
 113             throw err;
 114         }
 115 
 116         assertEquals(0, launchLatch.getCount());
 117     }
 118 
 119     static void doTeardownOnce() {
 120         Platform.exit();
 121     }
 122 
 123     protected void runDeferredSnapshotWait(final Node node,
 124             final Callback<SnapshotResult, Void> cb,
 125             final SnapshotParameters params,
 126             final WritableImage img,
 127             final Runnable runAfter) {
 128 
 129         final Throwable[] testError = new Throwable[1];
 130         final CountDownLatch latch = new CountDownLatch(1);
 131 
 132         Util.runAndWait(() -> {
 133             node.snapshot(result -> {
 134                 try {
 135                     cb.call(result);
 136                 } catch (Throwable th) {
 137                     testError[0] = th;
 138                 } finally {
 139                     latch.countDown();
 140                 }
 141                 return null;
 142             }, params, img);
 143             assertEquals(1, latch.getCount());
 144 
 145             if (runAfter != null) {
 146                 runAfter.run();
 147             }
 148         });
 149 
 150         try {
 151             if (!latch.await(TIMEOUT, TimeUnit.MILLISECONDS)) {
 152                 throw new AssertionFailedError("Timeout waiting for snapshot callback");
 153             }
 154         } catch (InterruptedException ex) {
 155             AssertionFailedError err = new AssertionFailedError("Unexpected exception");
 156             err.initCause(ex);
 157             throw err;
 158         }
 159 
 160         if (testError[0] != null) {
 161             if (testError[0] instanceof Error) {
 162                 throw (Error)testError[0];
 163             } else if (testError[0] instanceof RuntimeException) {
 164                 throw (RuntimeException)testError[0];
 165             } else {
 166                 AssertionFailedError err = new AssertionFailedError("Unknown execution exception");
 167                 err.initCause(testError[0].getCause());
 168                 throw err;
 169             }
 170         }
 171     }
 172 
 173     protected void runDeferredSnapshotWait(final Node node,
 174             final Callback<SnapshotResult, Void> cb,
 175             final SnapshotParameters params,
 176             final WritableImage img) {
 177 
 178         runDeferredSnapshotWait(node, cb, params, img, null);
 179     }
 180 
 181     protected void runDeferredSnapshotWait(final Scene scene,
 182             final Callback<SnapshotResult, Void> cb,
 183             final WritableImage img) {
 184 
 185         final Throwable[] testError = new Throwable[1];
 186         final CountDownLatch latch = new CountDownLatch(1);
 187 
 188         Util.runAndWait(() -> {
 189             scene.snapshot(result -> {
 190                 try {
 191                     cb.call(result);
 192                 } catch (Throwable th) {
 193                     testError[0] = th;
 194                 } finally {
 195                     latch.countDown();
 196                 }
 197                 return null;
 198             }, img);
 199             assertEquals(1, latch.getCount());
 200         });
 201 
 202         try {
 203             if (!latch.await(TIMEOUT, TimeUnit.MILLISECONDS)) {
 204                 throw new AssertionFailedError("Timeout waiting for snapshot callback");
 205             }
 206         } catch (InterruptedException ex) {
 207             AssertionFailedError err = new AssertionFailedError("Unexpected exception");
 208             err.initCause(ex);
 209             throw err;
 210         }
 211 
 212         if (testError[0] != null) {
 213             if (testError[0] instanceof Error) {
 214                 throw (Error)testError[0];
 215             } else if (testError[0] instanceof RuntimeException) {
 216                 throw (RuntimeException)testError[0];
 217             } else {
 218                 AssertionFailedError err = new AssertionFailedError("Unknown execution exception");
 219                 err.initCause(testError[0].getCause());
 220                 throw err;
 221             }
 222         }
 223     }
 224 
 225 }