test_async_clipboard_datatransfer.html (2242B)
1 <html> 2 <head> 3 <title>Test for bug 1756955</title> 4 <link rel="stylesheet" href="/tests/SimpleTest/test.css"> 5 <script src="/tests/SimpleTest/SimpleTest.js"></script> 6 <script src="/tests/SimpleTest/EventUtils.js"></script> 7 <script> 8 9 const kIsMac = navigator.platform.indexOf("Mac") > -1; 10 11 async function copyByDataTransfer(aItems) { 12 const copyPromise = new Promise(resolve => { 13 document.addEventListener("copy", (e) => { 14 e.preventDefault(); 15 for (const [key, value] of Object.entries(aItems)) { 16 e.clipboardData.setData(key, value); 17 } 18 resolve(); 19 }, { once: true }); 20 }); 21 synthesizeKey( 22 "c", 23 kIsMac ? { accelKey: true } : { ctrlKey: true } 24 ); 25 await copyPromise; 26 } 27 28 async function paste(aCallback) { 29 const pastePromise = new Promise(resolve => { 30 document.addEventListener("paste", (e) => { 31 resolve(aCallback(e.clipboardData)); 32 }, { once: true }); 33 }); 34 synthesizeKey( 35 "v", 36 kIsMac ? { accelKey: true } : { ctrlKey: true } 37 ); 38 await pastePromise; 39 } 40 41 add_task(async function test_mandatory_type() { 42 const items = { 43 "text/plain": "X" + Math.random(), 44 "custom/foo": "X" + Math.random(), 45 }; 46 await copyByDataTransfer(items); 47 await paste(async (clipboardData) => { 48 for (const [key, value] of Object.entries(items)) { 49 is(clipboardData.getData(key), value, `Check ${key} type`); 50 } 51 52 let clipboardItems = await navigator.clipboard.read(); 53 is(clipboardItems.length, 1, "Should only one clipboardItem"); 54 is(clipboardItems[0].types.length, 1, "Should only one type"); 55 is(await clipboardItems[0].getType("text/plain").then(blob => blob.text()), 56 items["text/plain"], 57 "Check text/plain type in clipbordItem"); 58 }); 59 }); 60 61 add_task(async function test_no_mandatory_type() { 62 const items = { 63 "custom/foo": "X" + Math.random(), 64 }; 65 await copyByDataTransfer(items); 66 await paste(async (clipboardData) => { 67 for (const [key, value] of Object.entries(items)) { 68 is(clipboardData.getData(key), value, `Check ${key} type`); 69 } 70 71 let clipboardItems = await navigator.clipboard.read(); 72 is(clipboardItems.length, 0, "Should only have no clipboardItem"); 73 }); 74 }); 75 76 </script> 77 </head> 78 <body> 79 </body> 80 </html>