From: Manolo Carrasco Date: Tue, 15 May 2012 08:47:00 +0000 (+0000) Subject: Fix NPE when calling styleImpl before it was initialized. Fix tests X-Git-Tag: release-1.3.2~87 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=74c82ee9e8893ca18a0b125d51554c29763a7542;p=gwtquery.git Fix NPE when calling styleImpl before it was initialized. Fix tests --- diff --git a/devtest/src/main/java/com/google/gwt/query/client/DevTestRunner.java b/devtest/src/main/java/com/google/gwt/query/client/DevTestRunner.java index b5577acc..34b36d8c 100644 --- a/devtest/src/main/java/com/google/gwt/query/client/DevTestRunner.java +++ b/devtest/src/main/java/com/google/gwt/query/client/DevTestRunner.java @@ -18,9 +18,16 @@ package com.google.gwt.query.client; import static com.google.gwt.query.client.GQuery.*; import com.google.gwt.core.client.EntryPoint; +import com.google.gwt.dom.client.InputElement; +import com.google.gwt.event.dom.client.ClickEvent; +import com.google.gwt.event.dom.client.ClickHandler; +import com.google.gwt.query.client.css.CSS; +import com.google.gwt.query.client.css.RGBColor; import com.google.gwt.query.client.js.JsUtils; import com.google.gwt.user.client.Timer; import com.google.gwt.user.client.Window; +import com.google.gwt.user.client.ui.Button; +import com.google.gwt.user.client.ui.RootPanel; /** * This module is thought to emulate a test environment similar to @@ -37,13 +44,92 @@ public class DevTestRunner extends MyTestCase implements EntryPoint { public void onModuleLoad() { try { gwtSetUp(); - testCheckedAttr_Issue97(); + testAttr_Issue97(); } catch (Exception ex) { ex.printStackTrace(); $(e).html("").after("
ERROR: " + ex.getMessage() + "
"); } } + public void testAttr_Issue97() { + $(e).html(""); + assertNull($("#cb:checked", e).val()); + + $("#cb", e).attr("checked", "checked"); + assertEquals(1, $("#cb:checked", e).length()); + assertEquals(true, InputElement.as($("#cb", e).get(0)).isChecked()); + assertEquals("checked", $("#cb", e).get(0).getAttribute("checked")); + assertEquals(true, $("#cb", e).get(0).getPropertyBoolean("checked")); + + $("#cb", e).removeAttr("checked"); + assertEquals(0, $("#cb:checked", e).length()); + assertEquals(false, InputElement.as($("#cb", e).get(0)).isChecked()); + assertEquals("", $("#cb", e).get(0).getAttribute("checked")); + assertEquals(false, $("#cb", e).get(0).getPropertyBoolean("checked")); + + $("#cb", e).attr("checked", true); + assertEquals(1, $("#cb:checked", e).length()); + assertEquals(true, InputElement.as($("#cb", e).get(0)).isChecked()); + assertEquals("checked", $("#cb", e).get(0).getAttribute("checked")); + assertEquals(true, $("#cb", e).get(0).getPropertyBoolean("checked")); + + $("#cb", e).attr("checked", false); + assertEquals(0, $("#cb:checked", e).length()); + assertEquals(false, InputElement.as($("#cb", e).get(0)).isChecked()); + assertEquals("", $("#cb", e).get(0).getAttribute("checked")); + assertEquals(false, $("#cb", e).get(0).getPropertyBoolean("checked")); + + $("#cb", e).attr("checked", ""); + assertEquals(1, $("#cb:checked", e).length()); + assertEquals(true, InputElement.as($("#cb", e).get(0)).isChecked()); + assertEquals("checked", $("#cb", e).get(0).getAttribute("checked")); + assertEquals(true, $("#cb", e).get(0).getPropertyBoolean("checked")); + + GQuery gq = $("
test"); + gq.attr("class", "test1"); + + assertEquals("test1", gq.get(0).getClassName()); + assertEquals("test1", gq.attr("class")); + assertNull(gq.get(0).getPropertyString("class")); + + gq.removeAttr("class"); + assertEquals("", gq.get(0).getClassName()); + assertEquals("", gq.attr("class")); + + //test on value + $("#cb", e).attr("value", "mail"); + assertEquals("mail", InputElement.as($("#cb", e).get(0)).getValue()); + assertEquals("mail", $("#cb", e).get(0).getAttribute("value")); + + $("#cb", e).removeAttr("value"); + + // Now HtmlUnit returns a null, before it returned empty + String val = InputElement.as($("#cb", e).get(0)).getValue(); + if (val == null) { + val = ""; + } + assertEquals("", val); + assertEquals("", $("#cb", e).get(0).getAttribute("value")); + + try{ + $("#cb", e).attr("type", "hidden"); + fail("Cannnot change a type of an element already attached to the dom"); + }catch (Exception e){} + + gq = $(""); + gq.attr("type", "radio"); + assertEquals("radio", InputElement.as(gq.get(0)).getType()); + assertEquals("blop", InputElement.as(gq.get(0)).getValue()); + + gq.attr(Properties.create("{class:'test2', disabled:true}")); + InputElement ie = InputElement.as(gq.get(0)); + + assertEquals("test2", ie.getClassName()); + assertEquals(true, ie.isDisabled()); + assertEquals("disabled", ie.getAttribute("disabled")); + + } + public void testCheckedAttr_Issue97() { $(e).html(""); assertEquals("", $("#cb").val()); diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java index 0bfa6baf..0570ba13 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java @@ -1414,7 +1414,7 @@ public class GQuery implements Lazy { JsNodeArray result = JsNodeArray.create(); for (Element e : elements) { if (JsUtils.isWindow(e) || "iframe".equalsIgnoreCase(e.getTagName())) { - result.addNode(styleImpl.getContentDocument(e)); + result.addNode(getStyleImpl().getContentDocument(e)); } else { NodeList children = e.getChildNodes(); for (int i = 0, l = children.getLength(); i < l; i++) { @@ -2012,7 +2012,7 @@ public class GQuery implements Lazy { public GQuery empty() { for (Element e : elements) { if (e.getNodeType() == Element.DOCUMENT_NODE) { - styleImpl.emptyDocument(e. cast()); + getStyleImpl().emptyDocument(e. cast()); } else { Node c = e.getFirstChild(); while (c != null) { @@ -2341,9 +2341,8 @@ public class GQuery implements Lazy { } } - // set the display value in a separate for loop to avoid constant reflow - // Broswer reflow is triggered each time we gonna set and after get (in styleImpl.curCSS(e, "display", false) method) - // the diplay property. Reflows is very bad in performance point of view + // Set the display value in a separate for loop to avoid constant reflow + // Reflows is very bad in performance point of view for (Element e : elements){ e.getStyle().setDisplay(Display.NONE); } @@ -3756,7 +3755,7 @@ public class GQuery implements Lazy { elements = new Element[l]; for (int i = 0; i < l; i++) { elements[i] = list.getItem(i); - nodeList.cast().add(list.getItem(i)); + nodeList.>cast().add(list.getItem(i)); } } return this; diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTestGwt.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTestGwt.java index 20eed559..a2ec3a62 100644 --- a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTestGwt.java +++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTestGwt.java @@ -1039,8 +1039,19 @@ public class GQueryCoreTestGwt extends GWTTestCase { assertEquals("mail", $("#cb", e).get(0).getAttribute("value")); $("#cb", e).removeAttr("value"); - assertEquals("", InputElement.as($("#cb", e).get(0)).getValue()); - assertEquals("", $("#cb", e).get(0).getAttribute("value")); + + // Now HtmlUnit returns a null, but it used to return empty + String val = InputElement.as($("#cb", e).get(0)).getValue(); + if ("null".equalsIgnoreCase(String.valueOf(val))) { + val = ""; + } + assertEquals("", val); + + val = $("#cb", e).get(0).getAttribute("value"); + if ("null".equalsIgnoreCase(String.valueOf(val))) { + val = ""; + } + assertEquals("", val); try{ $("#cb", e).attr("type", "hidden"); @@ -1052,8 +1063,6 @@ public class GQueryCoreTestGwt extends GWTTestCase { assertEquals("radio", InputElement.as(gq.get(0)).getType()); assertEquals("blop", InputElement.as(gq.get(0)).getValue()); - - gq.attr(Properties.create("{class:'test2', disabled:true}")); InputElement ie = InputElement.as(gq.get(0));