Bug 932898 - Fix leaks in DOM and browser tests. r=smaug, r=jaws, a=test-only
authorTim Taubert <ttaubert@mozilla.com>
Thu, 31 Oct 2013 02:30:00 +0000
changeset 167306 dee754949ca0a432d55da36d3f9ece8d92012e71
parent 167305 12e87b3b86c63ec13d26a2f9fb17595261301461
child 167307 e2b3761232a02b0a588f17e283021dd9994c1a03
push id428
push userbbajaj@mozilla.com
push dateTue, 28 Jan 2014 00:16:25 +0000
treeherdermozilla-release@cd72a7ff3a75 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug, jaws, test-only
bugs932898
milestone27.0a2
Bug 932898 - Fix leaks in DOM and browser tests. r=smaug, r=jaws, a=test-only
browser/base/content/browser.js
browser/base/content/test/general/browser_CTP_drag_drop.js
browser/base/content/test/general/browser_pageInfo_plugins.js
browser/base/content/test/general/browser_private_no_prompt.js
dom/indexedDB/test/browser_bug839193.js
dom/tests/browser/browser_xhr_sandbox.js
--- a/browser/base/content/browser.js
+++ b/browser/base/content/browser.js
@@ -3225,20 +3225,32 @@ function OpenBrowserWindow(options)
   var telemetryObj = {};
   TelemetryStopwatch.start("FX_NEW_WINDOW_MS", telemetryObj);
 
   function newDocumentShown(doc, topic, data) {
     if (topic == "document-shown" &&
         doc != document &&
         doc.defaultView == win) {
       Services.obs.removeObserver(newDocumentShown, "document-shown");
+      Services.obs.removeObserver(windowClosed, "domwindowclosed");
       TelemetryStopwatch.finish("FX_NEW_WINDOW_MS", telemetryObj);
     }
-  };
+  }
+
+  function windowClosed(subject) {
+    if (subject == win) {
+      Services.obs.removeObserver(newDocumentShown, "document-shown");
+      Services.obs.removeObserver(windowClosed, "domwindowclosed");
+    }
+  }
+
+  // Make sure to remove the 'document-shown' observer in case the window
+  // is being closed right after it was opened to avoid leaking.
   Services.obs.addObserver(newDocumentShown, "document-shown", false);
+  Services.obs.addObserver(windowClosed, "domwindowclosed", false);
 
   var charsetArg = new String();
   var handler = Components.classes["@mozilla.org/browser/clh;1"]
                           .getService(Components.interfaces.nsIBrowserHandler);
   var defaultArgs = handler.defaultArgs;
   var wintype = document.documentElement.getAttribute('windowtype');
 
   var extraFeatures = "";
--- a/browser/base/content/test/general/browser_CTP_drag_drop.js
+++ b/browser/base/content/test/general/browser_CTP_drag_drop.js
@@ -90,10 +90,11 @@ function part8() {
   // Click the activate button on doorhanger to make sure it works
   gNewWindow.PopupNotifications.panel.firstChild._primaryButton.click();
 
   let plugin = gNewWindow.gBrowser.selectedBrowser.contentDocument.getElementById("test");
   let objLoadingContent = plugin.QueryInterface(Ci.nsIObjectLoadingContent);
   ok(objLoadingContent.activated, "plugin should be activated now");
 
   gNewWindow.close();
+  gNewWindow = null;
   finish();
 }
