summaryrefslogtreecommitdiffstats
path: root/comm/mail/base/test/unit/test_mailGlue_distribution.js
blob: 0eb1bc95743f058ae3f6343570f037b7c9329ebe (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
var { TBDistCustomizer } = ChromeUtils.import(
  "resource:///modules/TBDistCustomizer.jsm"
);

function run_test() {
  do_test_pending();

  Services.locale.requestedLocales = ["en-US"];

  // Create an instance of nsIFile out of the current process directory
  let distroDir = Services.dirsvc.get("XCurProcD", Ci.nsIFile);

  // Construct a descendant of the distroDir file
  distroDir.append("distribution");

  // Create a clone of distroDir
  let iniFile = distroDir.clone();

  // Create a descendant of iniFile
  iniFile.append("distribution.ini");
  // It's a bug if distribution.ini already exists
  if (iniFile.exists()) {
    do_throw(
      "distribution.ini already exists in objdir/mozilla/dist/bin/distribution."
    );
  }

  registerCleanupFunction(function () {
    // Remove the distribution.ini file
    if (iniFile.exists()) {
      iniFile.remove(true);
    }
  });

  let testDir = Services.dirsvc.get("CurWorkD", Ci.nsIFile);
  let testDistributionFile = testDir.clone();

  // Construct descendant file
  testDistributionFile.append("distribution.ini");
  // Copy to distroDir
  testDistributionFile.copyTo(distroDir, "distribution.ini");
  Assert.ok(testDistributionFile.exists());

  // Set the prefs
  TBDistCustomizer.applyPrefDefaults();

  let testIni = Cc["@mozilla.org/xpcom/ini-parser-factory;1"]
    .getService(Ci.nsIINIParserFactory)
    .createINIParser(testDistributionFile);

  // Now check that prefs were set - test the Global prefs against the
  // Global section in the ini file
  let iniValue = testIni.getString("Global", "id");
  let pref = Services.prefs.getCharPref("distribution.id");
  Assert.equal(iniValue, pref);

  iniValue = testIni.getString("Global", "version");
  pref = Services.prefs.getCharPref("distribution.version");
  Assert.equal(iniValue, pref);

  let aboutLocale;
  try {
    aboutLocale = testIni.getString("Global", "about.en-US");
  } catch (e) {
    console.error(e);
  }

  if (aboutLocale == undefined) {
    aboutLocale = testIni.getString("Global", "about");
  }

  pref = Services.prefs.getCharPref("distribution.about");
  Assert.equal(aboutLocale, pref);

  // Test Preferences section
  let s = "Preferences";
  for (let key of testIni.getKeys(s)) {
    let value = TBDistCustomizer.parseValue(testIni.getString(s, key));
    switch (typeof value) {
      case "boolean":
        Assert.equal(value, Services.prefs.getBoolPref(key));
        break;
      case "number":
        Assert.equal(value, Services.prefs.getIntPref(key));
        break;
      case "string":
        Assert.equal(value, Services.prefs.getCharPref(key));
        break;
      default:
        do_throw(
          "The preference " + key + " is of unknown type: " + typeof value
        );
    }
  }

  // Test the LocalizablePreferences-[locale] section
  // Add any prefs found in it to the overrides array
  let overrides = [];
  s = "LocalizablePreferences-en-US";
  for (let key of testIni.getKeys(s)) {
    let value = TBDistCustomizer.parseValue(testIni.getString(s, key));
    value = "data:text/plain," + key + "=" + value;
    Assert.equal(value, Services.prefs.getCharPref(key));
    overrides.push(key);
  }

  // Test the LocalizablePreferences section
  // Any prefs here that aren't found in overrides are not overridden
  //   by LocalizablePrefs-[locale] and should be tested
  s = "LocalizablePreferences";
  for (let key of testIni.getKeys(s)) {
    if (!overrides.includes(key)) {
      let value = TBDistCustomizer.parseValue(testIni.getString(s, key));
      value = value.replace(/%LOCALE%/g, "en-US");
      value = "data:text/plain," + key + "=" + value;
      Assert.equal(value, Services.prefs.getCharPref(key));
    }
  }
  do_test_finished();
}