rt/emul/compacttest/src/test/java/org/apidesign/bck2brwsr/vmtest/impl/HtmlAnnotationsTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 19 Nov 2014 19:32:00 +0100
changeset 1723 3a1f262311cf
parent 1722 rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/vmtest/impl/HtmlAnnotationsTest.java@fd3a354d6e8f
child 1732 5ab1cb07a530
permissions -rw-r--r--
Separating compact profile tests into own project, so launcher.http can depend on compact profile JAR
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@1722
    34
            Object mul = windowMul();
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@1282
    73
    private static void assertEquals(double real, double exp) {
jaroslav@1282
    74
        if (real - exp < 0.01) {
jaroslav@1282
    75
            return;
jaroslav@1282
    76
        }
jaroslav@1282
    77
        assert false : "Expecting " + exp + " but was " + real;
jaroslav@1282
    78
    }
jaroslav@1282
    79
jaroslav@1282
    80
    private static void assertNotNull(Object obj, String msg) {
jaroslav@1282
    81
        assert obj != null : msg;
jaroslav@1282
    82
    }
jaroslav@1282
    83
    
jaroslav@1722
    84
    @JavaScriptBody(args = {}, body = "return window.mul ? window.mul : null;")
jaroslav@1722
    85
    private static native Object windowMul();
jaroslav@1722
    86
    
jaroslav@1282
    87
    @Factory public static Object[] create() {
jaroslav@1282
    88
        return VMTest.create(HtmlAnnotationsTest.class);
jaroslav@1282
    89
    }
jaroslav@1282
    90
}