boot-fx/src/test/java/net/java/html/boot/fx/FXBrowsersOnResourceTest.java
author Jaroslav Tulach <jaroslav.tulach@netbeans.org>
Fri, 07 Feb 2014 07:44:34 +0100
changeset 551 7ca2253fa86d
parent 365 5c93ad8c7a15
child 655 7211ec5f3172
permissions -rw-r--r--
Updating copyright headers to mention current year
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;
jaroslav@349
    54
import net.java.html.js.JavaScriptResource;
jaroslav@288
    55
import static org.testng.Assert.assertEquals;
jaroslav@288
    56
import static org.testng.Assert.assertNotNull;
jaroslav@288
    57
import static org.testng.Assert.assertNotSame;
jaroslav@288
    58
import org.testng.annotations.BeforeClass;
jaroslav@288
    59
import org.testng.annotations.Test;
jaroslav@288
    60
jaroslav@288
    61
/**
jaroslav@288
    62
 *
jaroslav@288
    63
 * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@288
    64
 */
jaroslav@349
    65
public class FXBrowsersOnResourceTest {
jaroslav@288
    66
    
jaroslav@349
    67
    public FXBrowsersOnResourceTest() {
jaroslav@288
    68
    }
jaroslav@288
    69
    
jaroslav@288
    70
    @BeforeClass public void initFX() throws Throwable {
jaroslav@288
    71
        new Thread("initFX") {
jaroslav@288
    72
            @Override
jaroslav@288
    73
            public void run() {
jaroslav@288
    74
                App.launch(App.class);
jaroslav@288
    75
            }
jaroslav@288
    76
        }.start();
jaroslav@288
    77
        App.CDL.await();
jaroslav@288
    78
    }
jaroslav@288
    79
jaroslav@288
    80
    @Test
jaroslav@288
    81
    public void behaviorOfTwoWebViewsAtOnce() throws Throwable {
jaroslav@288
    82
        class R implements Runnable {
jaroslav@288
    83
            CountDownLatch DONE = new CountDownLatch(1);
jaroslav@288
    84
            Throwable t;
jaroslav@288
    85
jaroslav@288
    86
            @Override
jaroslav@288
    87
            public void run() {
jaroslav@288
    88
                try {
jaroslav@288
    89
                    doTest();
jaroslav@288
    90
                } catch (Throwable ex) {
jaroslav@288
    91
                    t = ex;
jaroslav@288
    92
                } finally {
jaroslav@288
    93
                    DONE.countDown();
jaroslav@288
    94
                }
jaroslav@288
    95
            }
jaroslav@288
    96
            
jaroslav@288
    97
            private void doTest() throws Throwable {
jaroslav@362
    98
                URL u = FXBrowsersOnResourceTest.class.getResource("/org/netbeans/html/boot/fx/empty.html");
jaroslav@288
    99
                assertNotNull(u, "URL found");
jaroslav@288
   100
                FXBrowsers.load(App.getV1(), u, OnPages.class, "first");
jaroslav@288
   101
                
jaroslav@288
   102
            }
jaroslav@288
   103
        }
jaroslav@288
   104
        R run = new R();
jaroslav@288
   105
        Platform.runLater(run);
jaroslav@288
   106
        run.DONE.await();
jaroslav@288
   107
        for (int i = 0; i < 100; i++) {
jaroslav@288
   108
            if (run.t != null) {
jaroslav@288
   109
                throw run.t;
jaroslav@288
   110
            }
jaroslav@288
   111
            if (System.getProperty("finalSecond") == null) {
jaroslav@288
   112
                Thread.sleep(100);
jaroslav@288
   113
            }
jaroslav@288
   114
        }
jaroslav@288
   115
        
jaroslav@288
   116
        
jaroslav@288
   117
        
jaroslav@288
   118
        assertEquals(Integer.getInteger("finalFirst"), Integer.valueOf(3), "Three times in view one");
jaroslav@288
   119
        assertEquals(Integer.getInteger("finalSecond"), Integer.valueOf(2), "Two times in view one");
jaroslav@288
   120
    }
jaroslav@349
   121
jaroslav@349
   122
    @JavaScriptResource("wnd.js")
jaroslav@288
   123
    public static class OnPages {
jaroslav@288
   124
        static Class<?> first;
jaroslav@288
   125
        static Object firstWindow;
jaroslav@288
   126
        
jaroslav@288
   127
        public static void first() {
jaroslav@288
   128
            first = OnPages.class;
jaroslav@288
   129
            firstWindow = window();
jaroslav@288
   130
            assertNotNull(firstWindow, "First window found");
jaroslav@288
   131
            
jaroslav@288
   132
            assertEquals(increment(), 1, "Now it is one");
jaroslav@288
   133
            
jaroslav@362
   134
            URL u = FXBrowsersOnResourceTest.class.getResource("/org/netbeans/html/boot/fx/empty.html");
jaroslav@288
   135
            assertNotNull(u, "URL found");
jaroslav@288
   136
            FXBrowsers.load(App.getV2(), u, OnPages.class, "second", "Hello");
jaroslav@288
   137
            
jaroslav@288
   138
            assertEquals(increment(), 2, "Now it is two and not influenced by second view");
jaroslav@288
   139
            System.setProperty("finalFirst", "" + increment());
jaroslav@288
   140
        }
jaroslav@288
   141
        
jaroslav@288
   142
        public static void second(String... args) {
jaroslav@288
   143
            assertEquals(args.length, 1, "One string argument");
jaroslav@288
   144
            assertEquals(args[0], "Hello", "It is hello");
jaroslav@288
   145
            assertEquals(first, OnPages.class, "Both views share the same classloader");
jaroslav@288
   146
            
jaroslav@288
   147
            Object window = window();
jaroslav@288
   148
            assertNotNull(window, "Some window found");
jaroslav@288
   149
            assertNotNull(firstWindow, "First window is known");
jaroslav@288
   150
            assertNotSame(firstWindow, window, "The window objects should be different");
jaroslav@288
   151
            
jaroslav@288
   152
            assertEquals(increment(), 1, "Counting starts from zero");
jaroslav@288
   153
            System.setProperty("finalSecond", "" + increment());
jaroslav@288
   154
        }
jaroslav@288
   155
        
jaroslav@349
   156
        @JavaScriptBody(args = {}, body = "return wnd;")
jaroslav@288
   157
        private static native Object window();
jaroslav@288
   158
        
jaroslav@288
   159
        @JavaScriptBody(args = {}, body = ""
jaroslav@349
   160
            + "if (wnd.cnt) return ++wnd.cnt;"
jaroslav@349
   161
            + "return wnd.cnt = 1;"
jaroslav@288
   162
        )
jaroslav@288
   163
        private static native int increment();
jaroslav@288
   164
    }
jaroslav@288
   165
    
jaroslav@288
   166
    public static class App extends Application {
jaroslav@288
   167
        static final CountDownLatch CDL = new CountDownLatch(1);
jaroslav@288
   168
        private static BorderPane pane;
jaroslav@288
   169
jaroslav@288
   170
        /**
jaroslav@288
   171
         * @return the v1
jaroslav@288
   172
         */
jaroslav@288
   173
        static WebView getV1() {
jaroslav@288
   174
            return (WebView)System.getProperties().get("v1");
jaroslav@288
   175
        }
jaroslav@288
   176
jaroslav@288
   177
        /**
jaroslav@288
   178
         * @return the v2
jaroslav@288
   179
         */
jaroslav@288
   180
        static WebView getV2() {
jaroslav@288
   181
            return (WebView)System.getProperties().get("v2");
jaroslav@288
   182
        }
jaroslav@288
   183
jaroslav@288
   184
        @Override
jaroslav@288
   185
        public void start(Stage stage) throws Exception {
jaroslav@288
   186
            pane= new BorderPane();
jaroslav@288
   187
            Scene scene = new Scene(pane, 800, 600);
jaroslav@288
   188
            stage.setScene(scene);
jaroslav@288
   189
            
jaroslav@288
   190
            System.getProperties().put("v1", new WebView());
jaroslav@288
   191
            System.getProperties().put("v2", new WebView());
jaroslav@288
   192
jaroslav@288
   193
            pane.setCenter(getV1());
jaroslav@288
   194
            pane.setBottom(getV2());
jaroslav@288
   195
jaroslav@288
   196
            stage.show();
jaroslav@288
   197
            CDL.countDown();
jaroslav@288
   198
        }
jaroslav@288
   199
        
jaroslav@288
   200
        
jaroslav@288
   201
    }
jaroslav@288
   202
}