Adopting to new names of SPI packages to be introduced by version 1.0 of html4j API
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 java.io.BufferedReader;
21 import java.io.IOException;
22 import java.io.InputStreamReader;
24 import java.net.URISyntaxException;
26 import java.net.URLConnection;
28 import java.util.concurrent.Executor;
29 import net.java.html.BrwsrCtx;
30 import net.java.html.js.JavaScriptBody;
31 import org.apidesign.bck2brwsr.vmtest.VMTest;
32 import org.netbeans.html.boot.spi.Fn;
33 import org.netbeans.html.context.spi.Contexts;
34 import org.netbeans.html.json.spi.Technology;
35 import org.netbeans.html.json.spi.Transfer;
36 import org.netbeans.html.json.spi.WSTransfer;
37 import org.netbeans.html.json.tck.KOTest;
38 import org.netbeans.html.json.tck.KnockoutTCK;
39 import org.netbeans.html.ko4j.KO4J;
40 import org.netbeans.html.wstyrus.TyrusContext;
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 final Fn.Presenter p = Fn.activePresenter();
63 KO4J ko = new KO4J(p);
64 TyrusContext tc = new TyrusContext();
65 Contexts.Builder b = Contexts.newBuilder().
66 register(Technology.class, ko.knockout(), 10).
67 register(Transfer.class, ko.transfer(), 10);
68 if (p instanceof Executor) {
69 b.register(Executor.class, (Executor)p, 10);
72 Class.forName("java.util.function.Function");
73 // prefer WebView's WebSockets on JDK8
74 b.register(WSTransfer.class, ko.websockets(), 10);
75 } catch (ClassNotFoundException ex) {
76 // ok, JDK7 needs tyrus
77 b.register(WSTransfer.class, tc, 20);
78 b.register(Transfer.class, tc, 5);
84 public Object createJSON(Map<String, Object> values) {
85 Object json = createJSON();
86 for (Map.Entry<String, Object> entry : values.entrySet()) {
87 setProperty(json, entry.getKey(), entry.getValue());
92 @JavaScriptBody(args = {}, body = "return new Object();")
93 private static native Object createJSON();
94 @JavaScriptBody(args = { "json", "key", "value" }, body = "json[key] = value;")
95 private static native void setProperty(Object json, String key, Object value);
98 @JavaScriptBody(args = { "s", "args" }, body = ""
99 + "var f = new Function(s); "
100 + "return f.apply(null, args);"
102 public native Object executeScript(String script, Object[] arguments);
104 @JavaScriptBody(args = { }, body =
106 + "if (!!window && !!window.location && !!window.location.href)\n"
107 + " h = window.location.href;\n"
112 private static native String findBaseURL();
115 public URI prepareURL(String content, String mimeType, String[] parameters) {
117 final URL baseURL = new URL(findBaseURL());
118 StringBuilder sb = new StringBuilder();
119 sb.append("/dynamic?mimeType=").append(mimeType);
120 for (int i = 0; i < parameters.length; i++) {
121 sb.append("¶m" + i).append("=").append(parameters[i]);
123 String mangle = content.replace("\n", "%0a")
124 .replace("\"", "\\\"").replace(" ", "%20");
125 sb.append("&content=").append(mangle);
127 URL query = new URL(baseURL, sb.toString());
128 URLConnection c = query.openConnection();
129 BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
130 URI connectTo = new URI(br.readLine());
132 } catch (IOException ex) {
133 throw new IllegalStateException(ex);
134 } catch (URISyntaxException ex) {
135 throw new IllegalStateException(ex);