boot-fx/src/test/java/net/java/html/boot/fx/FXBrowsersTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Thu, 17 Dec 2015 22:23:41 +0100
changeset 1037 e390a06dbfac
parent 1016 665b10c62f3d
permissions -rw-r--r--
#257171: FXBrowsers.runInBrowser needs to properly set BrwsrCtx
jaroslav@288
     1
/**
jaroslav@358
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jaroslav@288
     3
 *
jaroslav@551
     4
 * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
jaroslav@288
     5
 *
jaroslav@358
     6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
jaroslav@358
     7
 * Other names may be trademarks of their respective owners.
jaroslav@288
     8
 *
jaroslav@358
     9
 * The contents of this file are subject to the terms of either the GNU
jaroslav@358
    10
 * General Public License Version 2 only ("GPL") or the Common
jaroslav@358
    11
 * Development and Distribution License("CDDL") (collectively, the
jaroslav@358
    12
 * "License"). You may not use this file except in compliance with the
jaroslav@358
    13
 * License. You can obtain a copy of the License at
jaroslav@358
    14
 * http://www.netbeans.org/cddl-gplv2.html
jaroslav@358
    15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jaroslav@358
    16
 * specific language governing permissions and limitations under the
jaroslav@358
    17
 * License.  When distributing the software, include this License Header
jaroslav@358
    18
 * Notice in each file and include the License file at
jaroslav@358
    19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
jaroslav@358
    20
 * particular file as subject to the "Classpath" exception as provided
jaroslav@358
    21
 * by Oracle in the GPL Version 2 section of the License file that
jaroslav@358
    22
 * accompanied this code. If applicable, add the following below the
jaroslav@358
    23
 * License Header, with the fields enclosed by brackets [] replaced by
jaroslav@358
    24
 * your own identifying information:
jaroslav@358
    25
 * "Portions Copyrighted [year] [name of copyright owner]"
jaroslav@358
    26
 *
jaroslav@358
    27
 * Contributor(s):
jaroslav@358
    28
 *
jaroslav@358
    29
 * The Original Software is NetBeans. The Initial Developer of the Original
jaroslav@551
    30
 * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
jaroslav@358
    31
 *
jaroslav@358
    32
 * If you wish your version of this file to be governed by only the CDDL
jaroslav@358
    33
 * or only the GPL Version 2, indicate your decision by adding
jaroslav@358
    34
 * "[Contributor] elects to include this software in this distribution
jaroslav@358
    35
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
jaroslav@358
    36
 * single choice of license, a recipient has the option to distribute
jaroslav@358
    37
 * your version of this file under either the CDDL, the GPL Version 2 or
jaroslav@358
    38
 * to extend the choice of license to its licensees as provided above.
jaroslav@358
    39
 * However, if you add GPL Version 2 code and therefore, elected the GPL
jaroslav@358
    40
 * Version 2 license, then the option applies only if the new code is
jaroslav@358
    41
 * made subject to such option by the copyright holder.
jaroslav@288
    42
 */
jaroslav@288
    43
package net.java.html.boot.fx;
jaroslav@288
    44
jaroslav@288
    45
import java.net.URL;
jaroslav@288
    46
import java.util.concurrent.CountDownLatch;
jaroslav@288
    47
import javafx.application.Application;
jaroslav@288
    48
import javafx.application.Platform;
jaroslav@288
    49
import javafx.scene.Scene;
jaroslav@288
    50
import javafx.scene.layout.BorderPane;
jaroslav@288
    51
import javafx.scene.web.WebView;
jaroslav@288
    52
import javafx.stage.Stage;
jtulach@1037
    53
import net.java.html.BrwsrCtx;
jaroslav@288
    54
import net.java.html.js.JavaScriptBody;
jtulach@656
    55
import static org.testng.Assert.*;
jaroslav@288
    56
import org.testng.annotations.BeforeClass;
jaroslav@288
    57
import org.testng.annotations.Test;
jaroslav@288
    58
jaroslav@288
    59
/**
jaroslav@288
    60
 *
jtulach@655
    61
 * @author Jaroslav Tulach
jaroslav@288
    62
 */
jaroslav@288
    63
