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 java.util.concurrent.Semaphore;
  29 import com.sun.fx.webnode.tests.commonUtils.BridgeTestClass;
  30 import com.sun.fx.webnode.tests.commonUtils.ToolkitInitializer;
  31 import javafx.application.Platform;
  32 import netscape.javascript.JSObject;
  33 import org.junit.Assert;
  34 import org.junit.BeforeClass;
  35 import org.junit.Test;
  36 
  37 /**
  38  * Tests for simple JavaScript to Java type conversions.
  39  * @author Irina Latysheva
  40  */
  41 public class BridgeSimpleTest extends BridgeTestClass {
  42 
  43     @BeforeClass
  44     public static void init(){
  45         test.javaclient.shared.Utils.launch(ToolkitInitializer.class, new String[0]);
  46     }
  47 
  48     /**
  49      * Test for JavaScript integer to java.lang.Integer conversion.
  50      */
  51     @Test(timeout=10000)
  52     public void testInteger() throws InterruptedException {
  53         resultObject = null;
  54         
  55         Platform.runLater(new Runnable() {
  56             public void run() {
  57                 initWebEngine();
  58                 resultObject = engine.executeScript("2 + 2");
  59             }
  60         });
  61         doWait(new Tester() {
  62             public boolean isPassed() {
  63                 return (resultObject != null);
  64             }
  65         });
  66 
  67         final Semaphore lock = new Semaphore(1, true);
  68         lock.acquire();
  69         Platform.runLater(new Runnable() {
  70             public void run() {
  71                 System.out.println("resultObject: " + resultObject);
  72                 lock.release();
  73             }
  74         });
  75         lock.acquire();
  76         Assert.assertTrue(resultObject instanceof java.lang.Integer);
  77         Assert.assertEquals(4, ((java.lang.Integer)resultObject).intValue());
  78     }
  79 
  80     /**
  81      * Test for JavaScript double to java.lang.Double conversion.
  82      */
  83     @Test(timeout=10000)
  84     public void testDouble() throws InterruptedException {
  85         resultObject = null;
  86         Platform.runLater(new Runnable() {
  87             public void run() {
  88                 initWebEngine();
  89                 resultObject = engine.executeScript("3 / 2");
  90             }
  91         });
  92         doWait(new Tester() {
  93             public boolean isPassed() {
  94                 return (resultObject != null);
  95             }
  96         });
  97 
  98         final Semaphore lock = new Semaphore(1, true);
  99         lock.acquire();
 100         Platform.runLater(new Runnable() {
 101             public void run() {
 102                 System.out.println("resultObject: " + resultObject);
 103                 lock.release();
 104             }
 105         });
 106         lock.acquire();
 107         Assert.assertTrue(resultObject instanceof java.lang.Double);
 108         Assert.assertEquals(1.5, ((java.lang.Double)resultObject).doubleValue(), 0.0000001);
 109     }
 110 
 111     /**
 112      * Test for JavaScript string to java.lang.String conversion.
 113      */
 114     @Test(timeout=10000)
 115     public void testString() throws InterruptedException {
 116         resultObject = null;
 117         Platform.runLater(new Runnable() {
 118             public void run() {
 119                 initWebEngine();
 120                 resultObject = engine.executeScript("'test' + 'me'");
 121             }
 122         });
 123         doWait(new Tester() {
 124             public boolean isPassed() {
 125                 return (resultObject != null);
 126             }
 127         });
 128 
 129         final Semaphore lock = new Semaphore(1, true);
 130         lock.acquire();
 131         Platform.runLater(new Runnable() {
 132             public void run() {
 133                 System.out.println("resultObject: " + resultObject);
 134                 lock.release();
 135             }
 136         });
 137         lock.acquire();
 138         Assert.assertTrue(resultObject instanceof java.lang.String);
 139         Assert.assertEquals((java.lang.String)resultObject, "testme");
 140     }
 141 
 142     /**
 143      * Test for JavaScript boolean to java.lang.Boolean conversion.
 144      */
 145     @Test(timeout=10000)
 146     public void testBoolean() throws InterruptedException {
 147         resultObject = null;
 148         Platform.runLater(new Runnable() {
 149             public void run() {
 150                 initWebEngine();
 151                 resultObject = engine.executeScript("5 == 5");
 152             }
 153         });
 154         doWait(new Tester() {
 155             public boolean isPassed() {
 156                 return (resultObject != null);
 157             }
 158         });
 159 
 160         final Semaphore lock = new Semaphore(1, true);
 161         lock.acquire();
 162         Platform.runLater(new Runnable() {
 163             public void run() {
 164                 System.out.println("resultObject: " + resultObject);
 165                 lock.release();
 166             }
 167         });
 168         lock.acquire();
 169         Assert.assertTrue(resultObject instanceof java.lang.Boolean);
 170         Assert.assertTrue(((java.lang.Boolean)resultObject).booleanValue());
 171     }
 172 
 173     /**
 174      * Test for JavaScript null to Java null conversion.
 175      */
 176     @Test(timeout=10000)
 177     public void testNull() throws InterruptedException {
 178         resultObject = new Object();
 179         Platform.runLater(new Runnable() {
 180             public void run() {
 181                 initWebEngine();
 182                 resultObject = engine.executeScript("null");
 183             }
 184         });
 185         doWait(new Tester() {
 186             public boolean isPassed() {
 187                 return (resultObject == null);
 188             }
 189         });
 190 
 191         final Semaphore lock = new Semaphore(1, true);
 192         lock.acquire();
 193         Platform.runLater(new Runnable() {
 194             public void run() {
 195                 System.out.println("resultObject: " + resultObject);
 196                 lock.release();
 197             }
 198         });
 199         lock.acquire();
 200         Assert.assertNull(resultObject);
 201     }
 202 
 203     /**
 204      * Test for JavaScript undefined to UNDEFINED conversion.
 205      */
 206     @Test(timeout=10000)
 207     public void testUndefined() throws InterruptedException {
 208         resultObject = null;
 209         Platform.runLater(new Runnable() {
 210             public void run() {
 211                 initWebEngine();
 212                 resultObject = engine.executeScript("alert('AAA!');");
 213             }
 214         });
 215         doWait(new Tester() {
 216             public boolean isPassed() {
 217                 return (resultObject != null);
 218             }
 219         });
 220 
 221         final Semaphore lock = new Semaphore(1, true);
 222         lock.acquire();
 223         Platform.runLater(new Runnable() {
 224             public void run() {
 225                 System.out.println("resultObject: " + resultObject);
 226                 lock.release();
 227             }
 228         });
 229         lock.acquire();
 230         Assert.assertEquals(UNDEFINED, resultObject);
 231     }
 232 
 233     /**
 234      * Test for JavaScript object to JSObject conversion.
 235      */
 236     @Test(timeout=10000)
 237     public void testJSObject() throws InterruptedException {
 238         resultObject = null;
 239         Platform.runLater(new Runnable() {
 240             public void run() {
 241                 initWebEngine();
 242                 resultObject = engine.executeScript("new Object()");
 243             }
 244         });
 245         doWait(new Tester() {
 246             public boolean isPassed() {
 247                 return (resultObject != null);
 248             }
 249         });
 250 
 251         final Semaphore lock = new Semaphore(1, true);
 252         lock.acquire();
 253         Platform.runLater(new Runnable() {
 254             public void run() {
 255                 System.out.println("resultObject: " + resultObject);
 256                 lock.release();
 257             }
 258         });
 259         lock.acquire();
 260         Assert.assertTrue(resultObject instanceof JSObject);
 261     }
 262 
 263     /**
 264      * Test for JavaScript DOM object to JSNode conversion.
 265      */
 266     @Test(timeout=10000)
 267     public void testDOMObject() throws InterruptedException {
 268         resultObject = null;
 269         Platform.runLater(new Runnable() {
 270             public void run() {
 271                 initWebEngine();
 272                 resultObject = engine.executeScript("document.createElement('span')");
 273             }
 274         });
 275         doWait(new Tester() {
 276             public boolean isPassed() {
 277                 return (resultObject != null);
 278             }
 279         });
 280 
 281         final Semaphore lock = new Semaphore(1, true);
 282         lock.acquire();
 283         Platform.runLater(new Runnable() {
 284             public void run() {
 285                 System.out.println("resultObject: " + resultObject);
 286                 lock.release();
 287             }
 288         });
 289         lock.acquire();
 290         Assert.assertTrue(resultObject instanceof org.w3c.dom.Node);
 291     }
 292 }