json-tck/src/main/java/net/java/html/json/tests/KnockoutTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 28 May 2013 20:01:30 +0200
branchcontext
changeset 115 b236fc0949e0
parent 94 06fd367604d0
child 121 81e976eb0fc2
permissions -rw-r--r--
Create default instances and just bind them to unit testing context
jaroslav@34
     1
/**
jaroslav@34
     2
 * HTML via Java(tm) Language Bindings
jaroslav@34
     3
 * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@34
     4
 *
jaroslav@34
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@34
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@34
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@34
     8
 *
jaroslav@34
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@34
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@34
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@34
    12
 * GNU General Public License for more details. apidesign.org
jaroslav@34
    13
 * designates this particular file as subject to the
jaroslav@34
    14
 * "Classpath" exception as provided by apidesign.org
jaroslav@34
    15
 * in the License file that accompanied this code.
jaroslav@34
    16
 *
jaroslav@34
    17
 * You should have received a copy of the GNU General Public License
jaroslav@34
    18
 * along with this program. Look for COPYING file in the top folder.
jaroslav@34
    19
 * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
jaroslav@34
    20
 */
jaroslav@34
    21
package net.java.html.json.tests;
jaroslav@34
    22
jaroslav@34
    23
import java.util.List;
jaroslav@34
    24
import net.java.html.json.ComputedProperty;
jaroslav@34
    25
import net.java.html.json.Function;
jaroslav@34
    26
import net.java.html.json.Model;
jaroslav@115
    27
import net.java.html.json.Models;
jaroslav@34
    28
import net.java.html.json.Property;
jaroslav@34
    29
import org.apidesign.bck2brwsr.vmtest.BrwsrTest;
jaroslav@34
    30
import org.apidesign.bck2brwsr.vmtest.HtmlFragment;
jaroslav@34
    31
import org.apidesign.bck2brwsr.vmtest.VMTest;
jaroslav@34
    32
jaroslav@34
    33
/**
jaroslav@34
    34
 *
jaroslav@34
    35
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@34
    36
 */
jaroslav@34
    37
@Model(className="KnockoutModel", properties={
jaroslav@34
    38
    @Property(name="name", type=String.class),
jaroslav@34
    39
    @Property(name="results", type=String.class, array = true),
jaroslav@34
    40
    @Property(name="callbackCount", type=int.class),
jaroslav@94
    41
    @Property(name="people", type=PersonImpl.class, array = true),
jaroslav@94
    42
    @Property(name="enabled", type=boolean.class)
jaroslav@34
    43
}) 
jaroslav@34
    44
