rt/emul/compacttest/src/test/java/org/apidesign/bck2brwsr/vmtest/impl/HtmlAnnotationsTest.java
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 assertMulNotDefinedForTheFirstTime();
84 Object april = HtmlAnnotations.april2016();
85 assertEquals(HtmlAnnotations.year(april), 2016);
88 @BrwsrTest public void yes() throws Exception {
89 assertMulNotDefinedForTheFirstTime();
90 String yes = HtmlAnnotations.yesNo(new Callable<Boolean>() {
92 public Boolean call() throws Exception {
96 assertEquals(yes, "yes", "TRUE is true");
99 @BrwsrTest public void no() throws Exception {
100 assertMulNotDefinedForTheFirstTime();
101 String no = HtmlAnnotations.yesNo(new Callable<Boolean>() {
103 public Boolean call() throws Exception {
104 return Boolean.FALSE;
107 assertEquals(no, "no", "FALSE is false");
111 public void compareArrayOfDoubles() throws Exception {
113 boolean res = HtmlAnnotations.compareArr(new Object[] { val }, val);
114 assertEquals(res, true, "Should be in the array");
117 private static void assertEquals(double real, double exp) {
118 if (real - exp < 0.01) {
121 assert false : "Expecting " + exp + " but was " + real;
124 private static void assertEquals(Object real, Object exp, String msg) {
128 if (real != null && real.equals(exp)) {
131 throw new AssertionError(msg + " expected: " + exp + " real: " + real);
134 private static void assertNotNull(Object obj, String msg) {
135 assert obj != null : msg;
138 @JavaScriptBody(args = { "n" }, body = "return window[n] ? window[n] : null;")
139 private static native Object window(String name);
140 @JavaScriptBody(args = { "fn" }, body = "return fn();")
141 private static native String invoke(Object fn);
143 @Factory public static Object[] create() {
144 return VMTest.create(HtmlAnnotationsTest.class);