boot-fx/src/test/java/net/java/html/boot/fx/FXBrowsersTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 16 Dec 2013 16:59:43 +0100
branchnetbeans
changeset 362 92fb71afdc0e
parent 358 80702021b851
child 365 5c93ad8c7a15
permissions -rw-r--r--
Moving implementation classes into org.netbeans.html namespace
jaroslav@288
     1
/**
jaroslav@358
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jaroslav@288
     3
 *
jaroslav@358
     4
 * Copyright 1997-2010 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@358
    30
 * Software is Oracle. Portions Copyright 2013-2013 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@288
    54
import static org.testng.Assert.assertEquals;
jaroslav@288
    55
import static org.testng.Assert.assertNotEquals;
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@288
    65
public class FXBrowsersTest {
jaroslav@288
    66
    
jaroslav@288
    67
    public FXBrowsersTest() {
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 = FXBrowsersTest.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@288
   121
    
jaroslav@288
   122
    public static class OnPages {
jaroslav@288
   123
        static Class<?> first;
jaroslav@288
   124
        static Object firstWindow;
jaroslav@288
   125
        
jaroslav@288
   126
        public static void first() {
jaroslav@288
   127
            first = OnPages.class;
jaroslav@288
   128
            firstWindow = window();
jaroslav@288
   129
            assertNotNull(firstWindow, "First window found");
jaroslav@288
   130
            
jaroslav@288
   131
            assertEquals(increment(), 1, "Now it is one");
jaroslav@288
   132
            
jaroslav@362
   133
            URL u = FXBrowsersTest.class.getResource("/org/netbeans/html/boot/fx/empty.html");
jaroslav@288
   134
            assertNotNull(u, "URL found");
jaroslav@288
   135
            FXBrowsers.load(App.getV2(), u, OnPages.class, "second", "Hello");
jaroslav@288
   136
            
jaroslav@288
   137
            assertEquals(increment(), 2, "Now it is two and not influenced by second view");
jaroslav@288
   138
            System.setProperty("finalFirst", "" + increment());
jaroslav@288
   139
        }
jaroslav@288
   140
        
jaroslav@288
   141
        public static void second(String... args) {
jaroslav@288
   142
            assertEquals(args.length, 1, "One string argument");
jaroslav@288
   143
            assertEquals(args[0], "Hello", "It is hello");
jaroslav@288
   144
            assertEquals(first, OnPages.class, "Both views share the same classloader");
jaroslav@288
   145
            
jaroslav@288
   146
            Object window = window();
jaroslav@288
   147
            assertNotNull(window, "Some window found");
jaroslav@288
   148
            assertNotNull(firstWindow, "First window is known");
jaroslav@288
   149
            assertNotSame(firstWindow, window, "The window objects should be different");
jaroslav@288
   150
            
jaroslav@288
   151
            assertEquals(increment(), 1, "Counting starts from zero");
jaroslav@288
   152
            System.setProperty("finalSecond", "" + increment());
jaroslav@288
   153
        }
jaroslav@288
   154
        
jaroslav@288
   155
        @JavaScriptBody(args = {}, body = "return window;")
jaroslav@288
   156
        private static native Object window();
jaroslav@288
   157
        
jaroslav@288
   158
        @JavaScriptBody(args = {}, body = ""
jaroslav@288
   159
            + "if (window.cnt) return ++window.cnt;"
jaroslav@288
   160
            + "return window.cnt = 1;"
jaroslav@288
   161
        )
jaroslav@288
   162
        private static native int increment();
jaroslav@288
   163
    }
jaroslav@288
   164
    
jaroslav@288
   165
    public static class App extends Application {
jaroslav@288
   166
        static final CountDownLatch CDL = new CountDownLatch(1);
jaroslav@288
   167
        private static BorderPane pane;
jaroslav@288
   168
jaroslav@288
   169
        /**
jaroslav@288
   170
         * @return the v1
jaroslav@288
   171
         */
jaroslav@288
   172
        static WebView getV1() {
jaroslav@288
   173
            return (WebView)System.getProperties().get("v1");
jaroslav@288
   174
        }
jaroslav@288
   175
jaroslav@288
   176
        /**
jaroslav@288
   177
         * @return the v2
jaroslav@288
   178
         */
jaroslav@288
   179
        static WebView getV2() {
jaroslav@288
   180
            return (WebView)System.getProperties().get("v2");
jaroslav@288
   181
        }
jaroslav@288
   182
jaroslav@288
   183
        @Override
jaroslav@288
   184
        public void start(Stage stage) throws Exception {
jaroslav@288
   185
            pane= new BorderPane();
jaroslav@288
   186
            Scene scene = new Scene(pane, 800, 600);
jaroslav@288
   187
            stage.setScene(scene);
jaroslav@288
   188
            
jaroslav@288
   189
            System.getProperties().put("v1", new WebView());
jaroslav@288
   190
            System.getProperties().put("v2", new WebView());
jaroslav@288
   191
jaroslav@288
   192
            pane.setCenter(getV1());
jaroslav@288
   193
            pane.setBottom(getV2());
jaroslav@288
   194
jaroslav@288
   195
            stage.show();
jaroslav@288
   196
            CDL.countDown();
jaroslav@288
   197
        }
jaroslav@288
   198
        
jaroslav@288
   199
        
jaroslav@288
   200
    }
jaroslav@288
   201
}