ko/fx/src/test/java/org/apidesign/bck2brwsr/kofx/KnockoutFXTest.java
branchclassloader
changeset 1230 466c30fd9cb0
child 1233 43fba26ba0c0
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/ko/fx/src/test/java/org/apidesign/bck2brwsr/kofx/KnockoutFXTest.java	Wed Jun 26 19:57:38 2013 +0200
     1.3 @@ -0,0 +1,123 @@
     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.kofx;
    1.22 +
    1.23 +import org.apidesign.bck2brwsr.kofx.FXContext;
    1.24 +import java.io.BufferedReader;
    1.25 +import java.io.IOException;
    1.26 +import java.io.InputStreamReader;
    1.27 +import java.io.PrintWriter;
    1.28 +import java.net.HttpURLConnection;
    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 net.java.html.js.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.json.JSONException;
    1.43 +import org.json.JSONObject;
    1.44 +import org.openide.util.lookup.ServiceProvider;
    1.45 +import org.testng.annotations.Factory;
    1.46 +
    1.47 +/**
    1.48 + *
    1.49 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.50 + */
    1.51 +@ServiceProvider(service = KnockoutTCK.class)
    1.52 +public final class KnockoutFXTest extends KnockoutTCK {
    1.53 +    public KnockoutFXTest() {
    1.54 +    }
    1.55 +
    1.56 +    @Factory public static Object[] compatibilityTests() {
    1.57 +        return VMTest.newTests().
    1.58 +            withClasses(testClasses()).
    1.59 +            withTestAnnotation(KOTest.class).
    1.60 +            withLaunchers("fxbrwsr").build();
    1.61 +    }
    1.62 +
    1.63 +    @Override
    1.64 +    public BrwsrCtx createContext() {
    1.65 +        FXContext fx = new FXContext();
    1.66 +        return Contexts.newBuilder().
    1.67 +            register(Technology.class, fx, 10).
    1.68 +            register(Transfer.class, fx, 10).
    1.69 +            build();
    1.70 +    }
    1.71 +
    1.72 +    @Override
    1.73 +    public Object createJSON(Map<String, Object> values) {
    1.74 +        JSONObject json = new JSONObject();
    1.75 +        for (Map.Entry<String, Object> entry : values.entrySet()) {
    1.76 +            try {
    1.77 +                json.put(entry.getKey(), entry.getValue());
    1.78 +            } catch (JSONException ex) {
    1.79 +                throw new IllegalStateException(ex);
    1.80 +            }
    1.81 +        }
    1.82 +        return json;
    1.83 +    }
    1.84 +
    1.85 +    @Override
    1.86 +    @JavaScriptBody(args = { "s", "args" }, body = ""
    1.87 +        + "var f = new Function(s); "
    1.88 +        + "return f.apply(null, args);"
    1.89 +    )
    1.90 +    public native Object executeScript(String script, 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 +            URLConnection c = query.openConnection();
   1.117 +            BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
   1.118 +            URI connectTo = new URI(br.readLine());
   1.119 +            return connectTo;
   1.120 +        } catch (IOException ex) {
   1.121 +            throw new IllegalStateException(ex);
   1.122 +        } catch (URISyntaxException ex) {
   1.123 +            throw new IllegalStateException(ex);
   1.124 +        }
   1.125 +    }
   1.126 +}