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

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

"use strict";

clipboardTypes.forEach(function (type) {
  if (!clipboard.isClipboardTypeSupported(type)) {
    add_task(async function test_clipboard_asyncGetData_not_support() {
      info(`Test asyncGetData request throwing on ${type}`);
      SimpleTest.doesThrow(
        () => clipboard.asyncGetData(["text/plain"], type, {}),
        "Passing unsupported clipboard type should throw"
      );
    });
    return;
  }

  add_task(async function test_clipboard_asyncGetData_throw() {
    info(`Test asyncGetData request throwing on ${type}`);
    SimpleTest.doesThrow(
      () => clipboard.asyncGetData([], type, {}),
      "Passing empty flavor list should throw"
    );

    SimpleTest.doesThrow(
      () => clipboard.asyncGetData(["text/plain"], type, null),
      "Passing no callback should throw"
    );
  });

  add_task(async function test_clipboard_asyncGetData_no_matched_flavor() {
    info(`Test asyncGetData have no matched flavor on ${type}`);
    cleanupAllClipboard();
    is(getClipboardData("text/plain", type), null, "ensure clipboard is empty");

    writeRandomStringToClipboard("text/plain", type);
    let request = await new Promise(resolve => {
      clipboard.asyncGetData(
        ["text/html"],
        type,
        null,
        SpecialPowers.Services.scriptSecurityManager.getSystemPrincipal(),
        {
          QueryInterface: SpecialPowers.ChromeUtils.generateQI([
            "nsIAsyncClipboardGetCallback",
          ]),
          // nsIAsyncClipboardGetCallback
          onSuccess: SpecialPowers.wrapCallback(function (
            aAsyncGetClipboardData
          ) {
            resolve(aAsyncGetClipboardData);
          }),
        }
      );
    });
    isDeeply(request.flavorList, [], "Check flavorList");
  });

  add_task(async function test_empty_data() {
    info(`Test asyncGetData request with empty data on ${type}`);
    cleanupAllClipboard();
    is(getClipboardData("text/plain", type), null, "ensure clipboard is empty");

    let request = await asyncGetClipboardData(type);
    isDeeply(request.flavorList, [], "Check flavorList");
    await asyncClipboardRequestGetData(request, "text/plain", true).catch(
      () => {}
    );
  });

  add_task(async function test_clipboard_asyncGetData_after_write() {
    info(`Test asyncGetData request after write on ${type}`);

    let str = writeRandomStringToClipboard("text/plain", type);
    let request = await asyncGetClipboardData(type);
    isDeeply(request.flavorList, ["text/plain"], "Check flavorList");
    is(
      await asyncClipboardRequestGetData(request, "text/plain"),
      str,
      "Check data"
    );
    ok(request.valid, "request should still be valid");
    // Requesting a flavor that is not in the list should throw error.
    await asyncClipboardRequestGetData(request, "text/html", true).catch(
      () => {}
    );
    ok(request.valid, "request should still be valid");

    // Writing a new data should invalid existing get request.
    str = writeRandomStringToClipboard("text/plain", type);
    await asyncClipboardRequestGetData(request, "text/plain").then(
      () => {
        ok(false, "asyncClipboardRequestGetData should not success");
      },
      e => {
        ok(true, "asyncClipboardRequestGetData should reject");
      }
    );
    ok(!request.valid, "request should no longer be valid");

    info(`check clipboard data again`);
    request = await asyncGetClipboardData(type);
    isDeeply(request.flavorList, ["text/plain"], "Check flavorList");
    is(
      await asyncClipboardRequestGetData(request, "text/plain"),
      str,
      "Check data"
    );

    cleanupAllClipboard();
  });

  add_task(async function test_clipboard_asyncGetData_after_empty() {
    info(`Test asyncGetData request after empty on ${type}`);

    let str = writeRandomStringToClipboard("text/plain", type);
    let request = await asyncGetClipboardData(type);
    isDeeply(request.flavorList, ["text/plain"], "Check flavorList");
    is(
      await asyncClipboardRequestGetData(request, "text/plain"),
      str,
      "Check data"
    );
    ok(request.valid, "request should still be valid");

    // Empty clipboard data
    emptyClipboardData(type);
    is(getClipboardData("text/plain", type), null, "ensure clipboard is empty");

    await asyncClipboardRequestGetData(request, "text/plain").then(
      () => {
        ok(false, "asyncClipboardRequestGetData should not success");
      },
      e => {
        ok(true, "asyncClipboardRequestGetData should reject");
      }
    );
    ok(!request.valid, "request should no longer be valid");

    info(`check clipboard data again`);
    request = await asyncGetClipboardData(type);
    isDeeply(request.flavorList, [], "Check flavorList");

    cleanupAllClipboard();
  });
});

add_task(async function test_html_data() {
  info(`Test asyncGetData request with html data`);

  const html_str = `<img src="https://example.com/oops">`;
  writeStringToClipboard(html_str, "text/html", clipboard.kGlobalClipboard);

  let request = await asyncGetClipboardData(clipboard.kGlobalClipboard);
  isDeeply(request.flavorList, ["text/html"], "Check flavorList");
  is(
    await asyncClipboardRequestGetData(request, "text/html"),
    // On Windows, widget adds extra data into HTML clipboard.
    navigator.platform.includes("Win")
      ? `<html><body>\n<!--StartFragment-->${html_str}<!--EndFragment-->\n</body>\n</html>`
      : html_str,
    "Check data"
  );
  // Requesting a flavor that is not in the list should throw error.
  await asyncClipboardRequestGetData(request, "text/plain", true).catch(
    () => {}
  );
});