2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
6 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7 * Other names may be trademarks of their respective owners.
9 * The contents of this file are subject to the terms of either the GNU
10 * General Public License Version 2 only ("GPL") or the Common
11 * Development and Distribution License("CDDL") (collectively, the
12 * "License"). You may not use this file except in compliance with the
13 * License. You can obtain a copy of the License at
14 * http://www.netbeans.org/cddl-gplv2.html
15 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16 * specific language governing permissions and limitations under the
17 * License. When distributing the software, include this License Header
18 * Notice in each file and include the License file at
19 * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
20 * particular file as subject to the "Classpath" exception as provided
21 * by Oracle in the GPL Version 2 section of the License file that
22 * accompanied this code. If applicable, add the following below the
23 * License Header, with the fields enclosed by brackets [] replaced by
24 * your own identifying information:
25 * "Portions Copyrighted [year] [name of copyright owner]"
29 * The Original Software is NetBeans. The Initial Developer of the Original
30 * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
32 * If you wish your version of this file to be governed by only the CDDL
33 * or only the GPL Version 2, indicate your decision by adding
34 * "[Contributor] elects to include this software in this distribution
35 * under the [CDDL or GPL Version 2] license." If you do not indicate a
36 * single choice of license, a recipient has the option to distribute
37 * your version of this file under either the CDDL, the GPL Version 2 or
38 * to extend the choice of license to its licensees as provided above.
39 * However, if you add GPL Version 2 code and therefore, elected the GPL
40 * Version 2 license, then the option applies only if the new code is
41 * made subject to such option by the copyright holder.
43 package net.java.html.json.tests;
45 import java.util.Arrays;
46 import java.util.List;
47 import java.util.Timer;
48 import java.util.TimerTask;
49 import net.java.html.BrwsrCtx;
50 import net.java.html.json.ComputedProperty;
51 import net.java.html.json.Function;
52 import net.java.html.json.Model;
53 import net.java.html.json.Models;
54 import net.java.html.json.Property;
55 import org.netbeans.html.json.tck.KOTest;
56 import static net.java.html.json.tests.Utils.assertEquals;
57 import static net.java.html.json.tests.Utils.assertNotNull;
58 import static net.java.html.json.tests.Utils.assertTrue;
59 import static net.java.html.json.tests.Utils.assertFalse;
63 * @author Jaroslav Tulach
65 @Model(className="KnockoutModel", targetId = "", properties={
66 @Property(name="name", type=String.class),
67 @Property(name="results", type=String.class, array = true),
68 @Property(name="numbers", type=int.class, array = true),
69 @Property(name="callbackCount", type=int.class),
70 @Property(name="people", type=PersonImpl.class, array = true),
71 @Property(name="enabled", type=boolean.class),
72 @Property(name="latitude", type=double.class),
73 @Property(name="choice", type=KnockoutTest.Choice.class),
74 @Property(name="archetype", type=ArchetypeData.class),
75 @Property(name="archetypes", type=ArchetypeData.class, array = true),
77 public final class KnockoutTest {
78 private KnockoutModel js;
84 @ComputedProperty static List<Integer> resultLengths(List<String> results) {
85 Integer[] arr = new Integer[results.size()];
86 for (int i = 0; i < arr.length; i++) {
87 arr[i] = results.get(i).length();
89 return Arrays.asList(arr);
92 @KOTest public void modifyValueAssertChangeInModelOnEnum() throws Throwable {
93 Object exp = Utils.exposeHTML(KnockoutTest.class,
94 "Latitude: <input id='input' data-bind=\"value: choice\"></input>\n"
98 KnockoutModel m = Models.bind(new KnockoutModel(), newContext());
99 m.setChoice(Choice.A);
102 String v = getSetInput("input", null);
103 assertEquals("A", v, "Value is really A: " + v);
105 getSetInput("input", "B");
106 triggerEvent("input", "change");
108 assertEquals(Choice.B, m.getChoice(), "Enum property updated: " + m.getChoice());
109 } catch (Throwable t) {
112 Utils.exposeHTML(KnockoutTest.class, "");
117 @KOTest public void modifyRadioValueOnEnum() throws Throwable {
118 Object exp = Utils.exposeHTML(KnockoutTest.class,
119 "<input id='i1' type=\"radio\" name=\"choice\" value=\"A\" data-bind=\"checked: choice\"></input>Right\n" +
120 "<input id='input' type=\"radio\" name=\"choice\" value=\"B\" data-bind=\"checked: choice\"></input>Never\n" +
125 KnockoutModel m = Models.bind(new KnockoutModel(), newContext());
126 m.setChoice(Choice.B);
129 assertFalse(isChecked("i1"), "B should be checked now");
130 assertTrue(isChecked("input"), "B should be checked now");
132 triggerEvent("i1", "click");
133 assertEquals(Choice.A, m.getChoice(), "Switched to A");
134 assertTrue(isChecked("i1"), "A should be checked now");
135 assertFalse(isChecked("input"), "A should be checked now");
137 triggerEvent("input", "click");
139 assertEquals(Choice.B, m.getChoice(), "Enum property updated: " + m.getChoice());
140 } catch (Throwable t) {
143 Utils.exposeHTML(KnockoutTest.class, "");
147 @KOTest public void modifyValueAssertChangeInModelOnDouble() throws Throwable {
148 Object exp = Utils.exposeHTML(KnockoutTest.class,
149 "Latitude: <input id='input' data-bind=\"value: latitude\"></input>\n"
153 KnockoutModel m = Models.bind(new KnockoutModel(), newContext());
157 String v = getSetInput("input", null);
158 assertEquals("50.5", v, "Value is really 50.5: " + v);
160 getSetInput("input", "49.5");
161 triggerEvent("input", "change");
163 assertEquals(49.5, m.getLatitude(), "Double property updated: " + m.getLatitude());
164 } catch (Throwable t) {
167 Utils.exposeHTML(KnockoutTest.class, "");
171 @KOTest public void rawObject() {
172 final BrwsrCtx ctx = newContext();
173 Person p1 = Models.bind(new Person(), ctx);
174 p1.setFirstName("Jarda");
175 p1.setLastName("Tulach");
176 Object raw = Models.toRaw(p1);
177 Person p2 = Models.fromRaw(ctx, Person.class, raw);
179 assertEquals(p2.getFirstName(), "Jarda", "First name");
180 assertEquals(p2.getLastName(), "Tulach", "Last name");
182 p2.setFirstName("Jirka");
183 assertEquals(p2.getFirstName(), "Jirka", "First name updated");
184 assertEquals(p1.getFirstName(), "Jirka", "First name updated in original object");
186 p1.setFirstName("Ondra");
187 assertEquals(p1.getFirstName(), "Ondra", "1st name updated in original object");
188 assertEquals(p2.getFirstName(), "Ondra", "1st name updated in copied object");
191 @KOTest public void modifyComputedProperty() throws Throwable {
192 Object exp = Utils.exposeHTML(KnockoutTest.class,
193 "Full name: <div data-bind='with:firstPerson'>\n"
194 + "<input id='input' data-bind=\"value: fullName\"></input>\n"
198 KnockoutModel m = new KnockoutModel();
199 m.getPeople().add(new Person());
201 m = Models.bind(m, newContext());
202 m.getFirstPerson().setFirstName("Jarda");
203 m.getFirstPerson().setLastName("Tulach");
206 String v = getSetInput("input", null);
207 assertEquals("Jarda Tulach", v, "Value: " + v);
209 getSetInput("input", "Mickey Mouse");
210 triggerEvent("input", "change");
212 assertEquals("Mickey", m.getFirstPerson().getFirstName(), "First name updated");
213 assertEquals("Mouse", m.getFirstPerson().getLastName(), "Last name updated");
214 } catch (Throwable t) {
217 Utils.exposeHTML(KnockoutTest.class, "");
221 @KOTest public void modifyValueAssertChangeInModelOnBoolean() throws Throwable {
222 Object exp = Utils.exposeHTML(KnockoutTest.class,
223 "Latitude: <input id='input' data-bind=\"value: enabled\"></input>\n"
227 KnockoutModel m = Models.bind(new KnockoutModel(), newContext());
231 String v = getSetInput("input", null);
232 assertEquals("true", v, "Value is really true: " + v);
234 getSetInput("input", "false");
235 triggerEvent("input", "change");
237 assertFalse(m.isEnabled(), "Boolean property updated: " + m.isEnabled());
238 } catch (Throwable t) {
241 Utils.exposeHTML(KnockoutTest.class, "");
245 @KOTest public void modifyValueAssertChangeInModel() throws Exception {
246 Object exp = Utils.exposeHTML(KnockoutTest.class,
247 "<h1 data-bind=\"text: helloMessage\">Loading Bck2Brwsr's Hello World...</h1>\n" +
248 "Your name: <input id='input' data-bind=\"value: name\"></input>\n" +
249 "<button id=\"hello\">Say Hello!</button>\n"
253 KnockoutModel m = Models.bind(new KnockoutModel(), newContext());
257 String v = getSetInput("input", null);
258 assertEquals("Kukuc", v, "Value is really kukuc: " + v);
260 getSetInput("input", "Jardo");
261 triggerEvent("input", "change");
263 assertEquals("Jardo", m.getName(), "Name property updated: " + m.getName());
265 Utils.exposeHTML(KnockoutTest.class, "");
269 private static String getSetSelected(int index, Object value) throws Exception {
270 String s = "var index = arguments[0];\n"
271 + "var value = arguments[1];\n"
272 + "var n = window.document.getElementById('input'); \n "
273 + "if (value != null) {\n"
274 + " n.options[index].value = 'me'; \n"
275 + " n.value = 'me'; \n"
276 + " ko.dataFor(n.options[index]).archetype(value); // haven't found better way to trigger ko change yet \n"
278 + "var op = n.options[n.selectedIndex]; \n"
279 + "return op ? op.text : n.selectedIndex;\n";
280 Object ret = Utils.executeScript(
284 return ret == null ? null : ret.toString();
287 @Model(className = "ArchetypeData", properties = {
288 @Property(name = "artifactId", type = String.class),
289 @Property(name = "groupId", type = String.class),
290 @Property(name = "version", type = String.class),
291 @Property(name = "name", type = String.class),
292 @Property(name = "description", type = String.class),
293 @Property(name = "url", type = String.class),
295 static class ArchModel {
298 @KOTest public void selectWorksOnModels() throws Exception {
300 Utils.exposeHTML(KnockoutTest.class,
301 "<select id='input' data-bind=\"options: archetypes,\n" +
302 " optionsText: 'name',\n" +
303 " value: archetype\">\n" +
308 js = Models.bind(new KnockoutModel(), newContext());
309 js.getArchetypes().add(new ArchetypeData("ko4j", "org.netbeans.html", "0.8.3", "ko4j", "ko4j", null));
310 js.getArchetypes().add(new ArchetypeData("crud", "org.netbeans.html", "0.8.3", "crud", "crud", null));
311 js.getArchetypes().add(new ArchetypeData("3rd", "org.netbeans.html", "0.8.3", "3rd", "3rd", null));
312 js.setArchetype(js.getArchetypes().get(1));
315 String v = getSetSelected(0, null);
316 assertEquals("crud", v, "Second index (e.g. crud) is selected: " + v);
318 String sel = getSetSelected(2, Models.toRaw(js.getArchetypes().get(2)));
319 assertEquals("3rd", sel, "3rd is selected now: " + sel);
322 if (js.getArchetype() != js.getArchetypes().get(2)) {
323 throw new InterruptedException();
326 Utils.exposeHTML(KnockoutTest.class, "");
329 @KOTest public void nestedObjectEqualsChange() throws Exception {
330 nestedObjectEqualsChange(true);
333 @KOTest public void nestedObjectChange() throws Exception {
334 nestedObjectEqualsChange(false);
336 private void nestedObjectEqualsChange(boolean preApply) throws Exception {
337 Utils.exposeHTML(KnockoutTest.class,
338 " <div data-bind='with: archetype'>\n" +
339 " <input id='input' data-bind='value: groupId'></input>\n" +
343 js = Models.bind(new KnockoutModel(), newContext());
347 js.setArchetype(new ArchetypeData());
348 js.getArchetype().setGroupId("org.netbeans.html");
351 String v = getSetInput("input", null);
352 assertEquals("org.netbeans.html", v, "groupId has been changed");
353 Utils.exposeHTML(KnockoutTest.class, "");
356 @KOTest public void modifyValueAssertAsyncChangeInModel() throws Exception {
358 Utils.exposeHTML(KnockoutTest.class,
359 "<h1 data-bind=\"text: helloMessage\">Loading Bck2Brwsr's Hello World...</h1>\n" +
360 "Your name: <input id='input' data-bind=\"value: name\"></input>\n" +
361 "<button id=\"hello\">Say Hello!</button>\n"
364 js = Models.bind(new KnockoutModel(), newContext());
368 String v = getSetInput("input", null);
369 assertEquals("Kukuc", v, "Value is really kukuc: " + v);
371 Timer t = new Timer("Set to Jardo");
372 t.schedule(new TimerTask() {
380 String v = getSetInput("input", null);
381 if (!"Jardo".equals(v)) {
382 throw new InterruptedException();
385 Utils.exposeHTML(KnockoutTest.class, "");
388 private static String getSetInput(String id, String value) throws Exception {
389 String s = "var value = arguments[0];\n"
390 + "var n = window.document.getElementById(arguments[1]); \n "
391 + "if (value != null) n['value'] = value; \n "
392 + "return n['value'];";
393 Object ret = Utils.executeScript(
397 return ret == null ? null : ret.toString();
400 private static boolean isChecked(String id) throws Exception {
402 + "var n = window.document.getElementById(arguments[0]); \n "
403 + "return n['checked'];";
404 Object ret = Utils.executeScript(
408 return Boolean.TRUE.equals(ret);
411 public static void triggerEvent(String id, String ev) throws Exception {
414 "ko.utils.triggerEvent(window.document.getElementById(arguments[0]), arguments[1]);",
419 @KOTest public void displayContentOfArray() throws Exception {
420 Object exp = Utils.exposeHTML(KnockoutTest.class,
421 "<ul id='ul' data-bind='foreach: results'>\n"
422 + " <li data-bind='text: $data, click: $root.call'/>\n"
426 KnockoutModel m = Models.bind(new KnockoutModel(), newContext());
427 m.getResults().add("Ahoj");
430 int cnt = Utils.countChildren(KnockoutTest.class, "ul");
431 assertEquals(cnt, 1, "One child, but was " + cnt);
433 m.getResults().add("Hi");
435 cnt = Utils.countChildren(KnockoutTest.class, "ul");
436 assertEquals(cnt, 2, "Two children now, but was " + cnt);
438 triggerChildClick("ul", 1);
440 assertEquals(1, m.getCallbackCount(), "One callback " + m.getCallbackCount());
441 assertEquals("Hi", m.getName(), "We got callback from 2nd child " + m.getName());
443 Utils.exposeHTML(KnockoutTest.class, "");
447 @KOTest public void displayContentOfAsyncArray() throws Exception {
449 Utils.exposeHTML(KnockoutTest.class,
450 "<ul id='ul' data-bind='foreach: results'>\n"
451 + " <li data-bind='text: $data, click: $root.call'/>\n"
454 js = Models.bind(new KnockoutModel(), newContext());
455 js.getResults().add("Ahoj");
458 int cnt = Utils.countChildren(KnockoutTest.class, "ul");
459 assertEquals(cnt, 1, "One child, but was " + cnt);
461 Timer t = new Timer("add to array");
462 t.schedule(new TimerTask() {
465 js.getResults().add("Hi");
471 int cnt = Utils.countChildren(KnockoutTest.class, "ul");
473 throw new InterruptedException();
477 triggerChildClick("ul", 1);
479 assertEquals(1, js.getCallbackCount(), "One callback " + js.getCallbackCount());
480 assertEquals("Hi", js.getName(), "We got callback from 2nd child " + js.getName());
482 Utils.exposeHTML(KnockoutTest.class, "");
486 @KOTest public void displayContentOfComputedArray() throws Exception {
487 Object exp = Utils.exposeHTML(KnockoutTest.class,
488 "<ul id='ul' data-bind='foreach: bothNames'>\n"
489 + " <li data-bind='text: $data, click: $root.assignFirstName'/>\n"
493 Pair m = Models.bind(new Pair("First", "Last", null), newContext());
496 int cnt = Utils.countChildren(KnockoutTest.class, "ul");
497 assertEquals(cnt, 2, "Two children now, but was " + cnt);
499 triggerChildClick("ul", 1);
501 assertEquals("Last", m.getFirstName(), "We got callback from 2nd child " + m.getFirstName());
503 m.setLastName("Verylast");
505 cnt = Utils.countChildren(KnockoutTest.class, "ul");
506 assertEquals(cnt, 2, "Two children now, but was " + cnt);
508 triggerChildClick("ul", 1);
510 assertEquals("Verylast", m.getFirstName(), "We got callback from 2nd child " + m.getFirstName());
513 Utils.exposeHTML(KnockoutTest.class, "");
517 @KOTest public void displayContentOfComputedArrayOnASubpair() throws Exception {
518 Object exp = Utils.exposeHTML(KnockoutTest.class,
519 "<div data-bind='with: next'>\n"
520 + "<ul id='ul' data-bind='foreach: bothNames'>\n"
521 + " <li data-bind='text: $data, click: $root.assignFirstName'/>\n"
526 final BrwsrCtx ctx = newContext();
527 Pair m = Models.bind(new Pair(null, null, new Pair("First", "Last", null)), ctx);
530 int cnt = Utils.countChildren(KnockoutTest.class, "ul");
531 assertEquals(cnt, 2, "Two children now, but was " + cnt);
533 triggerChildClick("ul", 1);
535 assertEquals(PairModel.ctx, ctx, "Context remains the same");
537 assertEquals("Last", m.getFirstName(), "We got callback from 2nd child " + m.getFirstName());
539 Utils.exposeHTML(KnockoutTest.class, "");
543 @KOTest public void displayContentOfComputedArrayOnComputedASubpair() throws Exception {
544 Object exp = Utils.exposeHTML(KnockoutTest.class,
545 "<div data-bind='with: nextOne'>\n"
546 + "<ul id='ul' data-bind='foreach: bothNames'>\n"
547 + " <li data-bind='text: $data, click: $root.assignFirstName'/>\n"
552 Pair m = Models.bind(new Pair(null, null, new Pair("First", "Last", null)), newContext());
555 int cnt = Utils.countChildren(KnockoutTest.class, "ul");
556 assertEquals(cnt, 2, "Two children now, but was " + cnt);
558 triggerChildClick("ul", 1);
560 assertEquals("Last", m.getFirstName(), "We got callback from 2nd child " + m.getFirstName());
562 Utils.exposeHTML(KnockoutTest.class, "");
566 @KOTest public void checkBoxToBooleanBinding() throws Exception {
567 Object exp = Utils.exposeHTML(KnockoutTest.class,
568 "<input type='checkbox' id='b' data-bind='checked: enabled'></input>\n"
571 KnockoutModel m = Models.bind(new KnockoutModel(), newContext());
574 assertFalse(m.isEnabled(), "Is disabled");
578 assertTrue(m.isEnabled(), "Now the model is enabled");
580 Utils.exposeHTML(KnockoutTest.class, "");
586 @KOTest public void displayContentOfDerivedArray() throws Exception {
587 Object exp = Utils.exposeHTML(KnockoutTest.class,
588 "<ul id='ul' data-bind='foreach: cmpResults'>\n"
589 + " <li><b data-bind='text: $data'></b></li>\n"
593 KnockoutModel m = Models.bind(new KnockoutModel(), newContext());
594 m.getResults().add("Ahoj");
597 int cnt = Utils.countChildren(KnockoutTest.class, "ul");
598 assertEquals(cnt, 1, "One child, but was " + cnt);
600 m.getResults().add("hello");
602 cnt = Utils.countChildren(KnockoutTest.class, "ul");
603 assertEquals(cnt, 2, "Two children now, but was " + cnt);
605 Utils.exposeHTML(KnockoutTest.class, "");
609 @KOTest public void displayContentOfArrayOfPeople() throws Exception {
610 Object exp = Utils.exposeHTML(KnockoutTest.class,
611 "<ul id='ul' data-bind='foreach: people'>\n"
612 + " <li data-bind='text: $data.firstName, click: $root.removePerson'></li>\n"
616 final BrwsrCtx c = newContext();
617 KnockoutModel m = Models.bind(new KnockoutModel(), c);
619 final Person first = Models.bind(new Person(), c);
620 first.setFirstName("first");
621 m.getPeople().add(first);
625 int cnt = Utils.countChildren(KnockoutTest.class, "ul");
626 assertEquals(cnt, 1, "One child, but was " + cnt);
628 final Person second = Models.bind(new Person(), c);
629 second.setFirstName("second");
630 m.getPeople().add(second);
632 cnt = Utils.countChildren(KnockoutTest.class, "ul");
633 assertEquals(cnt, 2, "Two children now, but was " + cnt);
635 triggerChildClick("ul", 1);
637 assertEquals(1, m.getCallbackCount(), "One callback " + m.getCallbackCount());
639 cnt = Utils.countChildren(KnockoutTest.class, "ul");
640 assertEquals(cnt , 1, "Again one child, but was " + cnt);
642 String txt = childText("ul", 0);
643 assertEquals("first", txt, "Expecting 'first': " + txt);
645 first.setFirstName("changed");
647 txt = childText("ul", 0);
648 assertEquals("changed", txt, "Expecting 'changed': " + txt);
650 Utils.exposeHTML(KnockoutTest.class, "");
655 static Person firstPerson(List<Person> people) {
656 return people.isEmpty() ? null : people.get(0);
659 @KOTest public void accessFirstPersonWithOnFunction() throws Exception {
660 Object exp = Utils.exposeHTML(KnockoutTest.class,
661 "<p id='ul' data-bind='with: firstPerson'>\n"
662 + " <span data-bind='text: firstName, click: changeSex'></span>\n"
668 Utils.exposeHTML(KnockoutTest.class, "");
672 @KOTest public void onPersonFunction() throws Exception {
673 Object exp = Utils.exposeHTML(KnockoutTest.class,
674 "<ul id='ul' data-bind='foreach: people'>\n"
675 + " <li data-bind='text: $data.firstName, click: changeSex'></li>\n"
681 Utils.exposeHTML(KnockoutTest.class, "");
685 private void trasfertToFemale() throws Exception {
686 KnockoutModel m = Models.bind(new KnockoutModel(), newContext());
688 final Person first = Models.bind(new Person(), newContext());
689 first.setFirstName("first");
690 first.setSex(Sex.MALE);
691 m.getPeople().add(first);
696 int cnt = Utils.countChildren(KnockoutTest.class, "ul");
697 assertEquals(cnt, 1, "One child, but was " + cnt);
700 triggerChildClick("ul", 0);
702 assertEquals(first.getSex(), Sex.FEMALE, "Transverted to female: " + first.getSex());
705 @KOTest public void stringArrayModificationVisible() throws Exception {
706 Object exp = Utils.exposeHTML(KnockoutTest.class,
708 + "<ul id='ul' data-bind='foreach: results'>\n"
709 + " <li data-bind='text: $data'></li>\n"
714 KnockoutModel m = Models.bind(new KnockoutModel(), newContext());
715 m.getResults().add("Ahoj");
716 m.getResults().add("Hello");
719 int cnt = Utils.countChildren(KnockoutTest.class, "ul");
720 assertEquals(cnt, 2, "Two children " + cnt);
722 Object arr = Utils.addChildren(KnockoutTest.class, "ul", "results", "Hi");
723 assertTrue(arr instanceof Object[], "Got back an array: " + arr);
724 final int len = ((Object[])arr).length;
726 assertEquals(len, 3, "Three elements in the array " + len);
728 int newCnt = Utils.countChildren(KnockoutTest.class, "ul");
729 assertEquals(newCnt, 3, "Three children in the DOM: " + newCnt);
731 assertEquals(m.getResults().size(), 3, "Three java strings: " + m.getResults());
733 Utils.exposeHTML(KnockoutTest.class, "");
737 @KOTest public void intArrayModificationVisible() throws Exception {
738 Object exp = Utils.exposeHTML(KnockoutTest.class,
740 + "<ul id='ul' data-bind='foreach: numbers'>\n"
741 + " <li data-bind='text: $data'></li>\n"
746 KnockoutModel m = Models.bind(new KnockoutModel(), newContext());
747 m.getNumbers().add(1);
748 m.getNumbers().add(31);
751 int cnt = Utils.countChildren(KnockoutTest.class, "ul");
752 assertEquals(cnt, 2, "Two children " + cnt);
754 Object arr = Utils.addChildren(KnockoutTest.class, "ul", "numbers", 42);
755 assertTrue(arr instanceof Object[], "Got back an array: " + arr);
756 final int len = ((Object[])arr).length;
758 assertEquals(len, 3, "Three elements in the array " + len);
760 int newCnt = Utils.countChildren(KnockoutTest.class, "ul");
761 assertEquals(newCnt, 3, "Three children in the DOM: " + newCnt);
763 assertEquals(m.getNumbers().size(), 3, "Three java ints: " + m.getNumbers());
764 assertEquals(m.getNumbers().get(2), 42, "Meaning of world: " + m.getNumbers());
766 Utils.exposeHTML(KnockoutTest.class, "");
770 @KOTest public void derivedIntArrayModificationVisible() throws Exception {
771 Object exp = Utils.exposeHTML(KnockoutTest.class,
773 + "<ul id='ul' data-bind='foreach: resultLengths'>\n"
774 + " <li data-bind='text: $data'></li>\n"
779 KnockoutModel m = Models.bind(new KnockoutModel(), newContext());
780 m.getResults().add("Ahoj");
781 m.getResults().add("Hello");
784 int cnt = Utils.countChildren(KnockoutTest.class, "ul");
785 assertEquals(cnt, 2, "Two children " + cnt);
787 Object arr = Utils.addChildren(KnockoutTest.class, "ul", "results", "Hi");
788 assertTrue(arr instanceof Object[], "Got back an array: " + arr);
789 final int len = ((Object[])arr).length;
791 assertEquals(len, 3, "Three elements in the array " + len);
793 int newCnt = Utils.countChildren(KnockoutTest.class, "ul");
794 assertEquals(newCnt, 3, "Three children in the DOM: " + newCnt);
796 assertEquals(m.getResultLengths().size(), 3, "Three java ints: " + m.getResultLengths());
797 assertEquals(m.getResultLengths().get(2), 2, "Size is two: " + m.getResultLengths());
799 Utils.exposeHTML(KnockoutTest.class, "");
803 @KOTest public void archetypeArrayModificationVisible() throws Exception {
804 Object exp = Utils.exposeHTML(KnockoutTest.class,
806 + "<ul id='ul' data-bind='foreach: archetypes'>\n"
807 + " <li data-bind='text: artifactId'></li>\n"
812 KnockoutModel m = Models.bind(new KnockoutModel(), newContext());
815 int cnt = Utils.countChildren(KnockoutTest.class, "ul");
816 assertEquals(cnt, 0, "No children " + cnt);
818 Object arr = Utils.addChildren(KnockoutTest.class, "ul", "archetypes", new ArchetypeData("aid", "gid", "v", "n", "d", "u"));
819 assertTrue(arr instanceof Object[], "Got back an array: " + arr);
820 final int len = ((Object[])arr).length;
822 assertEquals(len, 1, "One element in the array " + len);
824 int newCnt = Utils.countChildren(KnockoutTest.class, "ul");
825 assertEquals(newCnt, 1, "One child in the DOM: " + newCnt);
827 assertEquals(m.getArchetypes().size(), 1, "One archetype: " + m.getArchetypes());
828 assertNotNull(m.getArchetypes().get(0), "Not null: " + m.getArchetypes());
829 assertEquals(m.getArchetypes().get(0).getArtifactId(), "aid", "'aid' == " + m.getArchetypes());
831 Utils.exposeHTML(KnockoutTest.class, "");
836 static void call(KnockoutModel m, String data) {
838 m.setCallbackCount(m.getCallbackCount() + 1);
842 static void removePerson(KnockoutModel model, Person data) {
843 model.setCallbackCount(model.getCallbackCount() + 1);
844 model.getPeople().remove(data);
849 static String helloMessage(String name) {
850 return "Hello " + name + "!";
854 static List<String> cmpResults(List<String> results) {
858 private static void triggerClick(String id) throws Exception {
859 String s = "var id = arguments[0];"
860 + "var e = window.document.getElementById(id);\n "
861 + "if (e.checked) throw 'It should not be checked yet: ' + e;\n "
862 + "var ev = window.document.createEvent('MouseEvents');\n "
863 + "ev.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);\n "
864 + "e.dispatchEvent(ev);\n "
865 + "if (!e.checked) {\n"
866 + " e.checked = true;\n "
867 + " e.dispatchEvent(ev);\n "
873 private static void triggerChildClick(String id, int pos) throws Exception {
875 "var id = arguments[0]; var pos = arguments[1];\n" +
876 "var e = window.document.getElementById(id);\n " +
877 "var ev = window.document.createEvent('MouseEvents');\n " +
878 "ev.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);\n " +
879 "var list = e.childNodes;\n" +
881 "for (var i = 0; i < list.length; i++) {\n" +
882 " if (list[i].nodeType == 1) cnt++;\n" +
883 " if (cnt == pos) return list[i].dispatchEvent(ev);\n" +
891 private static String childText(String id, int pos) throws Exception {
893 "var id = arguments[0]; var pos = arguments[1];" +
894 "var e = window.document.getElementById(id);\n" +
895 "var list = e.childNodes;\n" +
897 "for (var i = 0; i < list.length; i++) {\n" +
898 " if (list[i].nodeType == 1) cnt++;\n" +
899 " if (cnt == pos) return list[i].innerHTML;\n" +
902 return (String)Utils.executeScript(
907 private static BrwsrCtx newContext() {
908 return Utils.newContext(KnockoutTest.class);