test_copy_profile.js (4021B)
1 /* Any copyright is dedicated to the Public Domain. 2 https://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 const { sinon } = ChromeUtils.importESModule( 7 "resource://testing-common/Sinon.sys.mjs" 8 ); 9 const { BackupService } = ChromeUtils.importESModule( 10 "resource:///modules/backup/BackupService.sys.mjs" 11 ); 12 13 const execProcess = sinon.fake(); 14 15 add_setup(async () => { 16 await initSelectableProfileService(); 17 18 sinon.replace(getSelectableProfileService(), "execProcess", execProcess); 19 }); 20 21 add_task(async function test_copy_profile() { 22 startProfileService(); 23 24 const SelectableProfileService = getSelectableProfileService(); 25 const ProfilesDatastoreService = getProfilesDatastoreService(); 26 27 await ProfilesDatastoreService.init(); 28 await SelectableProfileService.init(); 29 Assert.ok(SelectableProfileService.isEnabled, "Service should be enabled"); 30 31 let profiles = await SelectableProfileService.getAllProfiles(); 32 Assert.equal(profiles.length, 1, "Only one selectable profile exists"); 33 34 // Simulate creating a desktop shortcut. 35 Services.prefs.setCharPref( 36 "browser.profiles.shortcutFileName", 37 "test-shortcut-name" 38 ); 39 40 const backupServiceInstance = new BackupService(); 41 42 let encState = await backupServiceInstance.loadEncryptionState( 43 SelectableProfileService.currentProfile.path 44 ); 45 Assert.ok(!encState, "No encryption state before copyProfile called"); 46 47 let copiedProfile = 48 await SelectableProfileService.currentProfile.copyProfile(); 49 50 encState = await backupServiceInstance.loadEncryptionState( 51 SelectableProfileService.currentProfile.path 52 ); 53 Assert.ok(!encState, "No encryption state after copyProfile called"); 54 55 profiles = await SelectableProfileService.getAllProfiles(); 56 Assert.equal(profiles.length, 2, "Two selectable profiles exist"); 57 58 Assert.equal( 59 copiedProfile.avatar, 60 SelectableProfileService.currentProfile.avatar, 61 "Copied profile has the same avatar" 62 ); 63 64 Assert.equal( 65 copiedProfile.theme.themeId, 66 SelectableProfileService.currentProfile.theme.themeId, 67 "Copied profile has the same theme" 68 ); 69 70 let prefsPath = PathUtils.join(copiedProfile.path, "prefs.js"); 71 let prefsFile = await IOUtils.readUTF8(prefsPath, { encoding: "utf-8" }); 72 Assert.equal( 73 -1, 74 prefsFile.search("browser.profiles.shortcutFileName"), 75 "Copied profile should not have desktop shortcut pref" 76 ); 77 }); 78 79 add_task(async function test_copy_profile_with_encryption() { 80 startProfileService(); 81 82 const SelectableProfileService = getSelectableProfileService(); 83 const ProfilesDatastoreService = getProfilesDatastoreService(); 84 85 await ProfilesDatastoreService.init(); 86 await SelectableProfileService.init(); 87 Assert.ok(SelectableProfileService.isEnabled, "Service should be enabled"); 88 89 let profiles = await SelectableProfileService.getAllProfiles(); 90 Assert.equal(profiles.length, 2, "Only two selectable profiles exist"); 91 92 const backupServiceInstance = new BackupService(); 93 await backupServiceInstance.enableEncryption( 94 "testCopyProfile", 95 SelectableProfileService.currentProfile.path.path 96 ); 97 98 let encState = await backupServiceInstance.loadEncryptionState( 99 SelectableProfileService.currentProfile.path 100 ); 101 Assert.ok(encState, "Encryption state exists before copyProfile called"); 102 103 let copiedProfile = 104 await SelectableProfileService.currentProfile.copyProfile(); 105 106 encState = await backupServiceInstance.loadEncryptionState( 107 SelectableProfileService.currentProfile.path 108 ); 109 Assert.ok(encState, "Encryption state exists after copyProfile called"); 110 111 profiles = await SelectableProfileService.getAllProfiles(); 112 Assert.equal(profiles.length, 3, "Three selectable profiles exist"); 113 114 Assert.equal( 115 copiedProfile.avatar, 116 SelectableProfileService.currentProfile.avatar, 117 "Copied profile has the same avatar" 118 ); 119 120 Assert.equal( 121 copiedProfile.theme.themeId, 122 SelectableProfileService.currentProfile.theme.themeId, 123 "Copied profile has the same theme" 124 ); 125 });