Sharing the countChildren code and making sure they pass on older browsers like env.js envjs
authorJaroslav Tulach <jtulach@netbeans.org>
Wed, 04 Jun 2014 12:17:10 +0200
branchenvjs
changeset 68185a879c797c9
parent 680 9fa36fee05e5
child 682 258c54b5077c
Sharing the countChildren code and making sure they pass on older browsers like env.js
json-tck/src/main/java/net/java/html/json/tests/KnockoutTest.java
json-tck/src/main/java/net/java/html/json/tests/MinesTest.java
json-tck/src/main/java/net/java/html/json/tests/Utils.java
     1.1 --- a/json-tck/src/main/java/net/java/html/json/tests/KnockoutTest.java	Wed Jun 04 10:28:17 2014 +0200
     1.2 +++ b/json-tck/src/main/java/net/java/html/json/tests/KnockoutTest.java	Wed Jun 04 12:17:10 2014 +0200
     1.3 @@ -167,12 +167,12 @@
     1.4              m.getResults().add("Ahoj");
     1.5              m.applyBindings();
     1.6  
     1.7 -            int cnt = countChildren("ul");
     1.8 +            int cnt = Utils.countChildren(KnockoutTest.class, "ul");
     1.9              assert cnt == 1 : "One child, but was " + cnt;
    1.10  
    1.11              m.getResults().add("Hi");
    1.12  
    1.13 -            cnt = countChildren("ul");
    1.14 +            cnt = Utils.countChildren(KnockoutTest.class, "ul");
    1.15              assert cnt == 2 : "Two children now, but was " + cnt;
    1.16  
    1.17              triggerChildClick("ul", 1);
    1.18 @@ -194,7 +194,7 @@
    1.19              Pair m = Models.bind(new Pair("First", "Last", null), newContext());
    1.20              m.applyBindings();
    1.21  
    1.22 -            int cnt = countChildren("ul");
    1.23 +            int cnt = Utils.countChildren(KnockoutTest.class, "ul");
    1.24              assert cnt == 2 : "Two children now, but was " + cnt;
    1.25  
    1.26              triggerChildClick("ul", 1);
    1.27 @@ -203,7 +203,7 @@
    1.28              
    1.29              m.setLastName("Verylast");
    1.30  
    1.31 -            cnt = countChildren("ul");
    1.32 +            cnt = Utils.countChildren(KnockoutTest.class, "ul");
    1.33              assert cnt == 2 : "Two children now, but was " + cnt;
    1.34              
    1.35              triggerChildClick("ul", 1);
    1.36 @@ -228,7 +228,7 @@
    1.37              Pair m = Models.bind(new Pair(null, null, new Pair("First", "Last", null)), ctx);
    1.38              m.applyBindings();
    1.39  
    1.40 -            int cnt = countChildren("ul");
    1.41 +            int cnt = Utils.countChildren(KnockoutTest.class, "ul");
    1.42              assert cnt == 2 : "Two children now, but was " + cnt;
    1.43  
    1.44              triggerChildClick("ul", 1);
    1.45 @@ -253,7 +253,7 @@
    1.46              Pair m = Models.bind(new Pair(null, null, new Pair("First", "Last", null)), newContext());
    1.47              m.applyBindings();
    1.48  
    1.49 -            int cnt = countChildren("ul");
    1.50 +            int cnt = Utils.countChildren(KnockoutTest.class, "ul");
    1.51              assert cnt == 2 : "Two children now, but was " + cnt;
    1.52  
    1.53              triggerChildClick("ul", 1);
    1.54 @@ -295,12 +295,12 @@
    1.55              m.getResults().add("Ahoj");
    1.56              m.applyBindings();
    1.57  
    1.58 -            int cnt = countChildren("ul");
    1.59 +            int cnt = Utils.countChildren(KnockoutTest.class, "ul");
    1.60              assert cnt == 1 : "One child, but was " + cnt;
    1.61  
    1.62              m.getResults().add("hello");
    1.63  
    1.64 -            cnt = countChildren("ul");
    1.65 +            cnt = Utils.countChildren(KnockoutTest.class, "ul");
    1.66              assert cnt == 2 : "Two children now, but was " + cnt;
    1.67          } finally {
    1.68              Utils.exposeHTML(KnockoutTest.class, "");
    1.69 @@ -323,21 +323,21 @@
    1.70  
    1.71              m.applyBindings();
    1.72  
    1.73 -            int cnt = countChildren("ul");
    1.74 +            int cnt = Utils.countChildren(KnockoutTest.class, "ul");
    1.75              assert cnt == 1 : "One child, but was " + cnt;
    1.76  
    1.77              final Person second = Models.bind(new Person(), c);
    1.78              second.setFirstName("second");
    1.79              m.getPeople().add(second);
    1.80  
    1.81 -            cnt = countChildren("ul");
    1.82 +            cnt = Utils.countChildren(KnockoutTest.class, "ul");
    1.83              assert cnt == 2 : "Two children now, but was " + cnt;
    1.84  
    1.85              triggerChildClick("ul", 1);
    1.86  
    1.87              assert 1 == m.getCallbackCount() : "One callback " + m.getCallbackCount();
    1.88  
    1.89 -            cnt = countChildren("ul");
    1.90 +            cnt = Utils.countChildren(KnockoutTest.class, "ul");
    1.91              assert cnt == 1 : "Again one child, but was " + cnt;
    1.92  
    1.93              String txt = childText("ul", 0);
    1.94 @@ -394,7 +394,7 @@
    1.95  
    1.96          m.applyBindings();
    1.97  
    1.98 -        int cnt = countChildren("ul");
    1.99 +        int cnt = Utils.countChildren(KnockoutTest.class, "ul");
   1.100          assert cnt == 1 : "One child, but was " + cnt;
   1.101  
   1.102  
   1.103 @@ -426,16 +426,6 @@
   1.104          return results;
   1.105      }
   1.106      
   1.107 -    private static int countChildren(String id) throws Exception {
   1.108 -        return ((Number)Utils.executeScript(
   1.109 -          KnockoutTest.class,
   1.110 -          "var e = window.document.getElementById(arguments[0]);\n "
   1.111 -        + "if (typeof e === 'undefined') return -2;\n "
   1.112 -        + "return e.children.length;", 
   1.113 -            id
   1.114 -        )).intValue();
   1.115 -    }
   1.116 -
   1.117      private static void triggerClick(String id) throws Exception {
   1.118          String s = "var id = arguments[0];"
   1.119              + "var e = window.document.getElementById(id);\n "
     2.1 --- a/json-tck/src/main/java/net/java/html/json/tests/MinesTest.java	Wed Jun 04 10:28:17 2014 +0200
     2.2 +++ b/json-tck/src/main/java/net/java/html/json/tests/MinesTest.java	Wed Jun 04 12:17:10 2014 +0200
     2.3 @@ -85,13 +85,13 @@
     2.4              );
     2.5              m = Models.bind(new Mines(), ctx);
     2.6              m.applyBindings();
     2.7 -            int cnt = countChildren("table");
     2.8 +            int cnt = Utils.countChildren(MinesTest.class, "table");
     2.9              assert cnt == 0 : "Table is empty: " + cnt;
    2.10              scheduleClick("init", 100);
    2.11          }
    2.12  
    2.13  
    2.14 -        int cnt = countChildren("table");
    2.15 +        int cnt = Utils.countChildren(MinesTest.class, "table");
    2.16          if (cnt == 0) {
    2.17              throw new InterruptedException();
    2.18          }
    2.19 @@ -111,16 +111,6 @@
    2.20          assert cnt == 3 : "There are three mines around. Was: " + cnt;
    2.21      }
    2.22      
    2.23 -    private static int countChildren(String id) throws Exception {
    2.24 -        return ((Number)Utils.executeScript(
    2.25 -          MinesTest.class,
    2.26 -          "var e = window.document.getElementById(arguments[0]);\n "
    2.27 -        + "if (typeof e === 'undefined') return -2;\n "
    2.28 -        + "return e.children.length;", 
    2.29 -            id
    2.30 -        )).intValue();
    2.31 -    }
    2.32 -
    2.33      private static void scheduleClick(String id, int delay) throws Exception {
    2.34          String s = "var id = arguments[0]; var delay = arguments[1];"
    2.35              + "var e = window.document.getElementById(id);\n "
     3.1 --- a/json-tck/src/main/java/net/java/html/json/tests/Utils.java	Wed Jun 04 10:28:17 2014 +0200
     3.2 +++ b/json-tck/src/main/java/net/java/html/json/tests/Utils.java	Wed Jun 04 12:17:10 2014 +0200
     3.3 @@ -55,7 +55,7 @@
     3.4   */
     3.5  public final class Utils {
     3.6      private static KnockoutTCK instantiatedTCK;
     3.7 -    
     3.8 +
     3.9      private Utils() {
    3.10      }
    3.11      
    3.12 @@ -110,6 +110,20 @@
    3.13          return executeScript(clazz, s, html);
    3.14      }
    3.15  
    3.16 +    static int countChildren(Class<?> caller, String id) throws Exception {
    3.17 +        return ((Number) executeScript(caller, 
    3.18 +            "var e = window.document.getElementById(arguments[0]);\n" + 
    3.19 +            "if (typeof e === 'undefined') return -2;\n " + 
    3.20 +            "var list = e.childNodes;\n" +
    3.21 +            "var cnt = 0;\n" + 
    3.22 +            "for (var i = 0; i < list.length; i++) {\n" + 
    3.23 +            "  if (list[i].nodeType == 1) cnt++;\n" + 
    3.24 +            "}\n" + 
    3.25 +            "return cnt;\n"
    3.26 +            , id
    3.27 +        )).intValue();
    3.28 +    }
    3.29 +    
    3.30      static String prepareURL(
    3.31          Class<?> clazz, String content, String mimeType, String... parameters) {
    3.32          for (KnockoutTCK tck : tcks(clazz)) {