javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/ProcessPageTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 11 Feb 2013 19:55:00 +0100
branchemul
changeset 711 333326d65bf9
parent 492 854286e49061
child 813 2fa85847ccf7
permissions -rw-r--r--
Allows two isolated bck2brwsr VM in a single JavaScript page/context
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
    
jaroslav@25
    34
    @Test public void findsThreeIds() 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@25
    39
        assertEquals(ids.size(), 3, "Three 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@25
    44
    }
jaroslav@26
    45
    
jaroslav@100
    46
    @Test public void testCompileAndRunPageController() throws Exception {
jaroslav@100
    47
        StringBuilder sb = new StringBuilder();
jaroslav@100
    48
        sb.append(
jaroslav@100
    49
              "var window = new Object();\n"
jaroslav@100
    50
            + "var doc = new Object();\n"
jaroslav@100
    51
            + "doc.button = new Object();\n"
jaroslav@100
    52
            + "doc.title = new Object();\n"
jaroslav@100
    53
            + "doc.title.innerHTML = 'nothing';\n"
jaroslav@100
    54
            + "doc.text = new Object();\n"
jaroslav@100
    55
            + "doc.text.value = 'something';\n"
jaroslav@100
    56
            + "doc.getElementById = function(id) {\n"
jaroslav@100
    57
            + "    switch(id) {\n"
jaroslav@100
    58
            + "      case 'pg.button': return doc.button;\n"
jaroslav@100
    59
            + "      case 'pg.title': return doc.title;\n"
jaroslav@100
    60
            + "      case 'pg.text': return doc.text;\n"
jaroslav@100
    61
            + "    }\n"
jaroslav@100
    62
            + "    throw id;\n"
jaroslav@100
    63
            + "  }\n"
jaroslav@100
    64
            + "\n"
jaroslav@100
    65
            + "function clickAndCheck() {\n"
jaroslav@100
    66
            + "  doc.button.onclick();\n"
jaroslav@100
    67
            + "  return doc.title.innerHTML.toString();\n"
jaroslav@100
    68
            + "};\n"
jaroslav@100
    69
            + "\n"
jaroslav@100
    70
            + "window.document = doc;\n"
jaroslav@100
    71
        );
jaroslav@100
    72
        Invocable i = compileClass(sb, "org/apidesign/bck2brwsr/htmlpage/PageController");
jaroslav@100
    73
jaroslav@100
    74
        Object ret = null;
jaroslav@100
    75
        try {
jaroslav@100
    76
            ret = i.invokeFunction("clickAndCheck");
jaroslav@100
    77
        } catch (ScriptException ex) {
jaroslav@100
    78
            fail("Execution failed in " + sb, ex);
jaroslav@100
    79
        } catch (NoSuchMethodException ex) {
jaroslav@100
    80
            fail("Cannot find method in " + sb, ex);
jaroslav@100
    81
        }
jaroslav@104
    82
        assertEquals(ret, "You want this window to be named something", "We expect that the JavaCode performs all the wiring");
jaroslav@26
    83
    }
jaroslav@124
    84
    
jaroslav@124
    85
    @Test public void clickWithArgumentCalled() throws Exception {
jaroslav@124
    86
        StringBuilder sb = new StringBuilder();
jaroslav@124
    87
        sb.append(
jaroslav@124
    88
              "var window = new Object();\n"
jaroslav@124
    89
            + "var doc = new Object();\n"
jaroslav@124
    90
            + "doc.button = new Object();\n"
jaroslav@124
    91
            + "doc.title = new Object();\n"
jaroslav@124
    92
            + "doc.title.innerHTML = 'nothing';\n"
jaroslav@124
    93
            + "doc.text = new Object();\n"
jaroslav@124
    94
            + "doc.text.value = 'something';\n"
jaroslav@124
    95
            + "doc.getElementById = function(id) {\n"
jaroslav@124
    96
            + "    switch(id) {\n"
jaroslav@124
    97
            + "      case 'pg.button': return doc.button;\n"
jaroslav@124
    98
            + "      case 'pg.title': return doc.title;\n"
jaroslav@124
    99
            + "      case 'pg.text': return doc.text;\n"
jaroslav@124
   100
            + "    }\n"
jaroslav@124
   101
            + "    throw id;\n"
jaroslav@124
   102
            + "  }\n"
jaroslav@124
   103
            + "\n"
jaroslav@124
   104
            + "function clickAndCheck() {\n"
jaroslav@124
   105
            + "  doc.title.onclick();\n"
jaroslav@124
   106
            + "  return doc.title.innerHTML.toString();\n"
jaroslav@124
   107
            + "};\n"
jaroslav@124
   108
            + "\n"
jaroslav@124
   109
            + "window.document = doc;\n"
jaroslav@124
   110
        );
jaroslav@492
   111
        Invocable i = compileClass(sb, 
jaroslav@492
   112
            "org/apidesign/bck2brwsr/htmlpage/PageController"
jaroslav@492
   113
        );
jaroslav@124
   114
jaroslav@124
   115
        Object ret = null;
jaroslav@124
   116
        try {
jaroslav@124
   117
            ret = i.invokeFunction("clickAndCheck");
jaroslav@124
   118
        } catch (ScriptException ex) {
jaroslav@124
   119
            fail("Execution failed in " + sb, ex);
jaroslav@124
   120
        } catch (NoSuchMethodException ex) {
jaroslav@124
   121
            fail("Cannot find method in " + sb, ex);
jaroslav@124
   122
        }
jaroslav@124
   123
        assertEquals(ret, "pg.title", "Title has been passed to the method argument");
jaroslav@124
   124
    }
jaroslav@100
   125
jaroslav@100
   126
    static Invocable compileClass(StringBuilder sb, String... names) throws ScriptException, IOException {
jaroslav@100
   127
        if (sb == null) {
jaroslav@100
   128
            sb = new StringBuilder();
jaroslav@100
   129
        }
jaroslav@304
   130
        Bck2Brwsr.generate(sb, ProcessPageTest.class.getClassLoader(), names);
jaroslav@711
   131
        sb.append("var vm = this.bck2brwsr();\n");
jaroslav@711
   132
        
jaroslav@100
   133
        ScriptEngineManager sem = new ScriptEngineManager();
jaroslav@100
   134
        ScriptEngine js = sem.getEngineByExtension("js");
jaroslav@100
   135
        try {
jaroslav@100
   136
            Object res = js.eval(sb.toString());
jaroslav@100
   137
            assertTrue(js instanceof Invocable, "It is invocable object: " + res);
jaroslav@100
   138
            return (Invocable) js;
jaroslav@100
   139
        } catch (ScriptException ex) {
jaroslav@100
   140
            fail("Could not compile:\n" + sb, ex);
jaroslav@100
   141
            return null;
jaroslav@100
   142
        }
jaroslav@28
   143
    }
jaroslav@25
   144
}