Merge of c4628, c4639 from branches/snapshot-2009.01.29.


git-svn-id: https://google-web-toolkit.googlecode.com/svn/releases/1.6@4654 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/user/src/com/google/gwt/user/client/ui/PopupPanel.java b/user/src/com/google/gwt/user/client/ui/PopupPanel.java
index 640aa87..8155ae4 100644
--- a/user/src/com/google/gwt/user/client/ui/PopupPanel.java
+++ b/user/src/com/google/gwt/user/client/ui/PopupPanel.java
@@ -463,14 +463,10 @@
    *          {@link CloseHandler#onClose(CloseEvent)} when the popup is closed
    */
   public void hide(boolean autoClosed) {
-    if (!showing) {
+    if (!isShowing()) {
       return;
     }
-    showing = false;
-    if (nativePreviewHandlerRegistration != null) {
-      nativePreviewHandlerRegistration.removeHandler();
-      nativePreviewHandlerRegistration = null;
-    }
+    cleanup();
 
     // Hide the popup
     resizeAnimation.setState(false);
@@ -589,6 +585,14 @@
     return true;
   }
 
+  @Override
+  protected void onUnload() {
+    // Just to be sure, we perform cleanup when the popup is unloaded (i.e.
+    // removed from the DOM). This is normally taken care of in hide(), but it
+    // can be missed if someone removes the popup directly from the RootPanel.
+    cleanup();
+  }
+
   /**
    * Remove an autoHide partner.
    * 
@@ -890,6 +894,16 @@
     }
   }-*/;
 
+  private void cleanup() {
+    // Clear the 'showing' flag and make sure that the event preview is cleaned
+    // up.
+    showing = false;
+    if (nativePreviewHandlerRegistration != null) {
+      nativePreviewHandlerRegistration.removeHandler();
+      nativePreviewHandlerRegistration = null;
+    }
+  }
+
   /**
    * Does the event target one of the partner elements?
    * 
diff --git a/user/test/com/google/gwt/user/client/ui/DialogBoxTest.java b/user/test/com/google/gwt/user/client/ui/DialogBoxTest.java
index 04a9127..ef908a6 100644
--- a/user/test/com/google/gwt/user/client/ui/DialogBoxTest.java
+++ b/user/test/com/google/gwt/user/client/ui/DialogBoxTest.java
@@ -15,6 +15,8 @@
  */
 package com.google.gwt.user.client.ui;
 
+import com.google.gwt.event.dom.client.ClickEvent;
+import com.google.gwt.event.dom.client.ClickHandler;
 import com.google.gwt.user.client.Command;
 import com.google.gwt.user.client.DOM;
 import com.google.gwt.user.client.DeferredCommand;
@@ -68,6 +70,20 @@
     assertEquals("text", dialogBox.getText());
     assertTrue(dialogBox.getHTML().equalsIgnoreCase("<b>text</b>"));
   }
+  
+  public void testSimpleCloseButtonOnModalDialog() {
+    final DialogBox dialogBox = new DialogBox(false, true);
+    Button button = new Button();
+    button.addClickHandler(new ClickHandler() {
+      public void onClick(ClickEvent event) {
+        dialogBox.hide();
+      }
+    });
+    dialogBox.add(button);
+    dialogBox.show();
+    button.click();
+    assertFalse(dialogBox.isShowing());
+  }
 
   public void testDebugId() {
     DialogBox dBox = new DialogBox();
@@ -86,7 +102,6 @@
     // Check the header IDs
     DeferredCommand.addCommand(new Command() {
       public void execute() {
-        String prefix = UIObject.DEBUG_ID_PREFIX;
         UIObjectTest.assertDebugIdContents("myDialogBox-caption",
             "test caption");
         finishTest();