--- a/browser/base/content/test/general/browser_pageInfo_plugins.js
+++ b/browser/base/content/test/general/browser_pageInfo_plugins.js
@@ -37,17 +37,24 @@ function pageInfoObserve(win, topic, dat
   executeSoon(gNextTest);
 }
 
 function finishTest() {
   gPermissionManager.remove("127.0.0.1:8888", gTestPermissionString);
   gPermissionManager.remove("127.0.0.1:8888", gSecondTestPermissionString);
   Services.prefs.clearUserPref("plugins.click_to_play");
   gBrowser.removeCurrentTab();
-  finish();
+
+  gPageInfo = null;
+  gNextTest = null;
+  gTestBrowser = null;
+  gPluginHost = null;
+  gPermissionManager = null;
+
+  executeSoon(finish);
 }
 
 function test() {
   waitForExplicitFinish();
   Services.prefs.setBoolPref("plugins.click_to_play", true);
   setTestPluginEnabledState(Ci.nsIPluginTag.STATE_CLICKTOPLAY);
   setTestPluginEnabledState(Ci.nsIPluginTag.STATE_ENABLED, "Second Test Plug-in");
   gBrowser.selectedTab = gBrowser.addTab();
--- a/browser/base/content/test/general/browser_private_no_prompt.js
+++ b/browser/base/content/test/general/browser_private_no_prompt.js
@@ -1,13 +1,12 @@
 function test() {
   waitForExplicitFinish();
   var privateWin = OpenBrowserWindow({private: true});
-  privateWin.addEventListener("load", function onload() {
-    privateWin.removeEventListener("load", onload, false);
-    ok(true, "Load listener called");
 
+  whenDelayedStartupFinished(privateWin, function () {
     privateWin.BrowserOpenTab();
     privateWin.BrowserTryToCloseWindow();
     ok(true, "didn't prompt");
-    finish();                        
-  }, false);
-}
\ No newline at end of file
+
+    executeSoon(finish);
+  });
+}
--- a/dom/indexedDB/test/browser_bug839193.js
+++ b/dom/indexedDB/test/browser_bug839193.js
@@ -7,30 +7,30 @@ let gBugWindow = null;
 let gIterations = 5;
 
 function onLoad() {
   gBugWindow.close();
 }
 
 function onUnload() {
   if (!gIterations) {
+    gBugWindow = null;
+    Services.obs.removeObserver(onLoad, "bug839193-loaded");
+    Services.obs.removeObserver(onUnload, "bug839193-unloaded");
+
     window.focus();
     finish();
   } else {
     gBugWindow = window.openDialog(gTestRoot + "bug839193.xul");
     gIterations--;
   }
 }
 
 // This test is about leaks, which are handled by the test harness, so
 // there are no actual checks here. Whether or not this test passes or fails
 // will be apparent by the checks the harness performs.
 function test() {
   waitForExplicitFinish();
-  Components.classes["@mozilla.org/observer-service;1"]
-            .getService(Components.interfaces.nsIObserverService)
-            .addObserver(onLoad, "bug839193-loaded", false);
-  Components.classes["@mozilla.org/observer-service;1"]
-            .getService(Components.interfaces.nsIObserverService)
-            .addObserver(onUnload, "bug839193-unloaded", false);
+  Services.obs.addObserver(onLoad, "bug839193-loaded", false);
+  Services.obs.addObserver(onUnload, "bug839193-unloaded", false);
 
   gBugWindow = window.openDialog(gTestRoot + "bug839193.xul");
 }
--- a/dom/tests/browser/browser_xhr_sandbox.js
+++ b/dom/tests/browser/browser_xhr_sandbox.js
@@ -29,16 +29,17 @@ function test() {
   let frame = doc.createElement("iframe");
   frame.setAttribute("type", "content");
   frame.setAttribute("src", "http://mochi.test:8888/browser/dom/tests/browser/browser_xhr_sandbox.js");
 
   frame.addEventListener("load", function () {
     let workerWindow = frame.contentWindow;
     workerWindow.addEventListener("message", function(evt) {
       is(evt.data.result, "ok", "check the sandbox code was happy");
+      frame.remove();
       finish();
     }, true);
     let sandbox = new Cu.Sandbox(workerWindow);
     // inject some functions from the window into the sandbox.
     // postMessage so the async code in the sandbox can report a result.
     sandbox.importFunction(workerWindow.postMessage.bind(workerWindow), "postMessage");
     sandbox.importFunction(workerWindow.XMLHttpRequest, "XMLHttpRequest");
     Cu.evalInSandbox(sandboxCode, sandbox, "1.8");