tor-browser

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

browser_jsterm_screenshot_command_file.js (3937B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 // Test that screenshot command works properly
      5 
      6 "use strict";
      7 
      8 const TEST_URI =
      9  "http://example.com/browser/devtools/client/webconsole/" +
     10  "test/browser/test_jsterm_screenshot_command.html";
     11 
     12 // on some machines, such as macOS, dpr is set to 2. This is expected behavior, however
     13 // to keep tests consistant across OSs we are setting the dpr to 1
     14 const dpr = "--dpr 1";
     15 
     16 add_task(async function () {
     17  const hud = await openNewTabAndConsole(TEST_URI);
     18 
     19  info("wait for the iframes to be loaded");
     20  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], async () => {
     21    await ContentTaskUtils.waitForCondition(
     22      () => content.document.querySelectorAll(".loaded-iframe").length == 2
     23    );
     24  });
     25 
     26  info("Test :screenshot to file");
     27  const file = new FileUtils.File(
     28    PathUtils.join(PathUtils.tempDir, "TestScreenshotFile.png")
     29  );
     30  const command = `:screenshot ${file.path} ${dpr}`;
     31  await executeAndWaitForMessageByType(
     32    hud,
     33    command,
     34    `Saved to ${file.path}`,
     35    ".console-api"
     36  );
     37 
     38  const fileExists = file.exists();
     39  if (!fileExists) {
     40    throw new Error(`${file.path} does not exist`);
     41  }
     42 
     43  ok(fileExists, `Screenshot was saved to ${file.path}`);
     44 
     45  info("Create an image using the downloaded file as source");
     46  const image = new Image();
     47  image.src = PathUtils.toFileURI(file.path);
     48  await once(image, "load");
     49 
     50  // The page has the following structure
     51  // +--------------------------------------------------+
     52  // |        Fixed header [50px tall, red]             |
     53  // +--------------------------------------------------+
     54  // | Same-origin iframe [50px tall, rgb(255, 255, 0)] |
     55  // +--------------------------------------------------+
     56  // |    Remote iframe [50px tall, rgb(0, 255, 255)]   |
     57  // +--------------------------------------------------+
     58  // |  Image  |
     59  // |  100px  |
     60  // |         |
     61  // +---------+
     62 
     63  info("Check that the header is rendered in the screenshot");
     64  checkImageColorAt({
     65    image,
     66    y: 0,
     67    expectedColor: `rgb(255, 0, 0)`,
     68    label:
     69      "The top-left corner has the expected red color, matching the header element",
     70  });
     71 
     72  info("Check that the same-origin iframe is rendered in the screenshot");
     73  checkImageColorAt({
     74    image,
     75    y: 60,
     76    expectedColor: `rgb(255, 255, 0)`,
     77    label: "The same-origin iframe is rendered properly in the screenshot",
     78  });
     79 
     80  info("Check that the remote iframe is rendered in the screenshot");
     81  checkImageColorAt({
     82    image,
     83    y: 110,
     84    expectedColor: `rgb(0, 255, 255)`,
     85    label: "The remote iframe is rendered properly in the screenshot",
     86  });
     87 
     88  info("Test :screenshot to file default filename");
     89  const message = await executeAndWaitForMessageByType(
     90    hud,
     91    `:screenshot ${dpr}`,
     92    `Saved to`,
     93    ".console-api"
     94  );
     95  const date = new Date();
     96  const monthString = (date.getMonth() + 1).toString().padStart(2, "0");
     97  const dayString = date.getDate().toString().padStart(2, "0");
     98  const expectedDateString = `${date.getFullYear()}-${monthString}-${dayString}`;
     99 
    100  const { Downloads } = ChromeUtils.importESModule(
    101    "resource://gre/modules/Downloads.sys.mjs"
    102  );
    103  const screenshotDir = await Downloads.getPreferredScreenshotsDirectory();
    104 
    105  const { renderedDate, filePath } =
    106    /Saved to (?<filePath>.*Screen Shot (?<renderedDate>\d{4}-\d{2}-\d{2}) at \d{2}.\d{2}.\d{2}\.png)/.exec(
    107      message.node.textContent
    108    ).groups;
    109  is(
    110    renderedDate,
    111    expectedDateString,
    112    `Screenshot file has expected default name (full message: ${message.node.textContent})`
    113  );
    114  is(
    115    filePath.startsWith(screenshotDir),
    116    true,
    117    `Screenshot file is saved in default directory`
    118  );
    119 
    120  info("Remove the downloaded screenshot files and cleanup downloads");
    121  await IOUtils.remove(file.path);
    122  await IOUtils.remove(filePath);
    123  await resetDownloads();
    124 });