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 <jtulach@netbeans.org>
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;
80 @KOTest public void typeOfCharacter() {
81 String charType = Bodies.typeof('a', false);
82 assert "number".equals(charType) : "Expecting number type: " + charType;
84 @KOTest public void typeOfBoolean() {
85 String booleanType = Bodies.typeof(true, false);
86 assert "boolean".equals(booleanType) : "Expecting boolean type: " + booleanType;
89 @KOTest public void typeOfPrimitiveBoolean() {
90 String booleanType = Bodies.typeof(true);
91 assert "boolean".equals(booleanType) || "number".equals(booleanType):
92 "Expecting boolean or at least number type: " + booleanType;
95 @KOTest public void typeOfInteger() {
96 String intType = Bodies.typeof(1, false);
97 assert "number".equals(intType) : "Expecting number type: " + intType;
100 @KOTest public void typeOfString() {
101 String strType = Bodies.typeof("Ahoj", false);
102 assert "string".equals(strType) : "Expecting string type: " + strType;
105 @KOTest public void typeOfDouble() {
106 String doubleType = Bodies.typeof(0.33, false);
107 assert "number".equals(doubleType) : "Expecting number type: " + doubleType;
110 @KOTest public void typeOfBooleanValueOf() {
111 String booleanType = Bodies.typeof(true, true);
112 assert "boolean".equals(booleanType) : "Expecting boolean type: " + booleanType;
115 @KOTest public void typeOfIntegerValueOf() {
116 String intType = Bodies.typeof(1, true);
117 assert "number".equals(intType) : "Expecting number type: " + intType;
120 @KOTest public void typeOfStringValueOf() {
121 String strType = Bodies.typeof("Ahoj", true);
122 assert "string".equals(strType) : "Expecting string type: " + strType;
125 @KOTest public void typeOfDoubleValueOf() {
126 String doubleType = Bodies.typeof(0.33, true);
127 assert "number".equals(doubleType) : "Expecting number type: " + doubleType;
130 @KOTest public void computeInARunnable() {
131 final int[] sum = new int[2];
132 class First implements Runnable {
133 @Override public void run() {
134 sum[0] = Bodies.sum(22, 20);
135 sum[1] = Bodies.sum(32, 10);
138 Bodies.callback(new First());
139 assert sum[0] == 42 : "Computed OK " + sum[0];
140 assert sum[1] == 42 : "Computed OK too: " + sum[1];
143 @KOTest public void doubleCallbackToRunnable() {
144 final R run = new R();
145 final R r2 = new R();
146 class First implements Runnable {
147 @Override public void run() {
148 Bodies.callback(run);
152 Bodies.callback(new First());
153 assert run.cnt == 1 : "Can call even private implementation classes: " + run.cnt;
154 assert r2.cnt == 1 : "Can call even private implementation classes: " + r2.cnt;
157 @KOTest public void identity() {
158 Object p = new Object();
159 Object r = Bodies.id(p);
160 assert r == p : "The object is the same";
163 @KOTest public void encodingString() {
164 Object p = "Ji\n\"Hi\"\nHon";
165 Object r = Bodies.id(p);
166 assert p.equals(r) : "The object is the same: " + p + " != " + r;
169 @KOTest public void encodingBackslashString() {
170 Object p = "{\"firstName\":\"/*\\n * Copyright (c) 2013\",\"lastName\":null,\"sex\":\"MALE\",\"address\":{\"street\":null}}";
171 Object r = Bodies.id(p);
172 assert p.equals(r) : "The object is the same: " + p + " != " + r;
175 @KOTest public void nullIsNull() {
177 Object r = Bodies.id(p);
178 assert r == p : "The null is the same";
181 @KOTest public void callbackWithResult() {
182 Callable<Boolean> c = new C();
183 Object b = Bodies.callback(c);
184 assert b == Boolean.TRUE : "Should return true";
187 @KOTest public void callbackWithParameters() {
188 int res = Bodies.sumIndirect(new Sum());
189 assert res == 42 : "Expecting 42";
192 @KOTest public void selectFromStringJavaArray() {
193 String[] arr = { "Ahoj", "World" };
194 Object res = Bodies.select(arr, 1);
195 assert "World".equals(res) : "Expecting World, but was: " + res;
198 @KOTest public void selectFromObjectJavaArray() {
199 Object[] arr = { new Object(), new Object() };
200 Object res = Bodies.select(arr, 1);
201 assert arr[1].equals(res) : "Expecting " + arr[1] + ", but was: " + res;
204 @KOTest public void lengthOfJavaArray() {
205 String[] arr = { "Ahoj", "World" };
206 int res = Bodies.length(arr);
207 assert res == 2 : "Expecting 2, but was: " + res;
210 @KOTest public void isJavaArray() {
211 String[] arr = { "Ahoj", "World" };
212 boolean is = Bodies.isArray(arr);
213 assert is: "Expecting it to be an array: " + is;
216 @KOTest public void javaArrayInOutIsCopied() {
217 String[] arr = { "Ahoj", "World" };
218 Object res = Bodies.id(arr);
219 assert res != null : "Non-null is returned";
220 assert res instanceof Object[] : "Returned an array: " + res;
221 assert !(res instanceof String[]) : "Not returned a string array: " + res;
223 Object[] ret = (Object[]) res;
224 assert arr.length == ret.length : "Same length: " + ret.length;
225 assert arr[0].equals(ret[0]) : "Same first elem";
226 assert arr[1].equals(ret[1]) : "Same 2nd elem";
229 @KOTest public void modifyJavaArrayHasNoEffect() {
230 String[] arr = { "Ahoj", "World" };
231 String value = Bodies.modify(arr, 0, "Hello");
232 assert "Hello".equals(value) : "Inside JS the value is changed: " + value;
233 assert "Ahoj".equals(arr[0]) : "From a Java point of view it remains: " + arr[0];
237 public void callbackWithArray() {
238 class A implements Callable<String[]> {
240 public String[] call() throws Exception {
241 return new String[] { "Hello" };
244 Callable<String[]> a = new A();
245 Object b = Bodies.callbackAndPush(a, "World!");
246 assert b instanceof Object[] : "Returns an array: " + b;
247 Object[] arr = (Object[]) b;
248 String str = Arrays.toString(arr);
249 assert arr.length == 2 : "Size is two " + str;
250 assert "Hello".equals(arr[0]) : "Hello expected: " + arr[0];
251 assert "World!".equals(arr[1]) : "World! expected: " + arr[1];
254 @KOTest public void truth() {
255 assert Bodies.truth() : "True is true";
258 @KOTest public void factorial2() {
259 assert new Factorial().factorial(2) == 2;
262 @KOTest public void factorial3() {
263 assert new Factorial().factorial(3) == 6;
266 @KOTest public void factorial4() {
267 assert new Factorial().factorial(4) == 24;
270 @KOTest public void factorial5() {
271 assert new Factorial().factorial(5) == 120;
274 @KOTest public void factorial6() {
275 assert new Factorial().factorial(6) == 720;
278 @KOTest public void sumArray() {
279 int r = Bodies.sumArr(new Sum());
280 assert r == 6 : "Sum is six: " + r;
283 @KOTest public void callLater() throws Exception{
284 final Fn.Presenter p = Fn.activePresenter();
288 p.loadScript(new StringReader(
289 "if (typeof window === 'undefined') window = {};"
291 Later l = new Later();
293 p.loadScript(new StringReader(
296 for (int i = 0; i < 100 && l.call != 42; i++) {
299 assert l.call == 42 : "Method was called: " + l.call;
302 private static class R implements Runnable {
304 private final Thread initThread;
307 initThread = Thread.currentThread();
312 assert initThread == Thread.currentThread() : "Expecting to run in " + initThread + " but running in " + Thread.currentThread();
317 private static class C implements Callable<Boolean> {
319 public Boolean call() throws Exception {