/**
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
*
* Oracle and Java are registered trademarks of Oracle and/or its affiliates.
* Other names may be trademarks of their respective owners.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common
* Development and Distribution License("CDDL") (collectively, the
* "License"). You may not use this file except in compliance with the
* License. You can obtain a copy of the License at
* http://www.netbeans.org/cddl-gplv2.html
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
* specific language governing permissions and limitations under the
* License. When distributing the software, include this License Header
* Notice in each file and include the License file at
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the GPL Version 2 section of the License file that
* accompanied this code. If applicable, add the following below the
* License Header, with the fields enclosed by brackets [] replaced by
* your own identifying information:
* "Portions Copyrighted [year] [name of copyright owner]"
*
* Contributor(s):
*
* The Original Software is NetBeans. The Initial Developer of the Original
* Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
*
* If you wish your version of this file to be governed by only the CDDL
* or only the GPL Version 2, indicate your decision by adding
* "[Contributor] elects to include this software in this distribution
* under the [CDDL or GPL Version 2] license." If you do not indicate a
* single choice of license, a recipient has the option to distribute
* your version of this file under either the CDDL, the GPL Version 2 or
* to extend the choice of license to its licensees as provided above.
* However, if you add GPL Version 2 code and therefore, elected the GPL
* Version 2 license, then the option applies only if the new code is
* made subject to such option by the copyright holder.
*/
package org.teavm.html4j.test;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
import net.java.html.BrwsrCtx;
import net.java.html.js.JavaScriptBody;
import org.netbeans.html.boot.spi.Fn;
import org.netbeans.html.context.spi.Contexts;
import org.netbeans.html.json.spi.JSONCall;
import org.netbeans.html.json.spi.Technology;
import org.netbeans.html.json.spi.Transfer;
import org.netbeans.html.json.tck.KnockoutTCK;
import org.netbeans.html.ko4j.KO4J;
import org.testng.Assert;
/**
*
* @author Jaroslav Tulach <jtulach@netbeans.org>
*/
public final class KnockoutFXTest extends KnockoutTCK implements Transfer {
private static Class<?> browserClass;
private static Fn.Presenter browserContext;
private KO4J ko4j = new KO4J();
private Map<String, String> urlMap = new HashMap<>();
public KnockoutFXTest() {
}
static synchronized ClassLoader getClassLoader() throws InterruptedException {
while (browserClass == null) {
KnockoutFXTest.class.wait();
}
return browserClass.getClassLoader();
}
public static synchronized void initialized(Class<?> browserCls) throws Exception {
browserClass = browserCls;
browserContext = Fn.activePresenter();
KnockoutFXTest.class.notifyAll();
}
public static void initialized() throws Exception {
Assert.assertSame(
KnockoutFXTest.class.getClassLoader(),
ClassLoader.getSystemClassLoader(),
"No special classloaders"
);
KnockoutFXTest.initialized(KnockoutFXTest.class);
browserContext = Fn.activePresenter();
}
@Override
public BrwsrCtx createContext() {
KO4J ko4j = new KO4J();
return Contexts.newBuilder()
.register(Transfer.class, this, 1)
.register(Technology.class, ko4j.knockout(), 1)
.build();
}
@Override
public Object createJSON(Map<String, Object> values) {
Object json = createJSON();
for (Map.Entry<String, Object> entry : values.entrySet()) {
setProperty(json, entry.getKey(), entry.getValue());
}
return json;
}
@JavaScriptBody(args = {}, body = "return new Object();")
private static native Object createJSON();
@JavaScriptBody(args = { "json", "key", "value" }, body = "json[key] = value;")
private static native void setProperty(Object json, String key, Object value);
@Override
@JavaScriptBody(args = { "s", "args" }, body = ""
+ "var f = new Function(s); "
+ "return f.apply(null, args);"
)
public native Object executeScript(String script, Object[] arguments);
@JavaScriptBody(args = { }, body =
"var h;"
+ "if (!!window && !!window.location && !!window.location.href)\n"
+ " h = window.location.href;\n"
+ "else "
+ " h = null;"
+ "return h;\n"
)
private static native String findBaseURL();
@Override
public URI prepareURL(String content, String mimeType, String[] parameters) {
try {
String url = "http://localhost/dynamic/" + urlMap.size();
urlMap.put(url, content);
return new URI(url);
} catch (URISyntaxException ex) {
throw new IllegalStateException(ex);
}
}
@Override
public boolean canFailWebSocketTest() {
return true;
}
@Override
public void extract(Object obj, String[] props, Object[] values) {
ko4j.transfer().extract(obj, props, values);
}
@Override
public Object toJSON(InputStream is) throws IOException {
return ko4j.transfer().toJSON(is);
}
@Override
public void loadJSON(JSONCall call) {
if (call.isJSONP()) {
throw new IllegalArgumentException("This mock does not support JSONP calls");
}
String url = call.composeURL(null);
String data = urlMap.get(url);
if (data != null) {
try {
call.notifySuccess(toJSON(new ByteArrayInputStream(data.getBytes())));
} catch (IOException e) {
call.notifyError(e);
}
} else {
call.notifyError(new IllegalStateException());
}
}
}