javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/KnockoutTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 04 Apr 2013 13:08:26 +0200
branchmodel
changeset 930 e8916518b38d
parent 929 b43aaf398748
child 1011 9cc253aa9405
child 1787 ea12a3bb4b33
permissions -rw-r--r--
clone() 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 static org.testng.Assert.assertEquals;
    31 import org.testng.annotations.Factory;
    32 import org.testng.annotations.Test;
    33 
    34 /**
    35  *
    36  * @author Jaroslav Tulach <jtulach@netbeans.org>
    37  */
    38 @Page(xhtml="Knockout.xhtml", className="KnockoutModel", properties={
    39     @Property(name="name", type=String.class),
    40     @Property(name="results", type=String.class, array = true),
    41     @Property(name="callbackCount", type=int.class),
    42     @Property(name="people", type=PersonImpl.class, array = true)
    43 }) 
    44 public class KnockoutTest {
    45     
    46     @HtmlFragment(
    47         "<h1 data-bind=\"text: helloMessage\">Loading Bck2Brwsr's Hello World...</h1>\n" +
    48         "Your name: <input id='input' data-bind=\"value: name\"></input>\n" +
    49         "<button id=\"hello\">Say Hello!</button>\n"
    50     )
    51     @BrwsrTest public void modifyValueAssertChangeInModel() {
    52         KnockoutModel m = new KnockoutModel();
    53         m.setName("Kukuc");
    54         m.applyBindings();
    55         assert "Kukuc".equals(m.input.getValue()) : "Value is really kukuc: " + m.input.getValue();
    56         m.input.setValue("Jardo");
    57         m.triggerEvent(m.input, OnEvent.CHANGE);
    58         assert "Jardo".equals(m.getName()) : "Name property updated: " + m.getName();
    59     }
    60     
    61     @HtmlFragment(
    62         "<ul id='ul' data-bind='foreach: results'>\n"
    63         + "  <li data-bind='text: $data, click: $root.call'/>\n"
    64         + "</ul>\n"
    65     )
    66     @BrwsrTest public void displayContentOfArray() {
    67         KnockoutModel m = new KnockoutModel();
    68         m.getResults().add("Ahoj");
    69         m.applyBindings();
    70         
    71         int cnt = countChildren("ul");
    72         assert cnt == 1 : "One child, but was " + cnt;
    73         
    74         m.getResults().add("Hi");
    75 
    76         cnt = countChildren("ul");
    77         assert cnt == 2 : "Two children now, but was " + cnt;
    78         
    79         triggerChildClick("ul", 1);
    80         
    81         assert 1 == m.getCallbackCount() : "One callback " + m.getCallbackCount();
    82         assert "Hi".equals(m.getName()) : "We got callback from 2nd child " + m.getName();
    83     }
    84     
    85     @HtmlFragment(
    86         "<ul id='ul' data-bind='foreach: cmpResults'>\n"
    87         + "  <li><b data-bind='text: $data'></b></li>\n"
    88         + "</ul>\n"
    89     )
    90     @BrwsrTest public void displayContentOfDerivedArray() {
    91         KnockoutModel m = new KnockoutModel();
    92         m.getResults().add("Ahoj");
    93         m.applyBindings();
    94         
    95         int cnt = countChildren("ul");
    96         assert cnt == 1 : "One child, but was " + cnt;
    97         
    98         m.getResults().add("hello");
    99 
   100         cnt = countChildren("ul");
   101         assert cnt == 2 : "Two children now, but was " + cnt;
   102     }
   103     
   104     @HtmlFragment(
   105         "<ul id='ul' data-bind='foreach: people'>\n"
   106         + "  <li data-bind='text: $data.firstName, click: $root.removePerson'></li>\n"
   107         + "</ul>\n"
   108     )
   109     @BrwsrTest public void displayContentOfArrayOfPeople() {
   110         KnockoutModel m = new KnockoutModel();
   111         
   112         final Person first = new Person();
   113         first.setFirstName("first");
   114         m.getPeople().add(first);
   115         
   116         m.applyBindings();
   117         
   118         int cnt = countChildren("ul");
   119         assert cnt == 1 : "One child, but was " + cnt;
   120         
   121         final Person second = new Person();
   122         second.setFirstName("second");
   123         m.getPeople().add(second);
   124 
   125         cnt = countChildren("ul");
   126         assert cnt == 2 : "Two children now, but was " + cnt;
   127 
   128         triggerChildClick("ul", 1);
   129         
   130         assert 1 == m.getCallbackCount() : "One callback " + m.getCallbackCount();
   131 
   132         cnt = countChildren("ul");
   133         assert cnt == 1 : "Again one child, but was " + cnt;
   134         
   135         String txt = childText("ul", 0);
   136         assert "first".equals(txt) : "Expecting 'first': " + txt;
   137         
   138         first.setFirstName("changed");
   139         
   140         txt = childText("ul", 0);
   141         assert "changed".equals(txt) : "Expecting 'changed': " + txt;
   142     }
   143     
   144     @ComputedProperty
   145     static Person firstPerson(List<Person> people) {
   146         return people.isEmpty() ? null : people.get(0);
   147     }
   148     
   149     @HtmlFragment(
   150         "<p id='ul' data-bind='with: firstPerson'>\n"
   151         + "  <span data-bind='text: firstName, click: changeSex'></span>\n"
   152         + "</p>\n"
   153     )
   154     @BrwsrTest public void accessFirstPersonWithOnFunction() {
   155         trasfertToFemale();
   156     }
   157     
   158     @HtmlFragment(
   159         "<ul id='ul' data-bind='foreach: people'>\n"
   160         + "  <li data-bind='text: $data.firstName, click: changeSex'></li>\n"
   161         + "</ul>\n"
   162     )
   163     @BrwsrTest public void onPersonFunction() {
   164         trasfertToFemale();
   165     }
   166     
   167     private void trasfertToFemale() {
   168         KnockoutModel m = new KnockoutModel();
   169 
   170         final Person first = new Person();
   171         first.setFirstName("first");
   172         first.setSex(Sex.MALE);
   173         m.getPeople().add(first);
   174 
   175 
   176         m.applyBindings();
   177 
   178         int cnt = countChildren("ul");
   179         assert cnt == 1 : "One child, but was " + cnt;
   180 
   181 
   182         triggerChildClick("ul", 0);
   183 
   184         assert first.getSex() == Sex.FEMALE : "Transverted to female: " + first.getSex();
   185     }
   186     
   187     @Test public void cloneModel() {
   188         Person model = new Person();
   189         
   190         model.setFirstName("first");
   191         Person snd = model.clone();
   192         snd.setFirstName("clone");
   193         assertEquals("first", model.getFirstName(), "Value has not changed");
   194         assertEquals("clone", snd.getFirstName(), "Value has changed in clone");
   195     }
   196    
   197     
   198     @Test public void deepCopyOnClone() {
   199         People model = new People();
   200         model.getNicknames().add("Jarda");
   201         assertEquals(model.getNicknames().size(), 1, "One element");
   202         People snd = model.clone();
   203         snd.getNicknames().clear();
   204         assertEquals(snd.getNicknames().size(), 0, "Clone is empty");
   205         assertEquals(model.getNicknames().size(), 1, "Still one element");
   206     }
   207     
   208      
   209     @OnFunction
   210     static void call(KnockoutModel m, String data) {
   211         m.setName(data);
   212         m.setCallbackCount(m.getCallbackCount() + 1);
   213     }
   214 
   215     @OnFunction
   216     static void removePerson(KnockoutModel model, Person data) {
   217         model.setCallbackCount(model.getCallbackCount() + 1);
   218         model.getPeople().remove(data);
   219     }
   220     
   221     
   222     @ComputedProperty
   223     static String helloMessage(String name) {
   224         return "Hello " + name + "!";
   225     }
   226     
   227     @ComputedProperty
   228     static List<String> cmpResults(List<String> results) {
   229         return results;
   230     }
   231     
   232     @Factory
   233     public static Object[] create() {
   234         return VMTest.create(KnockoutTest.class);
   235     }
   236     
   237     @JavaScriptBody(args = { "id" }, body = 
   238           "var e = window.document.getElementById(id);\n "
   239         + "if (typeof e === 'undefined') return -2;\n "
   240         + "return e.children.length;\n "
   241     )
   242     private static native int countChildren(String id);
   243 
   244     @JavaScriptBody(args = { "id", "pos" }, body = 
   245           "var e = window.document.getElementById(id);\n "
   246         + "var ev = window.document.createEvent('MouseEvents');\n "
   247         + "ev.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);\n "
   248         + "e.children[pos].dispatchEvent(ev);\n "
   249     )
   250     private static native void triggerChildClick(String id, int pos);
   251 
   252     @JavaScriptBody(args = { "id", "pos" }, body = 
   253           "var e = window.document.getElementById(id);\n "
   254         + "var t = e.children[pos].innerHTML;\n "
   255         + "return t ? t : null;"
   256     )
   257     private static native String childText(String id, int pos);
   258 }