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"
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">
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() {
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() {
46 if (AppConstants.platform !=
"macosx") {
47 await SimpleTest.promiseClipboardChange(
"", () =
> {
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() {
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() {
71 if (AppConstants.platform !=
"macosx") {
72 await SimpleTest.promiseClipboardChange(
"", () =
> {
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(
"", () =
> {
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.
93 (items.length ==
1 && !items[
0].types.length)) {
94 ok(true,
"read() read the right thing from empty clipboard");
96 ok(false,
"read() read the wrong thing from empty clipboard");
100 async function testNoContentsReadText() {
101 await SimpleTest.promiseClipboardChange(
"", () =
> {
104 let actual = await navigator.clipboard.readText();
105 is(actual,
"",
"readText() read the right thing from empty clipboard");
110 await SpecialPowers.pushPrefEnv({
"set": [
111 [
"dom.events.asyncClipboard", true],
112 [
"dom.events.asyncClipboard.read", true],
113 [
"dom.events.asyncClipboard.clipboardItem", true],
116 await testReadText();
118 await testWriteText();
121 if (AppConstants.platform !=
"macosx") {
122 await testNoContentsRead();
123 await testNoContentsReadText();
132 <body xmlns=
"http://www.w3.org/1999/xhtml">
135 <div id=
"content" style=
"display: none">