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