summaryrefslogtreecommitdiffstats
path: root/toolkit/components/passwordmgr/test/mochitest/test_autofocus_js.html
blob: 2ce3293dda1fb2a03942eb2b8f9ba4f928e89b01 (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
107
108
109
110
111
112
113
114
115
<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Test login autocomplete is activated when focused by js on load</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
  <script type="text/javascript" src="/tests/SimpleTest/SpawnTask.js"></script>
  <script type="text/javascript" src="satchel_common.js"></script>
  <script type="text/javascript" src="pwmgr_common.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<script>
const chromeScript = runChecksAfterCommonInit(false);

runInParent(function addLogins() {
  const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
  Cu.import("resource://gre/modules/Services.jsm");

  // Create some logins just for this form, since we'll be deleting them.
  let nsLoginInfo = Components.Constructor("@mozilla.org/login-manager/loginInfo;1",
                                           Ci.nsILoginInfo, "init");

  let login0 = new nsLoginInfo("https://example.org", "https://example.org", null,
                               "name", "pass", "uname", "pword");

  let login1 = new nsLoginInfo("https://example.org", "https://example.org", null,
                               "name1", "pass1", "uname", "pword");

  try {
    Services.logins.addLogin(login0);
    Services.logins.addLogin(login1);
  } catch (e) {
    assert.ok(false, "addLogin threw: " + e);
  }
});
</script>
<p id="display"></p>

<div id="content">
  <iframe src="https://example.org/tests/toolkit/components/passwordmgr/test/mochitest/form_autofocus_js.html"></iframe>
</div>

<pre id="test">
<script class="testbody" type="text/javascript">

let iframe = SpecialPowers.wrap(document.getElementsByTagName("iframe")[0]);
let iframeDoc;

add_task(function* setup() {
  yield new Promise(resolve => {
    iframe.addEventListener("load", function onLoad() {
      iframe.removeEventListener("load", onLoad);
      resolve();
    });
  });

  iframeDoc = iframe.contentDocument;

  SimpleTest.requestFlakyTimeout("Giving a chance for the unexpected popupshown to occur");
});

add_task(function* test_initial_focus() {
  let results = yield notifyMenuChanged(2, "name");
  checkArrayValues(results, ["name", "name1"], "Two results");
  doKey("down");
  doKey("return");
  yield promiseFormsProcessed();
  is(iframeDoc.getElementById("form-basic-password").value, "pass", "Check first password filled");
  let popupState = yield getPopupState();
  is(popupState.open, false, "Check popup is now closed");
});

// This depends on the filling from the previous test.
add_task(function* test_not_reopened_if_filled() {
  listenForUnexpectedPopupShown();
  let usernameField = iframeDoc.getElementById("form-basic-username");
  usernameField.focus();
  info("Waiting to see if a popupshown occurs");
  yield new Promise(resolve => setTimeout(resolve, 1000));

  // cleanup
  gPopupShownExpected = true;
  iframeDoc.getElementById("form-basic-submit").focus();
});

add_task(function* test_reopened_after_edit_not_matching_saved() {
  let usernameField = iframeDoc.getElementById("form-basic-username");
  usernameField.value = "nam";
  let shownPromise = promiseACShown();
  usernameField.focus();
  yield shownPromise;
  iframeDoc.getElementById("form-basic-submit").focus();
});

add_task(function* test_not_reopened_after_selecting() {
  let formFillController = SpecialPowers.Cc["@mozilla.org/satchel/form-fill-controller;1"].
                           getService(SpecialPowers.Ci.nsIFormFillController);
  let usernameField = iframeDoc.getElementById("form-basic-username");
  usernameField.value = "";
  iframeDoc.getElementById("form-basic-password").value = "";
  listenForUnexpectedPopupShown();
  formFillController.markAsLoginManagerField(usernameField);
  info("Waiting to see if a popupshown occurs");
  yield new Promise(resolve => setTimeout(resolve, 1000));

  // Cleanup
  gPopupShownExpected = true;
});

</script>
</pre>
</body>
</html>