browser_bug1297539.js (4544B)
1 /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ 2 /* This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 5 6 /** 7 * Test for Bug 1297539 8 * Test that the content event "pasteTransferable" 9 * (mozilla::EventMessage::eContentCommandPasteTransferable) 10 * is handled correctly for plain text and html in the remote case. 11 * 12 * Original test test_bug525389.html for command content event 13 * "pasteTransferable" runs only in the content process. 14 * This doesn't test the remote case. 15 * 16 */ 17 18 "use strict"; 19 20 add_setup(async function () { 21 await SpecialPowers.pushPrefEnv({ 22 set: [["test.wait300msAfterTabSwitch", true]], 23 }); 24 }); 25 26 function getLoadContext() { 27 return window.docShell.QueryInterface(Ci.nsILoadContext); 28 } 29 30 function getTransferableFromClipboard(asHTML) { 31 let trans = Cc["@mozilla.org/widget/transferable;1"].createInstance( 32 Ci.nsITransferable 33 ); 34 trans.init(getLoadContext()); 35 if (asHTML) { 36 trans.addDataFlavor("text/html"); 37 } else { 38 trans.addDataFlavor("text/plain"); 39 } 40 Services.clipboard.getData( 41 trans, 42 Ci.nsIClipboard.kGlobalClipboard, 43 SpecialPowers.wrap(window).browsingContext.currentWindowContext 44 ); 45 return trans; 46 } 47 48 async function cutCurrentSelection(elementQueryString, property, browser) { 49 // Cut the current selection. 50 await BrowserTestUtils.synthesizeKey("x", { accelKey: true }, browser); 51 52 // The editor should be empty after cut. 53 await SpecialPowers.spawn( 54 browser, 55 [[elementQueryString, property]], 56 async function ([contentElementQueryString, contentProperty]) { 57 let element = content.document.querySelector(contentElementQueryString); 58 is( 59 element[contentProperty], 60 "", 61 `${contentElementQueryString} should be empty after cut (superkey + x)` 62 ); 63 } 64 ); 65 } 66 67 // Test that you are able to pasteTransferable for plain text 68 // which is handled by TextEditor::PasteTransferable to paste into the editor. 69 add_task(async function test_paste_transferable_plain_text() { 70 let testPage = 71 "data:text/html," + 72 '<textarea id="textarea">Write something here</textarea>'; 73 74 await BrowserTestUtils.withNewTab(testPage, async function (browser) { 75 // Select all the content in your editor element. 76 await BrowserTestUtils.synthesizeMouse("#textarea", 0, 0, {}, browser); 77 await BrowserTestUtils.synthesizeKey("a", { accelKey: true }, browser); 78 79 await cutCurrentSelection("#textarea", "value", browser); 80 81 let trans = getTransferableFromClipboard(false); 82 let DOMWindowUtils = EventUtils._getDOMWindowUtils(window); 83 DOMWindowUtils.sendContentCommandEvent("pasteTransferable", trans); 84 85 await SpecialPowers.spawn(browser, [], async function () { 86 let textArea = content.document.querySelector("#textarea"); 87 is( 88 textArea.value, 89 "Write something here", 90 "Send content command pasteTransferable successful" 91 ); 92 }); 93 }); 94 }); 95 96 // Test that you are able to pasteTransferable for html 97 // which is handled by HTMLEditor::PasteTransferable to paste into the editor. 98 // 99 // On Linux, 100 // BrowserTestUtils.synthesizeKey("a", {accelKey: true}, browser); 101 // doesn't seem to trigger for contenteditable which is why we use 102 // Selection to select the contenteditable contents. 103 add_task(async function test_paste_transferable_html() { 104 let testPage = 105 "data:text/html," + 106 '<div contenteditable="true"><b>Bold Text</b><i>italics</i></div>'; 107 108 await BrowserTestUtils.withNewTab(testPage, async function (browser) { 109 // Select all the content in your editor element. 110 await BrowserTestUtils.synthesizeMouse("div", 0, 0, {}, browser); 111 await SpecialPowers.spawn(browser, [], async function () { 112 let element = content.document.querySelector("div"); 113 let selection = content.window.getSelection(); 114 selection.selectAllChildren(element); 115 }); 116 117 await cutCurrentSelection("div", "textContent", browser); 118 119 let trans = getTransferableFromClipboard(true); 120 let DOMWindowUtils = EventUtils._getDOMWindowUtils(window); 121 DOMWindowUtils.sendContentCommandEvent("pasteTransferable", trans); 122 123 await SpecialPowers.spawn(browser, [], async function () { 124 let textArea = content.document.querySelector("div"); 125 is( 126 textArea.innerHTML, 127 "<b>Bold Text</b><i>italics</i>", 128 "Send content command pasteTransferable successful" 129 ); 130 }); 131 }); 132 });