ko/bck2brwsr/src/test/java/org/apidesign/bck2brwsr/ko2brwsr/Bck2BrwsrKnockoutTest.java
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.ko2brwsr;
20 import java.io.IOException;
22 import java.net.URISyntaxException;
25 import net.java.html.BrwsrCtx;
26 import org.apidesign.bck2brwsr.core.JavaScriptBody;
27 import org.apidesign.bck2brwsr.vmtest.VMTest;
28 import org.apidesign.html.context.spi.Contexts;
29 import org.apidesign.html.json.spi.Technology;
30 import org.apidesign.html.json.spi.Transfer;
31 import org.apidesign.html.json.spi.WSTransfer;
32 import org.apidesign.html.json.tck.KOTest;
33 import org.apidesign.html.json.tck.KnockoutTCK;
34 import org.netbeans.html.ko4j.KO4J;
35 import org.openide.util.lookup.ServiceProvider;
36 import org.testng.annotations.Factory;
40 * @author Jaroslav Tulach <jtulach@netbeans.org>
42 @ServiceProvider(service = KnockoutTCK.class)
43 public final class Bck2BrwsrKnockoutTest extends KnockoutTCK {
44 @Factory public static Object[] create() {
45 return VMTest.newTests().
46 withClasses(testClasses()).
47 withLaunchers("bck2brwsr").
48 withTestAnnotation(KOTest.class).
53 public BrwsrCtx createContext() {
54 KO4J ko = new KO4J(null);
55 return Contexts.newBuilder().
56 register(Transfer.class, ko.transfer(), 9).
57 register(WSTransfer.class, ko.websockets(), 9).
58 register(Technology.class, ko.knockout(), 9).build();
64 public Object createJSON(Map<String, Object> values) {
65 Object json = createJSON();
67 for (Map.Entry<String, Object> entry : values.entrySet()) {
68 putValue(json, entry.getKey(), entry.getValue());
73 @JavaScriptBody(args = {}, body = "return new Object();")
74 private static native Object createJSON();
76 @JavaScriptBody(args = { "json", "key", "value" }, body = "json[key] = value;")
77 private static native void putValue(Object json, String key, Object value);
80 public Object executeScript(String script, Object[] arguments) {
81 return execScript(script, arguments);
84 @JavaScriptBody(args = { "s", "args" }, body =
85 "var f = new Function(s); return f.apply(null, args);"
87 private static native Object execScript(String s, 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 String uri = (String) query.getContent(new Class[] { String.class });
114 URI connectTo = new URI(uri.trim());
116 } catch (IOException ex) {
117 throw new IllegalStateException(ex);
118 } catch (URISyntaxException ex) {
119 throw new IllegalStateException(ex);