javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/KnockoutTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 20 Feb 2013 18:14:59 +0100
branchmodel
changeset 769 0c0fe97fe0c7
parent 763 ecd7294f1e17
child 879 af170d42b5b3
permissions -rw-r--r--
Processor allows only primitive types and String as its values
     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.Page;
    25 import org.apidesign.bck2brwsr.htmlpage.api.Property;
    26 import org.apidesign.bck2brwsr.vmtest.BrwsrTest;
    27 import org.apidesign.bck2brwsr.vmtest.HtmlFragment;
    28 import org.apidesign.bck2brwsr.vmtest.VMTest;
    29 import org.testng.annotations.Factory;
    30 
    31 /**
    32  *
    33  * @author Jaroslav Tulach <jtulach@netbeans.org>
    34  */
    35 @Page(xhtml="Knockout.xhtml", className="KnockoutModel", properties={
    36     @Property(name="name", type=String.class),
    37     @Property(name="results", type=String.class, array = true)
    38 }) 
    39 public class KnockoutTest {
    40     
    41     @HtmlFragment(
    42         "<h1 data-bind=\"text: helloMessage\">Loading Bck2Brwsr's Hello World...</h1>\n" +
    43         "Your name: <input id='input' data-bind=\"value: name\"></input>\n" +
    44         "<button id=\"hello\">Say Hello!</button>\n"
    45     )
    46     @BrwsrTest public void modifyValueAssertChangeInModel() {
    47         KnockoutModel m = new KnockoutModel();
    48         m.setName("Kukuc");
    49         m.applyBindings();
    50         assert "Kukuc".equals(m.INPUT.getValue()) : "Value is really kukuc: " + m.INPUT.getValue();
    51         m.INPUT.setValue("Jardo");
    52         m.triggerEvent(m.INPUT, OnEvent.CHANGE);
    53         assert "Jardo".equals(m.getName()) : "Name property updated: " + m.getName();
    54     }
    55     
    56     @HtmlFragment(
    57         "<ul id='ul' data-bind='foreach: results'>\n"
    58         + "  <li><b data-bind='text: $data'></b></li>\n"
    59         + "</ul>\n"
    60     )
    61     @BrwsrTest public void displayContentOfArray() {
    62         KnockoutModel m = new KnockoutModel();
    63         m.getResults().add("Ahoj");
    64         m.applyBindings();
    65         
    66         int cnt = countChildren("ul");
    67         assert cnt == 1 : "One child, but was " + cnt;
    68         
    69         m.getResults().add("hello");
    70 
    71         cnt = countChildren("ul");
    72         assert cnt == 2 : "Two children now, but was " + cnt;
    73     }
    74     
    75     @HtmlFragment(
    76         "<ul id='ul' data-bind='foreach: cmpResults'>\n"
    77         + "  <li><b data-bind='text: $data'></b></li>\n"
    78         + "</ul>\n"
    79     )
    80     @BrwsrTest public void displayContentOfDerivedArray() {
    81         KnockoutModel m = new KnockoutModel();
    82         m.getResults().add("Ahoj");
    83         m.applyBindings();
    84         
    85         int cnt = countChildren("ul");
    86         assert cnt == 1 : "One child, but was " + cnt;
    87         
    88         m.getResults().add("hello");
    89 
    90         cnt = countChildren("ul");
    91         assert cnt == 2 : "Two children now, but was " + cnt;
    92     }
    93     
    94     @ComputedProperty
    95     static String helloMessage(String name) {
    96         return "Hello " + name + "!";
    97     }
    98     
    99     @ComputedProperty
   100     static List<String> cmpResults(List<String> results) {
   101         return results;
   102     }
   103     
   104     @Factory
   105     public static Object[] create() {
   106         return VMTest.create(KnockoutTest.class);
   107     }
   108     
   109     @JavaScriptBody(args = { "id" }, body = 
   110           "var e = window.document.getElementById(id);\n "
   111         + "if (typeof e === 'undefined') return -2;\n "
   112         + "return e.children.length;\n "
   113     )
   114     private static native int countChildren(String id);
   115 }