#257171: FXBrowsers.runInBrowser needs to properly set BrwsrCtx
authorJaroslav Tulach <jtulach@netbeans.org>
Thu, 17 Dec 2015 22:23:41 +0100
changeset 1037e390a06dbfac
parent 1034 f5d6e09568b6
child 1038 c1aabfb0bbb7
#257171: FXBrowsers.runInBrowser needs to properly set BrwsrCtx
boot-fx/src/main/java/net/java/html/boot/fx/FXBrowsers.java
boot-fx/src/test/java/net/java/html/boot/fx/FXBrowsersTest.java
     1.1 --- a/boot-fx/src/main/java/net/java/html/boot/fx/FXBrowsers.java	Mon Dec 14 06:10:54 2015 +0100
     1.2 +++ b/boot-fx/src/main/java/net/java/html/boot/fx/FXBrowsers.java	Thu Dec 17 22:23:41 2015 +0100
     1.3 @@ -113,9 +113,11 @@
     1.4      ) {
     1.5          Object[] context = new Object[args.length + 1];
     1.6          System.arraycopy(args, 0, context, 1, args.length);
     1.7 -        context[0] = new Load(webView);
     1.8 +        final Load load = new Load(webView, null);
     1.9 +        context[0] = load;
    1.10          BrowserBuilder.newBrowser(context).
    1.11              loadPage(url.toExternalForm()).
    1.12 +            loadFinished(load).
    1.13              loadClass(onPageLoad).
    1.14              invoke(methodName, args).
    1.15              showAndWait();
    1.16 @@ -194,10 +196,11 @@
    1.17      ) {
    1.18          Object[] newCtx = new Object[context.length + 1];
    1.19          System.arraycopy(context, 0, newCtx, 1, context.length);
    1.20 -        newCtx[0] = new Load(webView);
    1.21 +        final Load load = new Load(webView, onPageLoad);
    1.22 +        newCtx[0] = load;
    1.23          BrowserBuilder.newBrowser(newCtx).
    1.24                  loadPage(url.toExternalForm()).
    1.25 -                loadFinished(onPageLoad).
    1.26 +                loadFinished(load).
    1.27                  classloader(loader).
    1.28                  showAndWait();
    1.29      }
    1.30 @@ -227,15 +230,25 @@
    1.31          if (!(ud instanceof Load)) {
    1.32              throw new IllegalArgumentException();
    1.33          }
    1.34 -        ((Load)ud).execute(code);
    1.35 +        ((Load)ud).ctx.execute(code);
    1.36      }
    1.37      
    1.38 -    private static class Load extends AbstractFXPresenter {
    1.39 +    private static class Load extends AbstractFXPresenter implements Runnable {
    1.40          private final WebView webView;
    1.41 +        private final Runnable myLoad;
    1.42 +        private BrwsrCtx ctx;
    1.43  
    1.44 -        public Load(WebView webView) {
    1.45 +        public Load(WebView webView, Runnable onLoad) {
    1.46 +            this.webView = webView;
    1.47 +            this.myLoad = onLoad;
    1.48              webView.setUserData(this);
    1.49 -            this.webView = webView;
    1.50 +        }
    1.51 +
    1.52 +        public void run() {
    1.53 +            ctx = BrwsrCtx.findDefault(Load.class);
    1.54 +            if (myLoad != null) {
    1.55 +                myLoad.run();
    1.56 +            }
    1.57          }
    1.58          
    1.59          @Override
     2.1 --- a/boot-fx/src/test/java/net/java/html/boot/fx/FXBrowsersTest.java	Mon Dec 14 06:10:54 2015 +0100
     2.2 +++ b/boot-fx/src/test/java/net/java/html/boot/fx/FXBrowsersTest.java	Thu Dec 17 22:23:41 2015 +0100
     2.3 @@ -50,6 +50,7 @@
     2.4  import javafx.scene.layout.BorderPane;
     2.5  import javafx.scene.web.WebView;
     2.6  import javafx.stage.Stage;
     2.7 +import net.java.html.BrwsrCtx;
     2.8  import net.java.html.js.JavaScriptBody;
     2.9  import static org.testng.Assert.*;
    2.10  import org.testng.annotations.BeforeClass;
    2.11 @@ -82,6 +83,43 @@
    2.12          }.start();
    2.13          App.CDL.await();
    2.14      }
    2.15 +    
    2.16 +    @JavaScriptBody(args = {  }, body = "return true;")
    2.17 +    static boolean inJS() {
    2.18 +        return false;
    2.19 +    }
    2.20 +
    2.21 +    @Test
    2.22 +    public void brwsrCtxExecute() throws Throwable {
    2.23 +        assertFalse(inJS(), "We aren't in JS now");
    2.24 +        final CountDownLatch init = new CountDownLatch(1);
    2.25 +        final BrwsrCtx[] ctx = { null };
    2.26 +        FXBrowsers.runInBrowser(App.getV1(), new Runnable() {
    2.27 +            @Override
    2.28 +            public void run() {
    2.29 +                assertTrue(inJS(), "We are in JS context now");
    2.30 +                ctx[0] = BrwsrCtx.findDefault(FXBrowsersTest.class);
    2.31 +                init.countDown();
    2.32 +            }
    2.33 +        });
    2.34 +        init.await();
    2.35 +
    2.36 +        final CountDownLatch cdl = new CountDownLatch(1);
    2.37 +        class R implements Runnable {
    2.38 +            @Override
    2.39 +            public void run() {
    2.40 +                if (Platform.isFxApplicationThread()) {
    2.41 +                    assertTrue(inJS());
    2.42 +                    cdl.countDown();
    2.43 +                } else {
    2.44 +                    ctx[0].execute(this);
    2.45 +                }
    2.46 +            }
    2.47 +        }
    2.48 +        new Thread(new R(), "Background thread").start();
    2.49 +
    2.50 +        cdl.await();
    2.51 +    }
    2.52  
    2.53      @Test
    2.54      public void behaviorOfTwoWebViewsAtOnce() throws Throwable {