# HG changeset patch # User Jaroslav Tulach # Date 1366319370 -7200 # Node ID 6dc2c6c752df94b38edf17e125ef1e0d65863794 # Parent d7cff2cba6e5fde0f276b39bcf0c39ee9169821b Can execute 'dual' tests: bck2brwsr can use regular launcher, javaquery.api can use FX Web View one diff -r d7cff2cba6e5 -r 6dc2c6c752df javaquery/api/pom.xml --- a/javaquery/api/pom.xml Thu Apr 18 20:09:45 2013 +0200 +++ b/javaquery/api/pom.xml Thu Apr 18 23:09:30 2013 +0200 @@ -30,9 +30,18 @@ false + + org.apache.maven.plugins + maven-surefire-plugin + + + fx + + + - + UTF-8 diff -r d7cff2cba6e5 -r 6dc2c6c752df javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/ConvertTypes.java --- a/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/ConvertTypes.java Thu Apr 18 20:09:45 2013 +0200 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/ConvertTypes.java Thu Apr 18 23:09:30 2013 +0200 @@ -42,7 +42,6 @@ * @author Jaroslav Tulach */ public final class ConvertTypes { - private static final Logger LOG = Logger.getLogger(ConvertTypes.class.getName()); ConvertTypes() { } @@ -123,14 +122,16 @@ return "json" + Integer.toHexString(whenDone.hashCode()); } + @JavaScriptBody(args = { "url", "jsonResult", "whenDone", "jsonp" }, body = "throw 'ignore';") public static void loadJSON( String url, Object[] jsonResult, Runnable whenDone, String jsonp ) { - REQ.execute(new LoadJSON(url, jsonResult, whenDone, jsonp)); + LoadJSON.REQ.execute(new LoadJSON(url, jsonResult, whenDone, jsonp)); } - private static final Executor REQ = Executors.newCachedThreadPool(); private static final class LoadJSON implements Runnable { + static final Executor REQ = Executors.newCachedThreadPool(); + private static final Logger LOG = Logger.getLogger(ConvertTypes.class.getName()); private final String url; private final Object[] jsonResult; @@ -230,7 +231,8 @@ private static void loadJSONP(String url, String jsonp) { } - + + @JavaScriptBody(args = { "jsonObject", "props", "values" }, body = "throw 'ignore extractJSON';") public static void extractJSON(Object jsonObject, String[] props, Object[] values) { if (jsonObject instanceof JSONObject) { JSONObject obj = (JSONObject)jsonObject; @@ -238,7 +240,7 @@ try { values[i] = obj.has(props[i]) ? obj.get(props[i]) : null; } catch (JSONException ex) { - LOG.log(Level.SEVERE, "Can't read " + props[i] + " from " + jsonObject, ex); + LoadJSON.LOG.log(Level.SEVERE, "Can't read " + props[i] + " from " + jsonObject, ex); } } diff -r d7cff2cba6e5 -r 6dc2c6c752df javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/Knockout.java --- a/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/Knockout.java Thu Apr 18 20:09:45 2013 +0200 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/Knockout.java Thu Apr 18 23:09:30 2013 +0200 @@ -40,28 +40,6 @@ static Knockout next; private final Object model; - static { - BufferedReader r = new BufferedReader(new InputStreamReader(Knockout.class.getResourceAsStream("knockout-2.2.1.js"))); - StringBuilder sb = new StringBuilder(); - for (;;) { - try { - String l = r.readLine(); - if (l == null) { - break; - } - sb.append(l).append('\n'); - } catch (IOException ex) { - throw new IllegalStateException(ex); - } - } - web().executeScript(sb.toString()); - Object ko = web().executeScript("ko"); - assert ko != null : "Knockout library successfully defined 'ko'"; - - Console.register(web()); - } - - Knockout(Object model) { this.model = model == null ? this : model; } @@ -70,43 +48,15 @@ return model; } - private static final JSObject KObject; - static { - KObject = (JSObject) web().executeScript( - "(function(scope) {" - + " var kCnt = 0; " - + " scope.KObject = {};" - + " scope.KObject.create= function(value) {" - + " var cnt = ++kCnt;" - + " var ret = {};" - + " ret.toString = function() { return 'KObject' + cnt + ' value: ' + value + ' props: ' + Object.keys(this); };" - + " return ret;" - + " };" - - + " scope.KObject.array= function() {" - + " return Array.prototype.slice.call(arguments);" - + " };" - - + " scope.KObject.expose = function(bindings, model, prop, sig) {" - + " bindings[prop] = function(data, ev) {" -// + " console.log(\" callback on prop: \" + prop);" - + " model[sig](data, ev);" - + " };" - + " };" - - + "})(window); window.KObject" - ); - } - static Object toArray(Object[] arr) { - return KObject.call("array", arr); + return InvokeJS.KObject.call("array", arr); } public static Knockout applyBindings( Object model, String[] propsGettersAndSetters, String[] methodsAndSignatures ) { - Object bindings = KObject.call("create", model); + Object bindings = InvokeJS.KObject.call("create", model); applyImpl(propsGettersAndSetters, model.getClass(), bindings, model, methodsAndSignatures); return new Knockout(bindings); } @@ -117,7 +67,7 @@ Object bindings = next; next = null; if (bindings == null) { - bindings = KObject.call("create", model); + bindings = InvokeJS.KObject.call("create", model); } applyImpl(propsGettersAndSetters, modelClass, bindings, model, methodsAndSignatures); applyBindings(bindings); @@ -167,6 +117,9 @@ Object bindings, Object model, String prop, String getter, String setter, boolean primitive, boolean array ) { WebEngine e = web(); + if (e == null) { + return; + } JSObject bnd = (JSObject) e.executeScript("var x = {}; x.bnd = " + "new Function('ko', 'bindings', 'model', 'prop', 'getter', 'setter', 'primitive', 'array', '" + "var bnd = {" @@ -198,7 +151,7 @@ + "};" + "bindings[prop] = ko.computed(bnd);'" + "); x;"); - + Object ko = e.executeScript("ko"); try { KOProperty kop = new KOProperty(model, strip(getter), strip(setter)); @@ -224,9 +177,13 @@ private static void expose( Object bindings, Object model, String prop, String sig ) { + WebEngine e = web(); + if (e == null) { + return; + } try { KOFunction f = new KOFunction(model, strip(sig)); - KObject.call("expose", bindings, f, prop, "call"); + InvokeJS.KObject.call("expose", bindings, f, prop, "call"); } catch (Throwable ex) { LOG.log(Level.SEVERE, "Cannot define binding for " + prop + " in model " + model, ex); } @@ -234,8 +191,10 @@ @JavaScriptBody(args = { "bindings" }, body = "ko.applyBindings(bindings);") private static void applyBindings(Object bindings) { - JSObject ko = (JSObject) web().executeScript("ko"); - ko.call("applyBindings", bindings); + if (web() != null) { + JSObject ko = (JSObject) web().executeScript("ko"); + ko.call("applyBindings", bindings); + } } private static WebEngine web() { @@ -267,4 +226,50 @@ bindings, model, methodsAndSignatures[i], methodsAndSignatures[i + 1]); } } + + private static final class InvokeJS { + static final JSObject KObject; + + static { + BufferedReader r = new BufferedReader(new InputStreamReader(Knockout.class.getResourceAsStream("knockout-2.2.1.js"))); + StringBuilder sb = new StringBuilder(); + for (;;) { + try { + String l = r.readLine(); + if (l == null) { + break; + } + sb.append(l).append('\n'); + } catch (IOException ex) { + throw new IllegalStateException(ex); + } + } + web().executeScript(sb.toString()); + Object ko = web().executeScript("ko"); + assert ko != null : "Knockout library successfully defined 'ko'"; + + Console.register(web()); + KObject = (JSObject) web().executeScript( + "(function(scope) {" + + " var kCnt = 0; " + + " scope.KObject = {};" + + " scope.KObject.create= function(value) {" + + " var cnt = ++kCnt;" + + " var ret = {};" + + " ret.toString = function() { return 'KObject' + cnt + ' value: ' + value + ' props: ' + Object.keys(this); };" + + " return ret;" + + " };" + + " scope.KObject.array= function() {" + + " return Array.prototype.slice.call(arguments);" + + " };" + + " scope.KObject.expose = function(bindings, model, prop, sig) {" + + " bindings[prop] = function(data, ev) {" + // + " console.log(\" callback on prop: \" + prop);" + + " model[sig](data, ev);" + + " };" + + " };" + + "})(window); window.KObject"); + } + + } } diff -r d7cff2cba6e5 -r 6dc2c6c752df rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Launcher.java --- a/rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Launcher.java Thu Apr 18 20:09:45 2013 +0200 +++ b/rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Launcher.java Thu Apr 18 23:09:30 2013 +0200 @@ -20,7 +20,6 @@ import java.io.Closeable; import java.io.File; import java.io.IOException; -import java.lang.reflect.Method; import org.apidesign.vm4brwsr.Bck2Brwsr; /** An abstraction for executing tests in a Bck2Brwsr virtual machine. @@ -80,7 +79,7 @@ */ public static Launcher createBrowser(String cmd) { final Bck2BrwsrLauncher l; - if ("fx".equals(cmd) || cmd == null) { + if ("fx".equals(cmd)) { l = new WebViewLauncher(); } else { l = new Bck2BrwsrLauncher(cmd); diff -r d7cff2cba6e5 -r 6dc2c6c752df rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/WebViewLauncher.java --- a/rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/WebViewLauncher.java Thu Apr 18 20:09:45 2013 +0200 +++ b/rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/WebViewLauncher.java Thu Apr 18 23:09:30 2013 +0200 @@ -70,7 +70,6 @@ } catch (Throwable ex) { LOG.log(Level.WARNING, "Error launching Web View", ex); } - System.exit(0); } }); } catch (Throwable ex) { diff -r d7cff2cba6e5 -r 6dc2c6c752df rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/impl/Console.java --- a/rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/impl/Console.java Thu Apr 18 20:09:45 2013 +0200 +++ b/rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/impl/Console.java Thu Apr 18 23:09:30 2013 +0200 @@ -36,23 +36,22 @@ public final class Console { public Console() { } - private static final JSObject CObject; @JavaScriptBody(args = {"elem", "attr"}, body = "return elem[attr].toString();") private static Object getAttr(Object elem, String attr) { - return CObject.call("getAttr", elem, attr); + return InvokeJS.CObject.call("getAttr", elem, attr); } @JavaScriptBody(args = {"id", "attr", "value"}, body = "window.document.getElementById(id)[attr] = value;") private static void setAttr(String id, String attr, Object value) { - CObject.call("setAttrId", id, attr, value); + InvokeJS.CObject.call("setAttrId", id, attr, value); } @JavaScriptBody(args = {"elem", "attr", "value"}, body = "elem[attr] = value;") private static void setAttr(Object id, String attr, Object value) { - CObject.call("setAttr", id, attr, value); + InvokeJS.CObject.call("setAttr", id, attr, value); } @JavaScriptBody(args = {}, body = "return; window.close();") @@ -116,7 +115,7 @@ @JavaScriptBody(args = { "test", "c", "arr" }, body = BEGIN_TEST) private static void beginTest(String test, Case c, Object[] arr) { - CObject.call("beginTest", test, c, arr); + InvokeJS.CObject.call("beginTest", test, c, arr); } private static final String LOAD_TEXT = @@ -127,13 +126,17 @@ + " if (this.readyState!==4) return;\n" + " try {" + " arr[0] = this.responseText;\n" - + " callback.run();\n" + + " callback.run__V();\n" + " } catch (e) { alert(e); }" + "};" + "request.send();"; @JavaScriptBody(args = { "url", "callback", "arr" }, body = LOAD_TEXT) private static void loadText(String url, Runnable callback, String[] arr) throws IOException { - CObject.call("loadText", url, new Run(callback), arr); + InvokeJS.CObject.call("loadText", url, new Run(callback), arr); + } + + public static void runHarness(String url) throws IOException { + new Console().harness(url); } public void harness(String url) throws IOException { @@ -261,7 +264,7 @@ @JavaScriptBody(args = {"r", "time"}, body = "return window.setTimeout(function() { r.run__V(); }, time);") private static Object schedule(Runnable r, int time) { - return CObject.call("schedule", new Run(r), time); + return InvokeJS.CObject.call("schedule", new Run(r), time); } private static final class Case { @@ -285,7 +288,11 @@ } public int getRequestId() { - return (int) value("request", data); + Object v = value("request", data); + if (v instanceof Number) { + return ((Number)v).intValue(); + } + return Integer.parseInt(v.toString()); } public String getHtmlFragment() { @@ -357,7 +364,7 @@ @JavaScriptBody(args = "s", body = "return eval('(' + s + ')');") private static Object toJSON(String s) { - return CObject.call("toJSON", s); + return InvokeJS.CObject.call("toJSON", s); } @JavaScriptBody(args = {"p", "d"}, body = @@ -376,26 +383,33 @@ static { turnAssetionStatusOn(); + } + + private static final class InvokeJS { + static final JSObject CObject = initJS(); - WebEngine web = (WebEngine) System.getProperties().get("webEngine"); - CObject = (JSObject) web.executeScript("(function() {" - + "var CObject = {};" + @JavaScriptBody(args = { }, body = "return null;") + private static JSObject initJS() { + WebEngine web = (WebEngine) System.getProperties().get("webEngine"); + return (JSObject) web.executeScript("(function() {" + + "var CObject = {};" - + "CObject.getAttr = function(elem, attr) { return elem[attr].toString(); };" + + "CObject.getAttr = function(elem, attr) { return elem[attr].toString(); };" - + "CObject.setAttrId = function(id, attr, value) { window.document.getElementById(id)[attr] = value; };" - + "CObject.setAttr = function(elem, attr, value) { elem[attr] = value; };" + + "CObject.setAttrId = function(id, attr, value) { window.document.getElementById(id)[attr] = value; };" + + "CObject.setAttr = function(elem, attr, value) { elem[attr] = value; };" - + "CObject.beginTest = function(test, c, arr) {" + safe(BEGIN_TEST) + "};" + + "CObject.beginTest = function(test, c, arr) {" + safe(BEGIN_TEST) + "};" - + "CObject.loadText = function(url, callback, arr) {" + safe(LOAD_TEXT) + "};" - - + "CObject.schedule = function(r, time) { return window.setTimeout(function() { r.run(); }, time); };" - - + "CObject.toJSON = function(s) { return eval('(' + s + ')'); };" + + "CObject.loadText = function(url, callback, arr) {" + safe(LOAD_TEXT.replace("run__V", "run")) + "};" - + "return CObject;" - + "})(this)"); + + "CObject.schedule = function(r, time) { return window.setTimeout(function() { r.run(); }, time); };" + + + "CObject.toJSON = function(s) { return eval('(' + s + ')'); };" + + + "return CObject;" + + "})(this)"); + } } } diff -r d7cff2cba6e5 -r 6dc2c6c752df rt/launcher/src/main/resources/org/apidesign/bck2brwsr/launcher/harness.xhtml --- a/rt/launcher/src/main/resources/org/apidesign/bck2brwsr/launcher/harness.xhtml Thu Apr 18 20:09:45 2013 +0200 +++ b/rt/launcher/src/main/resources/org/apidesign/bck2brwsr/launcher/harness.xhtml Thu Apr 18 23:09:30 2013 +0200 @@ -38,8 +38,17 @@