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