public final class KnockoutTest {
jaroslav@34
    45
    
jaroslav@34
    46
    @HtmlFragment(
jaroslav@34
    47
        "<h1 data-bind=\"text: helloMessage\">Loading Bck2Brwsr's Hello World...</h1>\n" +
jaroslav@34
    48
        "Your name: <input id='input' data-bind=\"value: name\"></input>\n" +
jaroslav@34
    49
        "<button id=\"hello\">Say Hello!</button>\n"
jaroslav@34
    50
    )
jaroslav@36
    51
    @BrwsrTest public void modifyValueAssertChangeInModel() throws Exception {
jaroslav@115
    52
        KnockoutModel m = Models.bind(new KnockoutModel(), Utils.newContext());
jaroslav@34
    53
        m.setName("Kukuc");
jaroslav@34
    54
        m.applyBindings();
jaroslav@34
    55
        
jaroslav@34
    56
        String v = getSetInput(null);
jaroslav@34
    57
        assert "Kukuc".equals(v) : "Value is really kukuc: " + v;
jaroslav@34
    58
        
jaroslav@34
    59
        getSetInput("Jardo");
jaroslav@34
    60
        triggerEvent("input", "change");
jaroslav@34
    61
        
jaroslav@34
    62
        assert "Jardo".equals(m.getName()) : "Name property updated: " + m.getName();
jaroslav@34
    63
    }
jaroslav@34
    64
    
jaroslav@36
    65
    private static String getSetInput(String value) throws Exception {
jaroslav@36
    66
        String s = "var value = arguments[0];\n"
jaroslav@36
    67
        + "var n = window.document.getElementById('input'); \n "
jaroslav@34
    68
        + "if (value != null) n['value'] = value; \n "
jaroslav@36
    69
        + "return n['value'];";
jaroslav@36
    70
        return (String)Utils.executeScript(s, value);
jaroslav@34
    71
    }
jaroslav@34
    72
    
jaroslav@36
    73
    public static void triggerEvent(String id, String ev) throws Exception {
jaroslav@36
    74
        Utils.executeScript(
jaroslav@36
    75
            "ko.utils.triggerEvent(window.document.getElementById(arguments[0]), arguments[1]);",
jaroslav@36
    76
            id, ev
jaroslav@36
    77
        );
jaroslav@34
    78
    }
jaroslav@34
    79
    
jaroslav@34
    80
    @HtmlFragment(
jaroslav@34
    81
        "<ul id='ul' data-bind='foreach: results'>\n"
jaroslav@34
    82
        + "  <li data-bind='text: $data, click: $root.call'/>\n"
jaroslav@34
    83
        + "</ul>\n"
jaroslav@34
    84
    )
jaroslav@36
    85
    @BrwsrTest public void displayContentOfArray() throws Exception {
jaroslav@115
    86
        KnockoutModel m = Models.bind(new KnockoutModel(), Utils.newContext());
jaroslav@34
    87
        m.getResults().add("Ahoj");
jaroslav@34
    88
        m.applyBindings();
jaroslav@34
    89
        
jaroslav@34
    90
        int cnt = countChildren("ul");
jaroslav@34
    91
        assert cnt == 1 : "One child, but was " + cnt;
jaroslav@34
    92
        
jaroslav@34
    93
        m.getResults().add("Hi");
jaroslav@34
    94
jaroslav@34
    95
        cnt = countChildren("ul");
jaroslav@34
    96
        assert cnt == 2 : "Two children now, but was " + cnt;
jaroslav@34
    97
        
jaroslav@34
    98
        triggerChildClick("ul", 1);
jaroslav@34
    99
        
jaroslav@34
   100
        assert 1 == m.getCallbackCount() : "One callback " + m.getCallbackCount();
jaroslav@34
   101
        assert "Hi".equals(m.getName()) : "We got callback from 2nd child " + m.getName();
jaroslav@34
   102
    }
jaroslav@94
   103
jaroslav@94
   104
    @HtmlFragment(
jaroslav@94
   105
        "<input type='checkbox' id='b' data-bind='checked: enabled'></input>\n"
jaroslav@94
   106
    )
jaroslav@94
   107
    @BrwsrTest public void checkBoxToBooleanBinding() throws Exception {
jaroslav@115
   108
        KnockoutModel m = Models.bind(new KnockoutModel(), Utils.newContext());
jaroslav@94
   109
        m.applyBindings();
jaroslav@94
   110
        
jaroslav@94
   111
        assert !m.isEnabled() : "Is disabled";
jaroslav@94
   112
jaroslav@94
   113
        triggerClick("b");
jaroslav@94
   114
        
jaroslav@94
   115
        assert m.isEnabled() : "Now the model is enabled";
jaroslav@94
   116
    }
jaroslav@94
   117
    
jaroslav@94
   118
    
jaroslav@34
   119
    
jaroslav@34
   120
    @HtmlFragment(
jaroslav@34
   121
        "<ul id='ul' data-bind='foreach: cmpResults'>\n"
jaroslav@34
   122
        + "  <li><b data-bind='text: $data'></b></li>\n"
jaroslav@34
   123
        + "</ul>\n"
jaroslav@34
   124
    )
jaroslav@36
   125
    @BrwsrTest public void displayContentOfDerivedArray() throws Exception {
jaroslav@115
   126
        KnockoutModel m = Models.bind(new KnockoutModel(), Utils.newContext());
jaroslav@34
   127
        m.getResults().add("Ahoj");
jaroslav@34
   128
        m.applyBindings();
jaroslav@34
   129
        
jaroslav@34
   130
        int cnt = countChildren("ul");
jaroslav@34
   131
        assert cnt == 1 : "One child, but was " + cnt;
jaroslav@34
   132
        
jaroslav@34
   133
        m.getResults().add("hello");
jaroslav@34
   134
jaroslav@34
   135
        cnt = countChildren("ul");
jaroslav@34
   136
        assert cnt == 2 : "Two children now, but was " + cnt;
jaroslav@34
   137
    }
jaroslav@34
   138
    
jaroslav@34
   139
    @HtmlFragment(
jaroslav@34
   140
        "<ul id='ul' data-bind='foreach: people'>\n"
jaroslav@34
   141
        + "  <li data-bind='text: $data.firstName, click: $root.removePerson'></li>\n"
jaroslav@34
   142
        + "</ul>\n"
jaroslav@34
   143
    )
jaroslav@36
   144
    @BrwsrTest public void displayContentOfArrayOfPeople() throws Exception {
jaroslav@115
   145
        KnockoutModel m = Models.bind(new KnockoutModel(), Utils.newContext());
jaroslav@34
   146
        
jaroslav@115
   147
        final Person first = Models.bind(new Person(), Utils.newContext());
jaroslav@34
   148
        first.setFirstName("first");
jaroslav@34
   149
        m.getPeople().add(first);
jaroslav@34
   150
        
jaroslav@34
   151
        m.applyBindings();
jaroslav@34
   152
        
jaroslav@34
   153
        int cnt = countChildren("ul");
jaroslav@34
   154
        assert cnt == 1 : "One child, but was " + cnt;
jaroslav@34
   155
        
jaroslav@115
   156
        final Person second = Models.bind(new Person(), Utils.newContext());
jaroslav@34
   157
        second.setFirstName("second");
jaroslav@34
   158
        m.getPeople().add(second);
jaroslav@34
   159
jaroslav@34
   160
        cnt = countChildren("ul");
jaroslav@34
   161
        assert cnt == 2 : "Two children now, but was " + cnt;
jaroslav@34
   162
jaroslav@34
   163
        triggerChildClick("ul", 1);
jaroslav@34
   164
        
jaroslav@34
   165
        assert 1 == m.getCallbackCount() : "One callback " + m.getCallbackCount();
jaroslav@34
   166
jaroslav@34
   167
        cnt = countChildren("ul");
jaroslav@34
   168
        assert cnt == 1 : "Again one child, but was " + cnt;
jaroslav@34
   169
        
jaroslav@34
   170
        String txt = childText("ul", 0);
jaroslav@34
   171
        assert "first".equals(txt) : "Expecting 'first': " + txt;
jaroslav@34
   172
        
jaroslav@34
   173
        first.setFirstName("changed");
jaroslav@34
   174
        
jaroslav@34
   175
        txt = childText("ul", 0);
jaroslav@34
   176
        assert "changed".equals(txt) : "Expecting 'changed': " + txt;
jaroslav@34
   177
    }
jaroslav@34
   178
    
jaroslav@34
   179
    @ComputedProperty
jaroslav@34
   180
    static Person firstPerson(List<Person> people) {
jaroslav@34
   181
        return people.isEmpty() ? null : people.get(0);
jaroslav@34
   182
    }
jaroslav@34
   183
    
jaroslav@34
   184
    @HtmlFragment(
jaroslav@34
   185
        "<p id='ul' data-bind='with: firstPerson'>\n"
jaroslav@34
   186
        + "  <span data-bind='text: firstName, click: changeSex'></span>\n"
jaroslav@34
   187
        + "</p>\n"
jaroslav@34
   188
    )
jaroslav@36
   189
    @BrwsrTest public void accessFirstPersonWithOnFunction() throws Exception {
jaroslav@34
   190
        trasfertToFemale();
jaroslav@34
   191
    }
jaroslav@34
   192
    
jaroslav@34
   193
    @HtmlFragment(
jaroslav@34
   194
        "<ul id='ul' data-bind='foreach: people'>\n"
jaroslav@34
   195
        + "  <li data-bind='text: $data.firstName, click: changeSex'></li>\n"
jaroslav@34
   196
        + "</ul>\n"
jaroslav@34
   197
    )
jaroslav@36
   198
    @BrwsrTest public void onPersonFunction() throws Exception {
jaroslav@34
   199
        trasfertToFemale();
jaroslav@34
   200
    }
jaroslav@34
   201
    
jaroslav@36
   202
    private void trasfertToFemale() throws Exception {
jaroslav@115
   203
        KnockoutModel m = Models.bind(new KnockoutModel(), Utils.newContext());
jaroslav@34
   204
jaroslav@115
   205
        final Person first = Models.bind(new Person(), Utils.newContext());
jaroslav@34
   206
        first.setFirstName("first");
jaroslav@34
   207
        first.setSex(Sex.MALE);
jaroslav@34
   208
        m.getPeople().add(first);
jaroslav@34
   209
jaroslav@34
   210
jaroslav@34
   211
        m.applyBindings();
jaroslav@34
   212
jaroslav@34
   213
        int cnt = countChildren("ul");
jaroslav@34
   214
        assert cnt == 1 : "One child, but was " + cnt;
jaroslav@34
   215
jaroslav@34
   216
jaroslav@34
   217
        triggerChildClick("ul", 0);
jaroslav@34
   218
jaroslav@34
   219
        assert first.getSex() == Sex.FEMALE : "Transverted to female: " + first.getSex();
jaroslav@34
   220
    }
jaroslav@34
   221
    
jaroslav@34
   222
    @Function
jaroslav@34
   223
    static void call(KnockoutModel m, String data) {
jaroslav@34
   224
        m.setName(data);
jaroslav@34
   225
        m.setCallbackCount(m.getCallbackCount() + 1);
jaroslav@34
   226
    }
jaroslav@34
   227
jaroslav@34
   228
    @Function
jaroslav@34
   229
    static void removePerson(KnockoutModel model, Person data) {
jaroslav@34
   230
        model.setCallbackCount(model.getCallbackCount() + 1);
jaroslav@34
   231
        model.getPeople().remove(data);
jaroslav@34
   232
    }
jaroslav@34
   233
    
jaroslav@34
   234
    
jaroslav@34
   235
    @ComputedProperty
jaroslav@34
   236
    static String helloMessage(String name) {
jaroslav@34
   237
        return "Hello " + name + "!";
jaroslav@34
   238
    }
jaroslav@34
   239
    
jaroslav@34
   240
    @ComputedProperty
jaroslav@34
   241
    static List<String> cmpResults(List<String> results) {
jaroslav@34
   242
        return results;
jaroslav@34
   243
    }
jaroslav@34
   244
    
jaroslav@34
   245
    static Object[] create() {
jaroslav@34
   246
        return VMTest.create(KnockoutTest.class);
jaroslav@34
   247
    }
jaroslav@34
   248
    
jaroslav@36
   249
    private static int countChildren(String id) throws Exception {
jaroslav@36
   250
        return ((Number)Utils.executeScript(
jaroslav@36
   251
          "var e = window.document.getElementById(arguments[0]);\n "
jaroslav@34
   252
        + "if (typeof e === 'undefined') return -2;\n "
jaroslav@36
   253
        + "return e.children.length;", 
jaroslav@36
   254
            id
jaroslav@36
   255
        )).intValue();
jaroslav@36
   256
    }
jaroslav@34
   257
jaroslav@94
   258
    private static void triggerClick(String id) throws Exception {
jaroslav@94
   259
        String s = "var id = arguments[0];"
jaroslav@94
   260
            + "var e = window.document.getElementById(id);\n "
jaroslav@94
   261
            + "var ev = window.document.createEvent('MouseEvents');\n "
jaroslav@94
   262
            + "ev.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);\n "
jaroslav@94
   263
            + "e.dispatchEvent(ev);\n ";
jaroslav@94
   264
        Utils.executeScript(s, id);
jaroslav@94
   265
    }
jaroslav@36
   266
    private static void triggerChildClick(String id, int pos) throws Exception {
jaroslav@36
   267
        String s = "var id = arguments[0]; var pos = arguments[1];"
jaroslav@36
   268
            + "var e = window.document.getElementById(id);\n "
jaroslav@36
   269
            + "var ev = window.document.createEvent('MouseEvents');\n "
jaroslav@36
   270
            + "ev.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);\n "
jaroslav@36
   271
            + "e.children[pos].dispatchEvent(ev);\n ";
jaroslav@36
   272
        Utils.executeScript(s, id, pos);
jaroslav@36
   273
    }
jaroslav@34
   274
jaroslav@36
   275
    private static String childText(String id, int pos) throws Exception {
jaroslav@36
   276
        String s = "var id = arguments[0]; var pos = arguments[1];"
jaroslav@36
   277
        + "var e = window.document.getElementById(id);\n "
jaroslav@34
   278
        + "var t = e.children[pos].innerHTML;\n "
jaroslav@36
   279
        + "return t ? t : null;";
jaroslav@36
   280
        return (String)Utils.executeScript(s, id, pos);
jaroslav@36
   281
    }
jaroslav@34
   282
}