boot-fx/src/test/java/net/java/html/boot/fx/FXBrowsersTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Fri, 02 Oct 2015 08:57:14 +0200
changeset 1006 a0f79e32d526
parent 656 df15901c0a0a
child 1008 c535c36881af
permissions -rw-r--r--
Ability to run the JavaFX presenter in headless mode is useful for testing
     1 /**
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
     5  *
     6  * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
     7  * Other names may be trademarks of their respective owners.
     8  *
     9  * The contents of this file are subject to the terms of either the GNU
    10  * General Public License Version 2 only ("GPL") or the Common
    11  * Development and Distribution License("CDDL") (collectively, the
    12  * "License"). You may not use this file except in compliance with the
    13  * License. You can obtain a copy of the License at
    14  * http://www.netbeans.org/cddl-gplv2.html
    15  * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    16  * specific language governing permissions and limitations under the
    17  * License.  When distributing the software, include this License Header
    18  * Notice in each file and include the License file at
    19  * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
    20  * particular file as subject to the "Classpath" exception as provided
    21  * by Oracle in the GPL Version 2 section of the License file that
    22  * accompanied this code. If applicable, add the following below the
    23  * License Header, with the fields enclosed by brackets [] replaced by
    24  * your own identifying information:
    25  * "Portions Copyrighted [year] [name of copyright owner]"
    26  *
    27  * Contributor(s):
    28  *
    29  * The Original Software is NetBeans. The Initial Developer of the Original
    30  * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
    31  *
    32  * If you wish your version of this file to be governed by only the CDDL
    33  * or only the GPL Version 2, indicate your decision by adding
    34  * "[Contributor] elects to include this software in this distribution
    35  * under the [CDDL or GPL Version 2] license." If you do not indicate a
    36  * single choice of license, a recipient has the option to distribute
    37  * your version of this file under either the CDDL, the GPL Version 2 or
    38  * to extend the choice of license to its licensees as provided above.
    39  * However, if you add GPL Version 2 code and therefore, elected the GPL
    40  * Version 2 license, then the option applies only if the new code is
    41  * made subject to such option by the copyright holder.
    42  */
    43 package net.java.html.boot.fx;
    44 
    45 import java.net.URL;
    46 import java.util.concurrent.CountDownLatch;
    47 import javafx.application.Application;
    48 import javafx.application.Platform;
    49 import javafx.scene.Scene;
    50 import javafx.scene.layout.BorderPane;
    51 import javafx.scene.web.WebView;
    52 import javafx.stage.Stage;
    53 import net.java.html.js.JavaScriptBody;
    54 import static org.testng.Assert.*;
    55 import org.testng.annotations.BeforeClass;
    56 import org.testng.annotations.Test;
    57 
    58 /**
    59  *
    60  * @author Jaroslav Tulach
    61  */
    62 public class FXBrowsersTest {
    63     
    64     public FXBrowsersTest() {
    65     }
    66     
    67     @BeforeClass public void initFX() throws Throwable {
    68         new Thread("initFX") {
    69             @Override
    70             public void run() {
    71                 App.launch(App.class);
    72             }
    73         }.start();
    74         App.CDL.await();
    75     }
    76 
    77     @Test
    78     public void behaviorOfTwoWebViewsAtOnce() throws Throwable {
    79         class R implements Runnable {
    80             CountDownLatch DONE = new CountDownLatch(1);
    81             Throwable t;
    82 
    83             @Override
    84             public void run() {
    85                 try {
    86                     doTest();
    87                 } catch (Throwable ex) {
    88                     t = ex;
    89                 } finally {
    90                     DONE.countDown();
    91                 }
    92             }
    93             
    94             private void doTest() throws Throwable {
    95                 URL u = FXBrowsersTest.class.getResource("/org/netbeans/html/boot/fx/empty.html");
    96                 assertNotNull(u, "URL found");
    97                 FXBrowsers.load(App.getV1(), u, OnPages.class, "first");
    98                 
    99             }
   100         }
   101         R run = new R();
   102         Platform.runLater(run);
   103         run.DONE.await();
   104         for (int i = 0; i < 100; i++) {
   105             if (run.t != null) {
   106                 throw run.t;
   107             }
   108             if (System.getProperty("finalSecond") == null) {
   109                 Thread.sleep(100);
   110             }
   111         }
   112         
   113         
   114         
   115         assertEquals(Integer.getInteger("finalFirst"), Integer.valueOf(3), "Three times in view one");
   116         assertEquals(Integer.getInteger("finalSecond"), Integer.valueOf(2), "Two times in view one");
   117 
   118         final CountDownLatch finish = new CountDownLatch(1);
   119         final Object[] three = { 0 };
   120         assertFalse(Platform.isFxApplicationThread());
   121         FXBrowsers.runInBrowser(App.getV1(), new Runnable() {
   122             @Override
   123             public void run() {
   124                 assertTrue(Platform.isFxApplicationThread());
   125                 three[0] = App.getV1().getEngine().executeScript("window.cnt");
   126                 finish.countDown();
   127             }
   128         });
   129         finish.await();
   130         
   131         assertEquals(three[0], Integer.valueOf(3));
   132     }
   133     
   134     public static class OnPages {
   135         static Class<?> first;
   136         static Object firstWindow;
   137         
   138         public static void first() {
   139             first = OnPages.class;
   140             firstWindow = window();
   141             assertNotNull(firstWindow, "First window found");
   142             
   143             assertEquals(increment(), 1, "Now it is one");
   144             
   145             URL u = FXBrowsersTest.class.getResource("/org/netbeans/html/boot/fx/empty.html");
   146             assertNotNull(u, "URL found");
   147             FXBrowsers.load(App.getV2(), u, new Runnable() {
   148                 @Override
   149                 public void run() {
   150                     OnPages.second("Hello");
   151                 }
   152             });
   153             
   154             assertEquals(increment(), 2, "Now it is two and not influenced by second view");
   155             System.setProperty("finalFirst", "" + increment());
   156         }
   157         
   158         public static void second(String... args) {
   159             assertEquals(args.length, 1, "One string argument");
   160             assertEquals(args[0], "Hello", "It is hello");
   161             assertEquals(first, OnPages.class, "Both views share the same classloader");
   162             
   163             Object window = window();
   164             assertNotNull(window, "Some window found");
   165             assertNotNull(firstWindow, "First window is known");
   166             assertNotSame(firstWindow, window, "The window objects should be different");
   167             
   168             assertEquals(increment(), 1, "Counting starts from zero");
   169             System.setProperty("finalSecond", "" + increment());
   170         }
   171         
   172         @JavaScriptBody(args = {}, body = "return window;")
   173         private static native Object window();
   174         
   175         @JavaScriptBody(args = {}, body = ""
   176             + "if (window.cnt) return ++window.cnt;"
   177             + "return window.cnt = 1;"
   178         )
   179         private static native int increment();
   180     }
   181     
   182     public static class App extends Application {
   183         static final CountDownLatch CDL = new CountDownLatch(1);
   184         private static BorderPane pane;
   185 
   186         /**
   187          * @return the v1
   188          */
   189         static WebView getV1() {
   190             return (WebView)System.getProperties().get("v1");
   191         }
   192 
   193         /**
   194          * @return the v2
   195          */
   196         static WebView getV2() {
   197             return (WebView)System.getProperties().get("v2");
   198         }
   199 
   200         @Override
   201         public void start(Stage stage) throws Exception {
   202             pane= new BorderPane();
   203             Scene scene = new Scene(pane, 800, 600);
   204             stage.setScene(scene);
   205             
   206             System.getProperties().put("v1", new WebView());
   207             System.getProperties().put("v2", new WebView());
   208 
   209             pane.setCenter(getV1());
   210             pane.setBottom(getV2());
   211 
   212             CDL.countDown();
   213         }
   214         
   215         
   216     }
   217 }