htmlpage/src/test/java/org/apidesign/bck2brwsr/htmlpage/ProcessPageTest.java
branchemul
changeset 100 029e6eed60e9
parent 28 81ad7a739fed
child 104 1376481f15e7
     1.1 --- a/htmlpage/src/test/java/org/apidesign/bck2brwsr/htmlpage/ProcessPageTest.java	Tue Sep 25 09:55:34 2012 +0200
     1.2 +++ b/htmlpage/src/test/java/org/apidesign/bck2brwsr/htmlpage/ProcessPageTest.java	Thu Oct 11 06:15:22 2012 -0700
     1.3 @@ -19,17 +19,20 @@
     1.4  
     1.5  import java.io.IOException;
     1.6  import java.io.InputStream;
     1.7 +import java.lang.reflect.Method;
     1.8  import java.util.Set;
     1.9 +import javax.script.Invocable;
    1.10 +import javax.script.ScriptEngine;
    1.11 +import javax.script.ScriptEngineManager;
    1.12 +import javax.script.ScriptException;
    1.13  import org.testng.annotations.Test;
    1.14  import static org.testng.Assert.*;
    1.15  
    1.16 -import org.apidesign.bck2brwsr.htmlpage.api.*;
    1.17 -
    1.18  public class ProcessPageTest {
    1.19      
    1.20      
    1.21      @Test public void findsThreeIds() throws IOException {
    1.22 -        InputStream is = ProcessPageTest.class.getResourceAsStream("TestPage.xhtml");
    1.23 +        InputStream is = ProcessPageTest.class.getResourceAsStream("TestPage.html");
    1.24          assertNotNull(is, "Sample HTML page found");
    1.25          ProcessPage res = ProcessPage.readPage(is);
    1.26          final Set<String> ids = res.ids();
    1.27 @@ -40,16 +43,67 @@
    1.28          assertEquals(res.tagNameForId("pg.text"), "input");
    1.29      }
    1.30      
    1.31 -    void testWhetherWeCanCallTheGeneratedIdFields() {
    1.32 -        Title t = TestPage.PG_TITLE;
    1.33 +    @Test public void testCompileAndRunPageController() throws Exception {
    1.34 +        StringBuilder sb = new StringBuilder();
    1.35 +        sb.append(
    1.36 +              "var window = new Object();\n"
    1.37 +            + "var doc = new Object();\n"
    1.38 +            + "doc.button = new Object();\n"
    1.39 +            + "doc.title = new Object();\n"
    1.40 +            + "doc.title.innerHTML = 'nothing';\n"
    1.41 +            + "doc.text = new Object();\n"
    1.42 +            + "doc.text.value = 'something';\n"
    1.43 +            + "doc.getElementById = function(id) {\n"
    1.44 +            + "    switch(id) {\n"
    1.45 +            + "      case 'pg.button': return doc.button;\n"
    1.46 +            + "      case 'pg.title': return doc.title;\n"
    1.47 +            + "      case 'pg.text': return doc.text;\n"
    1.48 +            + "    }\n"
    1.49 +            + "    throw id;\n"
    1.50 +            + "  }\n"
    1.51 +            + "\n"
    1.52 +            + "function clickAndCheck() {\n"
    1.53 +            + "  doc.button.onclick();\n"
    1.54 +            + "  return doc.title.innerHTML.toString();\n"
    1.55 +            + "};\n"
    1.56 +            + "\n"
    1.57 +            + "window.document = doc;\n"
    1.58 +        );
    1.59 +        Invocable i = compileClass(sb, "org/apidesign/bck2brwsr/htmlpage/PageController");
    1.60 +
    1.61 +        Object ret = null;
    1.62 +        try {
    1.63 +            ret = i.invokeFunction("clickAndCheck");
    1.64 +        } catch (ScriptException ex) {
    1.65 +            fail("Execution failed in " + sb, ex);
    1.66 +        } catch (NoSuchMethodException ex) {
    1.67 +            fail("Cannot find method in " + sb, ex);
    1.68 +        }
    1.69 +        assertEquals(ret, "something", "We expect that the JavaCode performs all the wiring");
    1.70      }
    1.71 -    
    1.72 -    @OnClick(id="pg.button")
    1.73 -    static void handleButtonClick() {
    1.74 -        
    1.75 -    }
    1.76 -    
    1.77 -    @Test public void testOnclickHandlerGenerated() {
    1.78 -        
    1.79 +
    1.80 +    static Invocable compileClass(StringBuilder sb, String... names) throws ScriptException, IOException {
    1.81 +        if (sb == null) {
    1.82 +            sb = new StringBuilder();
    1.83 +        }
    1.84 +        try {
    1.85 +            Method m;
    1.86 +            Class<?> genJS = Class.forName("org.apidesign.vm4brwsr.GenJS");
    1.87 +            m = genJS.getDeclaredMethod("compile", Appendable.class, String[].class);
    1.88 +            m.setAccessible(true);
    1.89 +            m.invoke(null, sb, names);
    1.90 +        } catch (Exception exception) {
    1.91 +            throw new IOException(exception);
    1.92 +        }
    1.93 +        ScriptEngineManager sem = new ScriptEngineManager();
    1.94 +        ScriptEngine js = sem.getEngineByExtension("js");
    1.95 +        try {
    1.96 +            Object res = js.eval(sb.toString());
    1.97 +            assertTrue(js instanceof Invocable, "It is invocable object: " + res);
    1.98 +            return (Invocable) js;
    1.99 +        } catch (ScriptException ex) {
   1.100 +            fail("Could not compile:\n" + sb, ex);
   1.101 +            return null;
   1.102 +        }
   1.103      }
   1.104  }