Add a static method UIObject.ensureDebugId(Element, String) that allows users to add a debug ID to an arbitrary element.

Patch by: jlabanca
Review by: knorton (desk review)



git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@2851 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/user/src/com/google/gwt/user/client/ui/UIObject.java b/user/src/com/google/gwt/user/client/ui/UIObject.java
index 9759631..d6279a1 100644
--- a/user/src/com/google/gwt/user/client/ui/UIObject.java
+++ b/user/src/com/google/gwt/user/client/ui/UIObject.java
@@ -102,6 +102,11 @@
     // parameters
     public void ensureDebugId(UIObject uiObject, String id) {
     }
+
+    @SuppressWarnings("unused")
+    // parameters
+    public void ensureDebugId(Element elem, String id) {
+    }
   }
 
   /**
@@ -113,6 +118,11 @@
     public void ensureDebugId(UIObject uiObject, String id) {
       uiObject.onEnsureDebugId(id);
     }
+
+    @Override
+    public void ensureDebugId(Element elem, String id) {
+     UIObject.ensureDebugId(elem, "", id);
+    }
   }
 
   public static final String DEBUG_ID_PREFIX = "gwt-debug-";
@@ -130,6 +140,29 @@
   private static final String NULL_HANDLE_MSG = "Null widget handle. If you "
       + "are creating a composite, ensure that initWidget() has been called.";
 
+  /**
+   * <p>
+   * Ensure that elem has an ID property set, which allows it to integrate with
+   * third-party libraries and test tools.  If  elem already has an ID, this
+   * method WILL override it.  The ID that you specify will be prefixed by the
+   * static string {@link #DEBUG_ID_PREFIX}.
+   * </p>
+   * 
+   * <p>
+   * This method will be compiled out and will have no effect unless you inherit
+   * the DebugID module in your gwt.xml file by adding the following line:
+   * 
+   * <pre class="code">
+   * &lt;inherits name="com.google.gwt.user.Debug"/&gt;</pre>
+   * </p>
+   * 
+   * @param elem the target {@link Element}
+   * @param id the ID to set on the element
+   */
+  public static void ensureDebugId(Element elem, String id) {
+    debugIdImpl.ensureDebugId(elem, id);
+  }
+
   public static native boolean isVisible(Element elem) /*-{
     return (elem.style.display != 'none');
   }-*/;
@@ -476,7 +509,7 @@
    */
   public com.google.gwt.user.client.Element getElement() {
     assert (element != null) : MISSING_ELEMENT_ERROR;
-    return (com.google.gwt.user.client.Element)element;
+    return (com.google.gwt.user.client.Element) element;
   }
 
   /**
@@ -752,7 +785,7 @@
    * @param elem the object's element
    */
   protected final void setElement(Element elem) {
-    setElement((com.google.gwt.user.client.Element)elem);
+    setElement((com.google.gwt.user.client.Element) elem);
   }
 
   /**
diff --git a/user/test/com/google/gwt/user/client/ui/UIObjectTest.java b/user/test/com/google/gwt/user/client/ui/UIObjectTest.java
index a3ff445..7c151f1 100644
--- a/user/test/com/google/gwt/user/client/ui/UIObjectTest.java
+++ b/user/test/com/google/gwt/user/client/ui/UIObjectTest.java
@@ -112,6 +112,25 @@
     assertEquals("primary", o.getStylePrimaryName());
   }
 
+  public void testDebugIdOnElement() {
+    // Test basic set
+    Element oElem = DOM.createDiv();
+    UIObject.ensureDebugId(oElem, "test1");
+    assertDebugId("test1", oElem);
+
+    // Test override with new ID
+    UIObject.ensureDebugId(oElem, "test2");
+    assertDebugId("test2", oElem);
+
+    // Test setting actual id
+    DOM.setElementProperty(oElem, "id", "mytest");
+    assertEquals("mytest", DOM.getElementProperty(oElem, "id"));
+
+    // Test overriding with debug ID succeeds if ID present
+    UIObject.ensureDebugId(oElem, "test3");
+    assertDebugId("test3", oElem);
+  }
+
   public void testDebugId() {
     // Test basic set
     MyObject o = new MyObject();