Can execute tests in FX web view.
1.1 --- a/rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/impl/Console.java Thu Apr 18 05:47:45 2013 +0200
1.2 +++ b/rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/impl/Console.java Thu Apr 18 09:06:18 2013 +0200
1.3 @@ -25,6 +25,8 @@
1.4 import java.lang.reflect.Modifier;
1.5 import java.net.URL;
1.6 import java.util.Enumeration;
1.7 +import javafx.scene.web.WebEngine;
1.8 +import netscape.javascript.JSObject;
1.9 import org.apidesign.bck2brwsr.core.JavaScriptBody;
1.10
1.11 /**
1.12 @@ -34,26 +36,27 @@
1.13 public final class Console {
1.14 public Console() {
1.15 }
1.16 - static {
1.17 - turnAssetionStatusOn();
1.18 - }
1.19 + private static final JSObject CObject;
1.20
1.21 - @JavaScriptBody(args = {"id", "attr"}, body =
1.22 - "return window.document.getElementById(id)[attr].toString();")
1.23 - private static native Object getAttr(String id, String attr);
1.24 @JavaScriptBody(args = {"elem", "attr"}, body =
1.25 "return elem[attr].toString();")
1.26 - private static native Object getAttr(Object elem, String attr);
1.27 + private static Object getAttr(Object elem, String attr) {
1.28 + return CObject.call("getAttr", elem, attr);
1.29 + }
1.30
1.31 @JavaScriptBody(args = {"id", "attr", "value"}, body =
1.32 "window.document.getElementById(id)[attr] = value;")
1.33 - private static native void setAttr(String id, String attr, Object value);
1.34 + private static void setAttr(String id, String attr, Object value) {
1.35 + CObject.call("setAttrId", id, attr, value);
1.36 + }
1.37 @JavaScriptBody(args = {"elem", "attr", "value"}, body =
1.38 "elem[attr] = value;")
1.39 - private static native void setAttr(Object id, String attr, Object value);
1.40 + private static void setAttr(Object id, String attr, Object value) {
1.41 + CObject.call("setAttr", id, attr, value);
1.42 + }
1.43
1.44 @JavaScriptBody(args = {}, body = "return; window.close();")
1.45 - private static native void closeWindow();
1.46 + private static void closeWindow() {}
1.47
1.48 private static Object textArea;
1.49 private static Object statusArea;
1.50 @@ -84,8 +87,8 @@
1.51 textArea = null;
1.52 }
1.53
1.54 - @JavaScriptBody(args = { "test", "c", "arr" }, body =
1.55 - "var ul = window.document.getElementById('bck2brwsr.result');\n"
1.56 + private static final String BEGIN_TEST =
1.57 + "var ul = window.document.getElementById('bck2brwsr.result');\n"
1.58 + "var li = window.document.createElement('li');\n"
1.59 + "var span = window.document.createElement('span');"
1.60 + "span.innerHTML = test + ' - ';\n"
1.61 @@ -109,22 +112,29 @@
1.62 + "p.appendChild(pre);\n"
1.63 + "ul.appendChild(li);\n"
1.64 + "arr[0] = pre;\n"
1.65 - + "arr[1] = status;\n"
1.66 - )
1.67 - private static native void beginTest(String test, Case c, Object[] arr);
1.68 + + "arr[1] = status;\n";
1.69 +
1.70 + @JavaScriptBody(args = { "test", "c", "arr" }, body = BEGIN_TEST)
1.71 + private static void beginTest(String test, Case c, Object[] arr) {
1.72 + CObject.call("beginTest", test, c, arr);
1.73 + }
1.74
1.75 - @JavaScriptBody(args = { "url", "callback", "arr" }, body = ""
1.76 - + "var request = new XMLHttpRequest();\n"
1.77 + private static final String LOAD_TEXT =
1.78 + "var request = new XMLHttpRequest();\n"
1.79 + "request.open('GET', url, true);\n"
1.80 + "request.setRequestHeader('Content-Type', 'text/plain; charset=utf-8');\n"
1.81 + "request.onreadystatechange = function() {\n"
1.82 + " if (this.readyState!==4) return;\n"
1.83 + + " try {"
1.84 + " arr[0] = this.responseText;\n"
1.85 - + " callback.run__V();\n"
1.86 + + " callback.run();\n"
1.87 + + " } catch (e) { alert(e); }"
1.88 + "};"
1.89 - + "request.send();"
1.90 - )
1.91 - private static native void loadText(String url, Runnable callback, String[] arr) throws IOException;
1.92 + + "request.send();";
1.93 + @JavaScriptBody(args = { "url", "callback", "arr" }, body = LOAD_TEXT)
1.94 + private static void loadText(String url, Runnable callback, String[] arr) throws IOException {
1.95 + CObject.call("loadText", url, new Run(callback), arr);
1.96 + }
1.97
1.98 public void harness(String url) throws IOException {
1.99 log("Connecting to " + url);
1.100 @@ -250,7 +260,9 @@
1.101
1.102 @JavaScriptBody(args = {"r", "time"}, body =
1.103 "return window.setTimeout(function() { r.run__V(); }, time);")
1.104 - private static native Object schedule(Runnable r, int time);
1.105 + private static Object schedule(Runnable r, int time) {
1.106 + return CObject.call("schedule", new Run(r), time);
1.107 + }
1.108
1.109 private static final class Case {
1.110 private final Object data;
1.111 @@ -265,19 +277,19 @@
1.112 }
1.113
1.114 public String getMethodName() {
1.115 - return value("methodName", data);
1.116 + return (String) value("methodName", data);
1.117 }
1.118
1.119 public String getClassName() {
1.120 - return value("className", data);
1.121 + return (String) value("className", data);
1.122 }
1.123
1.124 - public String getRequestId() {
1.125 - return value("request", data);
1.126 + public int getRequestId() {
1.127 + return (int) value("request", data);
1.128 }
1.129
1.130 public String getHtmlFragment() {
1.131 - return value("html", data);
1.132 + return (String) value("html", data);
1.133 }
1.134
1.135 void again(Object[] arr) {
1.136 @@ -344,13 +356,46 @@
1.137 }
1.138
1.139 @JavaScriptBody(args = "s", body = "return eval('(' + s + ')');")
1.140 - private static native Object toJSON(String s);
1.141 + private static Object toJSON(String s) {
1.142 + return CObject.call("toJSON", s);
1.143 + }
1.144
1.145 @JavaScriptBody(args = {"p", "d"}, body =
1.146 "var v = d[p];\n"
1.147 + "if (typeof v === 'undefined') return null;\n"
1.148 + "return v.toString();"
1.149 )
1.150 - private static native String value(String p, Object d);
1.151 + private static Object value(String p, Object d) {
1.152 + return ((JSObject)d).getMember(p);
1.153 + }
1.154 }
1.155 +
1.156 + private static String safe(String txt) {
1.157 + return "try {" + txt + "} catch (err) { alert(err); }";
1.158 + }
1.159 +
1.160 + static {
1.161 + turnAssetionStatusOn();
1.162 +
1.163 + WebEngine web = (WebEngine) System.getProperties().get("webEngine");
1.164 + CObject = (JSObject) web.executeScript("(function() {"
1.165 + + "var CObject = {};"
1.166 +
1.167 + + "CObject.getAttr = function(elem, attr) { return elem[attr].toString(); };"
1.168 +
1.169 + + "CObject.setAttrId = function(id, attr, value) { window.document.getElementById(id)[attr] = value; };"
1.170 + + "CObject.setAttr = function(elem, attr, value) { elem[attr] = value; };"
1.171 +
1.172 + + "CObject.beginTest = function(test, c, arr) {" + safe(BEGIN_TEST) + "};"
1.173 +
1.174 + + "CObject.loadText = function(url, callback, arr) {" + safe(LOAD_TEXT) + "};"
1.175 +
1.176 + + "CObject.schedule = function(r, time) { return window.setTimeout(function() { r.run(); }, time); };"
1.177 +
1.178 + + "CObject.toJSON = function(s) { return eval('(' + s + ')'); };"
1.179 +
1.180 + + "return CObject;"
1.181 + + "})(this)");
1.182 + }
1.183 +
1.184 }
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/impl/Run.java Thu Apr 18 09:06:18 2013 +0200
2.3 @@ -0,0 +1,35 @@
2.4 +/**
2.5 + * Back 2 Browser Bytecode Translator
2.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
2.7 + *
2.8 + * This program is free software: you can redistribute it and/or modify
2.9 + * it under the terms of the GNU General Public License as published by
2.10 + * the Free Software Foundation, version 2 of the License.
2.11 + *
2.12 + * This program is distributed in the hope that it will be useful,
2.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2.15 + * GNU General Public License for more details.
2.16 + *
2.17 + * You should have received a copy of the GNU General Public License
2.18 + * along with this program. Look for COPYING file in the top folder.
2.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
2.20 + */
2.21 +
2.22 +package org.apidesign.bck2brwsr.launcher.impl;
2.23 +
2.24 +/**
2.25 + *
2.26 + * @author Jaroslav Tulach <jtulach@netbeans.org>
2.27 + */
2.28 +public final class Run implements Runnable {
2.29 + private final Runnable r;
2.30 + Run(Runnable r) {
2.31 + this.r = r;
2.32 + }
2.33 +
2.34 + @Override
2.35 + public void run() {
2.36 + r.run();
2.37 + }
2.38 +}