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