1 /*
   2  * Copyright (c) 2014, 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  * questions.
  24  */
  25 
  26 package com.sun.fx.webnode.tests.bridge;
  27 
  28 import com.sun.fx.webnode.tests.commonUtils.BridgeTestClass;
  29 import com.sun.fx.webnode.tests.commonUtils.ToolkitInitializer;
  30 import javafx.application.Platform;
  31 import netscape.javascript.JSObject;
  32 import org.junit.Assert;
  33 import org.junit.BeforeClass;
  34 import org.junit.Test;
  35 
  36 /**
  37  * Tests for simple JavaScript to Java type conversions.
  38  * @author Irina Latysheva
  39  */
  40 public class BridgeSimpleTest extends BridgeTestClass {
  41 
  42     @BeforeClass
  43     public static void init(){
  44         test.javaclient.shared.Utils.launch(ToolkitInitializer.class, new String[0]);
  45     }
  46 
  47     /**
  48      * Test for JavaScript integer to java.lang.Integer conversion.
  49      */
  50     @Test(timeout=10000)
  51     public void testInteger(){
  52         resultObject = null;
  53         Platform.runLater(new Runnable() {
  54             public void run() {
  55                 initWebEngine();
  56                 resultObject = engine.executeScript("2 + 2");
  57             }
  58         });
  59         doWait(new Tester() {
  60             public boolean isPassed() {
  61                 return (resultObject != null);
  62             }
  63         });
  64         System.out.println(resultObject);
  65         Assert.assertTrue(resultObject instanceof java.lang.Integer);
  66         Assert.assertEquals(4, ((java.lang.Integer)resultObject).intValue());
  67     }
  68 
  69     /**
  70      * Test for JavaScript double to java.lang.Double conversion.
  71      */
  72     @Test(timeout=10000)
  73     public void testDouble(){
  74         resultObject = null;
  75         Platform.runLater(new Runnable() {
  76             public void run() {
  77                 initWebEngine();
  78                 resultObject = engine.executeScript("3 / 2");
  79             }
  80         });
  81         doWait(new Tester() {
  82             public boolean isPassed() {
  83                 return (resultObject != null);
  84             }
  85         });
  86         System.out.println(resultObject);
  87         Assert.assertTrue(resultObject instanceof java.lang.Double);
  88         Assert.assertEquals(1.5, ((java.lang.Double)resultObject).doubleValue(), 0.0000001);
  89     }
  90 
  91     /**
  92      * Test for JavaScript string to java.lang.String conversion.
  93      */
  94     @Test(timeout=10000)
  95     public void testString(){
  96         resultObject = null;
  97         Platform.runLater(new Runnable() {
  98             public void run() {
  99                 initWebEngine();
 100                 resultObject = engine.executeScript("'test' + 'me'");
 101             }
 102         });
 103         doWait(new Tester() {
 104             public boolean isPassed() {
 105                 return (resultObject != null);
 106             }
 107         });
 108         System.out.println(resultObject);
 109         Assert.assertTrue(resultObject instanceof java.lang.String);
 110         Assert.assertEquals((java.lang.String)resultObject, "testme");
 111     }
 112 
 113     /**
 114      * Test for JavaScript boolean to java.lang.Boolean conversion.
 115      */
 116     @Test(timeout=10000)
 117     public void testBoolean(){
 118         resultObject = null;
 119         Platform.runLater(new Runnable() {
 120             public void run() {
 121                 initWebEngine();
 122                 resultObject = engine.executeScript("5 == 5");
 123             }
 124         });
 125         doWait(new Tester() {
 126             public boolean isPassed() {
 127                 return (resultObject != null);
 128             }
 129         });
 130         System.out.println(resultObject);
 131         Assert.assertTrue(resultObject instanceof java.lang.Boolean);
 132         Assert.assertTrue(((java.lang.Boolean)resultObject).booleanValue());
 133     }
 134 
 135     /**
 136      * Test for JavaScript null to Java null conversion.
 137      */
 138     @Test(timeout=10000)
 139     public void testNull(){
 140         resultObject = new Object();
 141         Platform.runLater(new Runnable() {
 142             public void run() {
 143                 initWebEngine();
 144                 resultObject = engine.executeScript("null");
 145             }
 146         });
 147         doWait(new Tester() {
 148             public boolean isPassed() {
 149                 return (resultObject == null);
 150             }
 151         });
 152 
 153         System.out.println(resultObject);
 154     }
 155 
 156     /**
 157      * Test for JavaScript undefined to UNDEFINED conversion.
 158      */
 159     @Test(timeout=10000)
 160     public void testUndefined(){
 161         resultObject = null;
 162         Platform.runLater(new Runnable() {
 163             public void run() {
 164                 initWebEngine();
 165                 resultObject = engine.executeScript("alert('AAA!');");
 166             }
 167         });
 168         doWait(new Tester() {
 169             public boolean isPassed() {
 170                 return (resultObject != null);
 171             }
 172         });
 173 
 174         System.out.println(resultObject);
 175         Assert.assertEquals(UNDEFINED, resultObject);
 176     }
 177 
 178     /**
 179      * Test for JavaScript object to JSObject conversion.
 180      */
 181     @Test(timeout=10000)
 182     public void testJSObject(){
 183         resultObject = null;
 184         Platform.runLater(new Runnable() {
 185             public void run() {
 186                 initWebEngine();
 187                 resultObject = engine.executeScript("new Object()");
 188             }
 189         });
 190         doWait(new Tester() {
 191             public boolean isPassed() {
 192                 return (resultObject != null);
 193             }
 194         });
 195 
 196         System.out.println(resultObject);
 197         Assert.assertTrue(resultObject instanceof JSObject);
 198     }
 199 
 200     /**
 201      * Test for JavaScript DOM object to JSNode conversion.
 202      */
 203     @Test(timeout=10000)
 204     public void testDOMObject(){
 205         resultObject = null;
 206         Platform.runLater(new Runnable() {
 207             public void run() {
 208                 initWebEngine();
 209                 resultObject = engine.executeScript("document.createElement('span')");
 210             }
 211         });
 212         doWait(new Tester() {
 213             public boolean isPassed() {
 214                 return (resultObject != null);
 215             }
 216         });
 217 
 218         System.out.println(resultObject);
 219         Assert.assertTrue(resultObject instanceof org.w3c.dom.Node);
 220     }
 221 }