ko/fx/src/test/java/org/apidesign/bck2brwsr/kofx/KnockoutFXTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 26 Jun 2013 20:27:06 +0200
branchclassloader
changeset 1233 43fba26ba0c0
parent 1230 466c30fd9cb0
child 1249 cdaeea7becf2
permissions -rw-r--r--
Using the net.java.html's ko-fx implementation
     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.tck.KOTest;
    35 import org.apidesign.html.json.tck.KnockoutTCK;
    36 import org.apidesign.html.kofx.FXContext;
    37 import org.json.JSONException;
    38 import org.json.JSONObject;
    39 import org.openide.util.lookup.ServiceProvider;
    40 import org.testng.annotations.Factory;
    41 
    42 /**
    43  *
    44  * @author Jaroslav Tulach <jtulach@netbeans.org>
    45  */
    46 @ServiceProvider(service = KnockoutTCK.class)
    47 public final class KnockoutFXTest extends KnockoutTCK {
    48     public KnockoutFXTest() {
    49     }
    50 
    51     @Factory public static Object[] compatibilityTests() {
    52         return VMTest.newTests().
    53             withClasses(testClasses()).
    54             withTestAnnotation(KOTest.class).
    55             withLaunchers("fxbrwsr").build();
    56     }
    57 
    58     @Override
    59     public BrwsrCtx createContext() {
    60         FXContext fx = new FXContext();
    61         return Contexts.newBuilder().
    62             register(Technology.class, fx, 10).
    63             register(Transfer.class, fx, 10).
    64             build();
    65     }
    66 
    67     @Override
    68     public Object createJSON(Map<String, Object> values) {
    69         JSONObject json = new JSONObject();
    70         for (Map.Entry<String, Object> entry : values.entrySet()) {
    71             try {
    72                 json.put(entry.getKey(), entry.getValue());
    73             } catch (JSONException ex) {
    74                 throw new IllegalStateException(ex);
    75             }
    76         }
    77         return json;
    78     }
    79 
    80     @Override
    81     @JavaScriptBody(args = { "s", "args" }, body = ""
    82         + "var f = new Function(s); "
    83         + "return f.apply(null, args);"
    84     )
    85     public native Object executeScript(String script, Object[] arguments);
    86 
    87     @JavaScriptBody(args = {  }, body = 
    88           "var h;"
    89         + "if (!!window && !!window.location && !!window.location.href)\n"
    90         + "  h = window.location.href;\n"
    91         + "else "
    92         + "  h = null;"
    93         + "return h;\n"
    94     )
    95     private static native String findBaseURL();
    96     
    97     @Override
    98     public URI prepareURL(String content, String mimeType, String[] parameters) {
    99         try {
   100             final URL baseURL = new URL(findBaseURL());
   101             StringBuilder sb = new StringBuilder();
   102             sb.append("/dynamic?mimeType=").append(mimeType);
   103             for (int i = 0; i < parameters.length; i++) {
   104                 sb.append("&param" + i).append("=").append(parameters[i]);
   105             }
   106             String mangle = content.replace("\n", "%0a")
   107                 .replace("\"", "\\\"").replace(" ", "%20");
   108             sb.append("&content=").append(mangle);
   109 
   110             URL query = new URL(baseURL, sb.toString());
   111             URLConnection c = query.openConnection();
   112             BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
   113             URI connectTo = new URI(br.readLine());
   114             return connectTo;
   115         } catch (IOException ex) {
   116             throw new IllegalStateException(ex);
   117         } catch (URISyntaxException ex) {
   118             throw new IllegalStateException(ex);
   119         }
   120     }
   121 }