Bug 1458308 - Tests for migration and UI of app.update.auto pref draft
authorKirk Steuber <ksteuber@mozilla.com>
Mon, 09 Jul 2018 16:11:47 -0700
changeset 829194 7911d7f492a9bb487d3010f277762dc994afbe41
parent 829193 c1445cf55db73e6fc916cc4a639168ede1197d13
push id118747
push userbmo:ksteuber@mozilla.com
push dateTue, 14 Aug 2018 21:08:44 +0000
bugs1458308
milestone63.0a1
Bug 1458308 - Tests for migration and UI of app.update.auto pref MozReview-Commit-ID: 84Yy8pT9cvj
toolkit/mozapps/update/tests/browser/browser.ini
toolkit/mozapps/update/tests/browser/browser_updateAutoPrefUI.js
toolkit/mozapps/update/tests/unit_aus_update/updateAutoPrefMigrate.js
toolkit/mozapps/update/tests/unit_aus_update/xpcshell.ini
--- a/toolkit/mozapps/update/tests/browser/browser.ini
+++ b/toolkit/mozapps/update/tests/browser/browser.ini
@@ -1,16 +1,17 @@
 [DEFAULT]
 tags = appupdate
 support-files =
   head.js
   downloadPage.html
   testConstants.js
 
 [browser_TelemetryUpdatePing.js]
+[browser_updateAutoPrefUI.js]
 [browser_updatesBackgroundWindow.js]
 [browser_updatesBackgroundWindowFailures.js]
 skip-if = verify
 [browser_updatesBasicPrompt.js]
 skip-if = asan
 reason = Bug 1168003
 [browser_updatesBasicPromptNoStaging.js]
 [browser_updatesCantApply.js]
new file mode 100644
--- /dev/null
+++ b/toolkit/mozapps/update/tests/browser/browser_updateAutoPrefUI.js
@@ -0,0 +1,61 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+ChromeUtils.import("resource://gre/modules/osfile.jsm", this);
+const aus = Cc["@mozilla.org/updates/update-service;1"]
+            .getService(Ci.nsIApplicationUpdateService);
+
+
+const FILE_UPDATE_PREFS = "prefs.json";
+
+let prefFile = getUpdatesRootDir();
+prefFile.append(FILE_UPDATE_PREFS);
+let decoder = new TextDecoder();
+
+// Changes, then verifies the pref value via the about:preferences UI. Requires
+// a tab with about:preferences open to be passed in.
+async function changeAndVerifyPref(tab, newPrefValue) {
+  await ContentTask.spawn(tab.linkedBrowser, {newPrefValue}, async function({newPrefValue}) {
+    let radioId = newPrefValue ? "autoDesktop" : "manualDesktop";
+    let radioElement = content.document.getElementById(radioId);
+    radioElement.click();
+  });
+
+  // At this point, we really need to wait for the change to finish being
+  // written to the disk before we go to verify anything. Unfortunately, it
+  // would be difficult to check for quick changes to the attributes of the
+  // about:preferences controls. So instead, just start the verification by
+  // asking the Application Update Service for the value of the pref. It already
+  // serializes reads and writes to the pref file, so this will not resolve
+  // until the file write is complete.
+
+  let prefValueRead = await aus.autoUpdateIsEnabled();
+  is(prefValueRead, newPrefValue,
+     "Value returned should have matched the expected value");
+
+  let fileContents = await OS.File.read(prefFile.path);
+  let saveObject = JSON.parse(decoder.decode(fileContents));
+  is(saveObject["app.update.auto"], newPrefValue,
+     "Value in file should match expected");
+
+  await ContentTask.spawn(tab.linkedBrowser, {newPrefValue}, async function({newPrefValue}) {
+    let updateRadioGroup = content.document.getElementById("updateRadioGroup");
+    is(updateRadioGroup.value, `${newPrefValue}`,
+       "Update preference should match expected");
+  });
+}
+
+add_task(async function testUpdateAutoPrefUI() {
+  let originalPrefValue = await aus.autoUpdateIsEnabled();
+  let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, "about:preferences");
+
+  await changeAndVerifyPref(tab, true);
+  await changeAndVerifyPref(tab, false);
+  await changeAndVerifyPref(tab, false);
+  await changeAndVerifyPref(tab, true);
+
+  await BrowserTestUtils.removeTab(tab);
+  await aus.setAutoUpdateEnabled(originalPrefValue);
+});
new file mode 100644
--- /dev/null
+++ b/toolkit/mozapps/update/tests/unit_aus_update/updateAutoPrefMigrate.js
@@ -0,0 +1,60 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+ChromeUtils.import("resource://gre/modules/osfile.jsm", this);
+let aus = Cc["@mozilla.org/updates/update-service;1"]
+          .getService(Ci.nsIApplicationUpdateService);
+
+const FILE_UPDATE_PREFS = "prefs.json";
+
+// Checks both the pref value returned by the update service and the one
+// written to pref file
+async function verifyPref(prefFile, expectedValue) {
+  let decoder = new TextDecoder();
+  let prefValue = await aus.autoUpdateIsEnabled();
+  Assert.equal(prefValue, expectedValue,
+               "Value returned should have matched the expected value");
+  let fileContents = await OS.File.read(prefFile.path);
+  let saveObject = JSON.parse(decoder.decode(fileContents));
+  Assert.equal(saveObject["app.update.auto"], expectedValue,
+               "Value in file should match expected");
+}
+
+async function run_test() {
+  setupTestCommon();
+  standardInit();
+
+  let prefFile = getUpdatesRootDir();
+  prefFile.append(FILE_UPDATE_PREFS);
+
+  let originalFileValue = await aus.autoUpdateIsEnabled();
+  let originalPrefValue = Services.prefs.getBoolPref("app.update.auto", null);
+
+  // Test migration of a |false| value
+  Services.prefs.setBoolPref("app.update.auto", false);
+  debugDump(`about to remove pref file`);
+  prefFile.remove(false);
+  Assert.ok(!prefFile.exists(), "Pref file should have been removed");
+  await verifyPref(prefFile, false);
+
+  // Test migration of a |true| value
+  Services.prefs.setBoolPref("app.update.auto", true);
+  prefFile.remove(false);
+  Assert.ok(!prefFile.exists(), "Pref file should have been removed");
+  await verifyPref(prefFile, true);
+
+  // Test that setting the pref without migrating also works
+  await aus.setAutoUpdateEnabled(false);
+  await verifyPref(prefFile, false);
+
+  // Restore original state
+  await aus.setAutoUpdateEnabled(originalFileValue);
+  if (originalPrefValue == null) {
+    Services.prefs.clearUserPref("app.update.auto");
+  } else {
+    Services.prefs.setBoolPref("app.update.auto", originalPrefValue);
+  }
+  doTestFinish();
+}
--- a/toolkit/mozapps/update/tests/unit_aus_update/xpcshell.ini
+++ b/toolkit/mozapps/update/tests/unit_aus_update/xpcshell.ini
@@ -23,10 +23,11 @@ head = head_update.js
 [downloadInterruptedOffline.js]
 [downloadInterruptedNoRecovery.js]
 [downloadInterruptedRecovery.js]
 [downloadResumeForSameAppVersion.js]
 [downloadCompleteAfterPartialFailure.js]
 [uiSilentPref.js]
 [uiUnsupportedAlreadyNotified.js]
 [uiAutoPref.js]
+[updateAutoPrefMigrate.js]
 [updateDirectoryMigrate.js]
 skip-if = os != 'win' # Update directory migration happens on Windows only