summaryrefslogtreecommitdiffstats
path: root/extensions/pref/autoconfig/test/unit/test_autoconfig_snap.js
blob: 1a58e11cf546a38598b9d524bc2bbde1a64fbafc (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

/* eslint no-unsafe-finally: "off"*/
/* Turning off this rule to allow control flow operations in finally block
 * http://eslint.org/docs/rules/no-unsafe-finally  */

const { updateAppInfo } = ChromeUtils.importESModule(
  "resource://testing-common/AppInfo.sys.mjs"
);

function ensureRemove(file) {
  try {
    file.remove(false);
  } catch (e) {
    if (e.result != Cr.NS_ERROR_FILE_NOT_FOUND) {
      throw e;
    }
  }
}

async function run_test() {
  let prefs = Services.prefs.getBranch(null);

  let testDir = do_get_cwd();
  let confDir = testDir.clone();
  confDir.append("MozSystemConfigDir");
  Services.env.set("MOZ_SYSTEM_CONFIG_DIR", confDir.path);
  Services.env.set("SNAP_INSTANCE_NAME", "xpcshell");

  updateAppInfo();

  let sysConfD = Services.dirsvc.get("SysConfD", Ci.nsIFile);

  let defaultPrefDExtra = sysConfD.clone();
  defaultPrefDExtra.append("defaults");
  defaultPrefDExtra.append("pref");

  await IOUtils.makeDirectory(defaultPrefDExtra.path);

  const kAutoConfigFile = defaultPrefDExtra.clone();
  kAutoConfigFile.append("autoconfig_snap.js");
  const kAutoConfigCfg = sysConfD.clone();
  kAutoConfigCfg.append("autoconfig-snap.cfg");

  let autoConfigJS = testDir.clone();
  autoConfigJS.append(kAutoConfigFile.leafName);

  let autoConfigCfg = testDir.clone();
  autoConfigCfg.append(kAutoConfigCfg.leafName);

  try {
    autoConfigJS.copyTo(kAutoConfigFile.parent, kAutoConfigFile.leafName);
    autoConfigCfg.copyTo(kAutoConfigCfg.parent, kAutoConfigCfg.leafName);

    // Make sure nsReadConfig is initialized.
    Cc["@mozilla.org/readconfig;1"].getService(Ci.nsISupports);
    Services.prefs.resetPrefs();

    Services.obs.notifyObservers(
      Services.prefs,
      "prefservice:before-read-userprefs"
    );

    ok(prefs.prefHasUserValue("_autoconfig_.test.userpref-snap"));
    equal(
      "userpref-snap",
      prefs.getStringPref("_autoconfig_.test.userpref-snap")
    );

    Services.prefs.resetPrefs();
  } finally {
    ensureRemove(kAutoConfigFile);
    ensureRemove(kAutoConfigCfg);
    Services.prefs.resetPrefs();
  }
}