summaryrefslogtreecommitdiffstats
path: root/toolkit/components/passwordmgr/test/mochitest/test_autocomplete_basic_form_related_realms.html
blob: 988cd05954bfd59a2b2dc0b21546af80ad70e806 (plain)
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
<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Test login autocomplete with related realms</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <script src="/tests/SimpleTest/EventUtils.js"></script>
  <script type="text/javascript" src="../../../satchel/test/satchel_common.js"></script>
  <script type="text/javascript" src="pwmgr_common.js"></script>
  <link rel="stylesheet" href="/tests/SimpleTest/test.css"/>
</head>
<body>
Login Manager test: related realms autocomplete
<p id="display"></p>
<div id="content"></div>
<pre id="test">
<script class="testbody" type="text/javascript">
/** Test for Login Manager: related realms autocomplete. **/

function sendFakeAutocompleteEvent(element) {
  var acEvent = document.createEvent("HTMLEvents");
  acEvent.initEvent("DOMAutoComplete", true, false);
  element.dispatchEvent(acEvent);
}

async function promiseACPopupClosed() {
  return SimpleTest.promiseWaitForCondition(async () => {
    const popupState = await getPopupState();
    return !popupState.open;
  }, "Wait for AC popup to be closed");
}

add_setup(async () => {
  await setStoredLoginsAsync(
    // Simple related domain relationship where example.com and other-example.com are in the related domains list
    ["https://other-example.com", "https://other-example.com", null, "relatedUser1", "relatedPass1", "uname", "pword"],

    // Example.com and example.co.uk are related, so sub.example.co.uk should appear on example.com's autocomplete dropdown
    // The intent is to cover the ebay.com/ebay.co.uk and all other country TLD cases
    // where the sign in page is actually signin.ebay.com/signin.ebay.co.uk but credentials could have manually been entered
    // for ebay.com/ebay.co.uk or automatically stored as signin.ebay.com/sigin.ebay.co.uk
    ["https://sub.example.co.uk", "https://sub.example.co.uk", null, "subUser1", "subPass1", "uname", "pword"],
  );
  listenForUnexpectedPopupShown();
});

add_task(async function test_form1_initial_empty() {
  const form = createLoginForm();
  await promiseFormsProcessedInSameProcess();

  // Make sure initial form is empty.
  checkLoginForm(form.uname, "", form.pword, "");
  const popupState = await getPopupState();
  is(popupState.open, false, "Check popup is initially closed");
});

add_task(async function test_form_related_domain_menuitems() {
  const form = createLoginForm();
  await promiseFormsProcessedInSameProcess();

  form.uname.focus();

  const autocompleteItems = await popupByArrowDown();
  const popupState = await getPopupState();

  is(popupState.selectedIndex, -1, "Check no entires are selected upon opening");

  const expectedMenuItems = ["relatedUser1", "subUser1"];
  checkAutoCompleteResults(autocompleteItems, expectedMenuItems, window.location.host, "Check all menuitems are displayed correctly");

  const acEvents = await getTelemetryEvents({ process: "parent", filterProps: TelemetryFilterPropsAC, clear: true });
  is(acEvents.length, 1, "One autocomplete event");
  checkACTelemetryEvent(acEvents[0], form.uname, {
    "hadPrevious": "0",
    "login": expectedMenuItems.length + "",
    "loginsFooter": "1"
  });
  checkLoginForm(form.uname, "", form.pword, ""); // value shouldn't update just by opening

  synthesizeKey("KEY_ArrowDown"); // first item
  checkLoginForm(form.uname, "", form.pword, ""); // value shouldn't update just by selecting

  synthesizeKey("KEY_Enter");
  await promiseFormsProcessedInSameProcess();
  is(form.pword.value, "relatedPass1", "password should match the login that was selected");
  checkLoginForm(form.uname, "relatedUser1", form.pword, "relatedPass1");

  form.uname.value = "";
  form.pword.value = "";
  form.uname.focus();

  await popupByArrowDown();

  synthesizeKey("KEY_ArrowDown"); // first item
  synthesizeKey("KEY_ArrowDown"); // second item
  checkLoginForm(form.uname, "", form.pword, ""); // value shouldn't update just by selecting

  synthesizeKey("KEY_Enter");
  await promiseFormsProcessedInSameProcess();
  is(form.pword.value, "subPass1", "password should match the login that was selected");
  checkLoginForm(form.uname, "subUser1", form.pword, "subPass1");
});
</script>
</pre>
</body>
</html>