2 * HTML via Java(tm) Language Bindings
3 * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
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.
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.
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
21 package net.java.html.boot.fx;
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;
41 * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
43 public class FXBrowsersOnResourceTest {
45 public FXBrowsersOnResourceTest() {
48 @BeforeClass public void initFX() throws Throwable {
49 new Thread("initFX") {
52 App.launch(App.class);
59 public void behaviorOfTwoWebViewsAtOnce() throws Throwable {
60 class R implements Runnable {
61 CountDownLatch DONE = new CountDownLatch(1);
68 } catch (Throwable ex) {
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");
83 Platform.runLater(run);
85 for (int i = 0; i < 100; i++) {
89 if (System.getProperty("finalSecond") == null) {
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");
100 @JavaScriptResource("wnd.js")
101 public static class OnPages {
102 static Class<?> first;
103 static Object firstWindow;
105 public static void first() {
106 first = OnPages.class;
107 firstWindow = window();
108 assertNotNull(firstWindow, "First window found");
110 assertEquals(increment(), 1, "Now it is one");
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");
116 assertEquals(increment(), 2, "Now it is two and not influenced by second view");
117 System.setProperty("finalFirst", "" + increment());
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");
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");
130 assertEquals(increment(), 1, "Counting starts from zero");
131 System.setProperty("finalSecond", "" + increment());
134 @JavaScriptBody(args = {}, body = "return wnd;")
135 private static native Object window();
137 @JavaScriptBody(args = {}, body = ""
138 + "if (wnd.cnt) return ++wnd.cnt;"
139 + "return wnd.cnt = 1;"
141 private static native int increment();
144 public static class App extends Application {
145 static final CountDownLatch CDL = new CountDownLatch(1);
146 private static BorderPane pane;
151 static WebView getV1() {
152 return (WebView)System.getProperties().get("v1");
158 static WebView getV2() {
159 return (WebView)System.getProperties().get("v2");
163 public void start(Stage stage) throws Exception {
164 pane= new BorderPane();
165 Scene scene = new Scene(pane, 800, 600);
166 stage.setScene(scene);
168 System.getProperties().put("v1", new WebView());
169 System.getProperties().put("v2", new WebView());
171 pane.setCenter(getV1());
172 pane.setBottom(getV2());