2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2013-2014 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-2014 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.io.StringReader;
46 import java.util.Arrays;
47 import java.util.concurrent.Callable;
48 import org.apidesign.html.boot.spi.Fn;
49 import org.apidesign.html.json.tck.KOTest;
53 * @author Jaroslav Tulach
55 public class JavaScriptBodyTest {
56 @KOTest public void sumTwoNumbers() {
57 int res = Bodies.sum(5, 3);
58 assert res == 8 : "Expecting 8: " + res;
61 @KOTest public void accessJsObject() {
62 Object o = Bodies.instance(10);
63 int ten = Bodies.readX(o);
64 assert ten == 10 : "Expecting ten: " + ten;
67 @KOTest public void callWithNoReturnType() {
68 Object o = Bodies.instance(10);
70 int ten = Bodies.readX(o);
71 assert ten == 11 : "Expecting eleven: " + ten;
74 @KOTest public void callbackToRunnable() {
77 assert run.cnt == 1 : "Can call even private implementation classes: " + run.cnt;
81 @KOTest public void asyncCallbackToRunnable() throws InterruptedException {
82 if (asyncRun == null) {
84 Bodies.asyncCallback(asyncRun);
86 if (asyncRun.cnt == 0) {
87 throw new InterruptedException();
89 assert asyncRun.cnt == 1 : "Even async callback must arrive once: " + asyncRun.cnt;
92 @KOTest public void asyncCallbackFlushed() throws InterruptedException {
94 for (int i = 0; i < 10; i++) {
95 Bodies.asyncCallback(r);
97 int fourtyTwo = Bodies.sum(35, 7);
98 assert r.cnt == 10 : "Ten calls: " + r.cnt;
99 assert fourtyTwo == 42 : "Meaning of the world expected: " + fourtyTwo;
102 @KOTest public void typeOfCharacter() {
103 String charType = Bodies.typeof('a', false);
104 assert "number".equals(charType) : "Expecting number type: " + charType;
106 @KOTest public void typeOfBoolean() {
107 String booleanType = Bodies.typeof(true, false);
108 assert "boolean".equals(booleanType) : "Expecting boolean type: " + booleanType;
111 @KOTest public void typeOfPrimitiveBoolean() {
112 String booleanType = Bodies.typeof(true);
113 assert "boolean".equals(booleanType) || "number".equals(booleanType):
114 "Expecting boolean or at least number type: " + booleanType;
117 @KOTest public void typeOfInteger() {
118 String intType = Bodies.typeof(1, false);
119 assert "number".equals(intType) : "Expecting number type: " + intType;
122 @KOTest public void typeOfString() {
123 String strType = Bodies.typeof("Ahoj", false);
124 assert "string".equals(strType) : "Expecting string type: " + strType;
127 @KOTest public void typeOfDouble() {
128 String doubleType = Bodies.typeof(0.33, false);
129 assert "number".equals(doubleType) : "Expecting number type: " + doubleType;
132 @KOTest public void typeOfBooleanValueOf() {
133 String booleanType = Bodies.typeof(true, true);
134 assert "boolean".equals(booleanType) : "Expecting boolean type: " + booleanType;
137 @KOTest public void typeOfIntegerValueOf() {
138 String intType = Bodies.typeof(1, true);
139 assert "number".equals(intType) : "Expecting number type: " + intType;
142 @KOTest public void typeOfStringValueOf() {
143 String strType = Bodies.typeof("Ahoj", true);
144 assert "string".equals(strType) : "Expecting string type: " + strType;
147 @KOTest public void typeOfDoubleValueOf() {
148 String doubleType = Bodies.typeof(0.33, true);
149 assert "number".equals(doubleType) : "Expecting number type: " + doubleType;
152 @KOTest public void computeInARunnable() {
153 final int[] sum = new int[2];
154 class First implements Runnable {
155 @Override public void run() {
156 sum[0] = Bodies.sum(22, 20);
157 sum[1] = Bodies.sum(32, 10);
160 Bodies.callback(new First());
161 assert sum[0] == 42 : "Computed OK " + sum[0];
162 assert sum[1] == 42 : "Computed OK too: " + sum[1];
165 @KOTest public void doubleCallbackToRunnable() {
166 final R run = new R();
167 final R r2 = new R();
168 class First implements Runnable {
169 @Override public void run() {
170 Bodies.callback(run);
174 Bodies.callback(new First());
175 assert run.cnt == 1 : "Can call even private implementation classes: " + run.cnt;
176 assert r2.cnt == 1 : "Can call even private implementation classes: " + r2.cnt;
179 @KOTest public void identity() {
180 Object p = new Object();
181 Object r = Bodies.id(p);
182 assert r == p : "The object is the same";
185 @KOTest public void encodingString() {
186 Object p = "Ji\n\"Hi\"\nHon";
187 Object r = Bodies.id(p);
188 assert p.equals(r) : "The object is the same: " + p + " != " + r;
191 @KOTest public void encodingBackslashString() {
192 Object p = "{\"firstName\":\"/*\\n * Copyright (c) 2013\",\"lastName\":null,\"sex\":\"MALE\",\"address\":{\"street\":null}}";
193 Object r = Bodies.id(p);
194 assert p.equals(r) : "The object is the same: " + p + " != " + r;
197 @KOTest public void nullIsNull() {
199 Object r = Bodies.id(p);
200 assert r == p : "The null is the same";
203 @KOTest public void callbackWithResult() {
204 Callable<Boolean> c = new C();
205 Object b = Bodies.callback(c);
206 assert b == Boolean.TRUE : "Should return true";
209 @KOTest public void callbackWithParameters() {
210 int res = Bodies.sumIndirect(new Sum());
211 assert res == 42 : "Expecting 42";
214 @KOTest public void selectFromStringJavaArray() {
215 String[] arr = { "Ahoj", "World" };
216 Object res = Bodies.select(arr, 1);
217 assert "World".equals(res) : "Expecting World, but was: " + res;
220 @KOTest public void selectFromObjectJavaArray() {
221 Object[] arr = { new Object(), new Object() };
222 Object res = Bodies.select(arr, 1);
223 assert arr[1].equals(res) : "Expecting " + arr[1] + ", but was: " + res;
226 @KOTest public void lengthOfJavaArray() {
227 String[] arr = { "Ahoj", "World" };
228 int res = Bodies.length(arr);
229 assert res == 2 : "Expecting 2, but was: " + res;
232 @KOTest public void isJavaArray() {
233 String[] arr = { "Ahoj", "World" };
234 boolean is = Bodies.isArray(arr);
235 assert is: "Expecting it to be an array: " + is;
238 @KOTest public void javaArrayInOutIsCopied() {
239 String[] arr = { "Ahoj", "World" };
240 Object res = Bodies.id(arr);
241 assert res != null : "Non-null is returned";
242 assert res instanceof Object[] : "Returned an array: " + res;
243 assert !(res instanceof String[]) : "Not returned a string array: " + res;
245 Object[] ret = (Object[]) res;
246 assert arr.length == ret.length : "Same length: " + ret.length;
247 assert arr[0].equals(ret[0]) : "Same first elem";
248 assert arr[1].equals(ret[1]) : "Same 2nd elem";
251 @KOTest public void modifyJavaArrayHasNoEffect() {
252 String[] arr = { "Ahoj", "World" };
253 String value = Bodies.modify(arr, 0, "Hello");
254 assert "Hello".equals(value) : "Inside JS the value is changed: " + value;
255 assert "Ahoj".equals(arr[0]) : "From a Java point of view it remains: " + arr[0];
259 public void callbackWithArray() {
260 class A implements Callable<String[]> {
262 public String[] call() throws Exception {
263 return new String[] { "Hello" };
266 Callable<String[]> a = new A();
267 Object b = Bodies.callbackAndPush(a, "World!");
268 assert b instanceof Object[] : "Returns an array: " + b;
269 Object[] arr = (Object[]) b;
270 String str = Arrays.toString(arr);
271 assert arr.length == 2 : "Size is two " + str;
272 assert "Hello".equals(arr[0]) : "Hello expected: " + arr[0];
273 assert "World!".equals(arr[1]) : "World! expected: " + arr[1];
276 @KOTest public void sumVector() {
277 double[] arr = { 1.0, 2.0, 3.0 };
278 double res = Bodies.sumVector(arr);
279 assert 6.0 == res : "Expecting six: " + res;
282 @KOTest public void sumMatrix() {
283 double[][] arr = { { 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0, 1.0 } };
284 double res = Bodies.sumMatrix(arr);
285 assert 6.0 == res : "Expecting six: " + res;
288 @KOTest public void truth() {
289 assert Bodies.truth() : "True is true";
292 @KOTest public void factorial2() {
293 assert new Factorial().factorial(2) == 2;
296 @KOTest public void factorial3() {
297 assert new Factorial().factorial(3) == 6;
300 @KOTest public void factorial4() {
301 assert new Factorial().factorial(4) == 24;
304 @KOTest public void factorial5() {
305 assert new Factorial().factorial(5) == 120;
308 @KOTest public void factorial6() {
309 assert new Factorial().factorial(6) == 720;
312 @KOTest public void sumArray() {
313 int r = Bodies.sumArr(new Sum());
314 assert r == 6 : "Sum is six: " + r;
317 @KOTest public void staticCallback() {
318 int r = Bodies.staticCallback();
319 assert r == 42 : "Expecting 42: " + r;
323 @KOTest public void callLater() throws Exception{
324 final Fn.Presenter p = Fn.activePresenter();
329 p.loadScript(new StringReader(
330 "if (typeof window === 'undefined') window = {};"
334 p.loadScript(new StringReader(
339 throw new InterruptedException();
341 assert l.call == 42 : "Method was called: " + l.call;
344 private static class R implements Runnable {
346 private final Thread initThread;
349 initThread = Thread.currentThread();
354 assert initThread == Thread.currentThread() : "Expecting to run in " + initThread + " but running in " + Thread.currentThread();
359 private static class C implements Callable<Boolean> {
361 public Boolean call() throws Exception {