ko/bck2brwsr/src/test/java/org/apidesign/html/ko2brwsr/Bck2BrwsrKnockoutTest.java
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/ko/bck2brwsr/src/test/java/org/apidesign/html/ko2brwsr/Bck2BrwsrKnockoutTest.java Mon Jun 24 17:50:44 2013 +0200
1.3 @@ -0,0 +1,124 @@
1.4 +/**
1.5 + * HTML via Java(tm) Language Bindings
1.6 + * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
1.7 + *
1.8 + * This program is free software: you can redistribute it and/or modify
1.9 + * it under the terms of the GNU General Public License as published by
1.10 + * the Free Software Foundation, version 2 of the License.
1.11 + *
1.12 + * This program is distributed in the hope that it will be useful,
1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.15 + * GNU General Public License for more details. apidesign.org
1.16 + * designates this particular file as subject to the
1.17 + * "Classpath" exception as provided by apidesign.org
1.18 + * in the License file that accompanied this code.
1.19 + *
1.20 + * You should have received a copy of the GNU General Public License
1.21 + * along with this program. Look for COPYING file in the top folder.
1.22 + * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
1.23 + */
1.24 +package org.apidesign.html.ko2brwsr;
1.25 +
1.26 +import java.io.BufferedReader;
1.27 +import java.io.IOException;
1.28 +import java.io.InputStreamReader;
1.29 +import java.net.URI;
1.30 +import java.net.URISyntaxException;
1.31 +import java.net.URL;
1.32 +import java.net.URLConnection;
1.33 +import java.util.Map;
1.34 +import net.java.html.BrwsrCtx;
1.35 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
1.36 +import org.apidesign.bck2brwsr.vmtest.VMTest;
1.37 +import org.apidesign.html.context.spi.Contexts;
1.38 +import org.apidesign.html.json.spi.Technology;
1.39 +import org.apidesign.html.json.spi.Transfer;
1.40 +import org.apidesign.html.json.tck.KOTest;
1.41 +import org.apidesign.html.json.tck.KnockoutTCK;
1.42 +import org.openide.util.lookup.ServiceProvider;
1.43 +import org.testng.annotations.Factory;
1.44 +
1.45 +/**
1.46 + *
1.47 + * @author Jaroslav Tulach <jtulach@netbeans.org>
1.48 + */
1.49 +@ServiceProvider(service = KnockoutTCK.class)
1.50 +public final class Bck2BrwsrKnockoutTest extends KnockoutTCK {
1.51 + @Factory public static Object[] create() {
1.52 + return VMTest.newTests().
1.53 + withClasses(testClasses()).
1.54 + withLaunchers("bck2brwsr").
1.55 + withTestAnnotation(KOTest.class).
1.56 + build();
1.57 + }
1.58 +
1.59 + @Override
1.60 + public BrwsrCtx createContext() {
1.61 + return Contexts.newBuilder().
1.62 + register(Transfer.class, BrwsrCtxImpl.DEFAULT, 9).
1.63 + register(Technology.class, BrwsrCtxImpl.DEFAULT, 9).build();
1.64 + }
1.65 +
1.66 +
1.67 +
1.68 + @Override
1.69 + public Object createJSON(Map<String, Object> values) {
1.70 + Object json = createJSON();
1.71 +
1.72 + for (Map.Entry<String, Object> entry : values.entrySet()) {
1.73 + putValue(json, entry.getKey(), entry.getValue());
1.74 + }
1.75 + return json;
1.76 + }
1.77 +
1.78 + @JavaScriptBody(args = {}, body = "return new Object();")
1.79 + private static native Object createJSON();
1.80 +
1.81 + @JavaScriptBody(args = { "json", "key", "value" }, body = "json[key] = value;")
1.82 + private static native void putValue(Object json, String key, Object value);
1.83 +
1.84 + @Override
1.85 + public Object executeScript(String script, Object[] arguments) {
1.86 + return execScript(script, arguments);
1.87 + }
1.88 +
1.89 + @JavaScriptBody(args = { "s", "args" }, body =
1.90 + "var f = new Function(s); return f.apply(null, args);"
1.91 + )
1.92 + private static native Object execScript(String s, Object[] arguments);
1.93 +
1.94 + @JavaScriptBody(args = { }, body =
1.95 + "var h;"
1.96 + + "if (!!window && !!window.location && !!window.location.href)\n"
1.97 + + " h = window.location.href;\n"
1.98 + + "else "
1.99 + + " h = null;"
1.100 + + "return h;\n"
1.101 + )
1.102 + private static native String findBaseURL();
1.103 +
1.104 + @Override
1.105 + public URI prepareURL(String content, String mimeType, String[] parameters) {
1.106 + try {
1.107 + final URL baseURL = new URL(findBaseURL());
1.108 + StringBuilder sb = new StringBuilder();
1.109 + sb.append("/dynamic?mimeType=").append(mimeType);
1.110 + for (int i = 0; i < parameters.length; i++) {
1.111 + sb.append("¶m" + i).append("=").append(parameters[i]);
1.112 + }
1.113 + String mangle = content.replace("\n", "%0a")
1.114 + .replace("\"", "\\\"").replace(" ", "%20");
1.115 + sb.append("&content=").append(mangle);
1.116 +
1.117 + URL query = new URL(baseURL, sb.toString());
1.118 + String uri = (String) query.getContent(new Class[] { String.class });
1.119 + URI connectTo = new URI(uri.trim());
1.120 + return connectTo;
1.121 + } catch (IOException ex) {
1.122 + throw new IllegalStateException(ex);
1.123 + } catch (URISyntaxException ex) {
1.124 + throw new IllegalStateException(ex);
1.125 + }
1.126 + }
1.127 +}