1.1 --- a/json-tck/src/main/java/net/java/html/json/tests/MinesTest.java Fri Mar 07 19:45:56 2014 +0100
1.2 +++ b/json-tck/src/main/java/net/java/html/json/tests/MinesTest.java Sat Mar 08 09:32:33 2014 +0100
1.3 @@ -48,6 +48,7 @@
1.4 import java.util.Random;
1.5 import net.java.html.BrwsrCtx;
1.6 import net.java.html.json.ComputedProperty;
1.7 +import net.java.html.json.Function;
1.8 import net.java.html.json.Model;
1.9 import net.java.html.json.ModelOperation;
1.10 import net.java.html.json.Models;
1.11 @@ -61,48 +62,67 @@
1.12 @Property(name = "rows", type = Row.class, array = true),
1.13 })
1.14 public final class MinesTest {
1.15 - @KOTest public void paintTheGrid() throws Throwable {
1.16 - BrwsrCtx ctx = Utils.newContext(KnockoutTest.class);
1.17 - Object exp = Utils.exposeHTML(KnockoutTest.class,
1.18 -" <table class=\"field\">\n" +
1.19 -" <tbody id='table'>\n" +
1.20 -" <!-- ko foreach: rows -->\n" +
1.21 -" <tr>\n" +
1.22 -" <!-- ko foreach: columns -->\n" +
1.23 -" <td data-bind=\"css: style, click: $parents[1].click\" >\n" +
1.24 -" <div data-bind='text: html'></div>\n" +
1.25 -" </td>\n" +
1.26 -" <!-- /ko -->\n" +
1.27 -" </tr>\n" +
1.28 -" <!-- /ko -->\n" +
1.29 -" </tbody>\n" +
1.30 -" </table>\n" +
1.31 -""
1.32 - );
1.33 - try {
1.34 + Mines m;
1.35 + @KOTest public void paintTheGridOnClick() throws Throwable {
1.36 + if (m == null) {
1.37 + BrwsrCtx ctx = Utils.newContext(MinesTest.class);
1.38 + Object exp = Utils.exposeHTML(MinesTest.class,
1.39 + " <button id='init' data-bind='click: normalSize'></button>\n" +
1.40 + " <table>\n" +
1.41 + " <tbody id='table'>\n" +
1.42 + " <!-- ko foreach: rows -->\n" +
1.43 + " <tr>\n" +
1.44 + " <!-- ko foreach: columns -->\n" +
1.45 + " <td data-bind='css: style' >\n" +
1.46 + " <div data-bind='text: html'></div>\n" +
1.47 + " </td>\n" +
1.48 + " <!-- /ko -->\n" +
1.49 + " </tr>\n" +
1.50 + " <!-- /ko -->\n" +
1.51 + " </tbody>\n" +
1.52 + " </table>\n" +
1.53 + ""
1.54 + );
1.55 + m = Models.bind(new Mines(), ctx);
1.56 + m.applyBindings();
1.57 + int cnt = countChildren("table");
1.58 + assert cnt == 0 : "Table is empty: " + cnt;
1.59 + scheduleClick("init", 100);
1.60 + }
1.61
1.62 - Mines m = Models.bind(new Mines(), ctx);
1.63 - m.init(10, 10, 0);
1.64 - m.applyBindings();
1.65
1.66 - int cnt = countChildren("table");
1.67 - assert cnt == 10 : "There is ten rows in the table: " + cnt;
1.68 - } catch (Throwable t) {
1.69 - throw t;
1.70 - } finally {
1.71 - Utils.exposeHTML(KnockoutTest.class, "");
1.72 + int cnt = countChildren("table");
1.73 + if (cnt == 0) {
1.74 + throw new InterruptedException();
1.75 }
1.76 + assert cnt == 10 : "There is ten rows in the table now: " + cnt;
1.77 +
1.78 + Utils.exposeHTML(MinesTest.class, "");
1.79 }
1.80
1.81 private static int countChildren(String id) throws Exception {
1.82 return ((Number)Utils.executeScript(
1.83 - KnockoutTest.class,
1.84 + MinesTest.class,
1.85 "var e = window.document.getElementById(arguments[0]);\n "
1.86 + "if (typeof e === 'undefined') return -2;\n "
1.87 + "return e.children.length;",
1.88 id
1.89 )).intValue();
1.90 }
1.91 +
1.92 + private static void scheduleClick(String id, int delay) throws Exception {
1.93 + String s = "var id = arguments[0]; var delay = arguments[1];"
1.94 + + "var e = window.document.getElementById(id);\n "
1.95 + + "var f = function() {;\n "
1.96 + + " var ev = window.document.createEvent('MouseEvents');\n "
1.97 + + " ev.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);\n "
1.98 + + " e.dispatchEvent(ev);\n"
1.99 + + "};\n"
1.100 + + "window.setTimeout(f, delay);";
1.101 + Utils.executeScript(
1.102 + MinesTest.class,
1.103 + s, id, delay);
1.104 + }
1.105
1.106 enum GameState {
1.107 IN_PROGRESS, WON, LOST;
1.108 @@ -159,6 +179,9 @@
1.109 return state != null;
1.110 }
1.111
1.112 + @Function static void normalSize(Mines m) {
1.113 + m.init(10, 10, 10);
1.114 + }
1.115
1.116 @ModelOperation static void init(Mines model, int width, int height, int mines) {
1.117 List<Row> rows = new ArrayList<Row>(height);