summaryrefslogtreecommitdiffstats
path: root/widget/tests/file_test_clipboard.js
blob: 4e720b63830c1e2fc213381093e5a444db44b7d4 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

/* import-globals-from clipboard_helper.js */

"use strict";

function getLoadContext() {
  return SpecialPowers.wrap(window).docShell.QueryInterface(Ci.nsILoadContext);
}

// Get clipboard data to paste.
function paste(clipboard) {
  let trans = Cc["@mozilla.org/widget/transferable;1"].createInstance(
    Ci.nsITransferable
  );
  trans.init(getLoadContext());
  trans.addDataFlavor("text/plain");
  clipboard.getData(
    trans,
    Ci.nsIClipboard.kGlobalClipboard,
    SpecialPowers.wrap(window).browsingContext.currentWindowContext
  );
  let str = SpecialPowers.createBlankObject();
  try {
    trans.getTransferData("text/plain", str);
  } catch (e) {
    str = "";
  }
  if (str) {
    str = str.value.QueryInterface(Ci.nsISupportsString);
    if (str) {
      str = str.data;
    }
  }
  return str;
}

add_setup(function init() {
  cleanupAllClipboard();
});

/* Test for bug 948065 */
add_task(function test_copy() {
  // Test copy.
  const data = "random number: " + Math.random();
  let helper = Cc["@mozilla.org/widget/clipboardhelper;1"].getService(
    Ci.nsIClipboardHelper
  );
  helper.copyString(data);
  is(paste(clipboard), data, "Data was successfully copied.");

  clipboard.emptyClipboard(Ci.nsIClipboard.kGlobalClipboard);
  is(paste(clipboard), "", "Data was successfully cleared.");

  cleanupAllClipboard();
});

/* Tests for bug 1834073 */
clipboardTypes.forEach(function (clipboardType) {
  if (clipboard.isClipboardTypeSupported(clipboardType)) {
    add_task(function test_clipboard_apis() {
      info(`Test clipboard apis for type ${clipboardType}`);

      // Set clipboard data
      let str;
      try {
        str = writeRandomStringToClipboard("text/plain", clipboardType);
      } catch (e) {
        ok(
          false,
          `setData should not throw error for clipboard type ${clipboardType}`
        );
      }

      // Test hasDataMatchingFlavors
      try {
        ok(
          clipboard.hasDataMatchingFlavors(["text/plain"], clipboardType),
          `Test hasDataMatchingFlavors for clipboard type ${clipboardType}`
        );
      } catch (e) {
        ok(
          false,
          `hasDataMatchingFlavors should not throw error for clipboard type ${clipboardType}`
        );
      }

      // Test getData
      try {
        is(
          getClipboardData("text/plain", clipboardType),
          str,
          `Test getData for clipboard type ${clipboardType}`
        );
      } catch (e) {
        ok(
          false,
          `getData should not throw error for clipboard type ${clipboardType}`
        );
      }
    });

    add_task(function test_clipboard_set_empty_string() {
      info(`Test setting empty string to type ${clipboardType}`);

      // Clear clipboard type.
      clipboard.emptyClipboard(clipboardType);
      is(
        getClipboardData("text/plain", clipboardType),
        null,
        `Should get null data on clipboard type ${clipboardType}`
      );
      ok(
        !clipboard.hasDataMatchingFlavors(["text/plain"], clipboardType),
        `Should not have text/plain flavor on clipboard ${clipboardType}`
      );

      // Set text/plain to empty string.
      writeStringToClipboard("", "text/plain", clipboardType);
      // XXX gtk doesn't support get empty text data from clipboard, bug 1852983.
      if (navigator.platform.includes("Linux")) {
        todo_is(
          getClipboardData("text/plain", clipboardType),
          "",
          `Should get empty string on clipboard type ${clipboardType}`
        );
      } else {
        is(
          getClipboardData("text/plain", clipboardType),
          "",
          `Should get empty string on clipboard type ${clipboardType}`
        );
      }
      // XXX android doesn't support setting empty text data to clipboard, bug 1841058.
      if (navigator.userAgent.includes("Android")) {
        todo_is(
          clipboard.hasDataMatchingFlavors(["text/plain"], clipboardType),
          true,
          `Should have text/plain flavor on clipboard ${clipboardType}`
        );
      } else {
        ok(
          clipboard.hasDataMatchingFlavors(["text/plain"], clipboardType),
          `Should have text/plain flavor on clipboard ${clipboardType}`
        );
      }

      // Clear all clipboard data.
      cleanupAllClipboard();
    });

    add_task(function test_unsupport_flavor() {
      try {
        is(
          getClipboardData("foo/bar", clipboardType),
          null,
          `Test getData for clipboard type ${clipboardType}`
        );
      } catch (e) {
        ok(
          false,
          `getData should not throw error for clipboard type ${clipboardType}`
        );
      }
    });
  }
});