1/* 2 * Copyright (c) 2013, 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. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24/** 25 * Base library for JavaFX canvas run by Nashorn testing. 26 * @subtest 27 * 28 * 29 */ 30 31var System = Java.type("java.lang.System"); 32var AWTImage = Java.type("org.jemmy.image.AWTImage"); 33var PNGDecoder = Java.type("org.jemmy.image.PNGDecoder"); 34var JemmyFxRoot = Java.type("org.jemmy.fx.Root"); 35var AWTRobotCapturer = Java.type("org.jemmy.image.AWTRobotCapturer"); 36var ByWindowType = Java.type("org.jemmy.fx.ByWindowType"); 37var Scene = Java.type("javafx.scene.Scene"); 38var Stage = Java.type("javafx.stage.Stage"); 39var File = Java.type("java.io.File"); 40var OSInfo = Java.type("sun.awt.OSInfo"); 41var OSType = Java.type("sun.awt.OSInfo.OSType"); 42var StringBuffer = Java.type("java.lang.StringBuffer"); 43var Paint = Java.type("javafx.scene.paint.Paint"); 44var Color = Java.type("javafx.scene.paint.Color"); 45var Image = Java.type("javafx.scene.image.Image"); 46var Canvas = Java.type("javafx.scene.canvas.Canvas"); 47var BorderPane = Java.type("javafx.scene.layout.BorderPane"); 48var StackPane = Java.type("javafx.scene.layout.StackPane"); 49var StrokeLineCap = Java.type("javafx.scene.shape.StrokeLineCap"); 50var Platform = Java.type("javafx.application.Platform"); 51var Runnable = Java.type("java.lang.Runnable"); 52var RunnableExtend = Java.extend(Runnable); 53var AnimationTimer = Java.type("javafx.animation.AnimationTimer"); 54var AnimationTimerExtend = Java.extend(AnimationTimer); 55var Timer = Java.type("java.util.Timer"); 56var TimerTask = Java.type("java.util.TimerTask"); 57 58var TESTNAME = "test"; 59var fsep = System.getProperty("file.separator"); 60 61function checkImageAndExit() { 62 var raceTimer = new Timer(true); 63 var timerTask = new TimerTask() { 64 run: function run() { 65 var tmpdir = System.getProperty("java.io.tmpdir"); 66 var timenow = (new Date()).getTime(); 67 var scrShotTmp = tmpdir + fsep + "screenshot" + timenow +".png"; 68 var goldenImageDir = __DIR__ + "jfx" + fsep + TESTNAME + fsep + "golden"; 69 makeScreenShot(scrShotTmp); 70 var dupImg = isDuplicateImages(scrShotTmp, goldenImageDir); 71 (new File(scrShotTmp)).delete(); 72 if (!dupImg) System.err.println("ERROR: screenshot does not match the golden image"); 73 exit(0); 74 } 75 }; 76 raceTimer.schedule(timerTask, 100); 77} 78 79function makeScreenShot(shootToImg) { 80 JemmyFxRoot.ROOT.getEnvironment().setImageCapturer(new AWTRobotCapturer()); 81 var wrap = JemmyFxRoot.ROOT.lookup(new ByWindowType($STAGE.class)).lookup(Scene.class).wrap(0); 82 var imageJemmy = wrap.getScreenImage(); 83 imageJemmy.save(shootToImg); 84} 85 86function isDuplicateImages(screenShot, goldenDir) { 87 var f1 = new File(screenShot); 88 var f2; 89 var sb = new StringBuffer(goldenDir); 90 if (OSInfo.getOSType() == OSType.WINDOWS) { 91 f2 = new File(sb.append(fsep + "windows.png").toString()); 92 } else if (OSInfo.getOSType() == OSType.LINUX) { 93 f2 = new File(sb.append(fsep + "linux.png").toString()); 94 } else if (OSInfo.getOSType() == OSType.MACOSX) { 95 f2 = new File(sb.append(fsep + "macosx.png").toString()); 96 } 97 if (f1.exists() && f2.exists()) { 98 var image1 = new AWTImage(PNGDecoder.decode(f1.getAbsolutePath())); 99 var image2 = new AWTImage(PNGDecoder.decode(f2.getAbsolutePath())); 100 return image1.compareTo(image2) == null ? true : false; 101 } 102 return false; 103} 104