2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
6 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7 * Other names may be trademarks of their respective owners.
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]"
29 * The Original Software is NetBeans. The Initial Developer of the Original
30 * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
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.
43 package net.java.html.boot.fx;
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.BrwsrCtx;
54 import net.java.html.js.JavaScriptBody;
55 import static org.testng.Assert.*;
56 import org.testng.annotations.BeforeClass;
57 import org.testng.annotations.Test;
61 * @author Jaroslav Tulach
63 public class FXBrowsersTest {
64 private static CountDownLatch PROPERTY_SET;
66 public FXBrowsersTest() {
69 @BeforeClass public void initFX() throws Throwable {
70 new Thread("initFX") {
73 if (Platform.isFxApplicationThread()) {
74 new App().start(new Stage());
77 App.launch(App.class);
78 } catch (IllegalStateException ex) {
79 Platform.runLater(this);
87 @JavaScriptBody(args = { }, body = "return true;")
88 static boolean inJS() {
93 public void brwsrCtxExecute() throws Throwable {
94 assertFalse(inJS(), "We aren't in JS now");
95 final CountDownLatch init = new CountDownLatch(1);
96 final BrwsrCtx[] ctx = { null };
97 FXBrowsers.runInBrowser(App.getV1(), new Runnable() {
100 assertTrue(inJS(), "We are in JS context now");
101 ctx[0] = BrwsrCtx.findDefault(FXBrowsersTest.class);
107 final CountDownLatch cdl = new CountDownLatch(1);
108 class R implements Runnable {
111 if (Platform.isFxApplicationThread()) {
115 ctx[0].execute(this);
119 new Thread(new R(), "Background thread").start();
125 public void behaviorOfTwoWebViewsAtOnce() throws Throwable {
126 class R implements Runnable {
133 } catch (Throwable ex) {
138 private void doTest() throws Throwable {
139 URL u = FXBrowsersTest.class.getResource("/org/netbeans/html/boot/fx/empty.html");
140 assertNotNull(u, "URL found");
141 FXBrowsers.load(App.getV1(), u, OnPages.class, "first");
145 PROPERTY_SET = new CountDownLatch(2);
146 Platform.runLater(run);
147 PROPERTY_SET.await();
149 assertEquals(Integer.getInteger("finalFirst"), Integer.valueOf(3), "Three times in view one");
150 assertEquals(Integer.getInteger("finalSecond"), Integer.valueOf(2), "Two times in view one");
152 final CountDownLatch finish = new CountDownLatch(1);
153 final Object[] three = { 0 };
154 assertFalse(Platform.isFxApplicationThread());
155 FXBrowsers.runInBrowser(App.getV1(), new Runnable() {
158 assertTrue(Platform.isFxApplicationThread());
159 three[0] = App.getV1().getEngine().executeScript("window.cntBrwsr");
165 assertEquals(three[0], Integer.valueOf(3));
168 public static class OnPages {
169 static Class<?> first;
170 static Object firstWindow;
172 public static void first() {
173 first = OnPages.class;
174 firstWindow = window();
175 assertNotNull(firstWindow, "First window found");
177 assertEquals(increment(), 1, "Now it is one");
179 URL u = FXBrowsersTest.class.getResource("/org/netbeans/html/boot/fx/empty.html");
180 assertNotNull(u, "URL found");
181 FXBrowsers.load(App.getV2(), u, new Runnable() {
184 OnPages.second("Hello");
188 assertEquals(increment(), 2, "Now it is two and not influenced by second view");
189 System.setProperty("finalFirst", "" + increment());
190 PROPERTY_SET.countDown();
193 public static void second(String... args) {
194 assertEquals(args.length, 1, "One string argument");
195 assertEquals(args[0], "Hello", "It is hello");
196 assertEquals(first, OnPages.class, "Both views share the same classloader");
198 Object window = window();
199 assertNotNull(window, "Some window found");
200 assertNotNull(firstWindow, "First window is known");
201 assertNotSame(firstWindow, window, "The window objects should be different");
203 assertEquals(increment(), 1, "Counting starts from zero");
204 System.setProperty("finalSecond", "" + increment());
205 PROPERTY_SET.countDown();
208 @JavaScriptBody(args = {}, body = "return window;")
209 private static native Object window();
211 @JavaScriptBody(args = {}, body = ""
212 + "if (window.cntBrwsr) return ++window.cntBrwsr;"
213 + "return window.cntBrwsr = 1;"
215 private static native int increment();
218 public static class App extends Application {
219 static final CountDownLatch CDL = new CountDownLatch(1);
220 private static BorderPane pane;
225 static WebView getV1() {
226 return (WebView)System.getProperties().get("v1");
232 static WebView getV2() {
233 return (WebView)System.getProperties().get("v2");
237 public void start(Stage stage) {
238 pane= new BorderPane();
239 Scene scene = new Scene(pane, 800, 600);
240 stage.setScene(scene);
242 System.getProperties().put("v1", new WebView());
243 System.getProperties().put("v2", new WebView());
245 pane.setCenter(getV1());
246 pane.setBottom(getV2());