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.Page;
25 import org.apidesign.bck2brwsr.htmlpage.api.Property;
26 import org.apidesign.bck2brwsr.vmtest.BrwsrTest;
27 import org.apidesign.bck2brwsr.vmtest.HtmlFragment;
28 import org.apidesign.bck2brwsr.vmtest.VMTest;
29 import org.testng.annotations.Factory;
33 * @author Jaroslav Tulach <jtulach@netbeans.org>
35 @Page(xhtml="Knockout.xhtml", className="KnockoutModel", properties={
36 @Property(name="name", type=String.class),
37 @Property(name="results", type=String.class, array = true)
39 public class KnockoutTest {
42 "<h1 data-bind=\"text: helloMessage\">Loading Bck2Brwsr's Hello World...</h1>\n" +
43 "Your name: <input id='input' data-bind=\"value: name\"></input>\n" +
44 "<button id=\"hello\">Say Hello!</button>\n"
46 @BrwsrTest public void modifyValueAssertChangeInModel() {
47 KnockoutModel m = new KnockoutModel();
50 assert "Kukuc".equals(m.INPUT.getValue()) : "Value is really kukuc: " + m.INPUT.getValue();
51 m.INPUT.setValue("Jardo");
52 m.triggerEvent(m.INPUT, OnEvent.CHANGE);
53 assert "Jardo".equals(m.getName()) : "Name property updated: " + m.getName();
57 "<ul id='ul' data-bind='foreach: results'>\n"
58 + " <li><b data-bind='text: $data'></b></li>\n"
61 @BrwsrTest public void displayContentOfArray() {
62 KnockoutModel m = new KnockoutModel();
63 m.getResults().add("Ahoj");
66 int cnt = countChildren("ul");
67 assert cnt == 1 : "One child, but was " + cnt;
69 m.getResults().add("hello");
71 cnt = countChildren("ul");
72 assert cnt == 2 : "Two children now, but was " + cnt;
76 "<ul id='ul' data-bind='foreach: cmpResults'>\n"
77 + " <li><b data-bind='text: $data'></b></li>\n"
80 @BrwsrTest public void displayContentOfDerivedArray() {
81 KnockoutModel m = new KnockoutModel();
82 m.getResults().add("Ahoj");
85 int cnt = countChildren("ul");
86 assert cnt == 1 : "One child, but was " + cnt;
88 m.getResults().add("hello");
90 cnt = countChildren("ul");
91 assert cnt == 2 : "Two children now, but was " + cnt;
95 static String helloMessage(String name) {
96 return "Hello " + name + "!";
100 static List<String> cmpResults(List<String> results) {
105 public static Object[] create() {
106 return VMTest.create(KnockoutTest.class);
109 @JavaScriptBody(args = { "id" }, body =
110 "var e = window.document.getElementById(id);\n "
111 + "if (typeof e === 'undefined') return -2;\n "
112 + "return e.children.length;\n "
114 private static native int countChildren(String id);