jaroslav@26: /** jaroslav@106: * Back 2 Browser Bytecode Translator jaroslav@106: * Copyright (C) 2012 Jaroslav Tulach jaroslav@26: * jaroslav@26: * This program is free software: you can redistribute it and/or modify jaroslav@26: * it under the terms of the GNU General Public License as published by jaroslav@26: * the Free Software Foundation, version 2 of the License. jaroslav@26: * jaroslav@26: * This program is distributed in the hope that it will be useful, jaroslav@26: * but WITHOUT ANY WARRANTY; without even the implied warranty of jaroslav@26: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the jaroslav@26: * GNU General Public License for more details. jaroslav@26: * jaroslav@26: * You should have received a copy of the GNU General Public License jaroslav@26: * along with this program. Look for COPYING file in the top folder. jaroslav@26: * If not, see http://opensource.org/licenses/GPL-2.0. jaroslav@26: */ jaroslav@25: package org.apidesign.bck2brwsr.htmlpage; jaroslav@25: jaroslav@25: import java.io.IOException; jaroslav@25: import java.io.InputStream; jaroslav@100: import java.lang.reflect.Method; jaroslav@25: import java.util.Set; jaroslav@100: import javax.script.Invocable; jaroslav@100: import javax.script.ScriptEngine; jaroslav@100: import javax.script.ScriptEngineManager; jaroslav@100: import javax.script.ScriptException; jaroslav@25: import org.testng.annotations.Test; jaroslav@25: import static org.testng.Assert.*; jaroslav@25: jaroslav@25: public class ProcessPageTest { jaroslav@25: jaroslav@25: jaroslav@25: @Test public void findsThreeIds() throws IOException { jaroslav@100: InputStream is = ProcessPageTest.class.getResourceAsStream("TestPage.html"); jaroslav@25: assertNotNull(is, "Sample HTML page found"); jaroslav@25: ProcessPage res = ProcessPage.readPage(is); jaroslav@25: final Set ids = res.ids(); jaroslav@25: assertEquals(ids.size(), 3, "Three ids found: " + ids); jaroslav@25: jaroslav@25: assertEquals(res.tagNameForId("pg.title"), "title"); jaroslav@25: assertEquals(res.tagNameForId("pg.button"), "button"); jaroslav@25: assertEquals(res.tagNameForId("pg.text"), "input"); jaroslav@25: } jaroslav@26: jaroslav@100: @Test public void testCompileAndRunPageController() throws Exception { jaroslav@100: StringBuilder sb = new StringBuilder(); jaroslav@100: sb.append( jaroslav@100: "var window = new Object();\n" jaroslav@100: + "var doc = new Object();\n" jaroslav@100: + "doc.button = new Object();\n" jaroslav@100: + "doc.title = new Object();\n" jaroslav@100: + "doc.title.innerHTML = 'nothing';\n" jaroslav@100: + "doc.text = new Object();\n" jaroslav@100: + "doc.text.value = 'something';\n" jaroslav@100: + "doc.getElementById = function(id) {\n" jaroslav@100: + " switch(id) {\n" jaroslav@100: + " case 'pg.button': return doc.button;\n" jaroslav@100: + " case 'pg.title': return doc.title;\n" jaroslav@100: + " case 'pg.text': return doc.text;\n" jaroslav@100: + " }\n" jaroslav@100: + " throw id;\n" jaroslav@100: + " }\n" jaroslav@100: + "\n" jaroslav@100: + "function clickAndCheck() {\n" jaroslav@100: + " doc.button.onclick();\n" jaroslav@100: + " return doc.title.innerHTML.toString();\n" jaroslav@100: + "};\n" jaroslav@100: + "\n" jaroslav@100: + "window.document = doc;\n" jaroslav@100: ); jaroslav@100: Invocable i = compileClass(sb, "org/apidesign/bck2brwsr/htmlpage/PageController"); jaroslav@100: jaroslav@100: Object ret = null; jaroslav@100: try { jaroslav@100: ret = i.invokeFunction("clickAndCheck"); jaroslav@100: } catch (ScriptException ex) { jaroslav@100: fail("Execution failed in " + sb, ex); jaroslav@100: } catch (NoSuchMethodException ex) { jaroslav@100: fail("Cannot find method in " + sb, ex); jaroslav@100: } jaroslav@104: assertEquals(ret, "You want this window to be named something", "We expect that the JavaCode performs all the wiring"); jaroslav@26: } jaroslav@100: jaroslav@100: static Invocable compileClass(StringBuilder sb, String... names) throws ScriptException, IOException { jaroslav@100: if (sb == null) { jaroslav@100: sb = new StringBuilder(); jaroslav@100: } jaroslav@100: try { jaroslav@100: Method m; jaroslav@100: Class genJS = Class.forName("org.apidesign.vm4brwsr.GenJS"); jaroslav@100: m = genJS.getDeclaredMethod("compile", Appendable.class, String[].class); jaroslav@100: m.setAccessible(true); jaroslav@100: m.invoke(null, sb, names); jaroslav@100: } catch (Exception exception) { jaroslav@100: throw new IOException(exception); jaroslav@100: } jaroslav@100: ScriptEngineManager sem = new ScriptEngineManager(); jaroslav@100: ScriptEngine js = sem.getEngineByExtension("js"); jaroslav@100: try { jaroslav@100: Object res = js.eval(sb.toString()); jaroslav@100: assertTrue(js instanceof Invocable, "It is invocable object: " + res); jaroslav@100: return (Invocable) js; jaroslav@100: } catch (ScriptException ex) { jaroslav@100: fail("Could not compile:\n" + sb, ex); jaroslav@100: return null; jaroslav@100: } jaroslav@28: } jaroslav@25: }