browser_suppressTips.js (4037B)
1 /* Any copyright is dedicated to the Public Domain. 2 * http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 // Tests that the browser tips are suppressed correctly. 5 6 "use strict"; 7 8 /* import-globals-from ../head.js */ 9 10 ChromeUtils.defineESModuleGetters(this, { 11 LaterRun: "resource:///modules/LaterRun.sys.mjs", 12 UrlbarProviderSearchTips: 13 "moz-src:///browser/components/urlbar/UrlbarProviderSearchTips.sys.mjs", 14 }); 15 16 const LAST_UPDATE_THRESHOLD_HOURS = 24; 17 18 add_setup(async function () { 19 await PlacesUtils.history.clear(); 20 21 await SpecialPowers.pushPrefEnv({ 22 set: [ 23 [ 24 `browser.urlbar.tipShownCount.${UrlbarProviderSearchTips.TIP_TYPE.ONBOARD}`, 25 0, 26 ], 27 ], 28 }); 29 30 registerCleanupFunction(() => { 31 resetSearchTipsProvider(); 32 Services.prefs.clearUserPref( 33 "browser.laterrun.bookkeeping.profileCreationTime" 34 ); 35 Services.prefs.clearUserPref( 36 "browser.laterrun.bookkeeping.updateAppliedTime" 37 ); 38 }); 39 }); 40 41 add_task(async function updateApplied() { 42 // Check the update time. 43 Assert.notEqual( 44 Services.prefs.getIntPref( 45 "browser.laterrun.bookkeeping.updateAppliedTime", 46 0 47 ), 48 0, 49 "updateAppliedTime pref should be updated when booting this test" 50 ); 51 Assert.equal( 52 LaterRun.hoursSinceUpdate, 53 0, 54 "LaterRun.hoursSinceUpdate is 0 since one hour should not have passed from starting this test" 55 ); 56 57 // To not suppress the tip by profile creation. 58 Services.prefs.setIntPref( 59 "browser.laterrun.bookkeeping.profileCreationTime", 60 secondsBasedOnNow(LAST_UPDATE_THRESHOLD_HOURS + 0.5) 61 ); 62 63 // The test harness will use the current tab and remove the tab's history. 64 // Since the page that is tested is opened prior to the test harness taking 65 // over the current tab the active-update.xml specifies two pages to open by 66 // having 'https://example.com/|https://example.com/' for the value of openURL 67 // and then uses the first tab for the test. 68 gBrowser.selectedTab = gBrowser.tabs[0]; 69 // The test harness also changes the page to about:blank so go back to the 70 // page that was originally opened. 71 gBrowser.goBack(); 72 // Wait for the page to go back to the original page. 73 await TestUtils.waitForCondition( 74 () => gBrowser.selectedBrowser?.currentURI?.spec == "https://example.com/", 75 "Waiting for the expected page to reopen" 76 ); 77 gBrowser.removeTab(gBrowser.selectedTab); 78 79 // Check whether the tip is suppressed by update. 80 await checkTab(window, "about:newtab"); 81 82 // Clean up. 83 const alternatePath = Services.prefs.getCharPref( 84 "app.update.altUpdateDirPath" 85 ); 86 const testRoot = Services.prefs.getCharPref("mochitest.testRoot"); 87 let relativePath = alternatePath.substring("<test-root>".length); 88 if (AppConstants.platform == "win") { 89 relativePath = relativePath.replace(/\//g, "\\"); 90 } 91 const updateDir = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile); 92 updateDir.initWithPath(testRoot + relativePath); 93 const updatesFile = updateDir.clone(); 94 updatesFile.append("updates.xml"); 95 await TestUtils.waitForCondition( 96 () => updatesFile.exists(), 97 "Waiting until the updates.xml file exists" 98 ); 99 updatesFile.remove(false); 100 }); 101 102 add_task(async function profileAge() { 103 // To not suppress the tip by profile creation and update. 104 Services.prefs.setIntPref( 105 "browser.laterrun.bookkeeping.profileCreationTime", 106 secondsBasedOnNow(LAST_UPDATE_THRESHOLD_HOURS + 0.5) 107 ); 108 Services.prefs.setIntPref( 109 "browser.laterrun.bookkeeping.updateAppliedTime", 110 secondsBasedOnNow(LAST_UPDATE_THRESHOLD_HOURS + 0.5) 111 ); 112 await checkTab( 113 window, 114 "about:newtab", 115 UrlbarProviderSearchTips.TIP_TYPE.ONBOARD 116 ); 117 118 // To suppress the tip by profile creation. 119 Services.prefs.setIntPref( 120 "browser.laterrun.bookkeeping.profileCreationTime", 121 secondsBasedOnNow() 122 ); 123 await checkTab(window, "about:newtab"); 124 }); 125 126 function secondsBasedOnNow(howManyHoursAgo = 0) { 127 return Math.floor(Date.now() / 1000 - howManyHoursAgo * 60 * 60); 128 }