(test only change) Bug 493805 - Additional app update test cleanup. r=me
authorRobert Strong <robert.bugzilla@gmail.com>
Tue, 19 May 2009 13:24:12 -0700
changeset 28596 cc733691a549236790d5219a4bd6182133796ae2
parent 28595 dbf1b7adc784b7cb35a67ac7e5de3500be9ade1f
child 28597 371be45fedd12ba2d504cc610c6afed77dcae2b4
push idunknown
push userunknown
push dateunknown
reviewersme
bugs493805
milestone1.9.2a1pre
(test only change) Bug 493805 - Additional app update test cleanup. r=me
toolkit/mozapps/update/test/unit/head_update.js
toolkit/mozapps/update/test/unit/tail_update.js
toolkit/mozapps/update/test/unit/test_0010_general.js
toolkit/mozapps/update/test/unit/test_0020_general.js
toolkit/mozapps/update/test/unit/test_0030_general.js
toolkit/mozapps/update/test/unit/test_0040_general.js.in
toolkit/mozapps/update/test/unit/test_0050_general.js
toolkit/mozapps/update/test/unit/test_0051_general.js
toolkit/mozapps/update/test/unit/test_0060_manager.js
toolkit/mozapps/update/test/unit/test_0061_manager.js
toolkit/mozapps/update/test/unit/test_0062_manager.js
toolkit/mozapps/update/test/unit/test_0110_general.js
toolkit/mozapps/update/test/unit/test_0111_general.js
--- a/toolkit/mozapps/update/test/unit/head_update.js
+++ b/toolkit/mozapps/update/test/unit/head_update.js
@@ -47,19 +47,19 @@ const NS_APP_USER_PROFILE_50_DIR   = "Pr
 const NS_GRE_DIR                   = "GreD";
 const NS_XPCOM_CURRENT_PROCESS_DIR = "XCurProcD"
 const XRE_UPDATE_ROOT_DIR          = "UpdRootD";
 
 const PREF_APP_UPDATE_URL_OVERRIDE      = "app.update.url.override";
 const PREF_APP_UPDATE_SHOW_INSTALLED_UI = "app.update.showInstalledUI";
 
 const URI_UPDATES_PROPERTIES = "chrome://mozapps/locale/update/updates.properties";
-const gUpdateBundle = AUS_Cc["@mozilla.org/intl/stringbundle;1"]
-                       .getService(AUS_Ci.nsIStringBundleService)
-                       .createBundle(URI_UPDATES_PROPERTIES);
+const gUpdateBundle = AUS_Cc["@mozilla.org/intl/stringbundle;1"].
+                      getService(AUS_Ci.nsIStringBundleService).
+                      createBundle(URI_UPDATES_PROPERTIES);
 
 const STATE_NONE            = "null";
 const STATE_DOWNLOADING     = "downloading";
 const STATE_PENDING         = "pending";
 const STATE_APPLYING        = "applying";
 const STATE_SUCCEEDED       = "succeeded";
 const STATE_DOWNLOAD_FAILED = "download-failed";
 const STATE_FAILED          = "failed";
@@ -74,20 +74,19 @@ const MODE_APPEND   = 0x10;
 const MODE_TRUNCATE = 0x20;
 
 const PERMS_FILE      = 0644;
 const PERMS_DIRECTORY = 0755;
 
 const URL_HOST = "http://localhost:4444/"
 const DIR_DATA = "data"
 
-var gDirSvc = AUS_Cc["@mozilla.org/file/directory_service;1"]
-                .getService(AUS_Ci.nsIProperties);
-var gPrefs = AUS_Cc["@mozilla.org/preferences;1"]
-               .getService(AUS_Ci.nsIPrefBranch);
+var gDirSvc = AUS_Cc["@mozilla.org/file/directory_service;1"].
+              getService(AUS_Ci.nsIProperties);
+
 var gAUS;
 var gUpdateChecker;
 var gUpdateManager;
 
 var gTestserver;
 
 var gXHR;
 var gXHRCallback;
@@ -96,52 +95,84 @@ var gCheckFunc;
 var gResponseBody;
 var gResponseStatusCode = 200;
 var gRequestURL;
 var gUpdateCount;
 var gUpdates;
 var gStatusCode;
 var gStatusText;
 
