1.1 --- a/javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/KnockoutTest.java Thu Apr 04 13:08:26 2013 +0200
1.2 +++ b/javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/KnockoutTest.java Thu Apr 18 16:58:55 2013 +0200
1.3 @@ -18,6 +18,8 @@
1.4 package org.apidesign.bck2brwsr.htmlpage;
1.5
1.6 import java.util.List;
1.7 +import javafx.scene.web.WebEngine;
1.8 +import netscape.javascript.JSObject;
1.9 import org.apidesign.bck2brwsr.core.JavaScriptBody;
1.10 import org.apidesign.bck2brwsr.htmlpage.api.ComputedProperty;
1.11 import org.apidesign.bck2brwsr.htmlpage.api.OnEvent;
1.12 @@ -39,7 +41,7 @@
1.13 @Property(name="name", type=String.class),
1.14 @Property(name="results", type=String.class, array = true),
1.15 @Property(name="callbackCount", type=int.class),
1.16 - @Property(name="people", type=PersonImpl.class, array = true)
1.17 + @Property(name="people", type=Person.class, array = true)
1.18 })
1.19 public class KnockoutTest {
1.20
1.21 @@ -234,25 +236,53 @@
1.22 return VMTest.create(KnockoutTest.class);
1.23 }
1.24
1.25 - @JavaScriptBody(args = { "id" }, body =
1.26 + private static final String COUNT_CHILDREN =
1.27 "var e = window.document.getElementById(id);\n "
1.28 + "if (typeof e === 'undefined') return -2;\n "
1.29 - + "return e.children.length;\n "
1.30 - )
1.31 - private static native int countChildren(String id);
1.32 + + "return e.children.length;\n";
1.33 + @JavaScriptBody(args = { "id" }, body = COUNT_CHILDREN)
1.34 + private static int countChildren(String id) {
1.35 + return ((Number)js().call("countChildren", id)).intValue();
1.36 + }
1.37
1.38 - @JavaScriptBody(args = { "id", "pos" }, body =
1.39 + private static final String TRIGGER_CHILD_CLICK =
1.40 "var e = window.document.getElementById(id);\n "
1.41 + "var ev = window.document.createEvent('MouseEvents');\n "
1.42 + "ev.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);\n "
1.43 - + "e.children[pos].dispatchEvent(ev);\n "
1.44 - )
1.45 - private static native void triggerChildClick(String id, int pos);
1.46 + + "e.children[pos].dispatchEvent(ev);\n ";
1.47 + @JavaScriptBody(args = { "id", "pos" }, body = TRIGGER_CHILD_CLICK)
1.48 + private static void triggerChildClick(String id, int pos) {
1.49 + js().call("triggerChildClick", id, pos);
1.50 + }
1.51
1.52 - @JavaScriptBody(args = { "id", "pos" }, body =
1.53 + private static final String CHILD_TEXT =
1.54 "var e = window.document.getElementById(id);\n "
1.55 + "var t = e.children[pos].innerHTML;\n "
1.56 - + "return t ? t : null;"
1.57 - )
1.58 - private static native String childText(String id, int pos);
1.59 + + "return t ? t : null;";
1.60 + @JavaScriptBody(args = { "id", "pos" }, body = CHILD_TEXT)
1.61 + private static String childText(String id, int pos) {
1.62 + return (String) js().call("childText", id, pos);
1.63 + }
1.64 +
1.65 +
1.66 + private static JSObject js;
1.67 + private static JSObject js() {
1.68 + if (js == null) {
1.69 + WebEngine eng = (WebEngine)System.getProperties().get("webEngine");
1.70 + if (eng == null) {
1.71 + js = null;
1.72 + } else {
1.73 + js = (JSObject) eng.executeScript("(function () {" +
1.74 + "var obj = {};"
1.75 + + ""
1.76 + + "obj.countChildren = function(id) { " + COUNT_CHILDREN + "};"
1.77 + + "obj.triggerChildClick = function(id, pos) { " + TRIGGER_CHILD_CLICK + "};"
1.78 + + "obj.childText = function(id, pos) { " + CHILD_TEXT + "};"
1.79 + + "return obj;"
1.80 + + "})();");
1.81 + }
1.82 + }
1.83 +
1.84 + return js;
1.85 + }
1.86 }