summaryrefslogtreecommitdiffstats
path: root/widget/headless/tests/test_headless_clipboard.js
blob: 24fb6fc50fc26d623a48a5b912fc5931eb139bd8 (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
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
"use strict";

function getString(clipboard) {
  var str = "";

  // Create transferable that will transfer the text.
  var trans = Cc["@mozilla.org/widget/transferable;1"].createInstance(
    Ci.nsITransferable
  );
  trans.init(null);
  trans.addDataFlavor("text/unicode");

  clipboard.getData(trans, Ci.nsIClipboard.kGlobalClipboard);

  try {
    var data = {};
    trans.getTransferData("text/unicode", data);

    if (data) {
      data = data.value.QueryInterface(Ci.nsISupportsString);
      str = data.data;
    }
  } catch (ex) {
    // If the clipboard is empty getTransferData will throw.
  }

  return str;
}

add_task(async function test_clipboard() {
  let clipboard = Services.clipboard;

  // Test copy.
  const data = "random number: " + Math.random();
  let helper = Cc["@mozilla.org/widget/clipboardhelper;1"].getService(
    Ci.nsIClipboardHelper
  );
  helper.copyString(data);
  equal(getString(clipboard), data, "Data was successfully copied.");

  clipboard.emptyClipboard(Ci.nsIClipboard.kGlobalClipboard);
  equal(getString(clipboard), "", "Data was successfully cleared.");
});