jaroslav@34
|
1 |
/**
|
jaroslav@358
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
jaroslav@34
|
3 |
*
|
jaroslav@551
|
4 |
* Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
|
jaroslav@34
|
5 |
*
|
jaroslav@358
|
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates.
|
jaroslav@358
|
7 |
* Other names may be trademarks of their respective owners.
|
jaroslav@34
|
8 |
*
|
jaroslav@358
|
9 |
* The contents of this file are subject to the terms of either the GNU
|
jaroslav@358
|
10 |
* General Public License Version 2 only ("GPL") or the Common
|
jaroslav@358
|
11 |
* Development and Distribution License("CDDL") (collectively, the
|
jaroslav@358
|
12 |
* "License"). You may not use this file except in compliance with the
|
jaroslav@358
|
13 |
* License. You can obtain a copy of the License at
|
jaroslav@358
|
14 |
* http://www.netbeans.org/cddl-gplv2.html
|
jaroslav@358
|
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
|
jaroslav@358
|
16 |
* specific language governing permissions and limitations under the
|
jaroslav@358
|
17 |
* License. When distributing the software, include this License Header
|
jaroslav@358
|
18 |
* Notice in each file and include the License file at
|
jaroslav@358
|
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
|
jaroslav@358
|
20 |
* particular file as subject to the "Classpath" exception as provided
|
jaroslav@358
|
21 |
* by Oracle in the GPL Version 2 section of the License file that
|
jaroslav@358
|
22 |
* accompanied this code. If applicable, add the following below the
|
jaroslav@358
|
23 |
* License Header, with the fields enclosed by brackets [] replaced by
|
jaroslav@358
|
24 |
* your own identifying information:
|
jaroslav@358
|
25 |
* "Portions Copyrighted [year] [name of copyright owner]"
|
jaroslav@358
|
26 |
*
|
jaroslav@358
|
27 |
* Contributor(s):
|
jaroslav@358
|
28 |
*
|
jaroslav@358
|
29 |
* The Original Software is NetBeans. The Initial Developer of the Original
|
jaroslav@551
|
30 |
* Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
|
jaroslav@358
|
31 |
*
|
jaroslav@358
|
32 |
* If you wish your version of this file to be governed by only the CDDL
|
jaroslav@358
|
33 |
* or only the GPL Version 2, indicate your decision by adding
|
jaroslav@358
|
34 |
* "[Contributor] elects to include this software in this distribution
|
jaroslav@358
|
35 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a
|
jaroslav@358
|
36 |
* single choice of license, a recipient has the option to distribute
|
jaroslav@358
|
37 |
* your version of this file under either the CDDL, the GPL Version 2 or
|
jaroslav@358
|
38 |
* to extend the choice of license to its licensees as provided above.
|
jaroslav@358
|
39 |
* However, if you add GPL Version 2 code and therefore, elected the GPL
|
jaroslav@358
|
40 |
* Version 2 license, then the option applies only if the new code is
|
jaroslav@358
|
41 |
* made subject to such option by the copyright holder.
|
jaroslav@34
|
42 |
*/
|
jaroslav@34
|
43 |
package net.java.html.json.tests;
|
jaroslav@34
|
44 |
|
jtulach@830
|
45 |
import java.util.Arrays;
|
jaroslav@34
|
46 |
import java.util.List;
|
jaroslav@717
|
47 |
import java.util.Timer;
|
jaroslav@717
|
48 |
import java.util.TimerTask;
|
jaroslav@121
|
49 |
import net.java.html.BrwsrCtx;
|
jaroslav@34
|
50 |
import net.java.html.json.ComputedProperty;
|
jaroslav@34
|
51 |
import net.java.html.json.Function;
|
jaroslav@34
|
52 |
import net.java.html.json.Model;
|
jaroslav@115
|
53 |
import net.java.html.json.Models;
|
jaroslav@34
|
54 |
import net.java.html.json.Property;
|
jtulach@838
|
55 |
import org.netbeans.html.json.tck.KOTest;
|
jtulach@934
|
56 |
import static net.java.html.json.tests.Utils.assertEquals;
|
jtulach@934
|
57 |
import static net.java.html.json.tests.Utils.assertNotNull;
|
jtulach@934
|
58 |
import static net.java.html.json.tests.Utils.assertTrue;
|
jtulach@934
|
59 |
import static net.java.html.json.tests.Utils.assertFalse;
|
jaroslav@34
|
60 |
|
jaroslav@34
|
61 |
/**
|
jaroslav@34
|
62 |
*
|
jtulach@790
|
63 |
* @author Jaroslav Tulach
|
jaroslav@34
|
64 |
*/
|
jtulach@920
|
65 |
@Model(className="KnockoutModel", targetId = "", properties={
|
jaroslav@34
|
66 |
@Property(name="name", type=String.class),
|
jaroslav@34
|
67 |
@Property(name="results", type=String.class, array = true),
|
jtulach@825
|
68 |
@Property(name="numbers", type=int.class, array = true),
|
jaroslav@34
|
69 |
@Property(name="callbackCount", type=int.class),
|
jaroslav@94
|
70 |
@Property(name="people", type=PersonImpl.class, array = true),
|
jaroslav@197
|
71 |
@Property(name="enabled", type=boolean.class),
|
jtulach@758
|
72 |
@Property(name="latitude", type=double.class),
|
jtulach@758
|
73 |
@Property(name="choice", type=KnockoutTest.Choice.class),
|
jtulach@809
|
74 |
@Property(name="archetype", type=ArchetypeData.class),
|
jtulach@809
|
75 |
@Property(name="archetypes", type=ArchetypeData.class, array = true),
|
jtulach@1055
|
76 |
})
|
jaroslav@34
|
77 |
public final class KnockoutTest {
|
jaroslav@717
|
78 |
private KnockoutModel js;
|
jtulach@1055
|
79 |
|
jtulach@758
|
80 |
enum Choice {
|
jtulach@758
|
81 |
A, B;
|
jtulach@758
|
82 |
}
|
jtulach@1055
|
83 |
|
jtulach@830
|
84 |
@ComputedProperty static List<Integer> resultLengths(List<String> results) {
|
jtulach@830
|
85 |
Integer[] arr = new Integer[results.size()];
|
jtulach@830
|
86 |
for (int i = 0; i < arr.length; i++) {
|
jtulach@830
|
87 |
arr[i] = results.get(i).length();
|
jtulach@830
|
88 |
}
|
jtulach@830
|
89 |
return Arrays.asList(arr);
|
jtulach@830
|
90 |
}
|
jtulach@1055
|
91 |
|
jtulach@758
|
92 |
@KOTest public void modifyValueAssertChangeInModelOnEnum() throws Throwable {
|
jtulach@1055
|
93 |
Object exp = Utils.exposeHTML(KnockoutTest.class,
|
jtulach@758
|
94 |
"Latitude: <input id='input' data-bind=\"value: choice\"></input>\n"
|
jtulach@758
|
95 |
);
|
jtulach@758
|
96 |
try {
|
jtulach@758
|
97 |
|
jtulach@758
|
98 |
KnockoutModel m = Models.bind(new KnockoutModel(), newContext());
|
jtulach@758
|
99 |
m.setChoice(Choice.A);
|
jtulach@758
|
100 |
m.applyBindings();
|
jtulach@758
|
101 |
|
jtulach@956
|
102 |
String v = getSetInput("input", null);
|
jtulach@934
|
103 |
assertEquals("A", v, "Value is really A: " + v);
|
jtulach@758
|
104 |
|
jtulach@956
|
105 |
getSetInput("input", "B");
|
jtulach@758
|
106 |
triggerEvent("input", "change");
|
jtulach@758
|
107 |
|
jtulach@934
|
108 |
assertEquals(Choice.B, m.getChoice(), "Enum property updated: " + m.getChoice());
|
jtulach@758
|
109 |
} catch (Throwable t) {
|
jtulach@758
|
110 |
throw t;
|
jtulach@758
|
111 |
} finally {
|
jtulach@758
|
112 |
Utils.exposeHTML(KnockoutTest.class, "");
|
jtulach@758
|
113 |
}
|
jtulach@758
|
114 |
}
|
jtulach@956
|
115 |
|
jtulach@956
|
116 |
|
jtulach@956
|
117 |
@KOTest public void modifyRadioValueOnEnum() throws Throwable {
|
jtulach@956
|
118 |
Object exp = Utils.exposeHTML(KnockoutTest.class,
|
jtulach@963
|
119 |
"<input id='i1' type=\"radio\" name=\"choice\" value=\"A\" data-bind=\"checked: choice\"></input>Right\n" +
|
jtulach@963
|
120 |
"<input id='input' type=\"radio\" name=\"choice\" value=\"B\" data-bind=\"checked: choice\"></input>Never\n" +
|
jtulach@956
|
121 |
"\n"
|
jtulach@956
|
122 |
);
|
jtulach@956
|
123 |
try {
|
jtulach@956
|
124 |
|
jtulach@956
|
125 |
KnockoutModel m = Models.bind(new KnockoutModel(), newContext());
|
jtulach@956
|
126 |
m.setChoice(Choice.B);
|
jtulach@956
|
127 |
m.applyBindings();
|
jtulach@956
|
128 |
|
jtulach@956
|
129 |
assertFalse(isChecked("i1"), "B should be checked now");
|
jtulach@956
|
130 |
assertTrue(isChecked("input"), "B should be checked now");
|
jtulach@956
|
131 |
|
jtulach@956
|
132 |
triggerEvent("i1", "click");
|
jtulach@956
|
133 |
assertEquals(Choice.A, m.getChoice(), "Switched to A");
|
jtulach@956
|
134 |
assertTrue(isChecked("i1"), "A should be checked now");
|
jtulach@956
|
135 |
assertFalse(isChecked("input"), "A should be checked now");
|
jtulach@956
|
136 |
|
jtulach@956
|
137 |
triggerEvent("input", "click");
|
jtulach@956
|
138 |
|
jtulach@956
|
139 |
assertEquals(Choice.B, m.getChoice(), "Enum property updated: " + m.getChoice());
|
jtulach@956
|
140 |
} catch (Throwable t) {
|
jtulach@956
|
141 |
throw t;
|
jtulach@956
|
142 |
} finally {
|
jtulach@956
|
143 |
Utils.exposeHTML(KnockoutTest.class, "");
|
jtulach@956
|
144 |
}
|
jtulach@956
|
145 |
}
|
jtulach@1055
|
146 |
|
jaroslav@197
|
147 |
@KOTest public void modifyValueAssertChangeInModelOnDouble() throws Throwable {
|
jtulach@1055
|
148 |
Object exp = Utils.exposeHTML(KnockoutTest.class,
|
jaroslav@197
|
149 |
"Latitude: <input id='input' data-bind=\"value: latitude\"></input>\n"
|
jaroslav@197
|
150 |
);
|
jaroslav@197
|
151 |
try {
|
jaroslav@197
|
152 |
|
jaroslav@197
|
153 |
KnockoutModel m = Models.bind(new KnockoutModel(), newContext());
|
jaroslav@197
|
154 |
m.setLatitude(50.5);
|
jaroslav@197
|
155 |
m.applyBindings();
|
jaroslav@197
|
156 |
|
jtulach@956
|
157 |
String v = getSetInput("input", null);
|
jtulach@934
|
158 |
assertEquals("50.5", v, "Value is really 50.5: " + v);
|
jaroslav@197
|
159 |
|
jtulach@956
|
160 |
getSetInput("input", "49.5");
|
jaroslav@197
|
161 |
triggerEvent("input", "change");
|
jaroslav@197
|
162 |
|
jtulach@934
|
163 |
assertEquals(49.5, m.getLatitude(), "Double property updated: " + m.getLatitude());
|
jaroslav@197
|
164 |
} catch (Throwable t) {
|
jaroslav@197
|
165 |
throw t;
|
jaroslav@197
|
166 |
} finally {
|
jaroslav@197
|
167 |
Utils.exposeHTML(KnockoutTest.class, "");
|
jaroslav@197
|
168 |
}
|
jaroslav@197
|
169 |
}
|
jtulach@1055
|
170 |
|
jtulach@1047
|
171 |
@KOTest public void rawObject() throws Exception {
|
jtulach@1047
|
172 |
if (js == null) {
|
jtulach@1047
|
173 |
final BrwsrCtx ctx = newContext();
|
jtulach@1047
|
174 |
Person p1 = Models.bind(new Person(), ctx);
|
jtulach@1047
|
175 |
p1.setFirstName("Jarda");
|
jtulach@1047
|
176 |
p1.setLastName("Tulach");
|
jtulach@1047
|
177 |
Object raw = Models.toRaw(p1);
|
jtulach@1047
|
178 |
Person p2 = Models.fromRaw(ctx, Person.class, raw);
|
jtulach@1028
|
179 |
|
jtulach@1047
|
180 |
assertEquals(p2.getFirstName(), "Jarda", "First name");
|
jtulach@1047
|
181 |
assertEquals(p2.getLastName(), "Tulach", "Last name");
|
jtulach@1029
|
182 |
|
jtulach@1047
|
183 |
p2.setFirstName("Jirka");
|
jtulach@1047
|
184 |
assertEquals(p2.getFirstName(), "Jirka", "First name updated");
|
jtulach@1047
|
185 |
|
jtulach@1047
|
186 |
js = new KnockoutModel();
|
jtulach@1047
|
187 |
js.getPeople().add(p1);
|
jtulach@1047
|
188 |
js.getPeople().add(p2);
|
jtulach@1047
|
189 |
}
|
jtulach@1047
|
190 |
|
jtulach@1047
|
191 |
Person p1 = js.getPeople().get(0);
|
jtulach@1047
|
192 |
Person p2 = js.getPeople().get(1);
|
jtulach@1047
|
193 |
|
jtulach@1047
|
194 |
if (js.getPeople().size() == 2) {
|
jtulach@1047
|
195 |
if (!"Jirka".equals(p1.getFirstName())) {
|
jtulach@1047
|
196 |
throw new InterruptedException();
|
jtulach@1047
|
197 |
}
|
jtulach@1047
|
198 |
|
jtulach@1047
|
199 |
assertEquals(p1.getFirstName(), "Jirka", "First name updated in original object");
|
jtulach@1047
|
200 |
|
jtulach@1047
|
201 |
p1.setFirstName("Ondra");
|
jtulach@1047
|
202 |
assertEquals(p1.getFirstName(), "Ondra", "1st name updated in original object");
|
jtulach@1055
|
203 |
|
jtulach@1047
|
204 |
js.getPeople().add(p1);
|
jtulach@1047
|
205 |
}
|
jtulach@1047
|
206 |
|
jtulach@1047
|
207 |
if (!"Ondra".equals(p2.getFirstName())) {
|
jtulach@1047
|
208 |
throw new InterruptedException();
|
jtulach@1047
|
209 |
}
|
jtulach@1029
|
210 |
assertEquals(p2.getFirstName(), "Ondra", "1st name updated in copied object");
|
jtulach@1020
|
211 |
}
|
jtulach@951
|
212 |
|
jtulach@951
|
213 |
@KOTest public void modifyComputedProperty() throws Throwable {
|
jtulach@951
|
214 |
Object exp = Utils.exposeHTML(KnockoutTest.class,
|
jtulach@951
|
215 |
"Full name: <div data-bind='with:firstPerson'>\n"
|
jtulach@951
|
216 |
+ "<input id='input' data-bind=\"value: fullName\"></input>\n"
|
jtulach@951
|
217 |
+ "</div>\n"
|
jtulach@951
|
218 |
);
|
jtulach@951
|
219 |
try {
|
jtulach@951
|
220 |
KnockoutModel m = new KnockoutModel();
|
jtulach@951
|
221 |
m.getPeople().add(new Person());
|
jtulach@951
|
222 |
|
jtulach@951
|
223 |
m = Models.bind(m, newContext());
|
jtulach@951
|
224 |
m.getFirstPerson().setFirstName("Jarda");
|
jtulach@951
|
225 |
m.getFirstPerson().setLastName("Tulach");
|
jtulach@951
|
226 |
m.applyBindings();
|
jtulach@951
|
227 |
|
jtulach@956
|
228 |
String v = getSetInput("input", null);
|
jtulach@951
|
229 |
assertEquals("Jarda Tulach", v, "Value: " + v);
|
jtulach@951
|
230 |
|
jtulach@956
|
231 |
getSetInput("input", "Mickey Mouse");
|
jtulach@951
|
232 |
triggerEvent("input", "change");
|
jtulach@951
|
233 |
|
jtulach@951
|
234 |
assertEquals("Mickey", m.getFirstPerson().getFirstName(), "First name updated");
|
jtulach@951
|
235 |
assertEquals("Mouse", m.getFirstPerson().getLastName(), "Last name updated");
|
jtulach@951
|
236 |
} catch (Throwable t) {
|
jtulach@951
|
237 |
throw t;
|
jtulach@951
|
238 |
} finally {
|
jtulach@951
|
239 |
Utils.exposeHTML(KnockoutTest.class, "");
|
jtulach@951
|
240 |
}
|
jtulach@951
|
241 |
}
|
jtulach@1055
|
242 |
|
jaroslav@197
|
243 |
@KOTest public void modifyValueAssertChangeInModelOnBoolean() throws Throwable {
|
jtulach@1055
|
244 |
Object exp = Utils.exposeHTML(KnockoutTest.class,
|
jaroslav@197
|
245 |
"Latitude: <input id='input' data-bind=\"value: enabled\"></input>\n"
|
jaroslav@197
|
246 |
);
|
jaroslav@197
|
247 |
try {
|
jaroslav@197
|
248 |
|
jaroslav@197
|
249 |
KnockoutModel m = Models.bind(new KnockoutModel(), newContext());
|
jaroslav@197
|
250 |
m.setEnabled(true);
|
jaroslav@197
|
251 |
m.applyBindings();
|
jaroslav@197
|
252 |
|
jtulach@956
|
253 |
String v = getSetInput("input", null);
|
jtulach@934
|
254 |
assertEquals("true", v, "Value is really true: " + v);
|
jaroslav@197
|
255 |
|
jtulach@956
|
256 |
getSetInput("input", "false");
|
jaroslav@197
|
257 |
triggerEvent("input", "change");
|
jaroslav@197
|
258 |
|
jtulach@934
|
259 |
assertFalse(m.isEnabled(), "Boolean property updated: " + m.isEnabled());
|
jaroslav@197
|
260 |
} catch (Throwable t) {
|
jaroslav@197
|
261 |
throw t;
|
jaroslav@197
|
262 |
} finally {
|
jaroslav@197
|
263 |
Utils.exposeHTML(KnockoutTest.class, "");
|
jaroslav@197
|
264 |
}
|
jaroslav@197
|
265 |
}
|
jtulach@1055
|
266 |
|
jaroslav@137
|
267 |
@KOTest public void modifyValueAssertChangeInModel() throws Exception {
|
jtulach@1055
|
268 |
Object exp = Utils.exposeHTML(KnockoutTest.class,
|
jaroslav@137
|
269 |
"<h1 data-bind=\"text: helloMessage\">Loading Bck2Brwsr's Hello World...</h1>\n" +
|
jaroslav@137
|
270 |
"Your name: <input id='input' data-bind=\"value: name\"></input>\n" +
|
jaroslav@137
|
271 |
"<button id=\"hello\">Say Hello!</button>\n"
|
jaroslav@137
|
272 |
);
|
jaroslav@141
|
273 |
try {
|
jaroslav@137
|
274 |
|
jaroslav@141
|
275 |
KnockoutModel m = Models.bind(new KnockoutModel(), newContext());
|
jaroslav@141
|
276 |
m.setName("Kukuc");
|
jaroslav@141
|
277 |
m.applyBindings();
|
jaroslav@137
|
278 |
|
jtulach@956
|
279 |
String v = getSetInput("input", null);
|
jtulach@934
|
280 |
assertEquals("Kukuc", v, "Value is really kukuc: " + v);
|
jaroslav@137
|
281 |
|
jtulach@956
|
282 |
getSetInput("input", "Jardo");
|
jaroslav@141
|
283 |
triggerEvent("input", "change");
|
jaroslav@137
|
284 |
|
jtulach@934
|
285 |
assertEquals("Jardo", m.getName(), "Name property updated: " + m.getName());
|
jaroslav@141
|
286 |
} finally {
|
jaroslav@141
|
287 |
Utils.exposeHTML(KnockoutTest.class, "");
|
jaroslav@141
|
288 |
}
|
jaroslav@34
|
289 |
}
|
jtulach@1055
|
290 |
|
jtulach@809
|
291 |
private static String getSetSelected(int index, Object value) throws Exception {
|
jtulach@809
|
292 |
String s = "var index = arguments[0];\n"
|
jtulach@809
|
293 |
+ "var value = arguments[1];\n"
|
jtulach@809
|
294 |
+ "var n = window.document.getElementById('input'); \n "
|
jtulach@809
|
295 |
+ "if (value != null) {\n"
|
jtulach@809
|
296 |
+ " n.options[index].value = 'me'; \n"
|
jtulach@809
|
297 |
+ " n.value = 'me'; \n"
|
jtulach@809
|
298 |
+ " ko.dataFor(n.options[index]).archetype(value); // haven't found better way to trigger ko change yet \n"
|
jtulach@809
|
299 |
+ "} \n "
|
jtulach@809
|
300 |
+ "var op = n.options[n.selectedIndex]; \n"
|
jtulach@809
|
301 |
+ "return op ? op.text : n.selectedIndex;\n";
|
jtulach@809
|
302 |
Object ret = Utils.executeScript(
|
jtulach@809
|
303 |
KnockoutTest.class,
|
jtulach@809
|
304 |
s, index, value
|
jtulach@809
|
305 |
);
|
jtulach@809
|
306 |
return ret == null ? null : ret.toString();
|
jtulach@809
|
307 |
}
|
jtulach@1055
|
308 |
|
jtulach@809
|
309 |
@Model(className = "ArchetypeData", properties = {
|
jtulach@809
|
310 |
@Property(name = "artifactId", type = String.class),
|
jtulach@809
|
311 |
@Property(name = "groupId", type = String.class),
|
jtulach@809
|
312 |
@Property(name = "version", type = String.class),
|
jtulach@809
|
313 |
@Property(name = "name", type = String.class),
|
jtulach@809
|
314 |
@Property(name = "description", type = String.class),
|
jtulach@809
|
315 |
@Property(name = "url", type = String.class),
|
jtulach@809
|
316 |
})
|
jtulach@809
|
317 |
static class ArchModel {
|
jtulach@809
|
318 |
}
|
jtulach@1055
|
319 |
|
jtulach@809
|
320 |
@KOTest public void selectWorksOnModels() throws Exception {
|
jtulach@809
|
321 |
if (js == null) {
|
jtulach@1055
|
322 |
Utils.exposeHTML(KnockoutTest.class,
|
jtulach@809
|
323 |
"<select id='input' data-bind=\"options: archetypes,\n" +
|
jtulach@809
|
324 |
" optionsText: 'name',\n" +
|
jtulach@809
|
325 |
" value: archetype\">\n" +
|
jtulach@809
|
326 |
" </select>\n" +
|
jtulach@809
|
327 |
""
|
jtulach@809
|
328 |
);
|
jtulach@1055
|
329 |
|
jtulach@809
|
330 |
js = Models.bind(new KnockoutModel(), newContext());
|
jtulach@809
|
331 |
js.getArchetypes().add(new ArchetypeData("ko4j", "org.netbeans.html", "0.8.3", "ko4j", "ko4j", null));
|
jtulach@809
|
332 |
js.getArchetypes().add(new ArchetypeData("crud", "org.netbeans.html", "0.8.3", "crud", "crud", null));
|
jtulach@809
|
333 |
js.getArchetypes().add(new ArchetypeData("3rd", "org.netbeans.html", "0.8.3", "3rd", "3rd", null));
|
jtulach@809
|
334 |
js.setArchetype(js.getArchetypes().get(1));
|
jtulach@809
|
335 |
js.applyBindings();
|
jtulach@1055
|
336 |
|
jtulach@809
|
337 |
String v = getSetSelected(0, null);
|
jtulach@934
|
338 |
assertEquals("crud", v, "Second index (e.g. crud) is selected: " + v);
|
jtulach@1055
|
339 |
|
jtulach@809
|
340 |
String sel = getSetSelected(2, Models.toRaw(js.getArchetypes().get(2)));
|
jtulach@934
|
341 |
assertEquals("3rd", sel, "3rd is selected now: " + sel);
|
jtulach@809
|
342 |
}
|
jtulach@1055
|
343 |
|
jtulach@809
|
344 |
if (js.getArchetype() != js.getArchetypes().get(2)) {
|
jtulach@809
|
345 |
throw new InterruptedException();
|
jtulach@809
|
346 |
}
|
jtulach@1055
|
347 |
|
jtulach@809
|
348 |
Utils.exposeHTML(KnockoutTest.class, "");
|
jtulach@809
|
349 |
}
|
jtulach@990
|
350 |
|
jtulach@990
|
351 |
@KOTest public void nestedObjectEqualsChange() throws Exception {
|
jtulach@990
|
352 |
nestedObjectEqualsChange(true);
|
jtulach@990
|
353 |
}
|
jtulach@990
|
354 |
|
jtulach@990
|
355 |
@KOTest public void nestedObjectChange() throws Exception {
|
jtulach@990
|
356 |
nestedObjectEqualsChange(false);
|
jtulach@990
|
357 |
}
|
jtulach@990
|
358 |
private void nestedObjectEqualsChange(boolean preApply) throws Exception {
|
jtulach@990
|
359 |
Utils.exposeHTML(KnockoutTest.class,
|
jtulach@990
|
360 |
" <div data-bind='with: archetype'>\n" +
|
jtulach@990
|
361 |
" <input id='input' data-bind='value: groupId'></input>\n" +
|
jtulach@990
|
362 |
" </div>\n"
|
jtulach@990
|
363 |
);
|
jtulach@990
|
364 |
|
jtulach@990
|
365 |
js = Models.bind(new KnockoutModel(), newContext());
|
jtulach@990
|
366 |
if (preApply) {
|
jtulach@990
|
367 |
js.applyBindings();
|
jtulach@990
|
368 |
}
|
jtulach@990
|
369 |
js.setArchetype(new ArchetypeData());
|
jtulach@990
|
370 |
js.getArchetype().setGroupId("org.netbeans.html");
|
jtulach@990
|
371 |
js.applyBindings();
|
jtulach@990
|
372 |
|
jtulach@990
|
373 |
String v = getSetInput("input", null);
|
jtulach@990
|
374 |
assertEquals("org.netbeans.html", v, "groupId has been changed");
|
jtulach@990
|
375 |
Utils.exposeHTML(KnockoutTest.class, "");
|
jtulach@990
|
376 |
}
|
jtulach@1055
|
377 |
|
jaroslav@717
|
378 |
@KOTest public void modifyValueAssertAsyncChangeInModel() throws Exception {
|
jaroslav@717
|
379 |
if (js == null) {
|
jtulach@1055
|
380 |
Utils.exposeHTML(KnockoutTest.class,
|
jaroslav@717
|
381 |
"<h1 data-bind=\"text: helloMessage\">Loading Bck2Brwsr's Hello World...</h1>\n" +
|
jaroslav@717
|
382 |
"Your name: <input id='input' data-bind=\"value: name\"></input>\n" +
|
jaroslav@717
|
383 |
"<button id=\"hello\">Say Hello!</button>\n"
|
jaroslav@717
|
384 |
);
|
jtulach@1055
|
385 |
|
jaroslav@717
|
386 |
js = Models.bind(new KnockoutModel(), newContext());
|
jaroslav@717
|
387 |
js.setName("Kukuc");
|
jaroslav@717
|
388 |
js.applyBindings();
|
jtulach@1055
|
389 |
|
jtulach@956
|
390 |
String v = getSetInput("input", null);
|
jtulach@934
|
391 |
assertEquals("Kukuc", v, "Value is really kukuc: " + v);
|
jtulach@1055
|
392 |
|
jaroslav@717
|
393 |
Timer t = new Timer("Set to Jardo");
|
jaroslav@717
|
394 |
t.schedule(new TimerTask() {
|
jaroslav@717
|
395 |
@Override
|
jaroslav@717
|
396 |
public void run() {
|
jaroslav@717
|
397 |
js.setName("Jardo");
|
jaroslav@717
|
398 |
}
|
jaroslav@717
|
399 |
}, 1);
|
jaroslav@717
|
400 |
}
|
jtulach@1055
|
401 |
|
jtulach@956
|
402 |
String v = getSetInput("input", null);
|
jaroslav@717
|
403 |
if (!"Jardo".equals(v)) {
|
jaroslav@717
|
404 |
throw new InterruptedException();
|
jaroslav@717
|
405 |
}
|
jtulach@1055
|
406 |
|
jaroslav@717
|
407 |
Utils.exposeHTML(KnockoutTest.class, "");
|
jaroslav@717
|
408 |
}
|
jtulach@1055
|
409 |
|
jtulach@1055
|
410 |
@Model(className = "ConstantModel", targetId = "", builder = "assign", properties = {
|
jtulach@1055
|
411 |
@Property(name = "doubleValue", mutable = false, type = double.class),
|
jtulach@1055
|
412 |
@Property(name = "longValue", mutable = false, type = long.class),
|
jtulach@1055
|
413 |
@Property(name = "stringValue", mutable = false, type = String.class),
|
jtulach@1055
|
414 |
@Property(name = "boolValue", mutable = false, type = boolean.class),
|
jtulach@1055
|
415 |
@Property(name = "intArray", mutable = false, type = int.class, array = true),
|
jtulach@1055
|
416 |
})
|
jtulach@1055
|
417 |
static class ConstantCntrl {
|
jtulach@1055
|
418 |
}
|
jtulach@1055
|
419 |
|
jtulach@1055
|
420 |
@KOTest public void nonMutableDouble() throws Exception {
|
jtulach@1055
|
421 |
Utils.exposeHTML(KnockoutTest.class,
|
jtulach@1055
|
422 |
"Type: <input id='input' data-bind=\"value: typeof doubleValue\"></input>\n"
|
jtulach@1055
|
423 |
);
|
jtulach@1055
|
424 |
|
jtulach@1055
|
425 |
ConstantModel model = Models.bind(new ConstantModel(), newContext());
|
jtulach@1055
|
426 |
model.assignStringValue("Hello").assignDoubleValue(10.0);
|
jtulach@1055
|
427 |
model.applyBindings();
|
jtulach@1055
|
428 |
|
jtulach@1055
|
429 |
String v = getSetInput("input", null);
|
jtulach@1055
|
430 |
assertEquals(v, "number", "Right type found: " + v);
|
jtulach@1055
|
431 |
|
jtulach@1055
|
432 |
Utils.exposeHTML(KnockoutTest.class, "");
|
jtulach@1055
|
433 |
}
|
jtulach@1055
|
434 |
|
jtulach@1055
|
435 |
@KOTest public void nonMutableString() throws Exception {
|
jtulach@1055
|
436 |
Utils.exposeHTML(KnockoutTest.class,
|
jtulach@1055
|
437 |
"Type: <input id='input' data-bind=\"value: typeof stringValue\"></input>\n"
|
jtulach@1055
|
438 |
);
|
jtulach@1055
|
439 |
|
jtulach@1055
|
440 |
ConstantModel model = Models.bind(new ConstantModel(), newContext());
|
jtulach@1055
|
441 |
model.assignStringValue("Hello").assignDoubleValue(10.0);
|
jtulach@1055
|
442 |
model.applyBindings();
|
jtulach@1055
|
443 |
|
jtulach@1055
|
444 |
String v = getSetInput("input", null);
|
jtulach@1055
|
445 |
assertEquals(v, "string", "Right type found: " + v);
|
jtulach@1055
|
446 |
|
jtulach@1055
|
447 |
Utils.exposeHTML(KnockoutTest.class, "");
|
jtulach@1055
|
448 |
}
|
jtulach@1055
|
449 |
|
jtulach@1055
|
450 |
@KOTest public void nonMutableBoolean() throws Exception {
|
jtulach@1055
|
451 |
Utils.exposeHTML(KnockoutTest.class,
|
jtulach@1055
|
452 |
"Type: <input id='input' data-bind=\"value: typeof boolValue\"></input>\n"
|
jtulach@1055
|
453 |
);
|
jtulach@1055
|
454 |
|
jtulach@1055
|
455 |
ConstantModel model = Models.bind(new ConstantModel(), newContext());
|
jtulach@1055
|
456 |
model.assignStringValue("Hello").assignBoolValue(true);
|
jtulach@1055
|
457 |
model.applyBindings();
|
jtulach@1055
|
458 |
|
jtulach@1055
|
459 |
String v = getSetInput("input", null);
|
jtulach@1055
|
460 |
assertEquals(v, "boolean", "Right type found: " + v);
|
jtulach@1055
|
461 |
|
jtulach@1055
|
462 |
Utils.exposeHTML(KnockoutTest.class, "");
|
jtulach@1055
|
463 |
}
|
jtulach@1055
|
464 |
|
jtulach@1055
|
465 |
@KOTest public void nonMutableLong() throws Exception {
|
jtulach@1055
|
466 |
Utils.exposeHTML(KnockoutTest.class,
|
jtulach@1055
|
467 |
"Type: <input id='input' data-bind=\"value: typeof longValue\"></input>\n"
|
jtulach@1055
|
468 |
);
|
jtulach@1055
|
469 |
|
jtulach@1055
|
470 |
ConstantModel model = Models.bind(new ConstantModel(), newContext());
|
jtulach@1055
|
471 |
model.assignStringValue("Hello").assignLongValue(Long.MAX_VALUE);
|
jtulach@1055
|
472 |
model.applyBindings();
|
jtulach@1055
|
473 |
|
jtulach@1055
|
474 |
String v = getSetInput("input", null);
|
jtulach@1055
|
475 |
assertEquals(v, "number", "Right type found: " + v);
|
jtulach@1055
|
476 |
|
jtulach@1055
|
477 |
Utils.exposeHTML(KnockoutTest.class, "");
|
jtulach@1055
|
478 |
}
|
jtulach@1055
|
479 |
|
jtulach@1055
|
480 |
@KOTest public void nonMutableIntArray() throws Exception {
|
jtulach@1055
|
481 |
Utils.exposeHTML(KnockoutTest.class,
|
jtulach@1055
|
482 |
"Type: <input id='input' data-bind=\"value: typeof intArray\"></input>\n"
|
jtulach@1055
|
483 |
);
|
jtulach@1055
|
484 |
|
jtulach@1055
|
485 |
ConstantModel model = Models.bind(new ConstantModel(), newContext());
|
jtulach@1055
|
486 |
model.assignStringValue("Hello").assignLongValue(Long.MAX_VALUE).assignIntArray(1, 2, 3, 4);
|
jtulach@1055
|
487 |
model.applyBindings();
|
jtulach@1055
|
488 |
|
jtulach@1055
|
489 |
String v = getSetInput("input", null);
|
jtulach@1055
|
490 |
assertEquals(v, "object", "Right type found: " + v);
|
jtulach@1055
|
491 |
|
jtulach@1055
|
492 |
Utils.exposeHTML(KnockoutTest.class, "");
|
jtulach@1055
|
493 |
}
|
jtulach@1055
|
494 |
|
jtulach@956
|
495 |
private static String getSetInput(String id, String value) throws Exception {
|
jaroslav@36
|
496 |
String s = "var value = arguments[0];\n"
|
jtulach@956
|
497 |
+ "var n = window.document.getElementById(arguments[1]); \n "
|
jaroslav@34
|
498 |
+ "if (value != null) n['value'] = value; \n "
|
jaroslav@36
|
499 |
+ "return n['value'];";
|
jtulach@692
|
500 |
Object ret = Utils.executeScript(
|
jaroslav@121
|
501 |
KnockoutTest.class,
|
jtulach@956
|
502 |
s, value, id
|
jaroslav@121
|
503 |
);
|
jtulach@692
|
504 |
return ret == null ? null : ret.toString();
|
jaroslav@34
|
505 |
}
|
jtulach@956
|
506 |
|
jtulach@956
|
507 |
private static boolean isChecked(String id) throws Exception {
|
jtulach@956
|
508 |
String s = ""
|
jtulach@956
|
509 |
+ "var n = window.document.getElementById(arguments[0]); \n "
|
jtulach@956
|
510 |
+ "return n['checked'];";
|
jtulach@956
|
511 |
Object ret = Utils.executeScript(
|
jtulach@956
|
512 |
KnockoutTest.class,
|
jtulach@956
|
513 |
s, id
|
jtulach@956
|
514 |
);
|
jtulach@956
|
515 |
return Boolean.TRUE.equals(ret);
|
jtulach@956
|
516 |
}
|
jtulach@1055
|
517 |
|
jaroslav@36
|
518 |
public static void triggerEvent(String id, String ev) throws Exception {
|
jaroslav@36
|
519 |
Utils.executeScript(
|
jaroslav@121
|
520 |
KnockoutTest.class,
|
jaroslav@36
|
521 |
"ko.utils.triggerEvent(window.document.getElementById(arguments[0]), arguments[1]);",
|
jaroslav@36
|
522 |
id, ev
|
jaroslav@36
|
523 |
);
|
jaroslav@34
|
524 |
}
|
jtulach@1055
|
525 |
|
jaroslav@137
|
526 |
@KOTest public void displayContentOfArray() throws Exception {
|
jtulach@1055
|
527 |
Object exp = Utils.exposeHTML(KnockoutTest.class,
|
jaroslav@137
|
528 |
"<ul id='ul' data-bind='foreach: results'>\n"
|
jaroslav@137
|
529 |
+ " <li data-bind='text: $data, click: $root.call'/>\n"
|
jaroslav@137
|
530 |
+ "</ul>\n"
|
jaroslav@137
|
531 |
);
|
jaroslav@141
|
532 |
try {
|
jaroslav@141
|
533 |
KnockoutModel m = Models.bind(new KnockoutModel(), newContext());
|
jaroslav@141
|
534 |
m.getResults().add("Ahoj");
|
jaroslav@141
|
535 |
m.applyBindings();
|
jaroslav@137
|
536 |
|
jtulach@681
|
537 |
int cnt = Utils.countChildren(KnockoutTest.class, "ul");
|
jtulach@934
|
538 |
assertEquals(cnt, 1, "One child, but was " + cnt);
|
jaroslav@137
|
539 |
|
jaroslav@141
|
540 |
m.getResults().add("Hi");
|
jaroslav@137
|
541 |
|
jtulach@681
|
542 |
cnt = Utils.countChildren(KnockoutTest.class, "ul");
|
jtulach@934
|
543 |
assertEquals(cnt, 2, "Two children now, but was " + cnt);
|
jaroslav@34
|
544 |
|
jaroslav@141
|
545 |
triggerChildClick("ul", 1);
|
jaroslav@137
|
546 |
|
jtulach@934
|
547 |
assertEquals(1, m.getCallbackCount(), "One callback " + m.getCallbackCount());
|
jtulach@934
|
548 |
assertEquals("Hi", m.getName(), "We got callback from 2nd child " + m.getName());
|
jaroslav@141
|
549 |
} finally {
|
jaroslav@141
|
550 |
Utils.exposeHTML(KnockoutTest.class, "");
|
jaroslav@141
|
551 |
}
|
jaroslav@34
|
552 |
}
|
jtulach@1055
|
553 |
|
jaroslav@719
|
554 |
@KOTest public void displayContentOfAsyncArray() throws Exception {
|
jaroslav@719
|
555 |
if (js == null) {
|
jtulach@1055
|
556 |
Utils.exposeHTML(KnockoutTest.class,
|
jaroslav@719
|
557 |
"<ul id='ul' data-bind='foreach: results'>\n"
|
jaroslav@719
|
558 |
+ " <li data-bind='text: $data, click: $root.call'/>\n"
|
jaroslav@719
|
559 |
+ "</ul>\n"
|
jaroslav@719
|
560 |
);
|
jaroslav@719
|
561 |
js = Models.bind(new KnockoutModel(), newContext());
|
jaroslav@719
|
562 |
js.getResults().add("Ahoj");
|
jaroslav@719
|
563 |
js.applyBindings();
|
jaroslav@719
|
564 |
|
jaroslav@719
|
565 |
int cnt = Utils.countChildren(KnockoutTest.class, "ul");
|
jtulach@934
|
566 |
assertEquals(cnt, 1, "One child, but was " + cnt);
|
jtulach@1055
|
567 |
|
jaroslav@719
|
568 |
Timer t = new Timer("add to array");
|
jaroslav@719
|
569 |
t.schedule(new TimerTask() {
|
jaroslav@719
|
570 |
@Override
|
jaroslav@719
|
571 |
public void run() {
|
jaroslav@719
|
572 |
js.getResults().add("Hi");
|
jaroslav@719
|
573 |
}
|
jaroslav@719
|
574 |
}, 1);
|
jaroslav@719
|
575 |
}
|
jaroslav@719
|
576 |
|
jaroslav@719
|
577 |
|
jaroslav@719
|
578 |
int cnt = Utils.countChildren(KnockoutTest.class, "ul");
|
jaroslav@719
|
579 |
if (cnt != 2) {
|
jaroslav@719
|
580 |
throw new InterruptedException();
|
jaroslav@719
|
581 |
}
|
jaroslav@719
|
582 |
|
jaroslav@719
|
583 |
try {
|
jaroslav@719
|
584 |
triggerChildClick("ul", 1);
|
jaroslav@719
|
585 |
|
jtulach@934
|
586 |
assertEquals(1, js.getCallbackCount(), "One callback " + js.getCallbackCount());
|
jtulach@934
|
587 |
assertEquals("Hi", js.getName(), "We got callback from 2nd child " + js.getName());
|
jaroslav@719
|
588 |
} finally {
|
jaroslav@719
|
589 |
Utils.exposeHTML(KnockoutTest.class, "");
|
jaroslav@719
|
590 |
}
|
jaroslav@719
|
591 |
}
|
jtulach@1055
|
592 |
|
jaroslav@235
|
593 |
@KOTest public void displayContentOfComputedArray() throws Exception {
|
jtulach@1055
|
594 |
Object exp = Utils.exposeHTML(KnockoutTest.class,
|
jaroslav@235
|
595 |
"<ul id='ul' data-bind='foreach: bothNames'>\n"
|
jaroslav@235
|
596 |
+ " <li data-bind='text: $data, click: $root.assignFirstName'/>\n"
|
jaroslav@235
|
597 |
+ "</ul>\n"
|
jaroslav@235
|
598 |
);
|
jaroslav@235
|
599 |
try {
|
jaroslav@236
|
600 |
Pair m = Models.bind(new Pair("First", "Last", null), newContext());
|
jaroslav@236
|
601 |
m.applyBindings();
|
jaroslav@236
|
602 |
|
jtulach@681
|
603 |
int cnt = Utils.countChildren(KnockoutTest.class, "ul");
|
jtulach@934
|
604 |
assertEquals(cnt, 2, "Two children now, but was " + cnt);
|
jaroslav@236
|
605 |
|
jaroslav@236
|
606 |
triggerChildClick("ul", 1);
|
jaroslav@236
|
607 |
|
jtulach@934
|
608 |
assertEquals("Last", m.getFirstName(), "We got callback from 2nd child " + m.getFirstName());
|
jtulach@1055
|
609 |
|
jtulach@626
|
610 |
m.setLastName("Verylast");
|
jtulach@626
|
611 |
|
jtulach@681
|
612 |
cnt = Utils.countChildren(KnockoutTest.class, "ul");
|
jtulach@934
|
613 |
assertEquals(cnt, 2, "Two children now, but was " + cnt);
|
jtulach@1055
|
614 |
|
jtulach@626
|
615 |
triggerChildClick("ul", 1);
|
jtulach@626
|
616 |
|
jtulach@934
|
617 |
assertEquals("Verylast", m.getFirstName(), "We got callback from 2nd child " + m.getFirstName());
|
jtulach@1055
|
618 |
|
jaroslav@236
|
619 |
} finally {
|
jaroslav@236
|
620 |
Utils.exposeHTML(KnockoutTest.class, "");
|
jaroslav@236
|
621 |
}
|
jaroslav@236
|
622 |
}
|
jtulach@1055
|
623 |
|
jaroslav@236
|
624 |
@KOTest public void displayContentOfComputedArrayOnASubpair() throws Exception {
|
jtulach@1055
|
625 |
Object exp = Utils.exposeHTML(KnockoutTest.class,
|
jaroslav@236
|
626 |
"<div data-bind='with: next'>\n"
|
jaroslav@236
|
627 |
+ "<ul id='ul' data-bind='foreach: bothNames'>\n"
|
jaroslav@236
|
628 |
+ " <li data-bind='text: $data, click: $root.assignFirstName'/>\n"
|
jaroslav@236
|
629 |
+ "</ul>"
|
jaroslav@236
|
630 |
+ "</div>\n"
|
jaroslav@236
|
631 |
);
|
jaroslav@236
|
632 |
try {
|
jtulach@569
|
633 |
final BrwsrCtx ctx = newContext();
|
jtulach@569
|
634 |
Pair m = Models.bind(new Pair(null, null, new Pair("First", "Last", null)), ctx);
|
jaroslav@236
|
635 |
m.applyBindings();
|
jaroslav@236
|
636 |
|
jtulach@681
|
637 |
int cnt = Utils.countChildren(KnockoutTest.class, "ul");
|
jtulach@934
|
638 |
assertEquals(cnt, 2, "Two children now, but was " + cnt);
|
jaroslav@236
|
639 |
|
jaroslav@236
|
640 |
triggerChildClick("ul", 1);
|
jtulach@1055
|
641 |
|
jtulach@934
|
642 |
assertEquals(PairModel.ctx, ctx, "Context remains the same");
|
jaroslav@236
|
643 |
|
jtulach@934
|
644 |
assertEquals("Last", m.getFirstName(), "We got callback from 2nd child " + m.getFirstName());
|
jaroslav@236
|
645 |
} finally {
|
jaroslav@236
|
646 |
Utils.exposeHTML(KnockoutTest.class, "");
|
jaroslav@236
|
647 |
}
|
jaroslav@236
|
648 |
}
|
jtulach@1055
|
649 |
|
jaroslav@236
|
650 |
@KOTest public void displayContentOfComputedArrayOnComputedASubpair() throws Exception {
|
jtulach@1055
|
651 |
Object exp = Utils.exposeHTML(KnockoutTest.class,
|
jaroslav@236
|
652 |
"<div data-bind='with: nextOne'>\n"
|
jaroslav@236
|
653 |
+ "<ul id='ul' data-bind='foreach: bothNames'>\n"
|
jaroslav@236
|
654 |
+ " <li data-bind='text: $data, click: $root.assignFirstName'/>\n"
|
jaroslav@236
|
655 |
+ "</ul>"
|
jaroslav@236
|
656 |
+ "</div>\n"
|
jaroslav@236
|
657 |
);
|
jaroslav@236
|
658 |
try {
|
jaroslav@236
|
659 |
Pair m = Models.bind(new Pair(null, null, new Pair("First", "Last", null)), newContext());
|
jaroslav@235
|
660 |
m.applyBindings();
|
jaroslav@235
|
661 |
|
jtulach@681
|
662 |
int cnt = Utils.countChildren(KnockoutTest.class, "ul");
|
jtulach@934
|
663 |
assertEquals(cnt, 2, "Two children now, but was " + cnt);
|
jaroslav@235
|
664 |
|
jaroslav@235
|
665 |
triggerChildClick("ul", 1);
|
jaroslav@235
|
666 |
|
jtulach@934
|
667 |
assertEquals("Last", m.getFirstName(), "We got callback from 2nd child " + m.getFirstName());
|
jaroslav@235
|
668 |
} finally {
|
jaroslav@235
|
669 |
Utils.exposeHTML(KnockoutTest.class, "");
|
jaroslav@235
|
670 |
}
|
jaroslav@235
|
671 |
}
|
jaroslav@94
|
672 |
|
jaroslav@137
|
673 |
@KOTest public void checkBoxToBooleanBinding() throws Exception {
|
jtulach@1055
|
674 |
Object exp = Utils.exposeHTML(KnockoutTest.class,
|
jaroslav@137
|
675 |
"<input type='checkbox' id='b' data-bind='checked: enabled'></input>\n"
|
jaroslav@137
|
676 |
);
|
jaroslav@141
|
677 |
try {
|
jaroslav@141
|
678 |
KnockoutModel m = Models.bind(new KnockoutModel(), newContext());
|
jaroslav@141
|
679 |
m.applyBindings();
|
jaroslav@137
|
680 |
|
jtulach@934
|
681 |
assertFalse(m.isEnabled(), "Is disabled");
|
jaroslav@137
|
682 |
|
jaroslav@141
|
683 |
triggerClick("b");
|
jaroslav@94
|
684 |
|
jtulach@934
|
685 |
assertTrue(m.isEnabled(), "Now the model is enabled");
|
jaroslav@141
|
686 |
} finally {
|
jaroslav@141
|
687 |
Utils.exposeHTML(KnockoutTest.class, "");
|
jaroslav@141
|
688 |
}
|
jaroslav@94
|
689 |
}
|
jtulach@1055
|
690 |
|
jtulach@1055
|
691 |
|
jtulach@1055
|
692 |
|
jaroslav@137
|
693 |
@KOTest public void displayContentOfDerivedArray() throws Exception {
|
jtulach@1055
|
694 |
Object exp = Utils.exposeHTML(KnockoutTest.class,
|
jaroslav@137
|
695 |
"<ul id='ul' data-bind='foreach: cmpResults'>\n"
|
jaroslav@137
|
696 |
+ " <li><b data-bind='text: $data'></b></li>\n"
|
jaroslav@137
|
697 |
+ "</ul>\n"
|
jaroslav@137
|
698 |
);
|
jaroslav@141
|
699 |
try {
|
jaroslav@141
|
700 |
KnockoutModel m = Models.bind(new KnockoutModel(), newContext());
|
jaroslav@141
|
701 |
m.getResults().add("Ahoj");
|
jaroslav@141
|
702 |
m.applyBindings();
|
jaroslav@137
|
703 |
|
jtulach@681
|
704 |
int cnt = Utils.countChildren(KnockoutTest.class, "ul");
|
jtulach@934
|
705 |
assertEquals(cnt, 1, "One child, but was " + cnt);
|
jaroslav@137
|
706 |
|
jaroslav@141
|
707 |
m.getResults().add("hello");
|
jaroslav@137
|
708 |
|
jtulach@681
|
709 |
cnt = Utils.countChildren(KnockoutTest.class, "ul");
|
jtulach@934
|
710 |
assertEquals(cnt, 2, "Two children now, but was " + cnt);
|
jaroslav@141
|
711 |
} finally {
|
jaroslav@141
|
712 |
Utils.exposeHTML(KnockoutTest.class, "");
|
jaroslav@141
|
713 |
}
|
jaroslav@34
|
714 |
}
|
jtulach@1055
|
715 |
|
jaroslav@137
|
716 |
@KOTest public void displayContentOfArrayOfPeople() throws Exception {
|
jtulach@1055
|
717 |
Object exp = Utils.exposeHTML(KnockoutTest.class,
|
jaroslav@137
|
718 |
"<ul id='ul' data-bind='foreach: people'>\n"
|
jaroslav@137
|
719 |
+ " <li data-bind='text: $data.firstName, click: $root.removePerson'></li>\n"
|
jaroslav@137
|
720 |
+ "</ul>\n"
|
jaroslav@137
|
721 |
);
|
jaroslav@141
|
722 |
try {
|
jaroslav@424
|
723 |
final BrwsrCtx c = newContext();
|
jaroslav@424
|
724 |
KnockoutModel m = Models.bind(new KnockoutModel(), c);
|
jaroslav@137
|
725 |
|
jaroslav@424
|
726 |
final Person first = Models.bind(new Person(), c);
|
jaroslav@141
|
727 |
first.setFirstName("first");
|
jaroslav@141
|
728 |
m.getPeople().add(first);
|
jaroslav@137
|
729 |
|
jaroslav@141
|
730 |
m.applyBindings();
|
jaroslav@137
|
731 |
|
jtulach@681
|
732 |
int cnt = Utils.countChildren(KnockoutTest.class, "ul");
|
jtulach@934
|
733 |
assertEquals(cnt, 1, "One child, but was " + cnt);
|
jaroslav@137
|
734 |
|
jaroslav@424
|
735 |
final Person second = Models.bind(new Person(), c);
|
jaroslav@141
|
736 |
second.setFirstName("second");
|
jaroslav@141
|
737 |
m.getPeople().add(second);
|
jaroslav@137
|
738 |
|
jtulach@681
|
739 |
cnt = Utils.countChildren(KnockoutTest.class, "ul");
|
jtulach@934
|
740 |
assertEquals(cnt, 2, "Two children now, but was " + cnt);
|
jaroslav@34
|
741 |
|
jaroslav@141
|
742 |
triggerChildClick("ul", 1);
|
jaroslav@34
|
743 |
|
jtulach@934
|
744 |
assertEquals(1, m.getCallbackCount(), "One callback " + m.getCallbackCount());
|
jaroslav@137
|
745 |
|
jtulach@681
|
746 |
cnt = Utils.countChildren(KnockoutTest.class, "ul");
|
jtulach@934
|
747 |
assertEquals(cnt , 1, "Again one child, but was " + cnt);
|
jaroslav@34
|
748 |
|
jaroslav@141
|
749 |
String txt = childText("ul", 0);
|
jtulach@934
|
750 |
assertEquals("first", txt, "Expecting 'first': " + txt);
|
jaroslav@137
|
751 |
|
jaroslav@141
|
752 |
first.setFirstName("changed");
|
jaroslav@137
|
753 |
|
jaroslav@141
|
754 |
txt = childText("ul", 0);
|
jtulach@934
|
755 |
assertEquals("changed", txt, "Expecting 'changed': " + txt);
|
jaroslav@141
|
756 |
} finally {
|
jaroslav@141
|
757 |
Utils.exposeHTML(KnockoutTest.class, "");
|
jaroslav@141
|
758 |
}
|
jaroslav@34
|
759 |
}
|
jtulach@1055
|
760 |
|
jaroslav@34
|
761 |
@ComputedProperty
|
jaroslav@34
|
762 |
static Person firstPerson(List<Person> people) {
|
jaroslav@34
|
763 |
return people.isEmpty() ? null : people.get(0);
|
jaroslav@34
|
764 |
}
|
jtulach@1055
|
765 |
|
jaroslav@137
|
766 |
@KOTest public void accessFirstPersonWithOnFunction() throws Exception {
|
jtulach@1055
|
767 |
Object exp = Utils.exposeHTML(KnockoutTest.class,
|
jaroslav@137
|
768 |
"<p id='ul' data-bind='with: firstPerson'>\n"
|
jaroslav@137
|
769 |
+ " <span data-bind='text: firstName, click: changeSex'></span>\n"
|
jaroslav@137
|
770 |
+ "</p>\n"
|
jaroslav@137
|
771 |
);
|
jaroslav@141
|
772 |
try {
|
jaroslav@141
|
773 |
trasfertToFemale();
|
jaroslav@141
|
774 |
} finally {
|
jaroslav@141
|
775 |
Utils.exposeHTML(KnockoutTest.class, "");
|
jaroslav@141
|
776 |
}
|
jaroslav@34
|
777 |
}
|
jtulach@1055
|
778 |
|
jaroslav@137
|
779 |
@KOTest public void onPersonFunction() throws Exception {
|
jtulach@1055
|
780 |
Object exp = Utils.exposeHTML(KnockoutTest.class,
|
jaroslav@137
|
781 |
"<ul id='ul' data-bind='foreach: people'>\n"
|
jaroslav@137
|
782 |
+ " <li data-bind='text: $data.firstName, click: changeSex'></li>\n"
|
jaroslav@137
|
783 |
+ "</ul>\n"
|
jaroslav@137
|
784 |
);
|
jaroslav@141
|
785 |
try {
|
jaroslav@141
|
786 |
trasfertToFemale();
|
jaroslav@141
|
787 |
} finally {
|
jaroslav@141
|
788 |
Utils.exposeHTML(KnockoutTest.class, "");
|
jaroslav@141
|
789 |
}
|
jaroslav@34
|
790 |
}
|
jtulach@1055
|
791 |
|
jaroslav@36
|
792 |
private void trasfertToFemale() throws Exception {
|
jaroslav@121
|
793 |
KnockoutModel m = Models.bind(new KnockoutModel(), newContext());
|
jaroslav@34
|
794 |
|
jaroslav@121
|
795 |
final Person first = Models.bind(new Person(), newContext());
|
jaroslav@34
|
796 |
first.setFirstName("first");
|
jaroslav@34
|
797 |
first.setSex(Sex.MALE);
|
jaroslav@34
|
798 |
m.getPeople().add(first);
|
jaroslav@34
|
799 |
|
jaroslav@34
|
800 |
|
jaroslav@34
|
801 |
m.applyBindings();
|
jaroslav@34
|
802 |
|
jtulach@681
|
803 |
int cnt = Utils.countChildren(KnockoutTest.class, "ul");
|
jtulach@934
|
804 |
assertEquals(cnt, 1, "One child, but was " + cnt);
|
jaroslav@34
|
805 |
|
jaroslav@34
|
806 |
|
jaroslav@34
|
807 |
triggerChildClick("ul", 0);
|
jaroslav@34
|
808 |
|
jtulach@934
|
809 |
assertEquals(first.getSex(), Sex.FEMALE, "Transverted to female: " + first.getSex());
|
jaroslav@34
|
810 |
}
|
jtulach@1055
|
811 |
|
jtulach@824
|
812 |
@KOTest public void stringArrayModificationVisible() throws Exception {
|
jtulach@824
|
813 |
Object exp = Utils.exposeHTML(KnockoutTest.class,
|
jtulach@824
|
814 |
"<div>\n"
|
jtulach@824
|
815 |
+ "<ul id='ul' data-bind='foreach: results'>\n"
|
jtulach@824
|
816 |
+ " <li data-bind='text: $data'></li>\n"
|
jtulach@824
|
817 |
+ "</ul>\n"
|
jtulach@824
|
818 |
+ "</div>\n"
|
jtulach@824
|
819 |
);
|
jtulach@824
|
820 |
try {
|
jtulach@824
|
821 |
KnockoutModel m = Models.bind(new KnockoutModel(), newContext());
|
jtulach@824
|
822 |
m.getResults().add("Ahoj");
|
jtulach@824
|
823 |
m.getResults().add("Hello");
|
jtulach@824
|
824 |
m.applyBindings();
|
jtulach@1055
|
825 |
|
jtulach@824
|
826 |
int cnt = Utils.countChildren(KnockoutTest.class, "ul");
|
jtulach@934
|
827 |
assertEquals(cnt, 2, "Two children " + cnt);
|
jtulach@1055
|
828 |
|
jtulach@825
|
829 |
Object arr = Utils.addChildren(KnockoutTest.class, "ul", "results", "Hi");
|
jtulach@934
|
830 |
assertTrue(arr instanceof Object[], "Got back an array: " + arr);
|
jtulach@824
|
831 |
final int len = ((Object[])arr).length;
|
jtulach@1055
|
832 |
|
jtulach@934
|
833 |
assertEquals(len, 3, "Three elements in the array " + len);
|
jtulach@1055
|
834 |
|
jtulach@824
|
835 |
int newCnt = Utils.countChildren(KnockoutTest.class, "ul");
|
jtulach@934
|
836 |
assertEquals(newCnt, 3, "Three children in the DOM: " + newCnt);
|
jtulach@1055
|
837 |
|
jtulach@934
|
838 |
assertEquals(m.getResults().size(), 3, "Three java strings: " + m.getResults());
|
jtulach@824
|
839 |
} finally {
|
jtulach@824
|
840 |
Utils.exposeHTML(KnockoutTest.class, "");
|
jtulach@824
|
841 |
}
|
jtulach@824
|
842 |
}
|
jtulach@830
|
843 |
|
jtulach@825
|
844 |
@KOTest public void intArrayModificationVisible() throws Exception {
|
jtulach@825
|
845 |
Object exp = Utils.exposeHTML(KnockoutTest.class,
|
jtulach@825
|
846 |
"<div>\n"
|
jtulach@825
|
847 |
+ "<ul id='ul' data-bind='foreach: numbers'>\n"
|
jtulach@825
|
848 |
+ " <li data-bind='text: $data'></li>\n"
|
jtulach@825
|
849 |
+ "</ul>\n"
|
jtulach@825
|
850 |
+ "</div>\n"
|
jtulach@825
|
851 |
);
|
jtulach@825
|
852 |
try {
|
jtulach@825
|
853 |
KnockoutModel m = Models.bind(new KnockoutModel(), newContext());
|
jtulach@825
|
854 |
m.getNumbers().add(1);
|
jtulach@825
|
855 |
m.getNumbers().add(31);
|
jtulach@825
|
856 |
m.applyBindings();
|
jtulach@1055
|
857 |
|
jtulach@825
|
858 |
int cnt = Utils.countChildren(KnockoutTest.class, "ul");
|
jtulach@934
|
859 |
assertEquals(cnt, 2, "Two children " + cnt);
|
jtulach@1055
|
860 |
|
jtulach@825
|
861 |
Object arr = Utils.addChildren(KnockoutTest.class, "ul", "numbers", 42);
|
jtulach@934
|
862 |
assertTrue(arr instanceof Object[], "Got back an array: " + arr);
|
jtulach@825
|
863 |
final int len = ((Object[])arr).length;
|
jtulach@1055
|
864 |
|
jtulach@934
|
865 |
assertEquals(len, 3, "Three elements in the array " + len);
|
jtulach@1055
|
866 |
|
jtulach@825
|
867 |
int newCnt = Utils.countChildren(KnockoutTest.class, "ul");
|
jtulach@934
|
868 |
assertEquals(newCnt, 3, "Three children in the DOM: " + newCnt);
|
jtulach@1055
|
869 |
|
jtulach@934
|
870 |
assertEquals(m.getNumbers().size(), 3, "Three java ints: " + m.getNumbers());
|
jtulach@934
|
871 |
assertEquals(m.getNumbers().get(2), 42, "Meaning of world: " + m.getNumbers());
|
jtulach@825
|
872 |
} finally {
|
jtulach@825
|
873 |
Utils.exposeHTML(KnockoutTest.class, "");
|
jtulach@825
|
874 |
}
|
jtulach@825
|
875 |
}
|
jtulach@830
|
876 |
|
jtulach@830
|
877 |
@KOTest public void derivedIntArrayModificationVisible() throws Exception {
|
jtulach@830
|
878 |
Object exp = Utils.exposeHTML(KnockoutTest.class,
|
jtulach@830
|
879 |
"<div>\n"
|
jtulach@830
|
880 |
+ "<ul id='ul' data-bind='foreach: resultLengths'>\n"
|
jtulach@830
|
881 |
+ " <li data-bind='text: $data'></li>\n"
|
jtulach@830
|
882 |
+ "</ul>\n"
|
jtulach@830
|
883 |
+ "</div>\n"
|
jtulach@830
|
884 |
);
|
jtulach@830
|
885 |
try {
|
jtulach@830
|
886 |
KnockoutModel m = Models.bind(new KnockoutModel(), newContext());
|
jtulach@830
|
887 |
m.getResults().add("Ahoj");
|
jtulach@830
|
888 |
m.getResults().add("Hello");
|
jtulach@830
|
889 |
m.applyBindings();
|
jtulach@1055
|
890 |
|
jtulach@830
|
891 |
int cnt = Utils.countChildren(KnockoutTest.class, "ul");
|
jtulach@934
|
892 |
assertEquals(cnt, 2, "Two children " + cnt);
|
jtulach@1055
|
893 |
|
jtulach@830
|
894 |
Object arr = Utils.addChildren(KnockoutTest.class, "ul", "results", "Hi");
|
jtulach@934
|
895 |
assertTrue(arr instanceof Object[], "Got back an array: " + arr);
|
jtulach@830
|
896 |
final int len = ((Object[])arr).length;
|
jtulach@1055
|
897 |
|
jtulach@934
|
898 |
assertEquals(len, 3, "Three elements in the array " + len);
|
jtulach@1055
|
899 |
|
jtulach@830
|
900 |
int newCnt = Utils.countChildren(KnockoutTest.class, "ul");
|
jtulach@934
|
901 |
assertEquals(newCnt, 3, "Three children in the DOM: " + newCnt);
|
jtulach@1055
|
902 |
|
jtulach@934
|
903 |
assertEquals(m.getResultLengths().size(), 3, "Three java ints: " + m.getResultLengths());
|
jtulach@934
|
904 |
assertEquals(m.getResultLengths().get(2), 2, "Size is two: " + m.getResultLengths());
|
jtulach@830
|
905 |
} finally {
|
jtulach@830
|
906 |
Utils.exposeHTML(KnockoutTest.class, "");
|
jtulach@830
|
907 |
}
|
jtulach@830
|
908 |
}
|
jtulach@1055
|
909 |
|
jtulach@826
|
910 |
@KOTest public void archetypeArrayModificationVisible() throws Exception {
|
jtulach@826
|
911 |
Object exp = Utils.exposeHTML(KnockoutTest.class,
|
jtulach@826
|
912 |
"<div>\n"
|
jtulach@826
|
913 |
+ "<ul id='ul' data-bind='foreach: archetypes'>\n"
|
jtulach@826
|
914 |
+ " <li data-bind='text: artifactId'></li>\n"
|
jtulach@826
|
915 |
+ "</ul>\n"
|
jtulach@826
|
916 |
+ "</div>\n"
|
jtulach@826
|
917 |
);
|
jtulach@826
|
918 |
try {
|
jtulach@826
|
919 |
KnockoutModel m = Models.bind(new KnockoutModel(), newContext());
|
jtulach@826
|
920 |
m.applyBindings();
|
jtulach@1055
|
921 |
|
jtulach@826
|
922 |
int cnt = Utils.countChildren(KnockoutTest.class, "ul");
|
jtulach@934
|
923 |
assertEquals(cnt, 0, "No children " + cnt);
|
jtulach@1055
|
924 |
|
jtulach@826
|
925 |
Object arr = Utils.addChildren(KnockoutTest.class, "ul", "archetypes", new ArchetypeData("aid", "gid", "v", "n", "d", "u"));
|
jtulach@934
|
926 |
assertTrue(arr instanceof Object[], "Got back an array: " + arr);
|
jtulach@826
|
927 |
final int len = ((Object[])arr).length;
|
jtulach@1055
|
928 |
|
jtulach@934
|
929 |
assertEquals(len, 1, "One element in the array " + len);
|
jtulach@1055
|
930 |
|
jtulach@826
|
931 |
int newCnt = Utils.countChildren(KnockoutTest.class, "ul");
|
jtulach@934
|
932 |
assertEquals(newCnt, 1, "One child in the DOM: " + newCnt);
|
jtulach@1055
|
933 |
|
jtulach@934
|
934 |
assertEquals(m.getArchetypes().size(), 1, "One archetype: " + m.getArchetypes());
|
jtulach@934
|
935 |
assertNotNull(m.getArchetypes().get(0), "Not null: " + m.getArchetypes());
|
jtulach@934
|
936 |
assertEquals(m.getArchetypes().get(0).getArtifactId(), "aid", "'aid' == " + m.getArchetypes());
|
jtulach@826
|
937 |
} finally {
|
jtulach@826
|
938 |
Utils.exposeHTML(KnockoutTest.class, "");
|
jtulach@826
|
939 |
}
|
jtulach@826
|
940 |
}
|
jtulach@824
|
941 |
|
jaroslav@34
|
942 |
@Function
|
jaroslav@34
|
943 |
static void call(KnockoutModel m, String data) {
|
jaroslav@34
|
944 |
m.setName(data);
|
jaroslav@34
|
945 |
m.setCallbackCount(m.getCallbackCount() + 1);
|
jaroslav@34
|
946 |
}
|
jaroslav@34
|
947 |
|
jaroslav@34
|
948 |
@Function
|
jaroslav@34
|
949 |
static void removePerson(KnockoutModel model, Person data) {
|
jaroslav@34
|
950 |
model.setCallbackCount(model.getCallbackCount() + 1);
|
jaroslav@34
|
951 |
model.getPeople().remove(data);
|
jaroslav@34
|
952 |
}
|
jtulach@1055
|
953 |
|
jtulach@1055
|
954 |
|
jaroslav@34
|
955 |
@ComputedProperty
|
jaroslav@34
|
956 |
static String helloMessage(String name) {
|
jaroslav@34
|
957 |
return "Hello " + name + "!";
|
jaroslav@34
|
958 |
}
|
jtulach@1055
|
959 |
|
jaroslav@34
|
960 |
@ComputedProperty
|
jaroslav@34
|
961 |
static List<String> cmpResults(List<String> results) {
|
jaroslav@34
|
962 |
return results;
|
jaroslav@34
|
963 |
}
|
jtulach@1055
|
964 |
|
jaroslav@94
|
965 |
private static void triggerClick(String id) throws Exception {
|
jaroslav@94
|
966 |
String s = "var id = arguments[0];"
|
jaroslav@94
|
967 |
+ "var e = window.document.getElementById(id);\n "
|
jtulach@695
|
968 |
+ "if (e.checked) throw 'It should not be checked yet: ' + e;\n "
|
jaroslav@94
|
969 |
+ "var ev = window.document.createEvent('MouseEvents');\n "
|
jaroslav@94
|
970 |
+ "ev.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);\n "
|
jtulach@695
|
971 |
+ "e.dispatchEvent(ev);\n "
|
jtulach@695
|
972 |
+ "if (!e.checked) {\n"
|
jtulach@695
|
973 |
+ " e.checked = true;\n "
|
jtulach@695
|
974 |
+ " e.dispatchEvent(ev);\n "
|
jtulach@695
|
975 |
+ "}\n";
|
jaroslav@121
|
976 |
Utils.executeScript(
|
jaroslav@121
|
977 |
KnockoutTest.class,
|
jaroslav@121
|
978 |
s, id);
|
jaroslav@94
|
979 |
}
|
jaroslav@36
|
980 |
private static void triggerChildClick(String id, int pos) throws Exception {
|
jtulach@1055
|
981 |
String s =
|
jtulach@684
|
982 |
"var id = arguments[0]; var pos = arguments[1];\n" +
|
jtulach@684
|
983 |
"var e = window.document.getElementById(id);\n " +
|
jtulach@684
|
984 |
"var ev = window.document.createEvent('MouseEvents');\n " +
|
jtulach@684
|
985 |
"ev.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);\n " +
|
jtulach@684
|
986 |
"var list = e.childNodes;\n" +
|
jtulach@1055
|
987 |
"var cnt = -1;\n" +
|
jtulach@1055
|
988 |
"for (var i = 0; i < list.length; i++) {\n" +
|
jtulach@1055
|
989 |
" if (list[i].nodeType == 1) cnt++;\n" +
|
jtulach@1055
|
990 |
" if (cnt == pos) return list[i].dispatchEvent(ev);\n" +
|
jtulach@1055
|
991 |
"}\n" +
|
jtulach@684
|
992 |
"return null;\n";
|
jaroslav@121
|
993 |
Utils.executeScript(
|
jaroslav@121
|
994 |
KnockoutTest.class,
|
jaroslav@121
|
995 |
s, id, pos);
|
jaroslav@36
|
996 |
}
|
jaroslav@34
|
997 |
|
jaroslav@36
|
998 |
private static String childText(String id, int pos) throws Exception {
|
jtulach@1055
|
999 |
String s =
|
jtulach@683
|
1000 |
"var id = arguments[0]; var pos = arguments[1];" +
|
jtulach@683
|
1001 |
"var e = window.document.getElementById(id);\n" +
|
jtulach@683
|
1002 |
"var list = e.childNodes;\n" +
|
jtulach@1055
|
1003 |
"var cnt = -1;\n" +
|
jtulach@1055
|
1004 |
"for (var i = 0; i < list.length; i++) {\n" +
|
jtulach@1055
|
1005 |
" if (list[i].nodeType == 1) cnt++;\n" +
|
jtulach@1055
|
1006 |
" if (cnt == pos) return list[i].innerHTML;\n" +
|
jtulach@1055
|
1007 |
"}\n" +
|
jtulach@683
|
1008 |
"return null;\n";
|
jaroslav@121
|
1009 |
return (String)Utils.executeScript(
|
jaroslav@121
|
1010 |
KnockoutTest.class,
|
jaroslav@121
|
1011 |
s, id, pos);
|
jaroslav@121
|
1012 |
}
|
jaroslav@121
|
1013 |
|
jaroslav@121
|
1014 |
private static BrwsrCtx newContext() {
|
jaroslav@121
|
1015 |
return Utils.newContext(KnockoutTest.class);
|
jaroslav@36
|
1016 |
}
|
jaroslav@34
|
1017 |
}
|