browser_document_command_paste_contextmenu_suppression_ext.js (4820B)
1 /* -*- Mode: JavaScript; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 "use strict"; 8 9 const kContentFileUrl = kBaseUrlForContent + "simple_page_ext.html"; 10 11 async function promiseExecCommandPasteFromExtension(aBrowser, aExtension) { 12 await SpecialPowers.spawn(aBrowser, [], async () => { 13 content.document.notifyUserGestureActivation(); 14 }); 15 16 aExtension.sendMessage("paste"); 17 return await aExtension.awaitMessage("result"); 18 } 19 20 add_setup(async function () { 21 await SpecialPowers.pushPrefEnv({ 22 set: [ 23 ["test.events.async.enabled", true], 24 // Disable the paste contextmenu delay to make the test run faster. 25 ["security.dialog_enable_delay", 0], 26 ], 27 }); 28 29 // Paste contextmenu should not be shown during the test. 30 let listener = function (e) { 31 if (e.target.getAttribute("id") == kPasteMenuPopupId) { 32 ok(false, "paste contextmenu should not be shown"); 33 } 34 }; 35 document.addEventListener("popupshown", listener); 36 37 registerCleanupFunction(() => { 38 document.removeEventListener("popupshown", listener); 39 }); 40 }); 41 42 kPasteCommandTests.forEach(test => { 43 describe(test.description, () => { 44 const contentScript = function () { 45 browser.test.onMessage.addListener(async aMsg => { 46 if (aMsg === "paste") { 47 let clipboardData = null; 48 document.addEventListener( 49 "paste", 50 e => { 51 clipboardData = e.clipboardData.getData("text/plain"); 52 }, 53 { once: true } 54 ); 55 56 const execCommandResult = document.execCommand("paste"); 57 browser.test.sendMessage("result", { 58 execCommandResult, 59 clipboardData, 60 }); 61 } 62 }); 63 }; 64 const extensionData = { 65 manifest: { 66 content_scripts: [ 67 { 68 js: ["contentscript.js"], 69 matches: ["https://example.com/*"], 70 }, 71 ], 72 }, 73 files: { 74 "contentscript.js": contentScript, 75 }, 76 }; 77 78 it("Same-origin data", async () => { 79 const clipboardText = "X" + Math.random(); 80 81 info(`Load and start the extension`); 82 const extension = ExtensionTestUtils.loadExtension(extensionData); 83 await extension.startup(); 84 85 await BrowserTestUtils.withNewTab( 86 kContentFileUrl, 87 async function (aBrowser) { 88 if (test.setupFn) { 89 info(`Setup`); 90 await test.setupFn(aBrowser); 91 } 92 93 info(`Write clipboard data`); 94 await SpecialPowers.spawn(aBrowser, [clipboardText], async text => { 95 content.document.notifyUserGestureActivation(); 96 return content.eval(`navigator.clipboard.writeText("${text}");`); 97 }); 98 99 info(`Trigger execCommand("paste") from extension`); 100 const { execCommandResult, clipboardData } = 101 await promiseExecCommandPasteFromExtension(aBrowser, extension); 102 ok(execCommandResult, `execCommand("paste") should be succeed`); 103 is(clipboardData, clipboardText, `Check clipboard data`); 104 105 if (test.additionalCheckFunc) { 106 info(`Additional checks`); 107 await test.additionalCheckFunc(aBrowser, clipboardText); 108 } 109 } 110 ); 111 112 info(`Unload extension`); 113 await extension.unload(); 114 }); 115 116 it("Extension with clipboardRead permission", async () => { 117 info(`Randomized text to avoid overlappings with other tests`); 118 const clipboardText = await promiseWritingRandomTextToClipboard(); 119 120 info(`Load and start the extension`); 121 extensionData.manifest.permissions = ["clipboardRead"]; 122 const extension = ExtensionTestUtils.loadExtension(extensionData); 123 await extension.startup(); 124 125 await BrowserTestUtils.withNewTab( 126 kContentFileUrl, 127 async function (aBrowser) { 128 if (test.setupFn) { 129 info(`Setup`); 130 await test.setupFn(aBrowser); 131 } 132 133 info(`Trigger execCommand("paste") from extension`); 134 const { execCommandResult, clipboardData } = 135 await promiseExecCommandPasteFromExtension(aBrowser, extension); 136 ok(execCommandResult, `execCommand("paste") should be succeed`); 137 is(clipboardData, clipboardText, `Check clipboard data`); 138 139 if (test.additionalCheckFunc) { 140 info(`Additional checks`); 141 await test.additionalCheckFunc(aBrowser, clipboardText); 142 } 143 } 144 ); 145 146 info(`Unload extension`); 147 await extension.unload(); 148 }); 149 }); 150 });