boot-fx/src/test/java/net/java/html/boot/fx/FXBrowsersTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sun, 11 Oct 2015 07:24:32 +0200
changeset 1008 c535c36881af
parent 1006 a0f79e32d526
child 1016 665b10c62f3d
permissions -rw-r--r--
#255831: BrowserBuilder.showAndWait can be used multiple times with JavaFX presenter. Demonstrated by running the boot-fx tests in 'fork once' mode.
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;
jaroslav@288
    53
import net.java.html.js.JavaScriptBody;
jtulach@656
    54
import static org.testng.Assert.*;
jaroslav@288
    55
import org.testng.annotations.BeforeClass;
jaroslav@288
    56
import org.testng.annotations.Test;
jaroslav@288
    57
jaroslav@288
    58
/**
jaroslav@288
    59
 *
jtulach@655
    60
 * @author Jaroslav Tulach
jaroslav@288
    61
 */
jaroslav@288
    62
public class FXBrowsersTest {
jaroslav@288
    63
    
jaroslav@288
    64
    public FXBrowsersTest() {
jaroslav@288
    65
    }
jaroslav@288
    66
    
jaroslav@288
    67
    @BeforeClass public void initFX() throws Throwable {
jaroslav@288
    68
        new Thread("initFX") {
jaroslav@288
    69
            @Override
jaroslav@288
    70
            public void run() {
jtulach@1008
    71
                if (Platform.isFxApplicationThread()) {
jtulach@1008
    72
                    new App().start(new Stage());
jtulach@1008
    73
                } else {
jtulach@1008
    74
                    try {
jtulach@1008
    75
                        App.launch(App.class);
jtulach@1008
    76
                    } catch (IllegalStateException ex) {
jtulach@1008
    77
                        Platform.runLater(this);
jtulach@1008
    78
                    }
jtulach@1008
    79
                }
jaroslav@288
    80
            }
jaroslav@288
    81
        }.start();
jaroslav@288
    82
        App.CDL.await();
jaroslav@288
    83
    }
jaroslav@288
    84
jaroslav@288
    85
    @Test
jaroslav@288
    86
    public void behaviorOfTwoWebViewsAtOnce() throws Throwable {
jaroslav@288
    87
        class R implements Runnable {
jaroslav@288
    88
            CountDownLatch DONE = new CountDownLatch(1);
jaroslav@288
    89
            Throwable t;
jaroslav@288
    90
jaroslav@288
    91
            @Override
jaroslav@288
    92
            public void run() {
jaroslav@288
    93
                try {
jaroslav@288
    94
                    doTest();
jaroslav@288
    95
                } catch (Throwable ex) {
jaroslav@288
    96
                    t = ex;
jaroslav@288
    97
                } finally {
jaroslav@288
    98
                    DONE.countDown();
jaroslav@288
    99
                }
jaroslav@288
   100
            }
jaroslav@288
   101
            
jaroslav@288
   102
            private void doTest() throws Throwable {
jaroslav@362
   103
                URL u = FXBrowsersTest.class.getResource("/org/netbeans/html/boot/fx/empty.html");
jaroslav@288
   104
                assertNotNull(u, "URL found");
jaroslav@288
   105
                FXBrowsers.load(App.getV1(), u, OnPages.class, "first");
jaroslav@288
   106
                
jaroslav@288
   107
            }
jaroslav@288
   108
        }
jaroslav@288
   109
        R run = new R();
jaroslav@288
   110
        Platform.runLater(run);
jaroslav@288
   111
        run.DONE.await();
jaroslav@288
   112
        for (int i = 0; i < 100; i++) {
jaroslav@288
   113
            if (run.t != null) {
jaroslav@288
   114
                throw run.t;
jaroslav@288
   115
            }
jaroslav@288
   116
            if (System.getProperty("finalSecond") == null) {
jaroslav@288
   117
                Thread.sleep(100);
jaroslav@288
   118
            }
jaroslav@288
   119
        }
jaroslav@288
   120
        
jaroslav@288
   121
        
jaroslav@288
   122
        
jaroslav@288
   123
        assertEquals(Integer.getInteger("finalFirst"), Integer.valueOf(3), "Three times in view one");
jaroslav@288
   124
        assertEquals(Integer.getInteger("finalSecond"), Integer.valueOf(2), "Two times in view one");
jtulach@656
   125
jtulach@656
   126
        final CountDownLatch finish = new CountDownLatch(1);
jtulach@656
   127
        final Object[] three = { 0 };
jtulach@656
   128
        assertFalse(Platform.isFxApplicationThread());
jtulach@656
   129
        FXBrowsers.runInBrowser(App.getV1(), new Runnable() {
jtulach@656
   130
            @Override
jtulach@656
   131
            public void run() {
jtulach@656
   132
                assertTrue(Platform.isFxApplicationThread());
jtulach@1008
   133
                three[0] = App.getV1().getEngine().executeScript("window.cntBrwsr");
jtulach@656
   134
                finish.countDown();
jtulach@656
   135
            }
jtulach@656
   136
        });
jtulach@656
   137
        finish.await();
jtulach@656
   138
        
jtulach@656
   139
        assertEquals(three[0], Integer.valueOf(3));
jaroslav@288
   140
    }
jaroslav@288
   141
    
jaroslav@288
   142
    public static class OnPages {
jaroslav@288
   143
        static Class<?> first;
jaroslav@288
   144
        static Object firstWindow;
jaroslav@288
   145
        
jaroslav@288
   146
        public static void first() {
jaroslav@288
   147
            first = OnPages.class;
jaroslav@288
   148
            firstWindow = window();
jaroslav@288
   149
            assertNotNull(firstWindow, "First window found");
jaroslav@288
   150
            
jaroslav@288
   151
            assertEquals(increment(), 1, "Now it is one");
jaroslav@288
   152
            
jaroslav@362
   153
            URL u = FXBrowsersTest.class.getResource("/org/netbeans/html/boot/fx/empty.html");
jaroslav@288
   154
            assertNotNull(u, "URL found");
jtulach@656
   155
            FXBrowsers.load(App.getV2(), u, new Runnable() {
jtulach@656
   156
                @Override
jtulach@656
   157
                public void run() {
jtulach@656
   158
                    OnPages.second("Hello");
jtulach@656
   159
                }
jtulach@656
   160
            });
jaroslav@288
   161
            
jaroslav@288
   162
            assertEquals(increment(), 2, "Now it is two and not influenced by second view");
jaroslav@288
   163
            System.setProperty("finalFirst", "" + increment());
jaroslav@288
   164
        }
jaroslav@288
   165
        
jaroslav@288
   166
        public static void second(String... args) {
jaroslav@288
   167
            assertEquals(args.length, 1, "One string argument");
jaroslav@288
   168
            assertEquals(args[0], "Hello", "It is hello");
jaroslav@288
   169
            assertEquals(first, OnPages.class, "Both views share the same classloader");
jaroslav@288
   170
            
jaroslav@288
   171
            Object window = window();
jaroslav@288
   172
            assertNotNull(window, "Some window found");
jaroslav@288
   173
            assertNotNull(firstWindow, "First window is known");
jaroslav@288
   174
            assertNotSame(firstWindow, window, "The window objects should be different");
jaroslav@288
   175
            
jaroslav@288
   176
            assertEquals(increment(), 1, "Counting starts from zero");
jaroslav@288
   177
            System.setProperty("finalSecond", "" + increment());
jaroslav@288
   178
        }
jaroslav@288
   179
        
jaroslav@288
   180
        @JavaScriptBody(args = {}, body = "return window;")
jaroslav@288
   181
        private static native Object window();
jaroslav@288
   182
        
jaroslav@288
   183
        @JavaScriptBody(args = {}, body = ""
jtulach@1008
   184
            + "if (window.cntBrwsr) return ++window.cntBrwsr;"
jtulach@1008
   185
            + "return window.cntBrwsr = 1;"
jaroslav@288
   186
        )
jaroslav@288
   187
        private static native int increment();
jaroslav@288
   188
    }
jaroslav@288
   189
    
jaroslav@288
   190
    public static class App extends Application {
jaroslav@288
   191
        static final CountDownLatch CDL = new CountDownLatch(1);
jaroslav@288
   192
        private static BorderPane pane;
jaroslav@288
   193
jaroslav@288
   194
        /**
jaroslav@288
   195
         * @return the v1
jaroslav@288
   196
         */
jaroslav@288
   197
        static WebView getV1() {
jaroslav@288
   198
            return (WebView)System.getProperties().get("v1");
jaroslav@288
   199
        }
jaroslav@288
   200
jaroslav@288
   201
        /**
jaroslav@288
   202
         * @return the v2
jaroslav@288
   203
         */
jaroslav@288
   204
        static WebView getV2() {
jaroslav@288
   205
            return (WebView)System.getProperties().get("v2");
jaroslav@288
   206
        }
jaroslav@288
   207
jaroslav@288
   208
        @Override
jtulach@1008
   209
        public void start(Stage stage) {
jaroslav@288
   210
            pane= new BorderPane();
jaroslav@288
   211
            Scene scene = new Scene(pane, 800, 600);
jaroslav@288
   212
            stage.setScene(scene);
jaroslav@288
   213
            
jaroslav@288
   214
            System.getProperties().put("v1", new WebView());
jaroslav@288
   215
            System.getProperties().put("v2", new WebView());
jaroslav@288
   216
jaroslav@288
   217
            pane.setCenter(getV1());
jaroslav@288
   218
            pane.setBottom(getV2());
jaroslav@288
   219
jaroslav@288
   220
            CDL.countDown();
jaroslav@288
   221
        }
jaroslav@288
   222
        
jaroslav@288
   223
        
jaroslav@288
   224
    }
jaroslav@288
   225
}