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