public class FXBrowsersTest {
jtulach@1016
    64
    private static CountDownLatch PROPERTY_SET;
jtulach@1016
    65
jaroslav@288
    66
    public FXBrowsersTest() {
jaroslav@288
    67
    }
jaroslav@288
    68
    
jaroslav@288
    69
    @BeforeClass public void initFX() throws Throwable {
jaroslav@288
    70
        new Thread("initFX") {
jaroslav@288
    71
            @Override
jaroslav@288
    72
            public void run() {
jtulach@1008
    73
                if (Platform.isFxApplicationThread()) {
jtulach@1008
    74
                    new App().start(new Stage());
jtulach@1008
    75
                } else {
jtulach@1008
    76
                    try {
jtulach@1008
    77
                        App.launch(App.class);
jtulach@1008
    78
                    } catch (IllegalStateException ex) {
jtulach@1008
    79
                        Platform.runLater(this);
jtulach@1008
    80
                    }
jtulach@1008
    81
                }
jaroslav@288
    82
            }
jaroslav@288
    83
        }.start();
jaroslav@288
    84
        App.CDL.await();
jaroslav@288
    85
    }
jtulach@1037
    86
    
jtulach@1037
    87
    @JavaScriptBody(args = {  }, body = "return true;")
jtulach@1037
    88
    static boolean inJS() {
jtulach@1037
    89
        return false;
jtulach@1037
    90
    }
jtulach@1037
    91
jtulach@1037
    92
    @Test
jtulach@1037
    93
    public void brwsrCtxExecute() throws Throwable {
jtulach@1037
    94
        assertFalse(inJS(), "We aren't in JS now");
jtulach@1037
    95
        final CountDownLatch init = new CountDownLatch(1);
jtulach@1037
    96
        final BrwsrCtx[] ctx = { null };
jtulach@1037
    97
        FXBrowsers.runInBrowser(App.getV1(), new Runnable() {
jtulach@1037
    98
            @Override
jtulach@1037
    99
            public void run() {
jtulach@1037
   100
                assertTrue(inJS(), "We are in JS context now");
jtulach@1037
   101
                ctx[0] = BrwsrCtx.findDefault(FXBrowsersTest.class);
jtulach@1037
   102
                init.countDown();
jtulach@1037
   103
            }
jtulach@1037
   104
        });
jtulach@1037
   105
        init.await();
jtulach@1037
   106
jtulach@1037
   107
        final CountDownLatch cdl = new CountDownLatch(1);
jtulach@1037
   108
        class R implements Runnable {
jtulach@1037
   109
            @Override
jtulach@1037
   110
            public void run() {
jtulach@1037
   111
                if (Platform.isFxApplicationThread()) {
jtulach@1037
   112
                    assertTrue(inJS());
jtulach@1037
   113
                    cdl.countDown();
jtulach@1037
   114
                } else {
jtulach@1037
   115
                    ctx[0].execute(this);
jtulach@1037
   116
                }
jtulach@1037
   117
            }
jtulach@1037
   118
        }
jtulach@1037
   119
        new Thread(new R(), "Background thread").start();
jtulach@1037
   120
jtulach@1037
   121
        cdl.await();
jtulach@1037
   122
    }
jaroslav@288
   123
jaroslav@288
   124
    @Test
jaroslav@288
   125
    public void behaviorOfTwoWebViewsAtOnce() throws Throwable {
jaroslav@288
   126
        class R implements Runnable {
jaroslav@288
   127
            Throwable t;
jaroslav@288
   128
jaroslav@288
   129
            @Override
jaroslav@288
   130
            public void run() {
jaroslav@288
   131
                try {
jaroslav@288
   132
                    doTest();
jaroslav@288
   133
                } catch (Throwable ex) {
jaroslav@288
   134
                    t = ex;
jaroslav@288
   135
                }
jaroslav@288
   136
            }
jaroslav@288
   137
            
jaroslav@288
   138
            private void doTest() throws Throwable {
jaroslav@362
   139
                URL u = FXBrowsersTest.class.getResource("/org/netbeans/html/boot/fx/empty.html");
jaroslav@288
   140
                assertNotNull(u, "URL found");
jaroslav@288
   141
                FXBrowsers.load(App.getV1(), u, OnPages.class, "first");
jaroslav@288
   142
            }
jaroslav@288
   143
        }
jaroslav@288
   144
        R run = new R();
jtulach@1016
   145
        PROPERTY_SET = new CountDownLatch(2);
jaroslav@288
   146
        Platform.runLater(run);
jtulach@1016
   147
        PROPERTY_SET.await();
jaroslav@288
   148
        
jaroslav@288
   149
        assertEquals(Integer.getInteger("finalFirst"), Integer.valueOf(3), "Three times in view one");
jaroslav@288
   150
        assertEquals(Integer.getInteger("finalSecond"), Integer.valueOf(2), "Two times in view one");
jtulach@656
   151
jtulach@656
   152
        final CountDownLatch finish = new CountDownLatch(1);
jtulach@656
   153
        final Object[] three = { 0 };
jtulach@656
   154
        assertFalse(Platform.isFxApplicationThread());
jtulach@656
   155
        FXBrowsers.runInBrowser(App.getV1(), new Runnable() {
jtulach@656
   156
            @Override
jtulach@656
   157
            public void run() {
jtulach@656
   158
                assertTrue(Platform.isFxApplicationThread());
jtulach@1008
   159
                three[0] = App.getV1().getEngine().executeScript("window.cntBrwsr");
jtulach@656
   160
                finish.countDown();
jtulach@656
   161
            }
jtulach@656
   162
        });
jtulach@656
   163
        finish.await();
jtulach@656
   164
        
jtulach@656
   165
        assertEquals(three[0], Integer.valueOf(3));
jaroslav@288
   166
    }
jaroslav@288
   167
    
jaroslav@288
   168
    public static class OnPages {
jaroslav@288
   169
        static Class<?> first;
jaroslav@288
   170
        static Object firstWindow;
jaroslav@288
   171
        
jaroslav@288
   172
        public static void first() {
jaroslav@288
   173
            first = OnPages.class;
jaroslav@288
   174
            firstWindow = window();
jaroslav@288
   175
            assertNotNull(firstWindow, "First window found");
jaroslav@288
   176
            
jaroslav@288
   177
            assertEquals(increment(), 1, "Now it is one");
jaroslav@288
   178
            
jaroslav@362
   179
            URL u = FXBrowsersTest.class.getResource("/org/netbeans/html/boot/fx/empty.html");
jaroslav@288
   180
            assertNotNull(u, "URL found");
jtulach@656
   181
            FXBrowsers.load(App.getV2(), u, new Runnable() {
jtulach@656
   182
                @Override
jtulach@656
   183
                public void run() {
jtulach@656
   184
                    OnPages.second("Hello");
jtulach@656
   185
                }
jtulach@656
   186
            });
jaroslav@288
   187
            
jaroslav@288
   188
            assertEquals(increment(), 2, "Now it is two and not influenced by second view");
jaroslav@288
   189
            System.setProperty("finalFirst", "" + increment());
jtulach@1016
   190
            PROPERTY_SET.countDown();
jaroslav@288
   191
        }
jaroslav@288
   192
        
jaroslav@288
   193
        public static void second(String... args) {
jaroslav@288
   194
            assertEquals(args.length, 1, "One string argument");
jaroslav@288
   195
            assertEquals(args[0], "Hello", "It is hello");
jaroslav@288
   196
            assertEquals(first, OnPages.class, "Both views share the same classloader");
jaroslav@288
   197
            
jaroslav@288
   198
            Object window = window();
jaroslav@288
   199
            assertNotNull(window, "Some window found");
jaroslav@288
   200
            assertNotNull(firstWindow, "First window is known");
jaroslav@288
   201
            assertNotSame(firstWindow, window, "The window objects should be different");
jaroslav@288
   202
            
jaroslav@288
   203
            assertEquals(increment(), 1, "Counting starts from zero");
jaroslav@288
   204
            System.setProperty("finalSecond", "" + increment());
jtulach@1016
   205
            PROPERTY_SET.countDown();
jaroslav@288
   206
        }
jaroslav@288
   207
        
jaroslav@288
   208
        @JavaScriptBody(args = {}, body = "return window;")
jaroslav@288
   209
        private static native Object window();
jaroslav@288
   210
        
jaroslav@288
   211
        @JavaScriptBody(args = {}, body = ""
jtulach@1008
   212
            + "if (window.cntBrwsr) return ++window.cntBrwsr;"
jtulach@1008
   213
            + "return window.cntBrwsr = 1;"
jaroslav@288
   214
        )
jaroslav@288
   215
        private static native int increment();
jaroslav@288
   216
    }
jaroslav@288
   217
    
jaroslav@288
   218
    public static class App extends Application {
jaroslav@288
   219
        static final CountDownLatch CDL = new CountDownLatch(1);
jaroslav@288
   220
        private static BorderPane pane;
jaroslav@288
   221
jaroslav@288
   222
        /**
jaroslav@288
   223
         * @return the v1
jaroslav@288
   224
         */
jaroslav@288
   225
        static WebView getV1() {
jaroslav@288
   226
            return (WebView)System.getProperties().get("v1");
jaroslav@288
   227
        }
jaroslav@288
   228
jaroslav@288
   229
        /**
jaroslav@288
   230
         * @return the v2
jaroslav@288
   231
         */
jaroslav@288
   232
        static WebView getV2() {
jaroslav@288
   233
            return (WebView)System.getProperties().get("v2");
jaroslav@288
   234
        }
jaroslav@288
   235
jaroslav@288
   236
        @Override
jtulach@1008
   237
        public void start(Stage stage) {
jaroslav@288
   238
            pane= new BorderPane();
jaroslav@288
   239
            Scene scene = new Scene(pane, 800, 600);
jaroslav@288
   240
            stage.setScene(scene);
jaroslav@288
   241
            
jaroslav@288
   242
            System.getProperties().put("v1", new WebView());
jaroslav@288
   243
            System.getProperties().put("v2", new WebView());
jaroslav@288
   244
jaroslav@288
   245
            pane.setCenter(getV1());
jaroslav@288
   246
            pane.setBottom(getV2());
jaroslav@288
   247
jaroslav@288
   248
            CDL.countDown();
jaroslav@288
   249
        }
jaroslav@288
   250
        
jaroslav@288
   251
        
jaroslav@288
   252
    }
jaroslav@288
   253
}