ko4j registers implementation of Transfer and WSTransfer based on XHR and WebSocket from a browser. The Java implementations of these interfaces has been moved to ko-ws-tyrus module. JavaScript arrays passed as parameters to Java callback methods need to be wrapped.
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2013-2013 Oracle and/or its affiliates. All rights reserved.
6 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7 * Other names may be trademarks of their respective owners.
9 * The contents of this file are subject to the terms of either the GNU
10 * General Public License Version 2 only ("GPL") or the Common
11 * Development and Distribution License("CDDL") (collectively, the
12 * "License"). You may not use this file except in compliance with the
13 * License. You can obtain a copy of the License at
14 * http://www.netbeans.org/cddl-gplv2.html
15 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16 * specific language governing permissions and limitations under the
17 * License. When distributing the software, include this License Header
18 * Notice in each file and include the License file at
19 * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
20 * particular file as subject to the "Classpath" exception as provided
21 * by Oracle in the GPL Version 2 section of the License file that
22 * accompanied this code. If applicable, add the following below the
23 * License Header, with the fields enclosed by brackets [] replaced by
24 * your own identifying information:
25 * "Portions Copyrighted [year] [name of copyright owner]"
29 * The Original Software is NetBeans. The Initial Developer of the Original
30 * Software is Oracle. Portions Copyright 2013-2013 Oracle. All Rights Reserved.
32 * If you wish your version of this file to be governed by only the CDDL
33 * or only the GPL Version 2, indicate your decision by adding
34 * "[Contributor] elects to include this software in this distribution
35 * under the [CDDL or GPL Version 2] license." If you do not indicate a
36 * single choice of license, a recipient has the option to distribute
37 * your version of this file under either the CDDL, the GPL Version 2 or
38 * to extend the choice of license to its licensees as provided above.
39 * However, if you add GPL Version 2 code and therefore, elected the GPL
40 * Version 2 license, then the option applies only if the new code is
41 * made subject to such option by the copyright holder.
43 package net.java.html.js.tests;
45 import java.util.Arrays;
46 import java.util.concurrent.Callable;
47 import org.apidesign.html.json.tck.KOTest;
51 * @author Jaroslav Tulach <jtulach@netbeans.org>
53 public class JavaScriptBodyTest {
54 @KOTest public void sumTwoNumbers() {
55 int res = Bodies.sum(5, 3);
56 assert res == 8 : "Expecting 8: " + res;
59 @KOTest public void accessJsObject() {
60 Object o = Bodies.instance(10);
61 int ten = Bodies.readX(o);
62 assert ten == 10 : "Expecting ten: " + ten;
65 @KOTest public void callWithNoReturnType() {
66 Object o = Bodies.instance(10);
68 int ten = Bodies.readX(o);
69 assert ten == 11 : "Expecting eleven: " + ten;
72 @KOTest public void callbackToRunnable() {
75 assert run.cnt == 1 : "Can call even private implementation classes: " + run.cnt;
78 @KOTest public void typeOfCharacter() {
79 String charType = Bodies.typeof('a', false);
80 assert "number".equals(charType) : "Expecting number type: " + charType;
82 @KOTest public void typeOfBoolean() {
83 String booleanType = Bodies.typeof(true, false);
84 assert "boolean".equals(booleanType) : "Expecting boolean type: " + booleanType;
87 @KOTest public void typeOfPrimitiveBoolean() {
88 String booleanType = Bodies.typeof(true);
89 assert "boolean".equals(booleanType) || "number".equals(booleanType):
90 "Expecting boolean or at least number type: " + booleanType;
93 @KOTest public void typeOfInteger() {
94 String intType = Bodies.typeof(1, false);
95 assert "number".equals(intType) : "Expecting number type: " + intType;
98 @KOTest public void typeOfString() {
99 String strType = Bodies.typeof("Ahoj", false);
100 assert "string".equals(strType) : "Expecting string type: " + strType;
103 @KOTest public void typeOfDouble() {
104 String doubleType = Bodies.typeof(0.33, false);
105 assert "number".equals(doubleType) : "Expecting number type: " + doubleType;
108 @KOTest public void typeOfBooleanValueOf() {
109 String booleanType = Bodies.typeof(true, true);
110 assert "boolean".equals(booleanType) : "Expecting boolean type: " + booleanType;
113 @KOTest public void typeOfIntegerValueOf() {
114 String intType = Bodies.typeof(1, true);
115 assert "number".equals(intType) : "Expecting number type: " + intType;
118 @KOTest public void typeOfStringValueOf() {
119 String strType = Bodies.typeof("Ahoj", true);
120 assert "string".equals(strType) : "Expecting string type: " + strType;
123 @KOTest public void typeOfDoubleValueOf() {
124 String doubleType = Bodies.typeof(0.33, true);
125 assert "number".equals(doubleType) : "Expecting number type: " + doubleType;
128 @KOTest public void computeInARunnable() {
129 final int[] sum = new int[2];
130 class First implements Runnable {
131 @Override public void run() {
132 sum[0] = Bodies.sum(22, 20);
133 sum[1] = Bodies.sum(32, 10);
136 Bodies.callback(new First());
137 assert sum[0] == 42 : "Computed OK " + sum[0];
138 assert sum[1] == 42 : "Computed OK too: " + sum[1];
141 @KOTest public void doubleCallbackToRunnable() {
142 final R run = new R();
143 final R r2 = new R();
144 class First implements Runnable {
145 @Override public void run() {
146 Bodies.callback(run);
150 Bodies.callback(new First());
151 assert run.cnt == 1 : "Can call even private implementation classes: " + run.cnt;
152 assert r2.cnt == 1 : "Can call even private implementation classes: " + r2.cnt;
155 @KOTest public void identity() {
156 Object p = new Object();
157 Object r = Bodies.id(p);
158 assert r == p : "The object is the same";
161 @KOTest public void encodingString() {
162 Object p = "Ji\n\"Hi\"\nHon";
163 Object r = Bodies.id(p);
164 assert p.equals(r) : "The object is the same: " + p + " != " + r;
167 @KOTest public void nullIsNull() {
169 Object r = Bodies.id(p);
170 assert r == p : "The null is the same";
173 @KOTest public void callbackWithResult() {
174 Callable<Boolean> c = new C();
175 Object b = Bodies.callback(c);
176 assert b == Boolean.TRUE : "Should return true";
179 @KOTest public void callbackWithParameters() {
180 int res = Bodies.sumIndirect(new Sum());
181 assert res == 42 : "Expecting 42";
184 @KOTest public void selectFromStringJavaArray() {
185 String[] arr = { "Ahoj", "World" };
186 Object res = Bodies.select(arr, 1);
187 assert "World".equals(res) : "Expecting World, but was: " + res;
190 @KOTest public void selectFromObjectJavaArray() {
191 Object[] arr = { new Object(), new Object() };
192 Object res = Bodies.select(arr, 1);
193 assert arr[1].equals(res) : "Expecting " + arr[1] + ", but was: " + res;
196 @KOTest public void lengthOfJavaArray() {
197 String[] arr = { "Ahoj", "World" };
198 int res = Bodies.length(arr);
199 assert res == 2 : "Expecting 2, but was: " + res;
202 @KOTest public void isJavaArray() {
203 String[] arr = { "Ahoj", "World" };
204 boolean is = Bodies.isArray(arr);
205 assert is: "Expecting it to be an array: " + is;
208 @KOTest public void javaArrayInOutIsCopied() {
209 String[] arr = { "Ahoj", "World" };
210 Object res = Bodies.id(arr);
211 assert res != null : "Non-null is returned";
212 assert res instanceof Object[] : "Returned an array: " + res;
213 assert !(res instanceof String[]) : "Not returned a string array: " + res;
215 Object[] ret = (Object[]) res;
216 assert arr.length == ret.length : "Same length: " + ret.length;
217 assert arr[0].equals(ret[0]) : "Same first elem";
218 assert arr[1].equals(ret[1]) : "Same 2nd elem";
221 @KOTest public void modifyJavaArrayHasNoEffect() {
222 String[] arr = { "Ahoj", "World" };
223 String value = Bodies.modify(arr, 0, "Hello");
224 assert "Hello".equals(value) : "Inside JS the value is changed: " + value;
225 assert "Ahoj".equals(arr[0]) : "From a Java point of view it remains: " + arr[0];
229 public void callbackWithArray() {
230 class A implements Callable<String[]> {
232 public String[] call() throws Exception {
233 return new String[] { "Hello" };
236 Callable<String[]> a = new A();
237 Object b = Bodies.callbackAndPush(a, "World!");
238 assert b instanceof Object[] : "Returns an array: " + b;
239 Object[] arr = (Object[]) b;
240 String str = Arrays.toString(arr);
241 assert arr.length == 2 : "Size is two " + str;
242 assert "Hello".equals(arr[0]) : "Hello expected: " + arr[0];
243 assert "World!".equals(arr[1]) : "World! expected: " + arr[1];
246 @KOTest public void truth() {
247 assert Bodies.truth() : "True is true";
250 @KOTest public void factorial2() {
251 assert new Factorial().factorial(2) == 2;
254 @KOTest public void factorial3() {
255 assert new Factorial().factorial(3) == 6;
258 @KOTest public void factorial4() {
259 assert new Factorial().factorial(4) == 24;
262 @KOTest public void factorial5() {
263 assert new Factorial().factorial(5) == 120;
266 @KOTest public void factorial6() {
267 assert new Factorial().factorial(6) == 720;
270 @KOTest public void sumArray() {
271 int r = Bodies.sumArr(new Sum());
272 assert r == 6 : "Sum is six: " + r;
275 private static class R implements Runnable {
277 private final Thread initThread;
280 initThread = Thread.currentThread();
285 assert initThread == Thread.currentThread() : "Expecting to run in " + initThread + " but running in " + Thread.currentThread();
290 private static class C implements Callable<Boolean> {
292 public Boolean call() throws Exception {