boot-fx/src/test/java/net/java/html/boot/fx/FXBrowsersTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sun, 01 Nov 2015 16:59:42 +0100
changeset 1016 665b10c62f3d
parent 1008 c535c36881af
child 1037 e390a06dbfac
permissions -rw-r--r--
Make the test more robust by counting down the latch only when the then to be tested property is set
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 {
jtulach@1016
    63
    private static CountDownLatch PROPERTY_SET;
jtulach@1016
    64
jaroslav@288
    65
    public FXBrowsersTest() {
jaroslav@288
    66
    }
jaroslav@288
    67
    
jaroslav@288
    68
    @BeforeClass public void initFX() throws Throwable {
jaroslav@288
    69
        new Thread("initFX") {
jaroslav@288
    70
            @Override
jaroslav@288
    71
            public void run() {
jtulach@1008
    72
                if (Platform.isFxApplicationThread()) {
jtulach@1008
    73
                    new App().start(new Stage());
jtulach@1008
    74
                } else {
jtulach@1008
    75
                    try {
jtulach@1008
    76
                        App.launch(App.class);
jtulach@1008
    77
                    } catch (IllegalStateException ex) {
jtulach@1008
    78
                        Platform.runLater(this);
jtulach@1008
    79
                    }
jtulach@1008
    80
                }
jaroslav@288
    81
            }
jaroslav@288
    82
        }.start();
jaroslav@288
    83
        App.CDL.await();
jaroslav@288
    84
    }
jaroslav@288
    85
jaroslav@288
    86
    @Test
jaroslav@288
    87
    public void behaviorOfTwoWebViewsAtOnce() throws Throwable {
jaroslav@288
    88
        class R implements Runnable {
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
                }
jaroslav@288
    98
            }
jaroslav@288
    99
            
jaroslav@288
   100
            private void doTest() throws Throwable {
jaroslav@362
   101
                URL u = FXBrowsersTest.class.getResource("/org/netbeans/html/boot/fx/empty.html");
jaroslav@288
   102
                assertNotNull(u, "URL found");
jaroslav@288
   103
                FXBrowsers.load(App.getV1(), u, OnPages.class, "first");
jaroslav@288
   104
            }
jaroslav@288
   105
        }
jaroslav@288
   106
        R run = new R();
jtulach@1016
   107
        PROPERTY_SET = new CountDownLatch(2);
jaroslav@288
   108
        Platform.runLater(run);
jtulach@1016
   109
        PROPERTY_SET.await();
jaroslav@288
   110
        
jaroslav@288
   111
        assertEquals(Integer.getInteger("finalFirst"), Integer.valueOf(3), "Three times in view one");
jaroslav@288
   112
        assertEquals(Integer.getInteger("finalSecond"), Integer.valueOf(2), "Two times in view one");
jtulach@656
   113
jtulach@656
   114
        final CountDownLatch finish = new CountDownLatch(1);
jtulach@656
   115
        final Object[] three = { 0 };
jtulach@656
   116
        assertFalse(Platform.isFxApplicationThread());
jtulach@656
   117
        FXBrowsers.runInBrowser(App.getV1(), new Runnable() {
jtulach@656
   118
            @Override
jtulach@656
   119
            public void run() {
jtulach@656
   120
                assertTrue(Platform.isFxApplicationThread());
jtulach@1008
   121
                three[0] = App.getV1().getEngine().executeScript("window.cntBrwsr");
jtulach@656
   122
                finish.countDown();
jtulach@656
   123
            }
jtulach@656
   124
        });
jtulach@656
   125
        finish.await();
jtulach@656
   126
        
jtulach@656
   127
        assertEquals(three[0], Integer.valueOf(3));
jaroslav@288
   128
    }
jaroslav@288
   129
    
