rt/emul/compacttest/src/test/java/org/apidesign/bck2brwsr/vmtest/impl/HtmlAnnotationsTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 17 Apr 2016 13:16:31 +0200
changeset 1923 4185cdeeee7e
parent 1919 a064702472ec
child 1924 cf3873164b9f
permissions -rw-r--r--
Perform necessary conversions before returning Java value to JavaScript from a Java callback
     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         Object april = HtmlAnnotations.april2016();
    84         assertEquals(HtmlAnnotations.year(april), 2016);
    85     }
    86 
    87     @BrwsrTest public void yes() throws Exception {
    88         String yes = HtmlAnnotations.yesNo(new Callable<Boolean>() {
    89             @Override
    90             public Boolean call() throws Exception {
    91                 return Boolean.TRUE;
    92             }
    93         });
    94         assertEquals(yes, "yes", "TRUE is true");
    95     }
    96 
    97     @BrwsrTest public void no() throws Exception {
    98         String no = HtmlAnnotations.yesNo(new Callable<Boolean>() {
    99             @Override
   100             public Boolean call() throws Exception {
   101                 return Boolean.FALSE;
   102             }
   103         });
   104         assertEquals(no, "no", "FALSE is false");
   105     }
   106     
   107     private static void assertEquals(double real, double exp) {
   108         if (real - exp < 0.01) {
   109             return;
   110         }
   111         assert false : "Expecting " + exp + " but was " + real;
   112     }
   113 
   114     private static void assertEquals(Object real, Object exp, String msg) {
   115         if (real == exp) {
   116             return;
   117         }
   118         if (real != null && real.equals(exp)) {
   119             return;
   120         }
   121         throw new AssertionError(msg + " expected: " + exp + " real: " + real);
   122     }
   123 
   124     private static void assertNotNull(Object obj, String msg) {
   125         assert obj != null : msg;
   126     }
   127     
   128     @JavaScriptBody(args = { "n" }, body = "return window[n] ? window[n] : null;")
   129     private static native Object window(String name);
   130     @JavaScriptBody(args = { "fn" }, body = "return fn();")
   131     private static native String invoke(Object fn);
   132     
   133     @Factory public static Object[] create() {
   134         return VMTest.create(HtmlAnnotationsTest.class);
   135     }
   136 }