clipboard-item.https.html (4226B)
1 <!doctype html> 2 <meta charset="utf-8"> 3 <title>ClipboardItem tests</title> 4 <link rel="help" href="https://w3c.github.io/clipboard-apis/#async-clipboard-api"> 5 <script src="/resources/testharness.js"></script> 6 <script src="/resources/testharnessreport.js"></script> 7 <script> 8 9 const blob = new Blob(['hello'], {type: 'text/plain'}); 10 const blob2 = new Blob(['this should work'], {type: 'not a/real type'}); 11 12 test(() => { 13 new ClipboardItem({'text/plain': blob}); 14 new ClipboardItem({'text/plain': blob, 'not a/real type': blob2}); 15 }, "ClipboardItem({string, Blob}) succeeds with different types"); 16 17 test(() => { 18 new ClipboardItem({'text/plain': blob}, {}); 19 }, "ClipboardItem() succeeds with empty options"); 20 21 test(() => { 22 assert_throws_js(TypeError, () => {new ClipboardItem({});}); 23 }, "ClipboardItem({}) fails with empty dictionary input"); 24 25 test(() => { 26 assert_throws_js(TypeError, () => {new ClipboardItem(blob);}); 27 }, "ClipboardItem(Blob) fails"); 28 29 test(() => { 30 assert_throws_js(TypeError, () => {new ClipboardItem(null);}); 31 }, "ClipboardItem() fails with null input"); 32 33 test(() => { 34 assert_throws_js(TypeError, () => {new ClipboardItem();}); 35 }, "ClipboardItem() fails with no input"); 36 37 test(() => { 38 const item = new ClipboardItem({'text/plain': blob}); 39 const types = item.types; 40 assert_equals(types.length, 1); 41 assert_equals(types[0], 'text/plain'); 42 const item2 = 43 new ClipboardItem({'text/plain': blob, 'not a/real type': blob2}); 44 const types2 = item2.types; 45 assert_equals(types2.length, 2); 46 assert_equals(types2[0], 'text/plain'); 47 assert_equals(types2[1], 'not a/real type'); 48 }, "types() returns correct values"); 49 50 promise_test(async () => { 51 const item = 52 new ClipboardItem({'text/plain': blob, 'not a/real type': blob2}); 53 54 const blobOutput = await item.getType('text/plain'); 55 assert_true(blobOutput.type.includes('text/plain')); 56 const text = await (new Response(blobOutput)).text(); 57 58 assert_equals('hello', text); 59 }, "getType(DOMString valid type) succeeds with correct output"); 60 61 promise_test(async () => { 62 const item = 63 new ClipboardItem({'text/plain': blob, 'not a/real type': blob2}); 64 65 const blobOutput = await item.getType('not a/real type'); 66 assert_true(blobOutput.type.includes('not a/real type')); 67 const text = await (new Response(blobOutput)).text(); 68 69 assert_equals('this should work', text); 70 }, "getType(DOMString invalid type) succeeds with correct output"); 71 72 promise_test(async t => { 73 const item = 74 new ClipboardItem({'text/plain': blob, 'not a/real type': blob2}); 75 promise_rejects_dom(t, "NotFoundError", item.getType('type not in item')); 76 promise_rejects_dom(t, "NotFoundError", item.getType('text/plain:subtype')); 77 }, "getType(DOMString type) rejects correctly when querying for missing type"); 78 79 promise_test(async () => { 80 const item = 81 new ClipboardItem({'text/plain': 'abc', 'not a/real type': 'xxx'}); 82 const blob = await item.getType('text/plain'); 83 assert_equals(blob.type, 'text/plain'); 84 85 const text = await (new Response(blob)).text(); 86 assert_equals(text, 'abc'); 87 }, "getType(DOMString valid type) converts DOMString to Blob"); 88 89 promise_test(async () => { 90 const item = 91 new ClipboardItem({'text/plain': 'abc', 'not a/real type': 'xxx'}); 92 const blob = await item.getType('not a/real type'); 93 assert_equals(blob.type, 'not a/real type'); 94 95 const text = await (new Response(blob)).text(); 96 assert_equals(text, 'xxx'); 97 }, "getType(DOMString invalid type) converts DOMString to Blob"); 98 99 [ 100 // mandatory data types 101 ['text/plain', true], 102 ['text/html', true], 103 ['image/png', true], 104 // optional data types 105 ['text/uri-list', true], 106 ['image/svg+xml', true], 107 ['web foo/bar', true], 108 ['web text/html', true], 109 // invalid types 110 ['web ', false], 111 ['web', false], 112 ['web foo', false], 113 ['foo/bar', false], 114 ['weB text/html', false], 115 [' web text/html', false], 116 ['not a/real type', false], 117 ['', false], 118 [' ', false], 119 ].forEach(([type, result]) => { 120 promise_test(async () => { 121 assert_equals(ClipboardItem.supports(type), result); 122 }, `supports(${type}) returns ${result ? "true" : "false"}`); 123 }); 124 </script>