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@1230
|
28 |
import net.java.html.BrwsrCtx;
|
jaroslav@1230
|
29 |
import net.java.html.js.JavaScriptBody;
|
jaroslav@1230
|
30 |
import org.apidesign.bck2brwsr.vmtest.VMTest;
|
jaroslav@1230
|
31 |
import org.apidesign.html.context.spi.Contexts;
|
jaroslav@1230
|
32 |
import org.apidesign.html.json.spi.Technology;
|
jaroslav@1230
|
33 |
import org.apidesign.html.json.spi.Transfer;
|
jaroslav@1249
|
34 |
import org.apidesign.html.json.spi.WSTransfer;
|
jaroslav@1230
|
35 |
import org.apidesign.html.json.tck.KOTest;
|
jaroslav@1230
|
36 |
import org.apidesign.html.json.tck.KnockoutTCK;
|
jaroslav@1233
|
37 |
import org.apidesign.html.kofx.FXContext;
|
jaroslav@1249
|
38 |
import org.apidesign.html.wstyrus.TyrusContext;
|
jaroslav@1230
|
39 |
import org.json.JSONException;
|
jaroslav@1230
|
40 |
import org.json.JSONObject;
|
jaroslav@1230
|
41 |
import org.openide.util.lookup.ServiceProvider;
|
jaroslav@1230
|
42 |
import org.testng.annotations.Factory;
|
jaroslav@1230
|
43 |
|
jaroslav@1230
|
44 |
/**
|
jaroslav@1230
|
45 |
*
|
jaroslav@1230
|
46 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jaroslav@1230
|
47 |
*/
|
jaroslav@1230
|
48 |
@ServiceProvider(service = KnockoutTCK.class)
|
jaroslav@1230
|
49 |
public final class KnockoutFXTest extends KnockoutTCK {
|
jaroslav@1230
|
50 |
public KnockoutFXTest() {
|
jaroslav@1230
|
51 |
}
|
jaroslav@1230
|
52 |
|
jaroslav@1230
|
53 |
@Factory public static Object[] compatibilityTests() {
|
jaroslav@1230
|
54 |
return VMTest.newTests().
|
jaroslav@1230
|
55 |
withClasses(testClasses()).
|
jaroslav@1230
|
56 |
withTestAnnotation(KOTest.class).
|
jaroslav@1230
|
57 |
withLaunchers("fxbrwsr").build();
|
jaroslav@1230
|
58 |
}
|
jaroslav@1230
|
59 |
|
jaroslav@1230
|
60 |
@Override
|
jaroslav@1230
|
61 |
public BrwsrCtx createContext() {
|
jaroslav@1230
|
62 |
FXContext fx = new FXContext();
|
jaroslav@1249
|
63 |
TyrusContext tc = new TyrusContext();
|
jaroslav@1249
|
64 |
Contexts.Builder b = Contexts.newBuilder().
|
jaroslav@1230
|
65 |
register(Technology.class, fx, 10).
|
jaroslav@1249
|
66 |
register(Transfer.class, fx, 10);
|
jaroslav@1249
|
67 |
try {
|
jaroslav@1249
|
68 |
Class.forName("java.util.function.Function");
|
jaroslav@1249
|
69 |
// prefer WebView's WebSockets on JDK8
|
jaroslav@1249
|
70 |
b.register(WSTransfer.class, fx, 10);
|
jaroslav@1249
|
71 |
} catch (ClassNotFoundException ex) {
|
jaroslav@1249
|
72 |
// ok, JDK7 needs tyrus
|
jaroslav@1249
|
73 |
b.register(WSTransfer.class, tc, 20);
|
jaroslav@1249
|
74 |
}
|
jaroslav@1249
|
75 |
return b.build();
|
jaroslav@1230
|
76 |
}
|
jaroslav@1230
|
77 |
|
jaroslav@1230
|
78 |
@Override
|
jaroslav@1230
|
79 |
public Object createJSON(Map<String, Object> values) {
|
jaroslav@1230
|
80 |
JSONObject json = new JSONObject();
|
jaroslav@1230
|
81 |
for (Map.Entry<String, Object> entry : values.entrySet()) {
|
jaroslav@1230
|
82 |
try {
|
jaroslav@1230
|
83 |
json.put(entry.getKey(), entry.getValue());
|
jaroslav@1230
|
84 |
} catch (JSONException ex) {
|
jaroslav@1230
|
85 |
throw new IllegalStateException(ex);
|
jaroslav@1230
|
86 |
}
|
jaroslav@1230
|
87 |
}
|
jaroslav@1230
|
88 |
return json;
|
jaroslav@1230
|
89 |
}
|
jaroslav@1230
|
90 |
|
jaroslav@1230
|
91 |
@Override
|
jaroslav@1230
|
92 |
@JavaScriptBody(args = { "s", "args" }, body = ""
|
jaroslav@1230
|
93 |
+ "var f = new Function(s); "
|
jaroslav@1230
|
94 |
+ "return f.apply(null, args);"
|
jaroslav@1230
|
95 |
)
|
jaroslav@1230
|
96 |
public native Object executeScript(String script, Object[] arguments);
|
jaroslav@1230
|
97 |
|
jaroslav@1230
|
98 |
@JavaScriptBody(args = { }, body =
|
jaroslav@1230
|
99 |
"var h;"
|
jaroslav@1230
|
100 |
+ "if (!!window && !!window.location && !!window.location.href)\n"
|
jaroslav@1230
|
101 |
+ " h = window.location.href;\n"
|
jaroslav@1230
|
102 |
+ "else "
|
jaroslav@1230
|
103 |
+ " h = null;"
|
jaroslav@1230
|
104 |
+ "return h;\n"
|
jaroslav@1230
|
105 |
)
|
jaroslav@1230
|
106 |
private static native String findBaseURL();
|
jaroslav@1230
|
107 |
|
jaroslav@1230
|
108 |
@Override
|
jaroslav@1230
|
109 |
public URI prepareURL(String content, String mimeType, String[] parameters) {
|
jaroslav@1230
|
110 |
try {
|
jaroslav@1230
|
111 |
final URL baseURL = new URL(findBaseURL());
|
jaroslav@1230
|
112 |
StringBuilder sb = new StringBuilder();
|
jaroslav@1230
|
113 |
sb.append("/dynamic?mimeType=").append(mimeType);
|
jaroslav@1230
|
114 |
for (int i = 0; i < parameters.length; i++) {
|
jaroslav@1230
|
115 |
sb.append("¶m" + i).append("=").append(parameters[i]);
|
jaroslav@1230
|
116 |
}
|
jaroslav@1230
|
117 |
String mangle = content.replace("\n", "%0a")
|
jaroslav@1230
|
118 |
.replace("\"", "\\\"").replace(" ", "%20");
|
jaroslav@1230
|
119 |
sb.append("&content=").append(mangle);
|
jaroslav@1230
|
120 |
|
jaroslav@1230
|
121 |
URL query = new URL(baseURL, sb.toString());
|
jaroslav@1230
|
122 |
URLConnection c = query.openConnection();
|
jaroslav@1230
|
123 |
BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
|
jaroslav@1230
|
124 |
URI connectTo = new URI(br.readLine());
|
jaroslav@1230
|
125 |
return connectTo;
|
jaroslav@1230
|
126 |
} catch (IOException ex) {
|
jaroslav@1230
|
127 |
throw new IllegalStateException(ex);
|
jaroslav@1230
|
128 |
} catch (URISyntaxException ex) {
|
jaroslav@1230
|
129 |
throw new IllegalStateException(ex);
|
jaroslav@1230
|
130 |
}
|
jaroslav@1230
|
131 |
}
|
jaroslav@1230
|
132 |
}
|