javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/ModelTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 21 Jan 2013 10:06:42 +0100
branchmodel
changeset 499 af027874f93e
parent 491 14268cd404a4
child 500 f9e80d48e9b4
permissions -rw-r--r--
Making the Knockout behavior testable
     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.ArrayList;
    21 import java.util.List;
    22 import org.apidesign.bck2brwsr.htmlpage.api.ComputedProperty;
    23 import org.apidesign.bck2brwsr.htmlpage.api.Page;
    24 import org.apidesign.bck2brwsr.htmlpage.api.Property;
    25 import static org.testng.Assert.*;
    26 import org.testng.annotations.Test;
    27 
    28 /**
    29  *
    30  * @author Jaroslav Tulach <jtulach@netbeans.org>
    31  */
    32 @Page(xhtml = "Empty.html", className = "Model", properties = {
    33     @Property(name = "value", type = int.class),
    34     @Property(name = "unrelated", type = long.class)
    35 })
    36 public class ModelTest {
    37     @Test public void classGeneratedWithSetterGetter() {
    38         Class<?> c = Model.class;
    39         assertNotNull(c, "Class for empty page generated");
    40         Model.setValue(10);
    41         assertEquals(10, Model.getValue(), "Value changed");
    42     }
    43     
    44     @Test public void computedMethod() {
    45         Model.setValue(4);
    46         assertEquals(16, Model.getPowerValue());
    47     }
    48     
    49     @Test public void derivedPropertiesAreNotified() {
    50         MockKnockout my = new MockKnockout();
    51         MockKnockout.next = my;
    52         
    53         Model.applyBindings();
    54         
    55         Model.setValue(33);
    56         
    57         assertEquals(my.mutated.size(), 2, "Two properties changed: " + my.mutated);
    58         assertTrue(my.mutated.contains("powerValue"), "Power value is in there: " + my.mutated);
    59         assertTrue(my.mutated.contains("value"), "Simple value is in there: " + my.mutated);
    60         
    61         my.mutated.clear();
    62         
    63         Model.setUnrelated(44);
    64         assertEquals(my.mutated.size(), 1, "One property changed");
    65         assertTrue(my.mutated.contains("unrelated"), "Its name is unrelated");
    66     }
    67     
    68     @ComputedProperty
    69     static int powerValue(int value) {
    70         return value * value;
    71     }
    72     
    73     static class MockKnockout extends Knockout {
    74         List<String> mutated = new ArrayList<String>();
    75         
    76         @Override
    77         public void valueHasMutated(String prop) {
    78             mutated.add(prop);
    79         }
    80     }
    81 }