Bug 551540 followup - Wait for the Organizer window to be closed before proceeding; r=dolske a=orange-fix
authorEhsan Akhgari <ehsan@mozilla.com>
Thu, 14 Oct 2010 15:38:56 -0400
changeset 56336 89963dd9890fb2b396a720da84c9701efbe51f4e
parent 56335 f4a06cca9fb95118a0565c5b58d2ee1f24f6b0f5
child 56337 112490ab7b5856dede85e38bf6bef0b9755fc278
push idunknown
push userunknown
push dateunknown
reviewersdolske, orange-fix
bugs551540
milestone2.0b8pre
Bug 551540 followup - Wait for the Organizer window to be closed before proceeding; r=dolske a=orange-fix
browser/components/places/tests/browser/browser_forgetthissite_single.js
browser/components/privatebrowsing/test/browser/browser_privatebrowsing_forgetthissite.js
--- a/browser/components/places/tests/browser/browser_forgetthissite_single.js
+++ b/browser/components/places/tests/browser/browser_forgetthissite_single.js
@@ -33,16 +33,34 @@
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
 // This test makes sure that the Forget This Site command is hidden for multiple
 // selections.
 
+/**
+ * Clears history invoking callback when done.
+ */
+function waitForClearHistory(aCallback) {
+  const TOPIC_EXPIRATION_FINISHED = "places-expiration-finished";
+  let observer = {
+    observe: function(aSubject, aTopic, aData) {
+      Services.obs.removeObserver(this, TOPIC_EXPIRATION_FINISHED);
+      aCallback();
+    }
+  };
+  Services.obs.addObserver(observer, TOPIC_EXPIRATION_FINISHED, false);
+
+  let hs = Cc["@mozilla.org/browser/nav-history-service;1"].
+           getService(Ci.nsINavHistoryService);
+  hs.QueryInterface(Ci.nsIBrowserHistory).removeAllPages();
+}
+
 function test() {
   // initialization
   let ww = Cc["@mozilla.org/embedcomp/window-watcher;1"].
            getService(Ci.nsIWindowWatcher);
   waitForExplicitFinish();
 
   // Add a history entry.
   let TEST_URIs = ["http://www.mozilla.org/test1", "http://www.mozilla.org/test2"];
@@ -56,16 +74,17 @@ function test() {
 
   function testForgetThisSiteVisibility(selectionCount, funcNext) {
     function observer(aSubject, aTopic, aData) {
       if (aTopic != "domwindowopened")
         return;
       ww.unregisterNotification(observer);
       let organizer = aSubject.QueryInterface(Ci.nsIDOMWindow);
       SimpleTest.waitForFocus(function() {
+        executeSoon(function() {
           // Select History in the left pane.
           organizer.PlacesOrganizer.selectLeftPaneQuery('History');
           let PO = organizer.PlacesOrganizer;
           let histContainer = PO._places.selectedNode.QueryInterface(Ci.nsINavHistoryContainerResultNode);
           histContainer.containerOpen = true;
           PO._places.selectNode(histContainer.getChild(0));
           // Select the first history entry.
           let doc = organizer.document;
@@ -81,39 +100,47 @@ function test() {
             contextmenu.removeEventListener("popupshown", arguments.callee, false);
             let forgetThisSite = doc.getElementById("placesContext_deleteHost");
             let hideForgetThisSite = (selectionCount != 1);
             is(forgetThisSite.hidden, hideForgetThisSite,
               "The Forget this site menu item should " + (hideForgetThisSite ? "" : "not ") +
               "be hidden with " + selectionCount + " items selected");
             // Close the context menu
             contextmenu.hidePopup();
+            // Wait for the Organizer window to actually be closed
+            function closeObserver(aSubject, aTopic, aData) {
+              if (aTopic != "domwindowclosed")
+                return;
+              ww.unregisterNotification(closeObserver);
+              SimpleTest.waitForFocus(function() {
+                // Proceed
+                funcNext();
+              });
+            }
+            ww.registerNotification(closeObserver);
             // Close Library window.
             organizer.close();
-            // Proceed
-            funcNext();
           }, false);
           // Get cell coordinates
           var x = {}, y = {}, width = {}, height = {};
           tree.treeBoxObject.getCoordsForCellItem(0, tree.columns[0], "text",
                                                   x, y, width, height);
           // Initiate a context menu for the selected cell
           EventUtils.synthesizeMouse(tree.body, x + 4, y + 4, {type: "contextmenu"}, organizer);
+        });
       }, organizer);
     }
 
     ww.registerNotification(observer);
     ww.openWindow(null,
                   "chrome://browser/content/places/places.xul",
                   "",
                   "chrome,toolbar=yes,dialog=no,resizable",
                   null);
   }
 
   testForgetThisSiteVisibility(1, function() {
     testForgetThisSiteVisibility(2, function() {
       // Cleanup
-      history.QueryInterface(Ci.nsIBrowserHistory)
-             .removeAllPages();
-      finish();
+      waitForClearHistory(finish);
     });
   });
 }
