tor-browser

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

browser_navigator_clipboard_readText.js (6814B)


      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 =
     10  kBaseUrlForContent + "simple_navigator_clipboard_readText.html";
     11 
     12 // @param aBrowser browser object of the content tab.
     13 // @param aMultipleReadTextCalls if false, exactly one call is made, two
     14 //                               otherwise.
     15 function promiseClickContentToTriggerClipboardReadText(
     16  aBrowser,
     17  aMultipleReadTextCalls
     18 ) {
     19  return promiseClickContentElement(
     20    aBrowser,
     21    aMultipleReadTextCalls ? "invokeReadTextTwiceId" : "invokeReadTextOnceId"
     22  );
     23 }
     24 
     25 // @param aBrowser browser object of the content tab.
     26 function promiseMutatedReadTextResultFromContentElement(aBrowser) {
     27  return promiseMutatedTextContentFromContentElement(
     28    aBrowser,
     29    "readTextResultId"
     30  );
     31 }
     32 
     33 add_setup(async function () {
     34  await SpecialPowers.pushPrefEnv({
     35    set: [["test.events.async.enabled", true]],
     36  });
     37 });
     38 
     39 add_task(async function test_paste_button_position() {
     40  // Ensure there's text on the clipboard.
     41  await promiseWritingRandomTextToClipboard();
     42 
     43  await BrowserTestUtils.withNewTab(kContentFileUrl, async function (browser) {
     44    const pasteButtonIsShown = promisePasteButtonIsShown();
     45    const coordsOfClickInContentRelativeToScreenInDevicePixels =
     46      await promiseClickContentToTriggerClipboardReadText(browser, false);
     47    info(
     48      "coordsOfClickInContentRelativeToScreenInDevicePixels: " +
     49        coordsOfClickInContentRelativeToScreenInDevicePixels.x +
     50        ", " +
     51        coordsOfClickInContentRelativeToScreenInDevicePixels.y
     52    );
     53 
     54    const pasteButtonCoordsRelativeToScreenInDevicePixels =
     55      await pasteButtonIsShown;
     56    info(
     57      "pasteButtonCoordsRelativeToScreenInDevicePixels: " +
     58        pasteButtonCoordsRelativeToScreenInDevicePixels.x +
     59        ", " +
     60        pasteButtonCoordsRelativeToScreenInDevicePixels.y
     61    );
     62 
     63    const mouseCoordsRelativeToScreenInDevicePixels =
     64      getMouseCoordsRelativeToScreenInDevicePixels();
     65    info(
     66      "mouseCoordsRelativeToScreenInDevicePixels: " +
     67        mouseCoordsRelativeToScreenInDevicePixels.x +
     68        ", " +
     69        mouseCoordsRelativeToScreenInDevicePixels.y
     70    );
     71 
     72    // Asserting not overlapping is important; otherwise, when the
     73    // "Paste" button is shown via a `mousedown` event, the following
     74    // `mouseup` event could accept the "Paste" button unnoticed by the
     75    // user.
     76    ok(
     77      isCloselyLeftOnTopOf(
     78        mouseCoordsRelativeToScreenInDevicePixels,
     79        pasteButtonCoordsRelativeToScreenInDevicePixels
     80      ),
     81      "'Paste' button is closely left on top of the mouse pointer."
     82    );
     83    ok(
     84      isCloselyLeftOnTopOf(
     85        coordsOfClickInContentRelativeToScreenInDevicePixels,
     86        pasteButtonCoordsRelativeToScreenInDevicePixels
     87      ),
     88      "Coords of click in content are closely left on top of the 'Paste' button."
     89    );
     90 
     91    // To avoid disturbing subsequent tests.
     92    const pasteButtonIsHidden = promisePasteButtonIsHidden();
     93    await promiseClickPasteButton();
     94    await pasteButtonIsHidden;
     95  });
     96 });
     97 
     98 add_task(async function test_accepting_paste_button() {
     99  // Randomized text to avoid overlappings with other tests.
    100  const clipboardText = await promiseWritingRandomTextToClipboard();
    101 
    102  await BrowserTestUtils.withNewTab(kContentFileUrl, async function (browser) {
    103    const pasteButtonIsShown = promisePasteButtonIsShown();
    104    await promiseClickContentToTriggerClipboardReadText(browser, false);
    105    await pasteButtonIsShown;
    106    const pasteButtonIsHidden = promisePasteButtonIsHidden();
    107    const mutatedReadTextResultFromContentElement =
    108      promiseMutatedReadTextResultFromContentElement(browser);
    109    await promiseClickPasteButton();
    110    await pasteButtonIsHidden;
    111    await mutatedReadTextResultFromContentElement.then(value => {
    112      is(
    113        value,
    114        "Resolved: " + clipboardText,
    115        "Text returned from `navigator.clipboard.readText()` is as expected."
    116      );
    117    });
    118  });
    119 });
    120 
    121 add_task(async function test_dismissing_paste_button() {
    122  await BrowserTestUtils.withNewTab(kContentFileUrl, async function (browser) {
    123    const pasteButtonIsShown = promisePasteButtonIsShown();
    124    await promiseClickContentToTriggerClipboardReadText(browser, false);
    125    await pasteButtonIsShown;
    126    const pasteButtonIsHidden = promisePasteButtonIsHidden();
    127    const mutatedReadTextResultFromContentElement =
    128      promiseMutatedReadTextResultFromContentElement(browser);
    129    await promiseDismissPasteButton();
    130    await pasteButtonIsHidden;
    131    await mutatedReadTextResultFromContentElement.then(value => {
    132      is(
    133        value,
    134        "Rejected.",
    135        "`navigator.clipboard.readText()` rejected after dismissing the 'Paste' button"
    136      );
    137    });
    138  });
    139 });
    140 
    141 add_task(
    142  async function test_multiple_readText_invocations_for_same_user_activation() {
    143    // Randomized text to avoid overlappings with other tests.
    144    const clipboardText = await promiseWritingRandomTextToClipboard();
    145 
    146    await BrowserTestUtils.withNewTab(
    147      kContentFileUrl,
    148      async function (browser) {
    149        const pasteButtonIsShown = promisePasteButtonIsShown();
    150        await promiseClickContentToTriggerClipboardReadText(browser, true);
    151        await pasteButtonIsShown;
    152        const mutatedReadTextResultFromContentElement =
    153          promiseMutatedReadTextResultFromContentElement(browser);
    154        const pasteButtonIsHidden = promisePasteButtonIsHidden();
    155        await promiseClickPasteButton();
    156        await mutatedReadTextResultFromContentElement.then(value => {
    157          is(
    158            value,
    159            "Resolved 1: " + clipboardText + "; Resolved 2: " + clipboardText,
    160            "Two calls of `navigator.clipboard.read()` both resolved with the expected text."
    161          );
    162        });
    163 
    164        // To avoid disturbing subsequent tests.
    165        await pasteButtonIsHidden;
    166      }
    167    );
    168  }
    169 );
    170 
    171 add_task(async function test_new_user_activation_shows_paste_button_again() {
    172  await BrowserTestUtils.withNewTab(kContentFileUrl, async function (browser) {
    173    // Ensure there's text on the clipboard.
    174    await promiseWritingRandomTextToClipboard();
    175 
    176    for (let i = 0; i < 2; ++i) {
    177      const pasteButtonIsShown = promisePasteButtonIsShown();
    178      // A click initiates a new user activation.
    179      await promiseClickContentToTriggerClipboardReadText(browser, false);
    180      await pasteButtonIsShown;
    181 
    182      const pasteButtonIsHidden = promisePasteButtonIsHidden();
    183      await promiseClickPasteButton();
    184      await pasteButtonIsHidden;
    185    }
    186  });
    187 });