test_IPPOnboardingMessageHelper.js (2877B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 "use strict"; 6 7 const { IPPOnboardingMessage } = ChromeUtils.importESModule( 8 "moz-src:///browser/components/ipprotection/IPPOnboardingMessageHelper.sys.mjs" 9 ); 10 const { ONBOARDING_PREF_FLAGS } = ChromeUtils.importESModule( 11 "chrome://browser/content/ipprotection/ipprotection-constants.mjs" 12 ); 13 const AUTOSTART_PREF = "browser.ipProtection.autoStartEnabled"; 14 const PERM_NAME = "ipp-vpn"; 15 16 add_setup(async function () { 17 await putServerInRemoteSettings(); 18 }); 19 20 /** 21 * Tests that onboarding message flags are set for VPN start, autostart, and site exceptions 22 */ 23 add_task(async function test_IPPOnboardingMessage() { 24 let sandbox = sinon.createSandbox(); 25 setupStubs(sandbox); 26 27 IPProtectionService.init(); 28 29 await waitForEvent( 30 IPProtectionService, 31 "IPProtectionService:StateChanged", 32 () => IPProtectionService.state === IPProtectionStates.READY 33 ); 34 35 Assert.ok( 36 !IPPProxyManager.activatedAt, 37 "IP Protection service should not be active initially" 38 ); 39 40 let startedEventPromise = waitForEvent( 41 IPPProxyManager, 42 "IPPProxyManager:StateChanged", 43 () => IPPProxyManager.state === IPPProxyStates.ACTIVE 44 ); 45 46 IPPProxyManager.start(); 47 48 Assert.equal( 49 IPPProxyManager.state, 50 IPPProxyStates.ACTIVATING, 51 "Proxy activation" 52 ); 53 54 await startedEventPromise; 55 info("after startedEventPromise"); 56 Assert.equal( 57 IPPProxyManager.state, 58 IPPProxyStates.ACTIVE, 59 "IP Protection service should be active after starting" 60 ); 61 62 let maskAfterVpn = IPPOnboardingMessage.readPrefMask(); 63 Assert.notStrictEqual( 64 maskAfterVpn & ONBOARDING_PREF_FLAGS.EVER_TURNED_ON_VPN, 65 0, 66 "Ever turned on VPN flag should be set" 67 ); 68 69 // Turn on autostart 70 Services.prefs.setBoolPref(AUTOSTART_PREF, true); 71 let maskAfterAutostart = IPPOnboardingMessage.readPrefMask(); 72 Assert.strictEqual( 73 maskAfterAutostart, 74 maskAfterVpn | ONBOARDING_PREF_FLAGS.EVER_TURNED_ON_AUTOSTART, 75 "Autostart flag should be added to the mask" 76 ); 77 78 // Add site exception 79 const site = "https://www.example.com"; 80 const principal = 81 Services.scriptSecurityManager.createContentPrincipalFromOrigin(site); 82 const capability = Ci.nsIPermissionManager.DENY_ACTION; 83 Services.perms.addFromPrincipal(principal, PERM_NAME, capability); 84 85 let maskAfterSiteException = IPPOnboardingMessage.readPrefMask(); 86 Assert.strictEqual( 87 maskAfterSiteException, 88 maskAfterAutostart | ONBOARDING_PREF_FLAGS.EVER_USED_SITE_EXCEPTIONS, 89 "Site exceptions flag should be added to the mask" 90 ); 91 92 // Cleanup 93 Services.perms.removeByType(PERM_NAME); 94 IPProtectionService.uninit(); 95 IPPOnboardingMessage.uninit(); 96 sandbox.restore(); 97 });