ko/fx/src/test/java/org/apidesign/bck2brwsr/kofx/KnockoutFXTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 09 Jan 2014 21:21:34 +0100
branchNbHtml4J
changeset 1429 66358a11c016
parent 1424 095ce896e094
child 1481 3680747f6a19
permissions -rw-r--r--
Need to implement FromJavaScript interface
     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.boot.spi.Fn;
    32 import org.apidesign.html.context.spi.Contexts;
    33 import org.apidesign.html.json.spi.Technology;
    34 import org.apidesign.html.json.spi.Transfer;
    35 import org.apidesign.html.json.spi.WSTransfer;
    36 import org.apidesign.html.json.tck.KOTest;
    37 import org.apidesign.html.json.tck.KnockoutTCK;
    38 import org.json.JSONException;
    39 import org.json.JSONObject;
    40 import org.netbeans.html.ko4j.KO4J;
    41 import org.netbeans.html.wstyrus.TyrusContext;
    42 import org.openide.util.lookup.ServiceProvider;
    43 import org.testng.annotations.Factory;
    44 
    45 /**
    46  *
    47  * @author Jaroslav Tulach <jtulach@netbeans.org>
    48  */
    49 @ServiceProvider(service = KnockoutTCK.class)
    50 public final class KnockoutFXTest extends KnockoutTCK {
    51     public KnockoutFXTest() {
    52     }
    53 
    54     @Factory public static Object[] compatibilityTests() {
    55         return VMTest.newTests().
    56             withClasses(testClasses()).
    57             withTestAnnotation(KOTest.class).
    58             withLaunchers("fxbrwsr").build();
    59     }
    60 
    61     @Override
    62     public BrwsrCtx createContext() {
    63         KO4J ko = new KO4J(Fn.activePresenter());
    64         TyrusContext tc = new TyrusContext();
    65         Contexts.Builder b = Contexts.newBuilder().
    66             register(Technology.class, ko.knockout(), 10).
    67             register(Transfer.class, ko.transfer(), 10);
    68         try {
    69             Class.forName("java.util.function.Function");
    70             // prefer WebView's WebSockets on JDK8
    71             b.register(WSTransfer.class, ko.websockets(), 10);
    72         } catch (ClassNotFoundException ex) {
    73             // ok, JDK7 needs tyrus
    74             b.register(WSTransfer.class, tc, 20);
    75             b.register(Transfer.class, tc, 5);
    76         }
    77         return b.build();
    78     }
    79 
    80     @Override
    81     public Object createJSON(Map<String, Object> values) {
    82         Object json = createJSON();
    83         for (Map.Entry<String, Object> entry : values.entrySet()) {
    84             setProperty(json, entry.getKey(), entry.getValue());
    85         }
    86         return json;
    87     }
    88     
    89     @JavaScriptBody(args = {}, body = "return new Object();")
    90     private static native Object createJSON();
    91     @JavaScriptBody(args = { "json", "key", "value" }, body = "json[key] = value;")
    92     private static native void setProperty(Object json, String key, Object value);
    93 
    94     @Override
    95     @JavaScriptBody(args = { "s", "args" }, body = ""
    96         + "var f = new Function(s); "
    97         + "return f.apply(null, args);"
    98     )
    99     public native Object executeScript(String script, Object[] arguments);
   100 
   101     @JavaScriptBody(args = {  }, body = 
   102           "var h;"
   103         + "if (!!window && !!window.location && !!window.location.href)\n"
   104         + "  h = window.location.href;\n"
   105         + "else "
   106         + "  h = null;"
   107         + "return h;\n"
   108     )
   109     private static native String findBaseURL();
   110     
   111     @Override
   112     public URI prepareURL(String content, String mimeType, String[] parameters) {
   113         try {
   114             final URL baseURL = new URL(findBaseURL());
   115             StringBuilder sb = new StringBuilder();
   116             sb.append("/dynamic?mimeType=").append(mimeType);
   117             for (int i = 0; i < parameters.length; i++) {
   118                 sb.append("&param" + i).append("=").append(parameters[i]);
   119             }
   120             String mangle = content.replace("\n", "%0a")
   121                 .replace("\"", "\\\"").replace(" ", "%20");
   122             sb.append("&content=").append(mangle);
   123 
   124             URL query = new URL(baseURL, sb.toString());
   125             URLConnection c = query.openConnection();
   126             BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
   127             URI connectTo = new URI(br.readLine());
   128             return connectTo;
   129         } catch (IOException ex) {
   130             throw new IllegalStateException(ex);
   131         } catch (URISyntaxException ex) {
   132             throw new IllegalStateException(ex);
   133         }
   134     }
   135 }