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