tor-browser

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

browser_toolbox_screenshot_tool.js (3814B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 const exampleOrgDocument = `https://example.org/document-builder.sjs`;
      5 const exampleComDocument = `https://example.com/document-builder.sjs`;
      6 
      7 const TEST_URL = `${exampleOrgDocument}?html=
      8  <style>
      9    body {
     10      margin: 0;
     11      height: 10001px;
     12    }
     13    iframe {
     14      height: 50px;
     15      border:none;
     16      display: block;
     17    }
     18  </style>
     19  <iframe
     20    src="${exampleOrgDocument}?html=<body style='margin:0;height:30px;background:rgb(255,0,0)'></body>"
     21    id="same-origin"></iframe>
     22  <iframe
     23    src="${exampleComDocument}?html=<body style='margin:0;height:30px;background:rgb(0,255,0)'></body>"
     24    id="remote"></iframe>`;
     25 
     26 add_task(async function () {
     27  await pushPref("devtools.command-button-screenshot.enabled", true);
     28 
     29  await addTab(TEST_URL);
     30 
     31  info("Open the toolbox");
     32  const toolbox = await gDevTools.showToolboxForTab(gBrowser.selectedTab);
     33 
     34  const onScreenshotDownloaded = waitUntilDownload();
     35  toolbox.doc.querySelector("#command-button-screenshot").click();
     36  const filePath = await onScreenshotDownloaded;
     37 
     38  ok(!!filePath, "The screenshot was taken");
     39 
     40  info("Create an image using the downloaded file as source");
     41  const image = new Image();
     42  const onImageLoad = once(image, "load");
     43  image.src = PathUtils.toFileURI(filePath);
     44  await onImageLoad;
     45 
     46  const dpr = await SpecialPowers.spawn(
     47    gBrowser.selectedBrowser,
     48    [],
     49    () => content.wrappedJSObject.devicePixelRatio
     50  );
     51 
     52  info("Check that the same-origin iframe is rendered in the screenshot");
     53  await checkImageColorAt({
     54    image,
     55    y: 10 * dpr,
     56    expectedColor: `rgb(255, 0, 0)`,
     57    label: "The same-origin iframe is rendered properly in the screenshot",
     58  });
     59 
     60  info("Check that the remote iframe is rendered in the screenshot");
     61  await checkImageColorAt({
     62    image,
     63    y: 60 * dpr,
     64    expectedColor: `rgb(0, 255, 0)`,
     65    label: "The remote iframe is rendered properly in the screenshot",
     66  });
     67 
     68  info(
     69    "Check that a warning message was displayed to indicate the screenshot was truncated"
     70  );
     71  const notificationBox = await waitFor(() =>
     72    toolbox.doc.querySelector(".notificationbox")
     73  );
     74 
     75  const message = notificationBox.querySelector(".notification").textContent;
     76  ok(
     77    message.startsWith("The image was cut off"),
     78    `The warning message is rendered as expected (${message})`
     79  );
     80 
     81  // Remove the downloaded screenshot file
     82  await IOUtils.remove(filePath);
     83 
     84  info(
     85    "Check that taking a screenshot in a private window doesn't appear in the non-private window"
     86  );
     87  const privateWindow = await BrowserTestUtils.openNewBrowserWindow({
     88    private: true,
     89  });
     90  ok(PrivateBrowsingUtils.isWindowPrivate(privateWindow), "window is private");
     91  const privateBrowser = privateWindow.gBrowser;
     92  privateBrowser.selectedTab = BrowserTestUtils.addTab(
     93    privateBrowser,
     94    TEST_URL
     95  );
     96 
     97  info("private tab opened");
     98  ok(
     99    PrivateBrowsingUtils.isBrowserPrivate(privateBrowser.selectedBrowser),
    100    "tab window is private"
    101  );
    102 
    103  const privateToolbox = await gDevTools.showToolboxForTab(
    104    privateBrowser.selectedTab
    105  );
    106 
    107  const onPrivateScreenshotDownloaded = waitUntilDownload({
    108    isWindowPrivate: true,
    109  });
    110  privateToolbox.doc.querySelector("#command-button-screenshot").click();
    111  const privateScreenshotFilePath = await onPrivateScreenshotDownloaded;
    112  ok(
    113    !!privateScreenshotFilePath,
    114    "The screenshot was taken in the private window"
    115  );
    116 
    117  // Remove the downloaded screenshot file
    118  await IOUtils.remove(privateScreenshotFilePath);
    119 
    120  // cleanup the downloads
    121  await resetDownloads();
    122 
    123  const closePromise = BrowserTestUtils.windowClosed(privateWindow);
    124  privateWindow.BrowserCommands.tryToCloseWindow();
    125  await closePromise;
    126 });