javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/KnockoutTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 31 Mar 2013 12:01:38 +0200
branchmodel
changeset 909 e51a474fcf79
parent 908 3e023bea2da4
child 914 81dcd71877d5
permissions -rw-r--r--
Mutable properties on 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     @OnFunction
   143     static void call(KnockoutModel m, String data) {
   144         m.setName(data);
   145         m.setCallbackCount(m.getCallbackCount() + 1);
   146     }
   147 
   148     @OnFunction
   149     static void removePerson(KnockoutModel model, Person data) {
   150         model.setCallbackCount(model.getCallbackCount() + 1);
   151         model.getPeople().remove(data);
   152     }
   153     
   154     
   155     @ComputedProperty
   156     static String helloMessage(String name) {
   157         return "Hello " + name + "!";
   158     }
   159     
   160     @ComputedProperty
   161     static List<String> cmpResults(List<String> results) {
   162         return results;
   163     }
   164     
   165     @Factory
   166     public static Object[] create() {
   167         return VMTest.create(KnockoutTest.class);
   168     }
   169     
   170     @JavaScriptBody(args = { "id" }, body = 
   171           "var e = window.document.getElementById(id);\n "
   172         + "if (typeof e === 'undefined') return -2;\n "
   173         + "return e.children.length;\n "
   174     )
   175     private static native int countChildren(String id);
   176 
   177     @JavaScriptBody(args = { "id", "pos" }, body = 
   178           "var e = window.document.getElementById(id);\n "
   179         + "var ev = window.document.createEvent('MouseEvents');\n "
   180         + "ev.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);\n "
   181         + "e.children[pos].dispatchEvent(ev);\n "
   182     )
   183     private static native void triggerChildClick(String id, int pos);
   184 
   185     @JavaScriptBody(args = { "id", "pos" }, body = 
   186           "var e = window.document.getElementById(id);\n "
   187         + "var t = e.children[pos].innerHTML;\n "
   188         + "return t ? t : null;"
   189     )
   190     private static native String childText(String id, int pos);
   191 }