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
jaroslav@26
     1
/**
jaroslav@106
     2
 * Back 2 Browser Bytecode Translator
jaroslav@106
     3
 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@26
     4
 *
jaroslav@26
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@26
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@26
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@26
     8
 *
jaroslav@26
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@26
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@26
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@26
    12
 * GNU General Public License for more details.
jaroslav@26
    13
 *
jaroslav@26
    14
 * You should have received a copy of the GNU General Public License
jaroslav@26
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@26
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@26
    17
 */
jaroslav@25
    18
package org.apidesign.bck2brwsr.htmlpage;
jaroslav@25
    19
toni@1135
    20
import java.io.File;
toni@1135
    21
import java.io.FileReader;
jaroslav@25
    22
import java.io.IOException;
jaroslav@25
    23
import java.io.InputStream;
toni@1135
    24
import java.io.PrintWriter;
jaroslav@25
    25
import java.util.Set;
jaroslav@100
    26
import javax.script.Invocable;
jaroslav@100
    27
import javax.script.ScriptEngine;
jaroslav@100
    28
import javax.script.ScriptEngineManager;
jaroslav@100
    29
import javax.script.ScriptException;
jaroslav@304
    30
import org.apidesign.vm4brwsr.Bck2Brwsr;
jaroslav@25
    31
import org.testng.annotations.Test;
jaroslav@25
    32
import static org.testng.Assert.*;
jaroslav@25
    33
jaroslav@25
    34
