json-tck/src/main/java/net/java/html/json/tests/KnockoutTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Mon, 08 Feb 2016 20:42:23 +0100
changeset 1047 8ac11c0b7767
parent 1029 e737d579349a
child 1055 c61d247f087a
permissions -rw-r--r--
Notification of changes may be delivered asynchronously by knockout
     1 /**
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
     5  *
     6  * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
     7  * Other names may be trademarks of their respective owners.
     8  *
     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]"
    26  *
    27  * Contributor(s):
    28  *
    29  * The Original Software is NetBeans. The Initial Developer of the Original
    30  * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
    31  *
    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.
    42  */
    43 package net.java.html.json.tests;
    44 
    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;
    60 
    61 /**
    62  *
    63  * @author Jaroslav Tulach
    64  */
    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),
    76 }) 
    77 public final class KnockoutTest {
    78     private KnockoutModel js;
    79     
    80     enum Choice {
    81         A, B;
    82     }
    83     
    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();
    88         }
    89         return Arrays.asList(arr);
    90     }
    91     
    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"
    95         );
    96         try {
    97 
    98             KnockoutModel m = Models.bind(new KnockoutModel(), newContext());
    99             m.setChoice(Choice.A);
   100             m.applyBindings();
   101 
   102             String v = getSetInput("input", null);
   103             assertEquals("A", v, "Value is really A: " + v);
   104 
   105             getSetInput("input", "B");
   106             triggerEvent("input", "change");
   107 
   108             assertEquals(Choice.B, m.getChoice(), "Enum property updated: " + m.getChoice());
   109         } catch (Throwable t) {
   110             throw t;
   111         } finally {
   112             Utils.exposeHTML(KnockoutTest.class, "");
   113         }
   114     }
   115 
   116 
   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" +
   121             "\n"
   122         );
   123         try {
   124 
   125             KnockoutModel m = Models.bind(new KnockoutModel(), newContext());
   126             m.setChoice(Choice.B);
   127             m.applyBindings();
   128 
   129             assertFalse(isChecked("i1"), "B should be checked now");
   130             assertTrue(isChecked("input"), "B should be checked now");
   131 
   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");
   136 
   137             triggerEvent("input", "click");
   138 
   139             assertEquals(Choice.B, m.getChoice(), "Enum property updated: " + m.getChoice());
   140         } catch (Throwable t) {
   141             throw t;
   142         } finally {
   143             Utils.exposeHTML(KnockoutTest.class, "");
   144         }
   145     }
   146     
   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"
   150         );
   151         try {
   152 
   153             KnockoutModel m = Models.bind(new KnockoutModel(), newContext());
   154             m.setLatitude(50.5);
   155             m.applyBindings();
   156 
   157             String v = getSetInput("input", null);
   158             assertEquals("50.5", v, "Value is really 50.5: " + v);
   159 
   160             getSetInput("input", "49.5");
   161             triggerEvent("input", "change");
   162 
   163             assertEquals(49.5, m.getLatitude(), "Double property updated: " + m.getLatitude());
   164         } catch (Throwable t) {
   165             throw t;
   166         } finally {
   167             Utils.exposeHTML(KnockoutTest.class, "");
   168         }
   169     }
   170     
   171     @KOTest public void rawObject() throws Exception {
   172         if (js == null) {
   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);
   179 
   180             assertEquals(p2.getFirstName(), "Jarda", "First name");
   181             assertEquals(p2.getLastName(), "Tulach", "Last name");
   182 
   183             p2.setFirstName("Jirka");
   184             assertEquals(p2.getFirstName(), "Jirka", "First name updated");
   185 
   186             js = new KnockoutModel();
   187             js.getPeople().add(p1);
   188             js.getPeople().add(p2);
   189         }
   190 
   191         Person p1 = js.getPeople().get(0);
   192         Person p2 = js.getPeople().get(1);
   193 
   194         if (js.getPeople().size() == 2) {
   195             if (!"Jirka".equals(p1.getFirstName())) {
   196                 throw new InterruptedException();
   197             }
   198 
   199             assertEquals(p1.getFirstName(), "Jirka", "First name updated in original object");
   200 
   201             p1.setFirstName("Ondra");
   202             assertEquals(p1.getFirstName(), "Ondra", "1st name updated in original object");
   203             
   204             js.getPeople().add(p1);
   205         }
   206 
   207         if (!"Ondra".equals(p2.getFirstName())) {
   208             throw new InterruptedException();
   209         }
   210         assertEquals(p2.getFirstName(), "Ondra", "1st name updated in copied object");
   211     }
   212 
   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"
   217                 + "</div>\n"
   218         );
   219         try {
   220             KnockoutModel m = new KnockoutModel();
   221             m.getPeople().add(new Person());
   222 
   223             m = Models.bind(m, newContext());
   224             m.getFirstPerson().setFirstName("Jarda");
   225             m.getFirstPerson().setLastName("Tulach");
   226             m.applyBindings();
   227 
   228             String v = getSetInput("input", null);
   229             assertEquals("Jarda Tulach", v, "Value: " + v);
   230 
   231             getSetInput("input", "Mickey Mouse");
   232             triggerEvent("input", "change");
   233 
   234             assertEquals("Mickey", m.getFirstPerson().getFirstName(), "First name updated");
   235             assertEquals("Mouse", m.getFirstPerson().getLastName(), "Last name updated");
   236         } catch (Throwable t) {
   237             throw t;
   238         } finally {
   239             Utils.exposeHTML(KnockoutTest.class, "");
   240         }
   241     }
   242     
   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"
   246         );
   247         try {
   248 
   249             KnockoutModel m = Models.bind(new KnockoutModel(), newContext());
   250             m.setEnabled(true);
   251             m.applyBindings();
   252 
   253             String v = getSetInput("input", null);
   254             assertEquals("true", v, "Value is really true: " + v);
   255 
   256             getSetInput("input", "false");
   257             triggerEvent("input", "change");
   258 
   259             assertFalse(m.isEnabled(), "Boolean property updated: " + m.isEnabled());
   260         } catch (Throwable t) {
   261             throw t;
   262         } finally {
   263             Utils.exposeHTML(KnockoutTest.class, "");
   264         }
   265     }
   266     
   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"
   272         );
   273         try {
   274 
   275             KnockoutModel m = Models.bind(new KnockoutModel(), newContext());
   276             m.setName("Kukuc");
   277             m.applyBindings();
   278 
   279             String v = getSetInput("input", null);
   280             assertEquals("Kukuc", v, "Value is really kukuc: " + v);
   281 
   282             getSetInput("input", "Jardo");
   283             triggerEvent("input", "change");
   284 
   285             assertEquals("Jardo", m.getName(), "Name property updated: " + m.getName());
   286         } finally {
   287             Utils.exposeHTML(KnockoutTest.class, "");
   288         }
   289     }
   290     
   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"
   299         + "} \n "
   300         + "var op = n.options[n.selectedIndex]; \n"
   301         + "return op ? op.text : n.selectedIndex;\n";
   302         Object ret = Utils.executeScript(
   303             KnockoutTest.class,
   304             s, index, value
   305         );
   306         return ret == null ? null : ret.toString();
   307     }
   308     
   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),
   316     })
   317     static class ArchModel {
   318     }
   319     
   320     @KOTest public void selectWorksOnModels() throws Exception {
   321         if (js == null) {
   322             Utils.exposeHTML(KnockoutTest.class, 
   323                 "<select id='input' data-bind=\"options: archetypes,\n" +
   324 "                       optionsText: 'name',\n" +
   325 "                       value: archetype\">\n" +
   326 "                  </select>\n" +
   327 ""
   328             );
   329             
   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));
   335             js.applyBindings();
   336             
   337             String v = getSetSelected(0, null);
   338             assertEquals("crud", v, "Second index (e.g. crud) is selected: " + v);
   339             
   340             String sel = getSetSelected(2, Models.toRaw(js.getArchetypes().get(2)));
   341             assertEquals("3rd", sel, "3rd is selected now: " + sel);
   342         }
   343         
   344         if (js.getArchetype() != js.getArchetypes().get(2)) {
   345             throw new InterruptedException();
   346         }
   347         
   348         Utils.exposeHTML(KnockoutTest.class, "");
   349     }
   350 
   351     @KOTest public void nestedObjectEqualsChange() throws Exception {
   352         nestedObjectEqualsChange(true);
   353     }
   354 
   355     @KOTest public void nestedObjectChange() throws Exception {
   356         nestedObjectEqualsChange(false);
   357     }
   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" +
   362 "            </div>\n"
   363         );
   364 
   365         js = Models.bind(new KnockoutModel(), newContext());
   366         if (preApply) {
   367             js.applyBindings();
   368         }
   369         js.setArchetype(new ArchetypeData());
   370         js.getArchetype().setGroupId("org.netbeans.html");
   371         js.applyBindings();
   372 
   373         String v = getSetInput("input", null);
   374         assertEquals("org.netbeans.html", v, "groupId has been changed");
   375         Utils.exposeHTML(KnockoutTest.class, "");
   376     }
   377     
   378     @KOTest public void modifyValueAssertAsyncChangeInModel() throws Exception {
   379         if (js == null) {
   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"
   384             );
   385             
   386             js = Models.bind(new KnockoutModel(), newContext());
   387             js.setName("Kukuc");
   388             js.applyBindings();
   389             
   390             String v = getSetInput("input", null);
   391             assertEquals("Kukuc", v, "Value is really kukuc: " + v);
   392             
   393             Timer t = new Timer("Set to Jardo");
   394             t.schedule(new TimerTask() {
   395                 @Override
   396                 public void run() {
   397                     js.setName("Jardo");
   398                 }
   399             }, 1);
   400         }
   401         
   402         String v = getSetInput("input", null);
   403         if (!"Jardo".equals(v)) {
   404             throw new InterruptedException();
   405         }
   406         
   407         Utils.exposeHTML(KnockoutTest.class, "");
   408     }
   409     
   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(
   416             KnockoutTest.class,
   417             s, value, id
   418         );
   419         return ret == null ? null : ret.toString();
   420     }
   421 
   422     private static boolean isChecked(String id) throws Exception {
   423         String s = ""
   424         + "var n = window.document.getElementById(arguments[0]); \n "
   425         + "return n['checked'];";
   426         Object ret = Utils.executeScript(
   427             KnockoutTest.class,
   428             s, id
   429         );
   430         return Boolean.TRUE.equals(ret);
   431     }
   432     
   433     public static void triggerEvent(String id, String ev) throws Exception {
   434         Utils.executeScript(
   435             KnockoutTest.class,
   436             "ko.utils.triggerEvent(window.document.getElementById(arguments[0]), arguments[1]);",
   437             id, ev
   438         );
   439     }
   440     
   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"
   445             + "</ul>\n"
   446         );
   447         try {
   448             KnockoutModel m = Models.bind(new KnockoutModel(), newContext());
   449             m.getResults().add("Ahoj");
   450             m.applyBindings();
   451 
   452             int cnt = Utils.countChildren(KnockoutTest.class, "ul");
   453             assertEquals(cnt, 1, "One child, but was " + cnt);
   454 
   455             m.getResults().add("Hi");
   456 
   457             cnt = Utils.countChildren(KnockoutTest.class, "ul");
   458             assertEquals(cnt, 2, "Two children now, but was " + cnt);
   459 
   460             triggerChildClick("ul", 1);
   461 
   462             assertEquals(1, m.getCallbackCount(), "One callback " + m.getCallbackCount());
   463             assertEquals("Hi", m.getName(), "We got callback from 2nd child " + m.getName());
   464         } finally {
   465             Utils.exposeHTML(KnockoutTest.class, "");
   466         }
   467     }
   468     
   469     @KOTest public void displayContentOfAsyncArray() throws Exception {
   470         if (js == null) {
   471             Utils.exposeHTML(KnockoutTest.class, 
   472                 "<ul id='ul' data-bind='foreach: results'>\n"
   473                 + "  <li data-bind='text: $data, click: $root.call'/>\n"
   474                 + "</ul>\n"
   475             );
   476             js = Models.bind(new KnockoutModel(), newContext());
   477             js.getResults().add("Ahoj");
   478             js.applyBindings();
   479 
   480             int cnt = Utils.countChildren(KnockoutTest.class, "ul");
   481             assertEquals(cnt, 1, "One child, but was " + cnt);
   482             
   483             Timer t = new Timer("add to array");
   484             t.schedule(new TimerTask() {
   485                 @Override
   486                 public void run() {
   487                     js.getResults().add("Hi");
   488                 }
   489             }, 1);
   490         }
   491 
   492 
   493         int cnt = Utils.countChildren(KnockoutTest.class, "ul");
   494         if (cnt != 2) {
   495             throw new InterruptedException();
   496         }
   497 
   498         try {
   499             triggerChildClick("ul", 1);
   500 
   501             assertEquals(1, js.getCallbackCount(), "One callback " + js.getCallbackCount());
   502             assertEquals("Hi", js.getName(), "We got callback from 2nd child " + js.getName());
   503         } finally {
   504             Utils.exposeHTML(KnockoutTest.class, "");
   505         }
   506     }
   507     
   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"
   512             + "</ul>\n"
   513         );
   514         try {
   515             Pair m = Models.bind(new Pair("First", "Last", null), newContext());
   516             m.applyBindings();
   517 
   518             int cnt = Utils.countChildren(KnockoutTest.class, "ul");
   519             assertEquals(cnt, 2, "Two children now, but was " + cnt);
   520 
   521             triggerChildClick("ul", 1);
   522 
   523             assertEquals("Last", m.getFirstName(), "We got callback from 2nd child " + m.getFirstName());
   524             
   525             m.setLastName("Verylast");
   526 
   527             cnt = Utils.countChildren(KnockoutTest.class, "ul");
   528             assertEquals(cnt, 2, "Two children now, but was " + cnt);
   529             
   530             triggerChildClick("ul", 1);
   531 
   532             assertEquals("Verylast", m.getFirstName(), "We got callback from 2nd child " + m.getFirstName());
   533             
   534         } finally {
   535             Utils.exposeHTML(KnockoutTest.class, "");
   536         }
   537     }
   538     
   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"
   544             + "</ul>"
   545             + "</div>\n"
   546         );
   547         try {
   548             final BrwsrCtx ctx = newContext();
   549             Pair m = Models.bind(new Pair(null, null, new Pair("First", "Last", null)), ctx);
   550             m.applyBindings();
   551 
   552             int cnt = Utils.countChildren(KnockoutTest.class, "ul");
   553             assertEquals(cnt, 2, "Two children now, but was " + cnt);
   554 
   555             triggerChildClick("ul", 1);
   556             
   557             assertEquals(PairModel.ctx, ctx, "Context remains the same");
   558 
   559             assertEquals("Last", m.getFirstName(), "We got callback from 2nd child " + m.getFirstName());
   560         } finally {
   561             Utils.exposeHTML(KnockoutTest.class, "");
   562         }
   563     }
   564     
   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"
   570             + "</ul>"
   571             + "</div>\n"
   572         );
   573         try {
   574             Pair m = Models.bind(new Pair(null, null, new Pair("First", "Last", null)), newContext());
   575             m.applyBindings();
   576 
   577             int cnt = Utils.countChildren(KnockoutTest.class, "ul");
   578             assertEquals(cnt, 2, "Two children now, but was " + cnt);
   579 
   580             triggerChildClick("ul", 1);
   581 
   582             assertEquals("Last", m.getFirstName(), "We got callback from 2nd child " + m.getFirstName());
   583         } finally {
   584             Utils.exposeHTML(KnockoutTest.class, "");
   585         }
   586     }
   587 
   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"
   591         );
   592         try {
   593             KnockoutModel m = Models.bind(new KnockoutModel(), newContext());
   594             m.applyBindings();
   595 
   596             assertFalse(m.isEnabled(), "Is disabled");
   597 
   598             triggerClick("b");
   599 
   600             assertTrue(m.isEnabled(), "Now the model is enabled");
   601         } finally {
   602             Utils.exposeHTML(KnockoutTest.class, "");
   603         }
   604     }
   605     
   606     
   607     
   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"
   612             + "</ul>\n"
   613         );
   614         try {
   615             KnockoutModel m = Models.bind(new KnockoutModel(), newContext());
   616             m.getResults().add("Ahoj");
   617             m.applyBindings();
   618 
   619             int cnt = Utils.countChildren(KnockoutTest.class, "ul");
   620             assertEquals(cnt, 1, "One child, but was " + cnt);
   621 
   622             m.getResults().add("hello");
   623 
   624             cnt = Utils.countChildren(KnockoutTest.class, "ul");
   625             assertEquals(cnt, 2, "Two children now, but was " + cnt);
   626         } finally {
   627             Utils.exposeHTML(KnockoutTest.class, "");
   628         }
   629     }
   630     
   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"
   635             + "</ul>\n"
   636         );
   637         try {
   638             final BrwsrCtx c = newContext();
   639             KnockoutModel m = Models.bind(new KnockoutModel(), c);
   640 
   641             final Person first = Models.bind(new Person(), c);
   642             first.setFirstName("first");
   643             m.getPeople().add(first);
   644 
   645             m.applyBindings();
   646 
   647             int cnt = Utils.countChildren(KnockoutTest.class, "ul");
   648             assertEquals(cnt, 1, "One child, but was " + cnt);
   649 
   650             final Person second = Models.bind(new Person(), c);
   651             second.setFirstName("second");
   652             m.getPeople().add(second);
   653 
   654             cnt = Utils.countChildren(KnockoutTest.class, "ul");
   655             assertEquals(cnt, 2, "Two children now, but was " + cnt);
   656 
   657             triggerChildClick("ul", 1);
   658 
   659             assertEquals(1, m.getCallbackCount(), "One callback " + m.getCallbackCount());
   660 
   661             cnt = Utils.countChildren(KnockoutTest.class, "ul");
   662             assertEquals(cnt , 1, "Again one child, but was " + cnt);
   663 
   664             String txt = childText("ul", 0);
   665             assertEquals("first", txt, "Expecting 'first': " + txt);
   666 
   667             first.setFirstName("changed");
   668 
   669             txt = childText("ul", 0);
   670             assertEquals("changed", txt, "Expecting 'changed': " + txt);
   671         } finally {
   672             Utils.exposeHTML(KnockoutTest.class, "");
   673         }
   674     }
   675     
   676     @ComputedProperty
   677     static Person firstPerson(List<Person> people) {
   678         return people.isEmpty() ? null : people.get(0);
   679     }
   680     
   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"
   685             + "</p>\n"
   686         );
   687         try {
   688             trasfertToFemale();
   689         } finally {
   690             Utils.exposeHTML(KnockoutTest.class, "");
   691         }
   692     }
   693     
   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"
   698             + "</ul>\n"
   699         );
   700         try {
   701             trasfertToFemale();
   702         } finally {
   703             Utils.exposeHTML(KnockoutTest.class, "");
   704         }
   705     }
   706     
   707     private void trasfertToFemale() throws Exception {
   708         KnockoutModel m = Models.bind(new KnockoutModel(), newContext());
   709 
   710         final Person first = Models.bind(new Person(), newContext());
   711         first.setFirstName("first");
   712         first.setSex(Sex.MALE);
   713         m.getPeople().add(first);
   714 
   715 
   716         m.applyBindings();
   717 
   718         int cnt = Utils.countChildren(KnockoutTest.class, "ul");
   719         assertEquals(cnt, 1, "One child, but was " + cnt);
   720 
   721 
   722         triggerChildClick("ul", 0);
   723 
   724         assertEquals(first.getSex(), Sex.FEMALE, "Transverted to female: " + first.getSex());
   725     }
   726     
   727     @KOTest public void stringArrayModificationVisible() throws Exception {
   728         Object exp = Utils.exposeHTML(KnockoutTest.class,
   729                 "<div>\n"
   730                 + "<ul id='ul' data-bind='foreach: results'>\n"
   731                 + "  <li data-bind='text: $data'></li>\n"
   732                 + "</ul>\n"
   733               + "</div>\n"
   734         );
   735         try {
   736             KnockoutModel m = Models.bind(new KnockoutModel(), newContext());
   737             m.getResults().add("Ahoj");
   738             m.getResults().add("Hello");
   739             m.applyBindings();
   740             
   741             int cnt = Utils.countChildren(KnockoutTest.class, "ul");
   742             assertEquals(cnt, 2, "Two children " + cnt);
   743             
   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;
   747             
   748             assertEquals(len, 3, "Three elements in the array " + len);
   749             
   750             int newCnt = Utils.countChildren(KnockoutTest.class, "ul");
   751             assertEquals(newCnt, 3, "Three children in the DOM: " + newCnt);
   752             
   753             assertEquals(m.getResults().size(), 3, "Three java strings: " + m.getResults());
   754         } finally {
   755             Utils.exposeHTML(KnockoutTest.class, "");
   756         }
   757     }
   758 
   759     @KOTest public void intArrayModificationVisible() throws Exception {
   760         Object exp = Utils.exposeHTML(KnockoutTest.class,
   761                 "<div>\n"
   762                 + "<ul id='ul' data-bind='foreach: numbers'>\n"
   763                 + "  <li data-bind='text: $data'></li>\n"
   764                 + "</ul>\n"
   765               + "</div>\n"
   766         );
   767         try {
   768             KnockoutModel m = Models.bind(new KnockoutModel(), newContext());
   769             m.getNumbers().add(1);
   770             m.getNumbers().add(31);
   771             m.applyBindings();
   772             
   773             int cnt = Utils.countChildren(KnockoutTest.class, "ul");
   774             assertEquals(cnt, 2, "Two children " + cnt);
   775             
   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;
   779             
   780             assertEquals(len, 3, "Three elements in the array " + len);
   781             
   782             int newCnt = Utils.countChildren(KnockoutTest.class, "ul");
   783             assertEquals(newCnt, 3, "Three children in the DOM: " + newCnt);
   784             
   785             assertEquals(m.getNumbers().size(), 3, "Three java ints: " + m.getNumbers());
   786             assertEquals(m.getNumbers().get(2), 42, "Meaning of world: " + m.getNumbers());
   787         } finally {
   788             Utils.exposeHTML(KnockoutTest.class, "");
   789         }
   790     }
   791 
   792     @KOTest public void derivedIntArrayModificationVisible() throws Exception {
   793         Object exp = Utils.exposeHTML(KnockoutTest.class,
   794                 "<div>\n"
   795                 + "<ul id='ul' data-bind='foreach: resultLengths'>\n"
   796                 + "  <li data-bind='text: $data'></li>\n"
   797                 + "</ul>\n"
   798               + "</div>\n"
   799         );
   800         try {
   801             KnockoutModel m = Models.bind(new KnockoutModel(), newContext());
   802             m.getResults().add("Ahoj");
   803             m.getResults().add("Hello");
   804             m.applyBindings();
   805             
   806             int cnt = Utils.countChildren(KnockoutTest.class, "ul");
   807             assertEquals(cnt, 2, "Two children " + cnt);
   808             
   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;
   812             
   813             assertEquals(len, 3, "Three elements in the array " + len);
   814             
   815             int newCnt = Utils.countChildren(KnockoutTest.class, "ul");
   816             assertEquals(newCnt, 3, "Three children in the DOM: " + newCnt);
   817             
   818             assertEquals(m.getResultLengths().size(), 3, "Three java ints: " + m.getResultLengths());
   819             assertEquals(m.getResultLengths().get(2), 2, "Size is two: " + m.getResultLengths());
   820         } finally {
   821             Utils.exposeHTML(KnockoutTest.class, "");
   822         }
   823     }
   824     
   825     @KOTest public void archetypeArrayModificationVisible() throws Exception {
   826         Object exp = Utils.exposeHTML(KnockoutTest.class,
   827                 "<div>\n"
   828                 + "<ul id='ul' data-bind='foreach: archetypes'>\n"
   829                 + "  <li data-bind='text: artifactId'></li>\n"
   830                 + "</ul>\n"
   831               + "</div>\n"
   832         );
   833         try {
   834             KnockoutModel m = Models.bind(new KnockoutModel(), newContext());
   835             m.applyBindings();
   836             
   837             int cnt = Utils.countChildren(KnockoutTest.class, "ul");
   838             assertEquals(cnt, 0, "No children " + cnt);
   839             
   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;
   843             
   844             assertEquals(len, 1, "One element in the array " + len);
   845             
   846             int newCnt = Utils.countChildren(KnockoutTest.class, "ul");
   847             assertEquals(newCnt, 1, "One child in the DOM: " + newCnt);
   848             
   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());
   852         } finally {
   853             Utils.exposeHTML(KnockoutTest.class, "");
   854         }
   855     }
   856 
   857     @Function
   858     static void call(KnockoutModel m, String data) {
   859         m.setName(data);
   860         m.setCallbackCount(m.getCallbackCount() + 1);
   861     }
   862 
   863     @Function
   864     static void removePerson(KnockoutModel model, Person data) {
   865         model.setCallbackCount(model.getCallbackCount() + 1);
   866         model.getPeople().remove(data);
   867     }
   868     
   869     
   870     @ComputedProperty
   871     static String helloMessage(String name) {
   872         return "Hello " + name + "!";
   873     }
   874     
   875     @ComputedProperty
   876     static List<String> cmpResults(List<String> results) {
   877         return results;
   878     }
   879     
   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 "
   890             + "}\n";
   891         Utils.executeScript(
   892             KnockoutTest.class,
   893             s, id);
   894     }
   895     private static void triggerChildClick(String id, int pos) throws Exception {
   896         String s = 
   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" +
   902             "var cnt = -1;\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" + 
   906             "}\n" + 
   907             "return null;\n";
   908         Utils.executeScript(
   909             KnockoutTest.class,
   910             s, id, pos);
   911     }
   912 
   913     private static String childText(String id, int pos) throws Exception {
   914         String s = 
   915             "var id = arguments[0]; var pos = arguments[1];" +
   916             "var e = window.document.getElementById(id);\n" +
   917             "var list = e.childNodes;\n" +
   918             "var cnt = -1;\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" + 
   922             "}\n" + 
   923             "return null;\n";
   924         return (String)Utils.executeScript(
   925             KnockoutTest.class,
   926             s, id, pos);
   927     }
   928 
   929     private static BrwsrCtx newContext() {
   930         return Utils.newContext(KnockoutTest.class);
   931     }
   932 }