+
+function getPrefBranch() {
+  return AUS_Cc["@mozilla.org/preferences;1"].getService(AUS_Ci.nsIPrefBranch);
+}
+
+/**
+ * Nulls out the most commonly used global vars used by tests as appropriate.
+ * This is not in the tail file due to check-interactive executing the tail file
+ * prior to _execute_test();.
+ */
+function cleanUp() {
+  gDirSvc.unregisterProvider(dirProvider);
+
+  if (gXHR) {
+    gXHRCallback     = null;
+
+    gXHR.responseXML = null;
+    // null out the event handlers to prevent a mFreeCount leak of 1
+    gXHR.onerror     = null;
+    gXHR.onload      = null;
+    gXHR.onprogress  = null;
+
+    gXHR             = null;
+  }
+
+  gUpdateManager = null;
+  gUpdateChecker = null;
+  gAUS           = null;
+  gTestserver    = null;
+}
+
 /**
  * Initializes the most commonly used global vars used by tests and
  * nsIApplicationUpdateService
  */
 function startAUS() {
   createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1.0", "2.0");
 
+  var pb = getPrefBranch();
   // Don't display UI for a successful installation. Some apps may not set this
   // pref to false like Firefox does.
-  gPrefs.setBoolPref(PREF_APP_UPDATE_SHOW_INSTALLED_UI, false);
+  pb.setBoolPref(PREF_APP_UPDATE_SHOW_INSTALLED_UI, false);
   // Enable Update logging
-  gPrefs.setBoolPref("app.update.log.all", true);
+  pb.setBoolPref("app.update.log.all", true);
   // Lessens the noise in the logs when using Update Service logging
-  gPrefs.setBoolPref("app.update.enabled", false);
-  gPrefs.setBoolPref("extensions.blocklist.enabled", false);
-  gPrefs.setBoolPref("extensions.update.enabled", false);
-  gPrefs.setBoolPref("browser.search.update", false);
-  gPrefs.setBoolPref("browser.microsummary.updateGenerators", false);
+  pb.setBoolPref("app.update.enabled", false);
+  pb.setBoolPref("extensions.blocklist.enabled", false);
+  pb.setBoolPref("extensions.update.enabled", false);
+  pb.setBoolPref("browser.search.update", false);
+  pb.setBoolPref("browser.microsummary.updateGenerators", false);
 
-  gAUS = AUS_Cc["@mozilla.org/updates/update-service;1"]
-           .getService(AUS_Ci.nsIApplicationUpdateService);
-  var os = AUS_Cc["@mozilla.org/observer-service;1"]
-             .getService(AUS_Ci.nsIObserverService);
+  gAUS = AUS_Cc["@mozilla.org/updates/update-service;1"].
+         getService(AUS_Ci.nsIApplicationUpdateService);
+  var os = AUS_Cc["@mozilla.org/observer-service;1"].
+           getService(AUS_Ci.nsIObserverService);
   os.notifyObservers(null, "profile-after-change", null);
 }
 
 /* Initializes nsIUpdateChecker */
 function startUpdateChecker() {
-  gUpdateChecker = AUS_Cc["@mozilla.org/updates/update-checker;1"]
-                     .createInstance(AUS_Ci.nsIUpdateChecker);
+  gUpdateChecker = AUS_Cc["@mozilla.org/updates/update-checker;1"].
+                   createInstance(AUS_Ci.nsIUpdateChecker);
 }
 
 /* Initializes nsIUpdateManager */
 function startUpdateManager() {
-  gUpdateManager = AUS_Cc["@mozilla.org/updates/update-manager;1"]
-                     .getService(AUS_Ci.nsIUpdateManager);
+  gUpdateManager = AUS_Cc["@mozilla.org/updates/update-manager;1"].
+                   getService(AUS_Ci.nsIUpdateManager);
 }
 
 /**
  * Constructs a string representing a remote update xml file.
  * @param   aUpdates
  *          The string representing the update elements.
  * @returns The string representing a remote update xml file.
  */
