editor/libeditor/html/tests/test_CF_HTML_clipboard.html

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 <!DOCTYPE HTML>
michael@0 2 <html>
michael@0 3 <!--
michael@0 4 https://bugzilla.mozilla.org/show_bug.cgi?id=572642
michael@0 5 -->
michael@0 6 <head>
michael@0 7 <title>Test for Bug 572642</title>
michael@0 8 <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
michael@0 9 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
michael@0 10 <script type="text/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
michael@0 11 </head>
michael@0 12 <body>
michael@0 13 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=572642">Mozilla Bug 572642</a>
michael@0 14 <p id="display"></p>
michael@0 15 <div id="content">
michael@0 16 <div id="editor1" contenteditable="true"></div>
michael@0 17 <iframe id="editor2"></iframe>
michael@0 18 </div>
michael@0 19 <pre id="test">
michael@0 20 <script type="application/javascript">
michael@0 21
michael@0 22 /** Test for Bug 572642 **/
michael@0 23
michael@0 24 function copyCF_HTML(cfhtml, success, failure) {
michael@0 25 const Cc = SpecialPowers.Cc;
michael@0 26 const Ci = SpecialPowers.Ci;
michael@0 27 const CF_HTML = "application/x-moz-nativehtml";
michael@0 28
michael@0 29 function getLoadContext() {
michael@0 30 return SpecialPowers.wrap(window).QueryInterface(Ci.nsIInterfaceRequestor)
michael@0 31 .getInterface(Ci.nsIWebNavigation)
michael@0 32 .QueryInterface(Ci.nsILoadContext);
michael@0 33 }
michael@0 34
michael@0 35 var cb = Cc["@mozilla.org/widget/clipboard;1"].
michael@0 36 getService(Ci.nsIClipboard);
michael@0 37
michael@0 38 var counter = 0;
michael@0 39 function copyCF_HTML_worker(success, failure) {
michael@0 40 if (++counter > 50) {
michael@0 41 ok(false, "Timed out while polling clipboard for pasted data");
michael@0 42 failure();
michael@0 43 return;
michael@0 44 }
michael@0 45
michael@0 46 var flavors = [CF_HTML];
michael@0 47 if (!cb.hasDataMatchingFlavors(flavors, flavors.length, cb.kGlobalClipboard)) {
michael@0 48 setTimeout(function() copyCF_HTML_worker(success, failure), 100);
michael@0 49 return;
michael@0 50 }
michael@0 51
michael@0 52 var trans = Cc["@mozilla.org/widget/transferable;1"].
michael@0 53 createInstance(Ci.nsITransferable);
michael@0 54 trans.init(getLoadContext());
michael@0 55 trans.addDataFlavor(CF_HTML);
michael@0 56 cb.getData(trans, cb.kGlobalClipboard);
michael@0 57 var data = SpecialPowers.createBlankObject();
michael@0 58 try {
michael@0 59 trans.getTransferData(CF_HTML, data, {});
michael@0 60 data = SpecialPowers.wrap(data).value.QueryInterface(Ci.nsISupportsCString).data;
michael@0 61 } catch (e) {
michael@0 62 setTimeout(function() copyCF_HTML_worker(success, failure), 100);
michael@0 63 return;
michael@0 64 }
michael@0 65 success();
michael@0 66 }
michael@0 67
michael@0 68 var trans = Cc["@mozilla.org/widget/transferable;1"].
michael@0 69 createInstance(Ci.nsITransferable);
michael@0 70 trans.init(getLoadContext());
michael@0 71 trans.addDataFlavor(CF_HTML);
michael@0 72 var data = Cc["@mozilla.org/supports-cstring;1"].
michael@0 73 createInstance(Ci.nsISupportsCString);
michael@0 74 data.data = cfhtml;
michael@0 75 trans.setTransferData(CF_HTML, data, cfhtml.length);
michael@0 76 cb.setData(trans, null, cb.kGlobalClipboard);
michael@0 77 copyCF_HTML_worker(success, failure);
michael@0 78 }
michael@0 79
michael@0 80 function loadCF_HTMLdata(filename) {
michael@0 81 var req = new XMLHttpRequest();
michael@0 82 req.open("GET", filename, false);
michael@0 83 req.overrideMimeType("text/plain; charset=x-user-defined");
michael@0 84 req.send(null);
michael@0 85 ok(req.status, 200, "Could not read the binary file " + filename);
michael@0 86 return req.responseText;
michael@0 87 }
michael@0 88
michael@0 89 var gTests = [
michael@0 90 // Copied from Firefox
michael@0 91 {fileName: "cfhtml-firefox.txt", expected: "Firefox"},
michael@0 92 // Copied from OpenOffice.org
michael@0 93 {fileName: "cfhtml-ooo.txt", expected: "hello"},
michael@0 94 // Copied from IE
michael@0 95 {fileName: "cfhtml-ie.txt", expected: "browser"},
michael@0 96 // Copied from Chromium
michael@0 97 {fileName: "cfhtml-chromium.txt", expected: "Pacific"},
michael@0 98 // CF_HTML with no context specified (StartHTML and EndHTML set to -1)
michael@0 99 {fileName: "cfhtml-nocontext.txt", expected: "3.1415926535897932"},
michael@0 100 ];
michael@0 101 var gTestIndex = 0;
michael@0 102
michael@0 103 SimpleTest.waitForExplicitFinish();
michael@0 104
michael@0 105 for (var i = 0; i < gTests.length; ++i) {
michael@0 106 gTests[i].data = loadCF_HTMLdata("data/" + gTests[i].fileName);
michael@0 107 }
michael@0 108
michael@0 109 function runTest() {
michael@0 110 var test = gTests[gTestIndex++];
michael@0 111
michael@0 112 copyCF_HTML(test.data, function() {
michael@0 113 // contenteditable
michael@0 114 var contentEditable = document.getElementById("editor1");
michael@0 115 contentEditable.innerHTML = "";
michael@0 116 contentEditable.focus();
michael@0 117 synthesizeKey("v", {accelKey: true});
michael@0 118 isnot(contentEditable.textContent.indexOf(test.expected), -1,
michael@0 119 "Paste operation for " + test.fileName + " should be successful in contenteditable");
michael@0 120
michael@0 121 // designMode
michael@0 122 var iframe = document.getElementById("editor2");
michael@0 123 iframe.addEventListener("load", function() {
michael@0 124 iframe.removeEventListener("load", arguments.callee, false);
michael@0 125 var doc = iframe.contentDocument;
michael@0 126 var win = doc.defaultView;
michael@0 127 setTimeout(function() {
michael@0 128 win.addEventListener("focus", function() {
michael@0 129 win.removeEventListener("focus", arguments.callee, false);
michael@0 130 doc.designMode = "on";
michael@0 131 synthesizeKey("v", {accelKey: true}, win);
michael@0 132 isnot(doc.body.textContent.indexOf(test.expected), -1,
michael@0 133 "Paste operation for " + test.fileName + " should be successful in designMode");
michael@0 134
michael@0 135 if (gTestIndex == gTests.length)
michael@0 136 SimpleTest.finish();
michael@0 137 else
michael@0 138 runTest();
michael@0 139 }, false);
michael@0 140 win.focus();
michael@0 141 }, 0);
michael@0 142 }, false);
michael@0 143 iframe.src = "data:text/html,";
michael@0 144 }, SimpleTest.finish);
michael@0 145 }
michael@0 146
michael@0 147 var isMac = ("nsILocalFileMac" in SpecialPowers.Ci);
michael@0 148 if (isMac)
michael@0 149 SimpleTest.waitForFocus(runTest);
michael@0 150 else {
michael@0 151 // This test is not yet supported on non-Mac platforms, see bug 574005.
michael@0 152 todo(false, "Test not supported on this platform");
michael@0 153 SimpleTest.finish();
michael@0 154 }
michael@0 155
michael@0 156 </script>
michael@0 157 </pre>
michael@0 158 </body>
michael@0 159 </html>

mercurial