browser_windows_launch_on_login.js (6989B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 ChromeUtils.defineESModuleGetters(this, { 7 BackgroundUpdate: "resource://gre/modules/BackgroundUpdate.sys.mjs", 8 MigrationUtils: "resource:///modules/MigrationUtils.sys.mjs", 9 PermissionTestUtils: "resource://testing-common/PermissionTestUtils.sys.mjs", 10 WindowsLaunchOnLogin: "resource://gre/modules/WindowsLaunchOnLogin.sys.mjs", 11 }); 12 13 const { MockRegistry } = ChromeUtils.importESModule( 14 "resource://testing-common/MockRegistry.sys.mjs" 15 ); 16 17 let profileService = Cc["@mozilla.org/toolkit/profile-service;1"].getService( 18 Ci.nsIToolkitProfileService 19 ); 20 let startWithLastProfileOriginal = profileService.startWithLastProfile; 21 let registry = null; 22 add_setup(() => { 23 registry = new MockRegistry(); 24 registry.setValue( 25 Ci.nsIWindowsRegKey.ROOT_KEY_CURRENT_USER, 26 "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", 27 "", 28 "" 29 ); 30 registry.setValue( 31 Ci.nsIWindowsRegKey.ROOT_KEY_CURRENT_USER, 32 "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\StartupApproved\\Run", 33 "", 34 "" 35 ); 36 registerCleanupFunction(() => { 37 registry.shutdown(); 38 }); 39 }); 40 41 add_task(async function test_check_uncheck_checkbox() { 42 await ExperimentAPI.ready(); 43 let doCleanup = await NimbusTestUtils.enrollWithFeatureConfig({ 44 featureId: "windowsLaunchOnLogin", 45 value: { enabled: true }, 46 }); 47 await WindowsLaunchOnLogin.withLaunchOnLoginRegistryKey(async wrk => { 48 // Open preferences to general pane 49 await openPreferencesViaOpenPreferencesAPI("paneGeneral", { 50 leaveOpen: true, 51 }); 52 let doc = gBrowser.contentDocument; 53 54 let launchOnLoginCheckbox = doc.getElementById("windowsLaunchOnLogin"); 55 let launchOnLoginControl = launchOnLoginCheckbox.parentElement; 56 57 ok(!launchOnLoginControl.hidden, "Autostart control is visible"); 58 59 ok( 60 !launchOnLoginCheckbox.checked, 61 "Autostart checkbox NOT checked by default" 62 ); 63 64 launchOnLoginCheckbox.click(); 65 66 ok(launchOnLoginCheckbox.checked, "Autostart checkbox checked after click"); 67 68 ok( 69 wrk.hasValue(WindowsLaunchOnLogin.getLaunchOnLoginRegistryName()), 70 "Key exists" 71 ); 72 73 launchOnLoginCheckbox.click(); 74 ok(!launchOnLoginCheckbox.checked, "Autostart checkbox unchecked"); 75 76 await TestUtils.waitForCondition( 77 () => !wrk.hasValue(WindowsLaunchOnLogin.getLaunchOnLoginRegistryName()), 78 "Waiting for Autostart registry key to be removed", 79 undefined, 80 200 81 ); 82 83 gBrowser.removeCurrentTab(); 84 }); 85 await doCleanup(); 86 }); 87 88 add_task(async function create_external_regkey() { 89 if (Services.sysinfo.getProperty("hasWinPackageId")) { 90 return; 91 } 92 await ExperimentAPI.ready(); 93 let doCleanup = await NimbusTestUtils.enrollWithFeatureConfig({ 94 featureId: "windowsLaunchOnLogin", 95 value: { enabled: true }, 96 }); 97 await WindowsLaunchOnLogin.withLaunchOnLoginRegistryKey(async wrk => { 98 // Delete any existing entries before testing 99 // Both functions are install specific so it's safe to run them 100 // like this. 101 wrk.removeValue(WindowsLaunchOnLogin.getLaunchOnLoginRegistryName()); 102 await WindowsLaunchOnLogin._removeLaunchOnLoginShortcuts(); 103 // Create registry key without using settings pane to check if 104 // this is reflected in the settings 105 let autostartPath = 106 WindowsLaunchOnLogin.quoteString( 107 Services.dirsvc.get("XREExeF", Ci.nsIFile).path 108 ) + " -os-autostart"; 109 wrk.writeStringValue( 110 WindowsLaunchOnLogin.getLaunchOnLoginRegistryName(), 111 autostartPath 112 ); 113 114 // Open preferences to general pane 115 await openPreferencesViaOpenPreferencesAPI("paneGeneral", { 116 leaveOpen: true, 117 }); 118 let doc = gBrowser.contentDocument; 119 120 let launchOnLoginCheckbox = doc.getElementById("windowsLaunchOnLogin"); 121 ok( 122 launchOnLoginCheckbox.checked, 123 "Autostart checkbox automatically checked" 124 ); 125 126 gBrowser.removeCurrentTab(); 127 }); 128 await doCleanup(); 129 }); 130 131 add_task(async function testRemoveLaunchOnLoginGuard() { 132 let registryName = WindowsLaunchOnLogin.getLaunchOnLoginRegistryName(); 133 // Simulate launch on login disabled from Windows settings 134 registry.setValue( 135 Ci.nsIWindowsRegKey.ROOT_KEY_CURRENT_USER, 136 "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\StartupApproved\\Run", 137 registryName, 138 0b1, 139 Ci.nsIWindowsRegKey.TYPE_BINARY 140 ); 141 // This function should now be non-op 142 WindowsLaunchOnLogin.removeLaunchOnLogin(); 143 await WindowsLaunchOnLogin.withLaunchOnLoginRegistryKey(async wrk => { 144 ok(wrk.hasValue(registryName), "Registry value is not deleted"); 145 }); 146 }); 147 148 add_task(async function delete_external_regkey() { 149 if (Services.sysinfo.getProperty("hasWinPackageId")) { 150 return; 151 } 152 await ExperimentAPI.ready(); 153 let doCleanup = await NimbusTestUtils.enrollWithFeatureConfig({ 154 featureId: "windowsLaunchOnLogin", 155 value: { enabled: true }, 156 }); 157 await WindowsLaunchOnLogin.withLaunchOnLoginRegistryKey(async wrk => { 158 // Delete registry key without using settings pane to check if 159 // this is reflected in the settings 160 wrk.removeValue(WindowsLaunchOnLogin.getLaunchOnLoginRegistryName()); 161 162 // Open preferences to general pane 163 await openPreferencesViaOpenPreferencesAPI("paneGeneral", { 164 leaveOpen: true, 165 }); 166 let doc = gBrowser.contentDocument; 167 168 let launchOnLoginCheckbox = doc.getElementById("windowsLaunchOnLogin"); 169 ok( 170 !launchOnLoginCheckbox.checked, 171 "Launch on login checkbox automatically unchecked" 172 ); 173 174 gBrowser.removeCurrentTab(); 175 }); 176 await doCleanup(); 177 }); 178 179 add_task(async function testDisablingLaunchOnLogin() { 180 Cc["@mozilla.org/toolkit/profile-service;1"].getService( 181 Ci.nsIToolkitProfileService 182 ).startWithLastProfile = false; 183 184 await openPreferencesViaOpenPreferencesAPI("paneGeneral", { 185 leaveOpen: true, 186 }); 187 let doc = gBrowser.contentDocument; 188 189 let launchOnLoginCheckbox = doc.getElementById("windowsLaunchOnLogin"); 190 ok(launchOnLoginCheckbox.disabled, "Autostart checkbox disabled"); 191 ok(!launchOnLoginCheckbox.checked, "Autostart checkbox unchecked"); 192 193 let launchOnLoginDisabledMessage = doc.getElementById( 194 "windowsLaunchOnLoginDisabledProfileBox" 195 ); 196 197 is( 198 launchOnLoginDisabledMessage.dataset.l10nId, 199 "startup-windows-launch-on-login-profile-disabled", 200 "Has proper fluent ID" 201 ); 202 203 ok(!launchOnLoginDisabledMessage.hidden, "Disabled message is displayed"); 204 205 gBrowser.removeCurrentTab(); 206 }); 207 208 registerCleanupFunction(async function () { 209 await WindowsLaunchOnLogin._removeLaunchOnLoginShortcuts(); 210 await WindowsLaunchOnLogin.withLaunchOnLoginRegistryKey(async wrk => { 211 let registryName = WindowsLaunchOnLogin.getLaunchOnLoginRegistryName(); 212 if (wrk.hasValue(registryName)) { 213 wrk.removeValue(registryName); 214 } 215 }); 216 profileService.startWithLastProfile = startWithLastProfileOriginal; 217 });