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.List;
46 import java.util.Timer;
47 import java.util.TimerTask;
48 import net.java.html.BrwsrCtx;
49 import net.java.html.json.ComputedProperty;
50 import net.java.html.json.Function;
51 import net.java.html.json.Model;
52 import net.java.html.json.Models;
53 import net.java.html.json.Property;
54 import org.apidesign.html.json.tck.KOTest;
58 * @author Jaroslav Tulach
60 @Model(className="KnockoutModel", properties={
61 @Property(name="name", type=String.class),
62 @Property(name="results", type=String.class, array = true),
63 @Property(name="callbackCount", type=int.class),
64 @Property(name="people", type=PersonImpl.class, array = true),
65 @Property(name="enabled", type=boolean.class),
66 @Property(name="latitude", type=double.class),
67 @Property(name="choice", type=KnockoutTest.Choice.class),
69 public final class KnockoutTest {
70 private KnockoutModel js;
76 @KOTest public void modifyValueAssertChangeInModelOnEnum() throws Throwable {
77 Object exp = Utils.exposeHTML(KnockoutTest.class,
78 "Latitude: <input id='input' data-bind=\"value: choice\"></input>\n"
82 KnockoutModel m = Models.bind(new KnockoutModel(), newContext());
83 m.setChoice(Choice.A);
86 String v = getSetInput(null);
87 assert "A".equals(v) : "Value is really A: " + v;
90 triggerEvent("input", "change");
92 assert Choice.B == m.getChoice(): "Enum property updated: " + m.getChoice();
93 } catch (Throwable t) {
96 Utils.exposeHTML(KnockoutTest.class, "");
100 @KOTest public void modifyValueAssertChangeInModelOnDouble() throws Throwable {
101 Object exp = Utils.exposeHTML(KnockoutTest.class,
102 "Latitude: <input id='input' data-bind=\"value: latitude\"></input>\n"
106 KnockoutModel m = Models.bind(new KnockoutModel(), newContext());
110 String v = getSetInput(null);
111 assert "50.5".equals(v) : "Value is really 50.5: " + v;
114 triggerEvent("input", "change");
116 assert 49.5 == m.getLatitude() : "Double property updated: " + m.getLatitude();
117 } catch (Throwable t) {
120 Utils.exposeHTML(KnockoutTest.class, "");
124 @KOTest public void modifyValueAssertChangeInModelOnBoolean() throws Throwable {
125 Object exp = Utils.exposeHTML(KnockoutTest.class,
126 "Latitude: <input id='input' data-bind=\"value: enabled\"></input>\n"
130 KnockoutModel m = Models.bind(new KnockoutModel(), newContext());
134 String v = getSetInput(null);
135 assert "true".equals(v) : "Value is really true: " + v;
137 getSetInput("false");
138 triggerEvent("input", "change");
140 assert false == m.isEnabled(): "Boolean property updated: " + m.isEnabled();
141 } catch (Throwable t) {
144 Utils.exposeHTML(KnockoutTest.class, "");
148 @KOTest public void modifyValueAssertChangeInModel() throws Exception {
149 Object exp = Utils.exposeHTML(KnockoutTest.class,
150 "<h1 data-bind=\"text: helloMessage\">Loading Bck2Brwsr's Hello World...</h1>\n" +
151 "Your name: <input id='input' data-bind=\"value: name\"></input>\n" +
152 "<button id=\"hello\">Say Hello!</button>\n"
156 KnockoutModel m = Models.bind(new KnockoutModel(), newContext());
160 String v = getSetInput(null);
161 assert "Kukuc".equals(v) : "Value is really kukuc: " + v;
163 getSetInput("Jardo");
164 triggerEvent("input", "change");
166 assert "Jardo".equals(m.getName()) : "Name property updated: " + m.getName();
168 Utils.exposeHTML(KnockoutTest.class, "");
172 @KOTest public void modifyValueAssertAsyncChangeInModel() throws Exception {
174 Utils.exposeHTML(KnockoutTest.class,
175 "<h1 data-bind=\"text: helloMessage\">Loading Bck2Brwsr's Hello World...</h1>\n" +
176 "Your name: <input id='input' data-bind=\"value: name\"></input>\n" +
177 "<button id=\"hello\">Say Hello!</button>\n"
180 js = Models.bind(new KnockoutModel(), newContext());
184 String v = getSetInput(null);
185 assert "Kukuc".equals(v) : "Value is really kukuc: " + v;
187 Timer t = new Timer("Set to Jardo");
188 t.schedule(new TimerTask() {
196 String v = getSetInput(null);
197 if (!"Jardo".equals(v)) {
198 throw new InterruptedException();
201 Utils.exposeHTML(KnockoutTest.class, "");
204 private static String getSetInput(String value) throws Exception {
205 String s = "var value = arguments[0];\n"
206 + "var n = window.document.getElementById('input'); \n "
207 + "if (value != null) n['value'] = value; \n "
208 + "return n['value'];";
209 Object ret = Utils.executeScript(
213 return ret == null ? null : ret.toString();
216 public static void triggerEvent(String id, String ev) throws Exception {
219 "ko.utils.triggerEvent(window.document.getElementById(arguments[0]), arguments[1]);",
224 @KOTest public void displayContentOfArray() throws Exception {
225 Object exp = Utils.exposeHTML(KnockoutTest.class,
226 "<ul id='ul' data-bind='foreach: results'>\n"
227 + " <li data-bind='text: $data, click: $root.call'/>\n"
231 KnockoutModel m = Models.bind(new KnockoutModel(), newContext());
232 m.getResults().add("Ahoj");
235 int cnt = Utils.countChildren(KnockoutTest.class, "ul");
236 assert cnt == 1 : "One child, but was " + cnt;
238 m.getResults().add("Hi");
240 cnt = Utils.countChildren(KnockoutTest.class, "ul");
241 assert cnt == 2 : "Two children now, but was " + cnt;
243 triggerChildClick("ul", 1);
245 assert 1 == m.getCallbackCount() : "One callback " + m.getCallbackCount();
246 assert "Hi".equals(m.getName()) : "We got callback from 2nd child " + m.getName();
248 Utils.exposeHTML(KnockoutTest.class, "");
252 @KOTest public void displayContentOfAsyncArray() throws Exception {
254 Utils.exposeHTML(KnockoutTest.class,
255 "<ul id='ul' data-bind='foreach: results'>\n"
256 + " <li data-bind='text: $data, click: $root.call'/>\n"
259 js = Models.bind(new KnockoutModel(), newContext());
260 js.getResults().add("Ahoj");
263 int cnt = Utils.countChildren(KnockoutTest.class, "ul");
264 assert cnt == 1 : "One child, but was " + cnt;
266 Timer t = new Timer("add to array");
267 t.schedule(new TimerTask() {
270 js.getResults().add("Hi");
276 int cnt = Utils.countChildren(KnockoutTest.class, "ul");
278 throw new InterruptedException();
282 triggerChildClick("ul", 1);
284 assert 1 == js.getCallbackCount() : "One callback " + js.getCallbackCount();
285 assert "Hi".equals(js.getName()) : "We got callback from 2nd child " + js.getName();
287 Utils.exposeHTML(KnockoutTest.class, "");
291 @KOTest public void displayContentOfComputedArray() throws Exception {
292 Object exp = Utils.exposeHTML(KnockoutTest.class,
293 "<ul id='ul' data-bind='foreach: bothNames'>\n"
294 + " <li data-bind='text: $data, click: $root.assignFirstName'/>\n"
298 Pair m = Models.bind(new Pair("First", "Last", null), newContext());
301 int cnt = Utils.countChildren(KnockoutTest.class, "ul");
302 assert cnt == 2 : "Two children now, but was " + cnt;
304 triggerChildClick("ul", 1);
306 assert "Last".equals(m.getFirstName()) : "We got callback from 2nd child " + m.getFirstName();
308 m.setLastName("Verylast");
310 cnt = Utils.countChildren(KnockoutTest.class, "ul");
311 assert cnt == 2 : "Two children now, but was " + cnt;
313 triggerChildClick("ul", 1);
315 assert "Verylast".equals(m.getFirstName()) : "We got callback from 2nd child " + m.getFirstName();
318 Utils.exposeHTML(KnockoutTest.class, "");
322 @KOTest public void displayContentOfComputedArrayOnASubpair() throws Exception {
323 Object exp = Utils.exposeHTML(KnockoutTest.class,
324 "<div data-bind='with: next'>\n"
325 + "<ul id='ul' data-bind='foreach: bothNames'>\n"
326 + " <li data-bind='text: $data, click: $root.assignFirstName'/>\n"
331 final BrwsrCtx ctx = newContext();
332 Pair m = Models.bind(new Pair(null, null, new Pair("First", "Last", null)), ctx);
335 int cnt = Utils.countChildren(KnockoutTest.class, "ul");
336 assert cnt == 2 : "Two children now, but was " + cnt;
338 triggerChildClick("ul", 1);
340 assert PairModel.ctx == ctx : "Context remains the same";
342 assert "Last".equals(m.getFirstName()) : "We got callback from 2nd child " + m.getFirstName();
344 Utils.exposeHTML(KnockoutTest.class, "");
348 @KOTest public void displayContentOfComputedArrayOnComputedASubpair() throws Exception {
349 Object exp = Utils.exposeHTML(KnockoutTest.class,
350 "<div data-bind='with: nextOne'>\n"
351 + "<ul id='ul' data-bind='foreach: bothNames'>\n"
352 + " <li data-bind='text: $data, click: $root.assignFirstName'/>\n"
357 Pair m = Models.bind(new Pair(null, null, new Pair("First", "Last", null)), newContext());
360 int cnt = Utils.countChildren(KnockoutTest.class, "ul");
361 assert cnt == 2 : "Two children now, but was " + cnt;
363 triggerChildClick("ul", 1);
365 assert "Last".equals(m.getFirstName()) : "We got callback from 2nd child " + m.getFirstName();
367 Utils.exposeHTML(KnockoutTest.class, "");
371 @KOTest public void checkBoxToBooleanBinding() throws Exception {
372 Object exp = Utils.exposeHTML(KnockoutTest.class,
373 "<input type='checkbox' id='b' data-bind='checked: enabled'></input>\n"
376 KnockoutModel m = Models.bind(new KnockoutModel(), newContext());
379 assert !m.isEnabled() : "Is disabled";
383 assert m.isEnabled() : "Now the model is enabled";
385 Utils.exposeHTML(KnockoutTest.class, "");
391 @KOTest public void displayContentOfDerivedArray() throws Exception {
392 Object exp = Utils.exposeHTML(KnockoutTest.class,
393 "<ul id='ul' data-bind='foreach: cmpResults'>\n"
394 + " <li><b data-bind='text: $data'></b></li>\n"
398 KnockoutModel m = Models.bind(new KnockoutModel(), newContext());
399 m.getResults().add("Ahoj");
402 int cnt = Utils.countChildren(KnockoutTest.class, "ul");
403 assert cnt == 1 : "One child, but was " + cnt;
405 m.getResults().add("hello");
407 cnt = Utils.countChildren(KnockoutTest.class, "ul");
408 assert cnt == 2 : "Two children now, but was " + cnt;
410 Utils.exposeHTML(KnockoutTest.class, "");
414 @KOTest public void displayContentOfArrayOfPeople() throws Exception {
415 Object exp = Utils.exposeHTML(KnockoutTest.class,
416 "<ul id='ul' data-bind='foreach: people'>\n"
417 + " <li data-bind='text: $data.firstName, click: $root.removePerson'></li>\n"
421 final BrwsrCtx c = newContext();
422 KnockoutModel m = Models.bind(new KnockoutModel(), c);
424 final Person first = Models.bind(new Person(), c);
425 first.setFirstName("first");
426 m.getPeople().add(first);
430 int cnt = Utils.countChildren(KnockoutTest.class, "ul");
431 assert cnt == 1 : "One child, but was " + cnt;
433 final Person second = Models.bind(new Person(), c);
434 second.setFirstName("second");
435 m.getPeople().add(second);
437 cnt = Utils.countChildren(KnockoutTest.class, "ul");
438 assert cnt == 2 : "Two children now, but was " + cnt;
440 triggerChildClick("ul", 1);
442 assert 1 == m.getCallbackCount() : "One callback " + m.getCallbackCount();
444 cnt = Utils.countChildren(KnockoutTest.class, "ul");
445 assert cnt == 1 : "Again one child, but was " + cnt;
447 String txt = childText("ul", 0);
448 assert "first".equals(txt) : "Expecting 'first': " + txt;
450 first.setFirstName("changed");
452 txt = childText("ul", 0);
453 assert "changed".equals(txt) : "Expecting 'changed': " + txt;
455 Utils.exposeHTML(KnockoutTest.class, "");
460 static Person firstPerson(List<Person> people) {
461 return people.isEmpty() ? null : people.get(0);
464 @KOTest public void accessFirstPersonWithOnFunction() throws Exception {
465 Object exp = Utils.exposeHTML(KnockoutTest.class,
466 "<p id='ul' data-bind='with: firstPerson'>\n"
467 + " <span data-bind='text: firstName, click: changeSex'></span>\n"
473 Utils.exposeHTML(KnockoutTest.class, "");
477 @KOTest public void onPersonFunction() throws Exception {
478 Object exp = Utils.exposeHTML(KnockoutTest.class,
479 "<ul id='ul' data-bind='foreach: people'>\n"
480 + " <li data-bind='text: $data.firstName, click: changeSex'></li>\n"
486 Utils.exposeHTML(KnockoutTest.class, "");
490 private void trasfertToFemale() throws Exception {
491 KnockoutModel m = Models.bind(new KnockoutModel(), newContext());
493 final Person first = Models.bind(new Person(), newContext());
494 first.setFirstName("first");
495 first.setSex(Sex.MALE);
496 m.getPeople().add(first);
501 int cnt = Utils.countChildren(KnockoutTest.class, "ul");
502 assert cnt == 1 : "One child, but was " + cnt;
505 triggerChildClick("ul", 0);
507 assert first.getSex() == Sex.FEMALE : "Transverted to female: " + first.getSex();
511 static void call(KnockoutModel m, String data) {
513 m.setCallbackCount(m.getCallbackCount() + 1);
517 static void removePerson(KnockoutModel model, Person data) {
518 model.setCallbackCount(model.getCallbackCount() + 1);
519 model.getPeople().remove(data);
524 static String helloMessage(String name) {
525 return "Hello " + name + "!";
529 static List<String> cmpResults(List<String> results) {
533 private static void triggerClick(String id) throws Exception {
534 String s = "var id = arguments[0];"
535 + "var e = window.document.getElementById(id);\n "
536 + "if (e.checked) throw 'It should not be checked yet: ' + e;\n "
537 + "var ev = window.document.createEvent('MouseEvents');\n "
538 + "ev.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);\n "
539 + "e.dispatchEvent(ev);\n "
540 + "if (!e.checked) {\n"
541 + " e.checked = true;\n "
542 + " e.dispatchEvent(ev);\n "
548 private static void triggerChildClick(String id, int pos) throws Exception {
550 "var id = arguments[0]; var pos = arguments[1];\n" +
551 "var e = window.document.getElementById(id);\n " +
552 "var ev = window.document.createEvent('MouseEvents');\n " +
553 "ev.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);\n " +
554 "var list = e.childNodes;\n" +
556 "for (var i = 0; i < list.length; i++) {\n" +
557 " if (list[i].nodeType == 1) cnt++;\n" +
558 " if (cnt == pos) return list[i].dispatchEvent(ev);\n" +
566 private static String childText(String id, int pos) throws Exception {
568 "var id = arguments[0]; var pos = arguments[1];" +
569 "var e = window.document.getElementById(id);\n" +
570 "var list = e.childNodes;\n" +
572 "for (var i = 0; i < list.length; i++) {\n" +
573 " if (list[i].nodeType == 1) cnt++;\n" +
574 " if (cnt == pos) return list[i].innerHTML;\n" +
577 return (String)Utils.executeScript(
582 private static BrwsrCtx newContext() {
583 return Utils.newContext(KnockoutTest.class);