blob: 89190b7dd16c64a9990edd7ec008f2f3e7cc53e5 [file] [log] [blame]
/**
* HTML via Java(tm) Language Bindings
* Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. apidesign.org
* designates this particular file as subject to the
* "Classpath" exception as provided by apidesign.org
* in the License file that accompanied this code.
*
* You should have received a copy of the GNU General Public License
* along with this program. Look for COPYING file in the top folder.
* If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
*/
package org.apidesign.html.kofx;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Map;
import net.java.html.BrwsrCtx;
import net.java.html.js.JavaScriptBody;
import org.apidesign.bck2brwsr.vmtest.VMTest;
import org.apidesign.html.context.spi.Contexts;
import org.apidesign.html.json.spi.Technology;
import org.apidesign.html.json.spi.Transfer;
import org.apidesign.html.json.tck.KOTest;
import org.apidesign.html.json.tck.KnockoutTCK;
import org.json.JSONException;
import org.json.JSONObject;
import org.openide.util.lookup.ServiceProvider;
import org.testng.annotations.Factory;
/**
*
* @author Jaroslav Tulach <jtulach@netbeans.org>
*/
@ServiceProvider(service = KnockoutTCK.class)
public final class KnockoutFXTest extends KnockoutTCK {
public KnockoutFXTest() {
}
@Factory public static Object[] compatibilityTests() {
return VMTest.newTests().
withClasses(testClasses()).
withTestAnnotation(KOTest.class).
withLaunchers("fxbrwsr").build();
}
@Override
public BrwsrCtx createContext() {
FXContext fx = new FXContext();
return Contexts.newBuilder().
register(Technology.class, fx, 10).
register(Transfer.class, fx, 10).
build();
}
@Override
public Object createJSON(Map<String, Object> values) {
JSONObject json = new JSONObject();
for (Map.Entry<String, Object> entry : values.entrySet()) {
try {
json.put(entry.getKey(), entry.getValue());
} catch (JSONException ex) {
throw new IllegalStateException(ex);
}
}
return json;
}
@Override
@JavaScriptBody(args = { "s", "args" }, body = ""
+ "var f = new Function(s); "
+ "return f.apply(null, args);"
)
public native Object executeScript(String script, Object[] arguments);
@JavaScriptBody(args = { }, body =
"var h;"
+ "if (!!window && !!window.location && !!window.location.href)\n"
+ " h = window.location.href;\n"
+ "else "
+ " h = null;"
+ "return h;\n"
)
private static native String findBaseURL();
@Override
public URI prepareURL(String content, String mimeType, String[] parameters) {
try {
final URL baseURL = new URL(findBaseURL());
StringBuilder sb = new StringBuilder();
sb.append("/dynamic?mimeType=").append(mimeType);
for (int i = 0; i < parameters.length; i++) {
sb.append("&param" + i).append("=").append(parameters[i]);
}
String mangle = content.replace("\n", "%0a")
.replace("\"", "\\\"").replace(" ", "%20");
sb.append("&content=").append(mangle);
URL query = new URL(baseURL, sb.toString());
URLConnection c = query.openConnection();
BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
URI connectTo = new URI(br.readLine());
return connectTo;
} catch (IOException ex) {
throw new IllegalStateException(ex);
} catch (URISyntaxException ex) {
throw new IllegalStateException(ex);
}
}
}