json-tck/src/main/java/net/java/html/js/tests/JavaScriptBodyTest.java
author Jaroslav Tulach <jaroslav.tulach@netbeans.org>
Fri, 07 Feb 2014 07:44:34 +0100
changeset 551 7ca2253fa86d
parent 529 75669f440267
child 565 f09184978a78
permissions -rw-r--r--
Updating copyright headers to mention current year
jaroslav@424
     1
/**
jaroslav@424
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jaroslav@424
     3
 *
jaroslav@551
     4
 * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
jaroslav@424
     5
 *
jaroslav@424
     6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
jaroslav@424
     7
 * Other names may be trademarks of their respective owners.
jaroslav@424
     8
 *
jaroslav@424
     9
 * The contents of this file are subject to the terms of either the GNU
jaroslav@424
    10
 * General Public License Version 2 only ("GPL") or the Common
jaroslav@424
    11
 * Development and Distribution License("CDDL") (collectively, the
jaroslav@424
    12
 * "License"). You may not use this file except in compliance with the
jaroslav@424
    13
 * License. You can obtain a copy of the License at
jaroslav@424
    14
 * http://www.netbeans.org/cddl-gplv2.html
jaroslav@424
    15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jaroslav@424
    16
 * specific language governing permissions and limitations under the
jaroslav@424
    17
 * License.  When distributing the software, include this License Header
jaroslav@424
    18
 * Notice in each file and include the License file at
jaroslav@424
    19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
jaroslav@424
    20
 * particular file as subject to the "Classpath" exception as provided
jaroslav@424
    21
 * by Oracle in the GPL Version 2 section of the License file that
jaroslav@424
    22
 * accompanied this code. If applicable, add the following below the
jaroslav@424
    23
 * License Header, with the fields enclosed by brackets [] replaced by
jaroslav@424
    24
 * your own identifying information:
jaroslav@424
    25
 * "Portions Copyrighted [year] [name of copyright owner]"
jaroslav@424
    26
 *
jaroslav@424
    27
 * Contributor(s):
jaroslav@424
    28
 *
jaroslav@424
    29
 * The Original Software is NetBeans. The Initial Developer of the Original
jaroslav@551
    30
 * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
jaroslav@424
    31
 *
jaroslav@424
    32
 * If you wish your version of this file to be governed by only the CDDL
jaroslav@424
    33
 * or only the GPL Version 2, indicate your decision by adding
jaroslav@424
    34
 * "[Contributor] elects to include this software in this distribution
jaroslav@424
    35
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
jaroslav@424
    36
 * single choice of license, a recipient has the option to distribute
jaroslav@424
    37
 * your version of this file under either the CDDL, the GPL Version 2 or
jaroslav@424
    38
 * to extend the choice of license to its licensees as provided above.
jaroslav@424
    39
 * However, if you add GPL Version 2 code and therefore, elected the GPL
jaroslav@424
    40
 * Version 2 license, then the option applies only if the new code is
jaroslav@424
    41
 * made subject to such option by the copyright holder.
jaroslav@424
    42
 */
jaroslav@424
    43
package net.java.html.js.tests;
jaroslav@424
    44
jaroslav@469
    45
import java.io.StringReader;
jaroslav@430
    46
import java.util.Arrays;
jaroslav@424
    47
import java.util.concurrent.Callable;
jaroslav@469
    48
import org.apidesign.html.boot.spi.Fn;
jaroslav@424
    49
import org.apidesign.html.json.tck.KOTest;
jaroslav@424
    50
jaroslav@424
    51
/**
jaroslav@424
    52
 *
jaroslav@424
    53
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@424
    54
 */
jaroslav@424
    55
