summaryrefslogtreecommitdiffstats
path: root/toolkit/mozapps/update/tests/browser/browser_aboutPrefs_settings.js
blob: 8e40e2bbcfa0a48751fd13a496278a8b125d74cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/* 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);

// Changes, then verifies the value of app.update.auto via the about:preferences
// UI. Requires a tab with about:preferences open to be passed in.
async function changeAndVerifyPref(tab, newConfigValue) {
  await SpecialPowers.spawn(
    tab.linkedBrowser,
    [{ newConfigValue }],
    async function({ newConfigValue }) {
      let radioId = newConfigValue ? "autoDesktop" : "manualDesktop";
      let radioElement = content.document.getElementById(radioId);
      radioElement.click();
    }
  );

  // On Windows, 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 (to wait for the controls to be disabled and
  // re-enabled). So instead, just start the verification by asking the
  // Application Update Service for the value of app.update.auto. It already
  // serializes reads and writes to the app update config file, so this will not
  // resolve until the file write is complete.
  let configValueRead = await UpdateUtils.getAppUpdateAutoEnabled();
  is(
    configValueRead,
    newConfigValue,
    "Value returned should have matched the expected value"
  );

  // Only Windows currently has the update configuration JSON file.
  if (AppConstants.platform == "win") {
    let configFile = getUpdateDirFile(FILE_UPDATE_CONFIG_JSON);
    let decoder = new TextDecoder();
    let fileContents = await OS.File.read(configFile.path);
    let saveObject = JSON.parse(decoder.decode(fileContents));
    is(
      saveObject["app.update.auto"],
      newConfigValue,
      "Value in file should match expected"
    );
  }

  await SpecialPowers.spawn(
    tab.linkedBrowser,
    [{ newConfigValue }],
    async function({ newConfigValue }) {
      let updateRadioGroup = content.document.getElementById(
        "updateRadioGroup"
      );
      is(
        updateRadioGroup.value,
        `${newConfigValue}`,
        "Update preference should match expected"
      );
    }
  );
}

add_task(async function testUpdateAutoPrefUI() {
  let tab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    "about:preferences"
  );

  await changeAndVerifyPref(tab, true);
  ok(
    !gUpdateManager.downloadingUpdate,
    "There should not be a downloading update"
  );
  ok(!gUpdateManager.readyUpdate, "There should not be a ready update");

  await changeAndVerifyPref(tab, false);
  ok(
    !gUpdateManager.downloadingUpdate,
    "There should not be a downloading update"
  );
  ok(!gUpdateManager.readyUpdate, "There should not be a ready update");

  let patchProps = { state: STATE_PENDING };
  let patches = getLocalPatchString(patchProps);
  let updateProps = { checkInterval: "1" };
  let updates = getLocalUpdateString(updateProps, patches);
  writeUpdatesToXMLFile(getLocalUpdatesXMLString(updates), true);
  writeStatusFile(STATE_PENDING);
  reloadUpdateManagerData();
  ok(!!gUpdateManager.readyUpdate, "There should be a ready update");

  // A value of 0 will keep the update and a value of 1 will discard the update
  // when the prompt service is called when the value of app.update.auto is
  // changed to false.
  let discardUpdate = 0;
  let { prompt } = Services;
  let promptService = {
    QueryInterface: ChromeUtils.generateQI(["nsIPromptService"]),
    confirmEx(...args) {
      promptService._confirmExArgs = args;
      return discardUpdate;
    },
  };
  Services.prompt = promptService;
  registerCleanupFunction(() => {
    Services.prompt = prompt;
  });

  // Setting the value to false will call the prompt service and with 1 for
  // discardUpdate the update won't be discarded so there should still be an
  // active update.
  discardUpdate = 1;
  await changeAndVerifyPref(tab, false);
  ok(!!gUpdateManager.readyUpdate, "There should be a ready update");

  // Setting the value to true should not call the prompt service so there
  // should still be an active update even with a value of 0 for
  // discardUpdate.
  discardUpdate = 0;
  await changeAndVerifyPref(tab, true);
  ok(!!gUpdateManager.readyUpdate, "There should be a ready update");

  // Setting the value to false will call the prompt service and with 0 for
  // discardUpdate the update should be discarded so there should not be an
  // active update.
  discardUpdate = 0;
  await changeAndVerifyPref(tab, false);
  ok(
    !gUpdateManager.downloadingUpdate,
    "There should not be a downloading update"
  );
  ok(!gUpdateManager.readyUpdate, "There should not be a ready update");

  await BrowserTestUtils.removeTab(tab);
});