ko/bck2brwsr/src/test/java/org/apidesign/bck2brwsr/ko2brwsr/Bck2BrwsrKnockoutTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 12 Jul 2013 14:51:33 +0200
changeset 1246 156c4911e45b
parent 1228 462dd9238173
child 1253 a936dd8280dc
permissions -rw-r--r--
Fixing imports
     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.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.apidesign.html.context.spi.Contexts;
    29 import org.apidesign.html.json.spi.Technology;
    30 import org.apidesign.html.json.spi.Transfer;
    31 import org.apidesign.html.json.tck.KOTest;
    32 import org.apidesign.html.json.tck.KnockoutTCK;
    33 import org.openide.util.lookup.ServiceProvider;
    34 import org.testng.annotations.Factory;
    35 
    36 /**
    37  *
    38  * @author Jaroslav Tulach <jtulach@netbeans.org>
    39  */
    40 @ServiceProvider(service = KnockoutTCK.class)
    41 public final class Bck2BrwsrKnockoutTest extends KnockoutTCK {
    42     @Factory public static Object[] create() {
    43         return VMTest.newTests().
    44             withClasses(testClasses()).
    45             withLaunchers("bck2brwsr").
    46             withTestAnnotation(KOTest.class).
    47             build();
    48     }
    49     
    50     @Override
    51     public BrwsrCtx createContext() {
    52         return Contexts.newBuilder().
    53             register(Transfer.class, BrwsrCtxImpl.DEFAULT, 9).
    54             register(Technology.class, BrwsrCtxImpl.DEFAULT, 9).build();
    55     }
    56 
    57 
    58     
    59     @Override
    60     public Object createJSON(Map<String, Object> values) {
    61         Object json = createJSON();
    62         
    63         for (Map.Entry<String, Object> entry : values.entrySet()) {
    64             putValue(json, entry.getKey(), entry.getValue());
    65         }
    66         return json;
    67     }
    68 
    69     @JavaScriptBody(args = {}, body = "return new Object();")
    70     private static native Object createJSON();
    71 
    72     @JavaScriptBody(args = { "json", "key", "value" }, body = "json[key] = value;")
    73     private static native void putValue(Object json, String key, Object value);
    74 
    75     @Override
    76     public Object executeScript(String script, Object[] arguments) {
    77         return execScript(script, arguments);
    78     }
    79     
    80     @JavaScriptBody(args = { "s", "args" }, body = 
    81         "var f = new Function(s); return f.apply(null, args);"
    82     )
    83     private static native Object execScript(String s, Object[] arguments);
    84     
    85     @JavaScriptBody(args = {  }, body = 
    86           "var h;"
    87         + "if (!!window && !!window.location && !!window.location.href)\n"
    88         + "  h = window.location.href;\n"
    89         + "else "
    90         + "  h = null;"
    91         + "return h;\n"
    92     )
    93     private static native String findBaseURL();
    94     
    95     @Override
    96     public URI prepareURL(String content, String mimeType, String[] parameters) {
    97         try {
    98             final URL baseURL = new URL(findBaseURL());
    99             StringBuilder sb = new StringBuilder();
   100             sb.append("/dynamic?mimeType=").append(mimeType);
   101             for (int i = 0; i < parameters.length; i++) {
   102                 sb.append("&param" + i).append("=").append(parameters[i]);
   103             }
   104             String mangle = content.replace("\n", "%0a")
   105                 .replace("\"", "\\\"").replace(" ", "%20");
   106             sb.append("&content=").append(mangle);
   107 
   108             URL query = new URL(baseURL, sb.toString());
   109             String uri = (String) query.getContent(new Class[] { String.class });
   110             URI connectTo = new URI(uri.trim());
   111             return connectTo;
   112         } catch (IOException ex) {
   113             throw new IllegalStateException(ex);
   114         } catch (URISyntaxException ex) {
   115             throw new IllegalStateException(ex);
   116         }
   117     }    
   118 }