Bug 1472338 - Part 2: Add chrome tests for navigator.clipboard* APIs, r=enndeakin draft
authorAnny Gakhokidze <agakhokidze@mozilla.com>
Thu, 09 Aug 2018 22:25:33 -0400
changeset 828142 496acc6af58240e8fdf88c0b0ef5739d69af9300
parent 828138 f50cd14ed1de055123e05b184dccab77bb9149f7
push id118643
push userbmo:agakhokidze@mozilla.com
push dateFri, 10 Aug 2018 04:52:51 +0000
reviewersenndeakin
bugs1472338
milestone63.0a1
Bug 1472338 - Part 2: Add chrome tests for navigator.clipboard* APIs, r=enndeakin MozReview-Commit-ID: CXdcUBQsp14
toolkit/content/tests/chrome/chrome.ini
toolkit/content/tests/chrome/test_async_clipboard.xul
--- a/toolkit/content/tests/chrome/chrome.ini
+++ b/toolkit/content/tests/chrome/chrome.ini
@@ -63,16 +63,17 @@ skip-if = (verify && (os == 'win'))
 [test_autocomplete_delayOnPaste.xul]
 subsuite = clipboard
 [test_autocomplete_emphasis.xul]
 [test_autocomplete_with_composition_on_input.html]
 [test_autocomplete_with_composition_on_textbox.xul]
 [test_autocomplete_placehold_last_complete.xul]
 [test_browser_drop.xul]
 [test_bug253481.xul]
+[test_async_clipboard.xul]
 subsuite = clipboard
 [test_bug263683.xul]
 skip-if = debug && (os == 'win' || os == 'linux')
 [test_bug304188.xul]
 [test_bug331215.xul]
 skip-if = os == 'win' && debug # Bug 1339326
 [test_bug360220.xul]
 [test_bug360437.xul]
new file mode 100644
--- /dev/null
+++ b/toolkit/content/tests/chrome/test_async_clipboard.xul
@@ -0,0 +1,124 @@
+<?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();
+  ChromeUtils.import("resource://gre/modules/Services.jsm");
+  ChromeUtils.import("resource://gre/modules/AppConstants.jsm");
+  ChromeUtils.import("resource://gre/modules/PlacesUtils.jsm");
+
+  function clearClipboard() {
+    if (AppConstants.platform == "android") {
+      // On android, this clears the actual system clipboard
+      Services.clipboard.emptyClipboard(Services.clipboard.kGlobalClipboard);
+      return;
+    }
+    // Need to do this hack on other platforms to clear the actual system clipboard
+    let transf = Cc["@mozilla.org/widget/transferable;1"]
+      .createInstance(Ci.nsITransferable);
+    transf.init(null);
+    // Empty transferables may cause crashes, so just add an unknown type.
+    const TYPE = "text/x-moz-place-empty";
+    transf.addDataFlavor(TYPE);
+    transf.setTransferData(TYPE, PlacesUtils.toISupportsString(""), 0);
+    Services.clipboard.setData(transf, null, Services.clipboard.kGlobalClipboard);
+  }
+
+  async function testRead() {
+    let expected = "HI";
+    await SimpleTest.promiseClipboardChange(expected, () => {
+      SpecialPowers.clipboardCopyString(expected);
+    }, "text/unicode");
+    let dt = await navigator.clipboard.read();
+    let actual = dt.getData("text/plain");
+    is(actual, expected, "readText() read the right thing");
+  }
+
+  async function testWrite() {
+    let expected = "HI";
+    let dt = new DataTransfer();
+    dt.items.add(expected, "text/plain");
+    await navigator.clipboard.write(dt);
+    let actual = SpecialPowers.getClipboardData("text/unicode");
+    is(actual, expected, "write() wrote the right thing");
+  }
+
+  async function testReadText() {
+    let expected = "HI";
+    await SimpleTest.promiseClipboardChange(expected, () => {
+      SpecialPowers.clipboardCopyString(expected);
+    }, "text/unicode");
+    let actual = await navigator.clipboard.readText();
+    is(actual, expected, "readText() read the right thing");
+  }
+
+  async function testWriteText() {
+    let expected = "HI";
+    await navigator.clipboard.writeText(expected);
+    let actual = SpecialPowers.getClipboardData("text/unicode");
+    is(actual, expected, "writeText() wrote the right thing");
+  }
+
+  async function testNoContentsRead() {
+    await SimpleTest.promiseClipboardChange("", () => {
+      clearClipboard();
+    }, "text/x-moz-place-empty");
+    let dt = await navigator.clipboard.read();
+    let actual = dt.getData("text/plain");
+    if (dt.items.length == 0 ||
+        (dt.items.length == 1 && dt.items[0].type == "text/plain" &&
+         dt.items[0].kind == "string")) {
+      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();
+    }, "text/x-moz-place-empty");
+    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", true],
+        ["dom.events.asyncClipboard.dataTransfer", true],
+      ]});
+      await testRead();
+      await testWrite();
+      await testReadText();
+      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>