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