test_AboutNewTab.js (7351B)
1 /* Any copyright is dedicated to the Public Domain. 2 * http://creativecommons.org/publicdomain/zero/1.0/ 3 */ 4 5 "use strict"; 6 7 /* global AppConstants */ 8 9 /** 10 * This file tests AboutNewTab for its default URL values, as well as its 11 * behaviour when overriding the default URL values. 12 */ 13 14 const { AboutNewTab } = ChromeUtils.importESModule( 15 "resource:///modules/AboutNewTab.sys.mjs" 16 ); 17 18 const IS_RELEASE_OR_BETA = AppConstants.RELEASE_OR_BETA; 19 20 const DOWNLOADS_URL = 21 "chrome://browser/content/downloads/contentAreaDownloadsView.xhtml"; 22 const SEPARATE_PRIVILEGED_CONTENT_PROCESS_PREF = 23 "browser.tabs.remote.separatePrivilegedContentProcess"; 24 const ACTIVITY_STREAM_DEBUG_PREF = "browser.newtabpage.activity-stream.debug"; 25 const SIMPLIFIED_WELCOME_ENABLED_PREF = "browser.aboutwelcome.enabled"; 26 27 function cleanup() { 28 Services.prefs.clearUserPref(SEPARATE_PRIVILEGED_CONTENT_PROCESS_PREF); 29 Services.prefs.clearUserPref(ACTIVITY_STREAM_DEBUG_PREF); 30 Services.prefs.clearUserPref(SIMPLIFIED_WELCOME_ENABLED_PREF); 31 AboutNewTab.resetNewTabURL(); 32 } 33 34 registerCleanupFunction(cleanup); 35 36 function nextChangeNotificationPromise(aNewURL, testMessage) { 37 return new Promise(resolve => { 38 Services.obs.addObserver(function observer(aSubject, aTopic, aData) { 39 Services.obs.removeObserver(observer, aTopic); 40 Assert.equal(aData, aNewURL, testMessage); 41 resolve(); 42 }, "newtab-url-changed"); 43 }); 44 } 45 46 function setPrivilegedContentProcessPref(usePrivilegedContentProcess) { 47 if ( 48 usePrivilegedContentProcess === AboutNewTab.privilegedAboutProcessEnabled 49 ) { 50 return Promise.resolve(); 51 } 52 53 let notificationPromise = nextChangeNotificationPromise("about:newtab"); 54 55 Services.prefs.setBoolPref( 56 SEPARATE_PRIVILEGED_CONTENT_PROCESS_PREF, 57 usePrivilegedContentProcess 58 ); 59 return notificationPromise; 60 } 61 62 function addTestsWithPrivilegedContentProcessPref(test) { 63 add_task(async () => { 64 await setPrivilegedContentProcessPref(true); 65 await test(); 66 }); 67 add_task(async () => { 68 await setPrivilegedContentProcessPref(false); 69 await test(); 70 }); 71 } 72 73 function setBoolPrefAndWaitForChange(pref, value, testMessage) { 74 return new Promise(resolve => { 75 Services.obs.addObserver(function observer(aSubject, aTopic, aData) { 76 Services.obs.removeObserver(observer, aTopic); 77 Assert.equal(aData, AboutNewTab.newTabURL, testMessage); 78 resolve(); 79 }, "newtab-url-changed"); 80 81 Services.prefs.setBoolPref(pref, value); 82 }); 83 } 84 85 add_setup(() => { 86 AboutNewTab.init(); 87 }); 88 89 add_task(async function test_as_initial_values() { 90 Assert.ok( 91 AboutNewTab.activityStreamEnabled, 92 ".activityStreamEnabled should be set to the correct initial value" 93 ); 94 // This pref isn't defined on release or beta, so we fall back to false 95 Assert.equal( 96 AboutNewTab.activityStreamDebug, 97 Services.prefs.getBoolPref(ACTIVITY_STREAM_DEBUG_PREF, false), 98 ".activityStreamDebug should be set to the correct initial value" 99 ); 100 }); 101 102 /** 103 * Test the overriding of the default URL 104 */ 105 add_task(async function test_override_activity_stream_disabled() { 106 let notificationPromise; 107 108 Assert.ok( 109 !AboutNewTab.newTabURLOverridden, 110 "Newtab URL should not be overridden" 111 ); 112 113 // override with some remote URL 114 let url = "http://example.com/"; 115 notificationPromise = nextChangeNotificationPromise(url); 116 AboutNewTab.newTabURL = url; 117 await notificationPromise; 118 Assert.ok(AboutNewTab.newTabURLOverridden, "Newtab URL should be overridden"); 119 Assert.ok( 120 !AboutNewTab.activityStreamEnabled, 121 "Newtab activity stream should not be enabled" 122 ); 123 Assert.equal( 124 AboutNewTab.newTabURL, 125 url, 126 "Newtab URL should be the custom URL" 127 ); 128 129 // test reset with activity stream disabled 130 notificationPromise = nextChangeNotificationPromise("about:newtab"); 131 AboutNewTab.resetNewTabURL(); 132 await notificationPromise; 133 Assert.ok( 134 !AboutNewTab.newTabURLOverridden, 135 "Newtab URL should not be overridden" 136 ); 137 Assert.equal( 138 AboutNewTab.newTabURL, 139 "about:newtab", 140 "Newtab URL should be the default" 141 ); 142 143 // test override to a chrome URL 144 notificationPromise = nextChangeNotificationPromise(DOWNLOADS_URL); 145 AboutNewTab.newTabURL = DOWNLOADS_URL; 146 await notificationPromise; 147 Assert.ok(AboutNewTab.newTabURLOverridden, "Newtab URL should be overridden"); 148 Assert.equal( 149 AboutNewTab.newTabURL, 150 DOWNLOADS_URL, 151 "Newtab URL should be the custom URL" 152 ); 153 154 cleanup(); 155 }); 156 157 addTestsWithPrivilegedContentProcessPref( 158 async function test_override_activity_stream_enabled() { 159 Assert.ok( 160 !AboutNewTab.newTabURLOverridden, 161 "Newtab URL should not be overridden" 162 ); 163 Assert.ok( 164 AboutNewTab.activityStreamEnabled, 165 "Activity Stream should be enabled" 166 ); 167 168 // change to a chrome URL while activity stream is enabled 169 let notificationPromise = nextChangeNotificationPromise(DOWNLOADS_URL); 170 AboutNewTab.newTabURL = DOWNLOADS_URL; 171 await notificationPromise; 172 Assert.equal( 173 AboutNewTab.newTabURL, 174 DOWNLOADS_URL, 175 "Newtab URL set to chrome url" 176 ); 177 Assert.ok( 178 AboutNewTab.newTabURLOverridden, 179 "Newtab URL should be overridden" 180 ); 181 Assert.ok( 182 !AboutNewTab.activityStreamEnabled, 183 "Activity Stream should not be enabled" 184 ); 185 186 cleanup(); 187 } 188 ); 189 190 addTestsWithPrivilegedContentProcessPref(async function test_default_url() { 191 // Only debug variants aren't available on release/beta 192 if (!IS_RELEASE_OR_BETA) { 193 await setBoolPrefAndWaitForChange( 194 ACTIVITY_STREAM_DEBUG_PREF, 195 true, 196 "A notification occurs after changing the debug pref to true" 197 ); 198 Assert.equal( 199 AboutNewTab.activityStreamDebug, 200 true, 201 "the .activityStreamDebug property is set to true" 202 ); 203 await setBoolPrefAndWaitForChange( 204 ACTIVITY_STREAM_DEBUG_PREF, 205 false, 206 "A notification occurs after changing the debug pref to false" 207 ); 208 } else { 209 Services.prefs.setBoolPref(ACTIVITY_STREAM_DEBUG_PREF, true); 210 211 Assert.equal( 212 AboutNewTab.activityStreamDebug, 213 false, 214 "the .activityStreamDebug property is remains false" 215 ); 216 } 217 218 cleanup(); 219 }); 220 221 /** 222 * Tests response to updates to prefs 223 */ 224 addTestsWithPrivilegedContentProcessPref(async function test_updates() { 225 // Simulates a "cold-boot" situation, with some pref already set before testing a series 226 // of changes. 227 AboutNewTab.resetNewTabURL(); // need to set manually because pref notifs are off 228 let notificationPromise; 229 230 // test update fires on override and reset 231 let testURL = "https://example.com/"; 232 notificationPromise = nextChangeNotificationPromise( 233 testURL, 234 "a notification occurs on override" 235 ); 236 AboutNewTab.newTabURL = testURL; 237 await notificationPromise; 238 239 // from overridden to default 240 notificationPromise = nextChangeNotificationPromise( 241 "about:newtab", 242 "a notification occurs on reset" 243 ); 244 AboutNewTab.resetNewTabURL(); 245 Assert.ok( 246 AboutNewTab.activityStreamEnabled, 247 "Activity Stream should be enabled" 248 ); 249 await notificationPromise; 250 251 // reset twice, only one notification for default URL 252 notificationPromise = nextChangeNotificationPromise( 253 "about:newtab", 254 "reset occurs" 255 ); 256 AboutNewTab.resetNewTabURL(); 257 await notificationPromise; 258 259 cleanup(); 260 });