diff -r e8916518b38d -r 9cc253aa9405 javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/KnockoutTest.java --- a/javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/KnockoutTest.java Thu Apr 04 13:08:26 2013 +0200 +++ b/javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/KnockoutTest.java Thu Apr 18 16:58:55 2013 +0200 @@ -18,6 +18,8 @@ package org.apidesign.bck2brwsr.htmlpage; import java.util.List; +import javafx.scene.web.WebEngine; +import netscape.javascript.JSObject; import org.apidesign.bck2brwsr.core.JavaScriptBody; import org.apidesign.bck2brwsr.htmlpage.api.ComputedProperty; import org.apidesign.bck2brwsr.htmlpage.api.OnEvent; @@ -39,7 +41,7 @@ @Property(name="name", type=String.class), @Property(name="results", type=String.class, array = true), @Property(name="callbackCount", type=int.class), - @Property(name="people", type=PersonImpl.class, array = true) + @Property(name="people", type=Person.class, array = true) }) public class KnockoutTest { @@ -234,25 +236,53 @@ return VMTest.create(KnockoutTest.class); } - @JavaScriptBody(args = { "id" }, body = + private static final String COUNT_CHILDREN = "var e = window.document.getElementById(id);\n " + "if (typeof e === 'undefined') return -2;\n " - + "return e.children.length;\n " - ) - private static native int countChildren(String id); + + "return e.children.length;\n"; + @JavaScriptBody(args = { "id" }, body = COUNT_CHILDREN) + private static int countChildren(String id) { + return ((Number)js().call("countChildren", id)).intValue(); + } - @JavaScriptBody(args = { "id", "pos" }, body = + private static final String TRIGGER_CHILD_CLICK = "var e = window.document.getElementById(id);\n " + "var ev = window.document.createEvent('MouseEvents');\n " + "ev.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);\n " - + "e.children[pos].dispatchEvent(ev);\n " - ) - private static native void triggerChildClick(String id, int pos); + + "e.children[pos].dispatchEvent(ev);\n "; + @JavaScriptBody(args = { "id", "pos" }, body = TRIGGER_CHILD_CLICK) + private static void triggerChildClick(String id, int pos) { + js().call("triggerChildClick", id, pos); + } - @JavaScriptBody(args = { "id", "pos" }, body = + private static final String CHILD_TEXT = "var e = window.document.getElementById(id);\n " + "var t = e.children[pos].innerHTML;\n " - + "return t ? t : null;" - ) - private static native String childText(String id, int pos); + + "return t ? t : null;"; + @JavaScriptBody(args = { "id", "pos" }, body = CHILD_TEXT) + private static String childText(String id, int pos) { + return (String) js().call("childText", id, pos); + } + + + private static JSObject js; + private static JSObject js() { + if (js == null) { + WebEngine eng = (WebEngine)System.getProperties().get("webEngine"); + if (eng == null) { + js = null; + } else { + js = (JSObject) eng.executeScript("(function () {" + + "var obj = {};" + + "" + + "obj.countChildren = function(id) { " + COUNT_CHILDREN + "};" + + "obj.triggerChildClick = function(id, pos) { " + TRIGGER_CHILD_CLICK + "};" + + "obj.childText = function(id, pos) { " + CHILD_TEXT + "};" + + "return obj;" + + "})();"); + } + } + + return js; + } }