boot-fx/src/test/java/org/netbeans/html/boot/fx/ReloadTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 02 Aug 2014 12:59:31 +0200
changeset 790 30f20d9c0986
parent 736 8e6e4f811215
child 838 bdc3d696dd4a
permissions -rw-r--r--
Fixing Javadoc to succeed on JDK8
jtulach@736
     1
/**
jtulach@736
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jtulach@736
     3
 *
jtulach@736
     4
 * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
jtulach@736
     5
 *
jtulach@736
     6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
jtulach@736
     7
 * Other names may be trademarks of their respective owners.
jtulach@736
     8
 *
jtulach@736
     9
 * The contents of this file are subject to the terms of either the GNU
jtulach@736
    10
 * General Public License Version 2 only ("GPL") or the Common
jtulach@736
    11
 * Development and Distribution License("CDDL") (collectively, the
jtulach@736
    12
 * "License"). You may not use this file except in compliance with the
jtulach@736
    13
 * License. You can obtain a copy of the License at
jtulach@736
    14
 * http://www.netbeans.org/cddl-gplv2.html
jtulach@736
    15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jtulach@736
    16
 * specific language governing permissions and limitations under the
jtulach@736
    17
 * License.  When distributing the software, include this License Header
jtulach@736
    18
 * Notice in each file and include the License file at
jtulach@736
    19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
jtulach@736
    20
 * particular file as subject to the "Classpath" exception as provided
jtulach@736
    21
 * by Oracle in the GPL Version 2 section of the License file that
jtulach@736
    22
 * accompanied this code. If applicable, add the following below the
jtulach@736
    23
 * License Header, with the fields enclosed by brackets [] replaced by
jtulach@736
    24
 * your own identifying information:
jtulach@736
    25
 * "Portions Copyrighted [year] [name of copyright owner]"
jtulach@736
    26
 *
jtulach@736
    27
 * Contributor(s):
jtulach@736
    28
 *
jtulach@736
    29
 * The Original Software is NetBeans. The Initial Developer of the Original
jtulach@736
    30
 * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
jtulach@736
    31
 *
jtulach@736
    32
 * If you wish your version of this file to be governed by only the CDDL
jtulach@736
    33
 * or only the GPL Version 2, indicate your decision by adding
jtulach@736
    34
 * "[Contributor] elects to include this software in this distribution
jtulach@736
    35
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
jtulach@736
    36
 * single choice of license, a recipient has the option to distribute
jtulach@736
    37
 * your version of this file under either the CDDL, the GPL Version 2 or
jtulach@736
    38
 * to extend the choice of license to its licensees as provided above.
jtulach@736
    39
 * However, if you add GPL Version 2 code and therefore, elected the GPL
jtulach@736
    40
 * Version 2 license, then the option applies only if the new code is
jtulach@736
    41
 * made subject to such option by the copyright holder.
jtulach@736
    42
 */
jtulach@736
    43
package org.netbeans.html.boot.fx;
jtulach@736
    44
jtulach@736
    45
import java.util.concurrent.CountDownLatch;
jtulach@736
    46
import java.util.concurrent.Executors;
jtulach@736
    47
import javafx.application.Platform;
jtulach@736
    48
import net.java.html.BrwsrCtx;
jtulach@736
    49
import net.java.html.boot.BrowserBuilder;
jtulach@736
    50
import net.java.html.js.JavaScriptBody;
jtulach@736
    51
import org.apidesign.html.boot.spi.Fn;
jtulach@736
    52
import static org.testng.Assert.*;
jtulach@736
    53
import org.testng.annotations.Test;
jtulach@736
    54
jtulach@736
    55
/**
jtulach@736
    56
 *
jtulach@790
    57
 * @author Jaroslav Tulach
jtulach@736
    58
 */
jtulach@736
    59
