test/script/jfx.js

Mon, 21 Oct 2013 13:31:03 +0400

author
kshefov
date
Mon, 21 Oct 2013 13:31:03 +0400
changeset 638
f22742d5daa3
child 667
bda654c6d59c
permissions
-rw-r--r--

8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
Reviewed-by: jlaskey, sundar

     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  */
    24 /**
    25  * Base library for JavaFX canvas run by Nashorn testing.
    26  * @subtest
    27  * 
    28  * 
    29  */
    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 Timer                = Java.type("java.util.Timer");
    41 var TimerTask            = Java.type("java.util.TimerTask");
    42 var OSInfo               = Java.type("sun.awt.OSInfo");
    43 var OSType               = Java.type("sun.awt.OSInfo.OSType");
    44 var StringBuffer         = Java.type("java.lang.StringBuffer");
    46 var WAIT = 2000;
    47 var TESTNAME = "test";
    48 var fsep = System.getProperty("file.separator");
    50 function checkImageAndExit() {
    51     var raceTimer = new Timer(true);
    52     var timerTask = new TimerTask() {
    53         run: function run() {
    54             var tmpdir = System.getProperty("java.io.tmpdir");
    55             var timenow = (new Date()).getTime();
    56             makeScreenShot(tmpdir + fsep + "screenshot" + timenow +".png");
    57             var dupImg = isDuplicateImages(tmpdir + fsep + "screenshot" + timenow +".png", __DIR__ + "jfx" + fsep + TESTNAME + fsep + "golden");
    58             (new File(mpdir + fsep + "screenshot" + timenow +".png")).delete();
    59             if (!dupImg) System.err.println("ERROR: screenshot does not match golden image");
    60             exit(0);
    61         }
    62     };
    63     raceTimer.schedule(timerTask, WAIT);
    64 }
    66 function makeScreenShot(shootToImg) {
    67    JemmyFxRoot.ROOT.getEnvironment().setImageCapturer(new AWTRobotCapturer());
    68    var wrap = JemmyFxRoot.ROOT.lookup(new ByWindowType($STAGE.class)).lookup(Scene.class).wrap(0);
    69    var imageJemmy = wrap.getScreenImage();
    70    imageJemmy.save(shootToImg);
    71 }
    73 function isDuplicateImages(file1, file2) {
    74     var f1 = new File(file1);
    75     var f2;
    76     var sb = new StringBuffer(file2);
    77     if (OSInfo.getOSType() == OSType.WINDOWS) {
    78         f2 = new File(sb.append(fsep + "windows.png").toString());
    79     } else if (OSInfo.getOSType() == OSType.LINUX) {
    80         f2 = new File(sb.append(fsep + "linux.png").toString());
    81     } else if (OSInfo.getOSType() == OSType.MACOSX) {
    82         f2 = new File(sb.append(fsep + "macosx.png").toString());
    83     }
    84     print(f1.getAbsolutePath());
    85     print(f2.getAbsolutePath());
    86     if (f1.exists() && f2.exists()) {
    87         var image1 = new AWTImage(PNGDecoder.decode(f1.getAbsolutePath()));
    88         var image2 = new AWTImage(PNGDecoder.decode(f2.getAbsolutePath()));
    89         return image1.compareTo(image2) == null ? true : false;
    90     }
    91     return false;
    92 }

mercurial