2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. Look for COPYING file in the top folder.
16 * If not, see http://opensource.org/licenses/GPL-2.0.
18 package org.apidesign.bck2brwsr.kofx;
20 import org.apidesign.bck2brwsr.kofx.FXContext;
21 import java.io.BufferedReader;
22 import java.io.IOException;
23 import java.io.InputStreamReader;
24 import java.io.PrintWriter;
25 import java.net.HttpURLConnection;
27 import java.net.URISyntaxException;
29 import java.net.URLConnection;
31 import net.java.html.BrwsrCtx;
32 import net.java.html.js.JavaScriptBody;
33 import org.apidesign.bck2brwsr.vmtest.VMTest;
34 import org.apidesign.html.context.spi.Contexts;
35 import org.apidesign.html.json.spi.Technology;
36 import org.apidesign.html.json.spi.Transfer;
37 import org.apidesign.html.json.tck.KOTest;
38 import org.apidesign.html.json.tck.KnockoutTCK;
39 import org.json.JSONException;
40 import org.json.JSONObject;
41 import org.openide.util.lookup.ServiceProvider;
42 import org.testng.annotations.Factory;
46 * @author Jaroslav Tulach <jtulach@netbeans.org>
48 @ServiceProvider(service = KnockoutTCK.class)
49 public final class KnockoutFXTest extends KnockoutTCK {
50 public KnockoutFXTest() {
53 @Factory public static Object[] compatibilityTests() {
54 return VMTest.newTests().
55 withClasses(testClasses()).
56 withTestAnnotation(KOTest.class).
57 withLaunchers("fxbrwsr").build();
61 public BrwsrCtx createContext() {
62 FXContext fx = new FXContext();
63 return Contexts.newBuilder().
64 register(Technology.class, fx, 10).
65 register(Transfer.class, fx, 10).
70 public Object createJSON(Map<String, Object> values) {
71 JSONObject json = new JSONObject();
72 for (Map.Entry<String, Object> entry : values.entrySet()) {
74 json.put(entry.getKey(), entry.getValue());
75 } catch (JSONException ex) {
76 throw new IllegalStateException(ex);
83 @JavaScriptBody(args = { "s", "args" }, body = ""
84 + "var f = new Function(s); "
85 + "return f.apply(null, args);"
87 public native Object executeScript(String script, Object[] arguments);
89 @JavaScriptBody(args = { }, body =
91 + "if (!!window && !!window.location && !!window.location.href)\n"
92 + " h = window.location.href;\n"
97 private static native String findBaseURL();
100 public URI prepareURL(String content, String mimeType, String[] parameters) {
102 final URL baseURL = new URL(findBaseURL());
103 StringBuilder sb = new StringBuilder();
104 sb.append("/dynamic?mimeType=").append(mimeType);
105 for (int i = 0; i < parameters.length; i++) {
106 sb.append("¶m" + i).append("=").append(parameters[i]);
108 String mangle = content.replace("\n", "%0a")
109 .replace("\"", "\\\"").replace(" ", "%20");
110 sb.append("&content=").append(mangle);
112 URL query = new URL(baseURL, sb.toString());
113 URLConnection c = query.openConnection();
114 BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
115 URI connectTo = new URI(br.readLine());
117 } catch (IOException ex) {
118 throw new IllegalStateException(ex);
119 } catch (URISyntaxException ex) {
120 throw new IllegalStateException(ex);