Backed out changeset 2450366cf7ca (bug 1891629) for causing win msix mochitest failures
[gecko.git] / widget / tests / file_test_clipboard.js
blob76bdbaa84d77e1867fde8dd2c216a019e0689fb8
1 /* Any copyright is dedicated to the Public Domain.
2    http://creativecommons.org/publicdomain/zero/1.0/ */
4 /* import-globals-from clipboard_helper.js */
6 "use strict";
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(
15     Ci.nsITransferable
16   );
17   trans.init(getLoadContext());
18   trans.addDataFlavor("text/plain");
19   clipboard.getData(
20     trans,
21     Ci.nsIClipboard.kGlobalClipboard,
22     SpecialPowers.wrap(window).browsingContext.currentWindowContext
23   );
24   let str = SpecialPowers.createBlankObject();
25   try {
26     trans.getTransferData("text/plain", str);
27   } catch (e) {
28     str = "";
29   }
30   if (str) {
31     str = str.value.QueryInterface(Ci.nsISupportsString);
32     if (str) {
33       str = str.data;
34     }
35   }
36   return str;
39 add_setup(function init() {
40   cleanupAllClipboard();
41 });
43 /* Test for bug 948065 */
44 add_task(function test_copy() {
45   // Test copy.
46   const data = "random number: " + Math.random();
47   let helper = Cc["@mozilla.org/widget/clipboardhelper;1"].getService(
48     Ci.nsIClipboardHelper
49   );
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();
57 });
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}`);
65       // Set clipboard data
66       let str;
67       try {
68         str = writeRandomStringToClipboard("text/plain", clipboardType);
69       } catch (e) {
70         ok(
71           false,
72           `setData should not throw error for clipboard type ${clipboardType}`
73         );
74       }
76       // Test hasDataMatchingFlavors
77       try {
78         ok(
79           clipboard.hasDataMatchingFlavors(["text/plain"], clipboardType),
80           `Test hasDataMatchingFlavors for clipboard type ${clipboardType}`
81         );
82       } catch (e) {
83         ok(
84           false,
85           `hasDataMatchingFlavors should not throw error for clipboard type ${clipboardType}`
86         );
87       }
89       // Test getData
90       try {
91         is(
92           getClipboardData("text/plain", clipboardType),
93           str,
94           `Test getData for clipboard type ${clipboardType}`
95         );
96       } catch (e) {
97         ok(
98           false,
99           `getData should not throw error for clipboard type ${clipboardType}`
100         );
101       }
102     });
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);
109       is(
110         getClipboardData("text/plain", clipboardType),
111         null,
112         `Should get null data on clipboard type ${clipboardType}`
113       );
114       ok(
115         !clipboard.hasDataMatchingFlavors(["text/plain"], clipboardType),
116         `Should not have text/plain flavor on clipboard ${clipboardType}`
117       );
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")) {
123         todo_is(
124           getClipboardData("text/plain", clipboardType),
125           "",
126           `Should get empty string on clipboard type ${clipboardType}`
127         );
128       } else {
129         is(
130           getClipboardData("text/plain", clipboardType),
131           "",
132           `Should get empty string on clipboard type ${clipboardType}`
133         );
134       }
135       // XXX android doesn't support setting empty text data to clipboard, bug 1841058.
136       if (navigator.userAgent.includes("Android")) {
137         todo_is(
138           clipboard.hasDataMatchingFlavors(["text/plain"], clipboardType),
139           true,
140           `Should have text/plain flavor on clipboard ${clipboardType}`
141         );
142       } else {
143         ok(
144           clipboard.hasDataMatchingFlavors(["text/plain"], clipboardType),
145           `Should have text/plain flavor on clipboard ${clipboardType}`
146         );
147       }
149       // Clear all clipboard data.
150       cleanupAllClipboard();
151     });
152   }