tor-browser

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

browser_document_command_copy.js (11050B)


      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 // https://bugzilla.mozilla.org/show_bug.cgi?id=1170911
     12 [true, false].forEach(aPrefValue => {
     13  describe(`dom.allow_cut_copy=${aPrefValue}`, () => {
     14    it("set preference", async () => {
     15      await SpecialPowers.pushPrefEnv({
     16        set: [["dom.allow_cut_copy", aPrefValue]],
     17      });
     18    });
     19 
     20    it(`called from system principal`, () => {
     21      document.clearUserGestureActivation();
     22 
     23      // Test without editing.
     24      ok(
     25        document.queryCommandSupported("copy"),
     26        "Check if the 'copy' command is supported"
     27      );
     28      ok(
     29        document.queryCommandEnabled("copy"),
     30        "Check if the 'copy' command is enabled"
     31      );
     32      ok(
     33        document.execCommand("copy"),
     34        "Check if the 'copy' command is succeed"
     35      );
     36 
     37      // Test with editing.
     38      const textArea = document.createElement("textarea");
     39      document.body.appendChild(textArea);
     40      textArea.textContent = "textarea text";
     41      textArea.setSelectionRange(0, textArea.value.length);
     42      textArea.focus();
     43      ok(
     44        document.queryCommandEnabled("copy"),
     45        "Check if the 'copy' command is enabled when editing"
     46      );
     47      ok(
     48        document.execCommand("copy"),
     49        "Check if the 'copy' command is succeed when editing"
     50      );
     51      textArea.remove();
     52    });
     53 
     54    it(`called from web content`, async () => {
     55      await BrowserTestUtils.withNewTab(kContentFileUrl, async browser => {
     56        await SpecialPowers.spawn(browser, [aPrefValue], async aPrefValue => {
     57          const doc = Cu.waiveXrays(content.document);
     58          is(
     59            doc.queryCommandSupported("copy"),
     60            aPrefValue,
     61            `Check if the 'copy' command is supported`
     62          );
     63 
     64          // https://bugzilla.mozilla.org/show_bug.cgi?id=1012662
     65          // Test no user activation.
     66          content.document.clearUserGestureActivation();
     67          ok(
     68            !doc.queryCommandEnabled("copy"),
     69            "Check if the 'copy' command is enabled without user activation"
     70          );
     71          ok(
     72            !doc.execCommand("copy"),
     73            "Check if the 'copy' command is succeed without user activation"
     74          );
     75 
     76          // Test with user activation.
     77          content.document.notifyUserGestureActivation();
     78          is(
     79            doc.queryCommandEnabled("copy"),
     80            aPrefValue,
     81            "Check if the 'copy' command is enabled with user activation"
     82          );
     83          is(
     84            doc.execCommand("copy"),
     85            aPrefValue,
     86            "Check if the 'copy' command is succeed with user activation"
     87          );
     88 
     89          // Test with editing.
     90          const textArea = content.document.createElement("textarea");
     91          content.document.body.appendChild(textArea);
     92          textArea.textContent = "textarea text";
     93          textArea.setSelectionRange(0, textArea.value.length);
     94          textArea.focus();
     95 
     96          // Test no user activation.
     97          content.document.clearUserGestureActivation();
     98          ok(
     99            !doc.queryCommandEnabled("copy"),
    100            "Check if the 'copy' command is enabled without user activation when editing"
    101          );
    102          ok(
    103            !doc.execCommand("copy"),
    104            "Check if the 'copy' command is succeed without user activation when editing"
    105          );
    106 
    107          // Test with user activation.
    108          content.document.notifyUserGestureActivation();
    109          is(
    110            doc.queryCommandEnabled("copy"),
    111            aPrefValue,
    112            "Check if the 'copy' command is enabled with user activation when editing"
    113          );
    114          is(
    115            doc.execCommand("copy"),
    116            aPrefValue,
    117            "Check if the 'copy' command is succeed with user activation when editing"
    118          );
    119        });
    120      });
    121    });
    122 
    123    [true, false].forEach(aPermission => {
    124      describe(`extension ${aPermission ? "with" : "without"} clipboardWrite permission`, () => {
    125        const sharedScript = function () {
    126          this.testCopyCommand = function () {
    127            return [
    128              document.queryCommandSupported("copy"),
    129              document.queryCommandEnabled("copy"),
    130              document.execCommand("copy"),
    131            ];
    132          };
    133        };
    134 
    135        it("called from content script", async () => {
    136          const contentScript = function () {
    137            document
    138              .querySelector("button")
    139              .addEventListener("click", function (e) {
    140                browser.test.sendMessage("result", testCopyCommand());
    141              });
    142            browser.test.sendMessage("ready", testCopyCommand());
    143          };
    144          const extensionData = {
    145            manifest: {
    146              content_scripts: [
    147                {
    148                  js: ["sharedScript.js", "contentscript.js"],
    149                  matches: ["https://example.com/*"],
    150                },
    151              ],
    152            },
    153            files: {
    154              "sharedScript.js": sharedScript,
    155              "contentscript.js": contentScript,
    156            },
    157          };
    158          if (aPermission) {
    159            extensionData.manifest.permissions = ["clipboardWrite"];
    160          }
    161 
    162          // Load and start the extension.
    163          const extension = ExtensionTestUtils.loadExtension(extensionData);
    164          await extension.startup();
    165          await BrowserTestUtils.withNewTab(kContentFileUrl, async browser => {
    166            let [supported, enabled, succeed] =
    167              await extension.awaitMessage("ready");
    168            is(
    169              supported,
    170              aPrefValue || aPermission,
    171              "Check if the 'copy' command is supported"
    172            );
    173            is(
    174              enabled,
    175              aPermission,
    176              "Check if the 'copy' command is enabled without user activation"
    177            );
    178            is(
    179              succeed,
    180              aPermission,
    181              "Check if the 'copy' command is succeed without user activation"
    182            );
    183 
    184            // Click on the content to trigger user activation.
    185            promiseClickContentElement(browser, "btn");
    186            [supported, enabled, succeed] =
    187              await extension.awaitMessage("result");
    188            is(
    189              enabled,
    190              aPrefValue || aPermission,
    191              "Check if the 'copy' command is enabled with user activation"
    192            );
    193            is(
    194              succeed,
    195              aPrefValue || aPermission,
    196              "Check if the 'copy' command is succeed with user activation"
    197            );
    198          });
    199          await extension.unload();
    200        });
    201 
    202        it("called from content script when editing", async () => {
    203          const contentScript = function () {
    204            const textArea = document.createElement("textarea");
    205            document.body.appendChild(textArea);
    206            const testCopyCommandWhenEditing = function () {
    207              // Start editing.
    208              textArea.textContent = "textarea text";
    209              textArea.setSelectionRange(0, textArea.value.length);
    210              textArea.focus();
    211              return testCopyCommand();
    212            };
    213            document
    214              .querySelector("button")
    215              .addEventListener("click", function (e) {
    216                browser.test.sendMessage(
    217                  "result",
    218                  testCopyCommandWhenEditing()
    219                );
    220              });
    221            browser.test.sendMessage("ready", testCopyCommandWhenEditing());
    222          };
    223          const extensionData = {
    224            manifest: {
    225              content_scripts: [
    226                {
    227                  js: ["sharedScript.js", "contentscript.js"],
    228                  matches: ["https://example.com/*"],
    229                },
    230              ],
    231            },
    232            files: {
    233              "sharedScript.js": sharedScript,
    234              "contentscript.js": contentScript,
    235            },
    236          };
    237          if (aPermission) {
    238            extensionData.manifest.permissions = ["clipboardWrite"];
    239          }
    240 
    241          // Load and start the extension.
    242          const extension = ExtensionTestUtils.loadExtension(extensionData);
    243          await extension.startup();
    244          await BrowserTestUtils.withNewTab(kContentFileUrl, async browser => {
    245            let [supported, enabled, succeed] =
    246              await extension.awaitMessage("ready");
    247            is(
    248              supported,
    249              aPrefValue || aPermission,
    250              "Check if the 'copy' command is supported"
    251            );
    252            is(
    253              enabled,
    254              aPermission,
    255              "Check if the 'copy' command is enabled without user activation"
    256            );
    257            is(
    258              succeed,
    259              aPermission,
    260              "Check if the 'copy' command is succeed without user activation"
    261            );
    262 
    263            // Click on the content to trigger user activation.
    264            promiseClickContentElement(browser, "btn");
    265            [supported, enabled, succeed] =
    266              await extension.awaitMessage("result");
    267            is(
    268              enabled,
    269              aPrefValue || aPermission,
    270              "Check if the 'copy' command is enabled with user activation"
    271            );
    272            is(
    273              succeed,
    274              aPrefValue || aPermission,
    275              "Check if the 'copy' command is succeed with user activation"
    276            );
    277          });
    278          await extension.unload();
    279        });
    280 
    281        it("called from background script", async () => {
    282          const backgroundScript = function () {
    283            browser.test.sendMessage("ready", testCopyCommand());
    284          };
    285          const extensionData = {
    286            background: [sharedScript, backgroundScript],
    287          };
    288          if (aPermission) {
    289            extensionData.manifest = {
    290              permissions: ["clipboardWrite"],
    291            };
    292          }
    293 
    294          // Load and start the extension.
    295          const extension = ExtensionTestUtils.loadExtension(extensionData);
    296          await extension.startup();
    297          await BrowserTestUtils.withNewTab(kContentFileUrl, async browser => {
    298            let [supported, enabled, succeed] =
    299              await extension.awaitMessage("ready");
    300            is(
    301              supported,
    302              aPrefValue || aPermission,
    303              "Check if the 'copy' command is supported"
    304            );
    305            is(enabled, aPermission, "Check if the 'copy' command is enabled");
    306            is(succeed, aPermission, "Check if the 'copy' command is succeed");
    307          });
    308          await extension.unload();
    309        });
    310      });
    311    });
    312  });
    313 });