javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/ProcessPageTest.java
2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012-2015 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.bck2brwsr.core.ExtraJavaScript;
28 import org.apidesign.vm4brwsr.Bck2Brwsr;
29 import org.testng.annotations.Test;
30 import static org.testng.Assert.*;
32 @ExtraJavaScript(processByteCode = false, resource = "")
33 public class ProcessPageTest {
36 @Test public void findsThreeIds() throws IOException {
37 InputStream is = ProcessPageTest.class.getResourceAsStream("TestPage.html");
38 assertNotNull(is, "Sample HTML page found");
39 ProcessPage res = ProcessPage.readPage(is);
40 final Set<String> ids = res.ids();
41 assertEquals(ids.size(), 4, "Four ids found: " + ids);
43 assertEquals(res.tagNameForId("pg.title"), "title");
44 assertEquals(res.tagNameForId("pg.button"), "button");
45 assertEquals(res.tagNameForId("pg.text"), "input");
46 assertEquals(res.tagNameForId("pg.canvas"), "canvas");
49 @Test public void testCompileAndRunPageController() throws Exception {
50 StringBuilder sb = new StringBuilder();
52 "var window = new Object();\n"
53 + "var doc = new Object();\n"
54 + "doc.button = new Object();\n"
55 + "doc.title = new Object();\n"
56 + "doc.title.innerHTML = 'nothing';\n"
57 + "doc.text = new Object();\n"
58 + "doc.text.value = 'something';\n"
59 + "doc.canvas = new Object();\n"
60 + "doc.getElementById = function(id) {\n"
62 + " case 'pg.button': return doc.button;\n"
63 + " case 'pg.title': return doc.title;\n"
64 + " case 'pg.text': return doc.text;\n"
65 + " case 'pg.canvas': return doc.canvas;\n"
70 + "function clickAndCheck() {\n"
71 + " doc.button.onclick();\n"
72 + " return doc.title.innerHTML.toString();\n"
75 + "window.document = doc;\n"
77 Invocable i = compileClass(sb, "org/apidesign/bck2brwsr/htmlpage/PageController");
81 ret = i.invokeFunction("clickAndCheck");
82 } catch (ScriptException ex) {
83 fail("Execution failed in " + sb, ex);
84 } catch (NoSuchMethodException ex) {
85 fail("Cannot find method in " + sb, ex);
87 assertEquals(ret, "You want this window to be named something", "We expect that the JavaCode performs all the wiring");
90 @Test public void clickWithArgumentCalled() throws Exception {
91 StringBuilder sb = new StringBuilder();
93 "var window = new Object();\n"
94 + "var doc = new Object();\n"
95 + "doc.button = new Object();\n"
96 + "doc.title = new Object();\n"
97 + "doc.title.innerHTML = 'nothing';\n"
98 + "doc.text = new Object();\n"
99 + "doc.text.value = 'something';\n"
100 + "doc.canvas = new Object();\n"
101 + "doc.getElementById = function(id) {\n"
103 + " case 'pg.button': return doc.button;\n"
104 + " case 'pg.title': return doc.title;\n"
105 + " case 'pg.text': return doc.text;\n"
106 + " case 'pg.canvas': return doc.canvas;\n"
111 + "function clickAndCheck() {\n"
112 + " doc.title.onclick();\n"
113 + " return doc.title.innerHTML.toString();\n"
116 + "window.document = doc;\n"
118 Invocable i = compileClass(sb,
119 "org/apidesign/bck2brwsr/htmlpage/PageController"
124 ret = i.invokeFunction("clickAndCheck");
125 } catch (ScriptException ex) {
126 fail("Execution failed in " + sb, ex);
127 } catch (NoSuchMethodException ex) {
128 fail("Cannot find method in " + sb, ex);
130 assertEquals(ret, "pg.title", "Title has been passed to the method argument");
133 @Test public void clickWithArgumentAndParameterCalled() throws Exception {
134 StringBuilder sb = new StringBuilder();
136 "var window = new Object();\n"
137 + "var doc = new Object();\n"
138 + "var eventObject = new Object();\n"
139 + "eventObject.layerX = 100;\n"
140 + "doc.button = new Object();\n"
141 + "doc.title = new Object();\n"
142 + "doc.title.innerHTML = 'nothing';\n"
143 + "doc.text = new Object();\n"
144 + "doc.text.value = 'something';\n"
145 + "doc.canvas = new Object();\n"
146 + "doc.canvas.width = 200;\n"
147 + "doc.getElementById = function(id) {\n"
149 + " case 'pg.button': return doc.button;\n"
150 + " case 'pg.title': return doc.title;\n"
151 + " case 'pg.text': return doc.text;\n"
152 + " case 'pg.canvas': return doc.canvas;\n"
157 + "function clickAndCheck() {\n"
158 + " doc.canvas.onclick(eventObject);\n"
159 + " return doc.canvas.width.toString();\n"
162 + "window.document = doc;\n"
164 Invocable i = compileClass(sb,
165 "org/apidesign/bck2brwsr/htmlpage/PageController"
170 ret = i.invokeFunction("clickAndCheck");
171 } catch (ScriptException ex) {
172 fail("Execution failed in " + sb, ex);
173 } catch (NoSuchMethodException ex) {
174 fail("Cannot find method in " + sb, ex);
176 assertEquals(ret, "100", "layerX has been passed to the method argument");
179 static Invocable compileClass(StringBuilder sb, String... names) throws ScriptException, IOException {
181 sb = new StringBuilder();
183 Bck2Brwsr.generate(sb, ProcessPageTest.class.getClassLoader(), names);
184 sb.append("var vm = this.bck2brwsr();\n");
185 for (String c : names) {
186 sb.append("vm.loadClass('").append(c.replace('/', '.')).append("');\n");
189 ScriptEngineManager sem = new ScriptEngineManager();
190 ScriptEngine js = sem.getEngineByExtension("js");
192 Object res = js.eval(sb.toString());
193 assertTrue(js instanceof Invocable, "It is invocable object: " + res);
194 return (Invocable) js;
195 } catch (ScriptException ex) {
196 fail("Could not compile:\n" + sb, ex);