Can execute 'dual' tests: bck2brwsr can use regular launcher, javaquery.api can use FX Web View one fx
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 18 Apr 2013 23:09:30 +0200
branchfx
changeset 10166dc2c6c752df
parent 1015 d7cff2cba6e5
Can execute 'dual' tests: bck2brwsr can use regular launcher, javaquery.api can use FX Web View one
javaquery/api/pom.xml
javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/ConvertTypes.java
javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/Knockout.java
rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Launcher.java
rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/WebViewLauncher.java
rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/impl/Console.java
rt/launcher/src/main/resources/org/apidesign/bck2brwsr/launcher/harness.xhtml
     1.1 --- a/javaquery/api/pom.xml	Thu Apr 18 20:09:45 2013 +0200
     1.2 +++ b/javaquery/api/pom.xml	Thu Apr 18 23:09:30 2013 +0200
     1.3 @@ -30,9 +30,18 @@
     1.4                      <skip>false</skip>
     1.5                  </configuration>
     1.6              </plugin>
     1.7 +            <plugin>
     1.8 +                <groupId>org.apache.maven.plugins</groupId>
     1.9 +                <artifactId>maven-surefire-plugin</artifactId>
    1.10 +                <configuration>
    1.11 +                    <systemPropertyVariables>
    1.12 +                        <vmtest.brwsrs>fx</vmtest.brwsrs>
    1.13 +                    </systemPropertyVariables>
    1.14 +                </configuration>
    1.15 +            </plugin>
    1.16          </plugins>
    1.17      </build>
    1.18 -    <properties>
    1.19 +  <properties>
    1.20      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    1.21    </properties>
    1.22    <dependencies>
     2.1 --- a/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/ConvertTypes.java	Thu Apr 18 20:09:45 2013 +0200
     2.2 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/ConvertTypes.java	Thu Apr 18 23:09:30 2013 +0200
     2.3 @@ -42,7 +42,6 @@
     2.4   * @author Jaroslav Tulach <jtulach@netbeans.org>
     2.5   */
     2.6  public final class ConvertTypes {
     2.7 -    private static final Logger LOG = Logger.getLogger(ConvertTypes.class.getName());
     2.8      ConvertTypes() {
     2.9      }
    2.10      
    2.11 @@ -123,14 +122,16 @@
    2.12          return "json" + Integer.toHexString(whenDone.hashCode());
    2.13      }
    2.14  
    2.15 +    @JavaScriptBody(args = { "url", "jsonResult", "whenDone", "jsonp" }, body = "throw 'ignore';")
    2.16      public static void loadJSON(
    2.17          String url, Object[] jsonResult, Runnable whenDone, String jsonp
    2.18      ) {
    2.19 -        REQ.execute(new LoadJSON(url, jsonResult, whenDone, jsonp));
    2.20 +        LoadJSON.REQ.execute(new LoadJSON(url, jsonResult, whenDone, jsonp));
    2.21      }
    2.22      
    2.23 -    private static final Executor REQ = Executors.newCachedThreadPool();
    2.24      private static final class LoadJSON implements Runnable {
    2.25 +        static final Executor REQ = Executors.newCachedThreadPool();
    2.26 +        private static final Logger LOG = Logger.getLogger(ConvertTypes.class.getName());
    2.27          
    2.28          private final String url;
    2.29          private final Object[] jsonResult;
    2.30 @@ -230,7 +231,8 @@
    2.31      private static void loadJSONP(String url, String jsonp) {
    2.32          
    2.33      }
    2.34 -    
    2.35 +
    2.36 +    @JavaScriptBody(args = { "jsonObject", "props", "values" }, body = "throw 'ignore extractJSON';")
    2.37      public static void extractJSON(Object jsonObject, String[] props, Object[] values) {
    2.38          if (jsonObject instanceof JSONObject) {
    2.39              JSONObject obj = (JSONObject)jsonObject;
    2.40 @@ -238,7 +240,7 @@
    2.41                  try {
    2.42                      values[i] = obj.has(props[i]) ? obj.get(props[i]) : null;
    2.43                  } catch (JSONException ex) {
    2.44 -                    LOG.log(Level.SEVERE, "Can't read " + props[i] + " from " + jsonObject, ex);
    2.45 +                    LoadJSON.LOG.log(Level.SEVERE, "Can't read " + props[i] + " from " + jsonObject, ex);
    2.46                  }
    2.47              }
    2.48              
     3.1 --- a/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/Knockout.java	Thu Apr 18 20:09:45 2013 +0200
     3.2 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/Knockout.java	Thu Apr 18 23:09:30 2013 +0200
     3.3 @@ -40,28 +40,6 @@
     3.4      static Knockout next;
     3.5      private final Object model;
     3.6  
     3.7 -    static {
     3.8 -        BufferedReader r = new BufferedReader(new InputStreamReader(Knockout.class.getResourceAsStream("knockout-2.2.1.js")));
     3.9 -        StringBuilder sb = new StringBuilder();
    3.10 -        for (;;) {
    3.11 -            try {
    3.12 -                String l = r.readLine();
    3.13 -                if (l == null) {
    3.14 -                    break;
    3.15 -                }
    3.16 -                sb.append(l).append('\n');
    3.17 -            } catch (IOException ex) {
    3.18 -                throw new IllegalStateException(ex);
    3.19 -            }
    3.20 -        }
    3.21 -        web().executeScript(sb.toString());
    3.22 -        Object ko = web().executeScript("ko");
    3.23 -        assert ko != null : "Knockout library successfully defined 'ko'";
    3.24 -        
    3.25 -        Console.register(web());
    3.26 -    }
    3.27 -    
    3.28 -    
    3.29      Knockout(Object model) {
    3.30          this.model = model == null ? this : model;
    3.31      }
    3.32 @@ -70,43 +48,15 @@
    3.33          return model;
    3.34      }
    3.35  
    3.36 -    private static final JSObject KObject;
    3.37 -    static {
    3.38 -        KObject = (JSObject) web().executeScript(
    3.39 -            "(function(scope) {"
    3.40 -            + "  var kCnt = 0; "
    3.41 -            + "  scope.KObject = {};"
    3.42 -            + "  scope.KObject.create= function(value) {"
    3.43 -            + "    var cnt = ++kCnt;"
    3.44 -            + "    var ret = {};"
    3.45 -            + "    ret.toString = function() { return 'KObject' + cnt + ' value: ' + value + ' props: ' + Object.keys(this); };"
    3.46 -            + "    return ret;"
    3.47 -            + "  };"
    3.48 -            
    3.49 -            + "  scope.KObject.array= function() {"
    3.50 -            + "    return Array.prototype.slice.call(arguments);"
    3.51 -            + "  };"
    3.52 -            
    3.53 -            + "  scope.KObject.expose = function(bindings, model, prop, sig) {"
    3.54 -            + "    bindings[prop] = function(data, ev) {"
    3.55 -//            + "         console.log(\"  callback on prop: \" + prop);"
    3.56 -            + "      model[sig](data, ev);"
    3.57 -            + "    };"
    3.58 -            + "  };"
    3.59 -            
    3.60 -            + "})(window); window.KObject"
    3.61 -            );
    3.62 -    }
    3.63 -    
    3.64      static Object toArray(Object[] arr) {
    3.65 -        return KObject.call("array", arr);
    3.66 +        return InvokeJS.KObject.call("array", arr);
    3.67      }
    3.68      
    3.69      public static <M> Knockout applyBindings(
    3.70          Object model, String[] propsGettersAndSetters,
    3.71          String[] methodsAndSignatures
    3.72      ) {
    3.73 -        Object bindings = KObject.call("create", model);
    3.74 +        Object bindings = InvokeJS.KObject.call("create", model);
    3.75          applyImpl(propsGettersAndSetters, model.getClass(), bindings, model, methodsAndSignatures);
    3.76          return new Knockout(bindings);
    3.77      }
    3.78 @@ -117,7 +67,7 @@
    3.79          Object bindings = next;
    3.80          next = null;
    3.81          if (bindings == null) {
    3.82 -            bindings = KObject.call("create", model);
    3.83 +            bindings = InvokeJS.KObject.call("create", model);
    3.84          }
    3.85          applyImpl(propsGettersAndSetters, modelClass, bindings, model, methodsAndSignatures);
    3.86          applyBindings(bindings);
    3.87 @@ -167,6 +117,9 @@
    3.88          Object bindings, Object model, String prop, String getter, String setter, boolean primitive, boolean array
    3.89      ) {
    3.90          WebEngine e = web();
    3.91 +        if (e == null) {
    3.92 +            return;
    3.93 +        }
    3.94          JSObject bnd = (JSObject) e.executeScript("var x = {}; x.bnd = "
    3.95          + "new Function('ko', 'bindings', 'model', 'prop', 'getter', 'setter', 'primitive', 'array', '"
    3.96          + "var bnd = {"
    3.97 @@ -198,7 +151,7 @@
    3.98          + "};"
    3.99          + "bindings[prop] = ko.computed(bnd);'"
   3.100          + "); x;");
   3.101 -        
   3.102 +
   3.103          Object ko = e.executeScript("ko");
   3.104          try {
   3.105              KOProperty kop = new KOProperty(model, strip(getter), strip(setter));
   3.106 @@ -224,9 +177,13 @@
   3.107      private static void expose(
   3.108          Object bindings, Object model, String prop, String sig
   3.109      ) {
   3.110 +        WebEngine e = web();
   3.111 +        if (e == null) {
   3.112 +            return;
   3.113 +        }
   3.114          try {
   3.115              KOFunction f = new KOFunction(model, strip(sig));
   3.116 -            KObject.call("expose", bindings, f, prop, "call");
   3.117 +            InvokeJS.KObject.call("expose", bindings, f, prop, "call");
   3.118          } catch (Throwable ex) {
   3.119              LOG.log(Level.SEVERE, "Cannot define binding for " + prop + " in model " + model, ex);
   3.120          }
   3.121 @@ -234,8 +191,10 @@
   3.122      
   3.123      @JavaScriptBody(args = { "bindings" }, body = "ko.applyBindings(bindings);")
   3.124      private static void applyBindings(Object bindings) {
   3.125 -        JSObject ko = (JSObject) web().executeScript("ko");
   3.126 -        ko.call("applyBindings", bindings);
   3.127 +        if (web() != null) {
   3.128 +            JSObject ko = (JSObject) web().executeScript("ko");
   3.129 +            ko.call("applyBindings", bindings);
   3.130 +        }
   3.131      }
   3.132      
   3.133      private static WebEngine web() {
   3.134 @@ -267,4 +226,50 @@
   3.135                  bindings, model, methodsAndSignatures[i], methodsAndSignatures[i + 1]);
   3.136          }
   3.137      }
   3.138 +    
   3.139 +    private static final class InvokeJS {
   3.140 +        static final JSObject KObject;
   3.141 +
   3.142 +        static {
   3.143 +            BufferedReader r = new BufferedReader(new InputStreamReader(Knockout.class.getResourceAsStream("knockout-2.2.1.js")));
   3.144 +            StringBuilder sb = new StringBuilder();
   3.145 +            for (;;) {
   3.146 +                try {
   3.147 +                    String l = r.readLine();
   3.148 +                    if (l == null) {
   3.149 +                        break;
   3.150 +                    }
   3.151 +                    sb.append(l).append('\n');
   3.152 +                } catch (IOException ex) {
   3.153 +                    throw new IllegalStateException(ex);
   3.154 +                }
   3.155 +            }
   3.156 +            web().executeScript(sb.toString());
   3.157 +            Object ko = web().executeScript("ko");
   3.158 +            assert ko != null : "Knockout library successfully defined 'ko'";
   3.159 +
   3.160 +            Console.register(web());
   3.161 +            KObject = (JSObject) web().executeScript(
   3.162 +                "(function(scope) {"
   3.163 +                + "  var kCnt = 0; "
   3.164 +                + "  scope.KObject = {};"
   3.165 +                + "  scope.KObject.create= function(value) {"
   3.166 +                + "    var cnt = ++kCnt;"
   3.167 +                + "    var ret = {};"
   3.168 +                + "    ret.toString = function() { return 'KObject' + cnt + ' value: ' + value + ' props: ' + Object.keys(this); };"
   3.169 +                + "    return ret;"
   3.170 +                + "  };"
   3.171 +                + "  scope.KObject.array= function() {"
   3.172 +                + "    return Array.prototype.slice.call(arguments);"
   3.173 +                + "  };"
   3.174 +                + "  scope.KObject.expose = function(bindings, model, prop, sig) {"
   3.175 +                + "    bindings[prop] = function(data, ev) {"
   3.176 +                //            + "         console.log(\"  callback on prop: \" + prop);"
   3.177 +                + "      model[sig](data, ev);"
   3.178 +                + "    };"
   3.179 +                + "  };"
   3.180 +                + "})(window); window.KObject");
   3.181 +        }
   3.182 +        
   3.183 +    }
   3.184  }
     4.1 --- a/rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Launcher.java	Thu Apr 18 20:09:45 2013 +0200
     4.2 +++ b/rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Launcher.java	Thu Apr 18 23:09:30 2013 +0200
     4.3 @@ -20,7 +20,6 @@
     4.4  import java.io.Closeable;
     4.5  import java.io.File;
     4.6  import java.io.IOException;
     4.7 -import java.lang.reflect.Method;
     4.8  import org.apidesign.vm4brwsr.Bck2Brwsr;
     4.9  
    4.10  /** An abstraction for executing tests in a Bck2Brwsr virtual machine.
    4.11 @@ -80,7 +79,7 @@
    4.12       */
    4.13      public static Launcher createBrowser(String cmd) {
    4.14          final Bck2BrwsrLauncher l;
    4.15 -        if ("fx".equals(cmd) || cmd == null) {
    4.16 +        if ("fx".equals(cmd)) {
    4.17              l = new WebViewLauncher();
    4.18          } else {
    4.19              l = new Bck2BrwsrLauncher(cmd);
     5.1 --- a/rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/WebViewLauncher.java	Thu Apr 18 20:09:45 2013 +0200
     5.2 +++ b/rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/WebViewLauncher.java	Thu Apr 18 23:09:30 2013 +0200
     5.3 @@ -70,7 +70,6 @@
     5.4                      } catch (Throwable ex) {
     5.5                          LOG.log(Level.WARNING, "Error launching Web View", ex);
     5.6                      }
     5.7 -                    System.exit(0);
     5.8                  }
     5.9              });
    5.10          } catch (Throwable ex) {
     6.1 --- a/rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/impl/Console.java	Thu Apr 18 20:09:45 2013 +0200
     6.2 +++ b/rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/impl/Console.java	Thu Apr 18 23:09:30 2013 +0200
     6.3 @@ -36,23 +36,22 @@
     6.4  public final class Console {
     6.5      public Console() {
     6.6      }
     6.7 -    private static final JSObject CObject;
     6.8      
     6.9      @JavaScriptBody(args = {"elem", "attr"}, body = 
    6.10          "return elem[attr].toString();")
    6.11      private static Object getAttr(Object elem, String attr) {
    6.12 -        return CObject.call("getAttr", elem, attr);
    6.13 +        return InvokeJS.CObject.call("getAttr", elem, attr);
    6.14      }
    6.15  
    6.16      @JavaScriptBody(args = {"id", "attr", "value"}, body = 
    6.17          "window.document.getElementById(id)[attr] = value;")
    6.18      private static void setAttr(String id, String attr, Object value) {
    6.19 -        CObject.call("setAttrId", id, attr, value);
    6.20 +        InvokeJS.CObject.call("setAttrId", id, attr, value);
    6.21      }
    6.22      @JavaScriptBody(args = {"elem", "attr", "value"}, body = 
    6.23          "elem[attr] = value;")
    6.24      private static void setAttr(Object id, String attr, Object value) {
    6.25 -        CObject.call("setAttr", id, attr, value);
    6.26 +        InvokeJS.CObject.call("setAttr", id, attr, value);
    6.27      }
    6.28      
    6.29      @JavaScriptBody(args = {}, body = "return; window.close();")
    6.30 @@ -116,7 +115,7 @@
    6.31          
    6.32      @JavaScriptBody(args = { "test", "c", "arr" }, body = BEGIN_TEST)
    6.33      private static void beginTest(String test, Case c, Object[] arr) {
    6.34 -        CObject.call("beginTest", test, c, arr);
    6.35 +        InvokeJS.CObject.call("beginTest", test, c, arr);
    6.36      }
    6.37      
    6.38      private static final String LOAD_TEXT = 
    6.39 @@ -127,13 +126,17 @@
    6.40          + "  if (this.readyState!==4) return;\n"
    6.41          + " try {"
    6.42          + "  arr[0] = this.responseText;\n"
    6.43 -        + "  callback.run();\n"
    6.44 +        + "  callback.run__V();\n"
    6.45          + " } catch (e) { alert(e); }"
    6.46          + "};"
    6.47          + "request.send();";
    6.48      @JavaScriptBody(args = { "url", "callback", "arr" }, body = LOAD_TEXT)
    6.49      private static void loadText(String url, Runnable callback, String[] arr) throws IOException {
    6.50 -        CObject.call("loadText", url, new Run(callback), arr);
    6.51 +        InvokeJS.CObject.call("loadText", url, new Run(callback), arr);
    6.52 +    }
    6.53 +    
    6.54 +    public static void runHarness(String url) throws IOException {
    6.55 +        new Console().harness(url);
    6.56      }
    6.57      
    6.58      public void harness(String url) throws IOException {
    6.59 @@ -261,7 +264,7 @@
    6.60      @JavaScriptBody(args = {"r", "time"}, body =
    6.61          "return window.setTimeout(function() { r.run__V(); }, time);")
    6.62      private static Object schedule(Runnable r, int time) {
    6.63 -        return CObject.call("schedule", new Run(r), time);
    6.64 +        return InvokeJS.CObject.call("schedule", new Run(r), time);
    6.65      }
    6.66      
    6.67      private static final class Case {
    6.68 @@ -285,7 +288,11 @@
    6.69          }
    6.70          
    6.71          public int getRequestId() {
    6.72 -            return (int) value("request", data);
    6.73 +            Object v = value("request", data);
    6.74 +            if (v instanceof Number) {
    6.75 +                return ((Number)v).intValue();
    6.76 +            }
    6.77 +            return Integer.parseInt(v.toString());
    6.78          }
    6.79  
    6.80          public String getHtmlFragment() {
    6.81 @@ -357,7 +364,7 @@
    6.82          
    6.83          @JavaScriptBody(args = "s", body = "return eval('(' + s + ')');")
    6.84          private static Object toJSON(String s) {
    6.85 -            return CObject.call("toJSON", s);
    6.86 +            return InvokeJS.CObject.call("toJSON", s);
    6.87          }
    6.88          
    6.89          @JavaScriptBody(args = {"p", "d"}, body = 
    6.90 @@ -376,26 +383,33 @@
    6.91      
    6.92      static {
    6.93          turnAssetionStatusOn();
    6.94 +    }
    6.95 +    
    6.96 +    private static final class InvokeJS {
    6.97 +        static final JSObject CObject = initJS();
    6.98  
    6.99 -        WebEngine web = (WebEngine) System.getProperties().get("webEngine");
   6.100 -        CObject = (JSObject) web.executeScript("(function() {"
   6.101 -            + "var CObject = {};"
   6.102 +        @JavaScriptBody(args = {  }, body = "return null;")
   6.103 +        private static JSObject initJS() {
   6.104 +            WebEngine web = (WebEngine) System.getProperties().get("webEngine");
   6.105 +            return (JSObject) web.executeScript("(function() {"
   6.106 +                + "var CObject = {};"
   6.107  
   6.108 -            + "CObject.getAttr = function(elem, attr) { return elem[attr].toString(); };"
   6.109 +                + "CObject.getAttr = function(elem, attr) { return elem[attr].toString(); };"
   6.110  
   6.111 -            + "CObject.setAttrId = function(id, attr, value) { window.document.getElementById(id)[attr] = value; };"
   6.112 -            + "CObject.setAttr = function(elem, attr, value) { elem[attr] = value; };"
   6.113 +                + "CObject.setAttrId = function(id, attr, value) { window.document.getElementById(id)[attr] = value; };"
   6.114 +                + "CObject.setAttr = function(elem, attr, value) { elem[attr] = value; };"
   6.115  
   6.116 -            + "CObject.beginTest = function(test, c, arr) {" + safe(BEGIN_TEST) + "};"
   6.117 +                + "CObject.beginTest = function(test, c, arr) {" + safe(BEGIN_TEST) + "};"
   6.118  
   6.119 -            + "CObject.loadText = function(url, callback, arr) {" + safe(LOAD_TEXT) + "};"
   6.120 -            
   6.121 -            + "CObject.schedule = function(r, time) { return window.setTimeout(function() { r.run(); }, time); };"
   6.122 -            
   6.123 -            + "CObject.toJSON = function(s) { return eval('(' + s + ')'); };"
   6.124 +                + "CObject.loadText = function(url, callback, arr) {" + safe(LOAD_TEXT.replace("run__V", "run")) + "};"
   6.125  
   6.126 -            + "return CObject;"
   6.127 -        + "})(this)");
   6.128 +                + "CObject.schedule = function(r, time) { return window.setTimeout(function() { r.run(); }, time); };"
   6.129 +
   6.130 +                + "CObject.toJSON = function(s) { return eval('(' + s + ')'); };"
   6.131 +
   6.132 +                + "return CObject;"
   6.133 +            + "})(this)");
   6.134 +        }
   6.135      }
   6.136      
   6.137  }
     7.1 --- a/rt/launcher/src/main/resources/org/apidesign/bck2brwsr/launcher/harness.xhtml	Thu Apr 18 20:09:45 2013 +0200
     7.2 +++ b/rt/launcher/src/main/resources/org/apidesign/bck2brwsr/launcher/harness.xhtml	Thu Apr 18 23:09:30 2013 +0200
     7.3 @@ -38,8 +38,17 @@
     7.4          
     7.5          <script type="text/javascript">
     7.6              try {
     7.7 -                var inst = vm.loadClass('org.apidesign.bck2brwsr.launcher.impl.Console').newInstance();
     7.8 -                inst.harness('$U/../data');
     7.9 +                (function() {
    7.10 +                    var cls = vm.loadClass('org.apidesign.bck2brwsr.launcher.impl.Console');
    7.11 +                    try {
    7.12 +                        // bck2brwsr mangling
    7.13 +                        cls.runHarness__VLjava_lang_String_2('$U/../data');
    7.14 +                    } catch (err) {
    7.15 +                        // fxbrwsr mangling
    7.16 +                        var inst = cls.newInstance();
    7.17 +                        inst.harness('$U/../data');
    7.18 +                    }
    7.19 +                })();
    7.20              } catch (err) { 
    7.21                  alert('Error executing harness: ' + err); 
    7.22              }