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