tor-browser

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

browser_markup_copy_image_data.js (2581B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 // Test that image nodes have the "copy data-uri" contextual menu item enabled
      7 // and that clicking it puts the image data into the clipboard
      8 
      9 add_task(async function () {
     10  await addTab(URL_ROOT + "doc_markup_image_and_canvas.html");
     11  const { inspector } = await openInspector();
     12 
     13  await selectNode("div", inspector);
     14  await assertCopyImageDataNotAvailable(inspector);
     15 
     16  await selectNode("img", inspector);
     17  await assertCopyImageDataAvailable(inspector);
     18  const expectedSrc = await getContentPageElementAttribute("img", "src");
     19  await triggerCopyImageUrlAndWaitForClipboard(expectedSrc, inspector);
     20 
     21  await selectNode("canvas", inspector);
     22  await assertCopyImageDataAvailable(inspector);
     23  const expectedURL = await SpecialPowers.spawn(
     24    gBrowser.selectedBrowser,
     25    [],
     26    () => content.document.querySelector(".canvas").toDataURL()
     27  );
     28  await triggerCopyImageUrlAndWaitForClipboard(expectedURL, inspector);
     29 
     30  // Check again that the menu isn't available on the DIV (to make sure our
     31  // menu updating mechanism works)
     32  await selectNode("div", inspector);
     33  await assertCopyImageDataNotAvailable(inspector);
     34 });
     35 
     36 function assertCopyImageDataNotAvailable(inspector) {
     37  const allMenuItems = openContextMenuAndGetAllItems(inspector);
     38  const item = allMenuItems.find(i => i.id === "node-menu-copyimagedatauri");
     39 
     40  ok(item, "The menu item was found in the contextual menu");
     41  ok(item.disabled, "The menu item is disabled");
     42 }
     43 
     44 function assertCopyImageDataAvailable(inspector) {
     45  const allMenuItems = openContextMenuAndGetAllItems(inspector);
     46  const item = allMenuItems.find(i => i.id === "node-menu-copyimagedatauri");
     47 
     48  ok(item, "The menu item was found in the contextual menu");
     49  ok(!item.disabled, "The menu item is enabled");
     50 }
     51 
     52 function triggerCopyImageUrlAndWaitForClipboard(expected, inspector) {
     53  return new Promise(resolve => {
     54    SimpleTest.waitForClipboard(
     55      expected,
     56      () => {
     57        inspector.markup
     58          .getContainer(inspector.selection.nodeFront)
     59          .copyImageDataUri();
     60      },
     61      () => {
     62        ok(
     63          true,
     64          "The clipboard contains the expected value " +
     65            expected.substring(0, 50) +
     66            "..."
     67        );
     68        resolve();
     69      },
     70      () => {
     71        ok(
     72          false,
     73          "The clipboard doesn't contain the expected value " +
     74            expected.substring(0, 50) +
     75            "..."
     76        );
     77        resolve();
     78      }
     79    );
     80  });
     81 }