2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
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.
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.
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.
18 package org.apidesign.bck2brwsr.htmlpage;
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.Iterator;
23 import java.util.List;
24 import java.util.ListIterator;
25 import org.apidesign.bck2brwsr.htmlpage.api.ComputedProperty;
26 import org.apidesign.bck2brwsr.htmlpage.api.OnFunction;
27 import org.apidesign.bck2brwsr.htmlpage.api.Page;
28 import org.apidesign.bck2brwsr.htmlpage.api.Property;
29 import static org.testng.Assert.*;
30 import org.testng.annotations.BeforeMethod;
31 import org.testng.annotations.Test;
35 * @author Jaroslav Tulach <jtulach@netbeans.org>
37 @Page(xhtml = "Empty.html", className = "Modelik", properties = {
38 @Property(name = "value", type = int.class),
39 @Property(name = "count", type = int.class),
40 @Property(name = "unrelated", type = long.class),
41 @Property(name = "names", type = String.class, array = true),
42 @Property(name = "values", type = int.class, array = true),
43 @Property(name = "people", type = PersonImpl.class, array = true)
45 public class ModelTest {
46 private Modelik model;
47 private static Modelik leakedModel;
50 public void createModel() {
51 model = new Modelik();
54 @Test public void classGeneratedWithSetterGetter() {
56 assertEquals(10, model.getValue(), "Value changed");
59 @Test public void computedMethod() {
61 assertEquals(16, model.getPowerValue());
64 @Test public void arrayIsMutable() {
65 assertEquals(model.getNames().size(), 0, "Is empty");
66 model.getNames().add("Jarda");
67 assertEquals(model.getNames().size(), 1, "One element");
70 @Test public void arrayChangesNotified() {
71 MockKnockout my = new MockKnockout();
72 MockKnockout.next = my;
74 model.applyBindings();
76 model.getNames().add("Hello");
78 assertFalse(my.mutated.isEmpty(), "There was a change" + my.mutated);
79 assertTrue(my.mutated.contains("names"), "Change in names property: " + my.mutated);
83 Iterator<String> it = model.getNames().iterator();
84 assertEquals(it.next(), "Hello");
87 assertFalse(my.mutated.isEmpty(), "There was a change" + my.mutated);
88 assertTrue(my.mutated.contains("names"), "Change in names property: " + my.mutated);
92 ListIterator<String> lit = model.getNames().listIterator();
95 assertFalse(my.mutated.isEmpty(), "There was a change" + my.mutated);
96 assertTrue(my.mutated.contains("names"), "Change in names property: " + my.mutated);
99 @Test public void autoboxedArray() {
100 MockKnockout my = new MockKnockout();
101 MockKnockout.next = my;
103 model.applyBindings();
105 model.getValues().add(10);
107 assertEquals(model.getValues().get(0), Integer.valueOf(10), "Really ten");
110 @Test public void derivedArrayProp() {
111 MockKnockout my = new MockKnockout();
112 MockKnockout.next = my;
114 model.applyBindings();
118 List<String> arr = model.getRepeat();
119 assertEquals(arr.size(), 10, "Ten items: " + arr);
125 arr = model.getRepeat();
126 assertEquals(arr.size(), 5, "Five items: " + arr);
128 assertEquals(my.mutated.size(), 2, "Two properties changed: " + my.mutated);
129 assertTrue(my.mutated.contains("repeat"), "Array is in there: " + my.mutated);
130 assertTrue(my.mutated.contains("count"), "Count is in there: " + my.mutated);
133 @Test public void derivedPropertiesAreNotified() {
134 MockKnockout my = new MockKnockout();
135 MockKnockout.next = my;
137 model.applyBindings();
141 assertEquals(my.mutated.size(), 2, "Two properties changed: " + my.mutated);
142 assertTrue(my.mutated.contains("powerValue"), "Power value is in there: " + my.mutated);
143 assertTrue(my.mutated.contains("value"), "Simple value is in there: " + my.mutated);
147 model.setUnrelated(44);
148 assertEquals(my.mutated.size(), 1, "One property changed");
149 assertTrue(my.mutated.contains("unrelated"), "Its name is unrelated");
152 @Test public void computedPropertyCannotWriteToModel() {
155 String res = model.getNotAllowedWrite();
156 fail("We should not be allowed to write to the model: " + res);
157 } catch (IllegalStateException ex) {
162 @Test public void computedPropertyCannotReadToModel() {
165 String res = model.getNotAllowedRead();
166 fail("We should not be allowed to read from the model: " + res);
167 } catch (IllegalStateException ex) {
173 static void doSomething() {
177 static int powerValue(int value) {
178 return value * value;
182 static String notAllowedRead() {
183 return "Not allowed callback: " + leakedModel.getUnrelated();
187 static String notAllowedWrite() {
188 leakedModel.setUnrelated(11);
189 return "Not allowed callback!";
193 static List<String> repeat(int count) {
194 return Collections.nCopies(count, "Hello");
197 static class MockKnockout extends Knockout {
198 List<String> mutated = new ArrayList<>();
205 public void valueHasMutated(String prop) {
210 public @Test void hasPersonPropertyAndComputedFullName() {
211 List<Person> arr = model.getPeople();
212 assertEquals(arr.size(), 0, "By default empty");
215 String fullNameGenerated = p.getFullName();
216 assertNotNull(fullNameGenerated);