test_usageProgileGroupID.js (3565B)
1 /* Any copyright is dedicated to the Public Domain. 2 https://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 ChromeUtils.defineESModuleGetters(lazy, { 7 ClientID: "resource://gre/modules/ClientID.sys.mjs", 8 TestUtils: "resource://testing-common/TestUtils.sys.mjs", 9 }); 10 11 const DAU_GROUPID_PREF_NAME = "datareporting.dau.cachedUsageProfileGroupID"; 12 const middleUUID = "4ff7abd7-5a64-4ef3-b88b-e5cd35b37807"; 13 const worstUUID = "96199cc4-828d-4018-82d3-6b8101ffddce"; 14 const bestUUID = "08f1975a-5128-4888-be29-0bab2c84cd62"; 15 16 add_setup(async () => { 17 startProfileService(); 18 const SelectableProfileService = getSelectableProfileService(); 19 const ProfilesDatastoreService = getProfilesDatastoreService(); 20 21 SelectableProfileService.constructor.permanentSharedPrefs.splice( 22 0, 23 SelectableProfileService.constructor.permanentSharedPrefs.length, 24 DAU_GROUPID_PREF_NAME 25 ); 26 27 lazy.ClientID.setUsageProfileGroupID(middleUUID); 28 29 await ProfilesDatastoreService.init(); 30 await SelectableProfileService.init(); 31 32 await SelectableProfileService.maybeSetupDataStore(); 33 }); 34 35 add_task(async function test_groupIDConvergence() { 36 const SelectableProfileService = getSelectableProfileService(); 37 let dbUUID = await SelectableProfileService.getDBPref(DAU_GROUPID_PREF_NAME); 38 39 Assert.equal( 40 dbUUID, 41 middleUUID, 42 `${DAU_GROUPID_PREF_NAME} should be ${middleUUID}` 43 ); 44 Assert.equal( 45 Services.prefs.getStringPref(DAU_GROUPID_PREF_NAME, ""), 46 middleUUID, 47 `${DAU_GROUPID_PREF_NAME} should be ${middleUUID}` 48 ); 49 50 // Set the DAU_GROUPID_PREF_NAME to a worse value in the db and then load the 51 // prefs from the db. The pref should not be updated. 52 let dbChangedPromise = lazy.TestUtils.topicObserved("pds-datastore-changed"); 53 await SelectableProfileService.setDBPref(DAU_GROUPID_PREF_NAME, worstUUID); 54 await dbChangedPromise; 55 await SelectableProfileService.loadSharedPrefsFromDatabase(); 56 57 dbUUID = await SelectableProfileService.getDBPref(DAU_GROUPID_PREF_NAME); 58 59 Assert.equal( 60 dbUUID, 61 worstUUID, 62 `${DAU_GROUPID_PREF_NAME} should be ${worstUUID}` 63 ); 64 Assert.equal( 65 Services.prefs.getStringPref(DAU_GROUPID_PREF_NAME, ""), 66 middleUUID, 67 `${DAU_GROUPID_PREF_NAME} should still be ${middleUUID}` 68 ); 69 70 // Now init the SelectableProfileService and confirm that we set the 71 // DAU_GROUPID_PREF_NAME to our pref value in the db. 72 await SelectableProfileService.uninit(); 73 await SelectableProfileService.init(); 74 75 dbUUID = await SelectableProfileService.getDBPref(DAU_GROUPID_PREF_NAME); 76 77 Assert.equal( 78 dbUUID, 79 middleUUID, 80 `${DAU_GROUPID_PREF_NAME} should now be ${middleUUID}` 81 ); 82 Assert.equal( 83 Services.prefs.getStringPref(DAU_GROUPID_PREF_NAME, ""), 84 middleUUID, 85 `${DAU_GROUPID_PREF_NAME} should still be ${middleUUID}` 86 ); 87 88 // Set the DAU_GROUPID_PREF_NAME to a better value in the db and then load 89 // the prefs from the db. The pref should be updated to the better value. 90 dbChangedPromise = lazy.TestUtils.topicObserved("pds-datastore-changed"); 91 await SelectableProfileService.setDBPref(DAU_GROUPID_PREF_NAME, bestUUID); 92 await dbChangedPromise; 93 await SelectableProfileService.loadSharedPrefsFromDatabase(); 94 95 dbUUID = await SelectableProfileService.getDBPref(DAU_GROUPID_PREF_NAME); 96 97 Assert.equal( 98 dbUUID, 99 bestUUID, 100 `${DAU_GROUPID_PREF_NAME} should now be ${bestUUID}` 101 ); 102 Assert.equal( 103 Services.prefs.getStringPref(DAU_GROUPID_PREF_NAME, ""), 104 bestUUID, 105 `${DAU_GROUPID_PREF_NAME} should now be ${bestUUID}` 106 ); 107 });