summaryrefslogtreecommitdiffstats
path: root/widget/tests/test_clipboard_asyncSetData.xhtml
diff options
context:
space:
mode:
Diffstat (limited to 'widget/tests/test_clipboard_asyncSetData.xhtml')
-rw-r--r--widget/tests/test_clipboard_asyncSetData.xhtml173
1 files changed, 173 insertions, 0 deletions
diff --git a/widget/tests/test_clipboard_asyncSetData.xhtml b/widget/tests/test_clipboard_asyncSetData.xhtml
new file mode 100644
index 0000000000..27f5fad58c
--- /dev/null
+++ b/widget/tests/test_clipboard_asyncSetData.xhtml
@@ -0,0 +1,173 @@
+<?xml version="1.0"?>
+<?xml-stylesheet type="text/css" href="chrome://global/skin"?>
+<?xml-stylesheet type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=1712122
+-->
+<window title="Mozilla Bug 1712122"
+ xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
+<script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
+
+<!-- test results are displayed in the html:body -->
+<body xmlns="http://www.w3.org/1999/xhtml">
+<p id="display"></p>
+<div id="content" style="display: none"></div>
+<pre id="test"></pre>
+</body>
+
+<!-- test code goes here -->
+<script class="testbody" type="application/javascript">
+<![CDATA[
+
+const clipboard = SpecialPowers.Services.clipboard;
+const clipboardTypes = [
+ clipboard.kGlobalClipboard,
+ clipboard.kSelectionClipboard,
+ clipboard.kFindClipboard,
+];
+
+function generateRandomString() {
+ return "random number: " + Math.random();
+}
+
+function generateNewTransferable(aFlavor, aStr) {
+ let trans = Cc["@mozilla.org/widget/transferable;1"].createInstance(Ci.nsITransferable);
+ trans.init(null);
+ trans.addDataFlavor(aFlavor);
+
+ let supportsStr = Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsString);
+ supportsStr.data = aStr;
+ trans.setTransferData(aFlavor, supportsStr);
+
+ return trans;
+}
+
+function getClipboardData(aFlavor, aClipboardType) {
+ var trans = Cc["@mozilla.org/widget/transferable;1"].createInstance(Ci.nsITransferable);
+ trans.init(null);
+ trans.addDataFlavor(aFlavor);
+ clipboard.getData(trans, aClipboardType);
+
+ var data = {};
+ trans.getTransferData(aFlavor, data);
+ return data.value.QueryInterface(SpecialPowers.Ci.nsISupportsString).data;
+}
+
+function setClipboardData(aFlavor, aStr, aClipboardType, aAsync) {
+ let trans = generateNewTransferable(aFlavor, aStr);
+ if (aAsync) {
+ let request = clipboard.asyncSetData(aClipboardType);
+ request.setData(trans, null);
+ return;
+ }
+
+ clipboard.setData(trans, null, aClipboardType);
+}
+
+function cleanupAllClipboard() {
+ clipboardTypes.forEach(function(type) {
+ if (clipboard.isClipboardTypeSupported(type)) {
+ info(`Cleanup clipboard ${type}`);
+ clipboard.emptyClipboard(type);
+ }
+ });
+}
+
+clipboardTypes.forEach(function(type) {
+ if (clipboard.isClipboardTypeSupported(type)) {
+ clipboardTypes.forEach(function(otherType) {
+ if (clipboard.isClipboardTypeSupported(otherType)) {
+ [true, false].forEach(function(async) {
+ add_task(async function test_clipboard_pending_asyncSetData() {
+ info(`Test having a pending asyncSetData request on ${type} and then make a new ${async ? "asyncSetData" : "setData"} request on ${otherType}`);
+
+ // Create a pending asyncSetData request
+ let priorResult;
+ let priorRequest = clipboard.asyncSetData(type, {
+ QueryInterface: ChromeUtils.generateQI(["nsIAsyncSetClipboardDataCallback"]),
+ onComplete(rv) {
+ priorResult = rv;
+ },
+ });
+
+ // Create a new request
+ let str = generateRandomString();
+ setClipboardData("text/plain", str, otherType, async);
+
+ if (type === otherType) {
+ info("The new request should cancel the prior pending request");
+ is(priorResult, Cr.NS_ERROR_ABORT, "The pending asyncSetData request should be canceled");
+ try {
+ priorRequest.setData(generateNewTransferable("text/plain", generateRandomString()));
+ ok(false, "An error should be thrown if setData is called on a canceled clipboard request");
+ } catch (e) {
+ is(e.result, Cr.NS_ERROR_FAILURE, "An error should be thrown if setData is called on a canceled clipboard request");
+ }
+ } else {
+ info("The new request should be used to cancel the prior pending request for different clipboard types");
+ is(priorResult, undefined, "The pending asyncSetData request should not be canceled");
+ str = generateRandomString();
+ priorRequest.setData(generateNewTransferable("text/plain", str), null);
+ try {
+ priorRequest.setData(generateNewTransferable("text/plain", generateRandomString()));
+ ok(false, "Calling setData multiple times should throw an error");
+ } catch(e) {
+ is(e.result, Cr.NS_ERROR_FAILURE, "Calling setData multiple times should throw an error");
+ }
+ }
+
+ // Test clipboard data.
+ is(getClipboardData("text/plain", type), str, `Test clipboard data for type ${type}`);
+
+ // Clean clipboard data.
+ cleanupAllClipboard();
+ });
+ });
+ }
+ });
+
+ add_task(async function test_clipboard_asyncSetData_abort() {
+ info(`Test abort asyncSetData request on ${type}`);
+
+ // Create a pending asyncSetData request
+ let result;
+ let request = clipboard.asyncSetData(type, {
+ QueryInterface: ChromeUtils.generateQI(["nsIAsyncSetClipboardDataCallback"]),
+ onComplete(rv) {
+ result = rv;
+ },
+ });
+
+ // Abort with NS_OK.
+ try {
+ request.abort(Cr.NS_OK);
+ ok(false, "Throw an error when attempting to abort with NS_OK");
+ } catch(e) {
+ is(e.result, Cr.NS_ERROR_FAILURE, "Should throw an error when attempting to abort with NS_OK");
+ }
+ is(result, undefined, "The asyncSetData request should not be canceled");
+
+ // Abort with NS_ERROR_ABORT.
+ request.abort(Cr.NS_ERROR_ABORT);
+ is(result, Cr.NS_ERROR_ABORT, "The asyncSetData request should be canceled");
+ try {
+ request.abort(Cr.NS_ERROR_FAILURE);
+ ok(false, "Throw an error when attempting to abort again");
+ } catch(e) {
+ is(e.result, Cr.NS_ERROR_FAILURE, "Should throw an error when attempting to abort again");
+ }
+ is(result, Cr.NS_ERROR_ABORT, "The callback should not be notified again");
+
+ try {
+ request.setData(generateNewTransferable("text/plain", generateRandomString()));
+ ok(false, "An error should be thrown if setData is called on a canceled clipboard request");
+ } catch (e) {
+ is(e.result, Cr.NS_ERROR_FAILURE, "An error should be thrown if setData is called on a canceled clipboard request");
+ }
+ });
+ }
+});
+
+]]>
+</script>
+</window>