test_copyimage.html (3205B)
1 <!DOCTYPE HTML> 2 <html> 3 <!-- 4 https://bugzilla.mozilla.org/show_bug.cgi?id=518249 5 https://bugzilla.mozilla.org/show_bug.cgi?id=952456 6 --> 7 <head> 8 <title>Test for copy image</title> 9 <script src="/tests/SimpleTest/SimpleTest.js"></script> 10 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 11 </head> 12 <body> 13 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=518249">Mozilla Bug 518249</a> 14 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=952456">Mozilla Bug 952456</a> 15 <p id="display"></p> 16 <div id="content" style="display: none"> 17 </div> 18 <pre id="test"> 19 <script class="testbody" type="text/javascript"> 20 21 function testCopyImage () { 22 var Ci = SpecialPowers.Ci; 23 var Cc = SpecialPowers.Cc; 24 var clipboard = SpecialPowers.Services.clipboard; 25 26 function getClipboardData(mime) { 27 var transferable = Cc['@mozilla.org/widget/transferable;1'] 28 .createInstance(Ci.nsITransferable); 29 var loadingContext = SpecialPowers.wrap(window).docShell 30 .QueryInterface(Ci.nsILoadContext); 31 transferable.init(loadingContext); 32 transferable.addDataFlavor(mime); 33 clipboard.getData(transferable, 1, SpecialPowers.wrap(window).browsingContext.currentWindowContext); 34 var data = SpecialPowers.createBlankObject(); 35 transferable.getTransferData(mime, data); 36 return data; 37 } 38 39 function testClipboardValue(mime, expected) { 40 var data = SpecialPowers.wrap(getClipboardData(mime)); 41 var str = data.value == null ? data.value : 42 data.value.QueryInterface(Ci.nsISupportsString).data; 43 is(str, expected, "clipboard has correct [" + mime + "] content") 44 } 45 46 //--------- Prepare data and copy it. 47 48 // Select the node. 49 var node = document.getElementById('logo'); 50 51 // Set node and copy image. 52 var docShell = SpecialPowers.wrap(window).docShell; 53 var documentViewer = docShell.docViewer 54 .QueryInterface(Ci.nsIDocumentViewerEdit); 55 documentViewer.setCommandNode(node); 56 documentViewer.copyImage(documentViewer.COPY_IMAGE_ALL); 57 58 //--------- Let's check the content of the clipboard now. 59 60 // Does the clipboard contain text/plain data ? 61 ok(clipboard.hasDataMatchingFlavors(["text/plain"], clipboard.kGlobalClipboard), "clipboard contains unicode text"); 62 // Does the clipboard contain text/html data ? 63 ok(clipboard.hasDataMatchingFlavors(["text/html"], clipboard.kGlobalClipboard), "clipboard contains html text"); 64 // Does the clipboard contain image data ? 65 ok(clipboard.hasDataMatchingFlavors(["image/png"], clipboard.kGlobalClipboard), "clipboard contains image"); 66 67 // Is the text/plain data correct ? 68 testClipboardValue('text/plain', 'about:logo'); 69 // Is the text/html data correct ? 70 var expected = '<img id="logo" src="about:logo">'; 71 if (navigator.platform.includes("Win")) { 72 expected = kTextHtmlPrefixClipboardDataWindows + expected + kTextHtmlSuffixClipboardDataWindows; 73 } 74 testClipboardValue('text/html', expected); 75 76 SimpleTest.finish(); 77 } 78 79 SimpleTest.waitForExplicitFinish(); 80 addLoadEvent(testCopyImage); 81 </script> 82 </pre> 83 <div> 84 <img id="logo" src="about:logo"> 85 </div> 86 </body> 87 </html>