summaryrefslogtreecommitdiffstats
path: root/toolkit/components/passwordmgr/test/mochitest/test_autocomplete_https_upgrade.html
blob: 7d57253225f53f5429584a49af4501d16f27f56e (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
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
<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Test autocomplete on an HTTPS page using upgraded HTTP logins</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");

  // We have two actual HTTPS to avoid autofill before the schemeUpgrades pref flips to true.
  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");

  // Same as above but HTTP instead of HTTPS (to test de-duping)
  let login2 = new nsLoginInfo("http://example.org", "http://example.org", null,
                               "name1", "passHTTP", "uname", "pword");

  // Different HTTP login to upgrade with secure formSubmitURL
  let login3 = new nsLoginInfo("http://example.org", "https://example.org", null,
                               "name2", "passHTTPtoHTTPS", "uname", "pword");

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

<!-- we presumably can't hide the content for this test. -->
<div id="content">
  <iframe src="https://example.org/tests/toolkit/components/passwordmgr/test/mochitest/form_basic.html"></iframe>
</div>

<pre id="test">
<script class="testbody" type="text/javascript">
const shiftModifier = SpecialPowers.Ci.nsIDOMEvent.SHIFT_MASK;

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

// Restore the form to the default state.
function restoreForm() {
  pword.focus();
  uname.value = "";
  pword.value = "";
  uname.focus();
}

// Check for expected username/password in form.
function checkACForm(expectedUsername, expectedPassword) {
  let formID = uname.parentNode.id;
  is(uname.value, expectedUsername, "Checking " + formID + " username");
  is(pword.value, expectedPassword, "Checking " + formID + " password");
}

add_task(function* setup() {
  yield SpecialPowers.pushPrefEnv({"set": [["signon.schemeUpgrades", true]]});

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

  iframeDoc = iframe.contentDocument;
  uname = iframeDoc.getElementById("form-basic-username");
  pword = iframeDoc.getElementById("form-basic-password");
});

add_task(function* test_empty_first_entry() {
  // Make sure initial form is empty.
  checkACForm("", "");
  // Trigger autocomplete popup
  restoreForm();
  let popupState = yield getPopupState();
  is(popupState.open, false, "Check popup is initially closed");
  let shownPromise = promiseACShown();
  doKey("down");
  let results = yield shownPromise;
  popupState = yield getPopupState();
  is(popupState.selectedIndex, -1, "Check no entries are selected");
  checkArrayValues(results, ["name", "name1", "name2"], "initial");

  // Check first entry
  let index0Promise = notifySelectedIndex(0);
  doKey("down");
  yield index0Promise;
  checkACForm("", ""); // value shouldn't update
  doKey("return"); // not "enter"!
  yield promiseFormsProcessed();
  checkACForm("name", "pass");
});

add_task(function* test_empty_second_entry() {
  restoreForm();
  let shownPromise = promiseACShown();
  doKey("down"); // open
  yield shownPromise;
  doKey("down"); // first
  doKey("down"); // second
  doKey("return"); // not "enter"!
  yield promiseFormsProcessed();
  checkACForm("name1", "pass1");
});

add_task(function* test_search() {
  restoreForm();
  let shownPromise = promiseACShown();
  // We need to blur for the autocomplete controller to notice the forced value below.
  uname.blur();
  uname.value = "name";
  uname.focus();
  sendChar("1");
  doKey("down"); // open
  let results = yield shownPromise;
  checkArrayValues(results, ["name1"], "check result deduping for 'name1'");
  doKey("down"); // first
  doKey("return"); // not "enter"!
  yield promiseFormsProcessed();
  checkACForm("name1", "pass1");

  let popupState = yield getPopupState();
  is(popupState.open, false, "Check popup is now closed");
});

add_task(function* test_delete_first_entry() {
  restoreForm();
  uname.focus();
  let shownPromise = promiseACShown();
  doKey("down");
  yield shownPromise;

  let index0Promise = notifySelectedIndex(0);
  doKey("down");
  yield index0Promise;

  let deletionPromise = promiseStorageChanged(["removeLogin"]);
  // 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.
  doKey("delete", shiftModifier);
  yield deletionPromise;
  checkACForm("", "");

  let results = yield notifyMenuChanged(2, "name1");

  checkArrayValues(results, ["name1", "name2"], "two should remain after deleting the first");
  let popupState = yield getPopupState();
  is(popupState.open, true, "Check popup stays open after deleting");
  doKey("escape");
  popupState = yield getPopupState();
  is(popupState.open, false, "Check popup closed upon ESC");
});

add_task(function* test_delete_duplicate_entry() {
  restoreForm();
  uname.focus();
  let shownPromise = promiseACShown();
  doKey("down");
  yield shownPromise;

  let index0Promise = notifySelectedIndex(0);
  doKey("down");
  yield index0Promise;

  let deletionPromise = promiseStorageChanged(["removeLogin"]);
  // 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.
  doKey("delete", shiftModifier);
  yield deletionPromise;
  checkACForm("", "");

  is(LoginManager.countLogins("http://example.org", "http://example.org", null), 1,
     "Check that the HTTP login remains");
  is(LoginManager.countLogins("https://example.org", "https://example.org", null), 0,
     "Check that the HTTPS login was deleted");

  // Two menu items should remain as the HTTPS login should have been deleted but
  // the HTTP would remain.
  let results = yield notifyMenuChanged(1, "name2");

  checkArrayValues(results, ["name2"], "one should remain after deleting the HTTPS name1");
  let popupState = yield getPopupState();
  is(popupState.open, true, "Check popup stays open after deleting");
  doKey("escape");
  popupState = yield getPopupState();
  is(popupState.open, false, "Check popup closed upon ESC");
});

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