json-tck/src/main/java/net/java/html/js/tests/JavaScriptBodyTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Tue, 26 Aug 2014 18:13:30 +0200
changeset 838 bdc3d696dd4a
parent 790 30f20d9c0986
child 848 511b38dde6b9
permissions -rw-r--r--
During the API review process (bug 246133) the reviewers decided that in order to include html4j to NetBeans Platform, we need to stop using org.apidesign namespace and switch to NetBeans one. Repackaging all SPI packages into org.netbeans.html.smthng.spi.
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;
jtulach@838
    48
import org.netbeans.html.boot.spi.Fn;
jtulach@838
    49
import org.netbeans.html.json.tck.KOTest;
jaroslav@424
    50
jaroslav@424
    51
/**
jaroslav@424
    52
 *
jtulach@790
    53
 * @author Jaroslav Tulach
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@577
    80
    private R asyncRun;
jaroslav@577
    81
    @KOTest public void asyncCallbackToRunnable() throws InterruptedException {
jaroslav@577
    82
        if (asyncRun == null) {
jaroslav@577
    83
            asyncRun = new R();
jaroslav@577
    84
            Bodies.asyncCallback(asyncRun);
jaroslav@577
    85
        }
jaroslav@577
    86
        if (asyncRun.cnt == 0) {
jaroslav@577
    87
            throw new InterruptedException();
jaroslav@577
    88
        }
jaroslav@577
    89
        assert asyncRun.cnt == 1 : "Even async callback must arrive once: " + asyncRun.cnt;
jaroslav@577
    90
    }
jaroslav@595
    91
jaroslav@595
    92
    @KOTest public void asyncCallbackFlushed() throws InterruptedException {
jaroslav@595
    93
        R r = new R();
jaroslav@595
    94
        for (int i = 0; i < 10; i++) {
jaroslav@595
    95
            Bodies.asyncCallback(r);
jaroslav@595
    96
        }
jaroslav@595
    97
        int fourtyTwo = Bodies.sum(35, 7);
jaroslav@595
    98
        assert r.cnt == 10 : "Ten calls: " + r.cnt;
jaroslav@595
    99
        assert fourtyTwo == 42 : "Meaning of the world expected: " + fourtyTwo;
jaroslav@595
   100
    }
jaroslav@577
   101
    
jaroslav@436
   102
    @KOTest public void typeOfCharacter() {
jaroslav@436
   103
        String charType = Bodies.typeof('a', false);
jaroslav@436
   104
        assert "number".equals(charType) : "Expecting number type: " + charType;
jaroslav@436
   105
    }
jaroslav@436
   106
    @KOTest public void typeOfBoolean() {
jaroslav@436
   107
        String booleanType = Bodies.typeof(true, false);
jaroslav@436
   108
        assert "boolean".equals(booleanType) : "Expecting boolean type: " + booleanType;
jaroslav@436
   109
    }
jaroslav@436
   110
jaroslav@436
   111
    @KOTest public void typeOfPrimitiveBoolean() {
jaroslav@436
   112
        String booleanType = Bodies.typeof(true);
jaroslav@440
   113
        assert "boolean".equals(booleanType) || "number".equals(booleanType): 
jaroslav@440
   114
            "Expecting boolean or at least number type: " + booleanType;
jaroslav@436
   115
    }
jaroslav@436
   116
jaroslav@436
   117
    @KOTest public void typeOfInteger() {
jaroslav@436
   118
        String intType = Bodies.typeof(1, false);
jaroslav@436
   119
        assert "number".equals(intType) : "Expecting number type: " + intType;
jaroslav@436
   120
    }
jaroslav@436
   121
jaroslav@436
   122
    @KOTest public void typeOfString() {
jaroslav@436
   123
        String strType = Bodies.typeof("Ahoj", false);
jaroslav@436
   124
        assert "string".equals(strType) : "Expecting string type: " + strType;
jaroslav@436
   125
    }
jaroslav@436
   126
jaroslav@436
   127
    @KOTest public void typeOfDouble() {
jaroslav@436
   128
        String doubleType = Bodies.typeof(0.33, false);
jaroslav@436
   129
        assert "number".equals(doubleType) : "Expecting number type: " + doubleType;
jaroslav@436
   130
    }
jaroslav@436
   131
    
jaroslav@436
   132
    @KOTest public void typeOfBooleanValueOf() {
jaroslav@436
   133
        String booleanType = Bodies.typeof(true, true);
jaroslav@436
   134
        assert "boolean".equals(booleanType) : "Expecting boolean type: " + booleanType;
jaroslav@436
   135
    }
jaroslav@436
   136
jaroslav@436
   137
    @KOTest public void typeOfIntegerValueOf() {
jaroslav@436
   138
        String intType = Bodies.typeof(1, true);
jaroslav@436
   139
        assert "number".equals(intType) : "Expecting number type: " + intType;
jaroslav@436
   140
    }
jaroslav@436
   141
jaroslav@436
   142
    @KOTest public void typeOfStringValueOf() {
jaroslav@436
   143
        String strType = Bodies.typeof("Ahoj", true);
jaroslav@436
   144
        assert "string".equals(strType) : "Expecting string type: " + strType;
jaroslav@436
   145
    }
jaroslav@436
   146
jaroslav@436
   147
    @KOTest public void typeOfDoubleValueOf() {
jaroslav@436
   148
        String doubleType = Bodies.typeof(0.33, true);
jaroslav@436
   149
        assert "number".equals(doubleType) : "Expecting number type: " + doubleType;
jaroslav@436
   150
    }
jaroslav@424
   151
jaroslav@424
   152
    @KOTest public void computeInARunnable() {
jaroslav@424
   153
        final int[] sum = new int[2];
jaroslav@424
   154
        class First implements Runnable {
jaroslav@424
   155
            @Override public void run() {
jaroslav@424
   156
                sum[0] = Bodies.sum(22, 20);
jaroslav@424
   157
                sum[1] = Bodies.sum(32, 10);
jaroslav@424
   158
            }
jaroslav@424
   159
        }
jaroslav@424
   160
        Bodies.callback(new First());
jaroslav@424
   161
        assert sum[0] == 42 : "Computed OK " + sum[0];
jaroslav@424
   162
        assert sum[1] == 42 : "Computed OK too: " + sum[1];
jaroslav@424
   163
    }
jaroslav@424
   164
    
jaroslav@424
   165
    @KOTest public void doubleCallbackToRunnable() {
jaroslav@424
   166
        final R run = new R();
jaroslav@424
   167
        final R r2 = new R();
jaroslav@424
   168
        class First implements Runnable {
jaroslav@424
   169
            @Override public void run() {
jaroslav@424
   170
                Bodies.callback(run);
jaroslav@424
   171
                Bodies.callback(r2);
jaroslav@424
   172
            }
jaroslav@424
   173
        }
jaroslav@424
   174
        Bodies.callback(new First());
jaroslav@424
   175
        assert run.cnt == 1 : "Can call even private implementation classes: " + run.cnt;
jaroslav@424
   176
        assert r2.cnt == 1 : "Can call even private implementation classes: " + r2.cnt;
jaroslav@424
   177
    }
jaroslav@424
   178
    
jaroslav@424
   179
    @KOTest public void identity() {
jaroslav@424
   180
        Object p = new Object();
jaroslav@424
   181
        Object r = Bodies.id(p);
jaroslav@424
   182
        assert r == p : "The object is the same";
jaroslav@424
   183
    }
jaroslav@424
   184
jaroslav@424
   185
    @KOTest public void encodingString() {
jaroslav@424
   186
        Object p = "Ji\n\"Hi\"\nHon";
jaroslav@424
   187
        Object r = Bodies.id(p);
jaroslav@424
   188
        assert p.equals(r) : "The object is the same: " + p + " != " + r;
jaroslav@424
   189
    }
jaroslav@424
   190
jaroslav@523
   191
    @KOTest public void encodingBackslashString() {
jaroslav@523
   192
        Object p = "{\"firstName\":\"/*\\n * Copyright (c) 2013\",\"lastName\":null,\"sex\":\"MALE\",\"address\":{\"street\":null}}";
jaroslav@523
   193
        Object r = Bodies.id(p);
jaroslav@523
   194
        assert p.equals(r) : "The object is the same: " + p + " != " + r;
jaroslav@523
   195
    }
jaroslav@523
   196
jaroslav@424
   197
    @KOTest public void nullIsNull() {
jaroslav@424
   198
        Object p = null;
jaroslav@424
   199
        Object r = Bodies.id(p);
jaroslav@424
   200
        assert r == p : "The null is the same";
jaroslav@424
   201
    }
jaroslav@424
   202
    
jaroslav@424
   203
    @KOTest public void callbackWithResult() {
jaroslav@424
   204
        Callable<Boolean> c = new C();
jaroslav@424
   205
        Object b = Bodies.callback(c);
jaroslav@424
   206
        assert b == Boolean.TRUE : "Should return true";
jaroslav@424
   207
    }
jaroslav@424
   208
    
jaroslav@424
   209
    @KOTest public void callbackWithParameters() {
jaroslav@424
   210
        int res = Bodies.sumIndirect(new Sum());
jaroslav@424
   211
        assert res == 42 : "Expecting 42";
jaroslav@424
   212
    }
jaroslav@424
   213
    
jaroslav@429
   214
    @KOTest public void selectFromStringJavaArray() {
jaroslav@424
   215
        String[] arr = { "Ahoj", "World" };
jaroslav@424
   216
        Object res = Bodies.select(arr, 1);
jaroslav@424
   217
        assert "World".equals(res) : "Expecting World, but was: " + res;
jaroslav@424
   218
    }
jaroslav@424
   219
jaroslav@429
   220
    @KOTest public void selectFromObjectJavaArray() {
jaroslav@429
   221
        Object[] arr = { new Object(), new Object() };
jaroslav@429
   222
        Object res = Bodies.select(arr, 1);
jaroslav@429
   223
        assert arr[1].equals(res) : "Expecting " + arr[1] + ", but was: " + res;
jaroslav@429
   224
    }
jaroslav@429
   225
jaroslav@424
   226
    @KOTest public void lengthOfJavaArray() {
jaroslav@424
   227
        String[] arr = { "Ahoj", "World" };
jaroslav@424
   228
        int res = Bodies.length(arr);
jaroslav@424
   229
        assert res == 2 : "Expecting 2, but was: " + res;
jaroslav@424
   230
    }
jaroslav@424
   231
jaroslav@429
   232
    @KOTest public void isJavaArray() {
jaroslav@429
   233
        String[] arr = { "Ahoj", "World" };
jaroslav@429
   234
        boolean is = Bodies.isArray(arr);
jaroslav@429
   235
        assert is: "Expecting it to be an array: " + is;
jaroslav@429
   236
    }
jaroslav@429
   237
jaroslav@429
   238
    @KOTest public void javaArrayInOutIsCopied() {
jaroslav@424
   239
        String[] arr = { "Ahoj", "World" };
jaroslav@424
   240
        Object res = Bodies.id(arr);
jaroslav@429
   241
        assert res != null : "Non-null is returned";
jaroslav@429
   242
        assert res instanceof Object[] : "Returned an array: " + res;
jaroslav@429
   243
        assert !(res instanceof String[]) : "Not returned a string array: " + res;
jaroslav@429
   244
        
jaroslav@429
   245
        Object[] ret = (Object[]) res;
jaroslav@429
   246
        assert arr.length == ret.length : "Same length: " + ret.length;
jaroslav@429
   247
        assert arr[0].equals(ret[0]) : "Same first elem";
jaroslav@429
   248
        assert arr[1].equals(ret[1]) : "Same 2nd elem";
jaroslav@424
   249
    }
jaroslav@424
   250
jaroslav@429
   251
    @KOTest public void modifyJavaArrayHasNoEffect() {
jaroslav@429
   252
        String[] arr = { "Ahoj", "World" };
jaroslav@429
   253
        String value = Bodies.modify(arr, 0, "Hello");
jaroslav@429
   254
        assert "Hello".equals(value) : "Inside JS the value is changed: " + value;
jaroslav@429
   255
        assert "Ahoj".equals(arr[0]) : "From a Java point of view it remains: " + arr[0];
jaroslav@429
   256
    }
jaroslav@424
   257
jaroslav@430
   258
    @KOTest
jaroslav@430
   259
    public void callbackWithArray() {
jaroslav@430
   260
        class A implements Callable<String[]> {
jaroslav@430
   261
            @Override
jaroslav@430
   262
            public String[] call() throws Exception {
jaroslav@430
   263
                return new String[] { "Hello" };
jaroslav@430
   264
            }
jaroslav@430
   265
        }
jaroslav@430
   266
        Callable<String[]> a = new A();
jaroslav@430
   267
        Object b = Bodies.callbackAndPush(a, "World!");
jaroslav@430
   268
        assert b instanceof Object[] : "Returns an array: " + b;
jaroslav@430
   269
        Object[] arr = (Object[]) b;
jaroslav@430
   270
        String str = Arrays.toString(arr);
jaroslav@430
   271
        assert arr.length == 2 : "Size is two " + str;
jaroslav@430
   272
        assert "Hello".equals(arr[0]) : "Hello expected: " + arr[0];
jaroslav@430
   273
        assert "World!".equals(arr[1]) : "World! expected: " + arr[1];
jaroslav@430
   274
    }
jtulach@710
   275
    
jtulach@710
   276
    @KOTest public void sumVector() {
jtulach@710
   277
        double[] arr = { 1.0, 2.0, 3.0 };
jtulach@710
   278
        double res = Bodies.sumVector(arr);
jtulach@710
   279
        assert 6.0 == res : "Expecting six: " + res;
jtulach@710
   280
    }
jtulach@710
   281
jtulach@710
   282
    @KOTest public void sumMatrix() {
jtulach@710
   283
        double[][] arr = { { 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0, 1.0 } };
jtulach@710
   284
        double res = Bodies.sumMatrix(arr);
jtulach@710
   285
        assert 6.0 == res : "Expecting six: " + res;
jtulach@710
   286
    }
jaroslav@430
   287
jaroslav@424
   288
    @KOTest public void truth() {
jaroslav@424
   289
        assert Bodies.truth() : "True is true";
jaroslav@424
   290
    }
jaroslav@424
   291
    
jaroslav@424
   292
    @KOTest public void factorial2() {
jaroslav@424
   293
        assert new Factorial().factorial(2) == 2;
jaroslav@424
   294
    }
jaroslav@424
   295
    
jaroslav@424
   296
    @KOTest public void factorial3() {
jaroslav@424
   297
        assert new Factorial().factorial(3) == 6;
jaroslav@424
   298
    }
jaroslav@424
   299
    
jaroslav@424
   300
    @KOTest public void factorial4() {
jaroslav@424
   301
        assert new Factorial().factorial(4) == 24;
jaroslav@424
   302
    }
jaroslav@424
   303
    
jaroslav@424
   304
    @KOTest public void factorial5() {
jaroslav@424
   305
        assert new Factorial().factorial(5) == 120;
jaroslav@424
   306
    }
jaroslav@424
   307
    
jaroslav@424
   308
    @KOTest public void factorial6() {
jaroslav@424
   309
        assert new Factorial().factorial(6) == 720;
jaroslav@424
   310
    }
jaroslav@424
   311
    
jaroslav@446
   312
    @KOTest public void sumArray() {
jaroslav@446
   313
        int r = Bodies.sumArr(new Sum());
jaroslav@446
   314
        assert r == 6 : "Sum is six: " + r;
jaroslav@446
   315
    }
jaroslav@446
   316
    
jaroslav@579
   317
    @KOTest public void staticCallback() {
jaroslav@579
   318
        int r = Bodies.staticCallback();
jaroslav@579
   319
        assert r == 42 : "Expecting 42: " + r;
jaroslav@579
   320
    }
jaroslav@579
   321
    
jaroslav@565
   322
    Later l;
jaroslav@469
   323
    @KOTest public void callLater() throws Exception{
jaroslav@519
   324
        final Fn.Presenter p = Fn.activePresenter();
jaroslav@519
   325
        if (p == null) {
jaroslav@519
   326
            return;
jaroslav@519
   327
        }
jaroslav@565
   328
        if (l == null) {
jaroslav@565
   329
            p.loadScript(new StringReader(
jaroslav@565
   330
                "if (typeof window === 'undefined') window = {};"
jaroslav@565
   331
            ));
jaroslav@565
   332
            l = new Later();
jaroslav@565
   333
            l.register();
jaroslav@565
   334
            p.loadScript(new StringReader(
jaroslav@565
   335
                "window.later();"
jaroslav@565
   336
            ));
jaroslav@565
   337
        }
jaroslav@565
   338
        if (l.call != 42) {
jaroslav@565
   339
            throw new InterruptedException();
jaroslav@529
   340
        }
jaroslav@469
   341
        assert l.call == 42 : "Method was called: " + l.call;
jaroslav@469
   342
    }
jaroslav@469
   343
    
jaroslav@424
   344
    private static class R implements Runnable {
jaroslav@424
   345
        int cnt;
jaroslav@424
   346
        private final Thread initThread;
jaroslav@424
   347
        
jaroslav@424
   348
        public R() {
jaroslav@424
   349
            initThread = Thread.currentThread();
jaroslav@424
   350
        }
jaroslav@424
   351
jaroslav@424
   352
        @Override
jaroslav@424
   353
        public void run() {
jaroslav@424
   354
            assert initThread == Thread.currentThread() : "Expecting to run in " + initThread + " but running in " + Thread.currentThread();
jaroslav@424
   355
            cnt++;
jaroslav@424
   356
        }
jaroslav@424
   357
    }
jaroslav@424
   358
    
jaroslav@424
   359
    private static class C implements Callable<Boolean> {
jaroslav@424
   360
        @Override
jaroslav@424
   361
        public Boolean call() throws Exception {
jaroslav@424
   362
            return Boolean.TRUE;
jaroslav@424
   363
        }
jaroslav@424
   364
    }
jaroslav@424
   365
}