1.1 --- a/json-tck/src/main/java/net/java/html/json/tests/KnockoutTest.java Sun Aug 02 10:03:54 2015 +0200
1.2 +++ b/json-tck/src/main/java/net/java/html/json/tests/KnockoutTest.java Mon Aug 24 22:37:29 2015 +0200
1.3 @@ -99,10 +99,10 @@
1.4 m.setChoice(Choice.A);
1.5 m.applyBindings();
1.6
1.7 - String v = getSetInput(null);
1.8 + String v = getSetInput("input", null);
1.9 assertEquals("A", v, "Value is really A: " + v);
1.10
1.11 - getSetInput("B");
1.12 + getSetInput("input", "B");
1.13 triggerEvent("input", "change");
1.14
1.15 assertEquals(Choice.B, m.getChoice(), "Enum property updated: " + m.getChoice());
1.16 @@ -112,6 +112,37 @@
1.17 Utils.exposeHTML(KnockoutTest.class, "");
1.18 }
1.19 }
1.20 +
1.21 +
1.22 + @KOTest public void modifyRadioValueOnEnum() throws Throwable {
1.23 + Object exp = Utils.exposeHTML(KnockoutTest.class,
1.24 + "<input id='i1' type=\"radio\" name=\"choice\" value=\"A\" data-bind=\"checked: choice\">Right\n" +
1.25 + "<input id='input' type=\"radio\" name=\"choice\" value=\"B\" data-bind=\"checked: choice\">Never\n" +
1.26 + "\n"
1.27 + );
1.28 + try {
1.29 +
1.30 + KnockoutModel m = Models.bind(new KnockoutModel(), newContext());
1.31 + m.setChoice(Choice.B);
1.32 + m.applyBindings();
1.33 +
1.34 + assertFalse(isChecked("i1"), "B should be checked now");
1.35 + assertTrue(isChecked("input"), "B should be checked now");
1.36 +
1.37 + triggerEvent("i1", "click");
1.38 + assertEquals(Choice.A, m.getChoice(), "Switched to A");
1.39 + assertTrue(isChecked("i1"), "A should be checked now");
1.40 + assertFalse(isChecked("input"), "A should be checked now");
1.41 +
1.42 + triggerEvent("input", "click");
1.43 +
1.44 + assertEquals(Choice.B, m.getChoice(), "Enum property updated: " + m.getChoice());
1.45 + } catch (Throwable t) {
1.46 + throw t;
1.47 + } finally {
1.48 + Utils.exposeHTML(KnockoutTest.class, "");
1.49 + }
1.50 + }
1.51
1.52 @KOTest public void modifyValueAssertChangeInModelOnDouble() throws Throwable {
1.53 Object exp = Utils.exposeHTML(KnockoutTest.class,
1.54 @@ -123,10 +154,10 @@
1.55 m.setLatitude(50.5);
1.56 m.applyBindings();
1.57
1.58 - String v = getSetInput(null);
1.59 + String v = getSetInput("input", null);
1.60 assertEquals("50.5", v, "Value is really 50.5: " + v);
1.61
1.62 - getSetInput("49.5");
1.63 + getSetInput("input", "49.5");
1.64 triggerEvent("input", "change");
1.65
1.66 assertEquals(49.5, m.getLatitude(), "Double property updated: " + m.getLatitude());
1.67 @@ -152,10 +183,10 @@
1.68 m.getFirstPerson().setLastName("Tulach");
1.69 m.applyBindings();
1.70
1.71 - String v = getSetInput(null);
1.72 + String v = getSetInput("input", null);
1.73 assertEquals("Jarda Tulach", v, "Value: " + v);
1.74
1.75 - getSetInput("Mickey Mouse");
1.76 + getSetInput("input", "Mickey Mouse");
1.77 triggerEvent("input", "change");
1.78
1.79 assertEquals("Mickey", m.getFirstPerson().getFirstName(), "First name updated");
1.80 @@ -177,10 +208,10 @@
1.81 m.setEnabled(true);
1.82 m.applyBindings();
1.83
1.84 - String v = getSetInput(null);
1.85 + String v = getSetInput("input", null);
1.86 assertEquals("true", v, "Value is really true: " + v);
1.87
1.88 - getSetInput("false");
1.89 + getSetInput("input", "false");
1.90 triggerEvent("input", "change");
1.91
1.92 assertFalse(m.isEnabled(), "Boolean property updated: " + m.isEnabled());
1.93 @@ -203,10 +234,10 @@
1.94 m.setName("Kukuc");
1.95 m.applyBindings();
1.96
1.97 - String v = getSetInput(null);
1.98 + String v = getSetInput("input", null);
1.99 assertEquals("Kukuc", v, "Value is really kukuc: " + v);
1.100
1.101 - getSetInput("Jardo");
1.102 + getSetInput("input", "Jardo");
1.103 triggerEvent("input", "change");
1.104
1.105 assertEquals("Jardo", m.getName(), "Name property updated: " + m.getName());
1.106 @@ -287,7 +318,7 @@
1.107 js.setName("Kukuc");
1.108 js.applyBindings();
1.109
1.110 - String v = getSetInput(null);
1.111 + String v = getSetInput("input", null);
1.112 assertEquals("Kukuc", v, "Value is really kukuc: " + v);
1.113
1.114 Timer t = new Timer("Set to Jardo");
1.115 @@ -299,7 +330,7 @@
1.116 }, 1);
1.117 }
1.118
1.119 - String v = getSetInput(null);
1.120 + String v = getSetInput("input", null);
1.121 if (!"Jardo".equals(v)) {
1.122 throw new InterruptedException();
1.123 }
1.124 @@ -307,17 +338,28 @@
1.125 Utils.exposeHTML(KnockoutTest.class, "");
1.126 }
1.127
1.128 - private static String getSetInput(String value) throws Exception {
1.129 + private static String getSetInput(String id, String value) throws Exception {
1.130 String s = "var value = arguments[0];\n"
1.131 - + "var n = window.document.getElementById('input'); \n "
1.132 + + "var n = window.document.getElementById(arguments[1]); \n "
1.133 + "if (value != null) n['value'] = value; \n "
1.134 + "return n['value'];";
1.135 Object ret = Utils.executeScript(
1.136 KnockoutTest.class,
1.137 - s, value
1.138 + s, value, id
1.139 );
1.140 return ret == null ? null : ret.toString();
1.141 }
1.142 +
1.143 + private static boolean isChecked(String id) throws Exception {
1.144 + String s = ""
1.145 + + "var n = window.document.getElementById(arguments[0]); \n "
1.146 + + "return n['checked'];";
1.147 + Object ret = Utils.executeScript(
1.148 + KnockoutTest.class,
1.149 + s, id
1.150 + );
1.151 + return Boolean.TRUE.equals(ret);
1.152 + }
1.153
1.154 public static void triggerEvent(String id, String ev) throws Exception {
1.155 Utils.executeScript(
2.1 --- a/ko4j/src/main/java/org/netbeans/html/ko4j/Knockout.java Sun Aug 02 10:03:54 2015 +0200
2.2 +++ b/ko4j/src/main/java/org/netbeans/html/ko4j/Knockout.java Mon Aug 24 22:37:29 2015 +0200
2.3 @@ -171,6 +171,7 @@
2.4 + " trigger();\n"
2.5 + " var r = activeGetter();\n"
2.6 + " activeGetter = realGetter;\n"
2.7 + + " r = @org.netbeans.html.ko4j.Knockout::noEnum(Ljava/lang/Object;)(r);\n"
2.8 + " if (r) try { var br = r.valueOf(); } catch (err) {}\n"
2.9 + " return br === undefined ? r: br;\n"
2.10 + " },\n"
2.11 @@ -230,4 +231,11 @@
2.12 return o;
2.13 }
2.14 }
2.15 +
2.16 + static Object noEnum(Object o) {
2.17 + if (o instanceof Enum) {
2.18 + return o.toString();
2.19 + }
2.20 + return o;
2.21 + }
2.22 }