summaryrefslogtreecommitdiffstats
path: root/widget/tests/clipboard_helper.js
blob: 76ed2aeb378a894f3a871f7e2cdb0304571fe6e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const Cc = SpecialPowers.Cc;
const Ci = SpecialPowers.Ci;
const Cr = SpecialPowers.Cr;
const clipboard = SpecialPowers.Services.clipboard;
const clipboardTypes = [
  clipboard.kGlobalClipboard,
  clipboard.kSelectionClipboard,
  clipboard.kFindClipboard,
  clipboard.kSelectionCache,
];

function emptyClipboardData(aType) {
  // XXX gtk doesn't support emptying clipboard data which is stored from
  // other application (bug 1853884). As a workaround, we set dummy data
  // to the clipboard first to ensure the subsequent emptyClipboard call
  // works.
  if (navigator.platform.includes("Linux")) {
    writeStringToClipboard("foo", "text/plain", aType);
  }

  clipboard.emptyClipboard(aType);
}

function cleanupAllClipboard() {
  clipboardTypes.forEach(function (type) {
    if (clipboard.isClipboardTypeSupported(type)) {
      info(`cleanup clipboard ${type}`);
      emptyClipboardData(type);
    }
  });
}

function generateRandomString() {
  return "random number: " + Math.random();
}

function generateNewTransferable(aFlavor, aStr) {
  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);

  return trans;
}

function addStringToTransferable(aFlavor, aStr, aTrans) {
  aTrans.addDataFlavor(aFlavor);

  let supportsStr = Cc["@mozilla.org/supports-string;1"].createInstance(
    Ci.nsISupportsString
  );
  supportsStr.data = aStr;
  aTrans.setTransferData(aFlavor, supportsStr);
}

function updateStringToTransferable(aFlavor, aStr, aTrans) {
  let supportsStr = Cc["@mozilla.org/supports-string;1"].createInstance(
    Ci.nsISupportsString
  );
  supportsStr.data = aStr;
  aTrans.setTransferData(aFlavor, supportsStr);
}

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);
  // XXX gtk doesn't support get empty text data from clipboard, bug 1852983.
  if (aStr == "" && navigator.platform.includes("Linux")) {
    todo_is(
      getClipboardData(aFlavor, aClipboardType),
      "",
      `Should get empty string on clipboard type ${aClipboardType}`
    );
  } else {
    is(
      getClipboardData(aFlavor, aClipboardType),
      // On Windows, widget adds extra data into HTML clipboard.
      aFlavor == "text/html" && navigator.platform.includes("Win")
        ? `<html><body>\n<!--StartFragment-->${aStr}<!--EndFragment-->\n</body>\n</html>`
        : aStr,
      "ensure clipboard data is set"
    );
  }
}

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,
    SpecialPowers.wrap(window).browsingContext.currentWindowContext
  );

  try {
    var data = SpecialPowers.createBlankObject();
    trans.getTransferData(aFlavor, data);
    return data.value.QueryInterface(SpecialPowers.Ci.nsISupportsString).data;
  } catch (ex) {
    // If the clipboard is empty getTransferData will throw.
    return null;
  }
}

function getClipboardDataSnapshotSync(aClipboardType) {
  return clipboard.getDataSnapshotSync(
    ["text/plain", "text/html", "image/png"],
    aClipboardType
  );
}

function asyncGetClipboardData(
  aClipboardType,
  aFormats = ["text/plain", "text/html", "image/png"]
) {
  return new Promise((resolve, reject) => {
    try {
      clipboard.asyncGetData(
        aFormats,
        aClipboardType,
        null,
        SpecialPowers.Services.scriptSecurityManager.getSystemPrincipal(),
        {
          QueryInterface: SpecialPowers.ChromeUtils.generateQI([
            "nsIAsyncClipboardGetCallback",
          ]),
          // nsIAsyncClipboardGetCallback
          onSuccess: SpecialPowers.wrapCallback(function (
            aAsyncGetClipboardData
          ) {
            resolve(aAsyncGetClipboardData);
          }),
          onError: SpecialPowers.wrapCallback(function (aResult) {
            reject(aResult);
          }),
        }
      );
    } catch (e) {
      ok(false, `asyncGetData should not throw`);
      reject(e);
    }
  });
}

function asyncClipboardRequestGetData(aRequest, aFlavor, aThrows = false) {
  return new Promise((resolve, reject) => {
    var trans = Cc["@mozilla.org/widget/transferable;1"].createInstance(
      Ci.nsITransferable
    );
    trans.init(null);
    trans.addDataFlavor(aFlavor);
    try {
      aRequest.getData(trans, aResult => {
        if (aResult != Cr.NS_OK) {
          reject(aResult);
          return;
        }

        try {
          var data = SpecialPowers.createBlankObject();
          trans.getTransferData(aFlavor, data);
          resolve(data.value.QueryInterface(Ci.nsISupportsString).data);
        } catch (ex) {
          // XXX: should widget set empty string to transferable when there no
          // data in system clipboard?
          resolve("");
        }
      });
      ok(
        !aThrows,
        `nsIAsyncGetClipboardData.getData should ${
          aThrows ? "throw" : "success"
        }`
      );
    } catch (e) {
      ok(
        aThrows,
        `nsIAsyncGetClipboardData.getData should ${
          aThrows ? "throw" : "success"
        }`
      );
      reject(e);
    }
  });
}