1.1 --- a/javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/KnockoutTest.java Tue Feb 19 15:54:36 2013 +0100
1.2 +++ b/javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/KnockoutTest.java Mon Mar 25 11:50:36 2013 +0100
1.3 @@ -21,6 +21,7 @@
1.4 import org.apidesign.bck2brwsr.core.JavaScriptBody;
1.5 import org.apidesign.bck2brwsr.htmlpage.api.ComputedProperty;
1.6 import org.apidesign.bck2brwsr.htmlpage.api.OnEvent;
1.7 +import org.apidesign.bck2brwsr.htmlpage.api.OnFunction;
1.8 import org.apidesign.bck2brwsr.htmlpage.api.Page;
1.9 import org.apidesign.bck2brwsr.htmlpage.api.Property;
1.10 import org.apidesign.bck2brwsr.vmtest.BrwsrTest;
1.11 @@ -34,7 +35,8 @@
1.12 */
1.13 @Page(xhtml="Knockout.xhtml", className="KnockoutModel", properties={
1.14 @Property(name="name", type=String.class),
1.15 - @Property(name="results", type=String.class, array = true)
1.16 + @Property(name="results", type=String.class, array = true),
1.17 + @Property(name="callbackCount", type=int.class)
1.18 })
1.19 public class KnockoutTest {
1.20
1.21 @@ -55,7 +57,7 @@
1.22
1.23 @HtmlFragment(
1.24 "<ul id='ul' data-bind='foreach: results'>\n"
1.25 - + " <li><b data-bind='text: $data'></b></li>\n"
1.26 + + " <li data-bind='text: $data, click: $root.call'/>\n"
1.27 + "</ul>\n"
1.28 )
1.29 @BrwsrTest public void displayContentOfArray() {
1.30 @@ -66,10 +68,15 @@
1.31 int cnt = countChildren("ul");
1.32 assert cnt == 1 : "One child, but was " + cnt;
1.33
1.34 - m.getResults().add("hello");
1.35 + m.getResults().add("Hi");
1.36
1.37 cnt = countChildren("ul");
1.38 assert cnt == 2 : "Two children now, but was " + cnt;
1.39 +
1.40 + triggerChildClick("ul", 1);
1.41 +
1.42 + assert 1 == m.getCallbackCount() : "One callback " + m.getCallbackCount();
1.43 + assert "Hi".equals(m.getName()) : "We got callback from 2nd child " + m.getName();
1.44 }
1.45
1.46 @HtmlFragment(
1.47 @@ -91,6 +98,12 @@
1.48 assert cnt == 2 : "Two children now, but was " + cnt;
1.49 }
1.50
1.51 + @OnFunction
1.52 + static void call(KnockoutModel m, String data) {
1.53 + m.setName(data);
1.54 + m.setCallbackCount(m.getCallbackCount() + 1);
1.55 + }
1.56 +
1.57 @ComputedProperty
1.58 static String helloMessage(String name) {
1.59 return "Hello " + name + "!";
1.60 @@ -112,4 +125,12 @@
1.61 + "return e.children.length;\n "
1.62 )
1.63 private static native int countChildren(String id);
1.64 +
1.65 + @JavaScriptBody(args = { "id", "pos" }, body =
1.66 + "var e = window.document.getElementById(id);\n "
1.67 + + "var ev = window.document.createEvent('MouseEvents');\n "
1.68 + + "ev.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);\n "
1.69 + + "e.children[pos].dispatchEvent(ev);\n "
1.70 + )
1.71 + private static native void triggerChildClick(String id, int pos);
1.72 }