tor-browser

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

browser_jsterm_copy_command.js (2390B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 // Tests that the `copy` console helper works as intended.
      5 
      6 "use strict";
      7 
      8 const text =
      9  "Lorem ipsum dolor sit amet, consectetur adipisicing " +
     10  "elit, sed do eiusmod tempor incididunt ut labore et dolore magna " +
     11  "aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco " +
     12  "laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure " +
     13  "dolor in reprehenderit in voluptate velit esse cillum dolore eu " +
     14  "fugiat nulla pariatur. Excepteur sint occaecat cupidatat non " +
     15  "proident, sunt in culpa qui officia deserunt mollit anim id est laborum." +
     16  new Date();
     17 
     18 const id = "select-me";
     19 const TEST_URI = `data:text/html;charset=utf-8,<!DOCTYPE html>
     20 <body>
     21  <div>
     22    <h1>Testing copy command</h1>
     23    <p>This is some example text</p>
     24    <p id="${id}">${text}</p>
     25  </div>
     26  <div><p></p></div>
     27 </body>`;
     28 
     29 add_task(async function () {
     30  const hud = await openNewTabAndConsole(TEST_URI);
     31  const random = Math.random();
     32  const string = "Text: " + random;
     33  const obj = { a: 1, b: "foo", c: random };
     34 
     35  await testCopy(hud, random, random.toString());
     36  await testCopy(hud, JSON.stringify(string), string);
     37  await testCopy(hud, obj.toSource(), JSON.stringify(obj, null, "  "));
     38  await testCopy(
     39    hud,
     40    `{ typedArray: new Float32Array([1, 2, 3]) }`,
     41    `{
     42  "typedArray": {
     43    "0": 1,
     44    "1": 2,
     45    "2": 3
     46  }
     47 }`
     48  );
     49 
     50  const outerHTML = await SpecialPowers.spawn(
     51    gBrowser.selectedBrowser,
     52    [id],
     53    function (elementId) {
     54      return content.document.getElementById(elementId).outerHTML;
     55    }
     56  );
     57  await testCopy(hud, `$("#${id}")`, outerHTML);
     58 });
     59 
     60 add_task(async function () {
     61  const hud = await openNewTabAndConsole(TEST_URI);
     62  await executeAndWaitForErrorMessage(
     63    hud,
     64    "var a = {}; a.b = a; copy(a);",
     65    "`copy` command failed, object can’t be stringified: TypeError: cyclic object value"
     66  );
     67 });
     68 
     69 function testCopy(hud, stringToCopy, expectedResult) {
     70  return waitForClipboardPromise(async () => {
     71    info(`Attempting to copy: "${stringToCopy}"`);
     72    const command = `copy(${stringToCopy})`;
     73    info(`Executing command: "${command}"`);
     74    await executeAndWaitForMessageByType(
     75      hud,
     76      command,
     77      "String was copied to clipboard",
     78      ".console-api"
     79    );
     80  }, expectedResult);
     81 }