1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/editor/libeditor/html/tests/test_CF_HTML_clipboard.html Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,159 @@ 1.4 +<!DOCTYPE HTML> 1.5 +<html> 1.6 +<!-- 1.7 +https://bugzilla.mozilla.org/show_bug.cgi?id=572642 1.8 +--> 1.9 +<head> 1.10 + <title>Test for Bug 572642</title> 1.11 + <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> 1.12 + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> 1.13 + <script type="text/javascript" src="/tests/SimpleTest/EventUtils.js"></script> 1.14 +</head> 1.15 +<body> 1.16 +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=572642">Mozilla Bug 572642</a> 1.17 +<p id="display"></p> 1.18 +<div id="content"> 1.19 + <div id="editor1" contenteditable="true"></div> 1.20 + <iframe id="editor2"></iframe> 1.21 +</div> 1.22 +<pre id="test"> 1.23 +<script type="application/javascript"> 1.24 + 1.25 +/** Test for Bug 572642 **/ 1.26 + 1.27 +function copyCF_HTML(cfhtml, success, failure) { 1.28 + const Cc = SpecialPowers.Cc; 1.29 + const Ci = SpecialPowers.Ci; 1.30 + const CF_HTML = "application/x-moz-nativehtml"; 1.31 + 1.32 + function getLoadContext() { 1.33 + return SpecialPowers.wrap(window).QueryInterface(Ci.nsIInterfaceRequestor) 1.34 + .getInterface(Ci.nsIWebNavigation) 1.35 + .QueryInterface(Ci.nsILoadContext); 1.36 + } 1.37 + 1.38 + var cb = Cc["@mozilla.org/widget/clipboard;1"]. 1.39 + getService(Ci.nsIClipboard); 1.40 + 1.41 + var counter = 0; 1.42 + function copyCF_HTML_worker(success, failure) { 1.43 + if (++counter > 50) { 1.44 + ok(false, "Timed out while polling clipboard for pasted data"); 1.45 + failure(); 1.46 + return; 1.47 + } 1.48 + 1.49 + var flavors = [CF_HTML]; 1.50 + if (!cb.hasDataMatchingFlavors(flavors, flavors.length, cb.kGlobalClipboard)) { 1.51 + setTimeout(function() copyCF_HTML_worker(success, failure), 100); 1.52 + return; 1.53 + } 1.54 + 1.55 + var trans = Cc["@mozilla.org/widget/transferable;1"]. 1.56 + createInstance(Ci.nsITransferable); 1.57 + trans.init(getLoadContext()); 1.58 + trans.addDataFlavor(CF_HTML); 1.59 + cb.getData(trans, cb.kGlobalClipboard); 1.60 + var data = SpecialPowers.createBlankObject(); 1.61 + try { 1.62 + trans.getTransferData(CF_HTML, data, {}); 1.63 + data = SpecialPowers.wrap(data).value.QueryInterface(Ci.nsISupportsCString).data; 1.64 + } catch (e) { 1.65 + setTimeout(function() copyCF_HTML_worker(success, failure), 100); 1.66 + return; 1.67 + } 1.68 + success(); 1.69 + } 1.70 + 1.71 + var trans = Cc["@mozilla.org/widget/transferable;1"]. 1.72 + createInstance(Ci.nsITransferable); 1.73 + trans.init(getLoadContext()); 1.74 + trans.addDataFlavor(CF_HTML); 1.75 + var data = Cc["@mozilla.org/supports-cstring;1"]. 1.76 + createInstance(Ci.nsISupportsCString); 1.77 + data.data = cfhtml; 1.78 + trans.setTransferData(CF_HTML, data, cfhtml.length); 1.79 + cb.setData(trans, null, cb.kGlobalClipboard); 1.80 + copyCF_HTML_worker(success, failure); 1.81 +} 1.82 + 1.83 +function loadCF_HTMLdata(filename) { 1.84 + var req = new XMLHttpRequest(); 1.85 + req.open("GET", filename, false); 1.86 + req.overrideMimeType("text/plain; charset=x-user-defined"); 1.87 + req.send(null); 1.88 + ok(req.status, 200, "Could not read the binary file " + filename); 1.89 + return req.responseText; 1.90 +} 1.91 + 1.92 +var gTests = [ 1.93 + // Copied from Firefox 1.94 + {fileName: "cfhtml-firefox.txt", expected: "Firefox"}, 1.95 + // Copied from OpenOffice.org 1.96 + {fileName: "cfhtml-ooo.txt", expected: "hello"}, 1.97 + // Copied from IE 1.98 + {fileName: "cfhtml-ie.txt", expected: "browser"}, 1.99 + // Copied from Chromium 1.100 + {fileName: "cfhtml-chromium.txt", expected: "Pacific"}, 1.101 + // CF_HTML with no context specified (StartHTML and EndHTML set to -1) 1.102 + {fileName: "cfhtml-nocontext.txt", expected: "3.1415926535897932"}, 1.103 +]; 1.104 +var gTestIndex = 0; 1.105 + 1.106 +SimpleTest.waitForExplicitFinish(); 1.107 + 1.108 +for (var i = 0; i < gTests.length; ++i) { 1.109 + gTests[i].data = loadCF_HTMLdata("data/" + gTests[i].fileName); 1.110 +} 1.111 + 1.112 +function runTest() { 1.113 + var test = gTests[gTestIndex++]; 1.114 + 1.115 + copyCF_HTML(test.data, function() { 1.116 + // contenteditable 1.117 + var contentEditable = document.getElementById("editor1"); 1.118 + contentEditable.innerHTML = ""; 1.119 + contentEditable.focus(); 1.120 + synthesizeKey("v", {accelKey: true}); 1.121 + isnot(contentEditable.textContent.indexOf(test.expected), -1, 1.122 + "Paste operation for " + test.fileName + " should be successful in contenteditable"); 1.123 + 1.124 + // designMode 1.125 + var iframe = document.getElementById("editor2"); 1.126 + iframe.addEventListener("load", function() { 1.127 + iframe.removeEventListener("load", arguments.callee, false); 1.128 + var doc = iframe.contentDocument; 1.129 + var win = doc.defaultView; 1.130 + setTimeout(function() { 1.131 + win.addEventListener("focus", function() { 1.132 + win.removeEventListener("focus", arguments.callee, false); 1.133 + doc.designMode = "on"; 1.134 + synthesizeKey("v", {accelKey: true}, win); 1.135 + isnot(doc.body.textContent.indexOf(test.expected), -1, 1.136 + "Paste operation for " + test.fileName + " should be successful in designMode"); 1.137 + 1.138 + if (gTestIndex == gTests.length) 1.139 + SimpleTest.finish(); 1.140 + else 1.141 + runTest(); 1.142 + }, false); 1.143 + win.focus(); 1.144 + }, 0); 1.145 + }, false); 1.146 + iframe.src = "data:text/html,"; 1.147 + }, SimpleTest.finish); 1.148 +} 1.149 + 1.150 +var isMac = ("nsILocalFileMac" in SpecialPowers.Ci); 1.151 +if (isMac) 1.152 + SimpleTest.waitForFocus(runTest); 1.153 +else { 1.154 + // This test is not yet supported on non-Mac platforms, see bug 574005. 1.155 + todo(false, "Test not supported on this platform"); 1.156 + SimpleTest.finish(); 1.157 +} 1.158 + 1.159 +</script> 1.160 +</pre> 1.161 +</body> 1.162 +</html>