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