jaroslav@530
|
1 |
/**
|
jaroslav@530
|
2 |
* Back 2 Browser Bytecode Translator
|
jaroslav@530
|
3 |
* Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jaroslav@530
|
4 |
*
|
jaroslav@530
|
5 |
* This program is free software: you can redistribute it and/or modify
|
jaroslav@530
|
6 |
* it under the terms of the GNU General Public License as published by
|
jaroslav@530
|
7 |
* the Free Software Foundation, version 2 of the License.
|
jaroslav@530
|
8 |
*
|
jaroslav@530
|
9 |
* This program is distributed in the hope that it will be useful,
|
jaroslav@530
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
jaroslav@530
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
jaroslav@530
|
12 |
* GNU General Public License for more details.
|
jaroslav@530
|
13 |
*
|
jaroslav@530
|
14 |
* You should have received a copy of the GNU General Public License
|
jaroslav@530
|
15 |
* along with this program. Look for COPYING file in the top folder.
|
jaroslav@530
|
16 |
* If not, see http://opensource.org/licenses/GPL-2.0.
|
jaroslav@530
|
17 |
*/
|
jaroslav@530
|
18 |
package org.apidesign.bck2brwsr.htmlpage;
|
jaroslav@530
|
19 |
|
jaroslav@767
|
20 |
import java.util.List;
|
jaroslav@763
|
21 |
import org.apidesign.bck2brwsr.core.JavaScriptBody;
|
jaroslav@530
|
22 |
import org.apidesign.bck2brwsr.htmlpage.api.ComputedProperty;
|
jaroslav@530
|
23 |
import org.apidesign.bck2brwsr.htmlpage.api.OnEvent;
|
jaroslav@530
|
24 |
import org.apidesign.bck2brwsr.htmlpage.api.Page;
|
jaroslav@530
|
25 |
import org.apidesign.bck2brwsr.htmlpage.api.Property;
|
jaroslav@530
|
26 |
import org.apidesign.bck2brwsr.vmtest.BrwsrTest;
|
jaroslav@530
|
27 |
import org.apidesign.bck2brwsr.vmtest.HtmlFragment;
|
jaroslav@530
|
28 |
import org.apidesign.bck2brwsr.vmtest.VMTest;
|
jaroslav@530
|
29 |
import org.testng.annotations.Factory;
|
jaroslav@530
|
30 |
|
jaroslav@530
|
31 |
/**
|
jaroslav@530
|
32 |
*
|
jaroslav@530
|
33 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jaroslav@530
|
34 |
*/
|
jaroslav@530
|
35 |
@Page(xhtml="Knockout.xhtml", className="KnockoutModel", properties={
|
jaroslav@763
|
36 |
@Property(name="name", type=String.class),
|
jaroslav@763
|
37 |
@Property(name="results", type=String.class, array = true)
|
jaroslav@530
|
38 |
})
|
jaroslav@530
|
39 |
public class KnockoutTest {
|
jaroslav@530
|
40 |
|
jaroslav@530
|
41 |
@HtmlFragment(
|
jaroslav@530
|
42 |
"<h1 data-bind=\"text: helloMessage\">Loading Bck2Brwsr's Hello World...</h1>\n" +
|
jaroslav@530
|
43 |
"Your name: <input id='input' data-bind=\"value: name\"></input>\n" +
|
jaroslav@530
|
44 |
"<button id=\"hello\">Say Hello!</button>\n"
|
jaroslav@530
|
45 |
)
|
jaroslav@530
|
46 |
@BrwsrTest public void modifyValueAssertChangeInModel() {
|
jaroslav@530
|
47 |
KnockoutModel m = new KnockoutModel();
|
jaroslav@530
|
48 |
m.setName("Kukuc");
|
jaroslav@530
|
49 |
m.applyBindings();
|
jaroslav@530
|
50 |
assert "Kukuc".equals(m.INPUT.getValue()) : "Value is really kukuc: " + m.INPUT.getValue();
|
jaroslav@530
|
51 |
m.INPUT.setValue("Jardo");
|
jaroslav@530
|
52 |
m.triggerEvent(m.INPUT, OnEvent.CHANGE);
|
jaroslav@530
|
53 |
assert "Jardo".equals(m.getName()) : "Name property updated: " + m.getName();
|
jaroslav@530
|
54 |
}
|
jaroslav@530
|
55 |
|
jaroslav@763
|
56 |
@HtmlFragment(
|
jaroslav@763
|
57 |
"<ul id='ul' data-bind='foreach: results'>\n"
|
jaroslav@763
|
58 |
+ " <li><b data-bind='text: $data'></b></li>\n"
|
jaroslav@763
|
59 |
+ "</ul>\n"
|
jaroslav@763
|
60 |
)
|
jaroslav@763
|
61 |
@BrwsrTest public void displayContentOfArray() {
|
jaroslav@763
|
62 |
KnockoutModel m = new KnockoutModel();
|
jaroslav@763
|
63 |
m.getResults().add("Ahoj");
|
jaroslav@763
|
64 |
m.applyBindings();
|
jaroslav@763
|
65 |
|
jaroslav@763
|
66 |
int cnt = countChildren("ul");
|
jaroslav@763
|
67 |
assert cnt == 1 : "One child, but was " + cnt;
|
jaroslav@763
|
68 |
|
jaroslav@763
|
69 |
m.getResults().add("hello");
|
jaroslav@763
|
70 |
|
jaroslav@763
|
71 |
cnt = countChildren("ul");
|
jaroslav@763
|
72 |
assert cnt == 2 : "Two children now, but was " + cnt;
|
jaroslav@763
|
73 |
}
|
jaroslav@763
|
74 |
|
jaroslav@767
|
75 |
@HtmlFragment(
|
jaroslav@767
|
76 |
"<ul id='ul' data-bind='foreach: cmpResults'>\n"
|
jaroslav@767
|
77 |
+ " <li><b data-bind='text: $data'></b></li>\n"
|
jaroslav@767
|
78 |
+ "</ul>\n"
|
jaroslav@767
|
79 |
)
|
jaroslav@767
|
80 |
@BrwsrTest public void displayContentOfDerivedArray() {
|
jaroslav@767
|
81 |
KnockoutModel m = new KnockoutModel();
|
jaroslav@767
|
82 |
m.getResults().add("Ahoj");
|
jaroslav@767
|
83 |
m.applyBindings();
|
jaroslav@767
|
84 |
|
jaroslav@767
|
85 |
int cnt = countChildren("ul");
|
jaroslav@767
|
86 |
assert cnt == 1 : "One child, but was " + cnt;
|
jaroslav@767
|
87 |
|
jaroslav@767
|
88 |
m.getResults().add("hello");
|
jaroslav@767
|
89 |
|
jaroslav@767
|
90 |
cnt = countChildren("ul");
|
jaroslav@767
|
91 |
assert cnt == 2 : "Two children now, but was " + cnt;
|
jaroslav@767
|
92 |
}
|
jaroslav@767
|
93 |
|
jaroslav@530
|
94 |
@ComputedProperty
|
jaroslav@530
|
95 |
static String helloMessage(String name) {
|
jaroslav@530
|
96 |
return "Hello " + name + "!";
|
jaroslav@530
|
97 |
}
|
jaroslav@530
|
98 |
|
jaroslav@767
|
99 |
@ComputedProperty
|
jaroslav@767
|
100 |
static List<String> cmpResults(List<String> results) {
|
jaroslav@767
|
101 |
return results;
|
jaroslav@767
|
102 |
}
|
jaroslav@767
|
103 |
|
jaroslav@530
|
104 |
@Factory
|
jaroslav@530
|
105 |
public static Object[] create() {
|
jaroslav@530
|
106 |
return VMTest.create(KnockoutTest.class);
|
jaroslav@530
|
107 |
}
|
jaroslav@763
|
108 |
|
jaroslav@763
|
109 |
@JavaScriptBody(args = { "id" }, body =
|
jaroslav@763
|
110 |
"var e = window.document.getElementById(id);\n "
|
jaroslav@763
|
111 |
+ "if (typeof e === 'undefined') return -2;\n "
|
jaroslav@763
|
112 |
+ "return e.children.length;\n "
|
jaroslav@763
|
113 |
)
|
jaroslav@763
|
114 |
private static native int countChildren(String id);
|
jaroslav@530
|
115 |
}
|