boot-fx/src/test/java/net/java/html/boot/fx/FXBrowsersOnResourceTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 12 Dec 2013 23:54:32 +0100
changeset 349 53634fd10e30
parent 288 boot-fx/src/test/java/net/java/html/boot/fx/FXBrowsersTest.java@8c5b40231d26
child 358 80702021b851
permissions -rw-r--r--
Load global scripts when first method in the class is called
     1 /**
     2  * HTML via Java(tm) Language Bindings
     3  * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, version 2 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details. apidesign.org
    13  * designates this particular file as subject to the
    14  * "Classpath" exception as provided by apidesign.org
    15  * in the License file that accompanied this code.
    16  *
    17  * You should have received a copy of the GNU General Public License
    18  * along with this program. Look for COPYING file in the top folder.
    19  * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
    20  */
    21 package net.java.html.boot.fx;
    22 
    23 import java.net.URL;
    24 import java.util.concurrent.CountDownLatch;
    25 import javafx.application.Application;
    26 import javafx.application.Platform;
    27 import javafx.scene.Scene;
    28 import javafx.scene.layout.BorderPane;
    29 import javafx.scene.web.WebView;
    30 import javafx.stage.Stage;
    31 import net.java.html.js.JavaScriptBody;
    32 import net.java.html.js.JavaScriptResource;
    33 import static org.testng.Assert.assertEquals;
    34 import static org.testng.Assert.assertNotNull;
    35 import static org.testng.Assert.assertNotSame;
    36 import org.testng.annotations.BeforeClass;
    37 import org.testng.annotations.Test;
    38 
    39 /**
    40  *
    41  * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    42  */
    43 public class FXBrowsersOnResourceTest {
    44     
    45     public FXBrowsersOnResourceTest() {
    46     }
    47     
    48     @BeforeClass public void initFX() throws Throwable {
    49         new Thread("initFX") {
    50             @Override
    51             public void run() {
    52                 App.launch(App.class);
    53             }
    54         }.start();
    55         App.CDL.await();
    56     }
    57 
    58     @Test
    59     public void behaviorOfTwoWebViewsAtOnce() throws Throwable {
    60         class R implements Runnable {
    61             CountDownLatch DONE = new CountDownLatch(1);
    62             Throwable t;
    63 
    64             @Override
    65             public void run() {
    66                 try {
    67                     doTest();
    68                 } catch (Throwable ex) {
    69                     t = ex;
    70                 } finally {
    71                     DONE.countDown();
    72                 }
    73             }
    74             
    75             private void doTest() throws Throwable {
    76                 URL u = FXBrowsersOnResourceTest.class.getResource("/org/apidesign/html/boot/fx/empty.html");
    77                 assertNotNull(u, "URL found");
    78                 FXBrowsers.load(App.getV1(), u, OnPages.class, "first");
    79                 
    80             }
    81         }
    82         R run = new R();
    83         Platform.runLater(run);
    84         run.DONE.await();
    85         for (int i = 0; i < 100; i++) {
    86             if (run.t != null) {
    87                 throw run.t;
    88             }
    89             if (System.getProperty("finalSecond") == null) {
    90                 Thread.sleep(100);
    91             }
    92         }
    93         
    94         
    95         
    96         assertEquals(Integer.getInteger("finalFirst"), Integer.valueOf(3), "Three times in view one");
    97         assertEquals(Integer.getInteger("finalSecond"), Integer.valueOf(2), "Two times in view one");
    98     }
    99 
   100     @JavaScriptResource("wnd.js")
   101     public static class OnPages {
   102         static Class<?> first;
   103         static Object firstWindow;
   104         
   105         public static void first() {
   106             first = OnPages.class;
   107             firstWindow = window();
   108             assertNotNull(firstWindow, "First window found");
   109             
   110             assertEquals(increment(), 1, "Now it is one");
   111             
   112             URL u = FXBrowsersOnResourceTest.class.getResource("/org/apidesign/html/boot/fx/empty.html");
   113             assertNotNull(u, "URL found");
   114             FXBrowsers.load(App.getV2(), u, OnPages.class, "second", "Hello");
   115             
   116             assertEquals(increment(), 2, "Now it is two and not influenced by second view");
   117             System.setProperty("finalFirst", "" + increment());
   118         }
   119         
   120         public static void second(String... args) {
   121             assertEquals(args.length, 1, "One string argument");
   122             assertEquals(args[0], "Hello", "It is hello");
   123             assertEquals(first, OnPages.class, "Both views share the same classloader");
   124             
   125             Object window = window();
   126             assertNotNull(window, "Some window found");
   127             assertNotNull(firstWindow, "First window is known");
   128             assertNotSame(firstWindow, window, "The window objects should be different");
   129             
   130             assertEquals(increment(), 1, "Counting starts from zero");
   131             System.setProperty("finalSecond", "" + increment());
   132         }
   133         
   134         @JavaScriptBody(args = {}, body = "return wnd;")
   135         private static native Object window();
   136         
   137         @JavaScriptBody(args = {}, body = ""
   138             + "if (wnd.cnt) return ++wnd.cnt;"
   139             + "return wnd.cnt = 1;"
   140         )
   141         private static native int increment();
   142     }
   143     
   144     public static class App extends Application {
   145         static final CountDownLatch CDL = new CountDownLatch(1);
   146         private static BorderPane pane;
   147 
   148         /**
   149          * @return the v1
   150          */
   151         static WebView getV1() {
   152             return (WebView)System.getProperties().get("v1");
   153         }
   154 
   155         /**
   156          * @return the v2
   157          */
   158         static WebView getV2() {
   159             return (WebView)System.getProperties().get("v2");
   160         }
   161 
   162         @Override
   163         public void start(Stage stage) throws Exception {
   164             pane= new BorderPane();
   165             Scene scene = new Scene(pane, 800, 600);
   166             stage.setScene(scene);
   167             
   168             System.getProperties().put("v1", new WebView());
   169             System.getProperties().put("v2", new WebView());
   170 
   171             pane.setCenter(getV1());
   172             pane.setBottom(getV2());
   173 
   174             stage.show();
   175             CDL.countDown();
   176         }
   177         
   178         
   179     }
   180 }