public class JavaScriptBodyTest {
jaroslav@424
    56
    @KOTest public void sumTwoNumbers() {
jaroslav@424
    57
        int res = Bodies.sum(5, 3);
jaroslav@424
    58
        assert res == 8 : "Expecting 8: " + res;
jaroslav@424
    59
    }
jaroslav@424
    60
    
jaroslav@424
    61
    @KOTest public void accessJsObject() {
jaroslav@424
    62
        Object o = Bodies.instance(10);
jaroslav@424
    63
        int ten = Bodies.readX(o);
jaroslav@424
    64
        assert ten == 10 : "Expecting ten: " + ten;
jaroslav@424
    65
    }
jaroslav@424
    66
jaroslav@424
    67
    @KOTest public void callWithNoReturnType() {
jaroslav@424
    68
        Object o = Bodies.instance(10);
jaroslav@424
    69
        Bodies.incrementX(o);
jaroslav@424
    70
        int ten = Bodies.readX(o);
jaroslav@424
    71
        assert ten == 11 : "Expecting eleven: " + ten;
jaroslav@424
    72
    }
jaroslav@424
    73
    
jaroslav@424
    74
    @KOTest public void callbackToRunnable() {
jaroslav@424
    75
        R run = new R();
jaroslav@424
    76
        Bodies.callback(run);
jaroslav@424
    77
        assert run.cnt == 1 : "Can call even private implementation classes: " + run.cnt;
jaroslav@424
    78
    }
jaroslav@436
    79
    
jaroslav@436
    80
    @KOTest public void typeOfCharacter() {
jaroslav@436
    81
        String charType = Bodies.typeof('a', false);
jaroslav@436
    82
        assert "number".equals(charType) : "Expecting number type: " + charType;
jaroslav@436
    83
    }
jaroslav@436
    84
    @KOTest public void typeOfBoolean() {
jaroslav@436
    85
        String booleanType = Bodies.typeof(true, false);
jaroslav@436
    86
        assert "boolean".equals(booleanType) : "Expecting boolean type: " + booleanType;
jaroslav@436
    87
    }
jaroslav@436
    88
jaroslav@436
    89
    @KOTest public void typeOfPrimitiveBoolean() {
jaroslav@436
    90
        String booleanType = Bodies.typeof(true);
jaroslav@440
    91
        assert "boolean".equals(booleanType) || "number".equals(booleanType): 
jaroslav@440
    92
            "Expecting boolean or at least number type: " + booleanType;
jaroslav@436
    93
    }
jaroslav@436
    94
jaroslav@436
    95
    @KOTest public void typeOfInteger() {
jaroslav@436
    96
        String intType = Bodies.typeof(1, false);
jaroslav@436
    97
        assert "number".equals(intType) : "Expecting number type: " + intType;
jaroslav@436
    98
    }
jaroslav@436
    99
jaroslav@436
   100
    @KOTest public void typeOfString() {
jaroslav@436
   101
        String strType = Bodies.typeof("Ahoj", false);
jaroslav@436
   102
        assert "string".equals(strType) : "Expecting string type: " + strType;
jaroslav@436
   103
    }
jaroslav@436
   104
jaroslav@436
   105
    @KOTest public void typeOfDouble() {
jaroslav@436
   106
        String doubleType = Bodies.typeof(0.33, false);
jaroslav@436
   107
        assert "number".equals(doubleType) : "Expecting number type: " + doubleType;
jaroslav@436
   108
    }
jaroslav@436
   109
    
jaroslav@436
   110
    @KOTest public void typeOfBooleanValueOf() {
jaroslav@436
   111
        String booleanType = Bodies.typeof(true, true);
jaroslav@436
   112
        assert "boolean".equals(booleanType) : "Expecting boolean type: " + booleanType;
jaroslav@436
   113
    }
jaroslav@436
   114
jaroslav@436
   115
    @KOTest public void typeOfIntegerValueOf() {
jaroslav@436
   116
        String intType = Bodies.typeof(1, true);
jaroslav@436
   117
        assert "number".equals(intType) : "Expecting number type: " + intType;
jaroslav@436
   118
    }
jaroslav@436
   119
jaroslav@436
   120
    @KOTest public void typeOfStringValueOf() {
jaroslav@436
   121
        String strType = Bodies.typeof("Ahoj", true);
jaroslav@436
   122
        assert "string".equals(strType) : "Expecting string type: " + strType;
jaroslav@436
   123
    }
jaroslav@436
   124
jaroslav@436
   125
    @KOTest public void typeOfDoubleValueOf() {
jaroslav@436
   126
        String doubleType = Bodies.typeof(0.33, true);
jaroslav@436
   127
        assert "number".equals(doubleType) : "Expecting number type: " + doubleType;
jaroslav@436
   128
    }
jaroslav@424
   129
jaroslav@424
   130
    @KOTest public void computeInARunnable() {
jaroslav@424
   131
        final int[] sum = new int[2];
jaroslav@424
   132
        class First implements Runnable {
jaroslav@424
   133
            @Override public void run() {
jaroslav@424
   134
                sum[0] = Bodies.sum(22, 20);
jaroslav@424
   135
                sum[1] = Bodies.sum(32, 10);
jaroslav@424
   136
            }
jaroslav@424
   137
        }
jaroslav@424
   138
        Bodies.callback(new First());
jaroslav@424
   139
        assert sum[0] == 42 : "Computed OK " + sum[0];
jaroslav@424
   140
        assert sum[1] == 42 : "Computed OK too: " + sum[1];
jaroslav@424
   141
    }
jaroslav@424
   142
    
jaroslav@424
   143
    @KOTest public void doubleCallbackToRunnable() {
jaroslav@424
   144
        final R run = new R();
jaroslav@424
   145
        final R r2 = new R();
jaroslav@424
   146
        class First implements Runnable {
jaroslav@424
   147
            @Override public void run() {
jaroslav@424
   148
                Bodies.callback(run);
jaroslav@424
   149
                Bodies.callback(r2);
jaroslav@424
   150
            }
jaroslav@424
   151
        }
jaroslav@424
   152
        Bodies.callback(new First());
jaroslav@424
   153
        assert run.cnt == 1 : "Can call even private implementation classes: " + run.cnt;
jaroslav@424
   154
        assert r2.cnt == 1 : "Can call even private implementation classes: " + r2.cnt;
jaroslav@424
   155
    }
jaroslav@424
   156
    
jaroslav@424
   157
    @KOTest public void identity() {
jaroslav@424
   158
        Object p = new Object();
jaroslav@424
   159
        Object r = Bodies.id(p);
jaroslav@424
   160
        assert r == p : "The object is the same";
jaroslav@424
   161
    }
jaroslav@424
   162
jaroslav@424
   163
    @KOTest public void encodingString() {
jaroslav@424
   164
        Object p = "Ji\n\"Hi\"\nHon";
jaroslav@424
   165
        Object r = Bodies.id(p);
jaroslav@424
   166
        assert p.equals(r) : "The object is the same: " + p + " != " + r;
jaroslav@424
   167
    }
jaroslav@424
   168
jaroslav@523
   169
    @KOTest public void encodingBackslashString() {
jaroslav@523
   170
        Object p = "{\"firstName\":\"/*\\n * Copyright (c) 2013\",\"lastName\":null,\"sex\":\"MALE\",\"address\":{\"street\":null}}";
jaroslav@523
   171
        Object r = Bodies.id(p);
jaroslav@523
   172
        assert p.equals(r) : "The object is the same: " + p + " != " + r;
jaroslav@523
   173
    }
jaroslav@523
   174
jaroslav@424
   175
    @KOTest public void nullIsNull() {
jaroslav@424
   176
        Object p = null;
jaroslav@424
   177
        Object r = Bodies.id(p);
jaroslav@424
   178
        assert r == p : "The null is the same";
jaroslav@424
   179
    }
jaroslav@424
   180
    
jaroslav@424
   181
    @KOTest public void callbackWithResult() {
jaroslav@424
   182
        Callable<Boolean> c = new C();
jaroslav@424
   183
        Object b = Bodies.callback(c);
jaroslav@424
   184
        assert b == Boolean.TRUE : "Should return true";
jaroslav@424
   185
    }
jaroslav@424
   186
    
jaroslav@424
   187
    @KOTest public void callbackWithParameters() {
jaroslav@424
   188
        int res = Bodies.sumIndirect(new Sum());
jaroslav@424
   189
        assert res == 42 : "Expecting 42";
jaroslav@424
   190
    }
jaroslav@424
   191
    
jaroslav@429
   192
    @KOTest public void selectFromStringJavaArray() {
jaroslav@424
   193
        String[] arr = { "Ahoj", "World" };
jaroslav@424
   194
        Object res = Bodies.select(arr, 1);
jaroslav@424
   195
        assert "World".equals(res) : "Expecting World, but was: " + res;
jaroslav@424
   196
    }
jaroslav@424
   197
jaroslav@429
   198
    @KOTest public void selectFromObjectJavaArray() {
jaroslav@429
   199
        Object[] arr = { new Object(), new Object() };
jaroslav@429
   200
        Object res = Bodies.select(arr, 1);
jaroslav@429
   201
        assert arr[1].equals(res) : "Expecting " + arr[1] + ", but was: " + res;
jaroslav@429
   202
    }
jaroslav@429
   203
jaroslav@424
   204
    @KOTest public void lengthOfJavaArray() {
jaroslav@424
   205
        String[] arr = { "Ahoj", "World" };
jaroslav@424
   206
        int res = Bodies.length(arr);
jaroslav@424
   207
        assert res == 2 : "Expecting 2, but was: " + res;
jaroslav@424
   208
    }
jaroslav@424
   209
jaroslav@429
   210
    @KOTest public void isJavaArray() {
jaroslav@429
   211
        String[] arr = { "Ahoj", "World" };
jaroslav@429
   212
        boolean is = Bodies.isArray(arr);
jaroslav@429
   213
        assert is: "Expecting it to be an array: " + is;
jaroslav@429
   214
    }
jaroslav@429
   215
jaroslav@429
   216
    @KOTest public void javaArrayInOutIsCopied() {
jaroslav@424
   217
        String[] arr = { "Ahoj", "World" };
jaroslav@424
   218
        Object res = Bodies.id(arr);
jaroslav@429
   219
        assert res != null : "Non-null is returned";
jaroslav@429
   220
        assert res instanceof Object[] : "Returned an array: " + res;
jaroslav@429
   221
        assert !(res instanceof String[]) : "Not returned a string array: " + res;
jaroslav@429
   222
        
jaroslav@429
   223
        Object[] ret = (Object[]) res;
jaroslav@429
   224
        assert arr.length == ret.length : "Same length: " + ret.length;
jaroslav@429
   225
        assert arr[0].equals(ret[0]) : "Same first elem";
jaroslav@429
   226
        assert arr[1].equals(ret[1]) : "Same 2nd elem";
jaroslav@424
   227
    }
jaroslav@424
   228
jaroslav@429
   229
    @KOTest public void modifyJavaArrayHasNoEffect() {
jaroslav@429
   230
        String[] arr = { "Ahoj", "World" };
jaroslav@429
   231
        String value = Bodies.modify(arr, 0, "Hello");
jaroslav@429
   232
        assert "Hello".equals(value) : "Inside JS the value is changed: " + value;
jaroslav@429
   233
        assert "Ahoj".equals(arr[0]) : "From a Java point of view it remains: " + arr[0];
jaroslav@429
   234
    }
jaroslav@424
   235
jaroslav@430
   236
    @KOTest
jaroslav@430
   237
    public void callbackWithArray() {
jaroslav@430
   238
        class A implements Callable<String[]> {
jaroslav@430
   239
            @Override
jaroslav@430
   240
            public String[] call() throws Exception {
jaroslav@430
   241
                return new String[] { "Hello" };
jaroslav@430
   242
            }
jaroslav@430
   243
        }
jaroslav@430
   244
        Callable<String[]> a = new A();
jaroslav@430
   245
        Object b = Bodies.callbackAndPush(a, "World!");
jaroslav@430
   246
        assert b instanceof Object[] : "Returns an array: " + b;
jaroslav@430
   247
        Object[] arr = (Object[]) b;
jaroslav@430
   248
        String str = Arrays.toString(arr);
jaroslav@430
   249
        assert arr.length == 2 : "Size is two " + str;
jaroslav@430
   250
        assert "Hello".equals(arr[0]) : "Hello expected: " + arr[0];
jaroslav@430
   251
        assert "World!".equals(arr[1]) : "World! expected: " + arr[1];
jaroslav@430
   252
    }
jaroslav@430
   253
jaroslav@424
   254
    @KOTest public void truth() {
jaroslav@424
   255
        assert Bodies.truth() : "True is true";
jaroslav@424
   256
    }
jaroslav@424
   257
    
jaroslav@424
   258
    @KOTest public void factorial2() {
jaroslav@424
   259
        assert new Factorial().factorial(2) == 2;
jaroslav@424
   260
    }
jaroslav@424
   261
    
jaroslav@424
   262
    @KOTest public void factorial3() {
jaroslav@424
   263
        assert new Factorial().factorial(3) == 6;
jaroslav@424
   264
    }
jaroslav@424
   265
    
jaroslav@424
   266
    @KOTest public void factorial4() {
jaroslav@424
   267
        assert new Factorial().factorial(4) == 24;
jaroslav@424
   268
    }
jaroslav@424
   269
    
jaroslav@424
   270
    @KOTest public void factorial5() {
jaroslav@424
   271
        assert new Factorial().factorial(5) == 120;
jaroslav@424
   272
    }
jaroslav@424
   273
    
jaroslav@424
   274
    @KOTest public void factorial6() {
jaroslav@424
   275
        assert new Factorial().factorial(6) == 720;
jaroslav@424
   276
    }
jaroslav@424
   277
    
jaroslav@446
   278
    @KOTest public void sumArray() {
jaroslav@446
   279
        int r = Bodies.sumArr(new Sum());
jaroslav@446
   280
        assert r == 6 : "Sum is six: " + r;
jaroslav@446
   281
    }
jaroslav@446
   282
    
jaroslav@469
   283
    @KOTest public void callLater() throws Exception{
jaroslav@519
   284
        final Fn.Presenter p = Fn.activePresenter();
jaroslav@519
   285
        if (p == null) {
jaroslav@519
   286
            return;
jaroslav@519
   287
        }
jaroslav@519
   288
        p.loadScript(new StringReader(
jaroslav@469
   289
            "if (typeof window === 'undefined') window = {};"
jaroslav@469
   290
        ));
jaroslav@469
   291
        Later l = new Later();
jaroslav@469
   292
        l.register();
jaroslav@519
   293
        p.loadScript(new StringReader(
jaroslav@469
   294
            "window.later();"
jaroslav@469
   295
        ));
jaroslav@529
   296
        for (int i = 0; i < 100 && l.call != 42; i++) {
jaroslav@529
   297
            Thread.sleep(50);
jaroslav@529
   298
        }
jaroslav@469
   299
        assert l.call == 42 : "Method was called: " + l.call;
jaroslav@469
   300
    }
jaroslav@469
   301
    
jaroslav@424
   302
    private static class R implements Runnable {
jaroslav@424
   303
        int cnt;
jaroslav@424
   304
        private final Thread initThread;
jaroslav@424
   305
        
jaroslav@424
   306
        public R() {
jaroslav@424
   307
            initThread = Thread.currentThread();
jaroslav@424
   308
        }
jaroslav@424
   309
jaroslav@424
   310
        @Override
jaroslav@424
   311
        public void run() {
jaroslav@424
   312
            assert initThread == Thread.currentThread() : "Expecting to run in " + initThread + " but running in " + Thread.currentThread();
jaroslav@424
   313
            cnt++;
jaroslav@424
   314
        }
jaroslav@424
   315
    }
jaroslav@424
   316
    
jaroslav@424
   317
    private static class C implements Callable<Boolean> {
jaroslav@424
   318
        @Override
jaroslav@424
   319
        public Boolean call() throws Exception {
jaroslav@424
   320
            return Boolean.TRUE;
jaroslav@424
   321
        }
jaroslav@424
   322
    }
jaroslav@424
   323
}