jaroslav@26
|
1 |
/**
|
jaroslav@106
|
2 |
* Back 2 Browser Bytecode Translator
|
jaroslav@106
|
3 |
* Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jaroslav@26
|
4 |
*
|
jaroslav@26
|
5 |
* This program is free software: you can redistribute it and/or modify
|
jaroslav@26
|
6 |
* it under the terms of the GNU General Public License as published by
|
jaroslav@26
|
7 |
* the Free Software Foundation, version 2 of the License.
|
jaroslav@26
|
8 |
*
|
jaroslav@26
|
9 |
* This program is distributed in the hope that it will be useful,
|
jaroslav@26
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
jaroslav@26
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
jaroslav@26
|
12 |
* GNU General Public License for more details.
|
jaroslav@26
|
13 |
*
|
jaroslav@26
|
14 |
* You should have received a copy of the GNU General Public License
|
jaroslav@26
|
15 |
* along with this program. Look for COPYING file in the top folder.
|
jaroslav@26
|
16 |
* If not, see http://opensource.org/licenses/GPL-2.0.
|
jaroslav@26
|
17 |
*/
|
jaroslav@25
|
18 |
package org.apidesign.bck2brwsr.htmlpage;
|
jaroslav@25
|
19 |
|
jaroslav@25
|
20 |
import java.io.IOException;
|
jaroslav@25
|
21 |
import java.io.InputStream;
|
jaroslav@25
|
22 |
import java.util.Set;
|
jaroslav@100
|
23 |
import javax.script.Invocable;
|
jaroslav@100
|
24 |
import javax.script.ScriptEngine;
|
jaroslav@100
|
25 |
import javax.script.ScriptEngineManager;
|
jaroslav@100
|
26 |
import javax.script.ScriptException;
|
jaroslav@304
|
27 |
import org.apidesign.vm4brwsr.Bck2Brwsr;
|
jaroslav@25
|
28 |
import org.testng.annotations.Test;
|
jaroslav@25
|
29 |
import static org.testng.Assert.*;
|
jaroslav@25
|
30 |
|
jaroslav@25
|
31 |
public class ProcessPageTest {
|
jaroslav@25
|
32 |
|
jaroslav@25
|
33 |
|
jaroslav@25
|
34 |
@Test public void findsThreeIds() throws IOException {
|
jaroslav@100
|
35 |
InputStream is = ProcessPageTest.class.getResourceAsStream("TestPage.html");
|
jaroslav@25
|
36 |
assertNotNull(is, "Sample HTML page found");
|
jaroslav@25
|
37 |
ProcessPage res = ProcessPage.readPage(is);
|
jaroslav@25
|
38 |
final Set<String> ids = res.ids();
|
jaroslav@813
|
39 |
assertEquals(ids.size(), 4, "Four ids found: " + ids);
|
jaroslav@25
|
40 |
|
jaroslav@25
|
41 |
assertEquals(res.tagNameForId("pg.title"), "title");
|
jaroslav@25
|
42 |
assertEquals(res.tagNameForId("pg.button"), "button");
|
jaroslav@25
|
43 |
assertEquals(res.tagNameForId("pg.text"), "input");
|
jaroslav@813
|
44 |
assertEquals(res.tagNameForId("pg.canvas"), "canvas");
|
jaroslav@25
|
45 |
}
|
jaroslav@26
|
46 |
|
jaroslav@100
|
47 |
@Test public void testCompileAndRunPageController() throws Exception {
|
jaroslav@100
|
48 |
StringBuilder sb = new StringBuilder();
|
jaroslav@100
|
49 |
sb.append(
|
jaroslav@100
|
50 |
"var window = new Object();\n"
|
jaroslav@100
|
51 |
+ "var doc = new Object();\n"
|
jaroslav@100
|
52 |
+ "doc.button = new Object();\n"
|
jaroslav@100
|
53 |
+ "doc.title = new Object();\n"
|
jaroslav@100
|
54 |
+ "doc.title.innerHTML = 'nothing';\n"
|
jaroslav@100
|
55 |
+ "doc.text = new Object();\n"
|
jaroslav@100
|
56 |
+ "doc.text.value = 'something';\n"
|
jaroslav@813
|
57 |
+ "doc.canvas = new Object();\n"
|
jaroslav@100
|
58 |
+ "doc.getElementById = function(id) {\n"
|
jaroslav@100
|
59 |
+ " switch(id) {\n"
|
jaroslav@100
|
60 |
+ " case 'pg.button': return doc.button;\n"
|
jaroslav@100
|
61 |
+ " case 'pg.title': return doc.title;\n"
|
jaroslav@100
|
62 |
+ " case 'pg.text': return doc.text;\n"
|
jaroslav@813
|
63 |
+ " case 'pg.canvas': return doc.canvas;\n"
|
jaroslav@100
|
64 |
+ " }\n"
|
jaroslav@100
|
65 |
+ " throw id;\n"
|
jaroslav@100
|
66 |
+ " }\n"
|
jaroslav@100
|
67 |
+ "\n"
|
jaroslav@100
|
68 |
+ "function clickAndCheck() {\n"
|
jaroslav@100
|
69 |
+ " doc.button.onclick();\n"
|
jaroslav@100
|
70 |
+ " return doc.title.innerHTML.toString();\n"
|
jaroslav@100
|
71 |
+ "};\n"
|
jaroslav@100
|
72 |
+ "\n"
|
jaroslav@100
|
73 |
+ "window.document = doc;\n"
|
jaroslav@100
|
74 |
);
|
jaroslav@100
|
75 |
Invocable i = compileClass(sb, "org/apidesign/bck2brwsr/htmlpage/PageController");
|
jaroslav@100
|
76 |
|
jaroslav@100
|
77 |
Object ret = null;
|
jaroslav@100
|
78 |
try {
|
jaroslav@100
|
79 |
ret = i.invokeFunction("clickAndCheck");
|
jaroslav@100
|
80 |
} catch (ScriptException ex) {
|
jaroslav@100
|
81 |
fail("Execution failed in " + sb, ex);
|
jaroslav@100
|
82 |
} catch (NoSuchMethodException ex) {
|
jaroslav@100
|
83 |
fail("Cannot find method in " + sb, ex);
|
jaroslav@100
|
84 |
}
|
jaroslav@104
|
85 |
assertEquals(ret, "You want this window to be named something", "We expect that the JavaCode performs all the wiring");
|
jaroslav@26
|
86 |
}
|
jaroslav@124
|
87 |
|
jaroslav@124
|
88 |
@Test public void clickWithArgumentCalled() throws Exception {
|
jaroslav@124
|
89 |
StringBuilder sb = new StringBuilder();
|
jaroslav@124
|
90 |
sb.append(
|
jaroslav@124
|
91 |
"var window = new Object();\n"
|
jaroslav@124
|
92 |
+ "var doc = new Object();\n"
|
jaroslav@124
|
93 |
+ "doc.button = new Object();\n"
|
jaroslav@124
|
94 |
+ "doc.title = new Object();\n"
|
jaroslav@124
|
95 |
+ "doc.title.innerHTML = 'nothing';\n"
|
jaroslav@124
|
96 |
+ "doc.text = new Object();\n"
|
jaroslav@124
|
97 |
+ "doc.text.value = 'something';\n"
|
jaroslav@813
|
98 |
+ "doc.canvas = new Object();\n"
|
jaroslav@124
|
99 |
+ "doc.getElementById = function(id) {\n"
|
jaroslav@124
|
100 |
+ " switch(id) {\n"
|
jaroslav@124
|
101 |
+ " case 'pg.button': return doc.button;\n"
|
jaroslav@124
|
102 |
+ " case 'pg.title': return doc.title;\n"
|
jaroslav@124
|
103 |
+ " case 'pg.text': return doc.text;\n"
|
jaroslav@813
|
104 |
+ " case 'pg.canvas': return doc.canvas;\n"
|
jaroslav@124
|
105 |
+ " }\n"
|
jaroslav@124
|
106 |
+ " throw id;\n"
|
jaroslav@124
|
107 |
+ " }\n"
|
jaroslav@124
|
108 |
+ "\n"
|
jaroslav@124
|
109 |
+ "function clickAndCheck() {\n"
|
jaroslav@124
|
110 |
+ " doc.title.onclick();\n"
|
jaroslav@124
|
111 |
+ " return doc.title.innerHTML.toString();\n"
|
jaroslav@124
|
112 |
+ "};\n"
|
jaroslav@124
|
113 |
+ "\n"
|
jaroslav@124
|
114 |
+ "window.document = doc;\n"
|
jaroslav@124
|
115 |
);
|
jaroslav@492
|
116 |
Invocable i = compileClass(sb,
|
jaroslav@492
|
117 |
"org/apidesign/bck2brwsr/htmlpage/PageController"
|
jaroslav@492
|
118 |
);
|
jaroslav@124
|
119 |
|
jaroslav@124
|
120 |
Object ret = null;
|
jaroslav@124
|
121 |
try {
|
jaroslav@124
|
122 |
ret = i.invokeFunction("clickAndCheck");
|
jaroslav@124
|
123 |
} catch (ScriptException ex) {
|
jaroslav@124
|
124 |
fail("Execution failed in " + sb, ex);
|
jaroslav@124
|
125 |
} catch (NoSuchMethodException ex) {
|
jaroslav@124
|
126 |
fail("Cannot find method in " + sb, ex);
|
jaroslav@124
|
127 |
}
|
jaroslav@124
|
128 |
assertEquals(ret, "pg.title", "Title has been passed to the method argument");
|
jaroslav@124
|
129 |
}
|
jaroslav@100
|
130 |
|
jaroslav@813
|
131 |
@Test public void clickWithArgumentAndParameterCalled() throws Exception {
|
jaroslav@813
|
132 |
StringBuilder sb = new StringBuilder();
|
jaroslav@813
|
133 |
sb.append(
|
jaroslav@813
|
134 |
"var window = new Object();\n"
|
jaroslav@813
|
135 |
+ "var doc = new Object();\n"
|
jaroslav@813
|
136 |
+ "var eventObject = new Object();\n"
|
jaroslav@813
|
137 |
+ "eventObject.layerX = 100;\n"
|
jaroslav@813
|
138 |
+ "doc.button = new Object();\n"
|
jaroslav@813
|
139 |
+ "doc.title = new Object();\n"
|
jaroslav@813
|
140 |
+ "doc.title.innerHTML = 'nothing';\n"
|
jaroslav@813
|
141 |
+ "doc.text = new Object();\n"
|
jaroslav@813
|
142 |
+ "doc.text.value = 'something';\n"
|
jaroslav@813
|
143 |
+ "doc.canvas = new Object();\n"
|
jaroslav@813
|
144 |
+ "doc.canvas.width = 200;\n"
|
jaroslav@813
|
145 |
+ "doc.getElementById = function(id) {\n"
|
jaroslav@813
|
146 |
+ " switch(id) {\n"
|
jaroslav@813
|
147 |
+ " case 'pg.button': return doc.button;\n"
|
jaroslav@813
|
148 |
+ " case 'pg.title': return doc.title;\n"
|
jaroslav@813
|
149 |
+ " case 'pg.text': return doc.text;\n"
|
jaroslav@813
|
150 |
+ " case 'pg.canvas': return doc.canvas;\n"
|
jaroslav@813
|
151 |
+ " }\n"
|
jaroslav@813
|
152 |
+ " throw id;\n"
|
jaroslav@813
|
153 |
+ " }\n"
|
jaroslav@813
|
154 |
+ "\n"
|
jaroslav@813
|
155 |
+ "function clickAndCheck() {\n"
|
jaroslav@813
|
156 |
+ " doc.canvas.onclick(eventObject);\n"
|
jaroslav@813
|
157 |
+ " return doc.canvas.width.toString();\n"
|
jaroslav@813
|
158 |
+ "};\n"
|
jaroslav@813
|
159 |
+ "\n"
|
jaroslav@813
|
160 |
+ "window.document = doc;\n"
|
jaroslav@813
|
161 |
);
|
jaroslav@813
|
162 |
Invocable i = compileClass(sb,
|
jaroslav@813
|
163 |
"org/apidesign/bck2brwsr/htmlpage/PageController"
|
jaroslav@813
|
164 |
);
|
jaroslav@813
|
165 |
|
jaroslav@813
|
166 |
Object ret = null;
|
jaroslav@813
|
167 |
try {
|
jaroslav@813
|
168 |
ret = i.invokeFunction("clickAndCheck");
|
jaroslav@813
|
169 |
} catch (ScriptException ex) {
|
jaroslav@813
|
170 |
fail("Execution failed in " + sb, ex);
|
jaroslav@813
|
171 |
} catch (NoSuchMethodException ex) {
|
jaroslav@813
|
172 |
fail("Cannot find method in " + sb, ex);
|
jaroslav@813
|
173 |
}
|
jaroslav@813
|
174 |
assertEquals(ret, "100", "layerX has been passed to the method argument");
|
jaroslav@813
|
175 |
}
|
jaroslav@813
|
176 |
|
jaroslav@100
|
177 |
static Invocable compileClass(StringBuilder sb, String... names) throws ScriptException, IOException {
|
jaroslav@100
|
178 |
if (sb == null) {
|
jaroslav@100
|
179 |
sb = new StringBuilder();
|
jaroslav@100
|
180 |
}
|
jaroslav@304
|
181 |
Bck2Brwsr.generate(sb, ProcessPageTest.class.getClassLoader(), names);
|
jaroslav@711
|
182 |
sb.append("var vm = this.bck2brwsr();\n");
|
jaroslav@834
|
183 |
for (String c : names) {
|
jaroslav@834
|
184 |
sb.append("vm.loadClass('").append(c.replace('/', '.')).append("');\n");
|
jaroslav@834
|
185 |
}
|
jaroslav@711
|
186 |
|
jaroslav@100
|
187 |
ScriptEngineManager sem = new ScriptEngineManager();
|
jaroslav@100
|
188 |
ScriptEngine js = sem.getEngineByExtension("js");
|
jaroslav@100
|
189 |
try {
|
jaroslav@100
|
190 |
Object res = js.eval(sb.toString());
|
jaroslav@100
|
191 |
assertTrue(js instanceof Invocable, "It is invocable object: " + res);
|
jaroslav@100
|
192 |
return (Invocable) js;
|
jaroslav@100
|
193 |
} catch (ScriptException ex) {
|
jaroslav@100
|
194 |
fail("Could not compile:\n" + sb, ex);
|
jaroslav@100
|
195 |
return null;
|
jaroslav@100
|
196 |
}
|
jaroslav@28
|
197 |
}
|
jaroslav@25
|
198 |
}
|