Backed out changeset 2450366cf7ca (bug 1891629) for causing win msix mochitest failures
[gecko.git] / widget / tests / file_test_clipboard_asyncSetData.js
blob5eb73f90fae5566503504d6b50b3070a573d48ff
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 clipboardTypes.forEach(function (type) {
9   if (clipboard.isClipboardTypeSupported(type)) {
10     clipboardTypes.forEach(function (otherType) {
11       if (clipboard.isClipboardTypeSupported(otherType)) {
12         [true, false].forEach(async function (async) {
13           add_task(async function test_clipboard_pending_asyncSetData() {
14             info(
15               `Test having a pending asyncSetData request on ${type} and then make a new ${
16                 async ? "asyncSetData" : "setData"
17               } request on ${otherType}`
18             );
20             // Create a pending asyncSetData request
21             let priorResult;
22             let priorRequest;
23             let priorPromise = new Promise(resolve => {
24               priorRequest = clipboard.asyncSetData(type, null, {
25                 QueryInterface: SpecialPowers.ChromeUtils.generateQI([
26                   "nsIAsyncSetClipboardDataCallback",
27                 ]),
28                 onComplete(rv) {
29                   priorResult = rv;
30                   resolve();
31                 },
32               });
33             });
35             // Create a new request
36             let str = writeRandomStringToClipboard(
37               "text/plain",
38               otherType,
39               null,
40               async
41             );
43             if (type === otherType) {
44               info(
45                 "The new request to the same clipboard type should cancel the prior pending request"
46               );
47               await priorPromise;
49               is(
50                 priorResult,
51                 Cr.NS_ERROR_ABORT,
52                 "The pending asyncSetData request should be canceled"
53               );
54               try {
55                 priorRequest.setData(
56                   generateNewTransferable("text/plain", generateRandomString())
57                 );
58                 ok(
59                   false,
60                   "An error should be thrown if setData is called on a canceled clipboard request"
61                 );
62               } catch (e) {
63                 is(
64                   e.result,
65                   Cr.NS_ERROR_FAILURE,
66                   "An error should be thrown if setData is called on a canceled clipboard request"
67                 );
68               }
69             } else {
70               info(
71                 "The new request to the different clipboard type should not cancel the prior pending request"
72               );
73               str = generateRandomString();
74               priorRequest.setData(
75                 generateNewTransferable("text/plain", str),
76                 null
77               );
78               await priorPromise;
80               is(
81                 priorResult,
82                 Cr.NS_OK,
83                 "The pending asyncSetData request should success"
84               );
86               try {
87                 priorRequest.setData(
88                   generateNewTransferable("text/plain", generateRandomString())
89                 );
90                 ok(
91                   false,
92                   "Calling setData multiple times should throw an error"
93                 );
94               } catch (e) {
95                 is(
96                   e.result,
97                   Cr.NS_ERROR_FAILURE,
98                   "Calling setData multiple times should throw an error"
99                 );
100               }
101             }
103             // Test clipboard data.
104             is(
105               getClipboardData("text/plain", type),
106               str,
107               `Test clipboard data for type ${type}`
108             );
110             // Clean clipboard data.
111             cleanupAllClipboard();
112           });
113         });
114       }
115     });
117     add_task(async function test_clipboard_asyncSetData_abort() {
118       info(`Test abort asyncSetData request on ${type}`);
120       // Create a pending asyncSetData request
121       let result;
122       let request = clipboard.asyncSetData(type, null, rv => {
123         result = rv;
124       });
126       // Abort with NS_OK.
127       try {
128         request.abort(Cr.NS_OK);
129         ok(false, "Throw an error when attempting to abort with NS_OK");
130       } catch (e) {
131         is(
132           e.result,
133           Cr.NS_ERROR_FAILURE,
134           "Should throw an error when attempting to abort with NS_OK"
135         );
136       }
137       is(result, undefined, "The asyncSetData request should not be canceled");
139       // Abort with NS_ERROR_ABORT.
140       request.abort(Cr.NS_ERROR_ABORT);
141       is(
142         result,
143         Cr.NS_ERROR_ABORT,
144         "The asyncSetData request should be canceled"
145       );
146       try {
147         request.abort(Cr.NS_ERROR_FAILURE);
148         ok(false, "Throw an error when attempting to abort again");
149       } catch (e) {
150         is(
151           e.result,
152           Cr.NS_ERROR_FAILURE,
153           "Should throw an error when attempting to abort again"
154         );
155       }
156       is(
157         result,
158         Cr.NS_ERROR_ABORT,
159         "The callback should not be notified again"
160       );
162       try {
163         request.setData(
164           generateNewTransferable("text/plain", generateRandomString())
165         );
166         ok(
167           false,
168           "An error should be thrown if setData is called on a canceled clipboard request"
169         );
170       } catch (e) {
171         is(
172           e.result,
173           Cr.NS_ERROR_FAILURE,
174           "An error should be thrown if setData is called on a canceled clipboard request"
175         );
176       }
177     });
178   }