ko4j/src/test/java/org/netbeans/html/ko4j/KnockoutFXTest.java
author Jaroslav Tulach <jaroslav.tulach@netbeans.org>
Fri, 07 Feb 2014 07:44:34 +0100
changeset 551 7ca2253fa86d
parent 488 fd8f1c043b0b
child 574 7958c9c205d9
permissions -rw-r--r--
Updating copyright headers to mention current year
jaroslav@37
     1
/**
jaroslav@358
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jaroslav@37
     3
 *
jaroslav@551
     4
 * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
jaroslav@37
     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@37
     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@37
    42
 */
jaroslav@442
    43
package org.netbeans.html.ko4j;
jaroslav@37
    44
jaroslav@144
    45
import java.io.BufferedReader;
jaroslav@144
    46
import java.io.IOException;
jaroslav@144
    47
import java.io.InputStreamReader;
jaroslav@146
    48
import java.lang.annotation.Annotation;
jaroslav@146
    49
import java.lang.reflect.Method;
jaroslav@138
    50
import java.net.URI;
jaroslav@144
    51
import java.net.URISyntaxException;
jaroslav@144
    52
import java.net.URL;
jaroslav@144
    53
import java.net.URLConnection;
jaroslav@146
    54
import java.util.ArrayList;
jaroslav@146
    55
import java.util.List;
jaroslav@37
    56
import java.util.Map;
jaroslav@146
    57
import java.util.concurrent.Executors;
jaroslav@110
    58
import net.java.html.BrwsrCtx;
jaroslav@146
    59
import net.java.html.boot.BrowserBuilder;
jaroslav@134
    60
import net.java.html.js.JavaScriptBody;
jaroslav@288
    61
import org.apidesign.html.boot.spi.Fn;
jaroslav@110
    62
import org.apidesign.html.context.spi.Contexts;
jaroslav@110
    63
import org.apidesign.html.json.spi.Technology;
jaroslav@110
    64
import org.apidesign.html.json.spi.Transfer;
jaroslav@258
    65
import org.apidesign.html.json.spi.WSTransfer;
jaroslav@137
    66
import org.apidesign.html.json.tck.KOTest;
jaroslav@37
    67
import org.apidesign.html.json.tck.KnockoutTCK;
jaroslav@488
    68
import org.netbeans.html.boot.impl.FnContext;
jaroslav@37
    69
import org.openide.util.lookup.ServiceProvider;
jaroslav@488
    70
import org.testng.Assert;
jaroslav@488
    71
import static org.testng.Assert.*;
jaroslav@37
    72
import org.testng.annotations.Factory;
jaroslav@37
    73
jaroslav@37
    74
/**
jaroslav@37
    75
 *
jaroslav@37
    76
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@37
    77
 */
jaroslav@37
    78
@ServiceProvider(service = KnockoutTCK.class)
jaroslav@37
    79
