summaryrefslogtreecommitdiffstats
path: root/dom/events/test/clipboard/test_async_clipboard.xhtml
blob: 787821efc3ea823d188fcf232015fc5a0ba4c9dd (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
<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin" type="text/css"?>
<?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css" type="text/css"?>

<window title="Async clipboard APIs Test"
        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
        onload="runTest();">

  <script type="application/javascript"
          src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
  <script type="application/javascript"
          src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"/>

<script class="testbody" type="application/javascript">
<![CDATA[

  SimpleTest.waitForExplicitFinish();

  const Services = SpecialPowers.Services;
  const { AppConstants } = SpecialPowers.ChromeUtils.import(
    "resource://gre/modules/AppConstants.jsm"
  );
  const { PlacesUtils } = SpecialPowers.ChromeUtils.import(
    "resource://gre/modules/PlacesUtils.jsm"
  );

  const kTextPlainMimeType = "text/plain";

  function clearClipboard() {
    Services.clipboard.emptyClipboard(Services.clipboard.kGlobalClipboard);
  }

  async function testRead() {
    let expected = "x";
    await SimpleTest.promiseClipboardChange(expected, () => {
      SpecialPowers.clipboardCopyString(expected);
    }, kTextPlainMimeType);
    let items = await navigator.clipboard.read();
    is(items.length, 1, "read() read exactly one item");
    const actual = await items[0].getType(kTextPlainMimeType).then(blob => blob.text());
    is(actual, expected, "read() read the right thing");
  }

  async function testWrite() {
    await SimpleTest.promiseClipboardChange("", () => {
      clearClipboard();
    });

    let expected = "x";
    // eslint-disable-next-line no-undef
    let item = new ClipboardItem({[kTextPlainMimeType]: expected});
    await navigator.clipboard.write([item]);
    let actual = SpecialPowers.getClipboardData(kTextPlainMimeType);
    is(actual, expected, "write() wrote the right thing");
  }

  async function testReadText() {
    let expected = "x";
    await SimpleTest.promiseClipboardChange(expected, () => {
      SpecialPowers.clipboardCopyString(expected);
    }, kTextPlainMimeType);
    let actual = await navigator.clipboard.readText();
    is(actual, expected, "readText() read the right thing");
  }

  async function testWriteText() {
    await SimpleTest.promiseClipboardChange("", () => {
      clearClipboard();
    });

    let expected = "x";
    await navigator.clipboard.writeText(expected);
    let actual = SpecialPowers.getClipboardData(kTextPlainMimeType);
    is(actual, expected, "writeText() wrote the right thing");
  }

  async function testNoContentsRead() {
    await SimpleTest.promiseClipboardChange("", () => {
      clearClipboard();
    });

    const items = await navigator.clipboard.read();

    // Bug 1756955: at least on Ubuntu 20.04, clearing the clipboard leads to
    // one item with no types.
    if (!items.length ||
        (items.length == 1 && !items[0].types.length)) {
      ok(true, "read() read the right thing from empty clipboard");
    } else {
      ok(false, "read() read the wrong thing from empty clipboard");
    }
  }

  async function testNoContentsReadText() {
    await SimpleTest.promiseClipboardChange("", () => {
      clearClipboard();
    });
    let actual = await navigator.clipboard.readText();
    is(actual, "", "readText() read the right thing from empty clipboard");
  }

  function runTest() {
    (async function() {
      await SpecialPowers.pushPrefEnv({"set": [
        ["dom.events.asyncClipboard.clipboardItem", true],
      ]});
      await testRead();
      await testReadText();
      await testWrite();
      await testWriteText();

      await testNoContentsRead();
      await testNoContentsReadText();

      SimpleTest.finish();
    })();
  }
  ]]>
</script>

<body xmlns="http://www.w3.org/1999/xhtml">
<p id="display">
</p>
<div id="content" style="display: none">
</div>
<pre id="test">
</pre>
</body>

</window>