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;
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(), 3, "Three ids found: " + ids);
41 assertEquals(res.tagNameForId("pg.title"), "title");
42 assertEquals(res.tagNameForId("pg.button"), "button");
43 assertEquals(res.tagNameForId("pg.text"), "input");
46 @Test public void testCompileAndRunPageController() throws Exception {
47 StringBuilder sb = new StringBuilder();
49 "var window = new Object();\n"
50 + "var doc = new Object();\n"
51 + "doc.button = new Object();\n"
52 + "doc.title = new Object();\n"
53 + "doc.title.innerHTML = 'nothing';\n"
54 + "doc.text = new Object();\n"
55 + "doc.text.value = 'something';\n"
56 + "doc.getElementById = function(id) {\n"
58 + " case 'pg.button': return doc.button;\n"
59 + " case 'pg.title': return doc.title;\n"
60 + " case 'pg.text': return doc.text;\n"
65 + "function clickAndCheck() {\n"
66 + " doc.button.onclick();\n"
67 + " return doc.title.innerHTML.toString();\n"
70 + "window.document = doc;\n"
72 Invocable i = compileClass(sb, "org/apidesign/bck2brwsr/htmlpage/PageController");
76 ret = i.invokeFunction("clickAndCheck");
77 } catch (ScriptException ex) {
78 fail("Execution failed in " + sb, ex);
79 } catch (NoSuchMethodException ex) {
80 fail("Cannot find method in " + sb, ex);
82 assertEquals(ret, "You want this window to be named something", "We expect that the JavaCode performs all the wiring");
85 @Test public void clickWithArgumentCalled() throws Exception {
86 StringBuilder sb = new StringBuilder();
88 "var window = new Object();\n"
89 + "var doc = new Object();\n"
90 + "doc.button = new Object();\n"
91 + "doc.title = new Object();\n"
92 + "doc.title.innerHTML = 'nothing';\n"
93 + "doc.text = new Object();\n"
94 + "doc.text.value = 'something';\n"
95 + "doc.getElementById = function(id) {\n"
97 + " case 'pg.button': return doc.button;\n"
98 + " case 'pg.title': return doc.title;\n"
99 + " case 'pg.text': return doc.text;\n"
104 + "function clickAndCheck() {\n"
105 + " doc.title.onclick();\n"
106 + " return doc.title.innerHTML.toString();\n"
109 + "window.document = doc;\n"
111 Invocable i = compileClass(sb,
112 "org/apidesign/bck2brwsr/htmlpage/PageController"
117 ret = i.invokeFunction("clickAndCheck");
118 } catch (ScriptException ex) {
119 fail("Execution failed in " + sb, ex);
120 } catch (NoSuchMethodException ex) {
121 fail("Cannot find method in " + sb, ex);
123 assertEquals(ret, "pg.title", "Title has been passed to the method argument");
126 static Invocable compileClass(StringBuilder sb, String... names) throws ScriptException, IOException {
128 sb = new StringBuilder();
130 Bck2Brwsr.generate(sb, ProcessPageTest.class.getClassLoader(), names);
131 sb.append("var vm = this.bck2brwsr();\n");
133 ScriptEngineManager sem = new ScriptEngineManager();
134 ScriptEngine js = sem.getEngineByExtension("js");
136 Object res = js.eval(sb.toString());
137 assertTrue(js instanceof Invocable, "It is invocable object: " + res);
138 return (Invocable) js;
139 } catch (ScriptException ex) {
140 fail("Could not compile:\n" + sb, ex);