ko/fx/src/test/java/org/apidesign/bck2brwsr/kofx/KnockoutFXTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 14 Jun 2016 06:11:20 +0200
branchLibraries
changeset 1977 46efebc7e220
parent 1787 ea12a3bb4b33
permissions -rw-r--r--
We need to keep Function.Ax classes in the VM
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, version 2 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://opensource.org/licenses/GPL-2.0.
    17  */
    18 package org.apidesign.bck2brwsr.kofx;
    19 
    20 import java.io.BufferedReader;
    21 import java.io.IOException;
    22 import java.io.InputStreamReader;
    23 import java.net.URI;
    24 import java.net.URISyntaxException;
    25 import java.net.URL;
    26 import java.net.URLConnection;
    27 import java.util.Map;
    28 import java.util.concurrent.Executor;
    29 import net.java.html.BrwsrCtx;
    30 import net.java.html.js.JavaScriptBody;
    31 import org.apidesign.bck2brwsr.vmtest.VMTest;
    32 import org.netbeans.html.boot.spi.Fn;
    33 import org.netbeans.html.context.spi.Contexts;
    34 import org.netbeans.html.json.spi.Technology;
    35 import org.netbeans.html.json.spi.Transfer;
    36 import org.netbeans.html.json.spi.WSTransfer;
    37 import org.netbeans.html.json.tck.KOTest;
    38 import org.netbeans.html.json.tck.KnockoutTCK;
    39 import org.netbeans.html.ko4j.KO4J;
    40 import org.netbeans.html.wstyrus.TyrusContext;
    41 import org.openide.util.lookup.ServiceProvider;
    42 import org.testng.annotations.Factory;
    43 
    44 /**
    45  *
    46  * @author Jaroslav Tulach <jtulach@netbeans.org>
    47  */
    48 @ServiceProvider(service = KnockoutTCK.class)
    49 public final class KnockoutFXTest extends KnockoutTCK {
    50     public KnockoutFXTest() {
    51     }
    52 
    53     @Factory public static Object[] compatibilityTests() {
    54         return VMTest.newTests().
    55             withClasses(filterTestClasses()).
    56             withTestAnnotation(KOTest.class).
    57             withLaunchers("fxbrwsr").build();
    58     }
    59     
    60     private static Class[] filterTestClasses() {
    61         Class[] arr = testClasses();
    62         for (int i = 0; i < arr.length; i++) {
    63             if (arr[i].getName().contains("GCBodyTest")) {
    64                 arr[i] = Object.class;
    65             }
    66         }
    67         return arr;
    68     }
    69     
    70     @Override
    71     public BrwsrCtx createContext() {
    72         final Fn.Presenter p = Fn.activePresenter();
    73         KO4J ko = new KO4J(p);
    74         TyrusContext tc = new TyrusContext();
    75         Contexts.Builder b = Contexts.newBuilder().
    76             register(Technology.class, ko.knockout(), 10).
    77             register(Transfer.class, ko.transfer(), 10);
    78         if (p instanceof Executor) {
    79             b.register(Executor.class, (Executor)p, 10);
    80         }
    81         try {
    82             Class.forName("java.util.function.Function");
    83             // prefer WebView's WebSockets on JDK8
    84             b.register(WSTransfer.class, ko.websockets(), 10);
    85         } catch (ClassNotFoundException ex) {
    86             // ok, JDK7 needs tyrus
    87             b.register(WSTransfer.class, tc, 20);
    88             b.register(Transfer.class, tc, 5);
    89         }
    90         return b.build();
    91     }
    92 
    93     @Override
    94     public Object createJSON(Map<String, Object> values) {
    95         Object json = createJSON();
    96         for (Map.Entry<String, Object> entry : values.entrySet()) {
    97             setProperty(json, entry.getKey(), entry.getValue());
    98         }
    99         return json;
   100     }
   101     
   102     @JavaScriptBody(args = {}, body = "return new Object();")
   103     private static native Object createJSON();
   104     @JavaScriptBody(args = { "json", "key", "value" }, body = "json[key] = value;")
   105     private static native void setProperty(Object json, String key, Object value);
   106 
   107     @Override
   108     @JavaScriptBody(args = { "s", "args" }, body = ""
   109         + "var f = new Function(s); "
   110         + "return f.apply(null, args);"
   111     )
   112     public native Object executeScript(String script, Object[] arguments);
   113 
   114     @JavaScriptBody(args = {  }, body = 
   115           "var h;"
   116         + "if (!!window && !!window.location && !!window.location.href)\n"
   117         + "  h = window.location.href;\n"
   118         + "else "
   119         + "  h = null;"
   120         + "return h;\n"
   121     )
   122     private static native String findBaseURL();
   123     
   124     @Override
   125     public URI prepareURL(String content, String mimeType, String[] parameters) {
   126         try {
   127             final URL baseURL = new URL(findBaseURL());
   128             StringBuilder sb = new StringBuilder();
   129             sb.append("/dynamic?mimeType=").append(mimeType);
   130             for (int i = 0; i < parameters.length; i++) {
   131                 sb.append("&param" + i).append("=").append(parameters[i]);
   132             }
   133             String mangle = content.replace("\n", "%0a")
   134                 .replace("\"", "\\\"").replace(" ", "%20");
   135             sb.append("&content=").append(mangle);
   136 
   137             URL query = new URL(baseURL, sb.toString());
   138             URLConnection c = query.openConnection();
   139             BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
   140             URI connectTo = new URI(br.readLine());
   141             return connectTo;
   142         } catch (IOException ex) {
   143             throw new IllegalStateException(ex);
   144         } catch (URISyntaxException ex) {
   145             throw new IllegalStateException(ex);
   146         }
   147     }
   148 }