test_turn_on_scheduled_backups.html (11968B)
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <title>Tests for the turn-on-scheduled-backups component</title> 6 <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> 7 <script src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"></script> 8 <script type="application/javascript" src="head.js"></script> 9 <script 10 src="chrome://browser/content/backup/turn-on-scheduled-backups.mjs" 11 type="module" 12 ></script> 13 <link rel="localization" href="browser/backupSettings.ftl"/> 14 <link rel="localization" href="branding/brand.ftl"/> 15 <link rel="stylesheet" href="chrome://mochikit/content/tests/SimpleTest/test.css"/> 16 <script> 17 18 const { BrowserTestUtils } = ChromeUtils.importESModule( 19 "resource://testing-common/BrowserTestUtils.sys.mjs" 20 ); 21 22 /** 23 * Tests that adding a turn-on-scheduled-backups element to the DOM causes it to 24 * fire a BackupUI:InitWidget event. 25 */ 26 add_task(async function test_initWidget() { 27 let turnOnScheduledBackups = document.createElement("turn-on-scheduled-backups"); 28 let content = document.getElementById("content"); 29 30 let sawInitWidget = BrowserTestUtils.waitForEvent(content, "BackupUI:InitWidget"); 31 content.appendChild(turnOnScheduledBackups); 32 await sawInitWidget; 33 ok(true, "Saw BackupUI:InitWidget"); 34 35 turnOnScheduledBackups.remove(); 36 }); 37 38 /** 39 * Tests that the dialog displays a default save location for backups and updates to a custom one 40 * if there is one selected. 41 */ 42 add_task(async function test_locationInputs() { 43 let turnOnScheduledBackups = document.getElementById("test-turn-on-scheduled-backups"); 44 45 let inputDefault = turnOnScheduledBackups.filePathInputDefaultEl; 46 ok(inputDefault, "Default input should be found"); 47 48 let promise = BrowserTestUtils.waitForCondition(() => inputDefault.value); 49 50 /* Normally we would pass in the default attributes, but for this test, we will 51 * hardcode them since file paths vary across different platforms. 52 */ 53 const defaultPathFilename = "testdefaultpath"; 54 let defaultPath = PathUtils.join(PathUtils.tempDir, defaultPathFilename); 55 turnOnScheduledBackups.defaultPath = defaultPath; 56 turnOnScheduledBackups.defaultLabel = defaultPathFilename; 57 await turnOnScheduledBackups.updateComplete; 58 await promise; 59 60 is(inputDefault.value, `${defaultPathFilename} (recommended)`, "Default input should not be empty and should contain part of the default path"); 61 62 // Now pretend a custom file path was selected 63 const newPathFilename = "testnewpath"; 64 let newPath = PathUtils.join(PathUtils.tempDir, newPathFilename); 65 turnOnScheduledBackups._newPath = newPath; 66 turnOnScheduledBackups._newLabel = newPathFilename; 67 await turnOnScheduledBackups.updateComplete; 68 69 let inputCustom = turnOnScheduledBackups.filePathInputCustomEl; 70 ok(inputCustom, "Input should be updated"); 71 is(inputCustom.value, newPathFilename, "Input value should be set to the new path"); 72 }) 73 74 /** 75 * Tests that pressing the confirm button will dispatch the expected events. 76 */ 77 add_task(async function test_confirm() { 78 let turnOnScheduledBackups = document.getElementById("test-turn-on-scheduled-backups"); 79 let confirmButton = turnOnScheduledBackups.confirmButtonEl; 80 81 // Reset the component state 82 turnOnScheduledBackups.defaultPath = null; 83 turnOnScheduledBackups.defaultLabel = null; 84 turnOnScheduledBackups._newPath = null; 85 turnOnScheduledBackups._newPathLabel = null; 86 87 await turnOnScheduledBackups.updateComplete; 88 89 // The confirm button will be disabled if BackupService cannot find a file path 90 ok(confirmButton.disabled, "confirm button should be inactive if there is no default path"); 91 92 // Now we can set the default path 93 const defaultPathFilename = "testdefaultpath"; 94 let defaultPath = PathUtils.join(PathUtils.tempDir, defaultPathFilename); 95 turnOnScheduledBackups.defaultPath = defaultPath; 96 turnOnScheduledBackups.defaultLabel = defaultPathFilename; 97 98 await turnOnScheduledBackups.updateComplete; 99 100 ok(confirmButton, "Confirm button should be found"); 101 ok(!confirmButton.disabled, "confirm button should be active"); 102 103 let content = document.getElementById("content"); 104 let promise = BrowserTestUtils.waitForEvent(content, "BackupUI:EnableScheduledBackups"); 105 106 confirmButton.click() 107 108 await promise; 109 ok(true, "Detected event after selecting the confirm button"); 110 }) 111 112 /** 113 * Tests that setting a new path will enable the confirm button and dispatch the expected events. 114 */ 115 add_task(async function test_confirm_newpath() { 116 let turnOnScheduledBackups = document.getElementById("test-turn-on-scheduled-backups"); 117 let confirmButton = turnOnScheduledBackups.confirmButtonEl; 118 119 // The confirm button will be disabled if BackupService cannot find a file path 120 const defaultPathFilename = null; 121 let defaultPath = PathUtils.join(PathUtils.tempDir, defaultPathFilename); 122 turnOnScheduledBackups.defaultPath = defaultPath; 123 turnOnScheduledBackups.defaultLabel = defaultPathFilename; 124 125 await turnOnScheduledBackups.updateComplete; 126 127 ok(confirmButton.disabled, "confirm button should be inactive if there is no default path"); 128 129 // Set a new file path to enable the button 130 const newPathFilename = "testnewpath"; 131 let newPath = PathUtils.join(PathUtils.tempDir, newPathFilename); 132 turnOnScheduledBackups._newPath = newPath; 133 turnOnScheduledBackups._newPathLabel = newPathFilename; 134 135 await turnOnScheduledBackups.updateComplete; 136 137 ok(confirmButton, "Confirm button should be found"); 138 ok(!confirmButton.disabled, "confirm button should be active"); 139 140 let content = document.getElementById("content"); 141 let promise = BrowserTestUtils.waitForEvent(content, "BackupUI:EnableScheduledBackups"); 142 143 confirmButton.click() 144 145 await promise; 146 ok(true, "Detected event after selecting the confirm button"); 147 }) 148 149 /** 150 * Tests that setting a persisted path will enable the confirm button and dispatch the expected events. 151 */ 152 add_task(async function test_confirm_persisted_path() { 153 let turnOnScheduledBackups = document.getElementById("test-turn-on-scheduled-backups"); 154 let confirmButton = turnOnScheduledBackups.confirmButtonEl; 155 // For the purposes of this test, we are embedded 156 turnOnScheduledBackups.embeddedFxBackupOptIn = true; 157 158 // Reset the component state 159 turnOnScheduledBackups.defaultPath = ""; 160 turnOnScheduledBackups.defaultLabel = ""; 161 turnOnScheduledBackups._newPath = ""; 162 turnOnScheduledBackups._newPathLabel = ""; 163 164 turnOnScheduledBackups.backupServiceState = createBackupServiceState({ 165 embeddedComponentPersistentData: {}, 166 }); 167 168 await turnOnScheduledBackups.updateComplete; 169 170 ok(confirmButton.disabled, "confirm button should be inactive if there is no default path"); 171 172 // Set the persisted path to enable the button 173 const persistedPathFilename = "testpersistedpath"; 174 let persistedPath = PathUtils.join(PathUtils.tempDir, persistedPathFilename); 175 turnOnScheduledBackups.backupServiceState.embeddedComponentPersistentData = { 176 path: persistedPath 177 } 178 179 turnOnScheduledBackups.reset(); 180 await turnOnScheduledBackups.updateComplete; 181 182 ok(confirmButton, "Confirm button should be found"); 183 ok(!confirmButton.disabled, "confirm button should be active"); 184 185 let content = document.getElementById("content"); 186 let promise = BrowserTestUtils.waitForEvent(content, "BackupUI:EnableScheduledBackups"); 187 188 confirmButton.click() 189 190 await promise; 191 ok(true, "Detected event after selecting the confirm button"); 192 }) 193 194 /** 195 * Tests that pressing the cancel button will dispatch the expected events. 196 */ 197 add_task(async function test_cancel() { 198 let turnOnScheduledBackups = document.getElementById("test-turn-on-scheduled-backups"); 199 let cancelButton = turnOnScheduledBackups.cancelButtonEl; 200 201 ok(cancelButton, "Cancel button should be found"); 202 203 let content = document.getElementById("content"); 204 let promise = BrowserTestUtils.waitForEvent(content, "dialogCancel"); 205 206 cancelButton.click() 207 208 await promise; 209 ok(true, "Detected event after selecting the cancel button"); 210 }) 211 212 /** 213 * Tests that selecting the checkbox for enabling backup encryption will show more 214 * options to configure the password needed for encryption. 215 */ 216 add_task(async function test_expandedPasswords() { 217 let turnOnScheduledBackups = document.getElementById("test-turn-on-scheduled-backups"); 218 let passwordsCheckbox = turnOnScheduledBackups.passwordOptionsCheckboxEl; 219 220 ok(passwordsCheckbox, "Passwords checkbox should be found"); 221 ok(!turnOnScheduledBackups.passwordOptionsExpandedEl, "Passwords expanded options should not be found"); 222 223 passwordsCheckbox.click(); 224 await turnOnScheduledBackups.updateComplete; 225 226 ok(turnOnScheduledBackups.passwordOptionsExpandedEl, "Passwords expanded options should be found"); 227 228 // Click again to verify collapse and reset checkbox state for suceeding tests 229 passwordsCheckbox.click(); 230 await turnOnScheduledBackups.updateComplete; 231 ok(!turnOnScheduledBackups.passwordOptionsExpandedEl, "Passwords expanded options should be hidden again"); 232 }) 233 234 /** 235 * Tests that the Confirm button cannot be selected if password settings are shown and 236 * inputted passwords are invalid. Once passwords are valid, verifies that the Confirm 237 * button can be selected. 238 */ 239 add_task(async function test_passwordValidityConfirmButton() { 240 let turnOnScheduledBackups = document.getElementById("test-turn-on-scheduled-backups"); 241 let passwordsCheckbox = turnOnScheduledBackups.passwordOptionsCheckboxEl; 242 243 // First check that the confirm button is not disabled with password options collapsed 244 ok(passwordsCheckbox, "Passwords checkbox should be found"); 245 ok(!turnOnScheduledBackups.passwordOptionsExpandedEl, "Passwords expanded options should not be found"); 246 247 let confirmButton = turnOnScheduledBackups.confirmButtonEl; 248 ok(confirmButton, "Confirm button should be found"); 249 ok(!confirmButton.disabled, "Confirm button should not be disabled since there are no passwords expanded options"); 250 251 passwordsCheckbox.click(); 252 await turnOnScheduledBackups.updateComplete; 253 254 // Now check that the confirm button is disabled 255 ok(turnOnScheduledBackups.passwordOptionsExpandedEl, "Passwords expanded options should be found"); 256 ok(confirmButton.disabled, "Confirm button should now be disabled since there are passwords expanded options"); 257 258 // Pretend that the password inputs dispatch an event indicating that we have valid passwords 259 let passwordOptionsExpanded = turnOnScheduledBackups.passwordOptionsExpandedEl; 260 let validPromise = createMockValidityPassEventPromise(turnOnScheduledBackups, passwordOptionsExpanded, "ValidPasswordsDetected"); 261 await validPromise; 262 263 ok(!confirmButton.disabled, "Confirm button should no longer be disabled"); 264 265 // Now pretend that passwords are no longer valid 266 let invalidPromise = createMockValidityPassEventPromise(turnOnScheduledBackups, passwordOptionsExpanded, "InvalidPasswordsDetected"); 267 await invalidPromise; 268 269 ok(confirmButton.disabled, "Confirm button should be disabled again"); 270 }) 271 </script> 272 </head> 273 <body> 274 <p id="display"></p> 275 <div id="content" style="display: none"> 276 <turn-on-scheduled-backups id="test-turn-on-scheduled-backups"></turn-on-scheduled-backups> 277 </div> 278 <pre id="test"></pre> 279 </body> 280 </html>