javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/KnockoutTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 31 Mar 2013 05:34:15 +0200
branchmodel
changeset 906 22358b42ec2a
parent 892 16fd25f3a75d
child 908 3e023bea2da4
permissions -rw-r--r--
@OnFunction can accept 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         m.getPeople().add(new Person());
   110         m.applyBindings();
   111         
   112         int cnt = countChildren("ul");
   113         assert cnt == 1 : "One child, but was " + cnt;
   114         
   115         m.getPeople().add(new Person());
   116 
   117         cnt = countChildren("ul");
   118         assert cnt == 2 : "Two children now, but was " + cnt;
   119 
   120         triggerChildClick("ul", 1);
   121         
   122         assert 1 == m.getCallbackCount() : "One callback " + m.getCallbackCount();
   123 
   124         cnt = countChildren("ul");
   125         assert cnt == 1 : "Again one child, but was " + cnt;
   126     }
   127      
   128     @OnFunction
   129     static void call(KnockoutModel m, String data) {
   130         m.setName(data);
   131         m.setCallbackCount(m.getCallbackCount() + 1);
   132     }
   133 
   134     @OnFunction
   135     static void removePerson(KnockoutModel model, Person data) {
   136         model.setCallbackCount(model.getCallbackCount() + 1);
   137         model.getPeople().remove(data);
   138     }
   139     
   140     
   141     @ComputedProperty
   142     static String helloMessage(String name) {
   143         return "Hello " + name + "!";
   144     }
   145     
   146     @ComputedProperty
   147     static List<String> cmpResults(List<String> results) {
   148         return results;
   149     }
   150     
   151     @Factory
   152     public static Object[] create() {
   153         return VMTest.create(KnockoutTest.class);
   154     }
   155     
   156     @JavaScriptBody(args = { "id" }, body = 
   157           "var e = window.document.getElementById(id);\n "
   158         + "if (typeof e === 'undefined') return -2;\n "
   159         + "return e.children.length;\n "
   160     )
   161     private static native int countChildren(String id);
   162 
   163     @JavaScriptBody(args = { "id", "pos" }, body = 
   164           "var e = window.document.getElementById(id);\n "
   165         + "var ev = window.document.createEvent('MouseEvents');\n "
   166         + "ev.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);\n "
   167         + "e.children[pos].dispatchEvent(ev);\n "
   168     )
   169     private static native void triggerChildClick(String id, int pos);
   170 }