Bug 1507404 - Change app update mochitest-chrome tests so they are sync. r=bytesized
authorRobert Strong <robert.bugzilla@gmail.com>
Tue, 20 Nov 2018 19:35:18 +0000
changeset 503803 983d8b9634c1c742bd65ebac6726f3b741087fc3
parent 503802 91c9a4e6d7a859f4578111b3c3b63f6af49c8e7a
child 503804 026d0eca5d0ebc6f25ef0b2f71450bb8793002a4
push id10290
push userffxbld-merge
push dateMon, 03 Dec 2018 16:23:23 +0000
treeherdermozilla-beta@700bed2445e6 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbytesized
bugs1507404
milestone65.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1507404 - Change app update mochitest-chrome tests so they are sync. r=bytesized Also sets the appropriate value for app.update.auto in xpcshell tests Differential Revision: https://phabricator.services.mozilla.com/D12105
toolkit/mozapps/update/tests/chrome/test_0010_background_basic.xul
toolkit/mozapps/update/tests/chrome/test_0152_notify_backgroundCheckOfflineRetry.xul
toolkit/mozapps/update/tests/chrome/utils.js
toolkit/mozapps/update/tests/data/shared.js
toolkit/mozapps/update/tests/data/xpcshellUtilsAUS.js
toolkit/mozapps/update/tests/unit_aus_update/uiAutoPref.js
toolkit/mozapps/update/tests/unit_aus_update/updateAutoPrefMigrate.js
toolkit/mozapps/update/tests/unit_aus_update/updateDirectoryMigrate.js
--- a/toolkit/mozapps/update/tests/chrome/test_0010_background_basic.xul
+++ b/toolkit/mozapps/update/tests/chrome/test_0010_background_basic.xul
@@ -24,20 +24,20 @@ const TESTS = [ {
   buttonClick: "next"
 }, {
   pageid: PAGEID_DOWNLOADING
 }, {
   pageid: PAGEID_FINISHED,
   buttonClick: "extra1"
 } ];
 
-async function runTest() {
+function runTest() {
   debugDump("entering");
 
-  await gAUS.setAutoUpdateIsEnabled(false);
+  setAppUpdateAutoSync(false);
   let url = URL_HTTP_UPDATE_XML + "?showDetails=1" + getVersionParams();
   setUpdateURL(url);
 
   gAUS.checkForBackgroundUpdates();
 }
 
 ]]>
 </script>
--- a/toolkit/mozapps/update/tests/chrome/test_0152_notify_backgroundCheckOfflineRetry.xul
+++ b/toolkit/mozapps/update/tests/chrome/test_0152_notify_backgroundCheckOfflineRetry.xul
@@ -22,23 +22,23 @@
 const TESTS = [ {
   pageid: PAGEID_FOUND_BASIC,
   buttonClick: "extra1"
 } ];
 
 const NETWORK_ERROR_OFFLINE = 111;
 var gProxyPrefValue;
 
