htmlpage/src/test/java/org/apidesign/bck2brwsr/htmlpage/ProcessPageTest.java
changeset 140 590958fcb7d7
parent 139 070fc5844295
child 141 63be794c1eeb
     1.1 --- a/htmlpage/src/test/java/org/apidesign/bck2brwsr/htmlpage/ProcessPageTest.java	Fri Nov 09 09:00:46 2012 +0100
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,148 +0,0 @@
     1.4 -/**
     1.5 - * Back 2 Browser Bytecode Translator
     1.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     1.7 - *
     1.8 - * This program is free software: you can redistribute it and/or modify
     1.9 - * it under the terms of the GNU General Public License as published by
    1.10 - * the Free Software Foundation, version 2 of the License.
    1.11 - *
    1.12 - * This program is distributed in the hope that it will be useful,
    1.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.15 - * GNU General Public License for more details.
    1.16 - *
    1.17 - * You should have received a copy of the GNU General Public License
    1.18 - * along with this program. Look for COPYING file in the top folder.
    1.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
    1.20 - */
    1.21 -package org.apidesign.bck2brwsr.htmlpage;
    1.22 -
    1.23 -import java.io.IOException;
    1.24 -import java.io.InputStream;
    1.25 -import java.lang.reflect.Method;
    1.26 -import java.util.Set;
    1.27 -import javax.script.Invocable;
    1.28 -import javax.script.ScriptEngine;
    1.29 -import javax.script.ScriptEngineManager;
    1.30 -import javax.script.ScriptException;
    1.31 -import org.testng.annotations.Test;
    1.32 -import static org.testng.Assert.*;
    1.33 -
    1.34 -public class ProcessPageTest {
    1.35 -    
    1.36 -    
    1.37 -    @Test public void findsThreeIds() throws IOException {
    1.38 -        InputStream is = ProcessPageTest.class.getResourceAsStream("TestPage.html");
    1.39 -        assertNotNull(is, "Sample HTML page found");
    1.40 -        ProcessPage res = ProcessPage.readPage(is);
    1.41 -        final Set<String> ids = res.ids();
    1.42 -        assertEquals(ids.size(), 3, "Three ids found: " + ids);
    1.43 -        
    1.44 -        assertEquals(res.tagNameForId("pg.title"), "title");
    1.45 -        assertEquals(res.tagNameForId("pg.button"), "button");
    1.46 -        assertEquals(res.tagNameForId("pg.text"), "input");
    1.47 -    }
    1.48 -    
    1.49 -    @Test public void testCompileAndRunPageController() throws Exception {
    1.50 -        StringBuilder sb = new StringBuilder();
    1.51 -        sb.append(
    1.52 -              "var window = new Object();\n"
    1.53 -            + "var doc = new Object();\n"
    1.54 -            + "doc.button = new Object();\n"
    1.55 -            + "doc.title = new Object();\n"
    1.56 -            + "doc.title.innerHTML = 'nothing';\n"
    1.57 -            + "doc.text = new Object();\n"
    1.58 -            + "doc.text.value = 'something';\n"
    1.59 -            + "doc.getElementById = function(id) {\n"
    1.60 -            + "    switch(id) {\n"
    1.61 -            + "      case 'pg.button': return doc.button;\n"
    1.62 -            + "      case 'pg.title': return doc.title;\n"
    1.63 -            + "      case 'pg.text': return doc.text;\n"
    1.64 -            + "    }\n"
    1.65 -            + "    throw id;\n"
    1.66 -            + "  }\n"
    1.67 -            + "\n"
    1.68 -            + "function clickAndCheck() {\n"
    1.69 -            + "  doc.button.onclick();\n"
    1.70 -            + "  return doc.title.innerHTML.toString();\n"
    1.71 -            + "};\n"
    1.72 -            + "\n"
    1.73 -            + "window.document = doc;\n"
    1.74 -        );
    1.75 -        Invocable i = compileClass(sb, "org/apidesign/bck2brwsr/htmlpage/PageController");
    1.76 -
    1.77 -        Object ret = null;
    1.78 -        try {
    1.79 -            ret = i.invokeFunction("clickAndCheck");
    1.80 -        } catch (ScriptException ex) {
    1.81 -            fail("Execution failed in " + sb, ex);
    1.82 -        } catch (NoSuchMethodException ex) {
    1.83 -            fail("Cannot find method in " + sb, ex);
    1.84 -        }
    1.85 -        assertEquals(ret, "You want this window to be named something", "We expect that the JavaCode performs all the wiring");
    1.86 -    }
    1.87 -    
    1.88 -    @Test public void clickWithArgumentCalled() throws Exception {
    1.89 -        StringBuilder sb = new StringBuilder();
    1.90 -        sb.append(
    1.91 -              "var window = new Object();\n"
    1.92 -            + "var doc = new Object();\n"
    1.93 -            + "doc.button = new Object();\n"
    1.94 -            + "doc.title = new Object();\n"
    1.95 -            + "doc.title.innerHTML = 'nothing';\n"
    1.96 -            + "doc.text = new Object();\n"
    1.97 -            + "doc.text.value = 'something';\n"
    1.98 -            + "doc.getElementById = function(id) {\n"
    1.99 -            + "    switch(id) {\n"
   1.100 -            + "      case 'pg.button': return doc.button;\n"
   1.101 -            + "      case 'pg.title': return doc.title;\n"
   1.102 -            + "      case 'pg.text': return doc.text;\n"
   1.103 -            + "    }\n"
   1.104 -            + "    throw id;\n"
   1.105 -            + "  }\n"
   1.106 -            + "\n"
   1.107 -            + "function clickAndCheck() {\n"
   1.108 -            + "  doc.title.onclick();\n"
   1.109 -            + "  return doc.title.innerHTML.toString();\n"
   1.110 -            + "};\n"
   1.111 -            + "\n"
   1.112 -            + "window.document = doc;\n"
   1.113 -        );
   1.114 -        Invocable i = compileClass(sb, "org/apidesign/bck2brwsr/htmlpage/PageController");
   1.115 -
   1.116 -        Object ret = null;
   1.117 -        try {
   1.118 -            ret = i.invokeFunction("clickAndCheck");
   1.119 -        } catch (ScriptException ex) {
   1.120 -            fail("Execution failed in " + sb, ex);
   1.121 -        } catch (NoSuchMethodException ex) {
   1.122 -            fail("Cannot find method in " + sb, ex);
   1.123 -        }
   1.124 -        assertEquals(ret, "pg.title", "Title has been passed to the method argument");
   1.125 -    }
   1.126 -
   1.127 -    static Invocable compileClass(StringBuilder sb, String... names) throws ScriptException, IOException {
   1.128 -        if (sb == null) {
   1.129 -            sb = new StringBuilder();
   1.130 -        }
   1.131 -        try {
   1.132 -            Method m;
   1.133 -            Class<?> genJS = Class.forName("org.apidesign.vm4brwsr.GenJS");
   1.134 -            m = genJS.getDeclaredMethod("compile", Appendable.class, String[].class);
   1.135 -            m.setAccessible(true);
   1.136 -            m.invoke(null, sb, names);
   1.137 -        } catch (Exception exception) {
   1.138 -            throw new IOException(exception);
   1.139 -        }
   1.140 -        ScriptEngineManager sem = new ScriptEngineManager();
   1.141 -        ScriptEngine js = sem.getEngineByExtension("js");
   1.142 -        try {
   1.143 -            Object res = js.eval(sb.toString());
   1.144 -            assertTrue(js instanceof Invocable, "It is invocable object: " + res);
   1.145 -            return (Invocable) js;
   1.146 -        } catch (ScriptException ex) {
   1.147 -            fail("Could not compile:\n" + sb, ex);
   1.148 -            return null;
   1.149 -        }
   1.150 -    }
   1.151 -}