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