-async function runTest() {
+function runTest() {
   debugDump("entering");
 
   let url = URL_HTTP_UPDATE_XML + "?showDetails=1" + getVersionParams();
   setUpdateURL(url);
 
-  await gAUS.setAutoUpdateIsEnabled(false);
+  setAppUpdateAutoSync(false);
   Services.prefs.setBoolPref(PREF_APP_UPDATE_DISABLEDFORTESTING, false);
 
   Services.io.offline = true;
   gProxyPrefValue = Services.prefs.getIntPref("network.proxy.type");
   Services.prefs.setIntPref("network.proxy.type", 0);
 
   gUpdateChecker.checkForUpdates(updateCheckListener, true);
 }
--- a/toolkit/mozapps/update/tests/chrome/utils.js
+++ b/toolkit/mozapps/update/tests/chrome/utils.js
@@ -163,31 +163,31 @@ this.__defineGetter__("gCallback", funct
   return gTest.overrideCallback ? gTest.overrideCallback
                                 : defaultCallback;
 });
 
 /**
  * nsIObserver for receiving window open and close notifications.
  */
 const gWindowObserver = {
-  observe: async function WO_observe(aSubject, aTopic, aData) {
+  observe: function WO_observe(aSubject, aTopic, aData) {
     let win = aSubject;
 
     if (aTopic == "domwindowclosed") {
       if (win.location != URI_UPDATE_PROMPT_DIALOG) {
         debugDump("domwindowclosed event for window not being tested - " +
                   "location: " + win.location + "... returning early");
         return;
       }
       // Allow tests the ability to provide their own function (it must be
       // named finishTest) for finishing the test.
       try {
         finishTest();
       } catch (e) {
-        await finishTestDefault();
+        finishTestDefault();
       }
       return;
     }
 
     win.addEventListener("load", function() {
       // Ignore windows other than the update UI window.
       if (win.location != URI_UPDATE_PROMPT_DIALOG) {
         debugDump("load event for window not being tested - location: " +
@@ -213,82 +213,82 @@ const gWindowObserver = {
 };
 
 /**
  * Default test run function that can be used by most tests. This function uses
  * protective measures to prevent the test from failing provided by
  * |runTestDefaultWaitForWindowClosed| helper functions to prevent failure due
  * to a previous test failure.
  */
-async function runTestDefault() {
+function runTestDefault() {
   debugDump("entering");
 
   SimpleTest.waitForExplicitFinish();
 
-  await runTestDefaultWaitForWindowClosed();
+  runTestDefaultWaitForWindowClosed();
 }
 
 /**
  * If an update window is found SimpleTest.executeSoon can callback before the
  * update window is fully closed especially with debug builds. If an update
  * window is found this function will call itself using SimpleTest.executeSoon
  * up to the amount declared in CLOSE_WINDOW_TIMEOUT_MAXCOUNT until the update
  * window has closed before continuing the test.
  */
-async function runTestDefaultWaitForWindowClosed() {
+function runTestDefaultWaitForWindowClosed() {
   gCloseWindowTimeoutCounter++;
   if (gCloseWindowTimeoutCounter > CLOSE_WINDOW_TIMEOUT_MAXCOUNT) {
     try {
       finishTest();
     } catch (e) {
-      await finishTestDefault();
+      finishTestDefault();
     }
     return;
   }
 
   // The update window should not be open at this time. If it is the call to
   // |closeUpdateWindow| will close it and cause the test to fail.
   if (closeUpdateWindow()) {
     SimpleTest.executeSoon(runTestDefaultWaitForWindowClosed);
   } else {
     Services.ww.registerNotification(gWindowObserver);
 
     gCloseWindowTimeoutCounter = 0;
 
     setupFiles();
-    await setupPrefs();
+    setupPrefs();
     gEnv.set("MOZ_TEST_SKIP_UPDATE_STAGE", "1");
     removeUpdateDirsAndFiles();
     setupTimer(gTestTimeout);
     SimpleTest.executeSoon(setupTestUpdater);
   }
 }
 
 /**
  * Default test finish function that can be used by most tests. This function
  * uses protective measures to prevent the next test from failing provided by
  * |finishTestDefaultWaitForWindowClosed| helper functions to prevent failure
  * due to an update window being left open.
  */
-async function finishTestDefault() {
+function finishTestDefault() {
   debugDump("entering");
   if (gTimeoutTimer) {
     gTimeoutTimer.cancel();
     gTimeoutTimer = null;
   }
 
   if (gChannel) {
     debugDump("channel = " + gChannel);
     gChannel = null;
     gPrefRoot.removeObserver(PREF_APP_UPDATE_CHANNEL, observer);
   }
 
   verifyTestsRan();
 
-  await resetPrefs();
+  resetPrefs();
   gEnv.set("MOZ_TEST_SKIP_UPDATE_STAGE", "");
   resetFiles();
   removeUpdateDirsAndFiles();
   reloadUpdateManagerData();
 
   Services.ww.unregisterNotification(gWindowObserver);
   if (gDocElem) {
     gDocElem.removeEventListener("pageshow", onPageShowDefault);
@@ -300,24 +300,24 @@ async function finishTestDefault() {
 /**
  * nsITimerCallback for the timeout timer to cleanly finish a test if the Update
  * Window doesn't close for a test. This allows the next test to run properly if
  * a previous test fails.
  *
  * @param  aTimer
  *         The nsITimer that fired.
  */
-async function finishTestTimeout(aTimer) {
+function finishTestTimeout(aTimer) {
   ok(false, "Test timed out. Maximum time allowed is " + (gTestTimeout / 1000) +
      " seconds");
 
   try {
     finishTest();
   } catch (e) {
-    await finishTestDefault();
+    finishTestDefault();
   }
 }
 
 /**
  * When a test finishes this will repeatedly attempt to restore the real updater
  * for tests that use the test updater and then call
  * finishTestDefaultWaitForWindowClosed after the restore is successful.
  */
@@ -766,17 +766,17 @@ function restoreUpdaterBackup() {
 }
 
 /**
  * Sets the most common preferences used by tests to values used by the majority
  * of the tests and when necessary saves the preference's original values if
  * present so they can be set back to the original values when the test has
  * finished.
  */
-async function setupPrefs() {
+function setupPrefs() {
   if (DEBUG_AUS_TEST) {
     Services.prefs.setBoolPref(PREF_APP_UPDATE_LOG, true);
   }
 
   // Prevent nsIUpdateTimerManager from notifying nsIApplicationUpdateService
   // to check for updates by setting the app update last update time to the
   // current time minus one minute in seconds and the interval time to 12 hours
   // in seconds.
@@ -784,18 +784,28 @@ async function setupPrefs() {
   Services.prefs.setIntPref(PREF_APP_UPDATE_LASTUPDATETIME, now);
   Services.prefs.setIntPref(PREF_APP_UPDATE_INTERVAL, 43200);
 
   if (Services.prefs.prefHasUserValue(PREF_APP_UPDATE_DISABLEDFORTESTING)) {
     gAppUpdateDisabled = Services.prefs.getBoolPref(PREF_APP_UPDATE_DISABLEDFORTESTING);
   }
   Services.prefs.setBoolPref(PREF_APP_UPDATE_DISABLEDFORTESTING, false);
 
-  gAppUpdateAuto = await gAUS.getAutoUpdateIsEnabled();
-  await gAUS.setAutoUpdateIsEnabled(true);
+  if (IS_WIN) {
+    let configFile = getUpdateConfigFile();
+    if (configFile.exists()) {
+      let configData = JSON.parse(readFileBytes(configFile));
+      gAppUpdateAuto = !!configData[CONFIG_APP_UPDATE_AUTO];
+    }
+  } else if (Services.prefs.prefHasUserValue(PREF_APP_UPDATE_AUTO)) {
+    gAppUpdateAuto = Services.prefs.getBoolPref(PREF_APP_UPDATE_AUTO);
+  }
+  if (gAppUpdateAuto !== true) {
+    setAppUpdateAutoSync(true);
+  }
 
   if (Services.prefs.prefHasUserValue(PREF_APP_UPDATE_SERVICE_ENABLED)) {
     gAppUpdateServiceEnabled = Services.prefs.getBoolPref(PREF_APP_UPDATE_SERVICE_ENABLED);
   }
   Services.prefs.setBoolPref(PREF_APP_UPDATE_SERVICE_ENABLED, false);
 
   if (Services.prefs.prefHasUserValue(PREF_APP_UPDATE_STAGING_ENABLED)) {
     gAppUpdateStagingEnabled = Services.prefs.getBoolPref(PREF_APP_UPDATE_STAGING_ENABLED);
@@ -840,28 +850,28 @@ function resetFiles() {
                   ", Exception: " + e);
     }
   }
 }
 
 /**
  * Resets the most common preferences used by tests to their original values.
  */
-async function resetPrefs() {
+function resetPrefs() {
   if (gAppUpdateURLDefault) {
     gDefaultPrefBranch.setCharPref(PREF_APP_UPDATE_URL, gAppUpdateURLDefault);
   }
 
   if (gAppUpdateDisabled !== undefined) {
     Services.prefs.setBoolPref(PREF_APP_UPDATE_DISABLEDFORTESTING, gAppUpdateDisabled);
   } else if (Services.prefs.prefHasUserValue(PREF_APP_UPDATE_DISABLEDFORTESTING)) {
     Services.prefs.clearUserPref(PREF_APP_UPDATE_DISABLEDFORTESTING);
   }
 
-  await gAUS.setAutoUpdateIsEnabled(gAppUpdateAuto);
+  setAppUpdateAutoSync(gAppUpdateAuto);
 
   if (gAppUpdateServiceEnabled !== undefined) {
     Services.prefs.setBoolPref(PREF_APP_UPDATE_SERVICE_ENABLED, gAppUpdateServiceEnabled);
   } else if (Services.prefs.prefHasUserValue(PREF_APP_UPDATE_SERVICE_ENABLED)) {
     Services.prefs.clearUserPref(PREF_APP_UPDATE_SERVICE_ENABLED);
   }
 
   if (gAppUpdateStagingEnabled !== undefined) {
--- a/toolkit/mozapps/update/tests/data/shared.js
+++ b/toolkit/mozapps/update/tests/data/shared.js
@@ -3,16 +3,17 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 /* Shared code for xpcshell and mochitests-chrome */
 /* eslint-disable no-undef */
 
 ChromeUtils.import("resource://gre/modules/FileUtils.jsm");
 ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
 
+const PREF_APP_UPDATE_AUTO                       = "app.update.auto";
 const PREF_APP_UPDATE_BACKGROUNDERRORS           = "app.update.backgroundErrors";
 const PREF_APP_UPDATE_BACKGROUNDMAXERRORS        = "app.update.backgroundMaxErrors";
 const PREF_APP_UPDATE_CANCELATIONS               = "app.update.cancelations";
 const PREF_APP_UPDATE_CHANNEL                    = "app.update.channel";
 const PREF_APP_UPDATE_DOORHANGER                 = "app.update.doorhanger";
 const PREF_APP_UPDATE_DOWNLOADPROMPTATTEMPTS     = "app.update.download.attempts";
 const PREF_APP_UPDATE_DOWNLOADPROMPTMAXATTEMPTS  = "app.update.download.maxAttempts";
 const PREF_APP_UPDATE_DOWNLOADBACKGROUNDINTERVAL = "app.update.download.backgroundInterval";
@@ -30,16 +31,18 @@ const PREF_APP_UPDATE_URL               
 const PREF_APP_UPDATE_URL_DETAILS                = "app.update.url.details";
 const PREF_APP_UPDATE_URL_MANUAL                 = "app.update.url.manual";
 
 const PREFBRANCH_APP_PARTNER         = "app.partner.";
 const PREF_DISTRIBUTION_ID           = "distribution.id";
 const PREF_DISTRIBUTION_VERSION      = "distribution.version";
 const PREF_DISABLE_SECURITY          = "security.turn_off_all_security_so_that_viruses_can_take_over_this_computer";
 
+const CONFIG_APP_UPDATE_AUTO         = "app.update.auto";
+
 const NS_APP_PROFILE_DIR_STARTUP   = "ProfDS";
 const NS_APP_USER_PROFILE_50_DIR   = "ProfD";
 const NS_GRE_BIN_DIR               = "GreBinD";
 const NS_GRE_DIR                   = "GreD";
 const NS_XPCOM_CURRENT_PROCESS_DIR = "XCurProcD";
 const XRE_EXECUTABLE_FILE          = "XREExeF";
 const XRE_OLD_UPDATE_ROOT_DIR      = "OldUpdRootD";
 const XRE_UPDATE_ROOT_DIR          = "UpdRootD";
@@ -228,16 +231,50 @@ function writeStatusFile(aStatus) {
  */
 function writeVersionFile(aVersion) {
   let file = getUpdatesPatchDir();
   file.append(FILE_UPDATE_VERSION);
   writeFile(file, aVersion + "\n");
 }
 
 /**
+ * Synchronously writes the value of the app.update.auto setting to the update
+ * configuration file on Windows or to a user preference on other platforms.
+ * When the value passed to this function is null or undefined it will remove
+ * the configuration file on Windows or the user preference on other platforms.
+ *
+ * @param  aEnabled
+ *         Possible values are true, false, null, and undefined. When true or
+ *         false this value will be written for app.update.auto in the update
+ *         configuration file on Windows or to the user preference on other
+ *         platforms. When null or undefined the update configuration file will
+ *         be removed on Windows or the user preference will be removed on other
+ *         platforms.
+ */
+function setAppUpdateAutoSync(aEnabled) {
+  if (IS_WIN) {
+    let file = getUpdateConfigFile();
+    if (aEnabled === undefined || aEnabled === null) {
+      if (file.exists()) {
+        file.remove(false);
+      }
+    } else {
+      writeFile(file, "{\"" + CONFIG_APP_UPDATE_AUTO + "\":" +
+                      aEnabled.toString() + "}");
+    }
+  } else if (aEnabled === undefined || aEnabled === null) {
+    if (Services.prefs.prefHasUserValue(PREF_APP_UPDATE_AUTO)) {
+      Services.prefs.clearUserPref(PREF_APP_UPDATE_AUTO);
+    }
+  } else {
+    Services.prefs.setBoolPref(PREF_APP_UPDATE_AUTO, aEnabled);
+  }
+}
+
+/**
  * Gets the root directory for the updates directory.
  *
  * @return nsIFile for the updates root directory.
  */
 function getUpdatesRootDir() {
   return Services.dirsvc.get(XRE_UPDATE_ROOT_DIR, Ci.nsIFile);
 }
 
--- a/toolkit/mozapps/update/tests/data/xpcshellUtilsAUS.js
+++ b/toolkit/mozapps/update/tests/data/xpcshellUtilsAUS.js
@@ -761,18 +761,21 @@ var gTestDirsPartialSuccess = [
     dirRemoved: true,
   }];
 
 // Concatenate the common files to the beginning of the array.
 gTestDirsPartialSuccess = gTestDirsCommon.concat(gTestDirsPartialSuccess);
 
 /**
  * Helper function for setting up the test environment.
+ *
+ * @param  aAppUpdateAutoEnabled
+ *         See setAppUpdateAutoSync in shared.js for details.
  */
-function setupTestCommon() {
+function setupTestCommon(aAppUpdateAutoEnabled = false) {
   debugDump("start - general test setup");
 
   Assert.strictEqual(gTestID, undefined,
                      "gTestID should be 'undefined' (setupTestCommon should " +
                      "only be called once)");
 
   let caller = Components.stack.caller;
   gTestID = caller.filename.toString().split("/").pop().split(".")[0];
@@ -869,16 +872,18 @@ function setupTestCommon() {
         removeDirRecursive(updatesDir);
       } catch (e) {
         logTestInfo("non-fatal error removing directory. Path: " +
                     updatesDir.path + ", Exception: " + e);
       }
     }
   }
 
+  setAppUpdateAutoSync(aAppUpdateAutoEnabled);
+
   debugDump("finish - general test setup");
   return true;
 }
 
 /**
  * Nulls out the most commonly used global vars used by tests to prevent leaks
  * as needed and attempts to restore the system to its original state.
  */
--- a/toolkit/mozapps/update/tests/unit_aus_update/uiAutoPref.js
+++ b/toolkit/mozapps/update/tests/unit_aus_update/uiAutoPref.js
@@ -14,25 +14,24 @@ const WindowMediator = {
   getMostRecentWindow(aWindowType) {
     executeSoon(check_status);
     return null;
   },
 
   QueryInterface: ChromeUtils.generateQI([Ci.nsIWindowMediator]),
 };
 
-async function run_test() {
+function run_test() {
   setupTestCommon();
   // Calling do_get_profile prevents an error from being logged
   do_get_profile();
 
   debugDump("testing that an update download doesn't start when the \"update " +
             "auto\" value is false");
 
-  await gAUS.setAutoUpdateIsEnabled(false);
   Services.prefs.setBoolPref(PREF_APP_UPDATE_SILENT, false);
 
   start_httpserver();
   setUpdateURL(gURLData + gHTTPHandlerPath);
   standardInit();
 
   let windowWatcherCID =
     MockRegistrar.register("@mozilla.org/embedcomp/window-watcher;1",
--- a/toolkit/mozapps/update/tests/unit_aus_update/updateAutoPrefMigrate.js
+++ b/toolkit/mozapps/update/tests/unit_aus_update/updateAutoPrefMigrate.js
@@ -15,17 +15,17 @@ async function verifyPref(configFile, ex
                "the expected value");
   let fileContents = await OS.File.read(configFile.path);
   let saveObject = JSON.parse(decoder.decode(fileContents));
   Assert.equal(saveObject["app.update.auto"], expectedValue,
                "Value in update config file should match expected");
 }
 
 async function run_test() {
-  setupTestCommon();
+  setupTestCommon(null);
   standardInit();
 
   let configFile = getUpdateConfigFile();
 
   // Test migration of a |false| value
   Services.prefs.setBoolPref("app.update.auto.migrated", false);
   Services.prefs.setBoolPref("app.update.auto", false);
   Assert.ok(!configFile.exists(), "Config file should not exist yet");
--- a/toolkit/mozapps/update/tests/unit_aus_update/updateDirectoryMigrate.js
+++ b/toolkit/mozapps/update/tests/unit_aus_update/updateDirectoryMigrate.js
@@ -89,17 +89,17 @@ function getOldUpdateLog(aLogLeafName) {
   if (aLogLeafName == FILE_UPDATE_LOG) {
     updateLog.append(DIR_PATCH);
   }
   updateLog.append(aLogLeafName);
   return updateLog;
 }
 
 function run_test() {
-  setupTestCommon();
+  setupTestCommon(null);
 
   debugDump("testing that the update directory is migrated after a successful update");
 
   Services.prefs.setIntPref(PREF_APP_UPDATE_CANCELATIONS, 5);
 
   let patchProps = {state: STATE_PENDING};
   let patches = getLocalPatchString(patchProps);
   let updates = getLocalUpdateString({}, patches);