javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/ProcessPageTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 20 Jan 2013 18:20:18 +0100
branchmodel
changeset 492 854286e49061
parent 304 3090a35d970f
child 711 333326d65bf9
permissions -rw-r--r--
Basic support for knockout.js. It can read and write model properties.
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     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.
     8  *
     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.
    13  *
    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.
    17  */
    18 package org.apidesign.bck2brwsr.htmlpage;
    19 
    20 import java.io.IOException;
    21 import java.io.InputStream;
    22 import java.util.Set;
    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.*;
    30 
    31 public class ProcessPageTest {
    32     
    33     
    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);
    40         
    41         assertEquals(res.tagNameForId("pg.title"), "title");
    42         assertEquals(res.tagNameForId("pg.button"), "button");
    43         assertEquals(res.tagNameForId("pg.text"), "input");
    44     }
    45     
    46     @Test public void testCompileAndRunPageController() throws Exception {
    47         StringBuilder sb = new StringBuilder();
    48         sb.append(
    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"
    57             + "    switch(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"
    61             + "    }\n"
    62             + "    throw id;\n"
    63             + "  }\n"
    64             + "\n"
    65             + "function clickAndCheck() {\n"
    66             + "  doc.button.onclick();\n"
    67             + "  return doc.title.innerHTML.toString();\n"
    68             + "};\n"
    69             + "\n"
    70             + "window.document = doc;\n"
    71         );
    72         Invocable i = compileClass(sb, "org/apidesign/bck2brwsr/htmlpage/PageController");
    73 
    74         Object ret = null;
    75         try {
    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);
    81         }
    82         assertEquals(ret, "You want this window to be named something", "We expect that the JavaCode performs all the wiring");
    83     }
    84     
    85     @Test public void clickWithArgumentCalled() throws Exception {
    86         StringBuilder sb = new StringBuilder();
    87         sb.append(
    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"
    96             + "    switch(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"
   100             + "    }\n"
   101             + "    throw id;\n"
   102             + "  }\n"
   103             + "\n"
   104             + "function clickAndCheck() {\n"
   105             + "  doc.title.onclick();\n"
   106             + "  return doc.title.innerHTML.toString();\n"
   107             + "};\n"
   108             + "\n"
   109             + "window.document = doc;\n"
   110         );
   111         Invocable i = compileClass(sb, 
   112             "org/apidesign/bck2brwsr/htmlpage/PageController"
   113         );
   114 
   115         Object ret = null;
   116         try {
   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);
   122         }
   123         assertEquals(ret, "pg.title", "Title has been passed to the method argument");
   124     }
   125 
   126     static Invocable compileClass(StringBuilder sb, String... names) throws ScriptException, IOException {
   127         if (sb == null) {
   128             sb = new StringBuilder();
   129         }
   130         Bck2Brwsr.generate(sb, ProcessPageTest.class.getClassLoader(), names);
   131         ScriptEngineManager sem = new ScriptEngineManager();
   132         ScriptEngine js = sem.getEngineByExtension("js");
   133         try {
   134             Object res = js.eval(sb.toString());
   135             assertTrue(js instanceof Invocable, "It is invocable object: " + res);
   136             return (Invocable) js;
   137         } catch (ScriptException ex) {
   138             fail("Could not compile:\n" + sb, ex);
   139             return null;
   140         }
   141     }
   142 }