gecko/content/base/test/test_bug166235.html
Ehsan Akhgari 45fe6d3ae2 Bug 722872 - Part 1: Add nsITransferable::Init(nsILoadContext*), enforce that it's called in debug builds, and add nsIDOMDocument* arguments to nsIClipboardHelper methods; r=roc
This patch does the following:

* It adds nsITransferable::Init(nsILoadContext*).  The load context
  might be null, which means that the transferable is non-private, but
  if it's non-null, we extract the boolean value for the privacy mode
  and store it in the transferable.
* It adds checks in debug builds to make sure that Init is always
  called, in form of fatal assertions.
* It adds nsIDOMDocument* agruments to nsIClipboardHelper methods which
  represent the document that the string is coming from.
  nsIClipboardHelper implementation internally gets the nsILoadContext
  from that and passes it on to the transferable upon creation.  The
  reason that I did this was that nsIClipboardHelper is supposed to be a
  high-level helper, and in most of its call sites, we have easy access
  to a document object.
* It modifies all of the call sites of the above interfaces according to
  this change.
* It adds a GetLoadContext helper to nsIDocument to help with changing
  the call sites.
2012-04-16 22:14:01 -04:00

134 lines
4.7 KiB
HTML

<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=166235
-->
<head>
<title>Test for Bug 166235</title>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=166235">Mozilla Bug 166235</a>
<p id="test0">This text should be copied.</p>
<p id="test1">This text should<span style="-moz-user-select: none;"> NOT</span> be copied.</p>
<p id="test2">This<span style="-moz-user-select: -moz-none;"><span style="-moz-user-select: text"> text should</span> NOT</span> be copied.</p>
<div id="content" style="display: none">
</div>
<textarea id="input"></textarea>
<pre id="test">
<script type="application/javascript">
/** Test for Bug 166235 **/
var Cc = SpecialPowers.wrap(Components).classes;
var Ci = SpecialPowers.wrap(Components).interfaces;
var webnav = SpecialPowers.wrap(window).QueryInterface(Components.interfaces.nsIInterfaceRequestor)
.getInterface(Components.interfaces.nsIWebNavigation)
var docShell = webnav.QueryInterface(Components.interfaces.nsIDocShell);
var documentViewer = docShell.contentViewer
.QueryInterface(Components.interfaces.nsIContentViewerEdit);
var clipboard = Cc["@mozilla.org/widget/clipboard;1"]
.getService(Components.interfaces.nsIClipboard);
var textarea = SpecialPowers.wrap(document).getElementById('input');
function getLoadContext() {
return SpecialPowers.wrap(window).QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIWebNavigation)
.QueryInterface(Ci.nsILoadContext);
}
function copyChildrenToClipboard(id) {
textarea.blur();
clipboard.emptyClipboard(1);
window.getSelection().selectAllChildren(document.getElementById(id));
documentViewer.copySelection();
is(clipboard.hasDataMatchingFlavors(["text/unicode"], 1,1), true);
is(clipboard.hasDataMatchingFlavors(["text/html"], 1,1), true);
}
function getClipboardData(mime) {
var transferable = Cc['@mozilla.org/widget/transferable;1']
.createInstance(Components.interfaces.nsITransferable);
transferable.init(getLoadContext());
transferable.addDataFlavor(mime);
clipboard.getData(transferable, 1);
var data = SpecialPowers.wrap({});
transferable.getTransferData(mime, data, {}) ;
return data;
}
function testClipboardValue(mime, expected, test) {
var data = getClipboardData(mime);
is (data.value == null ? data.value :
data.value.QueryInterface(Components.interfaces.nsISupportsString).data,
expected,
mime + " value in the clipboard");
return data.value;
}
function testPasteText(expected, test) {
textarea.value="";
textarea.focus();
textarea.QueryInterface(Components.interfaces.nsIDOMNSEditableElement)
.editor.paste(1);
is(textarea.value, expected, test + ": textarea paste");
}
function testInnerHTML(id, expected) {
var value = document.getElementById(id).innerHTML;
is(value, expected, id + ".innerHTML");
}
// expected results for Selection.toString()
var originalStrings = [
'This text should be copied.',
'This text should be copied.',
'This text should be copied.',
];
// expected results for clipboard text/html
var clipboardHTML = [
'<p id=\"test0\">This text should be copied.</p>',
'<p id=\"test1\">This text should be copied.</p>',
'<p id=\"test2\">This<span style=\"-moz-user-select: text\"> text should</span> be copied.</p>'
];
// expected results for clipboard text/unicode
var clipboardUnicode = [
'This text should be copied.',
'This text should be copied.',
'This text should be copied.'
];
// expected results for .innerHTML
var innerHTMLStrings = [
'This text should be copied.',
'This text should<span style=\"-moz-user-select: none;\"> NOT</span> be copied.',
'This<span style=\"-moz-user-select: -moz-none;\"><span style=\"-moz-user-select: text\"> text should</span> NOT</span> be copied.'
];
// expected results for pasting into a TEXTAREA
var textareaStrings = [
'This text should be copied.',
'This text should be copied.',
'This text should be copied.'
];
for (var i = 0; i < 3; i++) {
var id = 'test' + i;
copyChildrenToClipboard(id);
is(window.getSelection().toString(), originalStrings[i], id + ' Selection.toString()');
testClipboardValue("text/html", clipboardHTML[i], id);
testClipboardValue("text/unicode", clipboardUnicode[i], id);
testInnerHTML(id, innerHTMLStrings[i]);
testPasteText(textareaStrings[i], id + '.innerHTML');
}
</script>
</pre>
</body>
</html>