javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/ProcessPageTest.java
Trying to make the static compilation mode as close to lazy one as possible: Class initializer method are not executed until the class is really referenced.
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.io.IOException;
21 import java.io.InputStream;
23 import javax.script.Invocable;
24 import javax.script.ScriptEngine;
25 import javax.script.ScriptEngineManager;
26 import javax.script.ScriptException;
27 import org.apidesign.vm4brwsr.Bck2Brwsr;
28 import org.testng.annotations.Test;
29 import static org.testng.Assert.*;
31 public class ProcessPageTest {
34 @Test public void findsThreeIds() throws IOException {
35 InputStream is = ProcessPageTest.class.getResourceAsStream("TestPage.html");
36 assertNotNull(is, "Sample HTML page found");
37 ProcessPage res = ProcessPage.readPage(is);
38 final Set<String> ids = res.ids();
39 assertEquals(ids.size(), 4, "Four ids found: " + ids);
41 assertEquals(res.tagNameForId("pg.title"), "title");
42 assertEquals(res.tagNameForId("pg.button"), "button");
43 assertEquals(res.tagNameForId("pg.text"), "input");
44 assertEquals(res.tagNameForId("pg.canvas"), "canvas");
47 @Test public void testCompileAndRunPageController() throws Exception {
48 StringBuilder sb = new StringBuilder();
50 "var window = new Object();\n"
51 + "var doc = new Object();\n"
52 + "doc.button = new Object();\n"
53 + "doc.title = new Object();\n"
54 + "doc.title.innerHTML = 'nothing';\n"
55 + "doc.text = new Object();\n"
56 + "doc.text.value = 'something';\n"
57 + "doc.canvas = new Object();\n"
58 + "doc.getElementById = function(id) {\n"
60 + " case 'pg.button': return doc.button;\n"
61 + " case 'pg.title': return doc.title;\n"
62 + " case 'pg.text': return doc.text;\n"
63 + " case 'pg.canvas': return doc.canvas;\n"
68 + "function clickAndCheck() {\n"
69 + " doc.button.onclick();\n"
70 + " return doc.title.innerHTML.toString();\n"
73 + "window.document = doc;\n"
75 Invocable i = compileClass(sb, "org/apidesign/bck2brwsr/htmlpage/PageController");
79 ret = i.invokeFunction("clickAndCheck");
80 } catch (ScriptException ex) {
81 fail("Execution failed in " + sb, ex);
82 } catch (NoSuchMethodException ex) {
83 fail("Cannot find method in " + sb, ex);
85 assertEquals(ret, "You want this window to be named something", "We expect that the JavaCode performs all the wiring");
88 @Test public void clickWithArgumentCalled() throws Exception {
89 StringBuilder sb = new StringBuilder();
91 "var window = new Object();\n"
92 + "var doc = new Object();\n"
93 + "doc.button = new Object();\n"
94 + "doc.title = new Object();\n"
95 + "doc.title.innerHTML = 'nothing';\n"
96 + "doc.text = new Object();\n"
97 + "doc.text.value = 'something';\n"
98 + "doc.canvas = new Object();\n"
99 + "doc.getElementById = function(id) {\n"
101 + " case 'pg.button': return doc.button;\n"
102 + " case 'pg.title': return doc.title;\n"
103 + " case 'pg.text': return doc.text;\n"
104 + " case 'pg.canvas': return doc.canvas;\n"
109 + "function clickAndCheck() {\n"
110 + " doc.title.onclick();\n"
111 + " return doc.title.innerHTML.toString();\n"
114 + "window.document = doc;\n"
116 Invocable i = compileClass(sb,
117 "org/apidesign/bck2brwsr/htmlpage/PageController"
122 ret = i.invokeFunction("clickAndCheck");
123 } catch (ScriptException ex) {
124 fail("Execution failed in " + sb, ex);
125 } catch (NoSuchMethodException ex) {
126 fail("Cannot find method in " + sb, ex);
128 assertEquals(ret, "pg.title", "Title has been passed to the method argument");
131 @Test public void clickWithArgumentAndParameterCalled() throws Exception {
132 StringBuilder sb = new StringBuilder();
134 "var window = new Object();\n"
135 + "var doc = new Object();\n"
136 + "var eventObject = new Object();\n"
137 + "eventObject.layerX = 100;\n"
138 + "doc.button = new Object();\n"
139 + "doc.title = new Object();\n"
140 + "doc.title.innerHTML = 'nothing';\n"
141 + "doc.text = new Object();\n"
142 + "doc.text.value = 'something';\n"
143 + "doc.canvas = new Object();\n"
144 + "doc.canvas.width = 200;\n"
145 + "doc.getElementById = function(id) {\n"
147 + " case 'pg.button': return doc.button;\n"
148 + " case 'pg.title': return doc.title;\n"
149 + " case 'pg.text': return doc.text;\n"
150 + " case 'pg.canvas': return doc.canvas;\n"
155 + "function clickAndCheck() {\n"
156 + " doc.canvas.onclick(eventObject);\n"
157 + " return doc.canvas.width.toString();\n"
160 + "window.document = doc;\n"
162 Invocable i = compileClass(sb,
163 "org/apidesign/bck2brwsr/htmlpage/PageController"
168 ret = i.invokeFunction("clickAndCheck");
169 } catch (ScriptException ex) {
170 fail("Execution failed in " + sb, ex);
171 } catch (NoSuchMethodException ex) {
172 fail("Cannot find method in " + sb, ex);
174 assertEquals(ret, "100", "layerX has been passed to the method argument");
177 static Invocable compileClass(StringBuilder sb, String... names) throws ScriptException, IOException {
179 sb = new StringBuilder();
181 Bck2Brwsr.generate(sb, ProcessPageTest.class.getClassLoader(), names);
182 sb.append("var vm = this.bck2brwsr();\n");
183 for (String c : names) {
184 sb.append("vm.loadClass('").append(c.replace('/', '.')).append("');\n");
187 ScriptEngineManager sem = new ScriptEngineManager();
188 ScriptEngine js = sem.getEngineByExtension("js");
190 Object res = js.eval(sb.toString());
191 assertTrue(js instanceof Invocable, "It is invocable object: " + res);
192 return (Invocable) js;
193 } catch (ScriptException ex) {
194 fail("Could not compile:\n" + sb, ex);