--- a/browser/components/privatebrowsing/test/browser/browser_privatebrowsing_forgetthissite.js
+++ b/browser/components/privatebrowsing/test/browser/browser_privatebrowsing_forgetthissite.js
@@ -33,16 +33,34 @@
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
 // This test makes sure that the Forget This Site command is hidden in private
 // browsing mode.
 
+/**
+ * Clears history invoking callback when done.
+ */
+function waitForClearHistory(aCallback) {
+  const TOPIC_EXPIRATION_FINISHED = "places-expiration-finished";
+  let observer = {
+    observe: function(aSubject, aTopic, aData) {
+      Services.obs.removeObserver(this, TOPIC_EXPIRATION_FINISHED);
+      aCallback();
+    }
+  };
+  Services.obs.addObserver(observer, TOPIC_EXPIRATION_FINISHED, false);
+
+  let hs = Cc["@mozilla.org/browser/nav-history-service;1"].
+           getService(Ci.nsINavHistoryService);
+  hs.QueryInterface(Ci.nsIBrowserHistory).removeAllPages();
+}
+
 function test() {
   // initialization
   let pb = Cc["@mozilla.org/privatebrowsing;1"].
            getService(Ci.nsIPrivateBrowsingService);
   waitForExplicitFinish();
 
   // Add a history entry.
   const TEST_URI = "http://www.mozilla.org/privatebrowsing";
@@ -55,16 +73,17 @@ function test() {
   function testForgetThisSiteVisibility(expected, funcNext) {
     function observer(aSubject, aTopic, aData) {
       if (aTopic != "domwindowopened")
         return;
 
       Services.ww.unregisterNotification(observer);
       let organizer = aSubject.QueryInterface(Ci.nsIDOMWindow);
       SimpleTest.waitForFocus(function() {
+        executeSoon(function() {
           // Select History in the left pane.
           let PO = organizer.PlacesOrganizer;
           PO.selectLeftPaneQuery('History');
           let histContainer = PO._places.selectedNode.QueryInterface(Ci.nsINavHistoryContainerResultNode);
           histContainer.containerOpen = true;
           PO._places.selectNode(histContainer.getChild(0));
           // Select the first history entry.
           let doc = organizer.document;
@@ -80,27 +99,37 @@ function test() {
             let forgetThisSite = doc.getElementById("placesContext_deleteHost");
             is(forgetThisSite.hidden, !expected,
               "The Forget This Site menu item should " + (expected ? "not " : "") + "be hidden");
             let forgetThisSiteCmd = doc.getElementById("placesCmd_deleteDataHost");
             if (forgetThisSiteCmd.disabled, !expected,
               "The Forget This Site command should " + (expected ? "not " : "") + "be disabled");
             // Close the context menu
             contextmenu.hidePopup();
+            // Wait for the Organizer window to actually be closed
+            function closeObserver(aSubject, aTopic, aData) {
+              if (aTopic != "domwindowclosed")
+                return;
+              Services.ww.unregisterNotification(closeObserver);
+              SimpleTest.waitForFocus(function() {
+                // Proceed
+                funcNext();
+              });
+            }
+            Services.ww.registerNotification(closeObserver);
             // Close Library window.
             organizer.close();
-            // Proceed
-            funcNext();
           }, false);
           // Get cell coordinates
           var x = {}, y = {}, width = {}, height = {};
           tree.treeBoxObject.getCoordsForCellItem(0, tree.columns[0], "text",
                                                   x, y, width, height);
           // Initiate a context menu for the selected cell
           EventUtils.synthesizeMouse(tree.body, x + 4, y + 4, {type: "contextmenu"}, organizer);
+        });
       }, organizer);
     }
 
     Services.ww.registerNotification(observer);
     Services.ww.openWindow(null,
                            "chrome://browser/content/places/places.xul",
                            "",
                            "chrome,toolbar=yes,dialog=no,resizable",
@@ -110,15 +139,13 @@ function test() {
   testForgetThisSiteVisibility(true, function() {
     // Enter private browsing mode
     pb.privateBrowsingEnabled = true;
     testForgetThisSiteVisibility(false, function() {
       // Leave private browsing mode
       pb.privateBrowsingEnabled = false;
       testForgetThisSiteVisibility(true, function() {
         // Cleanup
-        history.QueryInterface(Ci.nsIBrowserHistory)
-               .removeAllPages();
-        finish();
+        waitForClearHistory(finish);
       });
     });
   });
 }