Bug 441716 - test_retention_is_0_closes.xul is flakey on linux

The test now listens for a "domwindowclosed" notification from the window
watch service instead of using a timer which is unreliable.  Upon receiving the
notification, it dispatches an event to the main thread to perform the proper
checks to allow time for any other events listening to that topic to process.
This also adds a utils.js file that download manager chrome tests can use.  So
far, only one function has been added that will execute a function on the main
thread; executeSoon.
r=gavin
This commit is contained in:
Shawn Wilsher 2008-06-27 11:59:14 -04:00
parent 02d228ed27
commit 7aa9c26bc9
3 changed files with 88 additions and 28 deletions

View File

@ -61,6 +61,7 @@ _CHROME_FILES = \
test_space_key_pauses_resumes.xul \
test_ui_stays_open_on_alert_clickback.xul \
test_bug_429247.xul \
utils.js \
$(NULL)
ifneq (,$(filter cocoa, $(MOZ_WIDGET_TOOLKIT)))

View File

@ -53,13 +53,21 @@
src="chrome://mochikit/content/MochiKit/packed.js"/>
<script type="application/javascript"
src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
<script type="application/javascript"
src="chrome://mochikit/content/chrome/toolkit/mozapps/downloads/tests/chrome/utils.js"/>
<script type="application/javascript">
<![CDATA[
const Cc = Components.classes;
const Ci = Components.interfaces;
const Cr = Components.results;
function setPref(aDoTest)
{
let prefs = Cc["@mozilla.org/preferences-service;1"].
getService(Ci.nsIPrefBranch);
// If we're testing, set retention to auto-remove and auto-close
prefs.setIntPref("browser.download.manager.retention", aDoTest ? 0 : 2);
prefs.setBoolPref("browser.download.manager.closeWhenDone", aDoTest);
}
function bug413093obs()
{
@ -70,10 +78,6 @@ bug413093obs.prototype = {
observe: function(aSubject, aTopic, aData)
{
if ("domwindowopened" == aTopic) {
let ww = Cc["@mozilla.org/embedcomp/window-watcher;1"].
getService(Ci.nsIWindowWatcher);
ww.unregisterNotification(this);
// If we opened before we were paused, we need to set up our proper state
// We also should not try to resume (we weren't paused!)
if (!this.wasPaused) {
@ -88,14 +92,22 @@ bug413093obs.prototype = {
let dm = Cc["@mozilla.org/download-manager;1"].
getService(Ci.nsIDownloadManager);
dm.resumeDownload(this.mDownload.id);
} else if ("timer-callback" == aTopic) {
this.setPref(false);
}
else if("domwindowclosed" == aTopic) {
let ww = Cc["@mozilla.org/embedcomp/window-watcher;1"].
getService(Ci.nsIWindowWatcher);
ww.unregisterNotification(this);
let dmui = Cc["@mozilla.org/download-manager-ui;1"].
getService(Ci.nsIDownloadManagerUI);
ok(!dmui.visible, "Download Manager UI is not showing");
// Let the UI finish doing whatever it needs to do
executeSoon(function() {
setPref(false);
SimpleTest.finish();
let dmui = Cc["@mozilla.org/download-manager-ui;1"].
getService(Ci.nsIDownloadManagerUI);
ok(!dmui.visible, "Download Manager UI is not showing");
SimpleTest.finish();
});
}
},
@ -119,25 +131,12 @@ bug413093obs.prototype = {
getService(Ci.nsIDownloadManager);
dm.removeListener(this);
// We have to do this on a timer so other JS stuff that handles the UI
// can actually catch up to us...
let timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
timer.init(this, 1000, Ci.nsITimer.TYPE_ONE_SHOT);
// If this test is going to pass, we'll get a domwindowclosed notification
}
},
onStateChange: function(a, b, c, d, e) { },
onProgressChange: function(a, b, c, d, e, f, g) { },
onSecurityChange: function(a, b, c, d) { },
setPref: function(aDoTest)
{
let prefs = Cc["@mozilla.org/preferences-service;1"].
getService(Ci.nsIPrefBranch);
// If we're testing, set retention to auto-remove and auto-close
prefs.setIntPref("browser.download.manager.retention", aDoTest ? 0 : 2);
prefs.setBoolPref("browser.download.manager.closeWhenDone", aDoTest);
}
};
function test()
{
@ -186,7 +185,7 @@ function test()
let obs = new bug413093obs();
dm.addListener(obs);
obs.setPref(true);
setPref(true);
// Start the test when the download manager window loads
let ww = Cc["@mozilla.org/embedcomp/window-watcher;1"].

View File

@ -0,0 +1,60 @@
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is
* Mozilla Corporation.
* Portions created by the Initial Developer are Copyright (C) 2008
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Shawn Wilsher <me@shawnwilsher.com> (Original Author)
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* 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 ***** */
/**
* Provides utility functions for the download manager chrome tests.
*/
const Cc = Components.classes;
const Ci = Components.interfaces;
const Cr = Components.results;
/**
* Executes a function shortly after the call, but lets the caller continue
* working (or finish).
*/
function executeSoon(aFunc)
{
let tm = Cc["@mozilla.org/thread-manager;1"].getService(Ci.nsIThreadManager);
tm.mainThread.dispatch({
run: function()
{
aFunc();
}
}, Ci.nsIThread.DISPATCH_NORMAL);
}