json-tck/src/main/java/net/java/html/json/tests/KnockoutTest.java
author Jaroslav Tulach <jaroslav.tulach@netbeans.org>
Mon, 16 Dec 2013 17:39:56 +0100
changeset 365 5c93ad8c7a15
parent 358 80702021b851
child 393 8b025bcde7bb
permissions -rw-r--r--
The work on this project started in 2013
jaroslav@34
     1
/**
jaroslav@358
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jaroslav@34
     3
 *
jaroslav@365
     4
 * Copyright 2013-2013 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@358
    30
 * Software is Oracle. Portions Copyright 2013-2013 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
jaroslav@34
    45
import java.util.List;
jaroslav@121
    46
import net.java.html.BrwsrCtx;
jaroslav@34
    47
import net.java.html.json.ComputedProperty;
jaroslav@34
    48
import net.java.html.json.Function;
jaroslav@34
    49
import net.java.html.json.Model;
jaroslav@115
    50
import net.java.html.json.Models;
jaroslav@34
    51
import net.java.html.json.Property;
jaroslav@137
    52
import org.apidesign.html.json.tck.KOTest;
jaroslav@34
    53
jaroslav@34
    54
/**
jaroslav@34
    55
 *
jaroslav@34
    56
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@34
    57
 */
jaroslav@34
    58
@Model(className="KnockoutModel", properties={
jaroslav@34
    59
    @Property(name="name", type=String.class),
jaroslav@34
    60
    @Property(name="results", type=String.class, array = true),
jaroslav@34
    61
    @Property(name="callbackCount", type=int.class),
jaroslav@94
    62
    @Property(name="people", type=PersonImpl.class, array = true),
jaroslav@197
    63
    @Property(name="enabled", type=boolean.class),
jaroslav@197
    64
    @Property(name="latitude", type=double.class)
jaroslav@34
    65
}) 
jaroslav@34
    66
