test_distribution_cachedexistence.js (4229B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 /** 5 * Tests that DistributionCustomizer correctly caches the existence 6 * of the distribution.ini file and just rechecks it after a version 7 * update. 8 */ 9 10 const PREF_CACHED_FILE_EXISTENCE = "distribution.iniFile.exists.value"; 11 const PREF_CACHED_FILE_APPVERSION = "distribution.iniFile.exists.appversion"; 12 const PREF_LOAD_FROM_PROFILE = "distribution.testing.loadFromProfile"; 13 14 const gTestDir = do_get_cwd(); 15 16 const { AppConstants } = ChromeUtils.importESModule( 17 "resource://gre/modules/AppConstants.sys.mjs" 18 ); 19 20 add_task(async function () { 21 // Start with a clean slate of the prefs that control this feature. 22 Services.prefs.clearUserPref(PREF_CACHED_FILE_APPVERSION); 23 Services.prefs.clearUserPref(PREF_CACHED_FILE_EXISTENCE); 24 setupTest(); 25 26 let { DistributionCustomizer } = ChromeUtils.importESModule( 27 "resource:///modules/distribution.sys.mjs" 28 ); 29 let distribution = new DistributionCustomizer(); 30 31 copyDistributionToProfile(); 32 33 // Check that checking for distribution.ini returns the right value and sets up 34 // the cached prefs. 35 let exists = distribution._hasDistributionIni; 36 37 Assert.ok(exists); 38 Assert.equal( 39 Services.prefs.getBoolPref(PREF_CACHED_FILE_EXISTENCE, undefined), 40 true 41 ); 42 Assert.equal( 43 Services.prefs.getStringPref(PREF_CACHED_FILE_APPVERSION, "unknown"), 44 AppConstants.MOZ_APP_VERSION 45 ); 46 47 // Check that calling _hasDistributionIni again will use the cached value. We do 48 // this by deleting the file and expecting it to still return true instead of false. 49 // Also, we need to delete _hasDistributionIni from the object because the getter 50 // was replaced with a stored value. 51 deleteDistribution(); 52 delete distribution._hasDistributionIni; 53 54 exists = distribution._hasDistributionIni; 55 Assert.ok(exists); 56 57 // Now let's invalidate the PREF_CACHED_FILE_EXISTENCE pref to make sure the 58 // value gets recomputed correctly. 59 Services.prefs.clearUserPref(PREF_CACHED_FILE_EXISTENCE); 60 delete distribution._hasDistributionIni; 61 exists = distribution._hasDistributionIni; 62 63 // It now should return false, as well as storing false in the pref. 64 Assert.ok(!exists); 65 Assert.equal( 66 Services.prefs.getBoolPref(PREF_CACHED_FILE_EXISTENCE, undefined), 67 false 68 ); 69 70 // Check now that it will use the new cached value instead of returning true in 71 // the presence of the file. 72 copyDistributionToProfile(); 73 delete distribution._hasDistributionIni; 74 exists = distribution._hasDistributionIni; 75 76 Assert.ok(!exists); 77 78 // Now let's do the same, but invalidating the App Version, as if a version 79 // update occurred. 80 Services.prefs.setStringPref(PREF_CACHED_FILE_APPVERSION, "older version"); 81 delete distribution._hasDistributionIni; 82 exists = distribution._hasDistributionIni; 83 84 Assert.ok(exists); 85 Assert.equal( 86 Services.prefs.getBoolPref(PREF_CACHED_FILE_EXISTENCE, undefined), 87 true 88 ); 89 Assert.equal( 90 Services.prefs.getStringPref(PREF_CACHED_FILE_APPVERSION, "unknown"), 91 AppConstants.MOZ_APP_VERSION 92 ); 93 }); 94 95 /* 96 * Helper functions 97 */ 98 function copyDistributionToProfile() { 99 // Copy distribution.ini file to the profile dir. 100 let distroDir = gProfD.clone(); 101 distroDir.leafName = "distribution"; 102 let iniFile = distroDir.clone(); 103 iniFile.append("distribution.ini"); 104 if (iniFile.exists()) { 105 iniFile.remove(false); 106 print("distribution.ini already exists, did some test forget to cleanup?"); 107 } 108 109 let testDistributionFile = gTestDir.clone(); 110 testDistributionFile.append("distribution.ini"); 111 testDistributionFile.copyTo(distroDir, "distribution.ini"); 112 Assert.ok(testDistributionFile.exists()); 113 } 114 115 function deleteDistribution() { 116 let distroDir = gProfD.clone(); 117 distroDir.leafName = "distribution"; 118 let iniFile = distroDir.clone(); 119 iniFile.append("distribution.ini"); 120 iniFile.remove(false); 121 } 122 123 function setupTest() { 124 // Set special pref to load distribution.ini from the profile folder. 125 Services.prefs.setBoolPref(PREF_LOAD_FROM_PROFILE, true); 126 } 127 128 registerCleanupFunction(function () { 129 deleteDistribution(); 130 Services.prefs.clearUserPref(PREF_LOAD_FROM_PROFILE); 131 });