1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
|
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Test autofill and autocomplete on autocomplete=new-password fields</title>
<!-- This test assumes that telemetry events are not cleared after the `setup` task. -->
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<script src="/tests/SimpleTest/EventUtils.js"></script>
<script src="pwmgr_common.js"></script>
<script src="../../../satchel/test/satchel_common.js"></script>
<link rel="stylesheet" href="/tests/SimpleTest/test.css" />
</head>
<body>
Login Manager test: autofill with autocomplete=new-password fields
<script>
function initLogins() {
const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
Services.logins.removeAllUserFacingLogins();
const {LoginManagerParent} = ChromeUtils.import("resource://gre/modules/LoginManagerParent.jsm");
if (LoginManagerParent.getGeneratedPasswordsByPrincipalOrigin()) {
LoginManagerParent.getGeneratedPasswordsByPrincipalOrigin().clear();
}
}
runInParent(initLogins);
let origin = window.location.origin;
addLoginsInParent([origin, "https://autofill", null, "user1", "pass1"]);
let readyPromise = registerRunTests(3);
</script>
<p id="display"></p>
<!-- we presumably can't hide the content for this test. -->
<div id="content">
<!-- form1 is the reference, sanity-check -->
<form id="form1" action="https://autofill" onsubmit="return false;">
<input type="text" name="uname">
<input type="password" name="p">
<button type="submit">Submit</button>
</form>
<!-- form2 uses a new-password type=password field -->
<form id="form2" action="https://autofill" onsubmit="return false;">
<input type="text" name="uname">
<input type="password" name="password" autocomplete="new-password">
<button type="submit">Submit</button>
</form>
<!-- form3 has enough signal to make new-password heuristics (Bug 1595244) detect
a new password field, even though there is no autocomplete="new-password"
attribute on the <input type="password"> element-->
<form id="form3" action="https://autofill" onsubmit="return false;">
<input type="text" name="username">
<label>
New password
<input type="password" name="password">
</label>
<button type="submit">Submit</button>
</form>
</div>
<pre id="test">
<script class="testbody" type="text/javascript">
const {ContentTaskUtils} =
SpecialPowers.Cu.import("resource://testing-common/ContentTaskUtils.jsm", {});
const { TestUtils } = SpecialPowers.Cu.import("resource://testing-common/TestUtils.jsm");
let dateAndTimeFormatter = new SpecialPowers.Services.intl.DateTimeFormat(undefined, {
dateStyle: "medium",
});
const TelemetryFilterPropsUsed = Object.freeze({
category: "pwmgr",
method: "autocomplete_field",
object: "generatedpassword",
});
const TelemetryFilterPropsShown = Object.freeze({
category: "pwmgr",
method: "autocomplete_shown",
object: "generatedpassword",
});
async function waitForTelemetryEventsCondition(cond, options = {},
errorMsg = "waitForTelemetryEventsCondition timed out", maxTries = 200) {
return TestUtils.waitForCondition(async () => {
let events = await getTelemetryEvents(options);
let result;
try {
result = cond(events);
info("waitForTelemetryEventsCondition, result: " + result);
} catch (e) {
info("waitForTelemetryEventsCondition caught exception, got events: " + JSON.stringify(events));
ok(false, `${e}\n${e.stack}`);
}
return result ? events : null;
}, errorMsg, maxTries);
}
async function promiseACPopupClosed() {
return SimpleTest.promiseWaitForCondition(async () => {
let popupState = await getPopupState();
return !popupState.open;
}, "Wait for AC popup to be closed");
}
async function showACPopup(formNumber, expectedACLabels) {
let shownPromise = promiseACShown();
synthesizeKey("KEY_ArrowDown");
let results = await shownPromise;
checkAutoCompleteResults(results, expectedACLabels,
window.location.host, "Check all rows are correct");
}
async function checkTelemetryEventsPWGenShown(expectedPWGenTelemetryEvents) {
info(`showed generated password option, check there are now ${expectedPWGenTelemetryEvents} generatedpassword telemetry events`);
await waitForTelemetryEventsCondition(events => {
return events.length == expectedPWGenTelemetryEvents;
}, { process: "parent", filterProps: TelemetryFilterPropsShown }, `Wait for there to be ${expectedPWGenTelemetryEvents} shown telemetry events`);
}
async function checkTelemetryEventsPWGenUsed(expectedPWGenTelemetryEvents) {
info("filled generated password again, ensure we don't record another generatedpassword autocomplete telemetry event");
let telemetryEvents;
try {
telemetryEvents = await waitForTelemetryEventsCondition(events => events.length == expectedPWGenTelemetryEvents + 1,
{ process: "parent", filterProps: TelemetryFilterPropsUsed },
`Wait for there to be ${expectedPWGenTelemetryEvents + 1} used events`, 50);
} catch (ex) {}
ok(!telemetryEvents, `Expected to timeout waiting for there to be ${expectedPWGenTelemetryEvents + 1} events`);
}
add_task(async function setup() {
await SpecialPowers.pushPrefEnv({"set": [
["signon.generation.available", false],
["signon.generation.enabled", false],
]});
ok(readyPromise, "check promise is available");
await readyPromise;
let useEvents = await getTelemetryEvents({ process: "parent", filterProps: TelemetryFilterPropsUsed, clear: true });
is(useEvents.length, 0, "Expect 0 use events");
let showEvents = await getTelemetryEvents({ process: "parent", filterProps: TelemetryFilterPropsShown, clear: true });
is(showEvents.length, 0, "Expect 0 show events");
let acEvents = await getTelemetryEvents({ process: "parent", filterProps: TelemetryFilterPropsAC, clear: true });
is(acEvents.length, 0, "Expect 0 autocomplete events");
// This can be removed once Bug 1623431 is resolved.
document.getElementById("form3").reset();
});
add_task(async function test_autofillAutocompleteUsername_noGeneration() {
// reference form was filled as expected?
checkForm(1, "user1", "pass1");
// 2nd form should not be filled
checkForm(2, "", "");
$_(2, "uname").focus();
await showACPopup(2, ["user1"]);
let acEvents = await waitForTelemetryEventsCondition(events => {
return events.length == 1;
}, { process: "parent", filterProps: TelemetryFilterPropsAC, clear: true }, `Wait for there to be 1 autocomplete telemetry event`);
checkACTelemetryEvent(acEvents[0], $_(2, "uname"), {
"hadPrevious": "0",
"login": "1",
"loginsFooter": "1"
});
synthesizeKey("KEY_ArrowDown");
synthesizeKey("KEY_Enter");
await promiseFormsProcessedInSameProcess();
checkForm(2, "user1", "pass1");
recreateTree(document.getElementById("form2"));
});
add_task(async function test_autofillAutocompletePassword_noGeneration() {
// 2nd form should not be filled
checkForm(2, "", "");
let pword = $_(2, "password");
pword.focus();
await showACPopup(2, ["user1"]);
let acEvents = await waitForTelemetryEventsCondition(events => {
return events.length == 1;
}, { process: "parent", filterProps: TelemetryFilterPropsAC, clear: true }, `Wait for there to be 1 autocomplete telemetry event`);
checkACTelemetryEvent(acEvents[0], pword, {
"hadPrevious": "0",
"login": "1",
"loginsFooter": "1"
});
synthesizeKey("KEY_ArrowDown");
synthesizeKey("KEY_Enter");
// Can't use promiseFormsProcessedInSameProcess() when autocomplete fills the field directly.
await SimpleTest.promiseWaitForCondition(() => pword.value == "pass1", "Check pw filled");
checkForm(2, "", "pass1");
// No autocomplete results should appear for non-empty pw fields.
synthesizeKey("KEY_ArrowDown");
await promiseNoUnexpectedPopupShown();
recreateTree(document.getElementById("form2"));
});
// All tests below this are with generation prefs enabled.
add_task(async function test_autofillAutocompleteUsername_noGeneration2() {
await SpecialPowers.pushPrefEnv({"set": [
["signon.generation.available", true],
["signon.generation.enabled", true],
["signon.showAutoCompleteOrigins", true],
]});
// 2nd form should not be filled
checkForm(2, "", "");
$_(2, "uname").focus();
// No generation option on username fields.
await showACPopup(2, ["user1"]);
let acEvents = await waitForTelemetryEventsCondition(events => {
return events.length == 1;
}, { process: "parent", filterProps: TelemetryFilterPropsAC, clear: true }, `Wait for there to be 1 autocomplete telemetry event`);
checkACTelemetryEvent(acEvents[0], $_(2, "uname"), {
"hadPrevious": "0",
"login": "1",
"loginsFooter": "1"
});
synthesizeKey("KEY_ArrowDown");
synthesizeKey("KEY_Enter");
await promiseFormsProcessedInSameProcess();
checkForm(2, "user1", "pass1");
recreateTree(document.getElementById("form2"));
});
add_task(async function test_autofillAutocompletePassword_withGeneration() {
const formNumbersToTest = [2, 3];
// Bug 1616356 and Bug 1548878: Recorded once per origin
let expectedPWGenTelemetryEvents = 0;
// Bug 1619498: Recorded once every time the autocomplete popup is shown
let expectedACShownTelemetryEvents = 0;
for (let formNumber of formNumbersToTest) {
// This form should not be filled
checkForm(formNumber, "", "");
let pword = $_(formNumber, "password");
pword.focus();
await showACPopup(formNumber, [
"user1",
"Use a Securely Generated Password",
]);
expectedPWGenTelemetryEvents++;
expectedACShownTelemetryEvents++;
await checkTelemetryEventsPWGenShown(expectedPWGenTelemetryEvents);
let acEvents = await waitForTelemetryEventsCondition(events => {
return events.length == expectedACShownTelemetryEvents;
}, { process: "parent", filterProps: TelemetryFilterPropsAC }, `Wait for there to be ${expectedACShownTelemetryEvents} autocomplete telemetry event(s)`);
checkACTelemetryEvent(acEvents[expectedACShownTelemetryEvents - 1], pword, {
"generatedPasswo": "1",
"hadPrevious": "0",
"login": "1",
"loginsFooter": "1"
});
synthesizeKey("KEY_ArrowDown");
synthesizeKey("KEY_Enter");
// Can't use promiseFormsProcessedInSameProcess() when autocomplete fills the field directly.
await SimpleTest.promiseWaitForCondition(() => pword.value == "pass1", "Check pw filled");
checkForm(formNumber, "", "pass1");
// No autocomplete results should appear for non-empty pw fields.
synthesizeKey("KEY_ArrowDown");
await promiseNoUnexpectedPopupShown();
info("Removing all logins to test auto-saving of generated passwords");
await LoginManager.removeAllUserFacingLogins();
while (pword.value) {
synthesizeKey("KEY_Backspace");
}
LOGIN_FIELD_UTILS.checkPasswordMasked(pword, true, "Blanked field");
info("This time select the generated password");
await showACPopup(formNumber, [
"Use a Securely Generated Password",
]);
expectedACShownTelemetryEvents++;
await checkTelemetryEventsPWGenShown(expectedPWGenTelemetryEvents);
acEvents = await waitForTelemetryEventsCondition(events => {
return events.length == expectedACShownTelemetryEvents;
}, { process: "parent", filterProps: TelemetryFilterPropsAC }, `Wait for there to be ${expectedACShownTelemetryEvents} autocomplete telemetry event(s)`);
checkACTelemetryEvent(acEvents[expectedACShownTelemetryEvents - 1], pword, {
"generatedPasswo": "1",
"hadPrevious": "0",
"loginsFooter": "1"
});
synthesizeKey("KEY_ArrowDown");
let storageAddPromise = promiseStorageChanged(["addLogin"]);
LOGIN_FIELD_UTILS.checkPasswordMasked(pword, true, "Before first fill of generated pw");
synthesizeKey("KEY_Enter");
info("waiting for the password field to be filled with the generated password");
await SimpleTest.promiseWaitForCondition(() => !!pword.value, "Check generated pw filled");
LOGIN_FIELD_UTILS.checkPasswordMasked(pword, false, "After first fill of generated pw");
info("Wait for generated password to be added to storage");
await storageAddPromise;
let logins = await LoginManager.getAllLogins();
let timePasswordChanged = logins[logins.length - 1].timePasswordChanged;
let time = dateAndTimeFormatter.format(new Date(timePasswordChanged));
const LABEL_NO_USERNAME = "No username (" + time + ")";
let generatedPW = pword.value;
is(generatedPW.length, GENERATED_PASSWORD_LENGTH, "Check generated password length");
ok(generatedPW.match(GENERATED_PASSWORD_REGEX), "Check generated password format");
LOGIN_FIELD_UTILS.checkPasswordMasked(pword, false, "After fill");
info("Check field is masked upon blurring");
synthesizeKey("KEY_Tab"); // blur
LOGIN_FIELD_UTILS.checkPasswordMasked(pword, true, "After blur");
synthesizeKey("KEY_Tab", { shiftKey: true }); // focus again
LOGIN_FIELD_UTILS.checkPasswordMasked(pword, false, "After shift-tab to focus again");
// Remove selection for OS where the whole value is selected upon focus.
synthesizeKey("KEY_ArrowRight");
while (pword.value) {
LOGIN_FIELD_UTILS.checkPasswordMasked(pword, false, pword.value);
synthesizeKey("KEY_Backspace");
}
LOGIN_FIELD_UTILS.checkPasswordMasked(pword, true, "Blanked field");
info("Blur the empty field to trigger a 'change' event");
synthesizeKey("KEY_Tab"); // blur
LOGIN_FIELD_UTILS.checkPasswordMasked(pword, true, "Blur after blanking");
synthesizeKey("KEY_Tab", { shiftKey: true }); // focus again
LOGIN_FIELD_UTILS.checkPasswordMasked(pword, true, "Focus again after blanking");
info("Type a single character after blanking");
synthesizeKey("@");
info("Blur the single-character field to trigger a 'change' event");
synthesizeKey("KEY_Tab"); // blur
LOGIN_FIELD_UTILS.checkPasswordMasked(pword, true, "Blur after backspacing");
synthesizeKey("KEY_Tab", { shiftKey: true }); // focus again
LOGIN_FIELD_UTILS.checkPasswordMasked(pword, true, "Focus again after backspacing");
synthesizeKey("KEY_Backspace"); // Blank the field again
await showACPopup(formNumber, [
LABEL_NO_USERNAME,
"Use a Securely Generated Password",
]);
expectedACShownTelemetryEvents++;
synthesizeKey("KEY_ArrowDown");
synthesizeKey("KEY_ArrowDown");
synthesizeKey("KEY_Enter");
await SimpleTest.promiseWaitForCondition(() => !!pword.value, "Check generated pw filled");
// Same generated password should be used, even despite the 'change' to @ earlier.
checkForm(formNumber, "", generatedPW);
LOGIN_FIELD_UTILS.checkPasswordMasked(pword, false, "Second fill of the generated pw");
await checkTelemetryEventsPWGenUsed(expectedPWGenTelemetryEvents);
logins = await LoginManager.getAllLogins();
is(logins.length, 1, "Still 1 login after filling the generated password a 2nd time");
is(logins[0].timePasswordChanged, timePasswordChanged, "Saved login wasn't changed");
is(logins[0].password, generatedPW, "Password is the same");
info("filling the saved login to ensure the field is masked again");
while (pword.value) {
LOGIN_FIELD_UTILS.checkPasswordMasked(pword, false, pword.value);
synthesizeKey("KEY_Backspace");
}
LOGIN_FIELD_UTILS.checkPasswordMasked(pword, true, "Blanked field again");
info("Blur the field to trigger a 'change' event again");
synthesizeKey("KEY_Tab"); // blur
LOGIN_FIELD_UTILS.checkPasswordMasked(pword, true, "Blur after blanking again");
synthesizeKey("KEY_Tab", { shiftKey: true }); // focus again
LOGIN_FIELD_UTILS.checkPasswordMasked(pword, true, "Focus again after blanking again");
// Remove selection for OS where the whole value is selected upon focus.
synthesizeKey("KEY_ArrowRight");
await showACPopup(formNumber, [
LABEL_NO_USERNAME,
"Use a Securely Generated Password",
]);
expectedACShownTelemetryEvents++;
synthesizeKey("KEY_ArrowDown");
synthesizeKey("KEY_Enter");
await SimpleTest.promiseWaitForCondition(() => !!pword.value, "Check saved generated pw filled");
// Same generated password should be used but from storage
checkForm(formNumber, "", generatedPW);
// Passwords from storage should always be masked.
LOGIN_FIELD_UTILS.checkPasswordMasked(pword, true, "after fill from storage");
synthesizeKey("KEY_Tab"); // blur
LOGIN_FIELD_UTILS.checkPasswordMasked(pword, true, "after blur");
synthesizeKey("KEY_Tab", { shiftKey: true }); // focus
LOGIN_FIELD_UTILS.checkPasswordMasked(pword, true, "after shift-tab to focus again");
info("reset initial login state with login1");
runInParent(initLogins);
addLoginsInParent([origin, "https://autofill", null, "user1", "pass1"]);
info("invalidate the autocomplete cache after updating storage above");
synthesizeKey("KEY_Backspace");
recreateTree(document.getElementById(`form${formNumber}`));
}
});
add_task(async function test_autofillAutocompletePassword_saveLoginDisabled() {
// form should not be filled
checkForm(2, "", "");
let formOrigin = new URL(document.documentURI).origin;
is(formOrigin, origin, "Expected form origin");
await LoginManager.setLoginSavingEnabled(origin, false);
let pword = $_(2, "password");
pword.focus();
// when login-saving is disabled for an origin, we expect no generated password row here
await showACPopup(2, ["user1"]);
// close any open menu
synthesizeKey("KEY_Escape");
await promiseACPopupClosed();
recreateTree(document.getElementById("form2"));
await LoginManager.setLoginSavingEnabled(origin, true);
});
add_task(async function test_deleteAndReselectGeneratedPassword() {
info("Removing all logins to test auto-saving of generated passwords");
await LoginManager.removeAllUserFacingLogins();
// form should not be filled
checkForm(2, "", "");
let pword = $_(2, "password");
let uname = $_(2, "uname");
async function showAndSelectACPopupItem(index) {
pword.focus();
if (pword.value) {
pword.select();
synthesizeKey("KEY_Backspace");
}
shownPromise = promiseACShown();
synthesizeKey("KEY_ArrowDown");
let results = await shownPromise;
if (index < 0) {
index = results.length + index;
}
for (let i=0; i<=index; i++) {
synthesizeKey("KEY_ArrowDown");
}
await TestUtils.waitForTick();
return results[index];
}
let storagePromise, shownPromise, menuLabel, itemIndex, savedLogins;
// fill the password field with the generated password via auto-complete menu
storagePromise = promiseStorageChanged(["addLogin"]);
// select last-but-2 item - the one before the footer
menuLabel = await showAndSelectACPopupItem(-2);
is(menuLabel, "Use a Securely Generated Password", "Check item label");
synthesizeKey("KEY_Enter");
info("waiting for the password field to be filled with the generated password");
await SimpleTest.promiseWaitForCondition(() => !!pword.value, "Check generated pw filled");
info("Wait for generated password to be added to storage");
await storagePromise;
uname.focus();
await TestUtils.waitForTick();
is(pword.value.length, LoginTestUtils.generation.LENGTH, "Check password looks generated");
const GENERATED_PASSWORD = pword.value;
savedLogins = await LoginManager.getAllLogins();
is(savedLogins.length, 1, "Check saved logins count");
info("clear the password field and delete the saved login using the AC menu")
storagePromise = promiseStorageChanged(["removeLogin"]);
itemIndex = 0;
menuLabel = await showAndSelectACPopupItem(itemIndex);
ok(menuLabel.includes("No username"), "Check first item is the auto-saved login");
// Send delete to remove the auto-saved login from storage
// On OS X, shift-backspace and shift-delete work, just delete does not.
// On Win/Linux, shift-backspace does not work, delete and shift-delete do.
synthesizeKey("KEY_Delete", {shiftKey: true});
await storagePromise;
uname.focus();
await TestUtils.waitForTick();
savedLogins = await LoginManager.getAllLogins();
is(savedLogins.length, 0, "Check saved logins count");
info("Re-fill with the generated password");
// select last-but-2 item - the one before the footer
menuLabel = await showAndSelectACPopupItem(-2);
is(menuLabel, "Use a Securely Generated Password", "Check item label");
synthesizeKey("KEY_Enter");
info("waiting for the password field to be filled with the generated password");
await SimpleTest.promiseWaitForCondition(() => !!pword.value, "Check generated pw filled");
uname.focus();
await TestUtils.waitForTick();
is(pword.value, GENERATED_PASSWORD, "Generated password has not changed");
recreateTree(document.getElementById("form2"));
});
add_task(async function test_passwordGenerationShownTelemetry() {
// Should only be recorded once per principal origin per session, but the cache is cleared each time ``initLogins`` is called.
await waitForTelemetryEventsCondition(events => {
return events.length == 3;
}, { process: "parent", filterProps: TelemetryFilterPropsShown }, "Expect 3 shown telemetry events");
});
</script>
</pre>
</body>
</html>
|