test_async_clipboard.xhtml (3376B)
1 <?xml version="1.0"?> 2 <?xml-stylesheet href="chrome://global/skin" type="text/css"?> 3 <?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css" type="text/css"?> 4 5 <window title="Async clipboard APIs Test" 6 xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" 7 onload="runTest();"> 8 9 <script type="application/javascript" 10 src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/> 11 <script type="application/javascript" 12 src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"/> 13 14 <script class="testbody" type="application/javascript"> 15 <![CDATA[ 16 17 SimpleTest.waitForExplicitFinish(); 18 19 const Services = SpecialPowers.Services; 20 21 const kTextPlainMimeType = "text/plain"; 22 23 function clearClipboard() { 24 Services.clipboard.emptyClipboard(Services.clipboard.kGlobalClipboard); 25 } 26 27 async function testRead() { 28 let expected = "x"; 29 await SimpleTest.promiseClipboardChange(expected, () => { 30 SpecialPowers.clipboardCopyString(expected); 31 }, kTextPlainMimeType); 32 let items = await navigator.clipboard.read(); 33 is(items.length, 1, "read() read exactly one item"); 34 const actual = await items[0].getType(kTextPlainMimeType).then(blob => blob.text()); 35 is(actual, expected, "read() read the right thing"); 36 } 37 38 async function testWrite() { 39 await SimpleTest.promiseClipboardChange("", () => { 40 clearClipboard(); 41 }); 42 43 let expected = "x"; 44 // eslint-disable-next-line no-undef 45 let item = new ClipboardItem({[kTextPlainMimeType]: expected}); 46 await navigator.clipboard.write([item]); 47 let actual = SpecialPowers.getClipboardData(kTextPlainMimeType); 48 is(actual, expected, "write() wrote the right thing"); 49 } 50 51 async function testReadText() { 52 let expected = "x"; 53 await SimpleTest.promiseClipboardChange(expected, () => { 54 SpecialPowers.clipboardCopyString(expected); 55 }, kTextPlainMimeType); 56 let actual = await navigator.clipboard.readText(); 57 is(actual, expected, "readText() read the right thing"); 58 } 59 60 async function testWriteText() { 61 await SimpleTest.promiseClipboardChange("", () => { 62 clearClipboard(); 63 }); 64 65 let expected = "x"; 66 await navigator.clipboard.writeText(expected); 67 let actual = SpecialPowers.getClipboardData(kTextPlainMimeType); 68 is(actual, expected, "writeText() wrote the right thing"); 69 } 70 71 async function testNoContentsRead() { 72 await SimpleTest.promiseClipboardChange("", () => { 73 clearClipboard(); 74 }); 75 76 const items = await navigator.clipboard.read(); 77 ok(!items.length, "read() read the right thing from empty clipboard"); 78 } 79 80 async function testNoContentsReadText() { 81 await SimpleTest.promiseClipboardChange("", () => { 82 clearClipboard(); 83 }); 84 let actual = await navigator.clipboard.readText(); 85 is(actual, "", "readText() read the right thing from empty clipboard"); 86 } 87 88 function runTest() { 89 (async function() { 90 await testRead(); 91 await testReadText(); 92 await testWrite(); 93 await testWriteText(); 94 95 await testNoContentsRead(); 96 await testNoContentsReadText(); 97 98 SimpleTest.finish(); 99 })(); 100 } 101 ]]> 102 </script> 103 104 <body xmlns="http://www.w3.org/1999/xhtml"> 105 <p id="display"> 106 </p> 107 <div id="content" style="display: none"> 108 </div> 109 <pre id="test"> 110 </pre> 111 </body> 112 113 </window>