Getting bunch of the knockout tests work in FX Web View fx
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 18 Apr 2013 16:58:55 +0200
branchfx
changeset 10119cc253aa9405
parent 1007 66ccab5a3530
child 1012 b22c86471837
Getting bunch of the knockout tests work in FX Web View
javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/ConvertTypes.java
javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/PageProcessor.java
javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/Element.java
javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/KnockoutTest.java
rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/impl/Console.java
     1.1 --- a/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/ConvertTypes.java	Thu Apr 18 09:06:18 2013 +0200
     1.2 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/ConvertTypes.java	Thu Apr 18 16:58:55 2013 +0200
     1.3 @@ -22,6 +22,7 @@
     1.4  import java.io.InputStreamReader;
     1.5  import java.io.PushbackInputStream;
     1.6  import java.io.Reader;
     1.7 +import java.net.MalformedURLException;
     1.8  import java.net.URL;
     1.9  import java.util.Iterator;
    1.10  import java.util.concurrent.Executor;
    1.11 @@ -29,6 +30,7 @@
    1.12  import java.util.logging.Level;
    1.13  import java.util.logging.Logger;
    1.14  import javafx.application.Platform;
    1.15 +import javafx.scene.web.WebEngine;
    1.16  import netscape.javascript.JSObject;
    1.17  import org.apidesign.bck2brwsr.core.JavaScriptBody;
    1.18  import org.json.JSONArray;
    1.19 @@ -106,6 +108,18 @@
    1.20          }
    1.21      }
    1.22      
    1.23 +    private static String findBaseURL() {
    1.24 +        WebEngine eng = (WebEngine) System.getProperties().get("webEngine");
    1.25 +        return (String)eng.executeScript(
    1.26 +          "var h;"
    1.27 +        + "if (!!window && !!window.location && !!window.location.href)\n"
    1.28 +        + "  h = window.location.href;\n"
    1.29 +        + "else "
    1.30 +        + "  h = null;"
    1.31 +        + "h\n"
    1.32 +        );
    1.33 +    }
    1.34 +    
    1.35      public static String createJSONP(Object[] jsonResult, Runnable whenDone) {
    1.36          return "json" + Integer.toHexString(whenDone.hashCode());
    1.37      }
    1.38 @@ -123,12 +137,21 @@
    1.39          private final Object[] jsonResult;
    1.40          private final Runnable whenDone;
    1.41          private final String jsonp;
    1.42 +        private final URL base;
    1.43  
    1.44          LoadJSON(String url, Object[] jsonResult, Runnable whenDone, String jsonp) {
    1.45              this.url = url;
    1.46              this.jsonResult = jsonResult;
    1.47              this.whenDone = whenDone;
    1.48              this.jsonp = jsonp;
    1.49 +            URL b;
    1.50 +            try {
    1.51 +                b = new URL(findBaseURL());
    1.52 +            } catch (MalformedURLException ex) {
    1.53 +                LOG.log(Level.SEVERE, "Can't find base url for " + url, ex);
    1.54 +                b = null;
    1.55 +            }
    1.56 +            this.base = b;
    1.57          }
    1.58  
    1.59          @Override
    1.60 @@ -138,7 +161,7 @@
    1.61                  return;
    1.62              }
    1.63              try {
    1.64 -                URL u = new URL(url.replace(" ", "%20"));
    1.65 +                URL u = new URL(base, url.replace(" ", "%20"));
    1.66                  InputStream is = u.openStream();
    1.67                  if (jsonp != null) {
    1.68                      PushbackInputStream pis = new PushbackInputStream(is, 1);
     2.1 --- a/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/PageProcessor.java	Thu Apr 18 09:06:18 2013 +0200
     2.2 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/PageProcessor.java	Thu Apr 18 16:58:55 2013 +0200
     2.3 @@ -200,10 +200,10 @@
     2.4                  w.append(body.toString());
     2.5                  w.append("  private static Class<" + inPckName(e) + "> modelFor() { return null; }\n");
     2.6                  w.append("  public ").append(className).append("() {\n");
     2.7 -                w.append("    intKnckt();\n");
     2.8                  w.append("  };\n");
     2.9 -                w.append("  private void intKnckt() {\n");
    2.10 -                w.append("    ko = org.apidesign.bck2brwsr.htmlpage.Knockout.applyBindings(this, ");
    2.11 +                w.append("  private org.apidesign.bck2brwsr.htmlpage.Knockout intKnckt() {\n");
    2.12 +                w.append("    if (ko != null) return ko;\n");
    2.13 +                w.append("    return ko = org.apidesign.bck2brwsr.htmlpage.Knockout.applyBindings(this, ");
    2.14                  writeStringArray(propsGetSet, w);
    2.15                  w.append(", ");
    2.16                  writeStringArray(functions, w);
    2.17 @@ -276,12 +276,11 @@
    2.18                      }
    2.19                      cnt++;
    2.20                  }
    2.21 -                w.append("    intKnckt();\n");
    2.22                  w.append("  };\n");
    2.23                  writeToString(props, w);
    2.24                  writeClone(className, props, w);
    2.25                  w.append("  public Object koData() {\n");
    2.26 -                w.append("    return ko.koData();\n");
    2.27 +                w.append("    return intKnckt().koData();\n");
    2.28                  w.append("  }\n");
    2.29                  w.append("}\n");
    2.30              } finally {
     3.1 --- a/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/Element.java	Thu Apr 18 09:06:18 2013 +0200
     3.2 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/Element.java	Thu Apr 18 16:58:55 2013 +0200
     3.3 @@ -47,14 +47,34 @@
     3.4          body="var e = window.document.getElementById(el._id());\n"
     3.5             + "e[property] = value;\n"
     3.6      )
     3.7 -    static native void setAttribute(Element el, String property, Object value);
     3.8 +    static void setAttribute(Element el, String property, Object value) {
     3.9 +        JSObject js = (JSObject) web().executeScript("(function () {"
    3.10 +            + "  var x = {}; "
    3.11 +            + "  x.setAttribute = function(id, property, value) { "
    3.12 +            + "    var e = window.document.getElementById(id);\n"
    3.13 +            + "    e[property] = value;"
    3.14 +            + "  };"
    3.15 +            + "  return x;"
    3.16 +            + "})()");
    3.17 +        js.call("setAttribute", el.id, property, value);
    3.18 +    }
    3.19  
    3.20      @JavaScriptBody(
    3.21          args={"el", "property"},
    3.22          body="var e = window.document.getElementById(el._id());\n"
    3.23             + "return e[property];\n"
    3.24      )
    3.25 -    static native Object getAttribute(Element el, String property);
    3.26 +    static Object getAttribute(Element el, String property) {
    3.27 +        JSObject js = (JSObject) web().executeScript("(function () {"
    3.28 +            + "  var x = {}; "
    3.29 +            + "  x.getAttribute = function(id, property) { "
    3.30 +            + "    var e = window.document.getElementById(id);\n"
    3.31 +            + "    return e[property];"
    3.32 +            + "  };"
    3.33 +            + "  return x;"
    3.34 +            + "})()");
    3.35 +        return js.call("getAttribute", el.id, property);
    3.36 +    }
    3.37      
    3.38      @JavaScriptBody(
    3.39          args={"el"},
     4.1 --- a/javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/KnockoutTest.java	Thu Apr 18 09:06:18 2013 +0200
     4.2 +++ b/javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/KnockoutTest.java	Thu Apr 18 16:58:55 2013 +0200
     4.3 @@ -18,6 +18,8 @@
     4.4  package org.apidesign.bck2brwsr.htmlpage;
     4.5  
     4.6  import java.util.List;
     4.7 +import javafx.scene.web.WebEngine;
     4.8 +import netscape.javascript.JSObject;
     4.9  import org.apidesign.bck2brwsr.core.JavaScriptBody;
    4.10  import org.apidesign.bck2brwsr.htmlpage.api.ComputedProperty;
    4.11  import org.apidesign.bck2brwsr.htmlpage.api.OnEvent;
    4.12 @@ -39,7 +41,7 @@
    4.13      @Property(name="name", type=String.class),
    4.14      @Property(name="results", type=String.class, array = true),
    4.15      @Property(name="callbackCount", type=int.class),
    4.16 -    @Property(name="people", type=PersonImpl.class, array = true)
    4.17 +    @Property(name="people", type=Person.class, array = true)
    4.18  }) 
    4.19  public class KnockoutTest {
    4.20      
    4.21 @@ -234,25 +236,53 @@
    4.22          return VMTest.create(KnockoutTest.class);
    4.23      }
    4.24      
    4.25 -    @JavaScriptBody(args = { "id" }, body = 
    4.26 +    private static final String COUNT_CHILDREN = 
    4.27            "var e = window.document.getElementById(id);\n "
    4.28          + "if (typeof e === 'undefined') return -2;\n "
    4.29 -        + "return e.children.length;\n "
    4.30 -    )
    4.31 -    private static native int countChildren(String id);
    4.32 +        + "return e.children.length;\n";
    4.33 +    @JavaScriptBody(args = { "id" }, body = COUNT_CHILDREN)
    4.34 +    private static int countChildren(String id) {
    4.35 +        return ((Number)js().call("countChildren", id)).intValue();
    4.36 +    }
    4.37  
    4.38 -    @JavaScriptBody(args = { "id", "pos" }, body = 
    4.39 +    private static final String TRIGGER_CHILD_CLICK = 
    4.40            "var e = window.document.getElementById(id);\n "
    4.41          + "var ev = window.document.createEvent('MouseEvents');\n "
    4.42          + "ev.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);\n "
    4.43 -        + "e.children[pos].dispatchEvent(ev);\n "
    4.44 -    )
    4.45 -    private static native void triggerChildClick(String id, int pos);
    4.46 +        + "e.children[pos].dispatchEvent(ev);\n ";
    4.47 +    @JavaScriptBody(args = { "id", "pos" }, body = TRIGGER_CHILD_CLICK)
    4.48 +    private static void triggerChildClick(String id, int pos) {
    4.49 +        js().call("triggerChildClick", id, pos);
    4.50 +    }
    4.51  
    4.52 -    @JavaScriptBody(args = { "id", "pos" }, body = 
    4.53 +    private static final String CHILD_TEXT = 
    4.54            "var e = window.document.getElementById(id);\n "
    4.55          + "var t = e.children[pos].innerHTML;\n "
    4.56 -        + "return t ? t : null;"
    4.57 -    )
    4.58 -    private static native String childText(String id, int pos);
    4.59 +        + "return t ? t : null;";
    4.60 +    @JavaScriptBody(args = { "id", "pos" }, body = CHILD_TEXT)
    4.61 +    private static String childText(String id, int pos) {
    4.62 +        return (String) js().call("childText", id, pos);
    4.63 +    }
    4.64 +    
    4.65 +    
    4.66 +    private static JSObject js;
    4.67 +    private static JSObject js() {
    4.68 +        if (js == null) {
    4.69 +            WebEngine eng = (WebEngine)System.getProperties().get("webEngine");
    4.70 +            if (eng == null) {
    4.71 +                js = null;
    4.72 +            } else {
    4.73 +                js = (JSObject) eng.executeScript("(function () {" +
    4.74 +                    "var obj = {};"
    4.75 +                    + ""
    4.76 +                    + "obj.countChildren = function(id) { " + COUNT_CHILDREN + "};"
    4.77 +                    + "obj.triggerChildClick = function(id, pos) { " + TRIGGER_CHILD_CLICK + "};"
    4.78 +                    + "obj.childText = function(id, pos) { " + CHILD_TEXT + "};"
    4.79 +                    + "return obj;"
    4.80 +                + "})();");
    4.81 +            }
    4.82 +        }
    4.83 +
    4.84 +        return js;
    4.85 +    }
    4.86  }
     5.1 --- a/rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/impl/Console.java	Thu Apr 18 09:06:18 2013 +0200
     5.2 +++ b/rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/impl/Console.java	Thu Apr 18 16:58:55 2013 +0200
     5.3 @@ -99,7 +99,7 @@
     5.4          + "var status = window.document.createElement('a');\n"
     5.5          + "status.innerHTML = 'running';"
     5.6          + "details.onclick = function() { li.appendChild(p); li.removeChild(details); status.innerHTML = 'Run Again'; status.href = '#'; };\n"
     5.7 -        + "status.onclick = function() { c.again__V_3Ljava_lang_Object_2(arr); }\n"
     5.8 +        + "status.onclick = function() { c.again(arr); }\n"
     5.9          + "var pre = window.document.createElement('textarea');\n"
    5.10          + "pre.cols = 100;"
    5.11          + "pre.rows = 10;"