2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. Look for COPYING file in the top folder.
16 * If not, see http://opensource.org/licenses/GPL-2.0.
18 package org.apidesign.bck2brwsr.htmlpage;
20 import java.util.List;
21 import org.apidesign.bck2brwsr.core.JavaScriptBody;
22 import org.apidesign.bck2brwsr.htmlpage.api.ComputedProperty;
23 import org.apidesign.bck2brwsr.htmlpage.api.OnEvent;
24 import org.apidesign.bck2brwsr.htmlpage.api.OnFunction;
25 import org.apidesign.bck2brwsr.htmlpage.api.Page;
26 import org.apidesign.bck2brwsr.htmlpage.api.Property;
27 import org.apidesign.bck2brwsr.vmtest.BrwsrTest;
28 import org.apidesign.bck2brwsr.vmtest.HtmlFragment;
29 import org.apidesign.bck2brwsr.vmtest.VMTest;
30 import org.testng.annotations.Factory;
34 * @author Jaroslav Tulach <jtulach@netbeans.org>
36 @Page(xhtml="Knockout.xhtml", className="KnockoutModel", properties={
37 @Property(name="name", type=String.class),
38 @Property(name="results", type=String.class, array = true),
39 @Property(name="callbackCount", type=int.class),
40 @Property(name="people", type=PersonImpl.class, array = true)
42 public class KnockoutTest {
45 "<h1 data-bind=\"text: helloMessage\">Loading Bck2Brwsr's Hello World...</h1>\n" +
46 "Your name: <input id='input' data-bind=\"value: name\"></input>\n" +
47 "<button id=\"hello\">Say Hello!</button>\n"
49 @BrwsrTest public void modifyValueAssertChangeInModel() {
50 KnockoutModel m = new KnockoutModel();
53 assert "Kukuc".equals(m.input.getValue()) : "Value is really kukuc: " + m.input.getValue();
54 m.input.setValue("Jardo");
55 m.triggerEvent(m.input, OnEvent.CHANGE);
56 assert "Jardo".equals(m.getName()) : "Name property updated: " + m.getName();
60 "<ul id='ul' data-bind='foreach: results'>\n"
61 + " <li data-bind='text: $data, click: $root.call'/>\n"
64 @BrwsrTest public void displayContentOfArray() {
65 KnockoutModel m = new KnockoutModel();
66 m.getResults().add("Ahoj");
69 int cnt = countChildren("ul");
70 assert cnt == 1 : "One child, but was " + cnt;
72 m.getResults().add("Hi");
74 cnt = countChildren("ul");
75 assert cnt == 2 : "Two children now, but was " + cnt;
77 triggerChildClick("ul", 1);
79 assert 1 == m.getCallbackCount() : "One callback " + m.getCallbackCount();
80 assert "Hi".equals(m.getName()) : "We got callback from 2nd child " + m.getName();
84 "<ul id='ul' data-bind='foreach: cmpResults'>\n"
85 + " <li><b data-bind='text: $data'></b></li>\n"
88 @BrwsrTest public void displayContentOfDerivedArray() {
89 KnockoutModel m = new KnockoutModel();
90 m.getResults().add("Ahoj");
93 int cnt = countChildren("ul");
94 assert cnt == 1 : "One child, but was " + cnt;
96 m.getResults().add("hello");
98 cnt = countChildren("ul");
99 assert cnt == 2 : "Two children now, but was " + cnt;
103 "<ul id='ul' data-bind='foreach: people'>\n"
104 + " <li data-bind='text: $data.firstName(), click: $root.removePerson'></li>\n"
107 @BrwsrTest public void displayContentOfArrayOfPeople() {
108 KnockoutModel m = new KnockoutModel();
110 final Person first = new Person();
111 first.setFirstName("first");
112 m.getPeople().add(first);
116 int cnt = countChildren("ul");
117 assert cnt == 1 : "One child, but was " + cnt;
119 final Person second = new Person();
120 second.setFirstName("second");
121 m.getPeople().add(second);
123 cnt = countChildren("ul");
124 assert cnt == 2 : "Two children now, but was " + cnt;
126 triggerChildClick("ul", 1);
128 assert 1 == m.getCallbackCount() : "One callback " + m.getCallbackCount();
130 cnt = countChildren("ul");
131 assert cnt == 1 : "Again one child, but was " + cnt;
133 String txt = childText("ul", 0);
134 assert "first".equals(txt) : "Expecting 'first': " + txt;
138 static void call(KnockoutModel m, String data) {
140 m.setCallbackCount(m.getCallbackCount() + 1);
144 static void removePerson(KnockoutModel model, Person data) {
145 model.setCallbackCount(model.getCallbackCount() + 1);
146 model.getPeople().remove(data);
151 static String helloMessage(String name) {
152 return "Hello " + name + "!";
156 static List<String> cmpResults(List<String> results) {
161 public static Object[] create() {
162 return VMTest.create(KnockoutTest.class);
165 @JavaScriptBody(args = { "id" }, body =
166 "var e = window.document.getElementById(id);\n "
167 + "if (typeof e === 'undefined') return -2;\n "
168 + "return e.children.length;\n "
170 private static native int countChildren(String id);
172 @JavaScriptBody(args = { "id", "pos" }, body =
173 "var e = window.document.getElementById(id);\n "
174 + "var ev = window.document.createEvent('MouseEvents');\n "
175 + "ev.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);\n "
176 + "e.children[pos].dispatchEvent(ev);\n "
178 private static native void triggerChildClick(String id, int pos);
180 @JavaScriptBody(args = { "id", "pos" }, body =
181 "var e = window.document.getElementById(id);\n "
182 + "var t = e.children[pos].innerHTML;\n "
183 + "return t ? t : null;"
185 private static native String childText(String id, int pos);