summaryrefslogtreecommitdiffstats
path: root/widget/tests/clipboard_helper.js
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-21 11:44:51 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-21 11:44:51 +0000
commit9e3c08db40b8916968b9f30096c7be3f00ce9647 (patch)
treea68f146d7fa01f0134297619fbe7e33db084e0aa /widget/tests/clipboard_helper.js
parentInitial commit. (diff)
downloadthunderbird-9e3c08db40b8916968b9f30096c7be3f00ce9647.tar.xz
thunderbird-9e3c08db40b8916968b9f30096c7be3f00ce9647.zip
Adding upstream version 1:115.7.0.upstream/1%115.7.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'widget/tests/clipboard_helper.js')
-rw-r--r--widget/tests/clipboard_helper.js83
1 files changed, 83 insertions, 0 deletions
diff --git a/widget/tests/clipboard_helper.js b/widget/tests/clipboard_helper.js
new file mode 100644
index 0000000000..7315bd19ac
--- /dev/null
+++ b/widget/tests/clipboard_helper.js
@@ -0,0 +1,83 @@
+/* Any copyright is dedicated to the Public Domain.
+ http://creativecommons.org/publicdomain/zero/1.0/ */
+
+"use strict";
+
+const clipboard = SpecialPowers.Services.clipboard;
+const clipboardTypes = [
+ clipboard.kGlobalClipboard,
+ clipboard.kSelectionClipboard,
+ clipboard.kFindClipboard,
+ clipboard.kSelectionCache,
+];
+
+function cleanupAllClipboard() {
+ clipboardTypes.forEach(function (type) {
+ if (clipboard.isClipboardTypeSupported(type)) {
+ info(`cleanup clipboard ${type}`);
+ clipboard.emptyClipboard(type);
+ }
+ });
+}
+
+function generateRandomString() {
+ return "random number: " + Math.random();
+}
+
+function writeStringToClipboard(
+ aStr,
+ aFlavor,
+ aClipboardType,
+ aClipboardOwner = null,
+ aAsync = false
+) {
+ 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);
+
+ if (aAsync) {
+ let request = clipboard.asyncSetData(aClipboardType);
+ request.setData(trans, aClipboardOwner);
+ return;
+ }
+
+ clipboard.setData(trans, aClipboardOwner, aClipboardType);
+}
+
+function writeRandomStringToClipboard(
+ aFlavor,
+ aClipboardType,
+ aClipboardOwner = null,
+ aAsync = false
+) {
+ let randomString = generateRandomString();
+ writeStringToClipboard(
+ randomString,
+ aFlavor,
+ aClipboardType,
+ aClipboardOwner,
+ aAsync
+ );
+ return randomString;
+}
+
+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;
+}