jaroslav@1230
|
1 |
/**
|
jaroslav@1230
|
2 |
* Back 2 Browser Bytecode Translator
|
jaroslav@1230
|
3 |
* Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jaroslav@1230
|
4 |
*
|
jaroslav@1230
|
5 |
* This program is free software: you can redistribute it and/or modify
|
jaroslav@1230
|
6 |
* it under the terms of the GNU General Public License as published by
|
jaroslav@1230
|
7 |
* the Free Software Foundation, version 2 of the License.
|
jaroslav@1230
|
8 |
*
|
jaroslav@1230
|
9 |
* This program is distributed in the hope that it will be useful,
|
jaroslav@1230
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
jaroslav@1230
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
jaroslav@1230
|
12 |
* GNU General Public License for more details.
|
jaroslav@1230
|
13 |
*
|
jaroslav@1230
|
14 |
* You should have received a copy of the GNU General Public License
|
jaroslav@1230
|
15 |
* along with this program. Look for COPYING file in the top folder.
|
jaroslav@1230
|
16 |
* If not, see http://opensource.org/licenses/GPL-2.0.
|
jaroslav@1230
|
17 |
*/
|
jaroslav@1230
|
18 |
package org.apidesign.bck2brwsr.kofx;
|
jaroslav@1230
|
19 |
|
jaroslav@1230
|
20 |
import java.io.BufferedReader;
|
jaroslav@1230
|
21 |
import java.io.IOException;
|
jaroslav@1230
|
22 |
import java.io.InputStreamReader;
|
jaroslav@1230
|
23 |
import java.net.URI;
|
jaroslav@1230
|
24 |
import java.net.URISyntaxException;
|
jaroslav@1230
|
25 |
import java.net.URL;
|
jaroslav@1230
|
26 |
import java.net.URLConnection;
|
jaroslav@1230
|
27 |
import java.util.Map;
|
jaroslav@1481
|
28 |
import java.util.concurrent.Executor;
|
jaroslav@1230
|
29 |
import net.java.html.BrwsrCtx;
|
jaroslav@1230
|
30 |
import net.java.html.js.JavaScriptBody;
|
jaroslav@1230
|
31 |
import org.apidesign.bck2brwsr.vmtest.VMTest;
|
jaroslav@1418
|
32 |
import org.apidesign.html.boot.spi.Fn;
|
jaroslav@1230
|
33 |
import org.apidesign.html.context.spi.Contexts;
|
jaroslav@1230
|
34 |
import org.apidesign.html.json.spi.Technology;
|
jaroslav@1230
|
35 |
import org.apidesign.html.json.spi.Transfer;
|
jaroslav@1249
|
36 |
import org.apidesign.html.json.spi.WSTransfer;
|
jaroslav@1230
|
37 |
import org.apidesign.html.json.tck.KOTest;
|
jaroslav@1230
|
38 |
import org.apidesign.html.json.tck.KnockoutTCK;
|
jaroslav@1230
|
39 |
import org.json.JSONException;
|
jaroslav@1230
|
40 |
import org.json.JSONObject;
|
jaroslav@1424
|
41 |
import org.netbeans.html.ko4j.KO4J;
|
jaroslav@1418
|
42 |
import org.netbeans.html.wstyrus.TyrusContext;
|
jaroslav@1230
|
43 |
import org.openide.util.lookup.ServiceProvider;
|
jaroslav@1230
|
44 |
import org.testng.annotations.Factory;
|
jaroslav@1230
|
45 |
|
jaroslav@1230
|
46 |
/**
|
jaroslav@1230
|
47 |
*
|
jaroslav@1230
|
48 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jaroslav@1230
|
49 |
*/
|
jaroslav@1230
|
50 |
@ServiceProvider(service = KnockoutTCK.class)
|
jaroslav@1230
|
51 |
public final class KnockoutFXTest extends KnockoutTCK {
|
jaroslav@1230
|
52 |
public KnockoutFXTest() {
|
jaroslav@1230
|
53 |
}
|
jaroslav@1230
|
54 |
|
jaroslav@1230
|
55 |
@Factory public static Object[] compatibilityTests() {
|
jaroslav@1230
|
56 |
return VMTest.newTests().
|
jaroslav@1230
|
57 |
withClasses(testClasses()).
|
jaroslav@1230
|
58 |
withTestAnnotation(KOTest.class).
|
jaroslav@1230
|
59 |
withLaunchers("fxbrwsr").build();
|
jaroslav@1230
|
60 |
}
|
jaroslav@1230
|
61 |
|
jaroslav@1230
|
62 |
@Override
|
jaroslav@1230
|
63 |
public BrwsrCtx createContext() {
|
jaroslav@1481
|
64 |
final Fn.Presenter p = Fn.activePresenter();
|
jaroslav@1481
|
65 |
KO4J ko = new KO4J(p);
|
jaroslav@1249
|
66 |
TyrusContext tc = new TyrusContext();
|
jaroslav@1249
|
67 |
Contexts.Builder b = Contexts.newBuilder().
|
jaroslav@1424
|
68 |
register(Technology.class, ko.knockout(), 10).
|
jaroslav@1429
|
69 |
register(Transfer.class, ko.transfer(), 10);
|
jaroslav@1481
|
70 |
if (p instanceof Executor) {
|
jaroslav@1481
|
71 |
b.register(Executor.class, (Executor)p, 10);
|
jaroslav@1481
|
72 |
}
|
jaroslav@1249
|
73 |
try {
|
jaroslav@1249
|
74 |
Class.forName("java.util.function.Function");
|
jaroslav@1249
|
75 |
// prefer WebView's WebSockets on JDK8
|
jaroslav@1429
|
76 |
b.register(WSTransfer.class, ko.websockets(), 10);
|
jaroslav@1249
|
77 |
} catch (ClassNotFoundException ex) {
|
jaroslav@1249
|
78 |
// ok, JDK7 needs tyrus
|
jaroslav@1249
|
79 |
b.register(WSTransfer.class, tc, 20);
|
jaroslav@1429
|
80 |
b.register(Transfer.class, tc, 5);
|
jaroslav@1249
|
81 |
}
|
jaroslav@1249
|
82 |
return b.build();
|
jaroslav@1230
|
83 |
}
|
jaroslav@1230
|
84 |
|
jaroslav@1230
|
85 |
@Override
|
jaroslav@1230
|
86 |
public Object createJSON(Map<String, Object> values) {
|
jaroslav@1429
|
87 |
Object json = createJSON();
|
jaroslav@1230
|
88 |
for (Map.Entry<String, Object> entry : values.entrySet()) {
|
jaroslav@1429
|
89 |
setProperty(json, entry.getKey(), entry.getValue());
|
jaroslav@1230
|
90 |
}
|
jaroslav@1230
|
91 |
return json;
|
jaroslav@1230
|
92 |
}
|
jaroslav@1429
|
93 |
|
jaroslav@1429
|
94 |
@JavaScriptBody(args = {}, body = "return new Object();")
|
jaroslav@1429
|
95 |
private static native Object createJSON();
|
jaroslav@1429
|
96 |
@JavaScriptBody(args = { "json", "key", "value" }, body = "json[key] = value;")
|
jaroslav@1429
|
97 |
private static native void setProperty(Object json, String key, Object value);
|
jaroslav@1230
|
98 |
|
jaroslav@1230
|
99 |
@Override
|
jaroslav@1230
|
100 |
@JavaScriptBody(args = { "s", "args" }, body = ""
|
jaroslav@1230
|
101 |
+ "var f = new Function(s); "
|
jaroslav@1230
|
102 |
+ "return f.apply(null, args);"
|
jaroslav@1230
|
103 |
)
|
jaroslav@1230
|
104 |
public native Object executeScript(String script, Object[] arguments);
|
jaroslav@1230
|
105 |
|
jaroslav@1230
|
106 |
@JavaScriptBody(args = { }, body =
|
jaroslav@1230
|
107 |
"var h;"
|
jaroslav@1230
|
108 |
+ "if (!!window && !!window.location && !!window.location.href)\n"
|
jaroslav@1230
|
109 |
+ " h = window.location.href;\n"
|
jaroslav@1230
|
110 |
+ "else "
|
jaroslav@1230
|
111 |
+ " h = null;"
|
jaroslav@1230
|
112 |
+ "return h;\n"
|
jaroslav@1230
|
113 |
)
|
jaroslav@1230
|
114 |
private static native String findBaseURL();
|
jaroslav@1230
|
115 |
|
jaroslav@1230
|
116 |
@Override
|
jaroslav@1230
|
117 |
public URI prepareURL(String content, String mimeType, String[] parameters) {
|
jaroslav@1230
|
118 |
try {
|
jaroslav@1230
|
119 |
final URL baseURL = new URL(findBaseURL());
|
jaroslav@1230
|
120 |
StringBuilder sb = new StringBuilder();
|
jaroslav@1230
|
121 |
sb.append("/dynamic?mimeType=").append(mimeType);
|
jaroslav@1230
|
122 |
for (int i = 0; i < parameters.length; i++) {
|
jaroslav@1230
|
123 |
sb.append("¶m" + i).append("=").append(parameters[i]);
|
jaroslav@1230
|
124 |
}
|
jaroslav@1230
|
125 |
String mangle = content.replace("\n", "%0a")
|
jaroslav@1230
|
126 |
.replace("\"", "\\\"").replace(" ", "%20");
|
jaroslav@1230
|
127 |
sb.append("&content=").append(mangle);
|
jaroslav@1230
|
128 |
|
jaroslav@1230
|
129 |
URL query = new URL(baseURL, sb.toString());
|
jaroslav@1230
|
130 |
URLConnection c = query.openConnection();
|
jaroslav@1230
|
131 |
BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
|
jaroslav@1230
|
132 |
URI connectTo = new URI(br.readLine());
|
jaroslav@1230
|
133 |
return connectTo;
|
jaroslav@1230
|
134 |
} catch (IOException ex) {
|
jaroslav@1230
|
135 |
throw new IllegalStateException(ex);
|
jaroslav@1230
|
136 |
} catch (URISyntaxException ex) {
|
jaroslav@1230
|
137 |
throw new IllegalStateException(ex);
|
jaroslav@1230
|
138 |
}
|
jaroslav@1230
|
139 |
}
|
jaroslav@1230
|
140 |
}
|