Bug 1472338: part 1) Add Chrome tests for the async Clipboard API. r=NeilDeakin
[gecko.git] / toolkit / content / tests / chrome / test_async_clipboard.xhtml
blobff01e60b82c6c7ef2f39e7f728662d994b4be66b
1 <?xml version="1.0"?>
2 <?xml-stylesheet href="chrome://global/skin" type="text/css"?>
3 <?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css" type="text/css"?>
5 <window title="Async clipboard APIs Test"
6 xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
7 onload="runTest();">
9 <script type="application/javascript"
10 src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
11 <script type="application/javascript"
12 src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"/>
14 <script class="testbody" type="application/javascript">
15 <![CDATA[
17 SimpleTest.waitForExplicitFinish();
19 const { Services } = SpecialPowers.Cu.import("resource://gre/modules/Services.jsm");
20 const { AppConstants } = SpecialPowers.Cu.import("resource://gre/modules/AppConstants.jsm");
21 const { PlacesUtils } = SpecialPowers.Cu.import("resource://gre/modules/PlacesUtils.jsm");
23 // Some of the clipboard code requires reading or writing "text/unicode" when
24 // actually "text/plain" is desired.
25 const kTextUnicodeMimeType = "text/unicode";
27 const kTextPlainMimeType = "text/plain";
29 function clearClipboard() {
30 Services.clipboard.emptyClipboard(Services.clipboard.kGlobalClipboard);
33 async function testRead() {
34 let expected = "x";
35 await SimpleTest.promiseClipboardChange(expected, () => {
36 SpecialPowers.clipboardCopyString(expected);
37 }, kTextUnicodeMimeType);
38 let items = await navigator.clipboard.read();
39 is(items.length, 1, "read() read exactly one item");
40 const actual = await items[0].getType(kTextPlainMimeType).then(blob => blob.text());
41 is(actual, expected, "read() read the right thing");
44 async function testWrite() {
45 // See bug 666254.
46 if (AppConstants.platform != "macosx") {
47 await SimpleTest.promiseClipboardChange("", () => {
48 clearClipboard();
49 });
52 let expected = "x";
53 // eslint-disable-next-line no-undef
54 let item = new ClipboardItem({[kTextPlainMimeType]: expected});
55 await navigator.clipboard.write([item]);
56 let actual = SpecialPowers.getClipboardData(kTextUnicodeMimeType);
57 is(actual, expected, "write() wrote the right thing");
60 async function testReadText() {
61 let expected = "x";
62 await SimpleTest.promiseClipboardChange(expected, () => {
63 SpecialPowers.clipboardCopyString(expected);
64 }, kTextUnicodeMimeType);
65 let actual = await navigator.clipboard.readText();
66 is(actual, expected, "readText() read the right thing");
69 async function testWriteText() {
70 // See bug 666254.
71 if (AppConstants.platform != "macosx") {
72 await SimpleTest.promiseClipboardChange("", () => {
73 clearClipboard();
74 });
77 let expected = "x";
78 await navigator.clipboard.writeText(expected);
79 let actual = SpecialPowers.getClipboardData(kTextUnicodeMimeType);
80 is(actual, expected, "writeText() wrote the right thing");
83 async function testNoContentsRead() {
84 await SimpleTest.promiseClipboardChange("", () => {
85 clearClipboard();
86 });
88 const items = await navigator.clipboard.read();
90 // Bug 1756955: at least on Ubuntu 20.04, clearing the clipboard leads to
91 // one item with no types.
92 if (!items.length ||
93 (items.length == 1 && !items[0].types.length)) {
94 ok(true, "read() read the right thing from empty clipboard");
95 } else {
96 ok(false, "read() read the wrong thing from empty clipboard");
100 async function testNoContentsReadText() {
101 await SimpleTest.promiseClipboardChange("", () => {
102 clearClipboard();
104 let actual = await navigator.clipboard.readText();
105 is(actual, "", "readText() read the right thing from empty clipboard");
108 function runTest() {
109 (async function() {
110 await SpecialPowers.pushPrefEnv({"set": [
111 ["dom.events.asyncClipboard", true],
112 ["dom.events.asyncClipboard.read", true],
113 ["dom.events.asyncClipboard.clipboardItem", true],
114 ]});
115 await testRead();
116 await testReadText();
117 await testWrite();
118 await testWriteText();
120 // See bug 666254.
121 if (AppConstants.platform != "macosx") {
122 await testNoContentsRead();
123 await testNoContentsReadText();
126 SimpleTest.finish();
127 })();
130 </script>
132 <body xmlns="http://www.w3.org/1999/xhtml">
133 <p id="display">
134 </p>
135 <div id="content" style="display: none">
136 </div>
137 <pre id="test">
138 </pre>
139 </body>
141 </window>