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