# HG changeset patch # User Jaroslav Tulach # Date 1450387421 -3600 # Node ID e390a06dbfac2a204060e6cc2b6e8cee641bbbc6 # Parent f5d6e09568b63f14fe642626fec8e8f27b80e748 #257171: FXBrowsers.runInBrowser needs to properly set BrwsrCtx diff -r f5d6e09568b6 -r e390a06dbfac boot-fx/src/main/java/net/java/html/boot/fx/FXBrowsers.java --- a/boot-fx/src/main/java/net/java/html/boot/fx/FXBrowsers.java Mon Dec 14 06:10:54 2015 +0100 +++ b/boot-fx/src/main/java/net/java/html/boot/fx/FXBrowsers.java Thu Dec 17 22:23:41 2015 +0100 @@ -113,9 +113,11 @@ ) { Object[] context = new Object[args.length + 1]; System.arraycopy(args, 0, context, 1, args.length); - context[0] = new Load(webView); + final Load load = new Load(webView, null); + context[0] = load; BrowserBuilder.newBrowser(context). loadPage(url.toExternalForm()). + loadFinished(load). loadClass(onPageLoad). invoke(methodName, args). showAndWait(); @@ -194,10 +196,11 @@ ) { Object[] newCtx = new Object[context.length + 1]; System.arraycopy(context, 0, newCtx, 1, context.length); - newCtx[0] = new Load(webView); + final Load load = new Load(webView, onPageLoad); + newCtx[0] = load; BrowserBuilder.newBrowser(newCtx). loadPage(url.toExternalForm()). - loadFinished(onPageLoad). + loadFinished(load). classloader(loader). showAndWait(); } @@ -227,15 +230,25 @@ if (!(ud instanceof Load)) { throw new IllegalArgumentException(); } - ((Load)ud).execute(code); + ((Load)ud).ctx.execute(code); } - private static class Load extends AbstractFXPresenter { + private static class Load extends AbstractFXPresenter implements Runnable { private final WebView webView; + private final Runnable myLoad; + private BrwsrCtx ctx; - public Load(WebView webView) { + public Load(WebView webView, Runnable onLoad) { + this.webView = webView; + this.myLoad = onLoad; webView.setUserData(this); - this.webView = webView; + } + + public void run() { + ctx = BrwsrCtx.findDefault(Load.class); + if (myLoad != null) { + myLoad.run(); + } } @Override diff -r f5d6e09568b6 -r e390a06dbfac boot-fx/src/test/java/net/java/html/boot/fx/FXBrowsersTest.java --- a/boot-fx/src/test/java/net/java/html/boot/fx/FXBrowsersTest.java Mon Dec 14 06:10:54 2015 +0100 +++ b/boot-fx/src/test/java/net/java/html/boot/fx/FXBrowsersTest.java Thu Dec 17 22:23:41 2015 +0100 @@ -50,6 +50,7 @@ import javafx.scene.layout.BorderPane; import javafx.scene.web.WebView; import javafx.stage.Stage; +import net.java.html.BrwsrCtx; import net.java.html.js.JavaScriptBody; import static org.testng.Assert.*; import org.testng.annotations.BeforeClass; @@ -82,6 +83,43 @@ }.start(); App.CDL.await(); } + + @JavaScriptBody(args = { }, body = "return true;") + static boolean inJS() { + return false; + } + + @Test + public void brwsrCtxExecute() throws Throwable { + assertFalse(inJS(), "We aren't in JS now"); + final CountDownLatch init = new CountDownLatch(1); + final BrwsrCtx[] ctx = { null }; + FXBrowsers.runInBrowser(App.getV1(), new Runnable() { + @Override + public void run() { + assertTrue(inJS(), "We are in JS context now"); + ctx[0] = BrwsrCtx.findDefault(FXBrowsersTest.class); + init.countDown(); + } + }); + init.await(); + + final CountDownLatch cdl = new CountDownLatch(1); + class R implements Runnable { + @Override + public void run() { + if (Platform.isFxApplicationThread()) { + assertTrue(inJS()); + cdl.countDown(); + } else { + ctx[0].execute(this); + } + } + } + new Thread(new R(), "Background thread").start(); + + cdl.await(); + } @Test public void behaviorOfTwoWebViewsAtOnce() throws Throwable {