summaryrefslogtreecommitdiffstats
path: root/widget/tests/test_clipboard_asyncSetData.xhtml
blob: 27f5fad58c565e775b9b50cc43d0e8e094fdeee1 (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
<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="chrome://global/skin"?>
<?xml-stylesheet type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1712122
-->
<window title="Mozilla Bug 1712122"
        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>

<!-- test results are displayed in the html:body -->
<body xmlns="http://www.w3.org/1999/xhtml">
<p id="display"></p>
<div id="content" style="display: none"></div>
<pre id="test"></pre>
</body>

<!-- test code goes here -->
<script class="testbody" type="application/javascript">
<![CDATA[

const clipboard = SpecialPowers.Services.clipboard;
const clipboardTypes = [
  clipboard.kGlobalClipboard,
  clipboard.kSelectionClipboard,
  clipboard.kFindClipboard,
];

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 getClipboardData(aFlavor, aClipboardType) {
  var trans = Cc["@mozilla.org/widget/transferable;1"].createInstance(Ci.nsITransferable);
  trans.init(null);
  trans.addDataFlavor(aFlavor);
  clipboard.getData(trans, aClipboardType);

  var data = {};
  trans.getTransferData(aFlavor, data);
  return data.value.QueryInterface(SpecialPowers.Ci.nsISupportsString).data;
}

function setClipboardData(aFlavor, aStr, aClipboardType, aAsync) {
  let trans = generateNewTransferable(aFlavor, aStr);
  if (aAsync) {
    let request = clipboard.asyncSetData(aClipboardType);
    request.setData(trans, null);
    return;
  }

  clipboard.setData(trans, null, aClipboardType);
}

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

clipboardTypes.forEach(function(type) {
  if (clipboard.isClipboardTypeSupported(type)) {
    clipboardTypes.forEach(function(otherType) {
      if (clipboard.isClipboardTypeSupported(otherType)) {
        [true, false].forEach(function(async) {
          add_task(async function test_clipboard_pending_asyncSetData() {
            info(`Test having a pending asyncSetData request on ${type} and then make a new ${async ? "asyncSetData" : "setData"} request on ${otherType}`);

            // Create a pending asyncSetData request
            let priorResult;
            let priorRequest = clipboard.asyncSetData(type, {
              QueryInterface: ChromeUtils.generateQI(["nsIAsyncSetClipboardDataCallback"]),
              onComplete(rv) {
                priorResult = rv;
              },
            });

            // Create a new request
            let str = generateRandomString();
            setClipboardData("text/plain", str, otherType, async);

            if (type === otherType) {
              info("The new request should cancel the prior pending request");
              is(priorResult, Cr.NS_ERROR_ABORT, "The pending asyncSetData request should be canceled");
              try {
                priorRequest.setData(generateNewTransferable("text/plain", generateRandomString()));
                ok(false, "An error should be thrown if setData is called on a canceled clipboard request");
              } catch (e) {
                is(e.result, Cr.NS_ERROR_FAILURE, "An error should be thrown if setData is called on a canceled clipboard request");
              }
            } else {
              info("The new request should be used to cancel the prior pending request for different clipboard types");
              is(priorResult, undefined, "The pending asyncSetData request should not be canceled");
              str = generateRandomString();
              priorRequest.setData(generateNewTransferable("text/plain", str), null);
              try {
                priorRequest.setData(generateNewTransferable("text/plain", generateRandomString()));
                ok(false, "Calling setData multiple times should throw an error");
              } catch(e) {
                is(e.result, Cr.NS_ERROR_FAILURE, "Calling setData multiple times should throw an error");
              }
            }

            // Test clipboard data.
            is(getClipboardData("text/plain", type), str, `Test clipboard data for type ${type}`);

            // Clean clipboard data.
            cleanupAllClipboard();
          });
        });
      }
    });

    add_task(async function test_clipboard_asyncSetData_abort() {
      info(`Test abort asyncSetData request on ${type}`);

      // Create a pending asyncSetData request
      let result;
      let request = clipboard.asyncSetData(type, {
        QueryInterface: ChromeUtils.generateQI(["nsIAsyncSetClipboardDataCallback"]),
        onComplete(rv) {
          result = rv;
        },
      });

      // Abort with NS_OK.
      try {
        request.abort(Cr.NS_OK);
        ok(false, "Throw an error when attempting to abort with NS_OK");
      } catch(e) {
        is(e.result, Cr.NS_ERROR_FAILURE, "Should throw an error when attempting to abort with NS_OK");
      }
      is(result, undefined, "The asyncSetData request should not be canceled");

      // Abort with NS_ERROR_ABORT.
      request.abort(Cr.NS_ERROR_ABORT);
      is(result, Cr.NS_ERROR_ABORT, "The asyncSetData request should be canceled");
      try {
        request.abort(Cr.NS_ERROR_FAILURE);
        ok(false, "Throw an error when attempting to abort again");
      } catch(e) {
        is(e.result, Cr.NS_ERROR_FAILURE, "Should throw an error when attempting to abort again");
      }
      is(result, Cr.NS_ERROR_ABORT, "The callback should not be notified again");

      try {
        request.setData(generateNewTransferable("text/plain", generateRandomString()));
        ok(false, "An error should be thrown if setData is called on a canceled clipboard request");
      } catch (e) {
        is(e.result, Cr.NS_ERROR_FAILURE, "An error should be thrown if setData is called on a canceled clipboard request");
      }
    });
  }
});

]]>
</script>
</window>