public final class KnockoutTest {
jaroslav@34
    67
    
jaroslav@197
    68
    @KOTest public void modifyValueAssertChangeInModelOnDouble() throws Throwable {
jaroslav@197
    69
        Object exp = Utils.exposeHTML(KnockoutTest.class, 
jaroslav@197
    70
            "Latitude: <input id='input' data-bind=\"value: latitude\"></input>\n"
jaroslav@197
    71
        );
jaroslav@197
    72
        try {
jaroslav@197
    73
jaroslav@197
    74
            KnockoutModel m = Models.bind(new KnockoutModel(), newContext());
jaroslav@197
    75
            m.setLatitude(50.5);
jaroslav@197
    76
            m.applyBindings();
jaroslav@197
    77
jaroslav@197
    78
            String v = getSetInput(null);
jaroslav@197
    79
            assert "50.5".equals(v) : "Value is really 50.5: " + v;
jaroslav@197
    80
jaroslav@197
    81
            getSetInput("49.5");
jaroslav@197
    82
            triggerEvent("input", "change");
jaroslav@197
    83
jaroslav@197
    84
            assert 49.5 == m.getLatitude() : "Double property updated: " + m.getLatitude();
jaroslav@197
    85
        } catch (Throwable t) {
jaroslav@197
    86
            throw t;
jaroslav@197
    87
        } finally {
jaroslav@197
    88
            Utils.exposeHTML(KnockoutTest.class, "");
jaroslav@197
    89
        }
jaroslav@197
    90
    }
jaroslav@197
    91
    
jaroslav@197
    92
    @KOTest public void modifyValueAssertChangeInModelOnBoolean() throws Throwable {
jaroslav@197
    93
        Object exp = Utils.exposeHTML(KnockoutTest.class, 
jaroslav@197
    94
            "Latitude: <input id='input' data-bind=\"value: enabled\"></input>\n"
jaroslav@197
    95
        );
jaroslav@197
    96
        try {
jaroslav@197
    97
jaroslav@197
    98
            KnockoutModel m = Models.bind(new KnockoutModel(), newContext());
jaroslav@197
    99
            m.setEnabled(true);
jaroslav@197
   100
            m.applyBindings();
jaroslav@197
   101
jaroslav@197
   102
            String v = getSetInput(null);
jaroslav@197
   103
            assert "true".equals(v) : "Value is really true: " + v;
jaroslav@197
   104
jaroslav@197
   105
            getSetInput("false");
jaroslav@197
   106
            triggerEvent("input", "change");
jaroslav@197
   107
jaroslav@197
   108
            assert false == m.isEnabled(): "Boolean property updated: " + m.isEnabled();
jaroslav@197
   109
        } catch (Throwable t) {
jaroslav@197
   110
            throw t;
jaroslav@197
   111
        } finally {
jaroslav@197
   112
            Utils.exposeHTML(KnockoutTest.class, "");
jaroslav@197
   113
        }
jaroslav@197
   114
    }
jaroslav@197
   115
    
jaroslav@137
   116
    @KOTest public void modifyValueAssertChangeInModel() throws Exception {
jaroslav@137
   117
        Object exp = Utils.exposeHTML(KnockoutTest.class, 
jaroslav@137
   118
            "<h1 data-bind=\"text: helloMessage\">Loading Bck2Brwsr's Hello World...</h1>\n" +
jaroslav@137
   119
            "Your name: <input id='input' data-bind=\"value: name\"></input>\n" +
jaroslav@137
   120
            "<button id=\"hello\">Say Hello!</button>\n"
jaroslav@137
   121
        );
jaroslav@141
   122
        try {
jaroslav@137
   123
jaroslav@141
   124
            KnockoutModel m = Models.bind(new KnockoutModel(), newContext());
jaroslav@141
   125
            m.setName("Kukuc");
jaroslav@141
   126
            m.applyBindings();
jaroslav@137
   127
jaroslav@141
   128
            String v = getSetInput(null);
jaroslav@141
   129
            assert "Kukuc".equals(v) : "Value is really kukuc: " + v;
jaroslav@137
   130
jaroslav@141
   131
            getSetInput("Jardo");
jaroslav@141
   132
            triggerEvent("input", "change");
jaroslav@137
   133
jaroslav@141
   134
            assert "Jardo".equals(m.getName()) : "Name property updated: " + m.getName();
jaroslav@141
   135
        } finally {
jaroslav@141
   136
            Utils.exposeHTML(KnockoutTest.class, "");
jaroslav@141
   137
        }
jaroslav@34
   138
    }
jaroslav@34
   139
    
jaroslav@36
   140
    private static String getSetInput(String value) throws Exception {
jaroslav@36
   141
        String s = "var value = arguments[0];\n"
jaroslav@36
   142
        + "var n = window.document.getElementById('input'); \n "
jaroslav@34
   143
        + "if (value != null) n['value'] = value; \n "
jaroslav@36
   144
        + "return n['value'];";
jaroslav@121
   145
        return (String)Utils.executeScript(
jaroslav@121
   146
            KnockoutTest.class,
jaroslav@121
   147
            s, value
jaroslav@121
   148
        );
jaroslav@34
   149
    }
jaroslav@34
   150
    
jaroslav@36
   151
    public static void triggerEvent(String id, String ev) throws Exception {
jaroslav@36
   152
        Utils.executeScript(
jaroslav@121
   153
            KnockoutTest.class,
jaroslav@36
   154
            "ko.utils.triggerEvent(window.document.getElementById(arguments[0]), arguments[1]);",
jaroslav@36
   155
            id, ev
jaroslav@36
   156
        );
jaroslav@34
   157
    }
jaroslav@34
   158
    
jaroslav@137
   159
    @KOTest public void displayContentOfArray() throws Exception {
jaroslav@137
   160
        Object exp = Utils.exposeHTML(KnockoutTest.class, 
jaroslav@137
   161
            "<ul id='ul' data-bind='foreach: results'>\n"
jaroslav@137
   162
            + "  <li data-bind='text: $data, click: $root.call'/>\n"
jaroslav@137
   163
            + "</ul>\n"
jaroslav@137
   164
        );
jaroslav@141
   165
        try {
jaroslav@141
   166
            KnockoutModel m = Models.bind(new KnockoutModel(), newContext());
jaroslav@141
   167
            m.getResults().add("Ahoj");
jaroslav@141
   168
            m.applyBindings();
jaroslav@137
   169
jaroslav@141
   170
            int cnt = countChildren("ul");
jaroslav@141
   171
            assert cnt == 1 : "One child, but was " + cnt;
jaroslav@137
   172
jaroslav@141
   173
            m.getResults().add("Hi");
jaroslav@137
   174
jaroslav@141
   175
            cnt = countChildren("ul");
jaroslav@141
   176
            assert cnt == 2 : "Two children now, but was " + cnt;
jaroslav@34
   177
jaroslav@141
   178
            triggerChildClick("ul", 1);
jaroslav@137
   179
jaroslav@141
   180
            assert 1 == m.getCallbackCount() : "One callback " + m.getCallbackCount();
jaroslav@141
   181
            assert "Hi".equals(m.getName()) : "We got callback from 2nd child " + m.getName();
jaroslav@141
   182
        } finally {
jaroslav@141
   183
            Utils.exposeHTML(KnockoutTest.class, "");
jaroslav@141
   184
        }
jaroslav@34
   185
    }
jaroslav@235
   186
    
jaroslav@235
   187
    @KOTest public void displayContentOfComputedArray() throws Exception {
jaroslav@235
   188
        Object exp = Utils.exposeHTML(KnockoutTest.class, 
jaroslav@235
   189
            "<ul id='ul' data-bind='foreach: bothNames'>\n"
jaroslav@235
   190
            + "  <li data-bind='text: $data, click: $root.assignFirstName'/>\n"
jaroslav@235
   191
            + "</ul>\n"
jaroslav@235
   192
        );
jaroslav@235
   193
        try {
jaroslav@236
   194
            Pair m = Models.bind(new Pair("First", "Last", null), newContext());
jaroslav@236
   195
            m.applyBindings();
jaroslav@236
   196
jaroslav@236
   197
            int cnt = countChildren("ul");
jaroslav@236
   198
            assert cnt == 2 : "Two children now, but was " + cnt;
jaroslav@236
   199
jaroslav@236
   200
            triggerChildClick("ul", 1);
jaroslav@236
   201
jaroslav@236
   202
            assert "Last".equals(m.getFirstName()) : "We got callback from 2nd child " + m.getFirstName();
jaroslav@236
   203
        } finally {
jaroslav@236
   204
            Utils.exposeHTML(KnockoutTest.class, "");
jaroslav@236
   205
        }
jaroslav@236
   206
    }
jaroslav@236
   207
    
jaroslav@236
   208
    @KOTest public void displayContentOfComputedArrayOnASubpair() throws Exception {
jaroslav@236
   209
        Object exp = Utils.exposeHTML(KnockoutTest.class, 
jaroslav@236
   210
              "<div data-bind='with: next'>\n"
jaroslav@236
   211
            + "<ul id='ul' data-bind='foreach: bothNames'>\n"
jaroslav@236
   212
            + "  <li data-bind='text: $data, click: $root.assignFirstName'/>\n"
jaroslav@236
   213
            + "</ul>"
jaroslav@236
   214
            + "</div>\n"
jaroslav@236
   215
        );
jaroslav@236
   216
        try {
jaroslav@236
   217
            Pair m = Models.bind(new Pair(null, null, new Pair("First", "Last", null)), newContext());
jaroslav@236
   218
            m.applyBindings();
jaroslav@236
   219
jaroslav@236
   220
            int cnt = countChildren("ul");
jaroslav@236
   221
            assert cnt == 2 : "Two children now, but was " + cnt;
jaroslav@236
   222
jaroslav@236
   223
            triggerChildClick("ul", 1);
jaroslav@236
   224
jaroslav@236
   225
            assert "Last".equals(m.getFirstName()) : "We got callback from 2nd child " + m.getFirstName();
jaroslav@236
   226
        } finally {
jaroslav@236
   227
            Utils.exposeHTML(KnockoutTest.class, "");
jaroslav@236
   228
        }
jaroslav@236
   229
    }
jaroslav@236
   230
    
jaroslav@236
   231
    @KOTest public void displayContentOfComputedArrayOnComputedASubpair() throws Exception {
jaroslav@236
   232
        Object exp = Utils.exposeHTML(KnockoutTest.class, 
jaroslav@236
   233
              "<div data-bind='with: nextOne'>\n"
jaroslav@236
   234
            + "<ul id='ul' data-bind='foreach: bothNames'>\n"
jaroslav@236
   235
            + "  <li data-bind='text: $data, click: $root.assignFirstName'/>\n"
jaroslav@236
   236
            + "</ul>"
jaroslav@236
   237
            + "</div>\n"
jaroslav@236
   238
        );
jaroslav@236
   239
        try {
jaroslav@236
   240
            Pair m = Models.bind(new Pair(null, null, new Pair("First", "Last", null)), newContext());
jaroslav@235
   241
            m.applyBindings();
jaroslav@235
   242
jaroslav@235
   243
            int cnt = countChildren("ul");
jaroslav@235
   244
            assert cnt == 2 : "Two children now, but was " + cnt;
jaroslav@235
   245
jaroslav@235
   246
            triggerChildClick("ul", 1);
jaroslav@235
   247
jaroslav@235
   248
            assert "Last".equals(m.getFirstName()) : "We got callback from 2nd child " + m.getFirstName();
jaroslav@235
   249
        } finally {
jaroslav@235
   250
            Utils.exposeHTML(KnockoutTest.class, "");
jaroslav@235
   251
        }
jaroslav@235
   252
    }
jaroslav@94
   253
jaroslav@137
   254
    @KOTest public void checkBoxToBooleanBinding() throws Exception {
jaroslav@137
   255
        Object exp = Utils.exposeHTML(KnockoutTest.class, 
jaroslav@137
   256
            "<input type='checkbox' id='b' data-bind='checked: enabled'></input>\n"
jaroslav@137
   257
        );
jaroslav@141
   258
        try {
jaroslav@141
   259
            KnockoutModel m = Models.bind(new KnockoutModel(), newContext());
jaroslav@141
   260
            m.applyBindings();
jaroslav@137
   261
jaroslav@141
   262
            assert !m.isEnabled() : "Is disabled";
jaroslav@137
   263
jaroslav@141
   264
            triggerClick("b");
jaroslav@94
   265
jaroslav@141
   266
            assert m.isEnabled() : "Now the model is enabled";
jaroslav@141
   267
        } finally {
jaroslav@141
   268
            Utils.exposeHTML(KnockoutTest.class, "");
jaroslav@141
   269
        }
jaroslav@94
   270
    }
jaroslav@94
   271
    
jaroslav@94
   272
    
jaroslav@34
   273
    
jaroslav@137
   274
    @KOTest public void displayContentOfDerivedArray() throws Exception {
jaroslav@137
   275
        Object exp = Utils.exposeHTML(KnockoutTest.class, 
jaroslav@137
   276
            "<ul id='ul' data-bind='foreach: cmpResults'>\n"
jaroslav@137
   277
            + "  <li><b data-bind='text: $data'></b></li>\n"
jaroslav@137
   278
            + "</ul>\n"
jaroslav@137
   279
        );
jaroslav@141
   280
        try {
jaroslav@141
   281
            KnockoutModel m = Models.bind(new KnockoutModel(), newContext());
jaroslav@141
   282
            m.getResults().add("Ahoj");
jaroslav@141
   283
            m.applyBindings();
jaroslav@137
   284
jaroslav@141
   285
            int cnt = countChildren("ul");
jaroslav@141
   286
            assert cnt == 1 : "One child, but was " + cnt;
jaroslav@137
   287
jaroslav@141
   288
            m.getResults().add("hello");
jaroslav@137
   289
jaroslav@141
   290
            cnt = countChildren("ul");
jaroslav@141
   291
            assert cnt == 2 : "Two children now, but was " + cnt;
jaroslav@141
   292
        } finally {
jaroslav@141
   293
            Utils.exposeHTML(KnockoutTest.class, "");
jaroslav@141
   294
        }
jaroslav@34
   295
    }
jaroslav@34
   296
    
jaroslav@137
   297
    @KOTest public void displayContentOfArrayOfPeople() throws Exception {
jaroslav@137
   298
        Object exp = Utils.exposeHTML(KnockoutTest.class, 
jaroslav@137
   299
            "<ul id='ul' data-bind='foreach: people'>\n"
jaroslav@137
   300
            + "  <li data-bind='text: $data.firstName, click: $root.removePerson'></li>\n"
jaroslav@137
   301
            + "</ul>\n"
jaroslav@137
   302
        );
jaroslav@141
   303
        try {
jaroslav@141
   304
            KnockoutModel m = Models.bind(new KnockoutModel(), newContext());
jaroslav@137
   305
jaroslav@141
   306
            final Person first = Models.bind(new Person(), newContext());
jaroslav@141
   307
            first.setFirstName("first");
jaroslav@141
   308
            m.getPeople().add(first);
jaroslav@137
   309
jaroslav@141
   310
            m.applyBindings();
jaroslav@137
   311
jaroslav@141
   312
            int cnt = countChildren("ul");
jaroslav@141
   313
            assert cnt == 1 : "One child, but was " + cnt;
jaroslav@137
   314
jaroslav@141
   315
            final Person second = Models.bind(new Person(), newContext());
jaroslav@141
   316
            second.setFirstName("second");
jaroslav@141
   317
            m.getPeople().add(second);
jaroslav@137
   318
jaroslav@141
   319
            cnt = countChildren("ul");
jaroslav@141
   320
            assert cnt == 2 : "Two children now, but was " + cnt;
jaroslav@34
   321
jaroslav@141
   322
            triggerChildClick("ul", 1);
jaroslav@34
   323
jaroslav@141
   324
            assert 1 == m.getCallbackCount() : "One callback " + m.getCallbackCount();
jaroslav@137
   325
jaroslav@141
   326
            cnt = countChildren("ul");
jaroslav@141
   327
            assert cnt == 1 : "Again one child, but was " + cnt;
jaroslav@34
   328
jaroslav@141
   329
            String txt = childText("ul", 0);
jaroslav@141
   330
            assert "first".equals(txt) : "Expecting 'first': " + txt;
jaroslav@137
   331
jaroslav@141
   332
            first.setFirstName("changed");
jaroslav@137
   333
jaroslav@141
   334
            txt = childText("ul", 0);
jaroslav@141
   335
            assert "changed".equals(txt) : "Expecting 'changed': " + txt;
jaroslav@141
   336
        } finally {
jaroslav@141
   337
            Utils.exposeHTML(KnockoutTest.class, "");
jaroslav@141
   338
        }
jaroslav@34
   339
    }
jaroslav@34
   340
    
jaroslav@34
   341
    @ComputedProperty
jaroslav@34
   342
    static Person firstPerson(List<Person> people) {
jaroslav@34
   343
        return people.isEmpty() ? null : people.get(0);
jaroslav@34
   344
    }
jaroslav@34
   345
    
jaroslav@137
   346
    @KOTest public void accessFirstPersonWithOnFunction() throws Exception {
jaroslav@137
   347
        Object exp = Utils.exposeHTML(KnockoutTest.class, 
jaroslav@137
   348
            "<p id='ul' data-bind='with: firstPerson'>\n"
jaroslav@137
   349
            + "  <span data-bind='text: firstName, click: changeSex'></span>\n"
jaroslav@137
   350
            + "</p>\n"
jaroslav@137
   351
        );
jaroslav@141
   352
        try {
jaroslav@141
   353
            trasfertToFemale();
jaroslav@141
   354
        } finally {
jaroslav@141
   355
            Utils.exposeHTML(KnockoutTest.class, "");
jaroslav@141
   356
        }
jaroslav@34
   357
    }
jaroslav@34
   358
    
jaroslav@137
   359
    @KOTest public void onPersonFunction() throws Exception {
jaroslav@137
   360
        Object exp = Utils.exposeHTML(KnockoutTest.class, 
jaroslav@137
   361
            "<ul id='ul' data-bind='foreach: people'>\n"
jaroslav@137
   362
            + "  <li data-bind='text: $data.firstName, click: changeSex'></li>\n"
jaroslav@137
   363
            + "</ul>\n"
jaroslav@137
   364
        );
jaroslav@141
   365
        try {
jaroslav@141
   366
            trasfertToFemale();
jaroslav@141
   367
        } finally {
jaroslav@141
   368
            Utils.exposeHTML(KnockoutTest.class, "");
jaroslav@141
   369
        }
jaroslav@34
   370
    }
jaroslav@34
   371
    
jaroslav@36
   372
    private void trasfertToFemale() throws Exception {
jaroslav@121
   373
        KnockoutModel m = Models.bind(new KnockoutModel(), newContext());
jaroslav@34
   374
jaroslav@121
   375
        final Person first = Models.bind(new Person(), newContext());
jaroslav@34
   376
        first.setFirstName("first");
jaroslav@34
   377
        first.setSex(Sex.MALE);
jaroslav@34
   378
        m.getPeople().add(first);
jaroslav@34
   379
jaroslav@34
   380
jaroslav@34
   381
        m.applyBindings();
jaroslav@34
   382
jaroslav@34
   383
        int cnt = countChildren("ul");
jaroslav@34
   384
        assert cnt == 1 : "One child, but was " + cnt;
jaroslav@34
   385
jaroslav@34
   386
jaroslav@34
   387
        triggerChildClick("ul", 0);
jaroslav@34
   388
jaroslav@34
   389
        assert first.getSex() == Sex.FEMALE : "Transverted to female: " + first.getSex();
jaroslav@34
   390
    }
jaroslav@34
   391
    
jaroslav@34
   392
    @Function
jaroslav@34
   393
    static void call(KnockoutModel m, String data) {
jaroslav@34
   394
        m.setName(data);
jaroslav@34
   395
        m.setCallbackCount(m.getCallbackCount() + 1);
jaroslav@34
   396
    }
jaroslav@34
   397
jaroslav@34
   398
    @Function
jaroslav@34
   399
    static void removePerson(KnockoutModel model, Person data) {
jaroslav@34
   400
        model.setCallbackCount(model.getCallbackCount() + 1);
jaroslav@34
   401
        model.getPeople().remove(data);
jaroslav@34
   402
    }
jaroslav@34
   403
    
jaroslav@34
   404
    
jaroslav@34
   405
    @ComputedProperty
jaroslav@34
   406
    static String helloMessage(String name) {
jaroslav@34
   407
        return "Hello " + name + "!";
jaroslav@34
   408
    }
jaroslav@34
   409
    
jaroslav@34
   410
    @ComputedProperty
jaroslav@34
   411
    static List<String> cmpResults(List<String> results) {
jaroslav@34
   412
        return results;
jaroslav@34
   413
    }
jaroslav@34
   414
    
jaroslav@36
   415
    private static int countChildren(String id) throws Exception {
jaroslav@36
   416
        return ((Number)Utils.executeScript(
jaroslav@121
   417
          KnockoutTest.class,
jaroslav@36
   418
          "var e = window.document.getElementById(arguments[0]);\n "
jaroslav@34
   419
        + "if (typeof e === 'undefined') return -2;\n "
jaroslav@36
   420
        + "return e.children.length;", 
jaroslav@36
   421
            id
jaroslav@36
   422
        )).intValue();
jaroslav@36
   423
    }
jaroslav@34
   424
jaroslav@94
   425
    private static void triggerClick(String id) throws Exception {
jaroslav@94
   426
        String s = "var id = arguments[0];"
jaroslav@94
   427
            + "var e = window.document.getElementById(id);\n "
jaroslav@94
   428
            + "var ev = window.document.createEvent('MouseEvents');\n "
jaroslav@94
   429
            + "ev.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);\n "
jaroslav@94
   430
            + "e.dispatchEvent(ev);\n ";
jaroslav@121
   431
        Utils.executeScript(
jaroslav@121
   432
            KnockoutTest.class,
jaroslav@121
   433
            s, id);
jaroslav@94
   434
    }
jaroslav@36
   435
    private static void triggerChildClick(String id, int pos) throws Exception {
jaroslav@36
   436
        String s = "var id = arguments[0]; var pos = arguments[1];"
jaroslav@36
   437
            + "var e = window.document.getElementById(id);\n "
jaroslav@36
   438
            + "var ev = window.document.createEvent('MouseEvents');\n "
jaroslav@36
   439
            + "ev.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);\n "
jaroslav@36
   440
            + "e.children[pos].dispatchEvent(ev);\n ";
jaroslav@121
   441
        Utils.executeScript(
jaroslav@121
   442
            KnockoutTest.class,
jaroslav@121
   443
            s, id, pos);
jaroslav@36
   444
    }
jaroslav@34
   445
jaroslav@36
   446
    private static String childText(String id, int pos) throws Exception {
jaroslav@36
   447
        String s = "var id = arguments[0]; var pos = arguments[1];"
jaroslav@36
   448
        + "var e = window.document.getElementById(id);\n "
jaroslav@34
   449
        + "var t = e.children[pos].innerHTML;\n "
jaroslav@36
   450
        + "return t ? t : null;";
jaroslav@121
   451
        return (String)Utils.executeScript(
jaroslav@121
   452
            KnockoutTest.class,
jaroslav@121
   453
            s, id, pos);
jaroslav@121
   454
    }
jaroslav@121
   455
jaroslav@121
   456
    private static BrwsrCtx newContext() {
jaroslav@121
   457
        return Utils.newContext(KnockoutTest.class);
jaroslav@36
   458
    }
jaroslav@34
   459
}