public class ProcessPageTest {
jaroslav@25
    35
    
jaroslav@25
    36
    
jaroslav@25
    37
    @Test public void findsThreeIds() throws IOException {
jaroslav@100
    38
        InputStream is = ProcessPageTest.class.getResourceAsStream("TestPage.html");
jaroslav@25
    39
        assertNotNull(is, "Sample HTML page found");
jaroslav@25
    40
        ProcessPage res = ProcessPage.readPage(is);
jaroslav@25
    41
        final Set<String> ids = res.ids();
jaroslav@813
    42
        assertEquals(ids.size(), 4, "Four ids found: " + ids);
jaroslav@25
    43
        
jaroslav@25
    44
        assertEquals(res.tagNameForId("pg.title"), "title");
jaroslav@25
    45
        assertEquals(res.tagNameForId("pg.button"), "button");
jaroslav@25
    46
        assertEquals(res.tagNameForId("pg.text"), "input");
jaroslav@813
    47
        assertEquals(res.tagNameForId("pg.canvas"), "canvas");
jaroslav@25
    48
    }
jaroslav@26
    49
    
jaroslav@100
    50
    @Test public void testCompileAndRunPageController() throws Exception {
jaroslav@100
    51
        StringBuilder sb = new StringBuilder();
jaroslav@100
    52
        sb.append(
jaroslav@100
    53
              "var window = new Object();\n"
jaroslav@100
    54
            + "var doc = new Object();\n"
jaroslav@100
    55
            + "doc.button = new Object();\n"
jaroslav@100
    56
            + "doc.title = new Object();\n"
jaroslav@100
    57
            + "doc.title.innerHTML = 'nothing';\n"
jaroslav@100
    58
            + "doc.text = new Object();\n"
jaroslav@100
    59
            + "doc.text.value = 'something';\n"
jaroslav@813
    60
            + "doc.canvas = new Object();\n"
jaroslav@100
    61
            + "doc.getElementById = function(id) {\n"
jaroslav@100
    62
            + "    switch(id) {\n"
jaroslav@100
    63
            + "      case 'pg.button': return doc.button;\n"
jaroslav@100
    64
            + "      case 'pg.title': return doc.title;\n"
jaroslav@100
    65
            + "      case 'pg.text': return doc.text;\n"
jaroslav@813
    66
            + "      case 'pg.canvas': return doc.canvas;\n"
jaroslav@100
    67
            + "    }\n"
jaroslav@100
    68
            + "    throw id;\n"
jaroslav@100
    69
            + "  }\n"
jaroslav@100
    70
            + "\n"
jaroslav@100
    71
            + "function clickAndCheck() {\n"
jaroslav@100
    72
            + "  doc.button.onclick();\n"
jaroslav@100
    73
            + "  return doc.title.innerHTML.toString();\n"
jaroslav@100
    74
            + "};\n"
jaroslav@100
    75
            + "\n"
jaroslav@100
    76
            + "window.document = doc;\n"
jaroslav@100
    77
        );
jaroslav@100
    78
        Invocable i = compileClass(sb, "org/apidesign/bck2brwsr/htmlpage/PageController");
jaroslav@100
    79
jaroslav@100
    80
        Object ret = null;
jaroslav@100
    81
        try {
jaroslav@100
    82
            ret = i.invokeFunction("clickAndCheck");
jaroslav@100
    83
        } catch (ScriptException ex) {
jaroslav@100
    84
            fail("Execution failed in " + sb, ex);
jaroslav@100
    85
        } catch (NoSuchMethodException ex) {
jaroslav@100
    86
            fail("Cannot find method in " + sb, ex);
jaroslav@100
    87
        }
jaroslav@104
    88
        assertEquals(ret, "You want this window to be named something", "We expect that the JavaCode performs all the wiring");
jaroslav@26
    89
    }
jaroslav@124
    90
    
toni@1135
    91
//    @Test public void clickWithArgumentCalled() throws Exception {
toni@1135
    92
//        StringBuilder sb = new StringBuilder();
toni@1135
    93
//        sb.append(
toni@1135
    94
//              "var window = new Object();\n"
toni@1135
    95
//            + "var doc = new Object();\n"
toni@1135
    96
//            + "doc.button = new Object();\n"
toni@1135
    97
//            + "doc.title = new Object();\n"
toni@1135
    98
//            + "doc.title.innerHTML = 'nothing';\n"
toni@1135
    99
//            + "doc.text = new Object();\n"
toni@1135
   100
//            + "doc.text.value = 'something';\n"
toni@1135
   101
//            + "doc.canvas = new Object();\n"
toni@1135
   102
//            + "doc.getElementById = function(id) {\n"
toni@1135
   103
//            + "    switch(id) {\n"
toni@1135
   104
//            + "      case 'pg.button': return doc.button;\n"
toni@1135
   105
//            + "      case 'pg.title': return doc.title;\n"
toni@1135
   106
//            + "      case 'pg.text': return doc.text;\n"
toni@1135
   107
//            + "      case 'pg.canvas': return doc.canvas;\n"
toni@1135
   108
//            + "    }\n"
toni@1135
   109
//            + "    throw id;\n"
toni@1135
   110
//            + "  }\n"
toni@1135
   111
//            + "\n"
toni@1135
   112
//            + "function clickAndCheck() {\n"
toni@1135
   113
//            + "  doc.title.onclick();\n"
toni@1135
   114
//            + "  return doc.title.innerHTML.toString();\n"
toni@1135
   115
//            + "};\n"
toni@1135
   116
//            + "\n"
toni@1135
   117
//            + "window.document = doc;\n"
toni@1135
   118
//        );
toni@1135
   119
//        Invocable i = compileClass(sb, 
toni@1135
   120
//            "org/apidesign/bck2brwsr/htmlpage/PageController"
toni@1135
   121
//        );
toni@1135
   122
//
toni@1135
   123
//        Object ret = null;
toni@1135
   124
//        try {
toni@1135
   125
//            ret = i.invokeFunction("clickAndCheck");
toni@1135
   126
//        } catch (ScriptException ex) {
toni@1135
   127
//            fail("Execution failed in " + sb, ex);
toni@1135
   128
//        } catch (NoSuchMethodException ex) {
toni@1135
   129
//            fail("Cannot find method in " + sb, ex);
toni@1135
   130
//        }
toni@1135
   131
//        assertEquals(ret, "pg.title", "Title has been passed to the method argument");
toni@1135
   132
//    }
jaroslav@124
   133
toni@1135
   134
//    @Test public void clickWithArgumentAndParameterCalled() throws Exception {
toni@1135
   135
//        StringBuilder sb = new StringBuilder();
toni@1135
   136
//        sb.append(
toni@1135
   137
//              "var window = new Object();\n"
toni@1135
   138
//            + "var doc = new Object();\n"
toni@1135
   139
//            + "var eventObject = new Object();\n"
toni@1135
   140
//            + "eventObject.layerX = 100;\n"
toni@1135
   141
//            + "doc.button = new Object();\n"
toni@1135
   142
//            + "doc.title = new Object();\n"
toni@1135
   143
//            + "doc.title.innerHTML = 'nothing';\n"
toni@1135
   144
//            + "doc.text = new Object();\n"
toni@1135
   145
//            + "doc.text.value = 'something';\n"
toni@1135
   146
//            + "doc.canvas = new Object();\n"
toni@1135
   147
//            + "doc.canvas.width = 200;\n"                
toni@1135
   148
//            + "doc.getElementById = function(id) {\n"
toni@1135
   149
//            + "    switch(id) {\n"
toni@1135
   150
//            + "      case 'pg.button': return doc.button;\n"
toni@1135
   151
//            + "      case 'pg.title': return doc.title;\n"
toni@1135
   152
//            + "      case 'pg.text': return doc.text;\n"
toni@1135
   153
//            + "      case 'pg.canvas': return doc.canvas;\n"
toni@1135
   154
//            + "    }\n"
toni@1135
   155
//            + "    throw id;\n"
toni@1135
   156
//            + "  }\n"
toni@1135
   157
//            + "\n"
toni@1135
   158
//            + "function clickAndCheck() {\n"
toni@1135
   159
//            + "  doc.canvas.onclick(eventObject);\n"
toni@1135
   160
//            + "  return doc.canvas.width.toString();\n"
toni@1135
   161
//            + "};\n"
toni@1135
   162
//            + "\n"
toni@1135
   163
//            + "window.document = doc;\n"
toni@1135
   164
//        );
toni@1135
   165
//        Invocable i = compileClass(sb, 
toni@1135
   166
//            "org/apidesign/bck2brwsr/htmlpage/PageController"
toni@1135
   167
//        );
toni@1135
   168
//
toni@1135
   169
//        Object ret = null;
toni@1135
   170
//        try {
toni@1135
   171
//            ret = i.invokeFunction("clickAndCheck");
toni@1135
   172
//        } catch (ScriptException ex) {
toni@1135
   173
//            fail("Execution failed in " + sb, ex);
toni@1135
   174
//        } catch (NoSuchMethodException ex) {
toni@1135
   175
//            fail("Cannot find method in " + sb, ex);
toni@1135
   176
//        }
toni@1135
   177
//       assertEquals(ret, "100", "layerX has been passed to the method argument");
toni@1135
   178
//    }
toni@1135
   179
//    
jaroslav@100
   180
    static Invocable compileClass(StringBuilder sb, String... names) throws ScriptException, IOException {
toni@1135
   181
        
jaroslav@100
   182
        if (sb == null) {
jaroslav@100
   183
            sb = new StringBuilder();
jaroslav@100
   184
        }
jaroslav@304
   185
        Bck2Brwsr.generate(sb, ProcessPageTest.class.getClassLoader(), names);
jaroslav@711
   186
        sb.append("var vm = this.bck2brwsr();\n");
jaroslav@834
   187
        for (String c : names) {
jaroslav@834
   188
            sb.append("vm.loadClass('").append(c.replace('/', '.')).append("');\n");
jaroslav@834
   189
        }
jaroslav@711
   190
        
jaroslav@100
   191
        ScriptEngineManager sem = new ScriptEngineManager();
jaroslav@100
   192
        ScriptEngine js = sem.getEngineByExtension("js");
jaroslav@100
   193
        try {
jaroslav@100
   194
            Object res = js.eval(sb.toString());
jaroslav@100
   195
            assertTrue(js instanceof Invocable, "It is invocable object: " + res);
jaroslav@100
   196
            return (Invocable) js;
jaroslav@100
   197
        } catch (ScriptException ex) {
toni@1135
   198
//                System.err.println("ex "+ex+ " col "+ex.getColumnNumber());
toni@1135
   199
//            PrintWriter out = new PrintWriter("filename.txt");
toni@1135
   200
//            out.println(sb.toString());
toni@1135
   201
//            out.flush();
toni@1135
   202
//            out.close();
toni@1135
   203
            fail("Could not compile:\n" , ex);
jaroslav@100
   204
            return null;
jaroslav@100
   205
        }
jaroslav@28
   206
    }
jaroslav@25
   207
}