From 36d22d82aa202bb199967e9512281e9a53db42c9 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 21:33:14 +0200 Subject: Adding upstream version 115.7.0esr. Signed-off-by: Daniel Baumann --- widget/tests/clipboard_helper.js | 83 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 widget/tests/clipboard_helper.js (limited to 'widget/tests/clipboard_helper.js') 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; +} -- cgit v1.2.3