summaryrefslogtreecommitdiffstats
path: root/browser/components/tests/unit/test_distribution_cachedexistence.js
blob: 192687e68f53cdd1e9ea2f6005f348e56358c43b (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

/**
 * Tests that DistributionCustomizer correctly caches the existence
 * of the distribution.ini file and just rechecks it after a version
 * update.
 */

const PREF_CACHED_FILE_EXISTENCE = "distribution.iniFile.exists.value";
const PREF_CACHED_FILE_APPVERSION = "distribution.iniFile.exists.appversion";
const PREF_LOAD_FROM_PROFILE = "distribution.testing.loadFromProfile";

const gTestDir = do_get_cwd();

const { AppConstants } = ChromeUtils.importESModule(
  "resource://gre/modules/AppConstants.sys.mjs"
);

add_task(async function() {
  // Start with a clean slate of the prefs that control this feature.
  Services.prefs.clearUserPref(PREF_CACHED_FILE_APPVERSION);
  Services.prefs.clearUserPref(PREF_CACHED_FILE_EXISTENCE);
  setupTest();

  let { DistributionCustomizer } = ChromeUtils.import(
    "resource:///modules/distribution.js"
  );
  let distribution = new DistributionCustomizer();

  copyDistributionToProfile();

  // Check that checking for distribution.ini returns the right value and sets up
  // the cached prefs.
  let exists = distribution._hasDistributionIni;

  Assert.ok(exists);
  Assert.equal(
    Services.prefs.getBoolPref(PREF_CACHED_FILE_EXISTENCE, undefined),
    true
  );
  Assert.equal(
    Services.prefs.getStringPref(PREF_CACHED_FILE_APPVERSION, "unknown"),
    AppConstants.MOZ_APP_VERSION
  );

  // Check that calling _hasDistributionIni again will use the cached value. We do
  // this by deleting the file and expecting it to still return true instead of false.
  // Also, we need to delete _hasDistributionIni from the object because the getter
  // was replaced with a stored value.
  deleteDistribution();
  delete distribution._hasDistributionIni;

  exists = distribution._hasDistributionIni;
  Assert.ok(exists);

  // Now let's invalidate the PREF_CACHED_FILE_EXISTENCE pref to make sure the
  // value gets recomputed correctly.
  Services.prefs.clearUserPref(PREF_CACHED_FILE_EXISTENCE);
  delete distribution._hasDistributionIni;
  exists = distribution._hasDistributionIni;

  // It now should return false, as well as storing false in the pref.
  Assert.ok(!exists);
  Assert.equal(
    Services.prefs.getBoolPref(PREF_CACHED_FILE_EXISTENCE, undefined),
    false
  );

  // Check now that it will use the new cached value instead of returning true in
  // the presence of the file.
  copyDistributionToProfile();
  delete distribution._hasDistributionIni;
  exists = distribution._hasDistributionIni;

  Assert.ok(!exists);

  // Now let's do the same, but invalidating the App Version, as if a version
  // update occurred.
  Services.prefs.setStringPref(PREF_CACHED_FILE_APPVERSION, "older version");
  delete distribution._hasDistributionIni;
  exists = distribution._hasDistributionIni;

  Assert.ok(exists);
  Assert.equal(
    Services.prefs.getBoolPref(PREF_CACHED_FILE_EXISTENCE, undefined),
    true
  );
  Assert.equal(
    Services.prefs.getStringPref(PREF_CACHED_FILE_APPVERSION, "unknown"),
    AppConstants.MOZ_APP_VERSION
  );
});

/*
 * Helper functions
 */
function copyDistributionToProfile() {
  // Copy distribution.ini file to the profile dir.
  let distroDir = gProfD.clone();
  distroDir.leafName = "distribution";
  let iniFile = distroDir.clone();
  iniFile.append("distribution.ini");
  if (iniFile.exists()) {
    iniFile.remove(false);
    print("distribution.ini already exists, did some test forget to cleanup?");
  }

  let testDistributionFile = gTestDir.clone();
  testDistributionFile.append("distribution.ini");
  testDistributionFile.copyTo(distroDir, "distribution.ini");
  Assert.ok(testDistributionFile.exists());
}

function deleteDistribution() {
  let distroDir = gProfD.clone();
  distroDir.leafName = "distribution";
  let iniFile = distroDir.clone();
  iniFile.append("distribution.ini");
  iniFile.remove(false);
}

function setupTest() {
  // Set special pref to load distribution.ini from the profile folder.
  Services.prefs.setBoolPref(PREF_LOAD_FROM_PROFILE, true);
}

registerCleanupFunction(function() {
  deleteDistribution();
  Services.prefs.clearUserPref(PREF_LOAD_FROM_PROFILE);
});