# HG changeset patch # User Jaroslav Tulach # Date 1366268778 -7200 # Node ID 66ccab5a353079934b7e4d42afe522988e0d29f0 # Parent 691c5cd3fb93629f7a475ae4ef9b2cccce92850a Can execute tests in FX web view. diff -r 691c5cd3fb93 -r 66ccab5a3530 rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/impl/Console.java --- a/rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/impl/Console.java Thu Apr 18 05:47:45 2013 +0200 +++ b/rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/impl/Console.java Thu Apr 18 09:06:18 2013 +0200 @@ -25,6 +25,8 @@ import java.lang.reflect.Modifier; import java.net.URL; import java.util.Enumeration; +import javafx.scene.web.WebEngine; +import netscape.javascript.JSObject; import org.apidesign.bck2brwsr.core.JavaScriptBody; /** @@ -34,26 +36,27 @@ public final class Console { public Console() { } - static { - turnAssetionStatusOn(); - } + private static final JSObject CObject; - @JavaScriptBody(args = {"id", "attr"}, body = - "return window.document.getElementById(id)[attr].toString();") - private static native Object getAttr(String id, String attr); @JavaScriptBody(args = {"elem", "attr"}, body = "return elem[attr].toString();") - private static native Object getAttr(Object elem, String attr); + private static Object getAttr(Object elem, String attr) { + return CObject.call("getAttr", elem, attr); + } @JavaScriptBody(args = {"id", "attr", "value"}, body = "window.document.getElementById(id)[attr] = value;") - private static native void setAttr(String id, String attr, Object value); + private static void setAttr(String id, String attr, Object value) { + CObject.call("setAttrId", id, attr, value); + } @JavaScriptBody(args = {"elem", "attr", "value"}, body = "elem[attr] = value;") - private static native void setAttr(Object id, String attr, Object value); + private static void setAttr(Object id, String attr, Object value) { + CObject.call("setAttr", id, attr, value); + } @JavaScriptBody(args = {}, body = "return; window.close();") - private static native void closeWindow(); + private static void closeWindow() {} private static Object textArea; private static Object statusArea; @@ -84,8 +87,8 @@ textArea = null; } - @JavaScriptBody(args = { "test", "c", "arr" }, body = - "var ul = window.document.getElementById('bck2brwsr.result');\n" + private static final String BEGIN_TEST = + "var ul = window.document.getElementById('bck2brwsr.result');\n" + "var li = window.document.createElement('li');\n" + "var span = window.document.createElement('span');" + "span.innerHTML = test + ' - ';\n" @@ -109,22 +112,29 @@ + "p.appendChild(pre);\n" + "ul.appendChild(li);\n" + "arr[0] = pre;\n" - + "arr[1] = status;\n" - ) - private static native void beginTest(String test, Case c, Object[] arr); + + "arr[1] = status;\n"; + + @JavaScriptBody(args = { "test", "c", "arr" }, body = BEGIN_TEST) + private static void beginTest(String test, Case c, Object[] arr) { + CObject.call("beginTest", test, c, arr); + } - @JavaScriptBody(args = { "url", "callback", "arr" }, body = "" - + "var request = new XMLHttpRequest();\n" + private static final String LOAD_TEXT = + "var request = new XMLHttpRequest();\n" + "request.open('GET', url, true);\n" + "request.setRequestHeader('Content-Type', 'text/plain; charset=utf-8');\n" + "request.onreadystatechange = function() {\n" + " if (this.readyState!==4) return;\n" + + " try {" + " arr[0] = this.responseText;\n" - + " callback.run__V();\n" + + " callback.run();\n" + + " } catch (e) { alert(e); }" + "};" - + "request.send();" - ) - private static native void loadText(String url, Runnable callback, String[] arr) throws IOException; + + "request.send();"; + @JavaScriptBody(args = { "url", "callback", "arr" }, body = LOAD_TEXT) + private static void loadText(String url, Runnable callback, String[] arr) throws IOException { + CObject.call("loadText", url, new Run(callback), arr); + } public void harness(String url) throws IOException { log("Connecting to " + url); @@ -250,7 +260,9 @@ @JavaScriptBody(args = {"r", "time"}, body = "return window.setTimeout(function() { r.run__V(); }, time);") - private static native Object schedule(Runnable r, int time); + private static Object schedule(Runnable r, int time) { + return CObject.call("schedule", new Run(r), time); + } private static final class Case { private final Object data; @@ -265,19 +277,19 @@ } public String getMethodName() { - return value("methodName", data); + return (String) value("methodName", data); } public String getClassName() { - return value("className", data); + return (String) value("className", data); } - public String getRequestId() { - return value("request", data); + public int getRequestId() { + return (int) value("request", data); } public String getHtmlFragment() { - return value("html", data); + return (String) value("html", data); } void again(Object[] arr) { @@ -344,13 +356,46 @@ } @JavaScriptBody(args = "s", body = "return eval('(' + s + ')');") - private static native Object toJSON(String s); + private static Object toJSON(String s) { + return CObject.call("toJSON", s); + } @JavaScriptBody(args = {"p", "d"}, body = "var v = d[p];\n" + "if (typeof v === 'undefined') return null;\n" + "return v.toString();" ) - private static native String value(String p, Object d); + private static Object value(String p, Object d) { + return ((JSObject)d).getMember(p); + } } + + private static String safe(String txt) { + return "try {" + txt + "} catch (err) { alert(err); }"; + } + + static { + turnAssetionStatusOn(); + + WebEngine web = (WebEngine) System.getProperties().get("webEngine"); + CObject = (JSObject) web.executeScript("(function() {" + + "var CObject = {};" + + + "CObject.getAttr = function(elem, attr) { return elem[attr].toString(); };" + + + "CObject.setAttrId = function(id, attr, value) { window.document.getElementById(id)[attr] = value; };" + + "CObject.setAttr = function(elem, attr, value) { elem[attr] = value; };" + + + "CObject.beginTest = function(test, c, arr) {" + safe(BEGIN_TEST) + "};" + + + "CObject.loadText = function(url, callback, arr) {" + safe(LOAD_TEXT) + "};" + + + "CObject.schedule = function(r, time) { return window.setTimeout(function() { r.run(); }, time); };" + + + "CObject.toJSON = function(s) { return eval('(' + s + ')'); };" + + + "return CObject;" + + "})(this)"); + } + } diff -r 691c5cd3fb93 -r 66ccab5a3530 rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/impl/Run.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/impl/Run.java Thu Apr 18 09:06:18 2013 +0200 @@ -0,0 +1,35 @@ +/** + * Back 2 Browser Bytecode Translator + * Copyright (C) 2012 Jaroslav Tulach + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. Look for COPYING file in the top folder. + * If not, see http://opensource.org/licenses/GPL-2.0. + */ + +package org.apidesign.bck2brwsr.launcher.impl; + +/** + * + * @author Jaroslav Tulach + */ +public final class Run implements Runnable { + private final Runnable r; + Run(Runnable r) { + this.r = r; + } + + @Override + public void run() { + r.run(); + } +}