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@760
|
38 |
@Property(name = "names", type = String.class, array = true)
|
jaroslav@490
|
39 |
})
|
jaroslav@489
|
40 |
public class ModelTest {
|
jaroslav@505
|
41 |
private Model model;
|
jaroslav@505
|
42 |
private static Model leakedModel;
|
jaroslav@505
|
43 |
|
jaroslav@505
|
44 |
@BeforeMethod
|
jaroslav@505
|
45 |
public void createModel() {
|
jaroslav@505
|
46 |
model = new Model();
|
jaroslav@505
|
47 |
}
|
jaroslav@505
|
48 |
|
jaroslav@490
|
49 |
@Test public void classGeneratedWithSetterGetter() {
|
jaroslav@505
|
50 |
model.setValue(10);
|
jaroslav@505
|
51 |
assertEquals(10, model.getValue(), "Value changed");
|
jaroslav@489
|
52 |
}
|
jaroslav@491
|
53 |
|
jaroslav@491
|
54 |
@Test public void computedMethod() {
|
jaroslav@505
|
55 |
model.setValue(4);
|
jaroslav@505
|
56 |
assertEquals(16, model.getPowerValue());
|
jaroslav@491
|
57 |
}
|
jaroslav@491
|
58 |
|
jaroslav@760
|
59 |
@Test public void arrayIsMutable() {
|
jaroslav@760
|
60 |
assertEquals(model.getNames().size(), 0, "Is empty");
|
jaroslav@760
|
61 |
model.getNames().add("Jarda");
|
jaroslav@760
|
62 |
assertEquals(model.getNames().size(), 1, "One element");
|
jaroslav@760
|
63 |
}
|
jaroslav@760
|
64 |
|
jaroslav@761
|
65 |
@Test public void arrayChangesNotified() {
|
jaroslav@761
|
66 |
MockKnockout my = new MockKnockout();
|
jaroslav@761
|
67 |
MockKnockout.next = my;
|
jaroslav@761
|
68 |
|
jaroslav@761
|
69 |
model.applyBindings();
|
jaroslav@761
|
70 |
|
jaroslav@761
|
71 |
model.getNames().add("Hello");
|
jaroslav@761
|
72 |
|
jaroslav@761
|
73 |
assertFalse(my.mutated.isEmpty(), "There was a change" + my.mutated);
|
jaroslav@761
|
74 |
assertTrue(my.mutated.contains("names"), "Change in names property: " + my.mutated);
|
jaroslav@761
|
75 |
|
jaroslav@761
|
76 |
my.mutated.clear();
|
jaroslav@761
|
77 |
|
jaroslav@761
|
78 |
Iterator<String> it = model.getNames().iterator();
|
jaroslav@761
|
79 |
assertEquals(it.next(), "Hello");
|
jaroslav@761
|
80 |
it.remove();
|
jaroslav@761
|
81 |
|
jaroslav@761
|
82 |
assertFalse(my.mutated.isEmpty(), "There was a change" + my.mutated);
|
jaroslav@761
|
83 |
assertTrue(my.mutated.contains("names"), "Change in names property: " + my.mutated);
|
jaroslav@761
|
84 |
|
jaroslav@761
|
85 |
my.mutated.clear();
|
jaroslav@761
|
86 |
|
jaroslav@761
|
87 |
ListIterator<String> lit = model.getNames().listIterator();
|
jaroslav@761
|
88 |
lit.add("Jarda");
|
jaroslav@761
|
89 |
|
jaroslav@761
|
90 |
assertFalse(my.mutated.isEmpty(), "There was a change" + my.mutated);
|
jaroslav@761
|
91 |
assertTrue(my.mutated.contains("names"), "Change in names property: " + my.mutated);
|
jaroslav@761
|
92 |
}
|
jaroslav@761
|
93 |
|
jaroslav@499
|
94 |
@Test public void derivedPropertiesAreNotified() {
|
jaroslav@499
|
95 |
MockKnockout my = new MockKnockout();
|
jaroslav@499
|
96 |
MockKnockout.next = my;
|
jaroslav@499
|
97 |
|
jaroslav@505
|
98 |
model.applyBindings();
|
jaroslav@499
|
99 |
|
jaroslav@505
|
100 |
model.setValue(33);
|
jaroslav@499
|
101 |
|
jaroslav@499
|
102 |
assertEquals(my.mutated.size(), 2, "Two properties changed: " + my.mutated);
|
jaroslav@499
|
103 |
assertTrue(my.mutated.contains("powerValue"), "Power value is in there: " + my.mutated);
|
jaroslav@499
|
104 |
assertTrue(my.mutated.contains("value"), "Simple value is in there: " + my.mutated);
|
jaroslav@499
|
105 |
|
jaroslav@499
|
106 |
my.mutated.clear();
|
jaroslav@499
|
107 |
|
jaroslav@505
|
108 |
model.setUnrelated(44);
|
jaroslav@499
|
109 |
assertEquals(my.mutated.size(), 1, "One property changed");
|
jaroslav@499
|
110 |
assertTrue(my.mutated.contains("unrelated"), "Its name is unrelated");
|
jaroslav@499
|
111 |
}
|
jaroslav@499
|
112 |
|
jaroslav@500
|
113 |
@Test public void computedPropertyCannotWriteToModel() {
|
jaroslav@505
|
114 |
leakedModel = model;
|
jaroslav@500
|
115 |
try {
|
jaroslav@505
|
116 |
String res = model.getNotAllowedWrite();
|
jaroslav@500
|
117 |
fail("We should not be allowed to write to the model: " + res);
|
jaroslav@500
|
118 |
} catch (IllegalStateException ex) {
|
jaroslav@500
|
119 |
// OK, we can't read
|
jaroslav@500
|
120 |
}
|
jaroslav@500
|
121 |
}
|
jaroslav@500
|
122 |
|
jaroslav@500
|
123 |
@Test public void computedPropertyCannotReadToModel() {
|
jaroslav@505
|
124 |
leakedModel = model;
|
jaroslav@500
|
125 |
try {
|
jaroslav@505
|
126 |
String res = model.getNotAllowedRead();
|
jaroslav@500
|
127 |
fail("We should not be allowed to read from the model: " + res);
|
jaroslav@500
|
128 |
} catch (IllegalStateException ex) {
|
jaroslav@500
|
129 |
// OK, we can't read
|
jaroslav@500
|
130 |
}
|
jaroslav@500
|
131 |
}
|
jaroslav@500
|
132 |
|
jaroslav@491
|
133 |
@ComputedProperty
|
jaroslav@491
|
134 |
static int powerValue(int value) {
|
jaroslav@491
|
135 |
return value * value;
|
jaroslav@491
|
136 |
}
|
jaroslav@499
|
137 |
|
jaroslav@500
|
138 |
@ComputedProperty
|
jaroslav@500
|
139 |
static String notAllowedRead() {
|
jaroslav@505
|
140 |
return "Not allowed callback: " + leakedModel.getUnrelated();
|
jaroslav@500
|
141 |
}
|
jaroslav@500
|
142 |
|
jaroslav@500
|
143 |
@ComputedProperty
|
jaroslav@500
|
144 |
static String notAllowedWrite() {
|
jaroslav@505
|
145 |
leakedModel.setUnrelated(11);
|
jaroslav@500
|
146 |
return "Not allowed callback!";
|
jaroslav@500
|
147 |
}
|
jaroslav@500
|
148 |
|
jaroslav@499
|
149 |
static class MockKnockout extends Knockout {
|
jaroslav@499
|
150 |
List<String> mutated = new ArrayList<String>();
|
jaroslav@499
|
151 |
|
jaroslav@499
|
152 |
@Override
|
jaroslav@499
|
153 |
public void valueHasMutated(String prop) {
|
jaroslav@499
|
154 |
mutated.add(prop);
|
jaroslav@499
|
155 |
}
|
jaroslav@499
|
156 |
}
|
jaroslav@489
|
157 |
}
|