1 /*
   2  * Copyright (c) 2009, 2012, 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  */
  24 
  25 package test.javaclient.shared;
  26 
  27 import java.io.*;
  28 import java.util.logging.Level;
  29 import java.util.logging.Logger;
  30 import javafx.scene.Scene;
  31 import javafx.stage.Stage;
  32 import javafx.stage.Window;
  33 import org.jemmy.TimeoutExpiredException;
  34 import org.jemmy.control.Wrap;
  35 import org.jemmy.env.Timeout;
  36 import org.jemmy.fx.ByWindowType;
  37 import org.jemmy.fx.Root;
  38 import org.jemmy.image.Image;
  39 import org.jemmy.timing.State;
  40 import org.jemmy.timing.Waiter;
  41 import test.javaclient.shared.description.TreeNode;
  42 import test.javaclient.shared.screenshots.ImagesManager;
  43 
  44 /**
  45  * Utility methods
  46  *
  47  * @author Sergey Grinev
  48  */
  49 public class TestUtil {
  50 
  51     public static final boolean IS_GOLDEN_MODE = Boolean.getBoolean("test.javaclient.creategolden");
  52     private static final boolean IS_SLOW_MODE = Boolean.getBoolean("test.javaclient.slowmode");
  53     public static final boolean IS_DESCRIBING_MODE = Boolean.getBoolean("test.javaclient.createdescription");
  54     public static final boolean IS_DESCRIPTION_MODE = Boolean.getBoolean("test.javaclient.description");
  55     /**
  56      * Suffixes for embedded mode.
  57      */
  58     public static final String[] SUFFIXES = new String[]{"First", "Second", "Third", "Forth"};
  59 
  60     /**
  61      * For embedded mode. Verify -Djavatest.vmOptions="-DembeddedMode=X",
  62      * whether X is true or false
  63      *
  64      * @return true if X is true otherwise false
  65      */
  66     public static boolean isEmbedded() {
  67         String vmOpt = System.getProperty("embeddedMode");
  68         return vmOpt != null && vmOpt.equals("true");
  69     }
  70 
  71     /**
  72      * For embedded mode. Verify -Djavatest.vmOptions="-DgraphicsProfile=X",
  73      * whether X is true or false
  74      *
  75      * @return true if X is true otherwise false
  76      */
  77     public static boolean isEmbeddedGraphicsProfile() {
  78         return System.getProperty("graphicsProfile", "false").equals("true");
  79     }
  80 
  81     /**
  82      * For embedded mode. Verify -Djavatest.vmOptions="-DembeddedPlatform=X",
  83      * whether X is eglfb
  84      *
  85      * @return argument value or null if it is not presented
  86      */
  87     public static String getEmbeddedFxPlatform() {
  88         String vmOpt = System.getProperty("embeddedPlatform");
  89         return (vmOpt != null) ? vmOpt : null;
  90     }
  91 
  92     public static boolean write(Serializable obj, String name) {
  93         try {
  94             FileOutputStream ostream = new FileOutputStream(name);
  95             ObjectOutputStream p = new ObjectOutputStream(ostream);
  96             p.writeObject(obj);
  97             p.flush();
  98             ostream.close();
  99         } catch (IOException ex) {
 100             Logger.getLogger(TreeNode.class.getName()).log(Level.SEVERE, null, ex);
 101             return false;
 102         }
 103         return true;
 104     }
 105 
 106     public static Object read(String name) {
 107         Object obj = null;
 108         try {
 109             FileInputStream ostream = new FileInputStream(name);
 110             ObjectInputStream p = new ObjectInputStream(ostream);
 111             obj = p.readObject();
 112             ostream.close();
 113         } catch (IOException ex) {
 114             Logger.getLogger(TreeNode.class.getName()).log(Level.SEVERE, null, ex);
 115         } catch (ClassNotFoundException ex) {
 116             Logger.getLogger(TreeNode.class.getName()).log(Level.SEVERE, null, ex);
 117         }
 118         return obj;
 119     }
 120 
 121     /**
 122      * Verify screenshots of two wraps, by comparing screenshots of both of
 123      * them, until they are equal.
 124      *
 125      * @param testName test name for a test
 126      * @param existingWrap wrap for object already placed on UI
 127      * @param oneToWaitForWrap wrap for object can be not yet placed on UI
 128      */
 129     public static void compareScreenshots(String testName, final Wrap existingWrap, final Wrap oneToWaitForWrap) {
 130         String existingName = ImagesManager.getInstance().getScreenshotPath(testName + "-existing"); //= RESULT_PATH + testName + "-diff.png";
 131         String waitingForName = ImagesManager.getInstance().getScreenshotPath(testName + "-waitingFor");
 132         String diffName = ImagesManager.getInstance().getScreenshotPath(testName + "-diff"); //= RESULT_PATH + testName + IMAGE_POSTFIX;
 133         String OUTPUT = oneToWaitForWrap.getClass().getName() + ".OUTPUT";
 134         try {
 135             oneToWaitForWrap.waitState(new State<Object>() {
 136                 public Object reached() {
 137                     return (existingWrap.getScreenImage().compareTo(oneToWaitForWrap.getScreenImage()) == null) ? true : null;
 138                 }
 139 
 140                 @Override
 141                 public String toString() {
 142                     return "Control having expected image";
 143                 }
 144             });
 145         } catch (TimeoutExpiredException e) {
 146             if (diffName != null) {
 147                 oneToWaitForWrap.getEnvironment().getOutput(OUTPUT).println("Saving diff to " + diffName);
 148                 existingWrap.getScreenImage().compareTo(oneToWaitForWrap.getScreenImage()).save(diffName);
 149             }
 150             if (waitingForName != null) {
 151                 oneToWaitForWrap.getEnvironment().getOutput(OUTPUT).println("Saving waiting for to " + waitingForName);
 152                 oneToWaitForWrap.getScreenImage().save(waitingForName);
 153             }
 154             throw e;
 155         } finally {
 156             if (existingName != null) {
 157                 oneToWaitForWrap.getEnvironment().getOutput(OUTPUT).println("Saving existing to " + existingName);
 158                 existingWrap.getScreenImage().save(existingName);
 159             }
 160         }
 161     }
 162 
 163     /**
 164      * Debugging Code. Will slow test execution if
 165      * <code>IS_SLOW_MODE</code> is set.
 166      */
 167     public static void slow(long delay) {
 168         if (IS_SLOW_MODE) {
 169             try {
 170                 Thread.sleep(delay);
 171             } catch (InterruptedException ex) {
 172             }
 173         }
 174     }
 175 
 176     public static Wrap<? extends Scene> getScene() {
 177         final Wrap<? extends Scene> scene;
 178 
 179         scene = Root.ROOT.lookup(new ByWindowType(Stage.class)).lookup(Scene.class).wrap(0);
 180         Utils.deferAction(new Runnable() {
 181             public void run() {
 182                 scene.getControl().getWindow().setFocused(true);
 183             }
 184         });
 185 
 186         return scene;
 187     }
 188 
 189     public static String getTitle(Scene scene) {
 190         Window window = scene.getWindow();
 191         if (Stage.class.isInstance(window)) {
 192             return ((Stage) window).getTitle();
 193         }
 194         return "";
 195     }
 196 
 197     public static void setTitle(Scene scene, String title) {
 198         Window window = scene.getWindow();
 199         if (Stage.class.isInstance(window)) {
 200             ((Stage) window).setTitle(title);
 201         }
 202     }
 203 }