Bug 1297539 - Write test for content event 'pasteTransferable' for the remote case. Ensures that EventStateManager dispatches the transferable correctly to TextEditor::PasteTransferable for plain text and HTMLEditor::PasteTransferable for html. r=mrbkap
authorJimmy Wang <jimmyw22@gmail.com>
Tue, 30 Aug 2016 22:02:56 -0400
changeset 312460 6f406d30eddb8ccbe7a15392b7cc583722145765
parent 312459 4e15e5606976b626dba9a577d86098d1ed568492
child 312461 3e28c1a68c2475acd925f233379f8dae8b5ddd7a
push id20447
push userkwierso@gmail.com
push dateFri, 02 Sep 2016 20:36:44 +0000
treeherderfx-team@969397f22187 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmrbkap
bugs1297539
milestone51.0a1
Bug 1297539 - Write test for content event 'pasteTransferable' for the remote case. Ensures that EventStateManager dispatches the transferable correctly to TextEditor::PasteTransferable for plain text and HTMLEditor::PasteTransferable for html. r=mrbkap MozReview-Commit-ID: Hz36piNqX31
browser/base/content/test/general/browser.ini
browser/base/content/test/general/browser_bug1297539.js
--- a/browser/base/content/test/general/browser.ini
+++ b/browser/base/content/test/general/browser.ini
@@ -213,16 +213,18 @@ skip-if = buildapp == 'mulet' # Bug 1066
 skip-if = true # bug 1057615
 [browser_bug563588.js]
 [browser_bug565575.js]
 [browser_bug567306.js]
 subsuite = clipboard
 [browser_bug1261299.js]
 subsuite = clipboard
 skip-if = toolkit != "cocoa" # Because of tests for supporting Service Menu of macOS, bug 1261299
+[browser_bug1297539.js]
+skip-if = toolkit != "cocoa" # Because of tests for supporting pasting from Service Menu of macOS, bug 1297539
 [browser_bug575561.js]
 [browser_bug575830.js]
 [browser_bug577121.js]
 [browser_bug578534.js]
 [browser_bug579872.js]
 [browser_bug580638.js]
 [browser_bug580956.js]
 [browser_bug581242.js]
new file mode 100644
--- /dev/null
+++ b/browser/base/content/test/general/browser_bug1297539.js
@@ -0,0 +1,114 @@
+/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+/**
+ * Test for Bug 1297539
+ * Test that the content event "pasteTransferable"
+ * (mozilla::EventMessage::eContentCommandPasteTransferable)
+ * is handled correctly for plain text and html in the remote case.
+ *
+ * Original test test_bug525389.html for command content event
+ * "pasteTransferable" runs only in the content process.
+ * This doesn't test the remote case.
+ *
+ */
+
+"use strict";
+
+function getLoadContext() {
+  return window.QueryInterface(Ci.nsIInterfaceRequestor)
+               .getInterface(Ci.nsIWebNavigation)
+               .QueryInterface(Ci.nsILoadContext);
+}
+
+function getTransferableFromClipboard(asHTML) {
+  let trans = Cc["@mozilla.org/widget/transferable;1"].
+                    createInstance(Ci.nsITransferable);
+  trans.init(getLoadContext());
+  if (asHTML) {
+    trans.addDataFlavor("text/html");
+  } else {
+    trans.addDataFlavor("text/unicode");
+  }
+  let clip = Cc["@mozilla.org/widget/clipboard;1"].getService(Ci.nsIClipboard);
+  clip.getData(trans, Ci.nsIClipboard.kGlobalClipboard);
+  return trans;
+}
+
+function* cutCurrentSelection(elementQueryString, property, browser) {
+  // Cut the current selection.
+  yield BrowserTestUtils.synthesizeKey("x", {accelKey: true}, browser);
+
+  // The editor should be empty after cut.
+  yield ContentTask.spawn(browser, {elementQueryString, property},
+    function* ({elementQueryString, property}) {
+      let element = content.document.querySelector(elementQueryString);
+      is(element[property], "",
+        `${elementQueryString} should be empty after cut (superkey + x)`);
+    });
+}
+
+// Test that you are able to pasteTransferable for plain text
+// which is handled by TextEditor::PasteTransferable to paste into the editor.
+add_task(function* test_paste_transferable_plain_text()
+{
+  let testPage =
+    'data:text/html,' +
+    '<textarea id="textarea">Write something here</textarea>';
+
+  yield BrowserTestUtils.withNewTab(testPage, function* (browser) {
+    // Select all the content in your editor element.
+    yield BrowserTestUtils.synthesizeMouse("#textarea", 0, 0, {}, browser);
+    yield BrowserTestUtils.synthesizeKey("a", {accelKey: true}, browser);
+
+    yield* cutCurrentSelection("#textarea", "value", browser);
+
+    let trans = getTransferableFromClipboard(false);
+    let DOMWindowUtils = EventUtils._getDOMWindowUtils(window);
+    DOMWindowUtils.sendContentCommandEvent("pasteTransferable", trans);
+
+    yield ContentTask.spawn(browser, null, function* () {
+      let textArea = content.document.querySelector('#textarea');
+      is(textArea.value, "Write something here",
+         "Send content command pasteTransferable successful");
+    });
+  });
+});
+
+// Test that you are able to pasteTransferable for html
+// which is handled by HTMLEditor::PasteTransferable to paste into the editor.
+//
+// On Linux,
+// BrowserTestUtils.synthesizeKey("a", {accelKey: true}, browser);
+// doesn't seem to trigger for contenteditable which is why we use
+// Selection to select the contenteditable contents.
+add_task(function* test_paste_transferable_html()
+{
+  let testPage =
+    'data:text/html,' +
+    '<div contenteditable="true"><b>Bold Text</b><i>italics</i></div>';
+
+  yield BrowserTestUtils.withNewTab(testPage, function* (browser) {
+    // Select all the content in your editor element.
+    yield BrowserTestUtils.synthesizeMouse("div", 0, 0, {}, browser);
+    yield ContentTask.spawn(browser, {}, function* () {
+      let element = content.document.querySelector("div");
+      let selection = content.window.getSelection();
+      selection.selectAllChildren(element);
+    });
+
+    yield* cutCurrentSelection("div", "textContent", browser);
+
+    let trans = getTransferableFromClipboard(true);
+    let DOMWindowUtils = EventUtils._getDOMWindowUtils(window);
+    DOMWindowUtils.sendContentCommandEvent("pasteTransferable", trans);
+
+    yield ContentTask.spawn(browser, null, function* () {
+      let textArea = content.document.querySelector('div');
+      is(textArea.innerHTML, "<b>Bold Text</b><i>italics</i>",
+         "Send content command pasteTransferable successful");
+    });
+  });
+});