jaroslav@1282
|
1 |
/**
|
jaroslav@1282
|
2 |
* Back 2 Browser Bytecode Translator
|
jaroslav@1787
|
3 |
* Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jaroslav@1282
|
4 |
*
|
jaroslav@1282
|
5 |
* This program is free software: you can redistribute it and/or modify
|
jaroslav@1282
|
6 |
* it under the terms of the GNU General Public License as published by
|
jaroslav@1282
|
7 |
* the Free Software Foundation, version 2 of the License.
|
jaroslav@1282
|
8 |
*
|
jaroslav@1282
|
9 |
* This program is distributed in the hope that it will be useful,
|
jaroslav@1282
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
jaroslav@1282
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
jaroslav@1282
|
12 |
* GNU General Public License for more details.
|
jaroslav@1282
|
13 |
*
|
jaroslav@1282
|
14 |
* You should have received a copy of the GNU General Public License
|
jaroslav@1282
|
15 |
* along with this program. Look for COPYING file in the top folder.
|
jaroslav@1282
|
16 |
* If not, see http://opensource.org/licenses/GPL-2.0.
|
jaroslav@1282
|
17 |
*/
|
jaroslav@1282
|
18 |
package org.apidesign.bck2brwsr.vmtest.impl;
|
jaroslav@1282
|
19 |
|
jaroslav@1923
|
20 |
import java.util.concurrent.Callable;
|
jaroslav@1722
|
21 |
import org.apidesign.bck2brwsr.core.JavaScriptBody;
|
jaroslav@1282
|
22 |
import org.apidesign.bck2brwsr.vmtest.BrwsrTest;
|
jaroslav@1282
|
23 |
import org.apidesign.bck2brwsr.vmtest.VMTest;
|
jaroslav@1282
|
24 |
import org.testng.annotations.Factory;
|
jaroslav@1282
|
25 |
|
jaroslav@1282
|
26 |
/** Verify cooperation with net.java.html.js annotations.
|
jaroslav@1282
|
27 |
*
|
jaroslav@1282
|
28 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jaroslav@1282
|
29 |
*/
|
jaroslav@1282
|
30 |
public class HtmlAnnotationsTest {
|
jaroslav@1722
|
31 |
static int firstCheck;
|
jaroslav@1722
|
32 |
|
jaroslav@1722
|
33 |
private void assertMulNotDefinedForTheFirstTime() {
|
jaroslav@1722
|
34 |
if (firstCheck++ == 0) {
|
jaroslav@1732
|
35 |
Object mul = window("mul");
|
jaroslav@1722
|
36 |
assert mul == null : "htmlannotations.js should not be processed before first call to HtmlAnnotations class";
|
jaroslav@1722
|
37 |
}
|
jaroslav@1722
|
38 |
}
|
jaroslav@1722
|
39 |
|
jaroslav@1282
|
40 |
@BrwsrTest public void fourtyTwo() throws Exception {
|
jaroslav@1722
|
41 |
assertMulNotDefinedForTheFirstTime();
|
jaroslav@1282
|
42 |
assertEquals(HtmlAnnotations.fourtyTwo(), 42);
|
jaroslav@1282
|
43 |
}
|
jaroslav@1282
|
44 |
|
jaroslav@1282
|
45 |
@BrwsrTest public void externalMul() throws Exception {
|
jaroslav@1722
|
46 |
assertMulNotDefinedForTheFirstTime();
|
jaroslav@1282
|
47 |
assertEquals(HtmlAnnotations.useExternalMul(7, 6), 42);
|
jaroslav@1282
|
48 |
}
|
jaroslav@1282
|
49 |
|
jaroslav@1282
|
50 |
@BrwsrTest public void callRunnableFromJS() throws Exception {
|
jaroslav@1722
|
51 |
assertMulNotDefinedForTheFirstTime();
|
jaroslav@1282
|
52 |
assertEquals(HtmlAnnotations.callback(), 1);
|
jaroslav@1282
|
53 |
}
|
jaroslav@1282
|
54 |
|
jaroslav@1282
|
55 |
@BrwsrTest public void callStaticMethodFromJS() throws Exception {
|
jaroslav@1722
|
56 |
assertMulNotDefinedForTheFirstTime();
|
jaroslav@1282
|
57 |
assertEquals(HtmlAnnotations.staticCallback(), 1);
|
jaroslav@1282
|
58 |
}
|
jaroslav@1282
|
59 |
|
jaroslav@1282
|
60 |
@BrwsrTest public void callbackWithFourParamsAndReturnType() throws Exception {
|
jaroslav@1722
|
61 |
assertMulNotDefinedForTheFirstTime();
|
jaroslav@1282
|
62 |
Object instance = HtmlAnnotations.create();
|
jaroslav@1282
|
63 |
assertNotNull(instance, "Instance created");
|
jaroslav@1282
|
64 |
assertEquals(HtmlAnnotations.first(instance, 42, 31), 42);
|
jaroslav@1282
|
65 |
}
|
jaroslav@1282
|
66 |
|
jaroslav@1282
|
67 |
@BrwsrTest public void callbackWithObjectParamsAndReturnType() throws Exception {
|
jaroslav@1722
|
68 |
assertMulNotDefinedForTheFirstTime();
|
jaroslav@1282
|
69 |
Object instance = HtmlAnnotations.create();
|
jaroslav@1282
|
70 |
assertNotNull(instance, "Instance created");
|
jaroslav@1282
|
71 |
assertEquals(HtmlAnnotations.onError(instance, 42.0), 42.0);
|
jaroslav@1282
|
72 |
}
|
jaroslav@1282
|
73 |
|
jaroslav@1732
|
74 |
@BrwsrTest public void quotedStar() throws Exception {
|
jaroslav@1732
|
75 |
assertMulNotDefinedForTheFirstTime();
|
jaroslav@1732
|
76 |
HtmlAnnotations.empty();
|
jaroslav@1732
|
77 |
Object fn = window("all");
|
jaroslav@1732
|
78 |
String msg = invoke(fn);
|
jaroslav@1732
|
79 |
assert "*/*".equals(msg) : "String '*/*' as expected: " + msg;
|
jaroslav@1732
|
80 |
}
|
jaroslav@1919
|
81 |
|
jaroslav@1919
|
82 |
@BrwsrTest public void date() throws Exception {
|
jaroslav@1919
|
83 |
Object april = HtmlAnnotations.april2016();
|
jaroslav@1919
|
84 |
assertEquals(HtmlAnnotations.year(april), 2016);
|
jaroslav@1919
|
85 |
}
|
jaroslav@1923
|
86 |
|
jaroslav@1923
|
87 |
@BrwsrTest public void yes() throws Exception {
|
jaroslav@1923
|
88 |
String yes = HtmlAnnotations.yesNo(new Callable<Boolean>() {
|
jaroslav@1923
|
89 |
@Override
|
jaroslav@1923
|
90 |
public Boolean call() throws Exception {
|
jaroslav@1923
|
91 |
return Boolean.TRUE;
|
jaroslav@1923
|
92 |
}
|
jaroslav@1923
|
93 |
});
|
jaroslav@1923
|
94 |
assertEquals(yes, "yes", "TRUE is true");
|
jaroslav@1923
|
95 |
}
|
jaroslav@1923
|
96 |
|
jaroslav@1923
|
97 |
@BrwsrTest public void no() throws Exception {
|
jaroslav@1923
|
98 |
String no = HtmlAnnotations.yesNo(new Callable<Boolean>() {
|
jaroslav@1923
|
99 |
@Override
|
jaroslav@1923
|
100 |
public Boolean call() throws Exception {
|
jaroslav@1923
|
101 |
return Boolean.FALSE;
|
jaroslav@1923
|
102 |
}
|
jaroslav@1923
|
103 |
});
|
jaroslav@1923
|
104 |
assertEquals(no, "no", "FALSE is false");
|
jaroslav@1923
|
105 |
}
|
jaroslav@1732
|
106 |
|
jaroslav@1282
|
107 |
private static void assertEquals(double real, double exp) {
|
jaroslav@1282
|
108 |
if (real - exp < 0.01) {
|
jaroslav@1282
|
109 |
return;
|
jaroslav@1282
|
110 |
}
|
jaroslav@1282
|
111 |
assert false : "Expecting " + exp + " but was " + real;
|
jaroslav@1282
|
112 |
}
|
jaroslav@1282
|
113 |
|
jaroslav@1923
|
114 |
private static void assertEquals(Object real, Object exp, String msg) {
|
jaroslav@1923
|
115 |
if (real == exp) {
|
jaroslav@1923
|
116 |
return;
|
jaroslav@1923
|
117 |
}
|
jaroslav@1923
|
118 |
if (real != null && real.equals(exp)) {
|
jaroslav@1923
|
119 |
return;
|
jaroslav@1923
|
120 |
}
|
jaroslav@1923
|
121 |
throw new AssertionError(msg + " expected: " + exp + " real: " + real);
|
jaroslav@1923
|
122 |
}
|
jaroslav@1923
|
123 |
|
jaroslav@1282
|
124 |
private static void assertNotNull(Object obj, String msg) {
|
jaroslav@1282
|
125 |
assert obj != null : msg;
|
jaroslav@1282
|
126 |
}
|
jaroslav@1282
|
127 |
|
jaroslav@1732
|
128 |
@JavaScriptBody(args = { "n" }, body = "return window[n] ? window[n] : null;")
|
jaroslav@1732
|
129 |
private static native Object window(String name);
|
jaroslav@1732
|
130 |
@JavaScriptBody(args = { "fn" }, body = "return fn();")
|
jaroslav@1732
|
131 |
private static native String invoke(Object fn);
|
jaroslav@1722
|
132 |
|
jaroslav@1282
|
133 |
@Factory public static Object[] create() {
|
jaroslav@1282
|
134 |
return VMTest.create(HtmlAnnotationsTest.class);
|
jaroslav@1282
|
135 |
}
|
jaroslav@1282
|
136 |
}
|