summaryrefslogtreecommitdiffstats
path: root/editor/libeditor/tests/test_password_unmask_API.html
blob: 0fc7ef819459ad22f352119fc929a68439457214 (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
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
<!DOCTYPE HTML>
<html>
<head>
  <title>Test for unmasking password API</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<input type="text">
<input type="password">
<script class="testbody" type="application/javascript">
SimpleTest.waitForExplicitFinish();
SimpleTest.waitForFocus(() => {
  let input = document.getElementsByTagName("input")[0];
  let password = document.getElementsByTagName("input")[1];

  let editor, passwordEditor;
  function updateEditors() {
    editor = SpecialPowers.wrap(input).editor;
    passwordEditor = SpecialPowers.wrap(password).editor;
  }

  try {
    updateEditors();
    editor.mask();
    ok(false,
       `nsIEditor.mask() should throw exception when called for <input type="text"> before nsIEditor.unmask()`);
  } catch (e) {
    ok(true,
       `nsIEditor.mask() should throw exception when called for <input type="text"> before nsIEditor.unmask() ${e}`);
  }

  try {
    updateEditors();
    editor.unmask();
    ok(false,
       `nsIEditor.unmask() should throw exception when called for <input type="text">`);
  } catch (e) {
    ok(true,
       `nsIEditor.unmask() should throw exception when called for <input type="text"> ${e}`);
  }

  try {
    updateEditors();
    editor.unmask(0);
    ok(false,
       `nsIEditor.unmask(0) should throw exception when called for <input type="text">`);
  } catch (e) {
    ok(true,
       `nsIEditor.unmask(0) should throw exception when called for <input type="text"> ${e}`);
  }

  input.value = "abcdef";
  try {
    updateEditors();
    editor.unmask();
    ok(false,
       `nsIEditor.unmask() should throw exception when called for <input type="text" value="abcdef">`);
  } catch (e) {
    ok(true,
       `nsIEditor.unmask() should throw exception when called for <input type="text" value="abcdef"> ${e}`);
  }

  try {
    updateEditors();
    editor.mask();
    ok(false,
       `nsIEditor.mask() should throw exception when called for <input type="text" value="abcdef"> after nsIEditor.unmask()`);
  } catch (e) {
    ok(true,
       `nsIEditor.mask() should throw exception when called for <input type="text" value="abcdef"> after nsIEditor.unmask() ${e}`);
  }

  try {
    updateEditors();
    passwordEditor.mask();
    ok(true,
       `nsIEditor.mask() shouldn't throw exception when called for <input type="password"> before nsIEditor.unmask()`);
    ok(passwordEditor.autoMaskingEnabled,
       `nsIEditor.autoMaskingEnabled should be true after nsIEditor.mask() for <input type="password"> before nsIEditor.unmask()`);
  } catch (e) {
    ok(false,
       `nsIEditor.mask() shouldn't throw exception when called for <input type="password"> before nsIEditor.unmask() ${e}`);
  }

  try {
    updateEditors();
    editor.unmask(5);
    ok(false,
       `nsIEditor.unmask(5) should throw exception when called for <input type="password" value="">`);
  } catch (e) {
    ok(true,
       `nsIEditor.unmask(5) should throw exception when called for <input type="password" value=""> ${e}`);
    ok(passwordEditor.autoMaskingEnabled,
       `nsIEditor.autoMaskingEnabled should keep true (<input type="password">)`);
  }

  try {
    updateEditors();
    passwordEditor.unmask();
    ok(true,
       `nsIEditor.unmask() shouldn't throw exception when called for <input type="password">`);
    ok(!passwordEditor.autoMaskingEnabled,
       `nsIEditor.autoMaskingEnabled should be false after nsIEditor.unmask() for <input type="password">)`);
    is(passwordEditor.unmaskedStart, 0,
       `nsIEditor.unmaskedStart should be 0 after nsIEditor.unmask() for <input type="password">`);
    is(passwordEditor.unmaskedEnd, 0,
       `nsIEditor.unmaskedEnd should be 0 after nsIEditor.unmask() for <input type="password">`);
  } catch (e) {
    ok(false,
       `nsIEditor.unmask() shouldn't throw exception when called for <input type="password"> ${e}`);
  }

  password.value = "abcdef";
  try {
    updateEditors();
    passwordEditor.unmask();
    ok(true,
       `nsIEditor.unmask() shouldn't throw exception when called for <input type="password" value="abcdef">)`);
    ok(!passwordEditor.autoMaskingEnabled,
       `nsIEditor.autoMaskingEnabled should be false after nsIEditor.unmask() for <input type="password" value="abcdef">`);
    is(passwordEditor.unmaskedStart, 0,
       `nsIEditor.unmaskedStart should be 0 after nsIEditor.unmask() for <input type="password" value="abcdef">`);
    is(passwordEditor.unmaskedEnd, 6,
       `nsIEditor.unmaskedEnd should be 0 after nsIEditor.unmask() for <input type="password" value="abcdef">`);
  } catch (e) {
    ok(false,
       `nsIEditor.unmask() shouldn't throw exception when called for <input type="password" value="abcdef"> ${e}`);
  }

  try {
    updateEditors();
    passwordEditor.mask();
    ok(true,
       `nsIEditor.mask() shouldn't throw exception when called for <input type="password" value="abcdef">`);
    ok(passwordEditor.autoMaskingEnabled,
       `nsIEditor.autoMaskingEnabled should be true after nsIEditor.mask() for <input type="password" value="abcdef">`);
  } catch (e) {
    ok(false,
       `nsIEditor.mask() shouldn't throw exception when called for <input type="password" value="abcdef"> ${e}`);
  }

  try {
    updateEditors();
    passwordEditor.unmask(0, 100, 1000);
    ok(true,
       `nsIEditor.unmask(0, 100, 1000) shouldn't throw exception when called for <input type="password" value="abcdef">`);
    ok(passwordEditor.autoMaskingEnabled,
       `nsIEditor.autoMaskingEnabled should be true after nsIEditor.unmask(0, 100, 1000) for <input type="password" value="abcdef">`);
    is(passwordEditor.unmaskedStart, 0,
       `nsIEditor.unmaskedStart should be 0 after nsIEditor.unmask(0, 100, 1000) for <input type="password" value="abcdef">`);
    is(passwordEditor.unmaskedEnd, 6,
       `nsIEditor.unmaskedEnd should be 6 after nsIEditor.unmask(0, 100, 1000) for <input type="password" value="abcdef">`);
  } catch (e) {
    ok(false,
       `nsIEditor.unmask(0, 100, 1000) shouldn't throw exception when called for <input type="password" value="abcdef"> ${e}`);
  }

  try {
    updateEditors();
    passwordEditor.unmask(3);
    ok(true,
       `nsIEditor.unmask(3) shouldn't throw exception when called for <input type="password" value="abcdef">`);
    ok(!passwordEditor.autoMaskingEnabled,
       `nsIEditor.autoMaskingEnabled should be false after nsIEditor.unmask(3) for <input type="password" value="abcdef">`);
    is(passwordEditor.unmaskedStart, 3,
       `nsIEditor.unmaskedStart should be 3 after nsIEditor.unmask(3) for <input type="password" value="abcdef">`);
    is(passwordEditor.unmaskedEnd, 6,
       `nsIEditor.unmaskedEnd should be 6 after nsIEditor.unmask(3) for <input type="password" value="abcdef">`);
  } catch (e) {
    ok(false,
       `nsIEditor.unmask(3) shouldn't throw exception when called for <input type="password" value="abcdef"> ${e}`);
  }

  try {
    updateEditors();
    passwordEditor.unmask(0);
    password.style.fontSize = "32px"; // reframe the `<input>` element
    password.getBoundingClientRect(); // flush pending reflow if there is
    // Then, new `TextEditor` should keep unmasked range.
    passwordEditor = SpecialPowers.wrap(password).editor;
    ok(!passwordEditor.autoMaskingEnabled,
      `nsIEditor.autoMaskingEnabled should be false after the password field reframed`);
    is(passwordEditor.unmaskedStart, 0,
       `nsIEditor.unmaskedStart should be 0 after the password field reframed`);
    is(passwordEditor.unmaskedEnd, 6,
       `nsIEditor.unmaskedEnd should be 6 after the password field reframed`);
  } catch (e) {
    ok(false, `Shouldn't throw exception while testing unmasked range after reframing ${e}`);
  } finally {
    password.style.fontSize = "";
    password.getBoundingClientRect();
  }

  try {
    updateEditors();
    passwordEditor.unmask(0);
    password.style.display = "none"; // Hide the password field temporarily
    password.getBoundingClientRect();
    password.style.display = "block"; // And show it again
    password.getBoundingClientRect();
    updateEditors();
    // Then, new `TextEditor` should keep unmasked range.
    ok(!passwordEditor.autoMaskingEnabled,
      `nsIEditor.autoMaskingEnabled should be false after the password field was temporarily hidden`);
    is(passwordEditor.unmaskedStart, 0,
       `nsIEditor.unmaskedStart should be 0 after the password field was temporarily hidden`);
    is(passwordEditor.unmaskedEnd, 6,
       `nsIEditor.unmaskedEnd should be 6 after the password field was temporarily hidden`);
  } catch (e) {
    ok(false, `Shouldn't throw exception while testing unmasked range after temporarily hiding the password field ${e}`);
  } finally {
    password.style.display = "";
    password.getBoundingClientRect();
    passwordEditor = SpecialPowers.wrap(password).editor;
  }

  try {
    updateEditors();
    passwordEditor.unmask(0);
    password.style.display = "none"; // Hide the password field temporarily
    password.getBoundingClientRect();
    password.value = "ghijkl"; // And modify the value
    password.style.display = "block"; // And show it again
    password.getBoundingClientRect();
    // Then, new `TextEditor` shouldn't keep unmasked range due to the value change.
    updateEditors();
    ok(passwordEditor.autoMaskingEnabled,
      `nsIEditor.autoMaskingEnabled should be true after the password field was temporarily hidden and changed its value`);
    is(passwordEditor.unmaskedStart, 0,
       `nsIEditor.unmaskedStart should be 0 after the password field was temporarily hidden and changed its value`);
    is(passwordEditor.unmaskedEnd, 0,
       `nsIEditor.unmaskedEnd should be 0  after the password field was temporarily hidden and changed its value`);
  } catch (e) {
    ok(false, `Shouldn't throw exception while testing unmasked range after temporarily hiding the password field and changing the value ${e}`);
  } finally {
    password.style.display = "";
    password.getBoundingClientRect();
    password.value = "abcdef";
    passwordEditor = SpecialPowers.wrap(password).editor;
  }

  try {
    updateEditors();
    passwordEditor.unmask(0);
    password.style.display = "none"; // Hide the password field temporarily
    password.getBoundingClientRect();
    password.value = "abcdef"; // And overwrite the value with same value
    password.style.display = "block"; // And show it again
    password.getBoundingClientRect();
    // Then, new `TextEditor` shouldn't keep unmasked range due to setting the value.
    updateEditors();
    ok(passwordEditor.autoMaskingEnabled,
      `nsIEditor.autoMaskingEnabled should be true after the password field was temporarily hidden and changed its value`);
    is(passwordEditor.unmaskedStart, 0,
       `nsIEditor.unmaskedStart should be 0 after the password field was temporarily hidden and changed its value`);
    is(passwordEditor.unmaskedEnd, 0,
       `nsIEditor.unmaskedEnd should be 0  after the password field was temporarily hidden and changed its value`);
  } catch (e) {
    ok(false, `Shouldn't throw exception while testing unmasked range after temporarily hiding the password field and changing the value ${e}`);
  } finally {
    password.style.display = "";
    password.getBoundingClientRect();
    password.value = "abcdef";
    passwordEditor = SpecialPowers.wrap(password).editor;
  }

  try {
    updateEditors();
    passwordEditor.unmask(0, 6, 10000);
    password.style.display = "none"; // Hide the password field temporarily
    password.getBoundingClientRect();
    password.style.display = "block"; // And show it again
    password.getBoundingClientRect();
    updateEditors();
    // Then, new `TextEditor` should mask all characters since nobody can mask it with the timer.
    ok(passwordEditor.autoMaskingEnabled,
      `nsIEditor.autoMaskingEnabled should be true after the password field was temporarily hidden (if auto-masking timer was set)`);
    is(passwordEditor.unmaskedStart, 0,
       `nsIEditor.unmaskedStart should be 0 after the password field was temporarily hidden (if auto-masking timer was set)`);
    is(passwordEditor.unmaskedEnd, 0,
       `nsIEditor.unmaskedEnd should be 0 after the password field was temporarily hidden (if auto-masking timer was set)`);
  } catch (e) {
    ok(false, `Shouldn't throw exception while testing unmasked range after temporarily hiding the password field whose auto-masking timer was set ${e}`);
  } finally {
    password.style.display = "";
    password.getBoundingClientRect();
    passwordEditor = SpecialPowers.wrap(password).editor;
  }

  try {
    updateEditors();
    passwordEditor.unmask(0);
    password.type = "text";
    password.getBoundingClientRect();
    password.type = "password";
    password.getBoundingClientRect();
    updateEditors();
    // Then, new `TextEditor` should mask all characters after `type` attribute was changed.
    ok(passwordEditor.autoMaskingEnabled,
      `nsIEditor.autoMaskingEnabled should be true after "type" attribute of the password field was changed`);
    is(passwordEditor.unmaskedStart, 0,
       `nsIEditor.unmaskedStart should be 0 after "type" attribute of the password field was changed`);
    is(passwordEditor.unmaskedEnd, 0,
       `nsIEditor.unmaskedEnd should be 0 after "type" attribute of the password field was changed`);
  } catch (e) {
    ok(false, `Shouldn't throw exception while testing unmasked range after "type" attribute of the password field was changed ${e}`);
  } finally {
    password.type = "password";
    password.getBoundingClientRect();
    passwordEditor = SpecialPowers.wrap(password).editor;
  }

  SimpleTest.finish();
});
</script>
</body>
</html>