ko/bck2brwsr/src/test/java/org/apidesign/bck2brwsr/ko2brwsr/Bck2BrwsrKnockoutTest.java
branchclassloader
changeset 1228 462dd9238173
parent 1227 5a907f38608d
child 1246 156c4911e45b
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/ko/bck2brwsr/src/test/java/org/apidesign/bck2brwsr/ko2brwsr/Bck2BrwsrKnockoutTest.java	Wed Jun 26 19:29:54 2013 +0200
     1.3 @@ -0,0 +1,122 @@
     1.4 +/**
     1.5 + * Back 2 Browser Bytecode Translator
     1.6 + * Copyright (C) 2012 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.
    1.16 + *
    1.17 + * You should have received a copy of the GNU General Public License
    1.18 + * along with this program. Look for COPYING file in the top folder.
    1.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    1.20 + */
    1.21 +package org.apidesign.bck2brwsr.ko2brwsr;
    1.22 +
    1.23 +import org.apidesign.bck2brwsr.ko2brwsr.BrwsrCtxImpl;
    1.24 +import java.io.BufferedReader;
    1.25 +import java.io.IOException;
    1.26 +import java.io.InputStreamReader;
    1.27 +import java.net.URI;
    1.28 +import java.net.URISyntaxException;
    1.29 +import java.net.URL;
    1.30 +import java.net.URLConnection;
    1.31 +import java.util.Map;
    1.32 +import net.java.html.BrwsrCtx;
    1.33 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
    1.34 +import org.apidesign.bck2brwsr.vmtest.VMTest;
    1.35 +import org.apidesign.html.context.spi.Contexts;
    1.36 +import org.apidesign.html.json.spi.Technology;
    1.37 +import org.apidesign.html.json.spi.Transfer;
    1.38 +import org.apidesign.html.json.tck.KOTest;
    1.39 +import org.apidesign.html.json.tck.KnockoutTCK;
    1.40 +import org.openide.util.lookup.ServiceProvider;
    1.41 +import org.testng.annotations.Factory;
    1.42 +
    1.43 +/**
    1.44 + *
    1.45 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.46 + */
    1.47 +@ServiceProvider(service = KnockoutTCK.class)
    1.48 +public final class Bck2BrwsrKnockoutTest extends KnockoutTCK {
    1.49 +    @Factory public static Object[] create() {
    1.50 +        return VMTest.newTests().
    1.51 +            withClasses(testClasses()).
    1.52 +            withLaunchers("bck2brwsr").
    1.53 +            withTestAnnotation(KOTest.class).
    1.54 +            build();
    1.55 +    }
    1.56 +    
    1.57 +    @Override
    1.58 +    public BrwsrCtx createContext() {
    1.59 +        return Contexts.newBuilder().
    1.60 +            register(Transfer.class, BrwsrCtxImpl.DEFAULT, 9).
    1.61 +            register(Technology.class, BrwsrCtxImpl.DEFAULT, 9).build();
    1.62 +    }
    1.63 +
    1.64 +
    1.65 +    
    1.66 +    @Override
    1.67 +    public Object createJSON(Map<String, Object> values) {
    1.68 +        Object json = createJSON();
    1.69 +        
    1.70 +        for (Map.Entry<String, Object> entry : values.entrySet()) {
    1.71 +            putValue(json, entry.getKey(), entry.getValue());
    1.72 +        }
    1.73 +        return json;
    1.74 +    }
    1.75 +
    1.76 +    @JavaScriptBody(args = {}, body = "return new Object();")
    1.77 +    private static native Object createJSON();
    1.78 +
    1.79 +    @JavaScriptBody(args = { "json", "key", "value" }, body = "json[key] = value;")
    1.80 +    private static native void putValue(Object json, String key, Object value);
    1.81 +
    1.82 +    @Override
    1.83 +    public Object executeScript(String script, Object[] arguments) {
    1.84 +        return execScript(script, arguments);
    1.85 +    }
    1.86 +    
    1.87 +    @JavaScriptBody(args = { "s", "args" }, body = 
    1.88 +        "var f = new Function(s); return f.apply(null, args);"
    1.89 +    )
    1.90 +    private static native Object execScript(String s, Object[] arguments);
    1.91 +    
    1.92 +    @JavaScriptBody(args = {  }, body = 
    1.93 +          "var h;"
    1.94 +        + "if (!!window && !!window.location && !!window.location.href)\n"
    1.95 +        + "  h = window.location.href;\n"
    1.96 +        + "else "
    1.97 +        + "  h = null;"
    1.98 +        + "return h;\n"
    1.99 +    )
   1.100 +    private static native String findBaseURL();
   1.101 +    
   1.102 +    @Override
   1.103 +    public URI prepareURL(String content, String mimeType, String[] parameters) {
   1.104 +        try {
   1.105 +            final URL baseURL = new URL(findBaseURL());
   1.106 +            StringBuilder sb = new StringBuilder();
   1.107 +            sb.append("/dynamic?mimeType=").append(mimeType);
   1.108 +            for (int i = 0; i < parameters.length; i++) {
   1.109 +                sb.append("&param" + i).append("=").append(parameters[i]);
   1.110 +            }
   1.111 +            String mangle = content.replace("\n", "%0a")
   1.112 +                .replace("\"", "\\\"").replace(" ", "%20");
   1.113 +            sb.append("&content=").append(mangle);
   1.114 +
   1.115 +            URL query = new URL(baseURL, sb.toString());
   1.116 +            String uri = (String) query.getContent(new Class[] { String.class });
   1.117 +            URI connectTo = new URI(uri.trim());
   1.118 +            return connectTo;
   1.119 +        } catch (IOException ex) {
   1.120 +            throw new IllegalStateException(ex);
   1.121 +        } catch (URISyntaxException ex) {
   1.122 +            throw new IllegalStateException(ex);
   1.123 +        }
   1.124 +    }    
   1.125 +}