ko/fx/src/test/java/org/apidesign/bck2brwsr/kofx/KnockoutFXTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 26 Aug 2013 08:56:37 +0200
changeset 1249 cdaeea7becf2
parent 1233 43fba26ba0c0
child 1282 8d29792a09c6
permissions -rw-r--r--
First updates to run with forthcoming version 0.5 - WebSockets in FX launcher and etc.
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 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 net.java.html.BrwsrCtx;
    29 import net.java.html.js.JavaScriptBody;
    30 import org.apidesign.bck2brwsr.vmtest.VMTest;
    31 import org.apidesign.html.context.spi.Contexts;
    32 import org.apidesign.html.json.spi.Technology;
    33 import org.apidesign.html.json.spi.Transfer;
    34 import org.apidesign.html.json.spi.WSTransfer;
    35 import org.apidesign.html.json.tck.KOTest;
    36 import org.apidesign.html.json.tck.KnockoutTCK;
    37 import org.apidesign.html.kofx.FXContext;
    38 import org.apidesign.html.wstyrus.TyrusContext;
    39 import org.json.JSONException;
    40 import org.json.JSONObject;
    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(testClasses()).
    56             withTestAnnotation(KOTest.class).
    57             withLaunchers("fxbrwsr").build();
    58     }
    59 
    60     @Override
    61     public BrwsrCtx createContext() {
    62         FXContext fx = new FXContext();
    63         TyrusContext tc = new TyrusContext();
    64         Contexts.Builder b = Contexts.newBuilder().
    65             register(Technology.class, fx, 10).
    66             register(Transfer.class, fx, 10);
    67         try {
    68             Class.forName("java.util.function.Function");
    69             // prefer WebView's WebSockets on JDK8
    70             b.register(WSTransfer.class, fx, 10);
    71         } catch (ClassNotFoundException ex) {
    72             // ok, JDK7 needs tyrus
    73             b.register(WSTransfer.class, tc, 20);
    74         }
    75         return b.build();
    76     }
    77 
    78     @Override
    79     public Object createJSON(Map<String, Object> values) {
    80         JSONObject json = new JSONObject();
    81         for (Map.Entry<String, Object> entry : values.entrySet()) {
    82             try {
    83                 json.put(entry.getKey(), entry.getValue());
    84             } catch (JSONException ex) {
    85                 throw new IllegalStateException(ex);
    86             }
    87         }
    88         return json;
    89     }
    90 
    91     @Override
    92     @JavaScriptBody(args = { "s", "args" }, body = ""
    93         + "var f = new Function(s); "
    94         + "return f.apply(null, args);"
    95     )
    96     public native Object executeScript(String script, Object[] arguments);
    97 
    98     @JavaScriptBody(args = {  }, body = 
    99           "var h;"
   100         + "if (!!window && !!window.location && !!window.location.href)\n"
   101         + "  h = window.location.href;\n"
   102         + "else "
   103         + "  h = null;"
   104         + "return h;\n"
   105     )
   106     private static native String findBaseURL();
   107     
   108     @Override
   109     public URI prepareURL(String content, String mimeType, String[] parameters) {
   110         try {
   111             final URL baseURL = new URL(findBaseURL());
   112             StringBuilder sb = new StringBuilder();
   113             sb.append("/dynamic?mimeType=").append(mimeType);
   114             for (int i = 0; i < parameters.length; i++) {
   115                 sb.append("&param" + i).append("=").append(parameters[i]);
   116             }
   117             String mangle = content.replace("\n", "%0a")
   118                 .replace("\"", "\\\"").replace(" ", "%20");
   119             sb.append("&content=").append(mangle);
   120 
   121             URL query = new URL(baseURL, sb.toString());
   122             URLConnection c = query.openConnection();
   123             BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
   124             URI connectTo = new URI(br.readLine());
   125             return connectTo;
   126         } catch (IOException ex) {
   127             throw new IllegalStateException(ex);
   128         } catch (URISyntaxException ex) {
   129             throw new IllegalStateException(ex);
   130         }
   131     }
   132 }