javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/ProcessPageTest.java
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;
21 import java.io.FileReader;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.PrintWriter;
26 import javax.script.Invocable;
27 import javax.script.ScriptEngine;
28 import javax.script.ScriptEngineManager;
29 import javax.script.ScriptException;
30 import org.apidesign.vm4brwsr.Bck2Brwsr;
31 import org.testng.annotations.Test;
32 import static org.testng.Assert.*;
34 public class ProcessPageTest {
37 @Test public void findsThreeIds() throws IOException {
38 InputStream is = ProcessPageTest.class.getResourceAsStream("TestPage.html");
39 assertNotNull(is, "Sample HTML page found");
40 ProcessPage res = ProcessPage.readPage(is);
41 final Set<String> ids = res.ids();
42 assertEquals(ids.size(), 4, "Four ids found: " + ids);
44 assertEquals(res.tagNameForId("pg.title"), "title");
45 assertEquals(res.tagNameForId("pg.button"), "button");
46 assertEquals(res.tagNameForId("pg.text"), "input");
47 assertEquals(res.tagNameForId("pg.canvas"), "canvas");
50 @Test public void testCompileAndRunPageController() throws Exception {
51 StringBuilder sb = new StringBuilder();
53 "var window = new Object();\n"
54 + "var doc = new Object();\n"
55 + "doc.button = new Object();\n"
56 + "doc.title = new Object();\n"
57 + "doc.title.innerHTML = 'nothing';\n"
58 + "doc.text = new Object();\n"
59 + "doc.text.value = 'something';\n"
60 + "doc.canvas = new Object();\n"
61 + "doc.getElementById = function(id) {\n"
63 + " case 'pg.button': return doc.button;\n"
64 + " case 'pg.title': return doc.title;\n"
65 + " case 'pg.text': return doc.text;\n"
66 + " case 'pg.canvas': return doc.canvas;\n"
71 + "function clickAndCheck() {\n"
72 + " doc.button.onclick();\n"
73 + " return doc.title.innerHTML.toString();\n"
76 + "window.document = doc;\n"
78 Invocable i = compileClass(sb, "org/apidesign/bck2brwsr/htmlpage/PageController");
82 ret = i.invokeFunction("clickAndCheck");
83 } catch (ScriptException ex) {
84 fail("Execution failed in " + sb, ex);
85 } catch (NoSuchMethodException ex) {
86 fail("Cannot find method in " + sb, ex);
88 assertEquals(ret, "You want this window to be named something", "We expect that the JavaCode performs all the wiring");
91 // @Test public void clickWithArgumentCalled() throws Exception {
92 // StringBuilder sb = new StringBuilder();
94 // "var window = new Object();\n"
95 // + "var doc = new Object();\n"
96 // + "doc.button = new Object();\n"
97 // + "doc.title = new Object();\n"
98 // + "doc.title.innerHTML = 'nothing';\n"
99 // + "doc.text = new Object();\n"
100 // + "doc.text.value = 'something';\n"
101 // + "doc.canvas = new Object();\n"
102 // + "doc.getElementById = function(id) {\n"
103 // + " switch(id) {\n"
104 // + " case 'pg.button': return doc.button;\n"
105 // + " case 'pg.title': return doc.title;\n"
106 // + " case 'pg.text': return doc.text;\n"
107 // + " case 'pg.canvas': return doc.canvas;\n"
112 // + "function clickAndCheck() {\n"
113 // + " doc.title.onclick();\n"
114 // + " return doc.title.innerHTML.toString();\n"
117 // + "window.document = doc;\n"
119 // Invocable i = compileClass(sb,
120 // "org/apidesign/bck2brwsr/htmlpage/PageController"
123 // Object ret = null;
125 // ret = i.invokeFunction("clickAndCheck");
126 // } catch (ScriptException ex) {
127 // fail("Execution failed in " + sb, ex);
128 // } catch (NoSuchMethodException ex) {
129 // fail("Cannot find method in " + sb, ex);
131 // assertEquals(ret, "pg.title", "Title has been passed to the method argument");
134 // @Test public void clickWithArgumentAndParameterCalled() throws Exception {
135 // StringBuilder sb = new StringBuilder();
137 // "var window = new Object();\n"
138 // + "var doc = new Object();\n"
139 // + "var eventObject = new Object();\n"
140 // + "eventObject.layerX = 100;\n"
141 // + "doc.button = new Object();\n"
142 // + "doc.title = new Object();\n"
143 // + "doc.title.innerHTML = 'nothing';\n"
144 // + "doc.text = new Object();\n"
145 // + "doc.text.value = 'something';\n"
146 // + "doc.canvas = new Object();\n"
147 // + "doc.canvas.width = 200;\n"
148 // + "doc.getElementById = function(id) {\n"
149 // + " switch(id) {\n"
150 // + " case 'pg.button': return doc.button;\n"
151 // + " case 'pg.title': return doc.title;\n"
152 // + " case 'pg.text': return doc.text;\n"
153 // + " case 'pg.canvas': return doc.canvas;\n"
158 // + "function clickAndCheck() {\n"
159 // + " doc.canvas.onclick(eventObject);\n"
160 // + " return doc.canvas.width.toString();\n"
163 // + "window.document = doc;\n"
165 // Invocable i = compileClass(sb,
166 // "org/apidesign/bck2brwsr/htmlpage/PageController"
169 // Object ret = null;
171 // ret = i.invokeFunction("clickAndCheck");
172 // } catch (ScriptException ex) {
173 // fail("Execution failed in " + sb, ex);
174 // } catch (NoSuchMethodException ex) {
175 // fail("Cannot find method in " + sb, ex);
177 // assertEquals(ret, "100", "layerX has been passed to the method argument");
180 static Invocable compileClass(StringBuilder sb, String... names) throws ScriptException, IOException {
183 sb = new StringBuilder();
185 Bck2Brwsr.generate(sb, ProcessPageTest.class.getClassLoader(), names);
186 sb.append("var vm = this.bck2brwsr();\n");
187 for (String c : names) {
188 sb.append("vm.loadClass('").append(c.replace('/', '.')).append("');\n");
191 ScriptEngineManager sem = new ScriptEngineManager();
192 ScriptEngine js = sem.getEngineByExtension("js");
194 Object res = js.eval(sb.toString());
195 assertTrue(js instanceof Invocable, "It is invocable object: " + res);
196 return (Invocable) js;
197 } catch (ScriptException ex) {
198 // System.err.println("ex "+ex+ " col "+ex.getColumnNumber());
199 // PrintWriter out = new PrintWriter("filename.txt");
200 // out.println(sb.toString());
203 fail("Could not compile:\n" , ex);