1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/ko-fx/src/test/java/org/netbeans/html/kofx/KnockoutFXTest.java Mon Dec 16 16:59:43 2013 +0100
1.3 @@ -0,0 +1,229 @@
1.4 +/**
1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
1.6 + *
1.7 + * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
1.8 + *
1.9 + * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
1.10 + * Other names may be trademarks of their respective owners.
1.11 + *
1.12 + * The contents of this file are subject to the terms of either the GNU
1.13 + * General Public License Version 2 only ("GPL") or the Common
1.14 + * Development and Distribution License("CDDL") (collectively, the
1.15 + * "License"). You may not use this file except in compliance with the
1.16 + * License. You can obtain a copy of the License at
1.17 + * http://www.netbeans.org/cddl-gplv2.html
1.18 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
1.19 + * specific language governing permissions and limitations under the
1.20 + * License. When distributing the software, include this License Header
1.21 + * Notice in each file and include the License file at
1.22 + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
1.23 + * particular file as subject to the "Classpath" exception as provided
1.24 + * by Oracle in the GPL Version 2 section of the License file that
1.25 + * accompanied this code. If applicable, add the following below the
1.26 + * License Header, with the fields enclosed by brackets [] replaced by
1.27 + * your own identifying information:
1.28 + * "Portions Copyrighted [year] [name of copyright owner]"
1.29 + *
1.30 + * Contributor(s):
1.31 + *
1.32 + * The Original Software is NetBeans. The Initial Developer of the Original
1.33 + * Software is Oracle. Portions Copyright 2013-2013 Oracle. All Rights Reserved.
1.34 + *
1.35 + * If you wish your version of this file to be governed by only the CDDL
1.36 + * or only the GPL Version 2, indicate your decision by adding
1.37 + * "[Contributor] elects to include this software in this distribution
1.38 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
1.39 + * single choice of license, a recipient has the option to distribute
1.40 + * your version of this file under either the CDDL, the GPL Version 2 or
1.41 + * to extend the choice of license to its licensees as provided above.
1.42 + * However, if you add GPL Version 2 code and therefore, elected the GPL
1.43 + * Version 2 license, then the option applies only if the new code is
1.44 + * made subject to such option by the copyright holder.
1.45 + */
1.46 +package org.netbeans.html.kofx;
1.47 +
1.48 +import java.io.BufferedReader;
1.49 +import java.io.IOException;
1.50 +import java.io.InputStreamReader;
1.51 +import java.lang.annotation.Annotation;
1.52 +import java.lang.reflect.Method;
1.53 +import java.net.URI;
1.54 +import java.net.URISyntaxException;
1.55 +import java.net.URL;
1.56 +import java.net.URLConnection;
1.57 +import java.util.ArrayList;
1.58 +import java.util.List;
1.59 +import java.util.Map;
1.60 +import java.util.concurrent.Executors;
1.61 +import net.java.html.BrwsrCtx;
1.62 +import net.java.html.boot.BrowserBuilder;
1.63 +import net.java.html.js.JavaScriptBody;
1.64 +import org.netbeans.html.boot.impl.FnContext;
1.65 +import org.apidesign.html.boot.spi.Fn;
1.66 +import org.apidesign.html.context.spi.Contexts;
1.67 +import org.apidesign.html.json.spi.Technology;
1.68 +import org.apidesign.html.json.spi.Transfer;
1.69 +import org.apidesign.html.json.spi.WSTransfer;
1.70 +import org.apidesign.html.json.tck.KOTest;
1.71 +import org.apidesign.html.json.tck.KnockoutTCK;
1.72 +import org.json.JSONException;
1.73 +import org.json.JSONObject;
1.74 +import org.openide.util.lookup.ServiceProvider;
1.75 +import org.testng.annotations.Factory;
1.76 +import static org.testng.Assert.*;
1.77 +
1.78 +/**
1.79 + *
1.80 + * @author Jaroslav Tulach <jtulach@netbeans.org>
1.81 + */
1.82 +@ServiceProvider(service = KnockoutTCK.class)
1.83 +public final class KnockoutFXTest extends KnockoutTCK {
1.84 + private static Class<?> browserClass;
1.85 + private static Fn.Presenter browserContext;
1.86 +
1.87 + public KnockoutFXTest() {
1.88 + }
1.89 +
1.90 + @Factory public static Object[] compatibilityTests() throws Exception {
1.91 + Class[] arr = testClasses();
1.92 + for (int i = 0; i < arr.length; i++) {
1.93 + assertEquals(
1.94 + arr[i].getClassLoader(),
1.95 + KnockoutFXTest.class.getClassLoader(),
1.96 + "All classes loaded by the same classloader"
1.97 + );
1.98 + }
1.99 +
1.100 + URI uri = DynamicHTTP.initServer();
1.101 +
1.102 + final BrowserBuilder bb = BrowserBuilder.newBrowser().loadClass(KnockoutFXTest.class).
1.103 + loadPage(uri.toString()).
1.104 + invoke("initialized");
1.105 +
1.106 + Executors.newSingleThreadExecutor().submit(new Runnable() {
1.107 + @Override
1.108 + public void run() {
1.109 + bb.showAndWait();
1.110 + }
1.111 + });
1.112 +
1.113 + ClassLoader l = getClassLoader();
1.114 + List<Object> res = new ArrayList<Object>();
1.115 + for (int i = 0; i < arr.length; i++) {
1.116 + Class<?> c = Class.forName(arr[i].getName(), true, l);
1.117 + seekKOTests(c, res);
1.118 + }
1.119 + Class<?> c = Class.forName(LessCallbacksCheck.class.getName(), true, l);
1.120 + seekKOTests(c, res);
1.121 + return res.toArray();
1.122 + }
1.123 +
1.124 + private static void seekKOTests(Class<?> c, List<Object> res) throws SecurityException, ClassNotFoundException {
1.125 + Class<? extends Annotation> koTest =
1.126 + c.getClassLoader().loadClass(KOTest.class.getName()).
1.127 + asSubclass(Annotation.class);
1.128 + for (Method m : c.getMethods()) {
1.129 + if (m.getAnnotation(koTest) != null) {
1.130 + res.add(new KOFx(browserContext, m));
1.131 + }
1.132 + }
1.133 + }
1.134 +
1.135 + static synchronized ClassLoader getClassLoader() throws InterruptedException {
1.136 + while (browserClass == null) {
1.137 + KnockoutFXTest.class.wait();
1.138 + }
1.139 + return browserClass.getClassLoader();
1.140 + }
1.141 +
1.142 + public static synchronized void initialized(Class<?> browserCls) throws Exception {
1.143 + browserClass = browserCls;
1.144 + browserContext = FnContext.currentPresenter();
1.145 + KnockoutFXTest.class.notifyAll();
1.146 + }
1.147 +
1.148 + public static void initialized() throws Exception {
1.149 + Class<?> classpathClass = ClassLoader.getSystemClassLoader().loadClass(KnockoutFXTest.class.getName());
1.150 + Method m = classpathClass.getMethod("initialized", Class.class);
1.151 + m.invoke(null, KnockoutFXTest.class);
1.152 + browserContext = FnContext.currentPresenter();
1.153 + }
1.154 +
1.155 + @Override
1.156 + public BrwsrCtx createContext() {
1.157 + FXContext fx = new FXContext(browserContext);
1.158 + Contexts.Builder cb = Contexts.newBuilder().
1.159 + register(Technology.class, fx, 10).
1.160 + register(Transfer.class, fx, 10);
1.161 + if (fx.areWebSocketsSupported()) {
1.162 + cb.register(WSTransfer.class, fx, 10);
1.163 + }
1.164 + return cb.build();
1.165 + }
1.166 +
1.167 + @Override
1.168 + public Object createJSON(Map<String, Object> values) {
1.169 + JSONObject json = new JSONObject();
1.170 + for (Map.Entry<String, Object> entry : values.entrySet()) {
1.171 + try {
1.172 + json.put(entry.getKey(), entry.getValue());
1.173 + } catch (JSONException ex) {
1.174 + throw new IllegalStateException(ex);
1.175 + }
1.176 + }
1.177 + return json;
1.178 + }
1.179 +
1.180 + @Override
1.181 + @JavaScriptBody(args = { "s", "args" }, body = ""
1.182 + + "var f = new Function(s); "
1.183 + + "return f.apply(null, args);"
1.184 + )
1.185 + public native Object executeScript(String script, Object[] arguments);
1.186 +
1.187 + @JavaScriptBody(args = { }, body =
1.188 + "var h;"
1.189 + + "if (!!window && !!window.location && !!window.location.href)\n"
1.190 + + " h = window.location.href;\n"
1.191 + + "else "
1.192 + + " h = null;"
1.193 + + "return h;\n"
1.194 + )
1.195 + private static native String findBaseURL();
1.196 +
1.197 + @Override
1.198 + public URI prepareURL(String content, String mimeType, String[] parameters) {
1.199 + try {
1.200 + final URL baseURL = new URL(findBaseURL());
1.201 + StringBuilder sb = new StringBuilder();
1.202 + sb.append("/dynamic?mimeType=").append(mimeType);
1.203 + for (int i = 0; i < parameters.length; i++) {
1.204 + sb.append("¶m" + i).append("=").append(parameters[i]);
1.205 + }
1.206 + String mangle = content.replace("\n", "%0a")
1.207 + .replace("\"", "\\\"").replace(" ", "%20");
1.208 + sb.append("&content=").append(mangle);
1.209 +
1.210 + URL query = new URL(baseURL, sb.toString());
1.211 + URLConnection c = query.openConnection();
1.212 + BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
1.213 + URI connectTo = new URI(br.readLine());
1.214 + return connectTo;
1.215 + } catch (IOException ex) {
1.216 + throw new IllegalStateException(ex);
1.217 + } catch (URISyntaxException ex) {
1.218 + throw new IllegalStateException(ex);
1.219 + }
1.220 + }
1.221 +
1.222 + @Override
1.223 + public boolean canFailWebSocketTest() {
1.224 + try {
1.225 + Class.forName("java.util.function.Function");
1.226 + return false;
1.227 + } catch (ClassNotFoundException ex) {
1.228 + // running on JDK7, FX WebView WebSocket impl does not work
1.229 + return true;
1.230 + }
1.231 + }
1.232 +}