1 /* Any copyright is dedicated to the Public Domain.
2 http://creativecommons.org/publicdomain/zero/1.0/ */
4 /* import-globals-from clipboard_helper.js */
8 function getLoadContext() {
9 return SpecialPowers.wrap(window).docShell.QueryInterface(Ci.nsILoadContext);
12 // Get clipboard data to paste.
13 function paste(clipboard) {
14 let trans = Cc["@mozilla.org/widget/transferable;1"].createInstance(
17 trans.init(getLoadContext());
18 trans.addDataFlavor("text/plain");
21 Ci.nsIClipboard.kGlobalClipboard,
22 SpecialPowers.wrap(window).browsingContext.currentWindowContext
24 let str = SpecialPowers.createBlankObject();
26 trans.getTransferData("text/plain", str);
31 str = str.value.QueryInterface(Ci.nsISupportsString);
39 add_setup(function init() {
40 cleanupAllClipboard();
43 /* Test for bug 948065 */
44 add_task(function test_copy() {
46 const data = "random number: " + Math.random();
47 let helper = Cc["@mozilla.org/widget/clipboardhelper;1"].getService(
50 helper.copyString(data);
51 is(paste(clipboard), data, "Data was successfully copied.");
53 clipboard.emptyClipboard(Ci.nsIClipboard.kGlobalClipboard);
54 is(paste(clipboard), "", "Data was successfully cleared.");
56 cleanupAllClipboard();
59 /* Tests for bug 1834073 */
60 clipboardTypes.forEach(function (clipboardType) {
61 if (clipboard.isClipboardTypeSupported(clipboardType)) {
62 add_task(function test_clipboard_apis() {
63 info(`Test clipboard apis for type ${clipboardType}`);
68 str = writeRandomStringToClipboard("text/plain", clipboardType);
72 `setData should not throw error for clipboard type ${clipboardType}`
76 // Test hasDataMatchingFlavors
79 clipboard.hasDataMatchingFlavors(["text/plain"], clipboardType),
80 `Test hasDataMatchingFlavors for clipboard type ${clipboardType}`
85 `hasDataMatchingFlavors should not throw error for clipboard type ${clipboardType}`
92 getClipboardData("text/plain", clipboardType),
94 `Test getData for clipboard type ${clipboardType}`
99 `getData should not throw error for clipboard type ${clipboardType}`
104 add_task(function test_clipboard_set_empty_string() {
105 info(`Test setting empty string to type ${clipboardType}`);
107 // Clear clipboard type.
108 clipboard.emptyClipboard(clipboardType);
110 getClipboardData("text/plain", clipboardType),
112 `Should get null data on clipboard type ${clipboardType}`
115 !clipboard.hasDataMatchingFlavors(["text/plain"], clipboardType),
116 `Should not have text/plain flavor on clipboard ${clipboardType}`
119 // Set text/plain to empty string.
120 writeStringToClipboard("", "text/plain", clipboardType);
121 // XXX gtk doesn't support get empty text data from clipboard, bug 1852983.
122 if (navigator.platform.includes("Linux")) {
124 getClipboardData("text/plain", clipboardType),
126 `Should get empty string on clipboard type ${clipboardType}`
130 getClipboardData("text/plain", clipboardType),
132 `Should get empty string on clipboard type ${clipboardType}`
135 // XXX android doesn't support setting empty text data to clipboard, bug 1841058.
136 if (navigator.userAgent.includes("Android")) {
138 clipboard.hasDataMatchingFlavors(["text/plain"], clipboardType),
140 `Should have text/plain flavor on clipboard ${clipboardType}`
144 clipboard.hasDataMatchingFlavors(["text/plain"], clipboardType),
145 `Should have text/plain flavor on clipboard ${clipboardType}`
149 // Clear all clipboard data.
150 cleanupAllClipboard();