rt/emul/compacttest/src/test/java/org/apidesign/bck2brwsr/vmtest/impl/HtmlAnnotationsTest.java
Perform necessary conversions before returning Java value to JavaScript from a Java callback
2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
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.
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.
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.
18 package org.apidesign.bck2brwsr.vmtest.impl;
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;
26 /** Verify cooperation with net.java.html.js annotations.
28 * @author Jaroslav Tulach <jtulach@netbeans.org>
30 public class HtmlAnnotationsTest {
31 static int firstCheck;
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";
40 @BrwsrTest public void fourtyTwo() throws Exception {
41 assertMulNotDefinedForTheFirstTime();
42 assertEquals(HtmlAnnotations.fourtyTwo(), 42);
45 @BrwsrTest public void externalMul() throws Exception {
46 assertMulNotDefinedForTheFirstTime();
47 assertEquals(HtmlAnnotations.useExternalMul(7, 6), 42);
50 @BrwsrTest public void callRunnableFromJS() throws Exception {
51 assertMulNotDefinedForTheFirstTime();
52 assertEquals(HtmlAnnotations.callback(), 1);
55 @BrwsrTest public void callStaticMethodFromJS() throws Exception {
56 assertMulNotDefinedForTheFirstTime();
57 assertEquals(HtmlAnnotations.staticCallback(), 1);
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);
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);
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;
82 @BrwsrTest public void date() throws Exception {
83 Object april = HtmlAnnotations.april2016();
84 assertEquals(HtmlAnnotations.year(april), 2016);
87 @BrwsrTest public void yes() throws Exception {
88 String yes = HtmlAnnotations.yesNo(new Callable<Boolean>() {
90 public Boolean call() throws Exception {
94 assertEquals(yes, "yes", "TRUE is true");
97 @BrwsrTest public void no() throws Exception {
98 String no = HtmlAnnotations.yesNo(new Callable<Boolean>() {
100 public Boolean call() throws Exception {
101 return Boolean.FALSE;
104 assertEquals(no, "no", "FALSE is false");
107 private static void assertEquals(double real, double exp) {
108 if (real - exp < 0.01) {
111 assert false : "Expecting " + exp + " but was " + real;
114 private static void assertEquals(Object real, Object exp, String msg) {
118 if (real != null && real.equals(exp)) {
121 throw new AssertionError(msg + " expected: " + exp + " real: " + real);
124 private static void assertNotNull(Object obj, String msg) {
125 assert obj != null : msg;
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);
133 @Factory public static Object[] create() {
134 return VMTest.create(HtmlAnnotationsTest.class);