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 
  31 var System               = Java.type("java.lang.System");
  32 var AWTImage             = Java.type("org.jemmy.image.AWTImage");
  33 var PNGDecoder           = Java.type("org.jemmy.image.PNGDecoder");
  34 var JemmyFxRoot          = Java.type("org.jemmy.fx.Root");
  35 var AWTRobotCapturer     = Java.type("org.jemmy.image.AWTRobotCapturer");
  36 var ByWindowType         = Java.type("org.jemmy.fx.ByWindowType");
  37 var Scene                = Java.type("javafx.scene.Scene");
  38 var Stage                = Java.type("javafx.stage.Stage");
  39 var File                 = Java.type("java.io.File");
  40 var OSInfo               = Java.type("sun.awt.OSInfo");
  41 var OSType               = Java.type("sun.awt.OSInfo.OSType");
  42 var StringBuffer         = Java.type("java.lang.StringBuffer");
  43 var Paint                = Java.type("javafx.scene.paint.Paint");
  44 var Color                = Java.type("javafx.scene.paint.Color");
  45 var Image                = Java.type("javafx.scene.image.Image");
  46 var Canvas               = Java.type("javafx.scene.canvas.Canvas");
  47 var BorderPane           = Java.type("javafx.scene.layout.BorderPane");
  48 var StackPane            = Java.type("javafx.scene.layout.StackPane");
  49 var StrokeLineCap        = Java.type("javafx.scene.shape.StrokeLineCap");
  50 var Platform             = Java.type("javafx.application.Platform");
  51 var Runnable             = Java.type("java.lang.Runnable");
  52 var RunnableExtend       = Java.extend(Runnable);
  53 var AnimationTimer       = Java.type("javafx.animation.AnimationTimer");
  54 var AnimationTimerExtend = Java.extend(AnimationTimer);
  55 var Timer                = Java.type("java.util.Timer");
  56 var TimerTask            = Java.type("java.util.TimerTask");
  57 
  58 var TESTNAME = "test";
  59 var fsep = System.getProperty("file.separator");
  60 
  61 function 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 
  79 function 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 
  86 function 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 }