public class ReloadTest {
jtulach@736
    60
    private static Runnable whenInitialized;
jtulach@736
    61
    
jtulach@736
    62
    public ReloadTest() {
jtulach@736
    63
    }
jtulach@736
    64
jtulach@736
    65
    @JavaScriptBody(args = { "a", "b"  }, body = "return a + b;")
jtulach@736
    66
    private static native int plus(int a, int b);
jtulach@736
    67
    
jtulach@736
    68
    @Test public void checkReload() throws Throwable {
jtulach@736
    69
        final Throwable[] arr = { null };
jtulach@736
    70
        
jtulach@736
    71
        final BrowserBuilder bb = BrowserBuilder.newBrowser().loadClass(ReloadTest.class).
jtulach@736
    72
                loadPage("empty.html").
jtulach@736
    73
                invoke("initialized");
jtulach@736
    74
        
jtulach@736
    75
        class ShowBrowser implements Runnable {
jtulach@736
    76
            @Override
jtulach@736
    77
            public void run() {
jtulach@736
    78
                bb.showAndWait();
jtulach@736
    79
            }
jtulach@736
    80
        }
jtulach@736
    81
        
jtulach@736
    82
        class WhenInitialized implements Runnable {
jtulach@736
    83
            CountDownLatch cdl = new CountDownLatch(1);
jtulach@736
    84
            AbstractFXPresenter p;
jtulach@736
    85
            BrwsrCtx ctx;
jtulach@736
    86
            
jtulach@736
    87
            @Override
jtulach@736
    88
            public void run() {
jtulach@736
    89
                try {
jtulach@736
    90
                    whenInitialized = null;
jtulach@736
    91
                    doCheckReload();
jtulach@736
    92
                    p = (AbstractFXPresenter) Fn.activePresenter();
jtulach@736
    93
                    assertNotNull(p, "Presenter is defined");
jtulach@736
    94
                    ctx = BrwsrCtx.findDefault(WhenInitialized.class);
jtulach@736
    95
                } catch (Throwable ex) {
jtulach@736
    96
                    arr[0] = ex;
jtulach@736
    97
                } finally {
jtulach@736
    98
                    cdl.countDown();
jtulach@736
    99
                }
jtulach@736
   100
            }
jtulach@736
   101
        }
jtulach@736
   102
        WhenInitialized when = new WhenInitialized();
jtulach@736
   103
        whenInitialized = when;
jtulach@736
   104
        Executors.newSingleThreadExecutor().submit(new ShowBrowser());
jtulach@736
   105
        when.cdl.await();
jtulach@736
   106
        if (arr[0] != null) throw arr[0];
jtulach@736
   107
        
jtulach@736
   108
        class ReloadPage implements Runnable {
jtulach@736
   109
            final CountDownLatch cdl = new CountDownLatch(1);
jtulach@736
   110
            private final AbstractFXPresenter p;
jtulach@736
   111
jtulach@736
   112
            public ReloadPage(AbstractFXPresenter p) {
jtulach@736
   113
                this.p = p;
jtulach@736
   114
            }
jtulach@736
   115
jtulach@736
   116
            @Override
jtulach@736
   117
            public void run() {
jtulach@736
   118
                p.engine.reload();
jtulach@736
   119
                cdl.countDown();
jtulach@736
   120
            }
jtulach@736
   121
        }
jtulach@736
   122
        ReloadPage relPage = new ReloadPage(when.p);
jtulach@736
   123
        
jtulach@736
   124
        class SecondInit implements Runnable {
jtulach@736
   125
            CountDownLatch cdl = new CountDownLatch(1);
jtulach@736
   126
            
jtulach@736
   127
            @Override
jtulach@736
   128
            public void run() {
jtulach@736
   129
                try {
jtulach@736
   130
                    whenInitialized = null;
jtulach@736
   131
                    doCheckReload();
jtulach@736
   132
                } catch (Throwable ex) {
jtulach@736
   133
                    arr[0] = ex;
jtulach@736
   134
                } finally {
jtulach@736
   135
                    cdl.countDown();
jtulach@736
   136
                }
jtulach@736
   137
                
jtulach@736
   138
            }
jtulach@736
   139
        }
jtulach@736
   140
        SecondInit second = new SecondInit();
jtulach@736
   141
        whenInitialized = second;
jtulach@736
   142
        
jtulach@736
   143
        Platform.runLater(relPage);
jtulach@736
   144
        
jtulach@736
   145
        second.cdl.await();
jtulach@736
   146
        if (arr[0] != null) throw arr[0];
jtulach@736
   147
    }
jtulach@736
   148
    
jtulach@736
   149
    final void doCheckReload() throws Exception {
jtulach@736
   150
        int res = plus(30, 12);
jtulach@736
   151
        assertEquals(res, 42, "Meaning of world computed");
jtulach@736
   152
    }
jtulach@736
   153
    
jtulach@736
   154
    public static synchronized void initialized() throws Exception {
jtulach@736
   155
        whenInitialized.run();
jtulach@736
   156
    }
jtulach@736
   157
}