Bug 1880216 - Migrate Fenix docs into Sphinx. r=owlish,geckoview-reviewers,android...
[gecko.git] / widget / tests / test_clipboard_cache_chrome.html
blob81d7a652b68df2756c7cf72dd85f31e48cbfd4b5
1 <!DOCTYPE HTML>
2 <html>
3 <!--
4 https://bugzilla.mozilla.org/show_bug.cgi?id=1812543
5 -->
6 <head>
7 <title>Test for Bug 1812543</title>
8 <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css" />
9 <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
10 <script src="clipboard_helper.js"></script>
11 </head>
12 <body>
13 <p id="display"></p>
14 <div id="content" style="display: none"></div>
15 <pre id="test"></pre>
16 <script class="testbody" type="application/javascript">
18 function testClipboardCache(aClipboardType, aAsync, aIsSupportGetFromCachedTransferable) {
19 add_task(function test_clipboard_get() {
20 info(`test_clipboard_get ${aAsync ? "async " : ""}` +
21 `with pref ${aIsSupportGetFromCachedTransferable ? "enabled" : "disabled"}`);
23 const string = generateRandomString();
24 const trans = generateNewTransferable("text/plain", string);
26 info(`Write text/plain data to clipboard ${aClipboardType}`);
27 if (aAsync) {
28 let request = clipboard.asyncSetData(aClipboardType);
29 request.setData(trans, null);
30 } else {
31 clipboard.setData(trans, null, aClipboardType);
33 is(getClipboardData("text/plain", aClipboardType), string,
34 `Check text/plain data on clipboard ${aClipboardType}`);
36 info(`Add text/foo data to transferable`);
37 addStringToTransferable("text/foo", string, trans);
38 // XXX macOS caches the transferable to implement kSelectionCache type, too,
39 // so it behaves differently than other types.
40 if (
41 navigator.platform.includes("Mac") &&
42 aClipboardType == clipboard.kSelectionCache &&
43 !aIsSupportGetFromCachedTransferable &&
44 !SpecialPowers.isHeadless
45 ) {
46 todo_is(getClipboardData("text/foo", aClipboardType),
47 aIsSupportGetFromCachedTransferable ? string : null,
48 `Check text/foo data on clipboard ${aClipboardType}`);
49 } else {
50 is(getClipboardData("text/foo", aClipboardType),
51 aIsSupportGetFromCachedTransferable ? string : null,
52 `Check text/foo data on clipboard ${aClipboardType}`);
55 info(`Should not get the data from other clipboard type`);
56 clipboardTypes.forEach(function(otherType) {
57 if (otherType != aClipboardType &&
58 clipboard.isClipboardTypeSupported(otherType)) {
59 is(getClipboardData("text/plain", otherType), null,
60 `Check text/plain data on clipboard ${otherType}`);
61 is(getClipboardData("text/foo", otherType), null,
62 `Check text/foo data on clipboard ${otherType}`);
64 info(`Write text/plain data to clipboard ${otherType}`);
65 writeRandomStringToClipboard("text/plain", otherType);
67 });
69 info(`Check data on clipboard ${aClipboardType} again`);
70 is(getClipboardData("text/plain", aClipboardType), string,
71 `Check text/plain data on clipboard ${aClipboardType} again`);
72 // XXX macOS caches the transferable to implement kSelectionCache type, too,
73 // so it behaves differently than other types.
74 if (
75 navigator.platform.includes("Mac") &&
76 aClipboardType == clipboard.kSelectionCache &&
77 !aIsSupportGetFromCachedTransferable &&
78 !SpecialPowers.isHeadless
79 ) {
80 todo_is(getClipboardData("text/foo", aClipboardType),
81 aIsSupportGetFromCachedTransferable ? string : null,
82 `Check text/foo data on clipboard ${aClipboardType} again`);
83 } else {
84 is(getClipboardData("text/foo", aClipboardType),
85 aIsSupportGetFromCachedTransferable ? string : null,
86 `Check text/foo data on clipboard ${aClipboardType} again`);
89 info(`Clean all clipboard data`);
90 cleanupAllClipboard();
91 });
94 function runClipboardCacheTests(aIsSupportGetFromCachedTransferable) {
95 add_task(async function setup() {
96 cleanupAllClipboard();
97 await SpecialPowers.pushPrefEnv({
98 set: [
100 "widget.clipboard.use-cached-data.enabled",
101 aIsSupportGetFromCachedTransferable,
107 clipboardTypes.forEach(function (type) {
108 if (!clipboard.isClipboardTypeSupported(type)) {
109 return;
112 add_task(function test_clipboard_hasDataMatchingFlavors() {
113 info(`test_clipboard_hasDataMatchingFlavors with pref ` +
114 `${aIsSupportGetFromCachedTransferable ? "enabled" : "disabled"}`);
116 const trans = generateNewTransferable("text/plain", generateRandomString());
118 info(`Write text/plain data to clipboard ${type}`);
119 clipboard.setData(trans, null, type);
120 ok(clipboard.hasDataMatchingFlavors(["text/plain"], type),
121 `Check if there is text/plain flavor on clipboard ${type}`);
122 ok(!clipboard.hasDataMatchingFlavors(["text/foo"], type),
123 `Check if there is text/foo flavor on clipboard ${type}`);
125 info(`Add text/foo data to transferable`);
126 addStringToTransferable("text/foo", generateRandomString(), trans);
127 ok(clipboard.hasDataMatchingFlavors(["text/plain"], type),
128 `Check if there is text/plain flavor on clipboard ${type}`);
129 // XXX macOS caches the transferable to implement kSelectionCache type, too,
130 // so it behaves differently than other types.
131 if (
132 navigator.platform.includes("Mac") &&
133 type == clipboard.kSelectionCache &&
134 !aIsSupportGetFromCachedTransferable &&
135 !SpecialPowers.isHeadless
137 todo_is(clipboard.hasDataMatchingFlavors(["text/foo"], type),
138 aIsSupportGetFromCachedTransferable,
139 `Check if there is text/foo flavor on clipboard ${type}`);
140 } else {
141 is(clipboard.hasDataMatchingFlavors(["text/foo"], type),
142 aIsSupportGetFromCachedTransferable,
143 `Check if there is text/foo flavor on clipboard ${type}`);
146 // Check other clipboard types.
147 clipboardTypes.forEach(function(otherType) {
148 if (otherType != type &&
149 clipboard.isClipboardTypeSupported(otherType)) {
150 ok(!clipboard.hasDataMatchingFlavors(["text/plain"], otherType),
151 `Check if there is text/plain flavor on clipboard ${otherType}`);
152 ok(!clipboard.hasDataMatchingFlavors(["text/foo"], otherType),
153 `Check if there is text/foo flavor on clipboard ${otherType}`);
155 info(`Write text/plain data to clipboard ${otherType}`);
156 writeRandomStringToClipboard("text/plain", otherType);
160 // Check again.
161 ok(clipboard.hasDataMatchingFlavors(["text/plain"], type),
162 `Check if there is text/plain flavor on clipboard ${type}`);
163 // XXX macOS caches the transferable to implement kSelectionCache type, too,
164 // so it behaves differently than other types.
165 if (
166 navigator.platform.includes("Mac") &&
167 type == clipboard.kSelectionCache &&
168 !aIsSupportGetFromCachedTransferable &&
169 !SpecialPowers.isHeadless
171 todo_is(clipboard.hasDataMatchingFlavors(["text/foo"], type),
172 aIsSupportGetFromCachedTransferable,
173 `Check if there is text/foo flavor on clipboard ${type}`);
174 } else {
175 is(clipboard.hasDataMatchingFlavors(["text/foo"], type),
176 aIsSupportGetFromCachedTransferable,
177 `Check if there is text/foo flavor on clipboard ${type}`);
180 info(`Write text/plain data to clipboard ${type} again`);
181 writeRandomStringToClipboard("text/plain", type);
182 ok(clipboard.hasDataMatchingFlavors(["text/plain"], type),
183 `Check if there is text/plain flavor on clipboard ${type}`);
184 ok(!clipboard.hasDataMatchingFlavors(["text/foo"], type),
185 `Check if there is text/foo flavor on clipboard ${type}`);
187 // Clean clipboard data.
188 cleanupAllClipboard();
191 add_task(async function test_clipboard_asyncGetData() {
192 const testClipboardData = async function(aRequest, aExpectedData) {
193 is(aRequest.flavorList.length, Object.keys(aExpectedData).length, "Check flavorList length");
194 for (const [key, value] of Object.entries(aExpectedData)) {
195 ok(aRequest.flavorList.includes(key), `${key} should be available`);
196 is(await asyncClipboardRequestGetData(aRequest, key), value,
197 `Check ${key} data`);
201 info(`test_clipboard_hasDataMatchingFlavors with pref ` +
202 `${aIsSupportGetFromCachedTransferable ? "enabled" : "disabled"}`);
204 const clipboardData = { "text/plain": generateRandomString() };
205 const trans = generateNewTransferable("text/plain", clipboardData["text/plain"]);
207 info(`Write text/plain data to clipboard ${type}`);
208 clipboard.setData(trans, null, type);
209 await testClipboardData(await asyncGetClipboardData(type), clipboardData);
211 info(`Add text/html data to transferable`);
212 const htmlString = `<div>${generateRandomString()}</div>`;
213 addStringToTransferable("text/html", htmlString, trans);
214 // XXX macOS uses cached transferable to implement kSelectionCache type, too,
215 // so it behaves differently than other types.
216 if (aIsSupportGetFromCachedTransferable ||
217 (type == clipboard.kSelectionCache && !SpecialPowers.isHeadless)) {
218 clipboardData["text/html"] = htmlString;
220 await testClipboardData(await asyncGetClipboardData(type), clipboardData);
222 info(`Should not get the data from other clipboard type`);
223 clipboardTypes.forEach(async function(otherType) {
224 if (otherType != type &&
225 clipboard.isClipboardTypeSupported(otherType)) {
226 info(`Check clipboard type ${otherType}`);
227 await testClipboardData(await asyncGetClipboardData(otherType), {});
231 info(`Check data on clipboard ${type} again`);
232 await testClipboardData(await asyncGetClipboardData(type), clipboardData);
235 add_task(async function test_flavorList_order() {
236 info(`test_flavorList_order with pref ` +
237 `${aIsSupportGetFromCachedTransferable ? "enabled" : "disabled"}`);
239 const trans = generateNewTransferable("text/plain", generateRandomString());
240 addStringToTransferable("text/html", `<div>${generateRandomString()}</div>`, trans);
242 info(`Writedata to clipboard ${type}`);
243 clipboard.setData(trans, null, type);
245 // Read with reverse order.
246 let flavors = trans.flavorsTransferableCanExport().reverse();
247 let request = await asyncGetClipboardData(type, flavors);
248 // XXX Not all clipboard type supports html format, e.g. kFindClipboard
249 // on macOS only support writing text/plain.
250 if (
251 navigator.platform.includes("Mac") &&
252 type == clipboard.kFindClipboard &&
253 !aIsSupportGetFromCachedTransferable &&
254 !SpecialPowers.isHeadless
256 isDeeply(request.flavorList, ["text/plain"], "check flavor orders");
257 } else {
258 isDeeply(request.flavorList, flavors, "check flavor orders");
262 // Test sync set clipboard data.
263 testClipboardCache(type, false, aIsSupportGetFromCachedTransferable);
265 // Test async set clipboard data.
266 testClipboardCache(type, true, aIsSupportGetFromCachedTransferable);
270 // Test not get data from clipboard cache.
271 runClipboardCacheTests(false);
273 // Test get data from clipboard cache.
274 runClipboardCacheTests(true);
276 </script>
277 </body>
278 </html>