tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

browser_document_command_paste_contextmenu.js (8854B)


      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 function promiseExecCommandPaste(aBrowser) {
     12  return SpecialPowers.spawn(aBrowser, [], () => {
     13    let clipboardData = null;
     14    content.document.addEventListener(
     15      "paste",
     16      e => {
     17        clipboardData = e.clipboardData.getData("text/plain");
     18      },
     19      { once: true }
     20    );
     21 
     22    content.document.notifyUserGestureActivation();
     23    const execCommandResult = Cu.waiveXrays(content.document).execCommand(
     24      "paste"
     25    );
     26 
     27    return { execCommandResult, clipboardData };
     28  });
     29 }
     30 
     31 function execCommandPasteWithoutWait(aBrowser) {
     32  return SpecialPowers.spawn(aBrowser, [], () => {
     33    SpecialPowers.executeSoon(() => {
     34      content.document.notifyUserGestureActivation();
     35      const execCommandResult = Cu.waiveXrays(content.document).execCommand(
     36        "paste"
     37      );
     38    });
     39  });
     40 }
     41 
     42 add_setup(async function () {
     43  await SpecialPowers.pushPrefEnv({
     44    set: [
     45      ["test.events.async.enabled", true],
     46      // Disable the paste contextmenu delay to make the test run faster.
     47      ["security.dialog_enable_delay", 0],
     48    ],
     49  });
     50 });
     51 
     52 kPasteCommandTests.forEach(test => {
     53  describe(test.description, () => {
     54    it("Accepting paste contextmenu", async () => {
     55      info(`Randomized text to avoid overlappings with other tests`);
     56      const clipboardText = await promiseWritingRandomTextToClipboard();
     57 
     58      await BrowserTestUtils.withNewTab(
     59        kContentFileUrl,
     60        async function (aBrowser) {
     61          if (test.setupFn) {
     62            info(`Setup`);
     63            await test.setupFn(aBrowser);
     64          }
     65 
     66          const pasteButtonIsShown = promisePasteButtonIsShown();
     67 
     68          info(`Trigger execCommand("paste")`);
     69          const pasteCommandResult = promiseExecCommandPaste(aBrowser);
     70 
     71          info(`Wait for paste context menu is shown`);
     72          await pasteButtonIsShown;
     73 
     74          info(`Click paste context menu`);
     75          const pasteButtonIsHidden = promisePasteButtonIsHidden();
     76          await promiseClickPasteButton();
     77          await pasteButtonIsHidden;
     78 
     79          const { execCommandResult, clipboardData } = await pasteCommandResult;
     80          ok(execCommandResult, `execCommand("paste") should be succeed`);
     81          is(clipboardData, clipboardText, `Check clipboard data`);
     82 
     83          if (test.additionalCheckFunc) {
     84            info(`Additional checks`);
     85            await test.additionalCheckFunc(aBrowser, clipboardText);
     86          }
     87        }
     88      );
     89    });
     90 
     91    it("Dismissing paste contextmenu", async () => {
     92      info(`Randomized text to avoid overlappings with other tests`);
     93      await promiseWritingRandomTextToClipboard();
     94 
     95      await BrowserTestUtils.withNewTab(
     96        kContentFileUrl,
     97        async function (aBrowser) {
     98          if (test.setupFn) {
     99            info(`Setup`);
    100            await test.setupFn(aBrowser);
    101          }
    102 
    103          const pasteButtonIsShown = promisePasteButtonIsShown();
    104 
    105          info(`Trigger execCommand("paste")`);
    106          const pasteCommandResult = promiseExecCommandPaste(aBrowser);
    107 
    108          info(`Wait for paste context menu is shown`);
    109          await pasteButtonIsShown;
    110 
    111          info(`Dismiss paste context menu`);
    112          const pasteButtonIsHidden = promisePasteButtonIsHidden();
    113          await promiseDismissPasteButton();
    114          await pasteButtonIsHidden;
    115 
    116          const { execCommandResult, clipboardData } = await pasteCommandResult;
    117          ok(!execCommandResult, `execCommand("paste") should not be succeed`);
    118          is(clipboardData, null, `Check clipboard data`);
    119 
    120          if (test.additionalCheckFunc) {
    121            info(`Additional checks`);
    122            await test.additionalCheckFunc(aBrowser, "");
    123          }
    124        }
    125      );
    126    });
    127 
    128    it("Tab close", async () => {
    129      info(`Randomized text to avoid overlappings with other tests`);
    130      await promiseWritingRandomTextToClipboard();
    131 
    132      let pasteButtonIsHidden;
    133      await BrowserTestUtils.withNewTab(
    134        kContentFileUrl,
    135        async function (aBrowser) {
    136          if (test.setupFn) {
    137            info(`Setup`);
    138            await test.setupFn(aBrowser);
    139          }
    140 
    141          const pasteButtonIsShown = promisePasteButtonIsShown();
    142 
    143          info(`Trigger execCommand("paste")`);
    144          execCommandPasteWithoutWait(aBrowser);
    145 
    146          info(`Wait for paste context menu is shown`);
    147          await pasteButtonIsShown;
    148 
    149          pasteButtonIsHidden = promisePasteButtonIsHidden();
    150          info("Close tab");
    151        }
    152      );
    153 
    154      await pasteButtonIsHidden;
    155    });
    156 
    157    it("Tab switch", async () => {
    158      info(`Randomized text to avoid overlappings with other tests`);
    159      await promiseWritingRandomTextToClipboard();
    160 
    161      await BrowserTestUtils.withNewTab(
    162        kContentFileUrl,
    163        async function (aBrowser) {
    164          if (test.setupFn) {
    165            info(`Setup`);
    166            await test.setupFn(aBrowser);
    167          }
    168 
    169          const pasteButtonIsShown = promisePasteButtonIsShown();
    170 
    171          info(`Trigger execCommand("paste")`);
    172          execCommandPasteWithoutWait(aBrowser);
    173 
    174          info(`Wait for paste context menu is shown`);
    175          await pasteButtonIsShown;
    176 
    177          info("Switch tab");
    178          let pasteButtonIsHidden = promisePasteButtonIsHidden();
    179          let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser);
    180 
    181          info(`Wait for paste context menu is hidden`);
    182          await pasteButtonIsHidden;
    183 
    184          BrowserTestUtils.removeTab(tab);
    185        }
    186      );
    187    });
    188 
    189    it("Window switch", async () => {
    190      info(`Randomized text to avoid overlappings with other tests`);
    191      await promiseWritingRandomTextToClipboard();
    192 
    193      await BrowserTestUtils.withNewTab(
    194        kContentFileUrl,
    195        async function (aBrowser) {
    196          if (test.setupFn) {
    197            info(`Setup`);
    198            await test.setupFn(aBrowser);
    199          }
    200 
    201          const pasteButtonIsShown = promisePasteButtonIsShown();
    202 
    203          info(`Trigger execCommand("paste")`);
    204          execCommandPasteWithoutWait(aBrowser);
    205 
    206          info(`Wait for paste context menu is shown`);
    207          await pasteButtonIsShown;
    208 
    209          info("Switch browser window");
    210          let pasteButtonIsHidden = promisePasteButtonIsHidden();
    211          let newWin = await BrowserTestUtils.openNewBrowserWindow();
    212 
    213          info(`Wait for paste context menu is hidden`);
    214          await pasteButtonIsHidden;
    215 
    216          await BrowserTestUtils.closeWindow(newWin);
    217        }
    218      );
    219    });
    220 
    221    it("Tab navigate", async () => {
    222      info(`Randomized text to avoid overlappings with other tests`);
    223      await promiseWritingRandomTextToClipboard();
    224 
    225      await BrowserTestUtils.withNewTab(
    226        kContentFileUrl,
    227        async function (aBrowser) {
    228          if (test.setupFn) {
    229            info(`Setup`);
    230            await test.setupFn(aBrowser);
    231          }
    232 
    233          const pasteButtonIsShown = promisePasteButtonIsShown();
    234 
    235          info(`Trigger execCommand("paste")`);
    236          execCommandPasteWithoutWait(aBrowser);
    237 
    238          info(`Wait for paste context menu is shown`);
    239          await pasteButtonIsShown;
    240 
    241          info("Navigate tab");
    242          let pasteButtonIsHidden = promisePasteButtonIsHidden();
    243          aBrowser.loadURI(Services.io.newURI("https://example.com/"), {
    244            triggeringPrincipal:
    245              Services.scriptSecurityManager.getSystemPrincipal(),
    246          });
    247 
    248          info(`Wait for paste context menu is hidden`);
    249          await pasteButtonIsHidden;
    250        }
    251      );
    252    });
    253 
    254    it("Tab reload", async () => {
    255      info(`Randomized text to avoid overlappings with other tests`);
    256      await promiseWritingRandomTextToClipboard();
    257 
    258      await BrowserTestUtils.withNewTab(
    259        kContentFileUrl,
    260        async function (aBrowser) {
    261          if (test.setupFn) {
    262            info(`Setup`);
    263            await test.setupFn(aBrowser);
    264          }
    265 
    266          const pasteButtonIsShown = promisePasteButtonIsShown();
    267 
    268          info(`Trigger execCommand("paste")`);
    269          execCommandPasteWithoutWait(aBrowser);
    270 
    271          info(`Wait for paste context menu is shown`);
    272          await pasteButtonIsShown;
    273 
    274          info("Reload tab");
    275          let pasteButtonIsHidden = promisePasteButtonIsHidden();
    276          await BrowserTestUtils.reloadTab(gBrowser.selectedTab);
    277 
    278          info(`Wait for paste context menu is hidden`);
    279          await pasteButtonIsHidden;
    280        }
    281      );
    282    });
    283  });
    284 });