public final class KnockoutFXTest extends KnockoutTCK {
jaroslav@156
    80
    private static Class<?> browserClass;
jaroslav@288
    81
    private static Fn.Presenter browserContext;
jaroslav@156
    82
    
jaroslav@37
    83
    public KnockoutFXTest() {
jaroslav@37
    84
    }
jaroslav@148
    85
    
jaroslav@146
    86
    @Factory public static Object[] compatibilityTests() throws Exception {
jaroslav@146
    87
        Class[] arr = testClasses();
jaroslav@146
    88
        for (int i = 0; i < arr.length; i++) {
jaroslav@156
    89
            assertEquals(
jaroslav@156
    90
                arr[i].getClassLoader(),
jaroslav@156
    91
                KnockoutFXTest.class.getClassLoader(),
jaroslav@156
    92
                "All classes loaded by the same classloader"
jaroslav@156
    93
            );
jaroslav@146
    94
        }
jaroslav@146
    95
        
jaroslav@148
    96
        URI uri = DynamicHTTP.initServer();
jaroslav@156
    97
    
jaroslav@146
    98
        final BrowserBuilder bb = BrowserBuilder.newBrowser().loadClass(KnockoutFXTest.class).
jaroslav@148
    99
            loadPage(uri.toString()).
jaroslav@156
   100
            invoke("initialized");
jaroslav@156
   101
        
jaroslav@146
   102
        Executors.newSingleThreadExecutor().submit(new Runnable() {
jaroslav@146
   103
            @Override
jaroslav@146
   104
            public void run() {
jaroslav@146
   105
                bb.showAndWait();
jaroslav@146
   106
            }
jaroslav@146
   107
        });
jaroslav@156
   108
        
jaroslav@156
   109
        ClassLoader l = getClassLoader();
jaroslav@146
   110
        List<Object> res = new ArrayList<Object>();
jaroslav@146
   111
        for (int i = 0; i < arr.length; i++) {
jaroslav@146
   112
            Class<?> c = Class.forName(arr[i].getName(), true, l);
jaroslav@279
   113
            seekKOTests(c, res);
jaroslav@279
   114
        }
jaroslav@279
   115
        Class<?> c = Class.forName(LessCallbacksCheck.class.getName(), true, l);
jaroslav@279
   116
        seekKOTests(c, res);
jaroslav@279
   117
        return res.toArray();
jaroslav@279
   118
    }
jaroslav@279
   119
jaroslav@279
   120
    private static void seekKOTests(Class<?> c, List<Object> res) throws SecurityException, ClassNotFoundException {
jaroslav@279
   121
        Class<? extends Annotation> koTest =
jaroslav@279
   122
            c.getClassLoader().loadClass(KOTest.class.getName()).
jaroslav@279
   123
            asSubclass(Annotation.class);
jaroslav@279
   124
        for (Method m : c.getMethods()) {
jaroslav@279
   125
            if (m.getAnnotation(koTest) != null) {
jaroslav@288
   126
                res.add(new KOFx(browserContext, m));
jaroslav@146
   127
            }
jaroslav@146
   128
        }
jaroslav@37
   129
    }
jaroslav@37
   130
jaroslav@156
   131
    static synchronized ClassLoader getClassLoader() throws InterruptedException {
jaroslav@156
   132
        while (browserClass == null) {
jaroslav@156
   133
            KnockoutFXTest.class.wait();
jaroslav@156
   134
        }
jaroslav@156
   135
        return browserClass.getClassLoader();
jaroslav@156
   136
    }
jaroslav@156
   137
    
jaroslav@156
   138
    public static synchronized void initialized(Class<?> browserCls) throws Exception {
jaroslav@156
   139
        browserClass = browserCls;
jaroslav@337
   140
        browserContext = Fn.activePresenter();
jaroslav@156
   141
        KnockoutFXTest.class.notifyAll();
jaroslav@156
   142
    }
jaroslav@156
   143
    
jaroslav@156
   144
    public static void initialized() throws Exception {
jaroslav@488
   145
        Assert.assertSame(
jaroslav@488
   146
            KnockoutFXTest.class.getClassLoader(),
jaroslav@488
   147
            ClassLoader.getSystemClassLoader(),
jaroslav@488
   148
            "No special classloaders"
jaroslav@488
   149
        );
jaroslav@488
   150
        KnockoutFXTest.initialized(KnockoutFXTest.class);
jaroslav@337
   151
        browserContext = Fn.activePresenter();
jaroslav@156
   152
    }
jaroslav@156
   153
    
jaroslav@37
   154
    @Override
jaroslav@110
   155
    public BrwsrCtx createContext() {
jaroslav@288
   156
        FXContext fx = new FXContext(browserContext);
jaroslav@258
   157
        Contexts.Builder cb = Contexts.newBuilder().
jaroslav@110
   158
            register(Technology.class, fx, 10).
jaroslav@258
   159
            register(Transfer.class, fx, 10);
jaroslav@258
   160
        if (fx.areWebSocketsSupported()) {
jaroslav@258
   161
            cb.register(WSTransfer.class, fx, 10);
jaroslav@258
   162
        }
jaroslav@258
   163
        return cb.build();
jaroslav@37
   164
    }
jaroslav@37
   165
jaroslav@37
   166
    @Override
jaroslav@37
   167
    public Object createJSON(Map<String, Object> values) {
jaroslav@446
   168
        Object json = createJSON();
jaroslav@37
   169
        for (Map.Entry<String, Object> entry : values.entrySet()) {
jaroslav@446
   170
            setProperty(json, entry.getKey(), entry.getValue());
jaroslav@37
   171
        }
jaroslav@37
   172
        return json;
jaroslav@37
   173
    }
jaroslav@446
   174
    
jaroslav@446
   175
    @JavaScriptBody(args = {}, body = "return new Object();")
jaroslav@446
   176
    private static native Object createJSON();
jaroslav@446
   177
    @JavaScriptBody(args = { "json", "key", "value" }, body = "json[key] = value;")
jaroslav@446
   178
    private static native void setProperty(Object json, String key, Object value);
jaroslav@37
   179
jaroslav@37
   180
    @Override
jaroslav@122
   181
    @JavaScriptBody(args = { "s", "args" }, body = ""
jaroslav@122
   182
        + "var f = new Function(s); "
jaroslav@122
   183
        + "return f.apply(null, args);"
jaroslav@122
   184
    )
jaroslav@122
   185
    public native Object executeScript(String script, Object[] arguments);
jaroslav@138
   186
jaroslav@144
   187
    @JavaScriptBody(args = {  }, body = 
jaroslav@144
   188
          "var h;"
jaroslav@144
   189
        + "if (!!window && !!window.location && !!window.location.href)\n"
jaroslav@144
   190
        + "  h = window.location.href;\n"
jaroslav@144
   191
        + "else "
jaroslav@144
   192
        + "  h = null;"
jaroslav@144
   193
        + "return h;\n"
jaroslav@144
   194
    )
jaroslav@144
   195
    private static native String findBaseURL();
jaroslav@144
   196
    
jaroslav@138
   197
    @Override
jaroslav@138
   198
    public URI prepareURL(String content, String mimeType, String[] parameters) {
jaroslav@138
   199
        try {
jaroslav@144
   200
            final URL baseURL = new URL(findBaseURL());
jaroslav@144
   201
            StringBuilder sb = new StringBuilder();
jaroslav@144
   202
            sb.append("/dynamic?mimeType=").append(mimeType);
jaroslav@144
   203
            for (int i = 0; i < parameters.length; i++) {
jaroslav@144
   204
                sb.append("&param" + i).append("=").append(parameters[i]);
jaroslav@144
   205
            }
jaroslav@144
   206
            String mangle = content.replace("\n", "%0a")
jaroslav@144
   207
                .replace("\"", "\\\"").replace(" ", "%20");
jaroslav@144
   208
            sb.append("&content=").append(mangle);
jaroslav@144
   209
jaroslav@144
   210
            URL query = new URL(baseURL, sb.toString());
jaroslav@144
   211
            URLConnection c = query.openConnection();
jaroslav@144
   212
            BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
jaroslav@144
   213
            URI connectTo = new URI(br.readLine());
jaroslav@144
   214
            return connectTo;
jaroslav@144
   215
        } catch (IOException ex) {
jaroslav@144
   216
            throw new IllegalStateException(ex);
jaroslav@144
   217
        } catch (URISyntaxException ex) {
jaroslav@138
   218
            throw new IllegalStateException(ex);
jaroslav@138
   219
        }
jaroslav@138
   220
    }
jaroslav@260
   221
jaroslav@260
   222
    @Override
jaroslav@260
   223
    public boolean canFailWebSocketTest() {
jaroslav@260
   224
        try {
jaroslav@260
   225
            Class.forName("java.util.function.Function");
jaroslav@260
   226
            return false;
jaroslav@260
   227
        } catch (ClassNotFoundException ex) {
jaroslav@260
   228
            // running on JDK7, FX WebView WebSocket impl does not work
jaroslav@260
   229
            return true;
jaroslav@260
   230
        }
jaroslav@260
   231
    }
jaroslav@37
   232
}