ko/bck2brwsr/src/test/java/org/apidesign/bck2brwsr/ko2brwsr/Bck2BrwsrKnockoutTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 24 Feb 2015 11:12:53 +0100
changeset 1787 ea12a3bb4b33
parent 1749 0417c5f323fd
permissions -rw-r--r--
Using year range 2012-2015 in copyright header
     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.ko2brwsr;
    19 
    20 import java.io.IOException;
    21 import java.net.URI;
    22 import java.net.URISyntaxException;
    23 import java.net.URL;
    24 import java.util.Map;
    25 import net.java.html.BrwsrCtx;
    26 import org.apidesign.bck2brwsr.core.JavaScriptBody;
    27 import org.apidesign.bck2brwsr.vmtest.VMTest;
    28 import org.netbeans.html.context.spi.Contexts;
    29 import org.netbeans.html.json.spi.Technology;
    30 import org.netbeans.html.json.spi.Transfer;
    31 import org.netbeans.html.json.spi.WSTransfer;
    32 import org.netbeans.html.json.tck.KOTest;
    33 import org.netbeans.html.json.tck.KnockoutTCK;
    34 import org.netbeans.html.ko4j.KO4J;
    35 import org.openide.util.lookup.ServiceProvider;
    36 import org.testng.annotations.Factory;
    37 
    38 /**
    39  *
    40  * @author Jaroslav Tulach <jtulach@netbeans.org>
    41  */
    42 @ServiceProvider(service = KnockoutTCK.class)
    43 public final class Bck2BrwsrKnockoutTest extends KnockoutTCK {
    44     @Factory public static Object[] create() {
    45         final Class<?>[] arr = testClasses();
    46         for (int i = 0; i < arr.length; i++) {
    47             if (arr[i].getSimpleName().startsWith("GC")) {
    48                 arr[i] = Object.class;
    49             }
    50         }
    51         return VMTest.newTests().
    52             withClasses(arr).
    53             withLaunchers("bck2brwsr").
    54             withTestAnnotation(KOTest.class).
    55             build();
    56     }
    57     
    58     @Override
    59     public BrwsrCtx createContext() {
    60         KO4J ko = new KO4J(null);
    61         return Contexts.newBuilder().
    62             register(Transfer.class, ko.transfer(), 9).
    63             register(WSTransfer.class, ko.websockets(), 9).
    64             register(Technology.class, ko.knockout(), 9).build();
    65     }
    66 
    67 
    68     
    69     @Override
    70     public Object createJSON(Map<String, Object> values) {
    71         Object json = createJSON();
    72         
    73         for (Map.Entry<String, Object> entry : values.entrySet()) {
    74             putValue(json, entry.getKey(), entry.getValue());
    75         }
    76         return json;
    77     }
    78 
    79     @JavaScriptBody(args = {}, body = "return new Object();")
    80     private static native Object createJSON();
    81 
    82     @JavaScriptBody(args = { "json", "key", "value" }, body = "json[key] = value;")
    83     private static native void putValue(Object json, String key, Object value);
    84 
    85     @Override
    86     public Object executeScript(String script, Object[] arguments) {
    87         return execScript(script, arguments);
    88     }
    89     
    90     @JavaScriptBody(args = { "s", "args" }, body = 
    91         "var f = new Function(s); return f.apply(null, args);"
    92     )
    93     private static native Object execScript(String s, Object[] arguments);
    94     
    95     @JavaScriptBody(args = {  }, body = 
    96           "var h;"
    97         + "if (!!window && !!window.location && !!window.location.href)\n"
    98         + "  h = window.location.href;\n"
    99         + "else "
   100         + "  h = null;"
   101         + "return h;\n"
   102     )
   103     private static native String findBaseURL();
   104     
   105     @Override
   106     public URI prepareURL(String content, String mimeType, String[] parameters) {
   107         try {
   108             final URL baseURL = new URL(findBaseURL());
   109             StringBuilder sb = new StringBuilder();
   110             sb.append("/dynamic?mimeType=").append(mimeType);
   111             for (int i = 0; i < parameters.length; i++) {
   112                 sb.append("&param" + i).append("=").append(parameters[i]);
   113             }
   114             String mangle = content.replace("\n", "%0a")
   115                 .replace("\"", "\\\"").replace(" ", "%20");
   116             sb.append("&content=").append(mangle);
   117 
   118             URL query = new URL(baseURL, sb.toString());
   119             String uri = (String) query.getContent(new Class[] { String.class });
   120             URI connectTo = new URI(uri.trim());
   121             return connectTo;
   122         } catch (IOException ex) {
   123             throw new IllegalStateException(ex);
   124         } catch (URISyntaxException ex) {
   125             throw new IllegalStateException(ex);
   126         }
   127     }    
   128 }