@@ -381,18 +412,18 @@ function writeStatusFile(aStatus) {
  * and create the file if it doesn't exist.
  * @param   aFile
  *          The file to write to. Will be created if it doesn't exist.
  * @param   aText
  *          The text to write to the file. If there is existing text it will be
  *          replaced.
  */
 function writeFile(aFile, aText) {
-  var fos = AUS_Cc["@mozilla.org/network/safe-file-output-stream;1"]
-              .createInstance(AUS_Ci.nsIFileOutputStream);
+  var fos = AUS_Cc["@mozilla.org/network/safe-file-output-stream;1"].
+            createInstance(AUS_Ci.nsIFileOutputStream);
   if (!aFile.exists())
     aFile.create(AUS_Ci.nsILocalFile.NORMAL_FILE_TYPE, PERMS_FILE);
   fos.init(aFile, MODE_WRONLY | MODE_CREATE | MODE_TRUNCATE, PERMS_FILE, 0);
   fos.write(aText, aText.length);
 
   if (fos instanceof AUS_Ci.nsISafeOutputStream) {
     try {
       fos.finish();
@@ -407,18 +438,18 @@ function writeFile(aFile, aText) {
 
 /**
  * Reads text from a file and returns the string.
  * @param   aFile
  *          The file to read from.
  * @returns The string of text read from the file.
  */
 function readFile(aFile) {
-  var fis = AUS_Cc["@mozilla.org/network/file-input-stream;1"]
-              .createInstance(AUS_Ci.nsIFileInputStream);
+  var fis = AUS_Cc["@mozilla.org/network/file-input-stream;1"].
+            createInstance(AUS_Ci.nsIFileInputStream);
   if (!aFile.exists())
     return null;
   fis.init(aFile, MODE_RDONLY, PERMS_FILE, 0);
   var sis = AUS_Cc["@mozilla.org/scriptableinputstream;1"].
             createInstance(AUS_Ci.nsIScriptableInputStream);
   sis.init(fis);
   var text = sis.read(sis.available());
   sis.close();
@@ -451,18 +482,18 @@ function getString(aName) {
  * Toggles network offline.
  *
  * Be sure to toggle back to online before the test finishes to prevent the
  * following from being printed to the test's log file.
  * WARNING: NS_ENSURE_TRUE(thread) failed: file c:/moz/mozilla-central/mozilla/netwerk/base/src/nsSocketTransportService2.cpp, line 115
  * WARNING: unable to post SHUTDOWN message
  */
 function toggleOffline(aOffline) {
-  const ioService = AUS_Cc["@mozilla.org/network/io-service;1"]
-                      .getService(AUS_Ci.nsIIOService);
+  const ioService = AUS_Cc["@mozilla.org/network/io-service;1"].
+                    getService(AUS_Ci.nsIIOService);
 
   try {
     ioService.manageOfflineStatus = !aOffline;
   }
   catch (e) {
   }
   if (ioService.offline != aOffline)
     ioService.offline = aOffline;
@@ -500,19 +531,19 @@ function xhr() {
 xhr.prototype = {
   overrideMimeType: function(mimetype) { },
   setRequestHeader: function(header, value) { },
   status: null,
   channel: { set notificationCallbacks(val) { } },
   _url: null,
   _method: null,
   open: function (method, url) {
-    gXHR.channel.originalURI = AUS_Cc["@mozilla.org/network/io-service;1"]
-                                 .getService(AUS_Ci.nsIIOService)
-                                 .newURI(url, null, null);
+    gXHR.channel.originalURI = AUS_Cc["@mozilla.org/network/io-service;1"].
+                               getService(AUS_Ci.nsIIOService).
+                               newURI(url, null, null);
     gXHR._method = method; gXHR._url = url;
   },
   responseXML: null,
   responseText: null,
   send: function(body) {
     do_timeout(0, "gXHRCallback()"); // Use a timeout so the XHR completes
   },
   _onprogress: null,
@@ -624,76 +655,60 @@ function removeUpdateDirsAndFiles() {
     if (file.exists())
       file.remove(false);
   }
   catch (e) {
     dump("Unable to remove file\npath: " + file.path +
          "\nException: " + e + "\n");
   }
 
-  var updatesSubDir = appDir.clone();
-  updatesSubDir.append("updates");
-  updatesSubDir.append("0");
-  if (updatesSubDir.exists()) {
-    file = updatesSubDir.clone();
-    file.append("update.mar");
-    try {
-      if (file.exists())
-        file.remove(false);
-    }
-    catch (e) {
-      dump("Unable to remove file\npath: " + file.path +
-           "\nException: " + e + "\n");
-    }
-
-    file = updatesSubDir.clone();
-    file.append("update.status");
-    try {
-      if (file.exists())
-        file.remove(false);
-    }
-    catch (e) {
-      dump("Unable to remove file\npath: " + file.path +
-           "\nException: " + e + "\n");
-    }
-
-    file = updatesSubDir.clone();
-    file.append("update.version");
-    try {
-      if (file.exists())
-        file.remove(false);
-    }
-    catch (e) {
-      dump("Unable to remove file\npath: " + file.path +
-           "\nException: " + e + "\n");
-    }
-
-    try {
-      updatesSubDir.remove(true);
-    }
-    catch (e) {
-      dump("Unable to remove directory\npath: " + updatesSubDir.path +
-           "\nException: " + e + "\n");
-    }
-  }
-
   // This fails sporadically on Mac OS X so wrap it in a try catch
   var updatesDir = appDir.clone();
   updatesDir.append("updates");
   try {
-    if (updatesDir.exists())
-      updatesDir.remove(true);
+    removeDirRecursive(updatesDir);
   }
   catch (e) {
     dump("Unable to remove directory\npath: " + updatesDir.path +
          "\nException: " + e + "\n");
   }
 }
 
 /**
+ * Deletes a directory and its children. First it tries nsIFile::Remove(true).
+ * If that fails it will fall back to recursing, setting the appropriate
+ * permissions, and deleting the current entry.
+ * @param   dir
+ *          A nsIFile for the directory to be deleted
+ */
+function removeDirRecursive(aDir) {
+  try {
+    aDir.remove(true);
+    return;
+  }
+  catch (e) {
+  }
+
+  var dirEntries = aDir.directoryEntries;
+  while (dirEntries.hasMoreElements()) {
+    var entry = dirEntries.getNext().QueryInterface(AUS_Ci.nsIFile);
+
+    if (entry.isDirectory()) {
+      removeDirRecursive(entry);
+    }
+    else {
+      entry.permissions = PERMS_FILE;
+      entry.remove(false);
+    }
+  }
+  aDir.permissions = PERMS_DIRECTORY;
+  aDir.remove(true);
+}
+
+/**
  * Helper for starting the http server used by the tests
  * @param   aRelativeDirName
  *          The directory name to register relative to
  *          toolkit/mozapps/update/test/unit/
  */
 function start_httpserver(aRelativeDirName) {
   do_load_httpd_js();
   gTestserver = new nsHttpServer();
--- a/toolkit/mozapps/update/test/unit/tail_update.js
+++ b/toolkit/mozapps/update/test/unit/tail_update.js
@@ -31,27 +31,10 @@
  * 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 *****
  */
 
-gDirSvc.unregisterProvider(dirProvider);
-
-if (gXHR) {
-  gXHRCallback     = null;
-
-  gXHR.responseXML = null;
-  // null out the event handlers to prevent a mFreeCount leak of 1
-  gXHR.onerror     = null;
-  gXHR.onload      = null;
-  gXHR.onprogress  = null;
-
-  gXHR             = null;
-}
-
-gUpdateManager = null;
-gUpdateChecker = null;
-gAUS           = null;
-gPrefs         = null;
-gTestserver    = null;
+// Not used since check-interactive executes this prior to _execute_test();
+// being executed.
--- a/toolkit/mozapps/update/test/unit/test_0010_general.js
+++ b/toolkit/mozapps/update/test/unit/test_0010_general.js
@@ -48,9 +48,10 @@ function run_test() {
   testFile.remove(false);
   do_check_false(testFile.exists());
 
   startAUS();
 
   // Check if the update dir can be written to
   dump("Testing: nsIApplicationUpdateService:canUpdate\n");
   do_check_true(gAUS.canUpdate);
+  cleanUp();
 }
--- a/toolkit/mozapps/update/test/unit/test_0020_general.js
+++ b/toolkit/mozapps/update/test/unit/test_0020_general.js
@@ -39,30 +39,30 @@
 /* General Update Check Update XML Tests */
 
 var gNextRunFunc;
 var gExpectedCount;
 
 function run_test() {
   do_test_pending();
   removeUpdateDirsAndFiles();
-  gPrefs.setCharPref(PREF_APP_UPDATE_URL_OVERRIDE,
-                     URL_HOST + "update.xml");
-  var defaults = gPrefs.QueryInterface(AUS_Ci.nsIPrefService)
-                   .getDefaultBranch(null);
+  var pb = getPrefBranch();
+  pb.setCharPref(PREF_APP_UPDATE_URL_OVERRIDE, URL_HOST + "update.xml");
+  var defaults = pb.QueryInterface(AUS_Ci.nsIPrefService).getDefaultBranch(null);
   defaults.setCharPref("app.update.channel", "bogus_channel");
   // The mock XMLHttpRequest is MUCH faster
   overrideXHR(callHandleEvent);
   startAUS();
   startUpdateChecker();
   do_timeout(0, "run_test_pt1()");
 }
 
 function end_test() {
   do_test_finished();
+  cleanUp();
 }
 
 // Helper function for testing update counts returned from an update xml
 function run_test_helper_pt1(aMsg, aExpectedCount, aNextRunFunc) {
   gUpdates = null;
   gUpdateCount = null;
   gCheckFunc = check_test_helper_pt1;
   gNextRunFunc = aNextRunFunc;
@@ -77,18 +77,18 @@ function check_test_helper_pt1() {
 }
 
 // Callback function used by the custom XMLHttpRequest implemetation to
 // call the nsIDOMEventListener's handleEvent method for onload.
 function callHandleEvent() {
   gXHR.status = 400;
   gXHR.responseText = gResponseBody;
   try {
-    var parser = AUS_Cc["@mozilla.org/xmlextras/domparser;1"]
-                   .createInstance(AUS_Ci.nsIDOMParser);
+    var parser = AUS_Cc["@mozilla.org/xmlextras/domparser;1"].
+                 createInstance(AUS_Ci.nsIDOMParser);
     gXHR.responseXML = parser.parseFromString(gResponseBody, "application/xml");
   }
   catch(e) {
     gXHR.responseXML = null;
   }
   var e = { target: gXHR };
   gXHR.onload.handleEvent(e);
 }
--- a/toolkit/mozapps/update/test/unit/test_0030_general.js
+++ b/toolkit/mozapps/update/test/unit/test_0030_general.js
@@ -41,37 +41,38 @@
 var gNextRunFunc;
 var gStatusResult;
 var gExpectedStatusResult;
 
 function run_test() {
   do_test_pending();
   removeUpdateDirsAndFiles();
   // The mock XMLHttpRequest is MUCH faster
-  gPrefs.setCharPref(PREF_APP_UPDATE_URL_OVERRIDE, URL_HOST + "update.xml");
+  getPrefBranch().setCharPref(PREF_APP_UPDATE_URL_OVERRIDE, URL_HOST + "update.xml");
   overrideXHR(callHandleEvent);
   startAUS();
   startUpdateChecker();
   // The HTTP server is only used for the mar file downloads which is slow
   start_httpserver(DIR_DATA);
   do_timeout(0, "run_test_pt1()");
 }
 
 function end_test() {
   stop_httpserver(do_test_finished);
+  cleanUp();
 }
 
 // Callback function used by the custom XMLHttpRequest implemetation to
 // call the nsIDOMEventListener's handleEvent method for onload.
 function callHandleEvent() {
   gXHR.status = 400;
   gXHR.responseText = gResponseBody;
   try {
-    var parser = AUS_Cc["@mozilla.org/xmlextras/domparser;1"]
-                   .createInstance(AUS_Ci.nsIDOMParser);
+    var parser = AUS_Cc["@mozilla.org/xmlextras/domparser;1"].
+                 createInstance(AUS_Ci.nsIDOMParser);
     gXHR.responseXML = parser.parseFromString(gResponseBody, "application/xml");
   }
   catch(e) {
   }
   var e = { target: gXHR };
   gXHR.onload.handleEvent(e);
 }
 
--- a/toolkit/mozapps/update/test/unit/test_0040_general.js.in
+++ b/toolkit/mozapps/update/test/unit/test_0040_general.js.in
@@ -50,24 +50,25 @@ var gInstallationLocale = "@AB_CD@";
 
 function run_test() {
   do_test_pending();
   removeUpdateDirsAndFiles();
   // The mock XMLHttpRequest is MUCH faster
   overrideXHR(callHandleEvent);
   startAUS();
   startUpdateChecker();
-  gAppInfo = AUS_Cc["@mozilla.org/xre/app-info;1"]
-               .getService(AUS_Ci.nsIXULAppInfo)
-               .QueryInterface(AUS_Ci.nsIXULRuntime);
+  gAppInfo = AUS_Cc["@mozilla.org/xre/app-info;1"].
+             getService(AUS_Ci.nsIXULAppInfo).
+             QueryInterface(AUS_Ci.nsIXULRuntime);
   do_timeout(0, "run_test_pt1()");
 }
 
 function end_test() {
- do_test_finished();
+  do_test_finished();
+  cleanUp();
 }
 
 // Callback function used by the custom XMLHttpRequest implemetation to
 // call the nsIDOMEventListener's handleEvent method for onload.
 function callHandleEvent() {
   var e = { target: gXHR };
   gXHR.onload.handleEvent(e);
 }
@@ -77,163 +78,170 @@ function getResult(url) {
   return url.substr(URL_PREFIX.length).split("/")[0];
 }
 
 // url constructed with %PRODUCT%
 function run_test_pt1() {
   gCheckFunc = check_test_pt1;
   var url = URL_PREFIX + "%PRODUCT%/";
   dump("Testing: url constructed with %PRODUCT% - " + url + "\n");
-  gPrefs.setCharPref(PREF_APP_UPDATE_URL_OVERRIDE, url);
+  getPrefBranch().setCharPref(PREF_APP_UPDATE_URL_OVERRIDE, url);
   gUpdateChecker.checkForUpdates(updateCheckListener, true);
 }
 
 function check_test_pt1() {
   do_check_eq(getResult(gRequestURL), gAppInfo.name);
   run_test_pt2();
 }
 
 // url constructed with %VERSION%
 function run_test_pt2() {
   gCheckFunc = check_test_pt2;
   var url = URL_PREFIX + "%VERSION%/";
   dump("Testing: url constructed with %VERSION% - " + url + "\n");
-  gPrefs.setCharPref(PREF_APP_UPDATE_URL_OVERRIDE, url);
+  getPrefBranch().setCharPref(PREF_APP_UPDATE_URL_OVERRIDE, url);
   gUpdateChecker.checkForUpdates(updateCheckListener, true);
 }
 
 function check_test_pt2() {
   do_check_eq(getResult(gRequestURL), gAppInfo.version);
   run_test_pt3();
 }
 
 // url constructed with %BUILD_ID%
 function run_test_pt3() {
   gCheckFunc = check_test_pt3;
   var url = URL_PREFIX + "%BUILD_ID%/";
   dump("Testing: url constructed with %BUILD_ID% - " + url + "\n");
-  gPrefs.setCharPref(PREF_APP_UPDATE_URL_OVERRIDE, url);
+  getPrefBranch().setCharPref(PREF_APP_UPDATE_URL_OVERRIDE, url);
   gUpdateChecker.checkForUpdates(updateCheckListener, true);
 }
 
 function check_test_pt3() {
   do_check_eq(getResult(gRequestURL), gAppInfo.appBuildID);
   run_test_pt4();
 }
 
 // url constructed with %BUILD_TARGET%
 // XXX TODO - it might be nice if we tested the actual ABI
 function run_test_pt4() {
   gCheckFunc = check_test_pt4;
   var url = URL_PREFIX + "%BUILD_TARGET%/";
   dump("Testing: url constructed with %BUILD_TARGET% - " + url + "\n");
-  gPrefs.setCharPref(PREF_APP_UPDATE_URL_OVERRIDE, url);
+  getPrefBranch().setCharPref(PREF_APP_UPDATE_URL_OVERRIDE, url);
   gUpdateChecker.checkForUpdates(updateCheckListener, true);
 }
 
 function check_test_pt4() {
   try {
     abi = gAppInfo.XPCOMABI;
   }
   catch (e) {
     do_throw("nsIXULAppInfo:XPCOMABI not defined\n");
   }
 
 #ifdef XP_MACOSX
   // Mac universal build should report a different ABI than either macppc
   // or mactel. This is necessary since nsUpdateService.js will set the ABI to
   // Universal-gcc3 for Mac universal builds.
-  var macutils = AUS_Cc["@mozilla.org/xpcom/mac-utils;1"]
-                   .getService(AUS_Ci.nsIMacUtils);
+  var macutils = AUS_Cc["@mozilla.org/xpcom/mac-utils;1"].
+                 getService(AUS_Ci.nsIMacUtils);
 
   if (macutils.isUniversalBinary)
     abi = "Universal-gcc3";
 #endif
 
   do_check_eq(getResult(gRequestURL), gAppInfo.OS + "_" + abi);
   run_test_pt5();
 }
 
 // url constructed with %LOCALE%
-// Bug 446527 added the locale to the updater.ini
+// Bug 488936 added the locale to the updater.locale file
 function run_test_pt5() {
   gCheckFunc = check_test_pt5;
   var url = URL_PREFIX + "%LOCALE%/";
   dump("Testing: url constructed with %LOCALE% - " + url + "\n");
-  gPrefs.setCharPref(PREF_APP_UPDATE_URL_OVERRIDE, url);
-  gUpdateChecker.checkForUpdates(updateCheckListener, true);
+  getPrefBranch().setCharPref(PREF_APP_UPDATE_URL_OVERRIDE, url);
+  try {
+    gUpdateChecker.checkForUpdates(updateCheckListener, true);
+  }
+  catch (e) {
+    dump("***\n*** The following error is most likely due to a missing " +
+         "update.locale file\n***\n");
+    do_throw(e);
+  }
 }
 
 function check_test_pt5() {
   do_check_eq(getResult(gRequestURL), gInstallationLocale);
   run_test_pt6();
 }
 
 // url constructed with %CHANNEL%
 function run_test_pt6() {
   gCheckFunc = check_test_pt6;
   var url = URL_PREFIX + "%CHANNEL%/";
   dump("Testing: url constructed with %CHANNEL% - " + url + "\n");
-  gPrefs.setCharPref(PREF_APP_UPDATE_URL_OVERRIDE, url);
-  var defaults = gPrefs.QueryInterface(AUS_Ci.nsIPrefService)
-                   .getDefaultBranch(null);
+  var pb = getPrefBranch();
+  pb.setCharPref(PREF_APP_UPDATE_URL_OVERRIDE, url);
+  var defaults = pb.QueryInterface(AUS_Ci.nsIPrefService).getDefaultBranch(null);
   defaults.setCharPref(PREF_APP_UPDATE_CHANNEL, "bogus_channel");
   gUpdateChecker.checkForUpdates(updateCheckListener, true);
 }
 
 function check_test_pt6() {
   do_check_eq(getResult(gRequestURL), "bogus_channel");
   run_test_pt7();
 }
 
 // url constructed with %CHANNEL% with distribution partners
 function run_test_pt7() {
   gCheckFunc = check_test_pt7;
   var url = URL_PREFIX + "%CHANNEL%/";
   dump("Testing: url constructed with %CHANNEL% - " + url + "\n");
-  gPrefs.setCharPref(PREF_APP_UPDATE_URL_OVERRIDE, url);
-  var defaults = gPrefs.QueryInterface(AUS_Ci.nsIPrefService)
-                   .getDefaultBranch(null);
+  var pb = getPrefBranch();
+  pb.setCharPref(PREF_APP_UPDATE_URL_OVERRIDE, url);
+  var defaults = pb.QueryInterface(AUS_Ci.nsIPrefService).getDefaultBranch(null);
   defaults.setCharPref(PREF_PARTNER_BRANCH + "bogus_partner1", "bogus_partner1");
   defaults.setCharPref(PREF_PARTNER_BRANCH + "bogus_partner2", "bogus_partner2");
   gUpdateChecker.checkForUpdates(updateCheckListener, true);
 }
 
 function check_test_pt7() {
   do_check_eq(getResult(gRequestURL), "bogus_channel-cck-bogus_partner1-bogus_partner2");
   run_test_pt8();
 }
 
 // url constructed with %PLATFORM_VERSION%
 function run_test_pt8() {
   gCheckFunc = check_test_pt8;
   var url = URL_PREFIX + "%PLATFORM_VERSION%/";
   dump("Testing: url constructed with %PLATFORM_VERSION% - " + url + "\n");
-  gPrefs.setCharPref(PREF_APP_UPDATE_URL_OVERRIDE, url);
+  getPrefBranch().setCharPref(PREF_APP_UPDATE_URL_OVERRIDE, url);
   gUpdateChecker.checkForUpdates(updateCheckListener, true);
 }
 
 function check_test_pt8() {
   do_check_eq(getResult(gRequestURL), gAppInfo.platformVersion);
   run_test_pt9();
 }
 
 // url constructed with %OS_VERSION%
 function run_test_pt9() {
   gCheckFunc = check_test_pt9;
   var url = URL_PREFIX + "%OS_VERSION%/";
   dump("Testing: url constructed with %OS_VERSION% - " + url + "\n");
-  gPrefs.setCharPref(PREF_APP_UPDATE_URL_OVERRIDE, url);
+  getPrefBranch().setCharPref(PREF_APP_UPDATE_URL_OVERRIDE, url);
   gUpdateChecker.checkForUpdates(updateCheckListener, true);
 }
 
 function check_test_pt9() {
   var osVersion;
-  var sysInfo = AUS_Cc["@mozilla.org/system-info;1"]
-                  .getService(AUS_Ci.nsIPropertyBag2);
+  var sysInfo = AUS_Cc["@mozilla.org/system-info;1"].
+                getService(AUS_Ci.nsIPropertyBag2);
   osVersion = sysInfo.getProperty("name") + " " + sysInfo.getProperty("version");
 
   if (osVersion) {
     try {
       osVersion += " (" + sysInfo.getProperty("secondaryLibrary") + ")";
     }
     catch (e) {
       // Not all platforms have a secondary widget library, so an error is
@@ -246,64 +254,64 @@ function check_test_pt9() {
   run_test_pt10();
 }
 
 // url constructed with %DISTRIBUTION%
 function run_test_pt10() {
   gCheckFunc = check_test_pt10;
   var url = URL_PREFIX + "%DISTRIBUTION%/";
   dump("Testing: url constructed with %DISTRIBUTION% - " + url + "\n");
-  gPrefs.setCharPref(PREF_APP_UPDATE_URL_OVERRIDE, url);
-  var defaults = gPrefs.QueryInterface(AUS_Ci.nsIPrefService)
-                   .getDefaultBranch(null);
+  var pb = getPrefBranch();
+  pb.setCharPref(PREF_APP_UPDATE_URL_OVERRIDE, url);
+  var defaults = pb.QueryInterface(AUS_Ci.nsIPrefService).getDefaultBranch(null);
   defaults.setCharPref(PREF_APP_DISTRIBUTION, "bogus_distro");
   gUpdateChecker.checkForUpdates(updateCheckListener, true);
 }
 
 function check_test_pt10() {
   do_check_eq(getResult(gRequestURL), "bogus_distro");
   run_test_pt11();
 }
 
 // url constructed with %DISTRIBUTION_VERSION%
 function run_test_pt11() {
   gCheckFunc = check_test_pt11;
   var url = URL_PREFIX + "%DISTRIBUTION_VERSION%/";
   dump("Testing: url constructed with %DISTRIBUTION_VERSION% - " + url + "\n");
-  gPrefs.setCharPref(PREF_APP_UPDATE_URL_OVERRIDE, url);
-  var defaults = gPrefs.QueryInterface(AUS_Ci.nsIPrefService)
-                   .getDefaultBranch(null);
+  var pb = getPrefBranch();
+  pb.setCharPref(PREF_APP_UPDATE_URL_OVERRIDE, url);
+  var defaults = pb.QueryInterface(AUS_Ci.nsIPrefService).getDefaultBranch(null);
   defaults.setCharPref(PREF_APP_DISTRIBUTION_VERSION, "bogus_distro_version");
   gUpdateChecker.checkForUpdates(updateCheckListener, true);
 }
 
 function check_test_pt11() {
   do_check_eq(getResult(gRequestURL), "bogus_distro_version");
   run_test_pt12();
 }
 
 // url constructed that doesn't have a parameter - bug 454357
 function run_test_pt12() {
   gCheckFunc = check_test_pt12;
   var url = URL_PREFIX;
   dump("Testing: url constructed that doesn't have a parameter - " + url + "\n");
-  gPrefs.setCharPref(PREF_APP_UPDATE_URL_OVERRIDE, url);
+  getPrefBranch().setCharPref(PREF_APP_UPDATE_URL_OVERRIDE, url);
   gUpdateChecker.checkForUpdates(updateCheckListener, true);
 }
 
 function check_test_pt12() {
   do_check_eq(getResult(gRequestURL), "?force=1");
   run_test_pt13();
 }
 
 // url constructed that has a parameter - bug 454357
 function run_test_pt13() {
   gCheckFunc = check_test_pt13;
   var url = URL_PREFIX + "?bogus=param";
   dump("Testing: url constructed that has a parameter - " + url + "\n");
-  gPrefs.setCharPref(PREF_APP_UPDATE_URL_OVERRIDE, url);
+  getPrefBranch().setCharPref(PREF_APP_UPDATE_URL_OVERRIDE, url);
   gUpdateChecker.checkForUpdates(updateCheckListener, true);
 }
 
 function check_test_pt13() {
   do_check_eq(getResult(gRequestURL), "?bogus=param&force=1");
   end_test();
 }
--- a/toolkit/mozapps/update/test/unit/test_0050_general.js
+++ b/toolkit/mozapps/update/test/unit/test_0050_general.js
@@ -45,22 +45,24 @@ var gNextRunFunc;
 var gExpectedStatusCode;
 var gExpectedStatusText;
 
 function run_test() {
   do_test_pending();
   removeUpdateDirsAndFiles();
   startAUS();
   startUpdateChecker();
-  gPrefs.setCharPref(PREF_APP_UPDATE_URL_OVERRIDE, URL_HOST + "update.xml");
+  getPrefBranch().setCharPref(PREF_APP_UPDATE_URL_OVERRIDE,
+                              URL_HOST + "update.xml");
   do_timeout(0, "run_test_pt1()");
 }
 
 function end_test() {
   stop_httpserver(do_test_finished);
+  cleanUp();
 }
 
 // Custom error httpd handler used to return error codes we can't simulate
 function httpdErrorHandler(metadata, response) {
   response.setStatusLine(metadata.httpVersion, gExpectedStatusCode, "Error");
 }
 
 // Helper functions for testing nsIUpdateCheckListener onerror error statusText
--- a/toolkit/mozapps/update/test/unit/test_0051_general.js
+++ b/toolkit/mozapps/update/test/unit/test_0051_general.js
@@ -46,23 +46,25 @@ var gNextRunFunc;
 var gExpectedStatusCode;
 var gExpectedStatusText;
 
 function run_test() {
   do_test_pending();
   removeUpdateDirsAndFiles();
   startAUS();
   startUpdateChecker();
-  gPrefs.setCharPref(PREF_APP_UPDATE_URL_OVERRIDE, URL_HOST + "update.xml");
+  getPrefBranch().setCharPref(PREF_APP_UPDATE_URL_OVERRIDE,
+                              URL_HOST + "update.xml");
   overrideXHR(callHandleEvent);
   do_timeout(0, "run_test_pt1()");
 }
 
 function end_test() {
   do_test_finished();
+  cleanUp();
 }
 
 // Callback function used by the custom XMLHttpRequest implemetation to
 // call the nsIDOMEventListener's handleEvent method for onload.
 function callHandleEvent() {
   gXHR.status = gExpectedStatusCode;
   var e = { target: gXHR };
   gXHR.onload.handleEvent(e);
--- a/toolkit/mozapps/update/test/unit/test_0060_manager.js
+++ b/toolkit/mozapps/update/test/unit/test_0060_manager.js
@@ -37,18 +37,18 @@
  */
 
 /* General Update Manager Tests */
 
 function run_test() {
   dump("Testing: addition of a successful update to " + FILE_UPDATES_DB +
        " and verification of update properties\n");
   removeUpdateDirsAndFiles();
-  var defaults = gPrefs.QueryInterface(AUS_Ci.nsIPrefService)
-                   .getDefaultBranch(null);
+  var defaults = getPrefBranch().QueryInterface(AUS_Ci.nsIPrefService).
+                 getDefaultBranch(null);
   defaults.setCharPref("app.update.channel", "bogus_channel");
 
   var patches = getLocalPatchString(null, null, null, null, null, null,
                                     STATE_FAILED);
   var updates = getLocalUpdateString(patches, "Existing", null, "3.0", "3.0",
                                      "3.0", null, null, null, null, null,
                                      getString("patchApplyFailure"));
   writeUpdatesToXMLFile(getLocalUpdatesXMLString(updates), false);
@@ -109,9 +109,10 @@ function run_test() {
   patch = update.selectedPatch;
   do_check_eq(patch.type, "complete");
   do_check_eq(patch.URL, "http://localhost:4444/data/empty.mar");
   do_check_eq(patch.hashFunction, "MD5");
   do_check_eq(patch.hashValue, "6232cd43a1c77e30191c53a329a3f99d");
   do_check_eq(patch.size, "775");
   do_check_true(patch.selected);
   do_check_eq(patch.state, STATE_FAILED);
+  cleanUp();
 }
--- a/toolkit/mozapps/update/test/unit/test_0061_manager.js
+++ b/toolkit/mozapps/update/test/unit/test_0061_manager.js
@@ -37,26 +37,27 @@
  */
 
 /* General Update Manager Tests */
 
 function run_test() {
   dump("Testing: removal of an update download in progress for an older " +
        "version of the application on startup - bug 485624\n");
   removeUpdateDirsAndFiles();
-  var defaults = gPrefs.QueryInterface(AUS_Ci.nsIPrefService)
-                   .getDefaultBranch(null);
+  var defaults = getPrefBranch().QueryInterface(AUS_Ci.nsIPrefService).
+                 getDefaultBranch(null);
   defaults.setCharPref("app.update.channel", "bogus_channel");
 
   writeUpdatesToXMLFile(getLocalUpdatesXMLString(""), false);
 
   patches = getLocalPatchString(null, null, null, null, null, null,
                                 STATE_DOWNLOADING);
   updates = getLocalUpdateString(patches, null, null, "0.9", null, "0.9");
   writeUpdatesToXMLFile(getLocalUpdatesXMLString(updates), true);
   writeStatusFile(STATE_DOWNLOADING);
 
   startAUS();
   startUpdateManager();
 
   do_check_eq(gUpdateManager.activeUpdate, null);
   do_check_eq(gUpdateManager.updateCount, 0);
+  cleanUp();
 }
--- a/toolkit/mozapps/update/test/unit/test_0062_manager.js
+++ b/toolkit/mozapps/update/test/unit/test_0062_manager.js
@@ -37,27 +37,28 @@
  */
 
 /* General Update Manager Tests */
 
 function run_test() {
   dump("Testing: resuming an update download in progress for the same " +
        "version of the application on startup - bug 485624\n");
   removeUpdateDirsAndFiles();
-  var defaults = gPrefs.QueryInterface(AUS_Ci.nsIPrefService)
-                   .getDefaultBranch(null);
+  var defaults = getPrefBranch().QueryInterface(AUS_Ci.nsIPrefService).
+                 getDefaultBranch(null);
   defaults.setCharPref("app.update.channel", "bogus_channel");
 
   writeUpdatesToXMLFile(getLocalUpdatesXMLString(""), false);
 
   patches = getLocalPatchString(null, null, null, null, null, null,
                                 STATE_DOWNLOADING);
   updates = getLocalUpdateString(patches, null, null, "1.0", null, "1.0", null,
                                  null, null, URL_HOST + DIR_DATA + "/empty.mar");
   writeUpdatesToXMLFile(getLocalUpdatesXMLString(updates), true);
   writeStatusFile(STATE_DOWNLOADING);
 
   startAUS();
   startUpdateManager();
 
   do_check_eq(gUpdateManager.updateCount, 1);
   do_check_eq(gUpdateManager.activeUpdate.state, STATE_DOWNLOADING);
+  cleanUp();
 }
--- a/toolkit/mozapps/update/test/unit/test_0110_general.js
+++ b/toolkit/mozapps/update/test/unit/test_0110_general.js
@@ -117,16 +117,18 @@ function run_test() {
     // binary was launched.
     if (updatesSubDir.exists())
       updatesSubDir.remove(true);
   }
   catch (e) {
     dump("Unable to remove directory\npath: " + updatesSubDir.path +
          "\nException: " + e + "\n");
   }
+
+  cleanUp();
 }
 
 // Launches the updater binary to apply a mar file
 function runUpdate(aUpdatesSubDir, aUpdater) {
   // Copy the updater binary to the update directory so the updater.ini is not
   // in the same directory as it is. This prevents ui from displaying and the
   // PostUpdate executable which is defined in the updater.ini from launching.
   aUpdater.copyTo(aUpdatesSubDir, aUpdater.leafName);
@@ -139,18 +141,18 @@ function runUpdate(aUpdatesSubDir, aUpda
     if (!updateBin.exists())
       do_throw("Unable to find the updater executable!");
   }
 
   var updatesSubDirPath = aUpdatesSubDir.path;
   if (/ /.test(updatesSubDirPath))
     updatesSubDirPath = '"' + updatesSubDirPath + '"';
 
-  var process = AUS_Cc["@mozilla.org/process/util;1"]
-                  .createInstance(AUS_Ci.nsIProcess);
+  var process = AUS_Cc["@mozilla.org/process/util;1"].
+                createInstance(AUS_Ci.nsIProcess);
   process.init(updateBin);
   var args = [updatesSubDirPath];
   process.run(true, args, args.length);
   return process.exitValue;
 }
 
 // Gets a file in the mar_test subdirectory of the current working directory
 // which is where the mar will be applied.
@@ -160,19 +162,19 @@ function getTestFile(aDir, aLeafName) {
   if (!(file instanceof AUS_Ci.nsILocalFile))
     do_throw("File must be a nsILocalFile for this test! File: " + aLeafName);
 
   return file;
 }
 
 // Returns the binary contents of a file
 function getFileBytes(aFile) {
-  var fis = AUS_Cc["@mozilla.org/network/file-input-stream;1"]
-              .createInstance(AUS_Ci.nsIFileInputStream);
+  var fis = AUS_Cc["@mozilla.org/network/file-input-stream;1"].
+            createInstance(AUS_Ci.nsIFileInputStream);
   fis.init(aFile, -1, -1, false);
-  var bis = AUS_Cc["@mozilla.org/binaryinputstream;1"]
-              .createInstance(AUS_Ci.nsIBinaryInputStream);
+  var bis = AUS_Cc["@mozilla.org/binaryinputstream;1"].
+            createInstance(AUS_Ci.nsIBinaryInputStream);
   bis.setInputStream(fis);
   var data = bis.readBytes(bis.available());
   bis.close();
   fis.close();
   return data;
 }
--- a/toolkit/mozapps/update/test/unit/test_0111_general.js
+++ b/toolkit/mozapps/update/test/unit/test_0111_general.js
@@ -128,16 +128,17 @@ function run_test() {
       updatesSubDir.remove(true);
   }
   catch (e) {
     dump("Unable to remove directory\npath: " + updatesSubDir.path +
          "\nException: " + e + "\n");
   }
 
   removeUpdateDirsAndFiles();
+  cleanUp();
 }
 
 // Launches the updater binary to apply a mar file
 function runUpdate(aUpdatesSubDir, aUpdater) {
   // Copy the updater binary to the update directory so the updater.ini is not
   // in the same directory as it is. This prevents ui from displaying and the
   // PostUpdate executable which is defined in the updater.ini from launching.
   aUpdater.copyTo(aUpdatesSubDir, aUpdater.leafName);
@@ -150,18 +151,18 @@ function runUpdate(aUpdatesSubDir, aUpda
     if (!updateBin.exists())
       do_throw("Unable to find the updater executable!");
   }
 
   var updatesSubDirPath = aUpdatesSubDir.path;
   if (/ /.test(updatesSubDirPath))
     updatesSubDirPath = '"' + updatesSubDirPath + '"';
 
-  var process = AUS_Cc["@mozilla.org/process/util;1"]
-                  .createInstance(AUS_Ci.nsIProcess);
+  var process = AUS_Cc["@mozilla.org/process/util;1"].
+                createInstance(AUS_Ci.nsIProcess);
   process.init(updateBin);
   var args = [updatesSubDirPath];
   process.run(true, args, args.length);
   return process.exitValue;
 }
 
 // Gets a file in the mar_test subdirectory of the current working directory
 // which is where the mar will be applied.
@@ -171,19 +172,19 @@ function getTestFile(aDir, aLeafName) {
   if (!(file instanceof AUS_Ci.nsILocalFile))
     do_throw("File must be a nsILocalFile for this test! File: " + aLeafName);
 
   return file;
 }
 
 // Returns the binary contents of a file
 function getFileBytes(aFile) {
-  var fis = AUS_Cc["@mozilla.org/network/file-input-stream;1"]
-              .createInstance(AUS_Ci.nsIFileInputStream);
+  var fis = AUS_Cc["@mozilla.org/network/file-input-stream;1"].
+            createInstance(AUS_Ci.nsIFileInputStream);
   fis.init(aFile, -1, -1, false);
-  var bis = AUS_Cc["@mozilla.org/binaryinputstream;1"]
-              .createInstance(AUS_Ci.nsIBinaryInputStream);
+  var bis = AUS_Cc["@mozilla.org/binaryinputstream;1"].
+            createInstance(AUS_Ci.nsIBinaryInputStream);
   bis.setInputStream(fis);
   var data = bis.readBytes(bis.available());
   bis.close();
   fis.close();
   return data;
 }