browser_settings_create_backup.js (7512B)
1 /* Any copyright is dedicated to the Public Domain. 2 https://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 let TEST_PROFILE_PATH; 7 const SCHEDULED_BACKUPS_ENABLED_PREF = "browser.backup.scheduled.enabled"; 8 const BACKUP_DEFAULT_LOCATION_PREF = "browser.backup.location"; 9 10 /** 11 * This test covers testing the "Backup now" button and it's states 12 * based on if a backup is in progress or not 13 */ 14 add_setup(async () => { 15 TEST_PROFILE_PATH = await IOUtils.createUniqueDirectory( 16 PathUtils.tempDir, 17 "testBackup" 18 ); 19 await SpecialPowers.pushPrefEnv({ 20 set: [ 21 [BACKUP_DEFAULT_LOCATION_PREF, ""], 22 [SCHEDULED_BACKUPS_ENABLED_PREF, false], 23 ], 24 }); 25 26 registerCleanupFunction(async () => { 27 // we'll make sure to clean this whole dir up after the test 28 await IOUtils.remove(TEST_PROFILE_PATH, { recursive: true }); 29 }); 30 }); 31 32 /** 33 * Tests the case where there is no DEFAULT_PARENT_DIR_PATH 34 */ 35 add_task(async function test_no_default_folder() { 36 const sandbox = sinon.createSandbox(); 37 38 let bs = getAndMaybeInitBackupService(); 39 bs.resetDefaultParentInternalState(); 40 41 let docStub = sandbox 42 .stub(BackupService, "docsDirFolderPath") 43 .get() 44 .returns(null); 45 46 await BrowserTestUtils.withNewTab("about:preferences#sync", async browser => { 47 let settings = browser.contentDocument.querySelector("backup-settings"); 48 let turnOnButton = settings.scheduledBackupsButtonEl; 49 50 await settings.updateComplete; 51 52 Assert.ok(bs.archiveEnabledStatus, "Archive is enabled for backups"); 53 54 Assert.ok( 55 turnOnButton, 56 "Button to turn on scheduled backups should be found" 57 ); 58 59 turnOnButton.click(); 60 61 await settings.updateComplete; 62 63 let turnOnScheduledBackups = settings.turnOnScheduledBackupsEl; 64 65 Assert.ok( 66 turnOnScheduledBackups, 67 "turn-on-scheduled-backups should be found" 68 ); 69 70 let filePathInputDefault = turnOnScheduledBackups.filePathInputDefaultEl; 71 72 Assert.equal( 73 filePathInputDefault.value, 74 "", 75 "Default input displays the expected text" 76 ); 77 78 let dialog = settings.turnOnScheduledBackupsDialogEl; 79 let dialogClosePromise = BrowserTestUtils.waitForEvent(dialog, "close"); 80 dialog.close(); 81 82 await dialogClosePromise; 83 }); 84 85 docStub.restore(); 86 87 await BrowserTestUtils.withNewTab("about:preferences#sync", async browser => { 88 let settings = browser.contentDocument.querySelector("backup-settings"); 89 90 Assert.ok( 91 settings.turnOnScheduledBackupsEl, 92 "turn-on-scheduled-backups should be found" 93 ); 94 settings.scheduledBackupsButtonEl.click(); 95 96 const documentsPath = BackupService.DEFAULT_PARENT_DIR_PATH; 97 98 Assert.equal( 99 settings.turnOnScheduledBackupsEl.filePathInputDefaultEl.value, 100 `${PathUtils.filename(documentsPath)} (recommended)`, 101 "Default input displays the expected text" 102 ); 103 }); 104 105 sandbox.restore(); 106 await SpecialPowers.popPrefEnv(); 107 }); 108 109 /** 110 * Test creating a new backup using the "Backup now" button 111 */ 112 add_task(async function test_create_new_backup_trigger() { 113 await SpecialPowers.pushPrefEnv({ 114 set: [ 115 [BACKUP_DEFAULT_LOCATION_PREF, TEST_PROFILE_PATH], 116 [SCHEDULED_BACKUPS_ENABLED_PREF, true], 117 ], 118 }); 119 await BrowserTestUtils.withNewTab("about:preferences#sync", async browser => { 120 Services.fog.testResetFOG(); 121 122 let settings = browser.contentDocument.querySelector("backup-settings"); 123 // disable the buffer for the test 124 settings.MESSAGE_BAR_BUFFER = 0; 125 126 let bs = getAndMaybeInitBackupService(); 127 128 Assert.ok(!bs.state.backupInProgress, "There is no backup in progress"); 129 130 Assert.ok( 131 !settings.triggerBackupButtonEl.disabled, 132 "No backup in progress and backup's enabled should mean that we can Backup Now" 133 ); 134 135 let stateUpdated = BrowserTestUtils.waitForEvent( 136 bs, 137 "BackupService:StateUpdate", 138 false, 139 () => { 140 return bs.state.backupInProgress; 141 } 142 ); 143 144 // ensure that the in progress message bar is not visible 145 Assert.ok( 146 !settings.backupInProgressMessageBarEl, 147 "A backup is not in progress, the message bar should not be visible" 148 ); 149 150 // click on button 151 settings.triggerBackupButtonEl.click(); 152 153 await stateUpdated; 154 155 // make sure the button changes to disabled 156 await settings.updateComplete; 157 158 Assert.ok( 159 settings.triggerBackupButtonEl.disabled, 160 "A backup is in progress, the trigger button should be disabled" 161 ); 162 163 // ensure that the in progress message bar is visible 164 Assert.ok( 165 settings.backupInProgressMessageBarEl, 166 "A backup is in progress, the message bar should be visible" 167 ); 168 169 await BrowserTestUtils.waitForEvent( 170 bs, 171 "BackupService:StateUpdate", 172 false, 173 () => { 174 return !bs.state.backupInProgress; 175 } 176 ); 177 178 await settings.updateComplete; 179 180 // make sure that the backup created is a valid backup 181 Assert.ok( 182 !settings.backupServiceState.backupInProgress, 183 "A backup is complete" 184 ); 185 186 let fileName = JSON.parse( 187 settings.lastBackupFileNameEl.getAttribute("data-l10n-args") 188 ).fileName; 189 190 // the file should show once it's created 191 Assert.ok(fileName, "the archive was created"); 192 193 await BrowserTestUtils.waitForCondition( 194 () => !settings.backupInProgressMessageBarEl, 195 "A backup is no longer in progress, the message bar should disappear" 196 ); 197 198 let gleanEvents = Glean.browserBackup.backupStart.testGetValue(); 199 Assert.equal(gleanEvents.length, 1, "backup_start event was recorded"); 200 Assert.equal( 201 gleanEvents[0].extra.reason, 202 "manual", 203 "correct reason for the backup was recorded" 204 ); 205 }); 206 }); 207 208 /** 209 * Tests if the "Backup now" button is disabled if a backup is already underway 210 */ 211 add_task(async function test_create_backup_trigger_disabled() { 212 let bs = getAndMaybeInitBackupService(); 213 214 const sandbox = sinon.createSandbox(); 215 216 // We'll unblock the backup when content notifies this topic 217 const UNBLOCK_TOPIC = "backup-test-allow-create"; 218 219 // Keep the original impl so the stub can delegate once unblocked 220 const origResolveDest = bs.resolveArchiveDestFolderPath.bind(bs); 221 222 sandbox 223 .stub(bs, "resolveArchiveDestFolderPath") 224 .callsFake(async (...args) => { 225 // Wait until the prefs page tells us it's ready 226 await TestUtils.topicObserved(UNBLOCK_TOPIC); 227 return origResolveDest(...args); 228 }); 229 230 // Kick off a backup *before* opening the page. This is simulating 231 // the backup background task 232 let backupPromise = bs.createBackup(); 233 234 await BrowserTestUtils.withNewTab("about:preferences#sync", async browser => { 235 let settings = browser.contentDocument.querySelector("backup-settings"); 236 Assert.ok( 237 settings.triggerBackupButtonEl.disabled, 238 "A backup is in progress" 239 ); 240 241 let stateUpdated = BrowserTestUtils.waitForEvent( 242 bs, 243 "BackupService:StateUpdate", 244 false, 245 () => { 246 return !bs.state.backupInProgress; 247 } 248 ); 249 250 // Unblock the stub so backup proceeds 251 Services.obs.notifyObservers(null, UNBLOCK_TOPIC); 252 let result = await backupPromise; 253 254 ok(result, `Backup completed and returned result ${result.archivePath}`); 255 await stateUpdated; 256 await settings.updateComplete; 257 258 Assert.ok( 259 !settings.backupServiceState.backupInProgress, 260 "No backup in progress, we can trigger a new one" 261 ); 262 }); 263 264 sandbox.restore(); 265 });