jaroslav@489
|
1 |
/**
|
jaroslav@489
|
2 |
* Back 2 Browser Bytecode Translator
|
jaroslav@489
|
3 |
* Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jaroslav@489
|
4 |
*
|
jaroslav@489
|
5 |
* This program is free software: you can redistribute it and/or modify
|
jaroslav@489
|
6 |
* it under the terms of the GNU General Public License as published by
|
jaroslav@489
|
7 |
* the Free Software Foundation, version 2 of the License.
|
jaroslav@489
|
8 |
*
|
jaroslav@489
|
9 |
* This program is distributed in the hope that it will be useful,
|
jaroslav@489
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
jaroslav@489
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
jaroslav@489
|
12 |
* GNU General Public License for more details.
|
jaroslav@489
|
13 |
*
|
jaroslav@489
|
14 |
* You should have received a copy of the GNU General Public License
|
jaroslav@489
|
15 |
* along with this program. Look for COPYING file in the top folder.
|
jaroslav@489
|
16 |
* If not, see http://opensource.org/licenses/GPL-2.0.
|
jaroslav@489
|
17 |
*/
|
jaroslav@489
|
18 |
package org.apidesign.bck2brwsr.htmlpage;
|
jaroslav@489
|
19 |
|
jaroslav@499
|
20 |
import java.util.ArrayList;
|
jaroslav@761
|
21 |
import java.util.Iterator;
|
jaroslav@499
|
22 |
import java.util.List;
|
jaroslav@761
|
23 |
import java.util.ListIterator;
|
jaroslav@491
|
24 |
import org.apidesign.bck2brwsr.htmlpage.api.ComputedProperty;
|
jaroslav@489
|
25 |
import org.apidesign.bck2brwsr.htmlpage.api.Page;
|
jaroslav@490
|
26 |
import org.apidesign.bck2brwsr.htmlpage.api.Property;
|
jaroslav@489
|
27 |
import static org.testng.Assert.*;
|
jaroslav@505
|
28 |
import org.testng.annotations.BeforeMethod;
|
jaroslav@489
|
29 |
import org.testng.annotations.Test;
|
jaroslav@489
|
30 |
|
jaroslav@489
|
31 |
/**
|
jaroslav@489
|
32 |
*
|
jaroslav@489
|
33 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jaroslav@489
|
34 |
*/
|
jaroslav@490
|
35 |
@Page(xhtml = "Empty.html", className = "Model", properties = {
|
jaroslav@499
|
36 |
@Property(name = "value", type = int.class),
|
jaroslav@760
|
37 |
@Property(name = "unrelated", type = long.class),
|
jaroslav@764
|
38 |
@Property(name = "names", type = String.class, array = true),
|
jaroslav@764
|
39 |
@Property(name = "values", type = int.class, array = true)
|
jaroslav@490
|
40 |
})
|
jaroslav@489
|
41 |
public class ModelTest {
|
jaroslav@505
|
42 |
private Model model;
|
jaroslav@505
|
43 |
private static Model leakedModel;
|
jaroslav@505
|
44 |
|
jaroslav@505
|
45 |
@BeforeMethod
|
jaroslav@505
|
46 |
public void createModel() {
|
jaroslav@505
|
47 |
model = new Model();
|
jaroslav@505
|
48 |
}
|
jaroslav@505
|
49 |
|
jaroslav@490
|
50 |
@Test public void classGeneratedWithSetterGetter() {
|
jaroslav@505
|
51 |
model.setValue(10);
|
jaroslav@505
|
52 |
assertEquals(10, model.getValue(), "Value changed");
|
jaroslav@489
|
53 |
}
|
jaroslav@491
|
54 |
|
jaroslav@491
|
55 |
@Test public void computedMethod() {
|
jaroslav@505
|
56 |
model.setValue(4);
|
jaroslav@505
|
57 |
assertEquals(16, model.getPowerValue());
|
jaroslav@491
|
58 |
}
|
jaroslav@491
|
59 |
|
jaroslav@760
|
60 |
@Test public void arrayIsMutable() {
|
jaroslav@760
|
61 |
assertEquals(model.getNames().size(), 0, "Is empty");
|
jaroslav@760
|
62 |
model.getNames().add("Jarda");
|
jaroslav@760
|
63 |
assertEquals(model.getNames().size(), 1, "One element");
|
jaroslav@760
|
64 |
}
|
jaroslav@760
|
65 |
|
jaroslav@761
|
66 |
@Test public void arrayChangesNotified() {
|
jaroslav@761
|
67 |
MockKnockout my = new MockKnockout();
|
jaroslav@761
|
68 |
MockKnockout.next = my;
|
jaroslav@761
|
69 |
|
jaroslav@761
|
70 |
model.applyBindings();
|
jaroslav@761
|
71 |
|
jaroslav@761
|
72 |
model.getNames().add("Hello");
|
jaroslav@761
|
73 |
|
jaroslav@761
|
74 |
assertFalse(my.mutated.isEmpty(), "There was a change" + my.mutated);
|
jaroslav@761
|
75 |
assertTrue(my.mutated.contains("names"), "Change in names property: " + my.mutated);
|
jaroslav@761
|
76 |
|
jaroslav@761
|
77 |
my.mutated.clear();
|
jaroslav@761
|
78 |
|
jaroslav@761
|
79 |
Iterator<String> it = model.getNames().iterator();
|
jaroslav@761
|
80 |
assertEquals(it.next(), "Hello");
|
jaroslav@761
|
81 |
it.remove();
|
jaroslav@761
|
82 |
|
jaroslav@761
|
83 |
assertFalse(my.mutated.isEmpty(), "There was a change" + my.mutated);
|
jaroslav@761
|
84 |
assertTrue(my.mutated.contains("names"), "Change in names property: " + my.mutated);
|
jaroslav@761
|
85 |
|
jaroslav@761
|
86 |
my.mutated.clear();
|
jaroslav@761
|
87 |
|
jaroslav@761
|
88 |
ListIterator<String> lit = model.getNames().listIterator();
|
jaroslav@761
|
89 |
lit.add("Jarda");
|
jaroslav@761
|
90 |
|
jaroslav@761
|
91 |
assertFalse(my.mutated.isEmpty(), "There was a change" + my.mutated);
|
jaroslav@761
|
92 |
assertTrue(my.mutated.contains("names"), "Change in names property: " + my.mutated);
|
jaroslav@761
|
93 |
}
|
jaroslav@761
|
94 |
|
jaroslav@764
|
95 |
@Test public void autoboxedArray() {
|
jaroslav@764
|
96 |
MockKnockout my = new MockKnockout();
|
jaroslav@764
|
97 |
MockKnockout.next = my;
|
jaroslav@764
|
98 |
|
jaroslav@764
|
99 |
model.applyBindings();
|
jaroslav@764
|
100 |
|
jaroslav@764
|
101 |
model.getValues().add(10);
|
jaroslav@764
|
102 |
|
jaroslav@764
|
103 |
assertEquals(model.getValues().get(0), Integer.valueOf(10), "Really ten");
|
jaroslav@764
|
104 |
}
|
jaroslav@764
|
105 |
|
jaroslav@499
|
106 |
@Test public void derivedPropertiesAreNotified() {
|
jaroslav@499
|
107 |
MockKnockout my = new MockKnockout();
|
jaroslav@499
|
108 |
MockKnockout.next = my;
|
jaroslav@499
|
109 |
|
jaroslav@505
|
110 |
model.applyBindings();
|
jaroslav@499
|
111 |
|
jaroslav@505
|
112 |
model.setValue(33);
|
jaroslav@499
|
113 |
|
jaroslav@499
|
114 |
assertEquals(my.mutated.size(), 2, "Two properties changed: " + my.mutated);
|
jaroslav@499
|
115 |
assertTrue(my.mutated.contains("powerValue"), "Power value is in there: " + my.mutated);
|
jaroslav@499
|
116 |
assertTrue(my.mutated.contains("value"), "Simple value is in there: " + my.mutated);
|
jaroslav@499
|
117 |
|
jaroslav@499
|
118 |
my.mutated.clear();
|
jaroslav@499
|
119 |
|
jaroslav@505
|
120 |
model.setUnrelated(44);
|
jaroslav@499
|
121 |
assertEquals(my.mutated.size(), 1, "One property changed");
|
jaroslav@499
|
122 |
assertTrue(my.mutated.contains("unrelated"), "Its name is unrelated");
|
jaroslav@499
|
123 |
}
|
jaroslav@499
|
124 |
|
jaroslav@500
|
125 |
@Test public void computedPropertyCannotWriteToModel() {
|
jaroslav@505
|
126 |
leakedModel = model;
|
jaroslav@500
|
127 |
try {
|
jaroslav@505
|
128 |
String res = model.getNotAllowedWrite();
|
jaroslav@500
|
129 |
fail("We should not be allowed to write to the model: " + res);
|
jaroslav@500
|
130 |
} catch (IllegalStateException ex) {
|
jaroslav@500
|
131 |
// OK, we can't read
|
jaroslav@500
|
132 |
}
|
jaroslav@500
|
133 |
}
|
jaroslav@500
|
134 |
|
jaroslav@500
|
135 |
@Test public void computedPropertyCannotReadToModel() {
|
jaroslav@505
|
136 |
leakedModel = model;
|
jaroslav@500
|
137 |
try {
|
jaroslav@505
|
138 |
String res = model.getNotAllowedRead();
|
jaroslav@500
|
139 |
fail("We should not be allowed to read from the model: " + res);
|
jaroslav@500
|
140 |
} catch (IllegalStateException ex) {
|
jaroslav@500
|
141 |
// OK, we can't read
|
jaroslav@500
|
142 |
}
|
jaroslav@500
|
143 |
}
|
jaroslav@500
|
144 |
|
jaroslav@491
|
145 |
@ComputedProperty
|
jaroslav@491
|
146 |
static int powerValue(int value) {
|
jaroslav@491
|
147 |
return value * value;
|
jaroslav@491
|
148 |
}
|
jaroslav@499
|
149 |
|
jaroslav@500
|
150 |
@ComputedProperty
|
jaroslav@500
|
151 |
static String notAllowedRead() {
|
jaroslav@505
|
152 |
return "Not allowed callback: " + leakedModel.getUnrelated();
|
jaroslav@500
|
153 |
}
|
jaroslav@500
|
154 |
|
jaroslav@500
|
155 |
@ComputedProperty
|
jaroslav@500
|
156 |
static String notAllowedWrite() {
|
jaroslav@505
|
157 |
leakedModel.setUnrelated(11);
|
jaroslav@500
|
158 |
return "Not allowed callback!";
|
jaroslav@500
|
159 |
}
|
jaroslav@500
|
160 |
|
jaroslav@499
|
161 |
static class MockKnockout extends Knockout {
|
jaroslav@499
|
162 |
List<String> mutated = new ArrayList<String>();
|
jaroslav@499
|
163 |
|
jaroslav@499
|
164 |
@Override
|
jaroslav@499
|
165 |
public void valueHasMutated(String prop) {
|
jaroslav@499
|
166 |
mutated.add(prop);
|
jaroslav@499
|
167 |
}
|
jaroslav@499
|
168 |
}
|
jaroslav@489
|
169 |
}
|