rt/emul/compacttest/src/test/java/org/apidesign/bck2brwsr/vmtest/impl/HtmlAnnotationsTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 30 Nov 2014 16:21:27 +0100
changeset 1732 5ab1cb07a530
parent 1723 3a1f262311cf
child 1787 ea12a3bb4b33
permissions -rw-r--r--
Better to not do any processign as '...*/*...' string can break our simple check. Keep comments in and let obfuscator remove them
jaroslav@1282
     1
/**
jaroslav@1282
     2
 * Back 2 Browser Bytecode Translator
jaroslav@1282
     3
 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@1282
     4
 *
jaroslav@1282
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@1282
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@1282
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@1282
     8
 *
jaroslav@1282
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@1282
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@1282
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@1282
    12
 * GNU General Public License for more details.
jaroslav@1282
    13
 *
jaroslav@1282
    14
 * You should have received a copy of the GNU General Public License
jaroslav@1282
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@1282
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@1282
    17
 */
jaroslav@1282
    18
package org.apidesign.bck2brwsr.vmtest.impl;
jaroslav@1282
    19
jaroslav@1722
    20
import org.apidesign.bck2brwsr.core.JavaScriptBody;
jaroslav@1282
    21
import org.apidesign.bck2brwsr.vmtest.BrwsrTest;
jaroslav@1282
    22
import org.apidesign.bck2brwsr.vmtest.VMTest;
jaroslav@1282
    23
import org.testng.annotations.Factory;
jaroslav@1282
    24
jaroslav@1282
    25
/** Verify cooperation with net.java.html.js annotations.
jaroslav@1282
    26
 *
jaroslav@1282
    27
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@1282
    28
 */
jaroslav@1282
    29
public class HtmlAnnotationsTest {
jaroslav@1722
    30
    static int firstCheck;
jaroslav@1722
    31
    
jaroslav@1722
    32
    private void assertMulNotDefinedForTheFirstTime() {
jaroslav@1722
    33
        if (firstCheck++ == 0) {
jaroslav@1732
    34
            Object mul = window("mul");
jaroslav@1722
    35
            assert mul == null : "htmlannotations.js should not be processed before first call to HtmlAnnotations class";
jaroslav@1722
    36
        }
jaroslav@1722
    37
    }
jaroslav@1722
    38
    
jaroslav@1282
    39
    @BrwsrTest public void fourtyTwo() throws Exception {
jaroslav@1722
    40
        assertMulNotDefinedForTheFirstTime();
jaroslav@1282
    41
        assertEquals(HtmlAnnotations.fourtyTwo(), 42);
jaroslav@1282
    42
    }
jaroslav@1282
    43
    
jaroslav@1282
    44
    @BrwsrTest public void externalMul() throws Exception {
jaroslav@1722
    45
        assertMulNotDefinedForTheFirstTime();
jaroslav@1282
    46
        assertEquals(HtmlAnnotations.useExternalMul(7, 6), 42);
jaroslav@1282
    47
    }
jaroslav@1282
    48
jaroslav@1282
    49
    @BrwsrTest public void callRunnableFromJS() throws Exception {
jaroslav@1722
    50
        assertMulNotDefinedForTheFirstTime();
jaroslav@1282
    51
        assertEquals(HtmlAnnotations.callback(), 1);
jaroslav@1282
    52
    }
jaroslav@1282
    53
jaroslav@1282
    54
    @BrwsrTest public void callStaticMethodFromJS() throws Exception {
jaroslav@1722
    55
        assertMulNotDefinedForTheFirstTime();
jaroslav@1282
    56
        assertEquals(HtmlAnnotations.staticCallback(), 1);
jaroslav@1282
    57
    }
jaroslav@1282
    58
jaroslav@1282
    59
    @BrwsrTest public void callbackWithFourParamsAndReturnType() throws Exception {
jaroslav@1722
    60
        assertMulNotDefinedForTheFirstTime();
jaroslav@1282
    61
        Object instance = HtmlAnnotations.create();
jaroslav@1282
    62
        assertNotNull(instance, "Instance created");
jaroslav@1282
    63
        assertEquals(HtmlAnnotations.first(instance, 42, 31), 42);
jaroslav@1282
    64
    }
jaroslav@1282
    65
jaroslav@1282
    66
    @BrwsrTest public void callbackWithObjectParamsAndReturnType() throws Exception {
jaroslav@1722
    67
        assertMulNotDefinedForTheFirstTime();
jaroslav@1282
    68
        Object instance = HtmlAnnotations.create();
jaroslav@1282
    69
        assertNotNull(instance, "Instance created");
jaroslav@1282
    70
        assertEquals(HtmlAnnotations.onError(instance, 42.0), 42.0);
jaroslav@1282
    71
    }
jaroslav@1282
    72
    
jaroslav@1732
    73
    @BrwsrTest public void quotedStar() throws Exception {
jaroslav@1732
    74
        assertMulNotDefinedForTheFirstTime();
jaroslav@1732
    75
        HtmlAnnotations.empty();
jaroslav@1732
    76
        Object fn = window("all");
jaroslav@1732
    77
        String msg = invoke(fn);
jaroslav@1732
    78
        assert "*/*".equals(msg) : "String '*/*' as expected: " + msg;
jaroslav@1732
    79
    }
jaroslav@1732
    80
    
jaroslav@1282
    81
    private static void assertEquals(double real, double exp) {
jaroslav@1282
    82
        if (real - exp < 0.01) {
jaroslav@1282
    83
            return;
jaroslav@1282
    84
        }
jaroslav@1282
    85
        assert false : "Expecting " + exp + " but was " + real;
jaroslav@1282
    86
    }
jaroslav@1282
    87
jaroslav@1282
    88
    private static void assertNotNull(Object obj, String msg) {
jaroslav@1282
    89
        assert obj != null : msg;
jaroslav@1282
    90
    }
jaroslav@1282
    91
    
jaroslav@1732
    92
    @JavaScriptBody(args = { "n" }, body = "return window[n] ? window[n] : null;")
jaroslav@1732
    93
    private static native Object window(String name);
jaroslav@1732
    94
    @JavaScriptBody(args = { "fn" }, body = "return fn();")
jaroslav@1732
    95
    private static native String invoke(Object fn);
jaroslav@1722
    96
    
jaroslav@1282
    97
    @Factory public static Object[] create() {
jaroslav@1282
    98
        return VMTest.create(HtmlAnnotationsTest.class);
jaroslav@1282
    99
    }
jaroslav@1282
   100
}