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 static org.testng.Assert.assertEquals;
31 import org.testng.annotations.Factory;
32 import org.testng.annotations.Test;
36 * @author Jaroslav Tulach <jtulach@netbeans.org>
38 @Page(xhtml="Knockout.xhtml", className="KnockoutModel", properties={
39 @Property(name="name", type=String.class),
40 @Property(name="results", type=String.class, array = true),
41 @Property(name="callbackCount", type=int.class),
42 @Property(name="people", type=PersonImpl.class, array = true)
44 public class KnockoutTest {
47 "<h1 data-bind=\"text: helloMessage\">Loading Bck2Brwsr's Hello World...</h1>\n" +
48 "Your name: <input id='input' data-bind=\"value: name\"></input>\n" +
49 "<button id=\"hello\">Say Hello!</button>\n"
51 @BrwsrTest public void modifyValueAssertChangeInModel() {
52 KnockoutModel m = new KnockoutModel();
55 assert "Kukuc".equals(m.input.getValue()) : "Value is really kukuc: " + m.input.getValue();
56 m.input.setValue("Jardo");
57 m.triggerEvent(m.input, OnEvent.CHANGE);
58 assert "Jardo".equals(m.getName()) : "Name property updated: " + m.getName();
62 "<ul id='ul' data-bind='foreach: results'>\n"
63 + " <li data-bind='text: $data, click: $root.call'/>\n"
66 @BrwsrTest public void displayContentOfArray() {
67 KnockoutModel m = new KnockoutModel();
68 m.getResults().add("Ahoj");
71 int cnt = countChildren("ul");
72 assert cnt == 1 : "One child, but was " + cnt;
74 m.getResults().add("Hi");
76 cnt = countChildren("ul");
77 assert cnt == 2 : "Two children now, but was " + cnt;
79 triggerChildClick("ul", 1);
81 assert 1 == m.getCallbackCount() : "One callback " + m.getCallbackCount();
82 assert "Hi".equals(m.getName()) : "We got callback from 2nd child " + m.getName();
86 "<ul id='ul' data-bind='foreach: cmpResults'>\n"
87 + " <li><b data-bind='text: $data'></b></li>\n"
90 @BrwsrTest public void displayContentOfDerivedArray() {
91 KnockoutModel m = new KnockoutModel();
92 m.getResults().add("Ahoj");
95 int cnt = countChildren("ul");
96 assert cnt == 1 : "One child, but was " + cnt;
98 m.getResults().add("hello");
100 cnt = countChildren("ul");
101 assert cnt == 2 : "Two children now, but was " + cnt;
105 "<ul id='ul' data-bind='foreach: people'>\n"
106 + " <li data-bind='text: $data.firstName, click: $root.removePerson'></li>\n"
109 @BrwsrTest public void displayContentOfArrayOfPeople() {
110 KnockoutModel m = new KnockoutModel();
112 final Person first = new Person();
113 first.setFirstName("first");
114 m.getPeople().add(first);
118 int cnt = countChildren("ul");
119 assert cnt == 1 : "One child, but was " + cnt;
121 final Person second = new Person();
122 second.setFirstName("second");
123 m.getPeople().add(second);
125 cnt = countChildren("ul");
126 assert cnt == 2 : "Two children now, but was " + cnt;
128 triggerChildClick("ul", 1);
130 assert 1 == m.getCallbackCount() : "One callback " + m.getCallbackCount();
132 cnt = countChildren("ul");
133 assert cnt == 1 : "Again one child, but was " + cnt;
135 String txt = childText("ul", 0);
136 assert "first".equals(txt) : "Expecting 'first': " + txt;
138 first.setFirstName("changed");
140 txt = childText("ul", 0);
141 assert "changed".equals(txt) : "Expecting 'changed': " + txt;
145 static Person firstPerson(List<Person> people) {
146 return people.isEmpty() ? null : people.get(0);
150 "<p id='ul' data-bind='with: firstPerson'>\n"
151 + " <span data-bind='text: firstName, click: changeSex'></span>\n"
154 @BrwsrTest public void accessFirstPersonWithOnFunction() {
159 "<ul id='ul' data-bind='foreach: people'>\n"
160 + " <li data-bind='text: $data.firstName, click: changeSex'></li>\n"
163 @BrwsrTest public void onPersonFunction() {
167 private void trasfertToFemale() {
168 KnockoutModel m = new KnockoutModel();
170 final Person first = new Person();
171 first.setFirstName("first");
172 first.setSex(Sex.MALE);
173 m.getPeople().add(first);
178 int cnt = countChildren("ul");
179 assert cnt == 1 : "One child, but was " + cnt;
182 triggerChildClick("ul", 0);
184 assert first.getSex() == Sex.FEMALE : "Transverted to female: " + first.getSex();
187 @Test public void cloneModel() {
188 Person model = new Person();
190 model.setFirstName("first");
191 Person snd = model.clone();
192 snd.setFirstName("clone");
193 assertEquals("first", model.getFirstName(), "Value has not changed");
194 assertEquals("clone", snd.getFirstName(), "Value has changed in clone");
198 @Test public void deepCopyOnClone() {
199 People model = new People();
200 model.getNicknames().add("Jarda");
201 assertEquals(model.getNicknames().size(), 1, "One element");
202 People snd = model.clone();
203 snd.getNicknames().clear();
204 assertEquals(snd.getNicknames().size(), 0, "Clone is empty");
205 assertEquals(model.getNicknames().size(), 1, "Still one element");
210 static void call(KnockoutModel m, String data) {
212 m.setCallbackCount(m.getCallbackCount() + 1);
216 static void removePerson(KnockoutModel model, Person data) {
217 model.setCallbackCount(model.getCallbackCount() + 1);
218 model.getPeople().remove(data);
223 static String helloMessage(String name) {
224 return "Hello " + name + "!";
228 static List<String> cmpResults(List<String> results) {
233 public static Object[] create() {
234 return VMTest.create(KnockoutTest.class);
237 @JavaScriptBody(args = { "id" }, body =
238 "var e = window.document.getElementById(id);\n "
239 + "if (typeof e === 'undefined') return -2;\n "
240 + "return e.children.length;\n "
242 private static native int countChildren(String id);
244 @JavaScriptBody(args = { "id", "pos" }, body =
245 "var e = window.document.getElementById(id);\n "
246 + "var ev = window.document.createEvent('MouseEvents');\n "
247 + "ev.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);\n "
248 + "e.children[pos].dispatchEvent(ev);\n "
250 private static native void triggerChildClick(String id, int pos);
252 @JavaScriptBody(args = { "id", "pos" }, body =
253 "var e = window.document.getElementById(id);\n "
254 + "var t = e.children[pos].innerHTML;\n "
255 + "return t ? t : null;"
257 private static native String childText(String id, int pos);