Execution of JavaScript needs to be provided by the instance of the TCK
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 25 Apr 2013 12:40:29 +0200
changeset 36d283d6c6fba9
parent 35 d13b8b8c9a81
child 37 184f08369400
Execution of JavaScript needs to be provided by the instance of the TCK
json-tck/src/main/java/net/java/html/json/tests/JSONTest.java
json-tck/src/main/java/net/java/html/json/tests/KnockoutTest.java
json-tck/src/main/java/net/java/html/json/tests/Utils.java
json-tck/src/main/java/org/apidesign/html/json/tck/KnockoutTCK.java
     1.1 --- a/json-tck/src/main/java/net/java/html/json/tests/JSONTest.java	Wed Apr 24 18:52:42 2013 +0200
     1.2 +++ b/json-tck/src/main/java/net/java/html/json/tests/JSONTest.java	Thu Apr 25 12:40:29 2013 +0200
     1.3 @@ -23,7 +23,6 @@
     1.4  import net.java.html.json.Model;
     1.5  import net.java.html.json.OnReceive;
     1.6  import net.java.html.json.Property;
     1.7 -import org.apidesign.bck2brwsr.core.JavaScriptBody;
     1.8  import org.apidesign.bck2brwsr.vmtest.BrwsrTest;
     1.9  import org.apidesign.bck2brwsr.vmtest.Http;
    1.10  import org.apidesign.bck2brwsr.vmtest.VMTest;
    1.11 @@ -120,7 +119,7 @@
    1.12          mimeType = "application/javascript",
    1.13          parameters = { "callme" }
    1.14      ))
    1.15 -    @BrwsrTest public void loadAndParseJSONP() throws InterruptedException {
    1.16 +    @BrwsrTest public void loadAndParseJSONP() throws InterruptedException, Exception {
    1.17          
    1.18          if (js == null) {
    1.19              orig = scriptElements();
    1.20 @@ -145,11 +144,13 @@
    1.21          assert orig == now : "The set of elements is unchanged. Delta: " + (now - orig);
    1.22      }
    1.23      
    1.24 -    @JavaScriptBody(args = {  }, body = "return window.document.getElementsByTagName('script').length;")
    1.25 -    private static native int scriptElements();
    1.26 +    private static int scriptElements() throws Exception {
    1.27 +        return ((Number)Utils.executeScript("return window.document.getElementsByTagName('script').length;")).intValue();
    1.28 +    }
    1.29  
    1.30 -    @JavaScriptBody(args = { "s" }, body = "return window.JSON.parse(s);")
    1.31 -    private static native Object parseJSON(String s);
    1.32 +    private static Object parseJSON(String s) throws Exception {
    1.33 +        return Utils.executeScript("return window.JSON.parse(arguments[0]);", s);
    1.34 +    }
    1.35      
    1.36      @Http(@Http.Resource(
    1.37          content = "{'firstName': 'Sitar', 'sex': 'MALE'}", 
     2.1 --- a/json-tck/src/main/java/net/java/html/json/tests/KnockoutTest.java	Wed Apr 24 18:52:42 2013 +0200
     2.2 +++ b/json-tck/src/main/java/net/java/html/json/tests/KnockoutTest.java	Thu Apr 25 12:40:29 2013 +0200
     2.3 @@ -25,7 +25,6 @@
     2.4  import net.java.html.json.Function;
     2.5  import net.java.html.json.Model;
     2.6  import net.java.html.json.Property;
     2.7 -import org.apidesign.bck2brwsr.core.JavaScriptBody;
     2.8  import org.apidesign.bck2brwsr.vmtest.BrwsrTest;
     2.9  import org.apidesign.bck2brwsr.vmtest.HtmlFragment;
    2.10  import org.apidesign.bck2brwsr.vmtest.VMTest;
    2.11 @@ -47,7 +46,7 @@
    2.12          "Your name: <input id='input' data-bind=\"value: name\"></input>\n" +
    2.13          "<button id=\"hello\">Say Hello!</button>\n"
    2.14      )
    2.15 -    @BrwsrTest public void modifyValueAssertChangeInModel() {
    2.16 +    @BrwsrTest public void modifyValueAssertChangeInModel() throws Exception {
    2.17          KnockoutModel m = new KnockoutModel(Utils.newContext());
    2.18          m.setName("Kukuc");
    2.19          m.applyBindings();
    2.20 @@ -61,16 +60,19 @@
    2.21          assert "Jardo".equals(m.getName()) : "Name property updated: " + m.getName();
    2.22      }
    2.23      
    2.24 -    @JavaScriptBody(args = {"value"}, body
    2.25 -        = "var n = window.document.getElementById('input'); \n "
    2.26 +    private static String getSetInput(String value) throws Exception {
    2.27 +        String s = "var value = arguments[0];\n"
    2.28 +        + "var n = window.document.getElementById('input'); \n "
    2.29          + "if (value != null) n['value'] = value; \n "
    2.30 -        + "return n['value']; \n ")
    2.31 -    private static String getSetInput(String value) {
    2.32 -        return null;
    2.33 +        + "return n['value'];";
    2.34 +        return (String)Utils.executeScript(s, value);
    2.35      }
    2.36      
    2.37 -    @JavaScriptBody(args = { "id", "ev" }, body = "ko.utils.triggerEvent(window.document.getElementById(id), ev);")
    2.38 -    public static void triggerEvent(String id, String ev) {
    2.39 +    public static void triggerEvent(String id, String ev) throws Exception {
    2.40 +        Utils.executeScript(
    2.41 +            "ko.utils.triggerEvent(window.document.getElementById(arguments[0]), arguments[1]);",
    2.42 +            id, ev
    2.43 +        );
    2.44      }
    2.45      
    2.46      @HtmlFragment(
    2.47 @@ -78,7 +80,7 @@
    2.48          + "  <li data-bind='text: $data, click: $root.call'/>\n"
    2.49          + "</ul>\n"
    2.50      )
    2.51 -    @BrwsrTest public void displayContentOfArray() {
    2.52 +    @BrwsrTest public void displayContentOfArray() throws Exception {
    2.53          KnockoutModel m = new KnockoutModel(Utils.newContext());
    2.54          m.getResults().add("Ahoj");
    2.55          m.applyBindings();
    2.56 @@ -102,7 +104,7 @@
    2.57          + "  <li><b data-bind='text: $data'></b></li>\n"
    2.58          + "</ul>\n"
    2.59      )
    2.60 -    @BrwsrTest public void displayContentOfDerivedArray() {
    2.61 +    @BrwsrTest public void displayContentOfDerivedArray() throws Exception {
    2.62          KnockoutModel m = new KnockoutModel(Utils.newContext());
    2.63          m.getResults().add("Ahoj");
    2.64          m.applyBindings();
    2.65 @@ -121,7 +123,7 @@
    2.66          + "  <li data-bind='text: $data.firstName, click: $root.removePerson'></li>\n"
    2.67          + "</ul>\n"
    2.68      )
    2.69 -    @BrwsrTest public void displayContentOfArrayOfPeople() {
    2.70 +    @BrwsrTest public void displayContentOfArrayOfPeople() throws Exception {
    2.71          KnockoutModel m = new KnockoutModel(Utils.newContext());
    2.72          
    2.73          final Person first = new Person(Utils.newContext());
    2.74 @@ -166,7 +168,7 @@
    2.75          + "  <span data-bind='text: firstName, click: changeSex'></span>\n"
    2.76          + "</p>\n"
    2.77      )
    2.78 -    @BrwsrTest public void accessFirstPersonWithOnFunction() {
    2.79 +    @BrwsrTest public void accessFirstPersonWithOnFunction() throws Exception {
    2.80          trasfertToFemale();
    2.81      }
    2.82      
    2.83 @@ -175,11 +177,11 @@
    2.84          + "  <li data-bind='text: $data.firstName, click: changeSex'></li>\n"
    2.85          + "</ul>\n"
    2.86      )
    2.87 -    @BrwsrTest public void onPersonFunction() {
    2.88 +    @BrwsrTest public void onPersonFunction() throws Exception {
    2.89          trasfertToFemale();
    2.90      }
    2.91      
    2.92 -    private void trasfertToFemale() {
    2.93 +    private void trasfertToFemale() throws Exception {
    2.94          KnockoutModel m = new KnockoutModel(Utils.newContext());
    2.95  
    2.96          final Person first = new Person(Utils.newContext());
    2.97 @@ -226,25 +228,29 @@
    2.98          return VMTest.create(KnockoutTest.class);
    2.99      }
   2.100      
   2.101 -    @JavaScriptBody(args = { "id" }, body = 
   2.102 -          "var e = window.document.getElementById(id);\n "
   2.103 +    private static int countChildren(String id) throws Exception {
   2.104 +        return ((Number)Utils.executeScript(
   2.105 +          "var e = window.document.getElementById(arguments[0]);\n "
   2.106          + "if (typeof e === 'undefined') return -2;\n "
   2.107 -        + "return e.children.length;\n "
   2.108 -    )
   2.109 -    private static native int countChildren(String id);
   2.110 +        + "return e.children.length;", 
   2.111 +            id
   2.112 +        )).intValue();
   2.113 +    }
   2.114  
   2.115 -    @JavaScriptBody(args = { "id", "pos" }, body = 
   2.116 -          "var e = window.document.getElementById(id);\n "
   2.117 -        + "var ev = window.document.createEvent('MouseEvents');\n "
   2.118 -        + "ev.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);\n "
   2.119 -        + "e.children[pos].dispatchEvent(ev);\n "
   2.120 -    )
   2.121 -    private static native void triggerChildClick(String id, int pos);
   2.122 +    private static void triggerChildClick(String id, int pos) throws Exception {
   2.123 +        String s = "var id = arguments[0]; var pos = arguments[1];"
   2.124 +            + "var e = window.document.getElementById(id);\n "
   2.125 +            + "var ev = window.document.createEvent('MouseEvents');\n "
   2.126 +            + "ev.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);\n "
   2.127 +            + "e.children[pos].dispatchEvent(ev);\n ";
   2.128 +        Utils.executeScript(s, id, pos);
   2.129 +    }
   2.130  
   2.131 -    @JavaScriptBody(args = { "id", "pos" }, body = 
   2.132 -          "var e = window.document.getElementById(id);\n "
   2.133 +    private static String childText(String id, int pos) throws Exception {
   2.134 +        String s = "var id = arguments[0]; var pos = arguments[1];"
   2.135 +        + "var e = window.document.getElementById(id);\n "
   2.136          + "var t = e.children[pos].innerHTML;\n "
   2.137 -        + "return t ? t : null;"
   2.138 -    )
   2.139 -    private static native String childText(String id, int pos);
   2.140 +        + "return t ? t : null;";
   2.141 +        return (String)Utils.executeScript(s, id, pos);
   2.142 +    }
   2.143  }
     3.1 --- a/json-tck/src/main/java/net/java/html/json/tests/Utils.java	Wed Apr 24 18:52:42 2013 +0200
     3.2 +++ b/json-tck/src/main/java/net/java/html/json/tests/Utils.java	Thu Apr 25 12:40:29 2013 +0200
     3.3 @@ -51,5 +51,11 @@
     3.4          }
     3.5          throw new AssertionError("Can't find appropriate Context in ServiceLoader!");
     3.6      }
     3.7 +    static Object executeScript(String script, Object... arguments) throws Exception {
     3.8 +        for (KnockoutTCK tck : ServiceLoader.load(KnockoutTCK.class)) {
     3.9 +            return tck.executeScript(script, arguments);
    3.10 +        }
    3.11 +        throw new AssertionError("Can't find appropriate Context in ServiceLoader!");
    3.12 +    }
    3.13      
    3.14  }
     4.1 --- a/json-tck/src/main/java/org/apidesign/html/json/tck/KnockoutTCK.java	Wed Apr 24 18:52:42 2013 +0200
     4.2 +++ b/json-tck/src/main/java/org/apidesign/html/json/tck/KnockoutTCK.java	Thu Apr 25 12:40:29 2013 +0200
     4.3 @@ -62,7 +62,14 @@
     4.4       * @param values mapping from names to values of properties
     4.5       */
     4.6      public abstract Object createJSON(Map<String,Object> values);
     4.7 -    
     4.8 +
     4.9 +    /** Executes script in the context of current window
    4.10 +     * 
    4.11 +     * @param script the JavaScript code to execute
    4.12 +     * @param arguments arguments sent to the script (can be referenced as <code>arguments[0]</code>)
    4.13 +     * @return the output of the execution
    4.14 +     */
    4.15 +    public abstract Object executeScript(String script, Object[] arguments);
    4.16      
    4.17      /** Gives you list of classes included in the TCK. Send them
    4.18       * to {@link VMTest#create(java.lang.Class)} factory method.
    4.19 @@ -76,5 +83,6 @@
    4.20              KnockoutTest.class
    4.21          };
    4.22      }
    4.23 +
    4.24      
    4.25  }