tor-browser

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

mochitest_support_internal.js (3168B)


      1 // This file supports translating W3C tests
      2 // to tests on auto MochiTest system with minimum changes.
      3 // Author: Maksim Lebedev <alessarik@gmail.com>
      4 
      5 const PARENT_ORIGIN = "http://mochi.test:8888/";
      6 
      7 // Since web platform tests don't check pointerId, we have to use some heuristic
      8 // to test them. and thus pointerIds are send to mochitest_support_external.js
      9 // before we start sending synthesized widget events. Here, we avoid using
     10 // default values used in Gecko to insure everything works as expected.
     11 const POINTER_MOUSE_ID = 7;
     12 const POINTER_PEN_ID = 8;
     13 const POINTER_TOUCH_ID = 9; // Extend for multiple touch points if needed.
     14 
     15 // Setup environment.
     16 addListeners(document.getElementById("target0"));
     17 addListeners(document.getElementById("target1"));
     18 
     19 // Setup communication between mochitest_support_external.js.
     20 // Function allows to initialize prerequisites before testing
     21 // and adds some callbacks to support mochitest system.
     22 function resultCallback(aTestObj) {
     23  var message = aTestObj.name + " (";
     24  message += "Get: " + JSON.stringify(aTestObj.status) + ", ";
     25  message += "Expect: " + JSON.stringify(aTestObj.PASS) + ")";
     26  window.opener.postMessage(
     27    {
     28      type: "RESULT",
     29      message,
     30      result: aTestObj.status === aTestObj.PASS,
     31    },
     32    PARENT_ORIGIN
     33  );
     34 }
     35 
     36 add_result_callback(resultCallback);
     37 add_completion_callback(() => {
     38  window.opener.postMessage({ type: "FIN" }, PARENT_ORIGIN);
     39 });
     40 
     41 window.addEventListener("load", () => {
     42  // Start testing.
     43  var startMessage = {
     44    type: "START",
     45    message: {
     46      mouseId: POINTER_MOUSE_ID,
     47      penId: POINTER_PEN_ID,
     48      touchId: POINTER_TOUCH_ID,
     49    },
     50  };
     51  window.opener.postMessage(startMessage, PARENT_ORIGIN);
     52 });
     53 
     54 function addListeners(elem) {
     55  if (!elem) {
     56    return;
     57  }
     58  var All_Events = [
     59    "pointerdown",
     60    "pointerup",
     61    "pointercancel",
     62    "pointermove",
     63    "pointerover",
     64    "pointerout",
     65    "pointerenter",
     66    "pointerleave",
     67    "gotpointercapture",
     68    "lostpointercapture",
     69  ];
     70  All_Events.forEach(function (name) {
     71    elem.addEventListener(name, function (event) {
     72      console.log("(" + event.type + ")-(" + event.pointerType + ")");
     73 
     74      // Perform checks only for trusted events.
     75      if (!event.isTrusted) {
     76        return;
     77      }
     78 
     79      // Compute the desired event.pointerId from event.pointerType.
     80      var pointerId = {
     81        mouse: POINTER_MOUSE_ID,
     82        pen: POINTER_PEN_ID,
     83        touch: POINTER_TOUCH_ID,
     84      }[event.pointerType];
     85 
     86      // Compare the pointerId.
     87      resultCallback({
     88        name: "Mismatched event.pointerId recieved.",
     89        status: event.pointerId,
     90        PASS: pointerId,
     91      });
     92    });
     93  });
     94 }
     95 
     96 // mock the touchScrollInTarget to make the test work.
     97 function touchScrollInTarget() {
     98  return Promise.resolve();
     99 }
    100 
    101 // mock test_driver to make the test work.
    102 function Actions() {}
    103 Actions.prototype = {
    104  addPointer() {
    105    return this;
    106  },
    107  pointerMove() {
    108    return this;
    109  },
    110  pointerDown() {
    111    return this;
    112  },
    113  pause() {
    114    return this;
    115  },
    116  pointerUp() {
    117    return this;
    118  },
    119  send() {
    120    return Promise.resolve();
    121  },
    122 };
    123 const test_driver = {
    124  Actions,
    125 };