jaroslav@288
   130
    public static class OnPages {
jaroslav@288
   131
        static Class<?> first;
jaroslav@288
   132
        static Object firstWindow;
jaroslav@288
   133
        
jaroslav@288
   134
        public static void first() {
jaroslav@288
   135
            first = OnPages.class;
jaroslav@288
   136
            firstWindow = window();
jaroslav@288
   137
            assertNotNull(firstWindow, "First window found");
jaroslav@288
   138
            
jaroslav@288
   139
            assertEquals(increment(), 1, "Now it is one");
jaroslav@288
   140
            
jaroslav@362
   141
            URL u = FXBrowsersTest.class.getResource("/org/netbeans/html/boot/fx/empty.html");
jaroslav@288
   142
            assertNotNull(u, "URL found");
jtulach@656
   143
            FXBrowsers.load(App.getV2(), u, new Runnable() {
jtulach@656
   144
                @Override
jtulach@656
   145
                public void run() {
jtulach@656
   146
                    OnPages.second("Hello");
jtulach@656
   147
                }
jtulach@656
   148
            });
jaroslav@288
   149
            
jaroslav@288
   150
            assertEquals(increment(), 2, "Now it is two and not influenced by second view");
jaroslav@288
   151
            System.setProperty("finalFirst", "" + increment());
jtulach@1016
   152
            PROPERTY_SET.countDown();
jaroslav@288
   153
        }
jaroslav@288
   154
        
jaroslav@288
   155
        public static void second(String... args) {
jaroslav@288
   156
            assertEquals(args.length, 1, "One string argument");
jaroslav@288
   157
            assertEquals(args[0], "Hello", "It is hello");
jaroslav@288
   158
            assertEquals(first, OnPages.class, "Both views share the same classloader");
jaroslav@288
   159
            
jaroslav@288
   160
            Object window = window();
jaroslav@288
   161
            assertNotNull(window, "Some window found");
jaroslav@288
   162
            assertNotNull(firstWindow, "First window is known");
jaroslav@288
   163
            assertNotSame(firstWindow, window, "The window objects should be different");
jaroslav@288
   164
            
jaroslav@288
   165
            assertEquals(increment(), 1, "Counting starts from zero");
jaroslav@288
   166
            System.setProperty("finalSecond", "" + increment());
jtulach@1016
   167
            PROPERTY_SET.countDown();
jaroslav@288
   168
        }
jaroslav@288
   169
        
jaroslav@288
   170
        @JavaScriptBody(args = {}, body = "return window;")
jaroslav@288
   171
        private static native Object window();
jaroslav@288
   172
        
jaroslav@288
   173
        @JavaScriptBody(args = {}, body = ""
jtulach@1008
   174
            + "if (window.cntBrwsr) return ++window.cntBrwsr;"
jtulach@1008
   175
            + "return window.cntBrwsr = 1;"
jaroslav@288
   176
        )
jaroslav@288
   177
        private static native int increment();
jaroslav@288
   178
    }
jaroslav@288
   179
    
jaroslav@288
   180
    public static class App extends Application {
jaroslav@288
   181
        static final CountDownLatch CDL = new CountDownLatch(1);
jaroslav@288
   182
        private static BorderPane pane;
jaroslav@288
   183
jaroslav@288
   184
        /**
jaroslav@288
   185
         * @return the v1
jaroslav@288
   186
         */
jaroslav@288
   187
        static WebView getV1() {
jaroslav@288
   188
            return (WebView)System.getProperties().get("v1");
jaroslav@288
   189
        }
jaroslav@288
   190
jaroslav@288
   191
        /**
jaroslav@288
   192
         * @return the v2
jaroslav@288
   193
         */
jaroslav@288
   194
        static WebView getV2() {
jaroslav@288
   195
            return (WebView)System.getProperties().get("v2");
jaroslav@288
   196
        }
jaroslav@288
   197
jaroslav@288
   198
        @Override
jtulach@1008
   199
        public void start(Stage stage) {
jaroslav@288
   200
            pane= new BorderPane();
jaroslav@288
   201
            Scene scene = new Scene(pane, 800, 600);
jaroslav@288
   202
            stage.setScene(scene);
jaroslav@288
   203
            
jaroslav@288
   204
            System.getProperties().put("v1", new WebView());
jaroslav@288
   205
            System.getProperties().put("v2", new WebView());
jaroslav@288
   206
jaroslav@288
   207
            pane.setCenter(getV1());
jaroslav@288
   208
            pane.setBottom(getV2());
jaroslav@288
   209
jaroslav@288
   210
            CDL.countDown();
jaroslav@288
   211
        }
jaroslav@288
   212
        
jaroslav@288
   213
        
jaroslav@288
   214
    }
jaroslav@288
   215
}