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() throws Exception {
173 final BrwsrCtx ctx = newContext();
174 Person p1 = Models.bind(new Person(), ctx);
175 p1.setFirstName("Jarda");
176 p1.setLastName("Tulach");
177 Object raw = Models.toRaw(p1);
178 Person p2 = Models.fromRaw(ctx, Person.class, raw);
180 assertEquals(p2.getFirstName(), "Jarda", "First name");
181 assertEquals(p2.getLastName(), "Tulach", "Last name");
183 p2.setFirstName("Jirka");
184 assertEquals(p2.getFirstName(), "Jirka", "First name updated");
186 js = new KnockoutModel();
187 js.getPeople().add(p1);
188 js.getPeople().add(p2);
191 Person p1 = js.getPeople().get(0);
192 Person p2 = js.getPeople().get(1);
194 if (js.getPeople().size() == 2) {
195 if (!"Jirka".equals(p1.getFirstName())) {
196 throw new InterruptedException();
199 assertEquals(p1.getFirstName(), "Jirka", "First name updated in original object");
201 p1.setFirstName("Ondra");
202 assertEquals(p1.getFirstName(), "Ondra", "1st name updated in original object");
204 js.getPeople().add(p1);
207 if (!"Ondra".equals(p2.getFirstName())) {
208 throw new InterruptedException();
210 assertEquals(p2.getFirstName(), "Ondra", "1st name updated in copied object");
213 @KOTest public void modifyComputedProperty() throws Throwable {
214 Object exp = Utils.exposeHTML(KnockoutTest.class,
215 "Full name: <div data-bind='with:firstPerson'>\n"
216 + "<input id='input' data-bind=\"value: fullName\"></input>\n"
220 KnockoutModel m = new KnockoutModel();
221 m.getPeople().add(new Person());
223 m = Models.bind(m, newContext());
224 m.getFirstPerson().setFirstName("Jarda");
225 m.getFirstPerson().setLastName("Tulach");
228 String v = getSetInput("input", null);
229 assertEquals("Jarda Tulach", v, "Value: " + v);
231 getSetInput("input", "Mickey Mouse");
232 triggerEvent("input", "change");
234 assertEquals("Mickey", m.getFirstPerson().getFirstName(), "First name updated");
235 assertEquals("Mouse", m.getFirstPerson().getLastName(), "Last name updated");
236 } catch (Throwable t) {
239 Utils.exposeHTML(KnockoutTest.class, "");
243 @KOTest public void modifyValueAssertChangeInModelOnBoolean() throws Throwable {
244 Object exp = Utils.exposeHTML(KnockoutTest.class,
245 "Latitude: <input id='input' data-bind=\"value: enabled\"></input>\n"
249 KnockoutModel m = Models.bind(new KnockoutModel(), newContext());
253 String v = getSetInput("input", null);
254 assertEquals("true", v, "Value is really true: " + v);
256 getSetInput("input", "false");
257 triggerEvent("input", "change");
259 assertFalse(m.isEnabled(), "Boolean property updated: " + m.isEnabled());
260 } catch (Throwable t) {
263 Utils.exposeHTML(KnockoutTest.class, "");
267 @KOTest public void modifyValueAssertChangeInModel() throws Exception {
268 Object exp = Utils.exposeHTML(KnockoutTest.class,
269 "<h1 data-bind=\"text: helloMessage\">Loading Bck2Brwsr's Hello World...</h1>\n" +
270 "Your name: <input id='input' data-bind=\"value: name\"></input>\n" +
271 "<button id=\"hello\">Say Hello!</button>\n"
275 KnockoutModel m = Models.bind(new KnockoutModel(), newContext());
279 String v = getSetInput("input", null);
280 assertEquals("Kukuc", v, "Value is really kukuc: " + v);
282 getSetInput("input", "Jardo");
283 triggerEvent("input", "change");
285 assertEquals("Jardo", m.getName(), "Name property updated: " + m.getName());
287 Utils.exposeHTML(KnockoutTest.class, "");
291 private static String getSetSelected(int index, Object value) throws Exception {
292 String s = "var index = arguments[0];\n"
293 + "var value = arguments[1];\n"
294 + "var n = window.document.getElementById('input'); \n "
295 + "if (value != null) {\n"
296 + " n.options[index].value = 'me'; \n"
297 + " n.value = 'me'; \n"
298 + " ko.dataFor(n.options[index]).archetype(value); // haven't found better way to trigger ko change yet \n"
300 + "var op = n.options[n.selectedIndex]; \n"
301 + "return op ? op.text : n.selectedIndex;\n";
302 Object ret = Utils.executeScript(
306 return ret == null ? null : ret.toString();
309 @Model(className = "ArchetypeData", properties = {
310 @Property(name = "artifactId", type = String.class),
311 @Property(name = "groupId", type = String.class),
312 @Property(name = "version", type = String.class),
313 @Property(name = "name", type = String.class),
314 @Property(name = "description", type = String.class),
315 @Property(name = "url", type = String.class),
317 static class ArchModel {
320 @KOTest public void selectWorksOnModels() throws Exception {
322 Utils.exposeHTML(KnockoutTest.class,
323 "<select id='input' data-bind=\"options: archetypes,\n" +
324 " optionsText: 'name',\n" +
325 " value: archetype\">\n" +
330 js = Models.bind(new KnockoutModel(), newContext());
331 js.getArchetypes().add(new ArchetypeData("ko4j", "org.netbeans.html", "0.8.3", "ko4j", "ko4j", null));
332 js.getArchetypes().add(new ArchetypeData("crud", "org.netbeans.html", "0.8.3", "crud", "crud", null));
333 js.getArchetypes().add(new ArchetypeData("3rd", "org.netbeans.html", "0.8.3", "3rd", "3rd", null));
334 js.setArchetype(js.getArchetypes().get(1));
337 String v = getSetSelected(0, null);
338 assertEquals("crud", v, "Second index (e.g. crud) is selected: " + v);
340 String sel = getSetSelected(2, Models.toRaw(js.getArchetypes().get(2)));
341 assertEquals("3rd", sel, "3rd is selected now: " + sel);
344 if (js.getArchetype() != js.getArchetypes().get(2)) {
345 throw new InterruptedException();
348 Utils.exposeHTML(KnockoutTest.class, "");
351 @KOTest public void nestedObjectEqualsChange() throws Exception {
352 nestedObjectEqualsChange(true);
355 @KOTest public void nestedObjectChange() throws Exception {
356 nestedObjectEqualsChange(false);
358 private void nestedObjectEqualsChange(boolean preApply) throws Exception {
359 Utils.exposeHTML(KnockoutTest.class,
360 " <div data-bind='with: archetype'>\n" +
361 " <input id='input' data-bind='value: groupId'></input>\n" +
365 js = Models.bind(new KnockoutModel(), newContext());
369 js.setArchetype(new ArchetypeData());
370 js.getArchetype().setGroupId("org.netbeans.html");
373 String v = getSetInput("input", null);
374 assertEquals("org.netbeans.html", v, "groupId has been changed");
375 Utils.exposeHTML(KnockoutTest.class, "");
378 @KOTest public void modifyValueAssertAsyncChangeInModel() throws Exception {
380 Utils.exposeHTML(KnockoutTest.class,
381 "<h1 data-bind=\"text: helloMessage\">Loading Bck2Brwsr's Hello World...</h1>\n" +
382 "Your name: <input id='input' data-bind=\"value: name\"></input>\n" +
383 "<button id=\"hello\">Say Hello!</button>\n"
386 js = Models.bind(new KnockoutModel(), newContext());
390 String v = getSetInput("input", null);
391 assertEquals("Kukuc", v, "Value is really kukuc: " + v);
393 Timer t = new Timer("Set to Jardo");
394 t.schedule(new TimerTask() {
402 String v = getSetInput("input", null);
403 if (!"Jardo".equals(v)) {
404 throw new InterruptedException();
407 Utils.exposeHTML(KnockoutTest.class, "");
410 private static String getSetInput(String id, String value) throws Exception {
411 String s = "var value = arguments[0];\n"
412 + "var n = window.document.getElementById(arguments[1]); \n "
413 + "if (value != null) n['value'] = value; \n "
414 + "return n['value'];";
415 Object ret = Utils.executeScript(
419 return ret == null ? null : ret.toString();
422 private static boolean isChecked(String id) throws Exception {
424 + "var n = window.document.getElementById(arguments[0]); \n "
425 + "return n['checked'];";
426 Object ret = Utils.executeScript(
430 return Boolean.TRUE.equals(ret);
433 public static void triggerEvent(String id, String ev) throws Exception {
436 "ko.utils.triggerEvent(window.document.getElementById(arguments[0]), arguments[1]);",
441 @KOTest public void displayContentOfArray() throws Exception {
442 Object exp = Utils.exposeHTML(KnockoutTest.class,
443 "<ul id='ul' data-bind='foreach: results'>\n"
444 + " <li data-bind='text: $data, click: $root.call'/>\n"
448 KnockoutModel m = Models.bind(new KnockoutModel(), newContext());
449 m.getResults().add("Ahoj");
452 int cnt = Utils.countChildren(KnockoutTest.class, "ul");
453 assertEquals(cnt, 1, "One child, but was " + cnt);
455 m.getResults().add("Hi");
457 cnt = Utils.countChildren(KnockoutTest.class, "ul");
458 assertEquals(cnt, 2, "Two children now, but was " + cnt);
460 triggerChildClick("ul", 1);
462 assertEquals(1, m.getCallbackCount(), "One callback " + m.getCallbackCount());
463 assertEquals("Hi", m.getName(), "We got callback from 2nd child " + m.getName());
465 Utils.exposeHTML(KnockoutTest.class, "");
469 @KOTest public void displayContentOfAsyncArray() throws Exception {
471 Utils.exposeHTML(KnockoutTest.class,
472 "<ul id='ul' data-bind='foreach: results'>\n"
473 + " <li data-bind='text: $data, click: $root.call'/>\n"
476 js = Models.bind(new KnockoutModel(), newContext());
477 js.getResults().add("Ahoj");
480 int cnt = Utils.countChildren(KnockoutTest.class, "ul");
481 assertEquals(cnt, 1, "One child, but was " + cnt);
483 Timer t = new Timer("add to array");
484 t.schedule(new TimerTask() {
487 js.getResults().add("Hi");
493 int cnt = Utils.countChildren(KnockoutTest.class, "ul");
495 throw new InterruptedException();
499 triggerChildClick("ul", 1);
501 assertEquals(1, js.getCallbackCount(), "One callback " + js.getCallbackCount());
502 assertEquals("Hi", js.getName(), "We got callback from 2nd child " + js.getName());
504 Utils.exposeHTML(KnockoutTest.class, "");
508 @KOTest public void displayContentOfComputedArray() throws Exception {
509 Object exp = Utils.exposeHTML(KnockoutTest.class,
510 "<ul id='ul' data-bind='foreach: bothNames'>\n"
511 + " <li data-bind='text: $data, click: $root.assignFirstName'/>\n"
515 Pair m = Models.bind(new Pair("First", "Last", null), newContext());
518 int cnt = Utils.countChildren(KnockoutTest.class, "ul");
519 assertEquals(cnt, 2, "Two children now, but was " + cnt);
521 triggerChildClick("ul", 1);
523 assertEquals("Last", m.getFirstName(), "We got callback from 2nd child " + m.getFirstName());
525 m.setLastName("Verylast");
527 cnt = Utils.countChildren(KnockoutTest.class, "ul");
528 assertEquals(cnt, 2, "Two children now, but was " + cnt);
530 triggerChildClick("ul", 1);
532 assertEquals("Verylast", m.getFirstName(), "We got callback from 2nd child " + m.getFirstName());
535 Utils.exposeHTML(KnockoutTest.class, "");
539 @KOTest public void displayContentOfComputedArrayOnASubpair() throws Exception {
540 Object exp = Utils.exposeHTML(KnockoutTest.class,
541 "<div data-bind='with: next'>\n"
542 + "<ul id='ul' data-bind='foreach: bothNames'>\n"
543 + " <li data-bind='text: $data, click: $root.assignFirstName'/>\n"
548 final BrwsrCtx ctx = newContext();
549 Pair m = Models.bind(new Pair(null, null, new Pair("First", "Last", null)), ctx);
552 int cnt = Utils.countChildren(KnockoutTest.class, "ul");
553 assertEquals(cnt, 2, "Two children now, but was " + cnt);
555 triggerChildClick("ul", 1);
557 assertEquals(PairModel.ctx, ctx, "Context remains the same");
559 assertEquals("Last", m.getFirstName(), "We got callback from 2nd child " + m.getFirstName());
561 Utils.exposeHTML(KnockoutTest.class, "");
565 @KOTest public void displayContentOfComputedArrayOnComputedASubpair() throws Exception {
566 Object exp = Utils.exposeHTML(KnockoutTest.class,
567 "<div data-bind='with: nextOne'>\n"
568 + "<ul id='ul' data-bind='foreach: bothNames'>\n"
569 + " <li data-bind='text: $data, click: $root.assignFirstName'/>\n"
574 Pair m = Models.bind(new Pair(null, null, new Pair("First", "Last", null)), newContext());
577 int cnt = Utils.countChildren(KnockoutTest.class, "ul");
578 assertEquals(cnt, 2, "Two children now, but was " + cnt);
580 triggerChildClick("ul", 1);
582 assertEquals("Last", m.getFirstName(), "We got callback from 2nd child " + m.getFirstName());
584 Utils.exposeHTML(KnockoutTest.class, "");
588 @KOTest public void checkBoxToBooleanBinding() throws Exception {
589 Object exp = Utils.exposeHTML(KnockoutTest.class,
590 "<input type='checkbox' id='b' data-bind='checked: enabled'></input>\n"
593 KnockoutModel m = Models.bind(new KnockoutModel(), newContext());
596 assertFalse(m.isEnabled(), "Is disabled");
600 assertTrue(m.isEnabled(), "Now the model is enabled");
602 Utils.exposeHTML(KnockoutTest.class, "");
608 @KOTest public void displayContentOfDerivedArray() throws Exception {
609 Object exp = Utils.exposeHTML(KnockoutTest.class,
610 "<ul id='ul' data-bind='foreach: cmpResults'>\n"
611 + " <li><b data-bind='text: $data'></b></li>\n"
615 KnockoutModel m = Models.bind(new KnockoutModel(), newContext());
616 m.getResults().add("Ahoj");
619 int cnt = Utils.countChildren(KnockoutTest.class, "ul");
620 assertEquals(cnt, 1, "One child, but was " + cnt);
622 m.getResults().add("hello");
624 cnt = Utils.countChildren(KnockoutTest.class, "ul");
625 assertEquals(cnt, 2, "Two children now, but was " + cnt);
627 Utils.exposeHTML(KnockoutTest.class, "");
631 @KOTest public void displayContentOfArrayOfPeople() throws Exception {
632 Object exp = Utils.exposeHTML(KnockoutTest.class,
633 "<ul id='ul' data-bind='foreach: people'>\n"
634 + " <li data-bind='text: $data.firstName, click: $root.removePerson'></li>\n"
638 final BrwsrCtx c = newContext();
639 KnockoutModel m = Models.bind(new KnockoutModel(), c);
641 final Person first = Models.bind(new Person(), c);
642 first.setFirstName("first");
643 m.getPeople().add(first);
647 int cnt = Utils.countChildren(KnockoutTest.class, "ul");
648 assertEquals(cnt, 1, "One child, but was " + cnt);
650 final Person second = Models.bind(new Person(), c);
651 second.setFirstName("second");
652 m.getPeople().add(second);
654 cnt = Utils.countChildren(KnockoutTest.class, "ul");
655 assertEquals(cnt, 2, "Two children now, but was " + cnt);
657 triggerChildClick("ul", 1);
659 assertEquals(1, m.getCallbackCount(), "One callback " + m.getCallbackCount());
661 cnt = Utils.countChildren(KnockoutTest.class, "ul");
662 assertEquals(cnt , 1, "Again one child, but was " + cnt);
664 String txt = childText("ul", 0);
665 assertEquals("first", txt, "Expecting 'first': " + txt);
667 first.setFirstName("changed");
669 txt = childText("ul", 0);
670 assertEquals("changed", txt, "Expecting 'changed': " + txt);
672 Utils.exposeHTML(KnockoutTest.class, "");
677 static Person firstPerson(List<Person> people) {
678 return people.isEmpty() ? null : people.get(0);
681 @KOTest public void accessFirstPersonWithOnFunction() throws Exception {
682 Object exp = Utils.exposeHTML(KnockoutTest.class,
683 "<p id='ul' data-bind='with: firstPerson'>\n"
684 + " <span data-bind='text: firstName, click: changeSex'></span>\n"
690 Utils.exposeHTML(KnockoutTest.class, "");
694 @KOTest public void onPersonFunction() throws Exception {
695 Object exp = Utils.exposeHTML(KnockoutTest.class,
696 "<ul id='ul' data-bind='foreach: people'>\n"
697 + " <li data-bind='text: $data.firstName, click: changeSex'></li>\n"
703 Utils.exposeHTML(KnockoutTest.class, "");
707 private void trasfertToFemale() throws Exception {
708 KnockoutModel m = Models.bind(new KnockoutModel(), newContext());
710 final Person first = Models.bind(new Person(), newContext());
711 first.setFirstName("first");
712 first.setSex(Sex.MALE);
713 m.getPeople().add(first);
718 int cnt = Utils.countChildren(KnockoutTest.class, "ul");
719 assertEquals(cnt, 1, "One child, but was " + cnt);
722 triggerChildClick("ul", 0);
724 assertEquals(first.getSex(), Sex.FEMALE, "Transverted to female: " + first.getSex());
727 @KOTest public void stringArrayModificationVisible() throws Exception {
728 Object exp = Utils.exposeHTML(KnockoutTest.class,
730 + "<ul id='ul' data-bind='foreach: results'>\n"
731 + " <li data-bind='text: $data'></li>\n"
736 KnockoutModel m = Models.bind(new KnockoutModel(), newContext());
737 m.getResults().add("Ahoj");
738 m.getResults().add("Hello");
741 int cnt = Utils.countChildren(KnockoutTest.class, "ul");
742 assertEquals(cnt, 2, "Two children " + cnt);
744 Object arr = Utils.addChildren(KnockoutTest.class, "ul", "results", "Hi");
745 assertTrue(arr instanceof Object[], "Got back an array: " + arr);
746 final int len = ((Object[])arr).length;
748 assertEquals(len, 3, "Three elements in the array " + len);
750 int newCnt = Utils.countChildren(KnockoutTest.class, "ul");
751 assertEquals(newCnt, 3, "Three children in the DOM: " + newCnt);
753 assertEquals(m.getResults().size(), 3, "Three java strings: " + m.getResults());
755 Utils.exposeHTML(KnockoutTest.class, "");
759 @KOTest public void intArrayModificationVisible() throws Exception {
760 Object exp = Utils.exposeHTML(KnockoutTest.class,
762 + "<ul id='ul' data-bind='foreach: numbers'>\n"
763 + " <li data-bind='text: $data'></li>\n"
768 KnockoutModel m = Models.bind(new KnockoutModel(), newContext());
769 m.getNumbers().add(1);
770 m.getNumbers().add(31);
773 int cnt = Utils.countChildren(KnockoutTest.class, "ul");
774 assertEquals(cnt, 2, "Two children " + cnt);
776 Object arr = Utils.addChildren(KnockoutTest.class, "ul", "numbers", 42);
777 assertTrue(arr instanceof Object[], "Got back an array: " + arr);
778 final int len = ((Object[])arr).length;
780 assertEquals(len, 3, "Three elements in the array " + len);
782 int newCnt = Utils.countChildren(KnockoutTest.class, "ul");
783 assertEquals(newCnt, 3, "Three children in the DOM: " + newCnt);
785 assertEquals(m.getNumbers().size(), 3, "Three java ints: " + m.getNumbers());
786 assertEquals(m.getNumbers().get(2), 42, "Meaning of world: " + m.getNumbers());
788 Utils.exposeHTML(KnockoutTest.class, "");
792 @KOTest public void derivedIntArrayModificationVisible() throws Exception {
793 Object exp = Utils.exposeHTML(KnockoutTest.class,
795 + "<ul id='ul' data-bind='foreach: resultLengths'>\n"
796 + " <li data-bind='text: $data'></li>\n"
801 KnockoutModel m = Models.bind(new KnockoutModel(), newContext());
802 m.getResults().add("Ahoj");
803 m.getResults().add("Hello");
806 int cnt = Utils.countChildren(KnockoutTest.class, "ul");
807 assertEquals(cnt, 2, "Two children " + cnt);
809 Object arr = Utils.addChildren(KnockoutTest.class, "ul", "results", "Hi");
810 assertTrue(arr instanceof Object[], "Got back an array: " + arr);
811 final int len = ((Object[])arr).length;
813 assertEquals(len, 3, "Three elements in the array " + len);
815 int newCnt = Utils.countChildren(KnockoutTest.class, "ul");
816 assertEquals(newCnt, 3, "Three children in the DOM: " + newCnt);
818 assertEquals(m.getResultLengths().size(), 3, "Three java ints: " + m.getResultLengths());
819 assertEquals(m.getResultLengths().get(2), 2, "Size is two: " + m.getResultLengths());
821 Utils.exposeHTML(KnockoutTest.class, "");
825 @KOTest public void archetypeArrayModificationVisible() throws Exception {
826 Object exp = Utils.exposeHTML(KnockoutTest.class,
828 + "<ul id='ul' data-bind='foreach: archetypes'>\n"
829 + " <li data-bind='text: artifactId'></li>\n"
834 KnockoutModel m = Models.bind(new KnockoutModel(), newContext());
837 int cnt = Utils.countChildren(KnockoutTest.class, "ul");
838 assertEquals(cnt, 0, "No children " + cnt);
840 Object arr = Utils.addChildren(KnockoutTest.class, "ul", "archetypes", new ArchetypeData("aid", "gid", "v", "n", "d", "u"));
841 assertTrue(arr instanceof Object[], "Got back an array: " + arr);
842 final int len = ((Object[])arr).length;
844 assertEquals(len, 1, "One element in the array " + len);
846 int newCnt = Utils.countChildren(KnockoutTest.class, "ul");
847 assertEquals(newCnt, 1, "One child in the DOM: " + newCnt);
849 assertEquals(m.getArchetypes().size(), 1, "One archetype: " + m.getArchetypes());
850 assertNotNull(m.getArchetypes().get(0), "Not null: " + m.getArchetypes());
851 assertEquals(m.getArchetypes().get(0).getArtifactId(), "aid", "'aid' == " + m.getArchetypes());
853 Utils.exposeHTML(KnockoutTest.class, "");
858 static void call(KnockoutModel m, String data) {
860 m.setCallbackCount(m.getCallbackCount() + 1);
864 static void removePerson(KnockoutModel model, Person data) {
865 model.setCallbackCount(model.getCallbackCount() + 1);
866 model.getPeople().remove(data);
871 static String helloMessage(String name) {
872 return "Hello " + name + "!";
876 static List<String> cmpResults(List<String> results) {
880 private static void triggerClick(String id) throws Exception {
881 String s = "var id = arguments[0];"
882 + "var e = window.document.getElementById(id);\n "
883 + "if (e.checked) throw 'It should not be checked yet: ' + e;\n "
884 + "var ev = window.document.createEvent('MouseEvents');\n "
885 + "ev.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);\n "
886 + "e.dispatchEvent(ev);\n "
887 + "if (!e.checked) {\n"
888 + " e.checked = true;\n "
889 + " e.dispatchEvent(ev);\n "
895 private static void triggerChildClick(String id, int pos) throws Exception {
897 "var id = arguments[0]; var pos = arguments[1];\n" +
898 "var e = window.document.getElementById(id);\n " +
899 "var ev = window.document.createEvent('MouseEvents');\n " +
900 "ev.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);\n " +
901 "var list = e.childNodes;\n" +
903 "for (var i = 0; i < list.length; i++) {\n" +
904 " if (list[i].nodeType == 1) cnt++;\n" +
905 " if (cnt == pos) return list[i].dispatchEvent(ev);\n" +
913 private static String childText(String id, int pos) throws Exception {
915 "var id = arguments[0]; var pos = arguments[1];" +
916 "var e = window.document.getElementById(id);\n" +
917 "var list = e.childNodes;\n" +
919 "for (var i = 0; i < list.length; i++) {\n" +
920 " if (list[i].nodeType == 1) cnt++;\n" +
921 " if (cnt == pos) return list[i].innerHTML;\n" +
924 return (String)Utils.executeScript(
929 private static BrwsrCtx newContext() {
930 return Utils.newContext(KnockoutTest.class);