tor-browser

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

browser_canvasframe_helper_03.js (4073B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 // Test the CanvasFrameAnonymousContentHelper event handling mechanism.
      7 
      8 const TEST_URL =
      9  "data:text/html;charset=utf-8,CanvasFrameAnonymousContentHelper test";
     10 
     11 add_task(async function () {
     12  const tab = await addTab(TEST_URL);
     13  await SpecialPowers.spawn(tab.linkedBrowser, [], async function () {
     14    const { require } = ChromeUtils.importESModule(
     15      "resource://devtools/shared/loader/Loader.sys.mjs"
     16    );
     17    const {
     18      HighlighterEnvironment,
     19    } = require("resource://devtools/server/actors/highlighters.js");
     20    const {
     21      CanvasFrameAnonymousContentHelper,
     22    } = require("resource://devtools/server/actors/highlighters/utils/markup.js");
     23    const doc = content.document;
     24 
     25    const nodeBuilder = () => {
     26      const root = doc.createElement("div");
     27      const child = doc.createElement("div");
     28      child.style =
     29        "pointer-events:auto;width:200px;height:200px;background:red;";
     30      child.id = "child-element";
     31      child.className = "child-element";
     32      root.appendChild(child);
     33      return root;
     34    };
     35 
     36    info("Building the helper");
     37    const env = new HighlighterEnvironment();
     38    env.initFromWindow(doc.defaultView);
     39    const helper = new CanvasFrameAnonymousContentHelper(env, nodeBuilder);
     40    await helper.initialize();
     41 
     42    const el = helper.getElement("child-element");
     43 
     44    info("Adding an event listener on the inserted element");
     45    let mouseDownHandled = 0;
     46    function onMouseDown(e, id) {
     47      is(
     48        id,
     49        "child-element",
     50        "The mousedown event was triggered on the element"
     51      );
     52      ok(!e.originalTarget, "The originalTarget property isn't available");
     53      mouseDownHandled++;
     54    }
     55    el.addEventListener("mousedown", onMouseDown);
     56 
     57    function once(target, event) {
     58      return new Promise(done => {
     59        target.addEventListener(event, done, { once: true });
     60      });
     61    }
     62 
     63    info("Synthesizing an event on the inserted element");
     64    let onDocMouseDown = once(doc, "mousedown");
     65    synthesizeMouseDown(100, 100, doc.defaultView);
     66    await onDocMouseDown;
     67 
     68    is(
     69      mouseDownHandled,
     70      1,
     71      "The mousedown event was handled once on the element"
     72    );
     73 
     74    info("Synthesizing an event somewhere else");
     75    onDocMouseDown = once(doc, "mousedown");
     76    synthesizeMouseDown(400, 400, doc.defaultView);
     77    await onDocMouseDown;
     78 
     79    is(
     80      mouseDownHandled,
     81      1,
     82      "The mousedown event was not handled on the element"
     83    );
     84 
     85    info("Removing the event listener");
     86    el.removeEventListener("mousedown", onMouseDown);
     87 
     88    info("Synthesizing another event after the listener has been removed");
     89    // Using a document event listener to know when the event has been synthesized.
     90    onDocMouseDown = once(doc, "mousedown");
     91    synthesizeMouseDown(100, 100, doc.defaultView);
     92    await onDocMouseDown;
     93 
     94    is(
     95      mouseDownHandled,
     96      1,
     97      "The mousedown event hasn't been handled after the listener was removed"
     98    );
     99 
    100    info("Adding again the event listener");
    101    el.addEventListener("mousedown", onMouseDown);
    102 
    103    info("Destroying the helper");
    104    env.destroy();
    105    helper.destroy();
    106 
    107    info("Synthesizing another event after the helper has been destroyed");
    108    // Using a document event listener to know when the event has been synthesized.
    109    onDocMouseDown = once(doc, "mousedown");
    110    synthesizeMouseDown(100, 100, doc.defaultView);
    111    await onDocMouseDown;
    112 
    113    is(
    114      mouseDownHandled,
    115      1,
    116      "The mousedown event hasn't been handled after the helper was destroyed"
    117    );
    118 
    119    function synthesizeMouseDown(x, y, win) {
    120      // We need to make sure the inserted anonymous content can be targeted by the
    121      // event right after having been inserted, and so we need to force a sync
    122      // reflow.
    123      win.document.documentElement.offsetWidth;
    124      EventUtils.synthesizeMouseAtPoint(x, y, { type: "mousedown" }, win);
    125    }
    126  });
    127 
    128  gBrowser.removeCurrentTab();
    129 });