boot-fx/src/test/java/net/java/html/boot/fx/FXBrowsersTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sun, 01 Nov 2015 16:59:42 +0100
changeset 1016 665b10c62f3d
parent 1008 c535c36881af
child 1037 e390a06dbfac
permissions -rw-r--r--
Make the test more robust by counting down the latch only when the then to be tested property is set
     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     private static CountDownLatch PROPERTY_SET;
    64 
    65     public FXBrowsersTest() {
    66     }
    67     
    68     @BeforeClass public void initFX() throws Throwable {
    69         new Thread("initFX") {
    70             @Override
    71             public void run() {
    72                 if (Platform.isFxApplicationThread()) {
    73                     new App().start(new Stage());
    74                 } else {
    75                     try {
    76                         App.launch(App.class);
    77                     } catch (IllegalStateException ex) {
    78                         Platform.runLater(this);
    79                     }
    80                 }
    81             }
    82         }.start();
    83         App.CDL.await();
    84     }
    85 
    86     @Test
    87     public void behaviorOfTwoWebViewsAtOnce() throws Throwable {
    88         class R implements Runnable {
    89             Throwable t;
    90 
    91             @Override
    92             public void run() {
    93                 try {
    94                     doTest();
    95                 } catch (Throwable ex) {
    96                     t = ex;
    97                 }
    98             }
    99             
   100             private void doTest() throws Throwable {
   101                 URL u = FXBrowsersTest.class.getResource("/org/netbeans/html/boot/fx/empty.html");
   102                 assertNotNull(u, "URL found");
   103                 FXBrowsers.load(App.getV1(), u, OnPages.class, "first");
   104             }
   105         }
   106         R run = new R();
   107         PROPERTY_SET = new CountDownLatch(2);
   108         Platform.runLater(run);
   109         PROPERTY_SET.await();
   110         
   111         assertEquals(Integer.getInteger("finalFirst"), Integer.valueOf(3), "Three times in view one");
   112         assertEquals(Integer.getInteger("finalSecond"), Integer.valueOf(2), "Two times in view one");
   113 
   114         final CountDownLatch finish = new CountDownLatch(1);
   115         final Object[] three = { 0 };
   116         assertFalse(Platform.isFxApplicationThread());
   117         FXBrowsers.runInBrowser(App.getV1(), new Runnable() {
   118             @Override
   119             public void run() {
   120                 assertTrue(Platform.isFxApplicationThread());
   121                 three[0] = App.getV1().getEngine().executeScript("window.cntBrwsr");
   122                 finish.countDown();
   123             }
   124         });
   125         finish.await();
   126         
   127         assertEquals(three[0], Integer.valueOf(3));
   128     }
   129     
   130     public static class OnPages {
   131         static Class<?> first;
   132         static Object firstWindow;
   133         
   134         public static void first() {
   135             first = OnPages.class;
   136             firstWindow = window();
   137             assertNotNull(firstWindow, "First window found");
   138             
   139             assertEquals(increment(), 1, "Now it is one");
   140             
   141             URL u = FXBrowsersTest.class.getResource("/org/netbeans/html/boot/fx/empty.html");
   142             assertNotNull(u, "URL found");
   143             FXBrowsers.load(App.getV2(), u, new Runnable() {
   144                 @Override
   145                 public void run() {
   146                     OnPages.second("Hello");
   147                 }
   148             });
   149             
   150             assertEquals(increment(), 2, "Now it is two and not influenced by second view");
   151             System.setProperty("finalFirst", "" + increment());
   152             PROPERTY_SET.countDown();
   153         }
   154         
   155         public static void second(String... args) {
   156             assertEquals(args.length, 1, "One string argument");
   157             assertEquals(args[0], "Hello", "It is hello");
   158             assertEquals(first, OnPages.class, "Both views share the same classloader");
   159             
   160             Object window = window();
   161             assertNotNull(window, "Some window found");
   162             assertNotNull(firstWindow, "First window is known");
   163             assertNotSame(firstWindow, window, "The window objects should be different");
   164             
   165             assertEquals(increment(), 1, "Counting starts from zero");
   166             System.setProperty("finalSecond", "" + increment());
   167             PROPERTY_SET.countDown();
   168         }
   169         
   170         @JavaScriptBody(args = {}, body = "return window;")
   171         private static native Object window();
   172         
   173         @JavaScriptBody(args = {}, body = ""
   174             + "if (window.cntBrwsr) return ++window.cntBrwsr;"
   175             + "return window.cntBrwsr = 1;"
   176         )
   177         private static native int increment();
   178     }
   179     
   180     public static class App extends Application {
   181         static final CountDownLatch CDL = new CountDownLatch(1);
   182         private static BorderPane pane;
   183 
   184         /**
   185          * @return the v1
   186          */
   187         static WebView getV1() {
   188             return (WebView)System.getProperties().get("v1");
   189         }
   190 
   191         /**
   192          * @return the v2
   193          */
   194         static WebView getV2() {
   195             return (WebView)System.getProperties().get("v2");
   196         }
   197 
   198         @Override
   199         public void start(Stage stage) {
   200             pane= new BorderPane();
   201             Scene scene = new Scene(pane, 800, 600);
   202             stage.setScene(scene);
   203             
   204             System.getProperties().put("v1", new WebView());
   205             System.getProperties().put("v2", new WebView());
   206 
   207             pane.setCenter(getV1());
   208             pane.setBottom(getV2());
   209 
   210             CDL.countDown();
   211         }
   212         
   213         
   214     }
   215 }