javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/KnockoutTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 02 Apr 2013 15:40:08 +0200
branchmodel
changeset 914 81dcd71877d5
parent 909 e51a474fcf79
child 929 b43aaf398748
permissions -rw-r--r--
@OnFunction can be in @Model classes
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, version 2 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://opensource.org/licenses/GPL-2.0.
    17  */
    18 package org.apidesign.bck2brwsr.htmlpage;
    19 
    20 import java.util.List;
    21 import org.apidesign.bck2brwsr.core.JavaScriptBody;
    22 import org.apidesign.bck2brwsr.htmlpage.api.ComputedProperty;
    23 import org.apidesign.bck2brwsr.htmlpage.api.OnEvent;
    24 import org.apidesign.bck2brwsr.htmlpage.api.OnFunction;
    25 import org.apidesign.bck2brwsr.htmlpage.api.Page;
    26 import org.apidesign.bck2brwsr.htmlpage.api.Property;
    27 import org.apidesign.bck2brwsr.vmtest.BrwsrTest;
    28 import org.apidesign.bck2brwsr.vmtest.HtmlFragment;
    29 import org.apidesign.bck2brwsr.vmtest.VMTest;
    30 import org.testng.annotations.Factory;
    31 
    32 /**
    33  *
    34  * @author Jaroslav Tulach <jtulach@netbeans.org>
    35  */
    36 @Page(xhtml="Knockout.xhtml", className="KnockoutModel", properties={
    37     @Property(name="name", type=String.class),
    38     @Property(name="results", type=String.class, array = true),
    39     @Property(name="callbackCount", type=int.class),
    40     @Property(name="people", type=PersonImpl.class, array = true)
    41 }) 
    42 public class KnockoutTest {
    43     
    44     @HtmlFragment(
    45         "<h1 data-bind=\"text: helloMessage\">Loading Bck2Brwsr's Hello World...</h1>\n" +
    46         "Your name: <input id='input' data-bind=\"value: name\"></input>\n" +
    47         "<button id=\"hello\">Say Hello!</button>\n"
    48     )
    49     @BrwsrTest public void modifyValueAssertChangeInModel() {
    50         KnockoutModel m = new KnockoutModel();
    51         m.setName("Kukuc");
    52         m.applyBindings();
    53         assert "Kukuc".equals(m.input.getValue()) : "Value is really kukuc: " + m.input.getValue();
    54         m.input.setValue("Jardo");
    55         m.triggerEvent(m.input, OnEvent.CHANGE);
    56         assert "Jardo".equals(m.getName()) : "Name property updated: " + m.getName();
    57     }
    58     
    59     @HtmlFragment(
    60         "<ul id='ul' data-bind='foreach: results'>\n"
    61         + "  <li data-bind='text: $data, click: $root.call'/>\n"
    62         + "</ul>\n"
    63     )
    64     @BrwsrTest public void displayContentOfArray() {
    65         KnockoutModel m = new KnockoutModel();
    66         m.getResults().add("Ahoj");
    67         m.applyBindings();
    68         
    69         int cnt = countChildren("ul");
    70         assert cnt == 1 : "One child, but was " + cnt;
    71         
    72         m.getResults().add("Hi");
    73 
    74         cnt = countChildren("ul");
    75         assert cnt == 2 : "Two children now, but was " + cnt;
    76         
    77         triggerChildClick("ul", 1);
    78         
    79         assert 1 == m.getCallbackCount() : "One callback " + m.getCallbackCount();
    80         assert "Hi".equals(m.getName()) : "We got callback from 2nd child " + m.getName();
    81     }
    82     
    83     @HtmlFragment(
    84         "<ul id='ul' data-bind='foreach: cmpResults'>\n"
    85         + "  <li><b data-bind='text: $data'></b></li>\n"
    86         + "</ul>\n"
    87     )
    88     @BrwsrTest public void displayContentOfDerivedArray() {
    89         KnockoutModel m = new KnockoutModel();
    90         m.getResults().add("Ahoj");
    91         m.applyBindings();
    92         
    93         int cnt = countChildren("ul");
    94         assert cnt == 1 : "One child, but was " + cnt;
    95         
    96         m.getResults().add("hello");
    97 
    98         cnt = countChildren("ul");
    99         assert cnt == 2 : "Two children now, but was " + cnt;
   100     }
   101     
   102     @HtmlFragment(
   103         "<ul id='ul' data-bind='foreach: people'>\n"
   104         + "  <li data-bind='text: $data.firstName, click: $root.removePerson'></li>\n"
   105         + "</ul>\n"
   106     )
   107     @BrwsrTest public void displayContentOfArrayOfPeople() {
   108         KnockoutModel m = new KnockoutModel();
   109         
   110         final Person first = new Person();
   111         first.setFirstName("first");
   112         m.getPeople().add(first);
   113         
   114         m.applyBindings();
   115         
   116         int cnt = countChildren("ul");
   117         assert cnt == 1 : "One child, but was " + cnt;
   118         
   119         final Person second = new Person();
   120         second.setFirstName("second");
   121         m.getPeople().add(second);
   122 
   123         cnt = countChildren("ul");
   124         assert cnt == 2 : "Two children now, but was " + cnt;
   125 
   126         triggerChildClick("ul", 1);
   127         
   128         assert 1 == m.getCallbackCount() : "One callback " + m.getCallbackCount();
   129 
   130         cnt = countChildren("ul");
   131         assert cnt == 1 : "Again one child, but was " + cnt;
   132         
   133         String txt = childText("ul", 0);
   134         assert "first".equals(txt) : "Expecting 'first': " + txt;
   135         
   136         first.setFirstName("changed");
   137         
   138         txt = childText("ul", 0);
   139         assert "changed".equals(txt) : "Expecting 'changed': " + txt;
   140     }
   141     
   142     @HtmlFragment(
   143         "<ul id='ul' data-bind='foreach: people'>\n"
   144         + "  <li data-bind='text: $data.firstName, click: changeSex'></li>\n"
   145         + "</ul>\n"
   146     )
   147     @BrwsrTest public void onPersonFunction() {
   148         KnockoutModel m = new KnockoutModel();
   149         
   150         final Person first = new Person();
   151         first.setFirstName("first");
   152         first.setSex(Sex.MALE);
   153         m.getPeople().add(first);
   154         
   155         
   156         m.applyBindings();
   157         
   158         int cnt = countChildren("ul");
   159         assert cnt == 1 : "One child, but was " + cnt;
   160         
   161         
   162         triggerChildClick("ul", 0);
   163         
   164         assert first.getSex() == Sex.FEMALE : "Transverted to female: " + first.getSex();
   165     }
   166      
   167     @OnFunction
   168     static void call(KnockoutModel m, String data) {
   169         m.setName(data);
   170         m.setCallbackCount(m.getCallbackCount() + 1);
   171     }
   172 
   173     @OnFunction
   174     static void removePerson(KnockoutModel model, Person data) {
   175         model.setCallbackCount(model.getCallbackCount() + 1);
   176         model.getPeople().remove(data);
   177     }
   178     
   179     
   180     @ComputedProperty
   181     static String helloMessage(String name) {
   182         return "Hello " + name + "!";
   183     }
   184     
   185     @ComputedProperty
   186     static List<String> cmpResults(List<String> results) {
   187         return results;
   188     }
   189     
   190     @Factory
   191     public static Object[] create() {
   192         return VMTest.create(KnockoutTest.class);
   193     }
   194     
   195     @JavaScriptBody(args = { "id" }, body = 
   196           "var e = window.document.getElementById(id);\n "
   197         + "if (typeof e === 'undefined') return -2;\n "
   198         + "return e.children.length;\n "
   199     )
   200     private static native int countChildren(String id);
   201 
   202     @JavaScriptBody(args = { "id", "pos" }, body = 
   203           "var e = window.document.getElementById(id);\n "
   204         + "var ev = window.document.createEvent('MouseEvents');\n "
   205         + "ev.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);\n "
   206         + "e.children[pos].dispatchEvent(ev);\n "
   207     )
   208     private static native void triggerChildClick(String id, int pos);
   209 
   210     @JavaScriptBody(args = { "id", "pos" }, body = 
   211           "var e = window.document.getElementById(id);\n "
   212         + "var t = e.children[pos].innerHTML;\n "
   213         + "return t ? t : null;"
   214     )
   215     private static native String childText(String id, int pos);
   216 }