ko4j registers implementation of Transfer and WSTransfer based on XHR and WebSocket from a browser. The Java implementations of these interfaces has been moved to ko-ws-tyrus module. JavaScript arrays passed as parameters to Java callback methods need to be wrapped.
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2013-2013 Oracle and/or its affiliates. All rights reserved.
6 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7 * Other names may be trademarks of their respective owners.
9 * The contents of this file are subject to the terms of either the GNU
10 * General Public License Version 2 only ("GPL") or the Common
11 * Development and Distribution License("CDDL") (collectively, the
12 * "License"). You may not use this file except in compliance with the
13 * License. You can obtain a copy of the License at
14 * http://www.netbeans.org/cddl-gplv2.html
15 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16 * specific language governing permissions and limitations under the
17 * License. When distributing the software, include this License Header
18 * Notice in each file and include the License file at
19 * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
20 * particular file as subject to the "Classpath" exception as provided
21 * by Oracle in the GPL Version 2 section of the License file that
22 * accompanied this code. If applicable, add the following below the
23 * License Header, with the fields enclosed by brackets [] replaced by
24 * your own identifying information:
25 * "Portions Copyrighted [year] [name of copyright owner]"
29 * The Original Software is NetBeans. The Initial Developer of the Original
30 * Software is Oracle. Portions Copyright 2013-2013 Oracle. All Rights Reserved.
32 * If you wish your version of this file to be governed by only the CDDL
33 * or only the GPL Version 2, indicate your decision by adding
34 * "[Contributor] elects to include this software in this distribution
35 * under the [CDDL or GPL Version 2] license." If you do not indicate a
36 * single choice of license, a recipient has the option to distribute
37 * your version of this file under either the CDDL, the GPL Version 2 or
38 * to extend the choice of license to its licensees as provided above.
39 * However, if you add GPL Version 2 code and therefore, elected the GPL
40 * Version 2 license, then the option applies only if the new code is
41 * made subject to such option by the copyright holder.
43 package org.netbeans.html.ko4j;
45 import java.io.BufferedReader;
46 import java.io.IOException;
47 import java.io.InputStreamReader;
48 import java.lang.annotation.Annotation;
49 import java.lang.reflect.Method;
51 import java.net.URISyntaxException;
53 import java.net.URLConnection;
54 import java.util.ArrayList;
55 import java.util.List;
57 import java.util.concurrent.Executors;
58 import net.java.html.BrwsrCtx;
59 import net.java.html.boot.BrowserBuilder;
60 import net.java.html.js.JavaScriptBody;
61 import org.netbeans.html.boot.impl.FnContext;
62 import org.apidesign.html.boot.spi.Fn;
63 import org.apidesign.html.context.spi.Contexts;
64 import org.apidesign.html.json.spi.Technology;
65 import org.apidesign.html.json.spi.Transfer;
66 import org.apidesign.html.json.spi.WSTransfer;
67 import org.apidesign.html.json.tck.KOTest;
68 import org.apidesign.html.json.tck.KnockoutTCK;
69 import org.openide.util.lookup.ServiceProvider;
70 import org.testng.annotations.Factory;
71 import static org.testng.Assert.*;
75 * @author Jaroslav Tulach <jtulach@netbeans.org>
77 @ServiceProvider(service = KnockoutTCK.class)
78 public final class KnockoutFXTest extends KnockoutTCK {
79 private static Class<?> browserClass;
80 private static Fn.Presenter browserContext;
82 public KnockoutFXTest() {
85 @Factory public static Object[] compatibilityTests() throws Exception {
86 Class[] arr = testClasses();
87 for (int i = 0; i < arr.length; i++) {
89 arr[i].getClassLoader(),
90 KnockoutFXTest.class.getClassLoader(),
91 "All classes loaded by the same classloader"
95 URI uri = DynamicHTTP.initServer();
97 final BrowserBuilder bb = BrowserBuilder.newBrowser().loadClass(KnockoutFXTest.class).
98 loadPage(uri.toString()).
99 invoke("initialized");
101 Executors.newSingleThreadExecutor().submit(new Runnable() {
108 ClassLoader l = getClassLoader();
109 List<Object> res = new ArrayList<Object>();
110 for (int i = 0; i < arr.length; i++) {
111 Class<?> c = Class.forName(arr[i].getName(), true, l);
114 Class<?> c = Class.forName(LessCallbacksCheck.class.getName(), true, l);
116 return res.toArray();
119 private static void seekKOTests(Class<?> c, List<Object> res) throws SecurityException, ClassNotFoundException {
120 Class<? extends Annotation> koTest =
121 c.getClassLoader().loadClass(KOTest.class.getName()).
122 asSubclass(Annotation.class);
123 for (Method m : c.getMethods()) {
124 if (m.getAnnotation(koTest) != null) {
125 res.add(new KOFx(browserContext, m));
130 static synchronized ClassLoader getClassLoader() throws InterruptedException {
131 while (browserClass == null) {
132 KnockoutFXTest.class.wait();
134 return browserClass.getClassLoader();
137 public static synchronized void initialized(Class<?> browserCls) throws Exception {
138 browserClass = browserCls;
139 browserContext = FnContext.currentPresenter();
140 KnockoutFXTest.class.notifyAll();
143 public static void initialized() throws Exception {
144 Class<?> classpathClass = ClassLoader.getSystemClassLoader().loadClass(KnockoutFXTest.class.getName());
145 Method m = classpathClass.getMethod("initialized", Class.class);
146 m.invoke(null, KnockoutFXTest.class);
147 browserContext = FnContext.currentPresenter();
151 public BrwsrCtx createContext() {
152 FXContext fx = new FXContext(browserContext);
153 Contexts.Builder cb = Contexts.newBuilder().
154 register(Technology.class, fx, 10).
155 register(Transfer.class, fx, 10);
156 if (fx.areWebSocketsSupported()) {
157 cb.register(WSTransfer.class, fx, 10);
163 public Object createJSON(Map<String, Object> values) {
164 Object json = createJSON();
165 for (Map.Entry<String, Object> entry : values.entrySet()) {
166 setProperty(json, entry.getKey(), entry.getValue());
171 @JavaScriptBody(args = {}, body = "return new Object();")
172 private static native Object createJSON();
173 @JavaScriptBody(args = { "json", "key", "value" }, body = "json[key] = value;")
174 private static native void setProperty(Object json, String key, Object value);
177 @JavaScriptBody(args = { "s", "args" }, body = ""
178 + "var f = new Function(s); "
179 + "return f.apply(null, args);"
181 public native Object executeScript(String script, Object[] arguments);
183 @JavaScriptBody(args = { }, body =
185 + "if (!!window && !!window.location && !!window.location.href)\n"
186 + " h = window.location.href;\n"
191 private static native String findBaseURL();
194 public URI prepareURL(String content, String mimeType, String[] parameters) {
196 final URL baseURL = new URL(findBaseURL());
197 StringBuilder sb = new StringBuilder();
198 sb.append("/dynamic?mimeType=").append(mimeType);
199 for (int i = 0; i < parameters.length; i++) {
200 sb.append("¶m" + i).append("=").append(parameters[i]);
202 String mangle = content.replace("\n", "%0a")
203 .replace("\"", "\\\"").replace(" ", "%20");
204 sb.append("&content=").append(mangle);
206 URL query = new URL(baseURL, sb.toString());
207 URLConnection c = query.openConnection();
208 BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
209 URI connectTo = new URI(br.readLine());
211 } catch (IOException ex) {
212 throw new IllegalStateException(ex);
213 } catch (URISyntaxException ex) {
214 throw new IllegalStateException(ex);
219 public boolean canFailWebSocketTest() {
221 Class.forName("java.util.function.Function");
223 } catch (ClassNotFoundException ex) {
224 // running on JDK7, FX WebView WebSocket impl does not work