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