htmlpage/src/test/java/org/apidesign/bck2brwsr/htmlpage/ProcessPageTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 16 Oct 2012 12:42:00 +0200
changeset 106 346633cd13d6
parent 104 1376481f15e7
child 124 a5f8cb32549e
permissions -rw-r--r--
Fixing license in all files
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@100
    22
import java.lang.reflect.Method;
jaroslav@25
    23
import java.util.Set;
jaroslav@100
    24
import javax.script.Invocable;
jaroslav@100
    25
import javax.script.ScriptEngine;
jaroslav@100
    26
import javax.script.ScriptEngineManager;
jaroslav@100
    27
import javax.script.ScriptException;
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@100
    84
jaroslav@100
    85
    static Invocable compileClass(StringBuilder sb, String... names) throws ScriptException, IOException {
jaroslav@100
    86
        if (sb == null) {
jaroslav@100
    87
            sb = new StringBuilder();
jaroslav@100
    88
        }
jaroslav@100
    89
        try {
jaroslav@100
    90
            Method m;
jaroslav@100
    91
            Class<?> genJS = Class.forName("org.apidesign.vm4brwsr.GenJS");
jaroslav@100
    92
            m = genJS.getDeclaredMethod("compile", Appendable.class, String[].class);
jaroslav@100
    93
            m.setAccessible(true);
jaroslav@100
    94
            m.invoke(null, sb, names);
jaroslav@100
    95
        } catch (Exception exception) {
jaroslav@100
    96
            throw new IOException(exception);
jaroslav@100
    97
        }
jaroslav@100
    98
        ScriptEngineManager sem = new ScriptEngineManager();
jaroslav@100
    99
        ScriptEngine js = sem.getEngineByExtension("js");
jaroslav@100
   100
        try {
jaroslav@100
   101
            Object res = js.eval(sb.toString());
jaroslav@100
   102
            assertTrue(js instanceof Invocable, "It is invocable object: " + res);
jaroslav@100
   103
            return (Invocable) js;
jaroslav@100
   104
        } catch (ScriptException ex) {
jaroslav@100
   105
            fail("Could not compile:\n" + sb, ex);
jaroslav@100
   106
            return null;
jaroslav@100
   107
        }
jaroslav@28
   108
    }
jaroslav@25
   109
}