javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/ProcessPageTest.java
author Anton Epple <toni.epple@eppleton.de>
Thu, 23 May 2013 15:33:14 +0200
branchcanvas
changeset 1135 836bc1845c65
parent 834 b0b23e5ebf9d
child 1138 94bd7330ff58
permissions -rw-r--r--
moved some non-api classes out of api package
     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.File;
    21 import java.io.FileReader;
    22 import java.io.IOException;
    23 import java.io.InputStream;
    24 import java.io.PrintWriter;
    25 import java.util.Set;
    26 import javax.script.Invocable;
    27 import javax.script.ScriptEngine;
    28 import javax.script.ScriptEngineManager;
    29 import javax.script.ScriptException;
    30 import org.apidesign.vm4brwsr.Bck2Brwsr;
    31 import org.testng.annotations.Test;
    32 import static org.testng.Assert.*;
    33 
    34 public class ProcessPageTest {
    35     
    36     
    37     @Test public void findsThreeIds() throws IOException {
    38         InputStream is = ProcessPageTest.class.getResourceAsStream("TestPage.html");
    39         assertNotNull(is, "Sample HTML page found");
    40         ProcessPage res = ProcessPage.readPage(is);
    41         final Set<String> ids = res.ids();
    42         assertEquals(ids.size(), 4, "Four ids found: " + ids);
    43         
    44         assertEquals(res.tagNameForId("pg.title"), "title");
    45         assertEquals(res.tagNameForId("pg.button"), "button");
    46         assertEquals(res.tagNameForId("pg.text"), "input");
    47         assertEquals(res.tagNameForId("pg.canvas"), "canvas");
    48     }
    49     
    50     @Test public void testCompileAndRunPageController() throws Exception {
    51         StringBuilder sb = new StringBuilder();
    52         sb.append(
    53               "var window = new Object();\n"
    54             + "var doc = new Object();\n"
    55             + "doc.button = new Object();\n"
    56             + "doc.title = new Object();\n"
    57             + "doc.title.innerHTML = 'nothing';\n"
    58             + "doc.text = new Object();\n"
    59             + "doc.text.value = 'something';\n"
    60             + "doc.canvas = new Object();\n"
    61             + "doc.getElementById = function(id) {\n"
    62             + "    switch(id) {\n"
    63             + "      case 'pg.button': return doc.button;\n"
    64             + "      case 'pg.title': return doc.title;\n"
    65             + "      case 'pg.text': return doc.text;\n"
    66             + "      case 'pg.canvas': return doc.canvas;\n"
    67             + "    }\n"
    68             + "    throw id;\n"
    69             + "  }\n"
    70             + "\n"
    71             + "function clickAndCheck() {\n"
    72             + "  doc.button.onclick();\n"
    73             + "  return doc.title.innerHTML.toString();\n"
    74             + "};\n"
    75             + "\n"
    76             + "window.document = doc;\n"
    77         );
    78         Invocable i = compileClass(sb, "org/apidesign/bck2brwsr/htmlpage/PageController");
    79 
    80         Object ret = null;
    81         try {
    82             ret = i.invokeFunction("clickAndCheck");
    83         } catch (ScriptException ex) {
    84             fail("Execution failed in " + sb, ex);
    85         } catch (NoSuchMethodException ex) {
    86             fail("Cannot find method in " + sb, ex);
    87         }
    88         assertEquals(ret, "You want this window to be named something", "We expect that the JavaCode performs all the wiring");
    89     }
    90     
    91 //    @Test public void clickWithArgumentCalled() throws Exception {
    92 //        StringBuilder sb = new StringBuilder();
    93 //        sb.append(
    94 //              "var window = new Object();\n"
    95 //            + "var doc = new Object();\n"
    96 //            + "doc.button = new Object();\n"
    97 //            + "doc.title = new Object();\n"
    98 //            + "doc.title.innerHTML = 'nothing';\n"
    99 //            + "doc.text = new Object();\n"
   100 //            + "doc.text.value = 'something';\n"
   101 //            + "doc.canvas = new Object();\n"
   102 //            + "doc.getElementById = function(id) {\n"
   103 //            + "    switch(id) {\n"
   104 //            + "      case 'pg.button': return doc.button;\n"
   105 //            + "      case 'pg.title': return doc.title;\n"
   106 //            + "      case 'pg.text': return doc.text;\n"
   107 //            + "      case 'pg.canvas': return doc.canvas;\n"
   108 //            + "    }\n"
   109 //            + "    throw id;\n"
   110 //            + "  }\n"
   111 //            + "\n"
   112 //            + "function clickAndCheck() {\n"
   113 //            + "  doc.title.onclick();\n"
   114 //            + "  return doc.title.innerHTML.toString();\n"
   115 //            + "};\n"
   116 //            + "\n"
   117 //            + "window.document = doc;\n"
   118 //        );
   119 //        Invocable i = compileClass(sb, 
   120 //            "org/apidesign/bck2brwsr/htmlpage/PageController"
   121 //        );
   122 //
   123 //        Object ret = null;
   124 //        try {
   125 //            ret = i.invokeFunction("clickAndCheck");
   126 //        } catch (ScriptException ex) {
   127 //            fail("Execution failed in " + sb, ex);
   128 //        } catch (NoSuchMethodException ex) {
   129 //            fail("Cannot find method in " + sb, ex);
   130 //        }
   131 //        assertEquals(ret, "pg.title", "Title has been passed to the method argument");
   132 //    }
   133 
   134 //    @Test public void clickWithArgumentAndParameterCalled() throws Exception {
   135 //        StringBuilder sb = new StringBuilder();
   136 //        sb.append(
   137 //              "var window = new Object();\n"
   138 //            + "var doc = new Object();\n"
   139 //            + "var eventObject = new Object();\n"
   140 //            + "eventObject.layerX = 100;\n"
   141 //            + "doc.button = new Object();\n"
   142 //            + "doc.title = new Object();\n"
   143 //            + "doc.title.innerHTML = 'nothing';\n"
   144 //            + "doc.text = new Object();\n"
   145 //            + "doc.text.value = 'something';\n"
   146 //            + "doc.canvas = new Object();\n"
   147 //            + "doc.canvas.width = 200;\n"                
   148 //            + "doc.getElementById = function(id) {\n"
   149 //            + "    switch(id) {\n"
   150 //            + "      case 'pg.button': return doc.button;\n"
   151 //            + "      case 'pg.title': return doc.title;\n"
   152 //            + "      case 'pg.text': return doc.text;\n"
   153 //            + "      case 'pg.canvas': return doc.canvas;\n"
   154 //            + "    }\n"
   155 //            + "    throw id;\n"
   156 //            + "  }\n"
   157 //            + "\n"
   158 //            + "function clickAndCheck() {\n"
   159 //            + "  doc.canvas.onclick(eventObject);\n"
   160 //            + "  return doc.canvas.width.toString();\n"
   161 //            + "};\n"
   162 //            + "\n"
   163 //            + "window.document = doc;\n"
   164 //        );
   165 //        Invocable i = compileClass(sb, 
   166 //            "org/apidesign/bck2brwsr/htmlpage/PageController"
   167 //        );
   168 //
   169 //        Object ret = null;
   170 //        try {
   171 //            ret = i.invokeFunction("clickAndCheck");
   172 //        } catch (ScriptException ex) {
   173 //            fail("Execution failed in " + sb, ex);
   174 //        } catch (NoSuchMethodException ex) {
   175 //            fail("Cannot find method in " + sb, ex);
   176 //        }
   177 //       assertEquals(ret, "100", "layerX has been passed to the method argument");
   178 //    }
   179 //    
   180     static Invocable compileClass(StringBuilder sb, String... names) throws ScriptException, IOException {
   181         
   182         if (sb == null) {
   183             sb = new StringBuilder();
   184         }
   185         Bck2Brwsr.generate(sb, ProcessPageTest.class.getClassLoader(), names);
   186         sb.append("var vm = this.bck2brwsr();\n");
   187         for (String c : names) {
   188             sb.append("vm.loadClass('").append(c.replace('/', '.')).append("');\n");
   189         }
   190         
   191         ScriptEngineManager sem = new ScriptEngineManager();
   192         ScriptEngine js = sem.getEngineByExtension("js");
   193         try {
   194             Object res = js.eval(sb.toString());
   195             assertTrue(js instanceof Invocable, "It is invocable object: " + res);
   196             return (Invocable) js;
   197         } catch (ScriptException ex) {
   198 //                System.err.println("ex "+ex+ " col "+ex.getColumnNumber());
   199 //            PrintWriter out = new PrintWriter("filename.txt");
   200 //            out.println(sb.toString());
   201 //            out.flush();
   202 //            out.close();
   203             fail("Could not compile:\n" , ex);
   204             return null;
   205         }
   206     }
   207 }