test_cut_copy_password.html (4009B)
1 <!doctype html> 2 <html> 3 <head> 4 <title>Test for cut/copy in password field</title> 5 <script src="/tests/SimpleTest/SimpleTest.js"></script> 6 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> 7 </head> 8 <body> 9 <input type="password"> 10 <script> 11 SimpleTest.waitForExplicitFinish(); 12 SimpleTest.waitForFocus(async () => { 13 let input = document.getElementsByTagName("input")[0]; 14 let editor = SpecialPowers.wrap(input).editor; 15 const kMask = editor.passwordMask; 16 async function copyToClipboard(aExpectedValue) { 17 try { 18 await SimpleTest.promiseClipboardChange( 19 aExpectedValue, () => { SpecialPowers.doCommand(window, "cmd_copy"); }, 20 undefined, undefined, aExpectedValue === null); 21 } catch (e) { 22 console.error(e); 23 } 24 } 25 async function cutToClipboard(aExpectedValue) { 26 try { 27 await SimpleTest.promiseClipboardChange( 28 aExpectedValue, () => { SpecialPowers.doCommand(window, "cmd_cut"); }, 29 undefined, undefined, aExpectedValue === null); 30 } catch (e) { 31 console.error(e); 32 } 33 } 34 input.value = "abcdef"; 35 input.focus(); 36 37 input.setSelectionRange(0, 6); 38 ok(true, "Trying to copy masked password..."); 39 await copyToClipboard(null); 40 isnot(SpecialPowers.getClipboardData("text/plain"), "abcdef", 41 "Copying masked password shouldn't copy raw value into the clipboard"); 42 isnot(SpecialPowers.getClipboardData("text/plain"), `${kMask}${kMask}${kMask}${kMask}${kMask}${kMask}`, 43 "Copying masked password shouldn't copy masked value into the clipboard"); 44 ok(true, "Trying to cut masked password..."); 45 await cutToClipboard(null); 46 isnot(SpecialPowers.getClipboardData("text/plain"), "abcdef", 47 "Cutting masked password shouldn't copy raw value into the clipboard"); 48 isnot(SpecialPowers.getClipboardData("text/plain"), `${kMask}${kMask}${kMask}${kMask}${kMask}${kMask}`, 49 "Cutting masked password shouldn't copy masked value into the clipboard"); 50 is(input.value, "abcdef", 51 "Cutting masked password shouldn't modify the value"); 52 53 editor.unmask(2, 4); 54 input.setSelectionRange(0, 6); 55 ok(true, "Trying to copy partially masked password..."); 56 await copyToClipboard(null); 57 isnot(SpecialPowers.getClipboardData("text/plain"), "abcdef", 58 "Copying partially masked password shouldn't copy raw value into the clipboard"); 59 isnot(SpecialPowers.getClipboardData("text/plain"), `${kMask}${kMask}cd${kMask}${kMask}`, 60 "Copying partially masked password shouldn't copy partially masked value into the clipboard"); 61 isnot(SpecialPowers.getClipboardData("text/plain"), `${kMask}${kMask}${kMask}${kMask}${kMask}${kMask}`, 62 "Copying partially masked password shouldn't copy masked value into the clipboard"); 63 ok(true, "Trying to cut partially masked password..."); 64 await cutToClipboard(null); 65 isnot(SpecialPowers.getClipboardData("text/plain"), "abcdef", 66 "Cutting partially masked password shouldn't copy raw value into the clipboard"); 67 isnot(SpecialPowers.getClipboardData("text/plain"), `${kMask}${kMask}cd${kMask}${kMask}`, 68 "Cutting partially masked password shouldn't copy partially masked value into the clipboard"); 69 isnot(SpecialPowers.getClipboardData("text/plain"), `${kMask}${kMask}${kMask}${kMask}${kMask}${kMask}`, 70 "Cutting partially masked password shouldn't copy masked value into the clipboard"); 71 is(input.value, "abcdef", 72 "Cutting partially masked password shouldn't modify the value"); 73 74 input.setSelectionRange(2, 4); 75 ok(true, "Trying to copy unmasked password..."); 76 await copyToClipboard("cd"); 77 is(input.value, "abcdef", 78 "Copying unmasked password shouldn't modify the value"); 79 80 input.value = "012345"; 81 editor.unmask(2, 4); 82 input.setSelectionRange(2, 4); 83 ok(true, "Trying to cut unmasked password..."); 84 await cutToClipboard("23"); 85 is(input.value, "0145", 86 "Cutting unmasked password should modify the value"); 87 88 SimpleTest.finish(); 89 }); 90 </script> 91 </body> 92 </html>