rt/emul/compacttest/src/test/java/org/apidesign/bck2brwsr/vmtest/impl/HtmlAnnotationsTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 17 Jan 2017 07:04:06 +0100
changeset 1985 cd1cc103a03c
parent 1958 aa2e9630b6d5
permissions -rw-r--r--
Implementation of ClassValue for bck2brwsr
     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 java.util.concurrent.Callable;
    21 import org.apidesign.bck2brwsr.core.JavaScriptBody;
    22 import org.apidesign.bck2brwsr.vmtest.BrwsrTest;
    23 import org.apidesign.bck2brwsr.vmtest.VMTest;
    24 import org.testng.annotations.Factory;
    25 
    26 /** Verify cooperation with net.java.html.js annotations.
    27  *
    28  * @author Jaroslav Tulach <jtulach@netbeans.org>
    29  */
    30 public class HtmlAnnotationsTest {
    31     static int firstCheck;
    32     
    33     private void assertMulNotDefinedForTheFirstTime() {
    34         if (firstCheck++ == 0) {
    35             Object mul = window("mul");
    36             assert mul == null : "htmlannotations.js should not be processed before first call to HtmlAnnotations class";
    37         }
    38     }
    39     
    40     @BrwsrTest public void fourtyTwo() throws Exception {
    41         assertMulNotDefinedForTheFirstTime();
    42         assertEquals(HtmlAnnotations.fourtyTwo(), 42);
    43     }
    44     
    45     @BrwsrTest public void externalMul() throws Exception {
    46         assertMulNotDefinedForTheFirstTime();
    47         assertEquals(HtmlAnnotations.useExternalMul(7, 6), 42);
    48     }
    49 
    50     @BrwsrTest public void callRunnableFromJS() throws Exception {
    51         assertMulNotDefinedForTheFirstTime();
    52         assertEquals(HtmlAnnotations.callback(), 1);
    53     }
    54 
    55     @BrwsrTest public void callStaticMethodFromJS() throws Exception {
    56         assertMulNotDefinedForTheFirstTime();
    57         assertEquals(HtmlAnnotations.staticCallback(), 1);
    58     }
    59 
    60     @BrwsrTest public void callbackWithFourParamsAndReturnType() throws Exception {
    61         assertMulNotDefinedForTheFirstTime();
    62         Object instance = HtmlAnnotations.create();
    63         assertNotNull(instance, "Instance created");
    64         assertEquals(HtmlAnnotations.first(instance, 42, 31), 42);
    65     }
    66 
    67     @BrwsrTest public void callbackWithObjectParamsAndReturnType() throws Exception {
    68         assertMulNotDefinedForTheFirstTime();
    69         Object instance = HtmlAnnotations.create();
    70         assertNotNull(instance, "Instance created");
    71         assertEquals(HtmlAnnotations.onError(instance, 42.0), 42.0);
    72     }
    73     
    74     @BrwsrTest public void quotedStar() throws Exception {
    75         assertMulNotDefinedForTheFirstTime();
    76         HtmlAnnotations.empty();
    77         Object fn = window("all");
    78         String msg = invoke(fn);
    79         assert "*/*".equals(msg) : "String '*/*' as expected: " + msg;
    80     }
    81 
    82     @BrwsrTest public void date() throws Exception {
    83         assertMulNotDefinedForTheFirstTime();
    84         Object april = HtmlAnnotations.april2016();
    85         assertEquals(HtmlAnnotations.year(april), 2016);
    86     }
    87 
    88     @BrwsrTest public void yes() throws Exception {
    89         assertMulNotDefinedForTheFirstTime();
    90         String yes = HtmlAnnotations.yesNo(new Callable<Boolean>() {
    91             @Override
    92             public Boolean call() throws Exception {
    93                 return Boolean.TRUE;
    94             }
    95         });
    96         assertEquals(yes, "yes", "TRUE is true");
    97     }
    98 
    99     @BrwsrTest public void no() throws Exception {
   100         assertMulNotDefinedForTheFirstTime();
   101         String no = HtmlAnnotations.yesNo(new Callable<Boolean>() {
   102             @Override
   103             public Boolean call() throws Exception {
   104                 return Boolean.FALSE;
   105             }
   106         });
   107         assertEquals(no, "no", "FALSE is false");
   108     }
   109 
   110     @BrwsrTest
   111     public void compareArrayOfDoubles() throws Exception {
   112         assertMulNotDefinedForTheFirstTime();
   113         Double val = 2.2;
   114         boolean res = HtmlAnnotations.compareArr(new Object[] { val }, val);
   115         assertEquals(res, true, "Should be in the array");
   116     }
   117     
   118     private static void assertEquals(double real, double exp) {
   119         if (real - exp < 0.01) {
   120             return;
   121         }
   122         assert false : "Expecting " + exp + " but was " + real;
   123     }
   124 
   125     private static void assertEquals(Object real, Object exp, String msg) {
   126         if (real == exp) {
   127             return;
   128         }
   129         if (real != null && real.equals(exp)) {
   130             return;
   131         }
   132         throw new AssertionError(msg + " expected: " + exp + " real: " + real);
   133     }
   134 
   135     private static void assertNotNull(Object obj, String msg) {
   136         assert obj != null : msg;
   137     }
   138     
   139     @JavaScriptBody(args = { "n" }, body = "return window[n] ? window[n] : null;")
   140     private static native Object window(String name);
   141     @JavaScriptBody(args = { "fn" }, body = "return fn();")
   142     private static native String invoke(Object fn);
   143     
   144     @Factory public static Object[] create() {
   145         return VMTest.create(HtmlAnnotationsTest.class);
   146     }
   147 }