tor-browser

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

pointerevent_support.js (9339B)


      1 var All_Pointer_Events = [
      2  "pointerdown",
      3  "pointerup",
      4  "pointercancel",
      5  "pointermove",
      6  "pointerover",
      7  "pointerout",
      8  "pointerenter",
      9  "pointerleave",
     10  "gotpointercapture",
     11  "lostpointercapture",
     12 ];
     13 
     14 // Check for conformance to PointerEvent interface
     15 // TA: 1.1, 1.2, 1.6, 1.7, 1.8, 1.9, 1.10, 1.11, 1.12, 1.13
     16 function check_PointerEvent(event, testNamePrefix) {
     17  if (testNamePrefix === undefined) {
     18    testNamePrefix = "";
     19  }
     20 
     21  // Use expectedPointerType if set otherwise just use the incoming event pointerType in the test name.
     22  var pointerTestName =
     23    testNamePrefix +
     24    " " +
     25    (expectedPointerType == null ? event.pointerType : expectedPointerType) +
     26    " " +
     27    event.type;
     28 
     29  if (expectedPointerType != null) {
     30    test(function () {
     31      assert_equals(
     32        event.pointerType,
     33        expectedPointerType,
     34        "pointerType should be the one specified in the test page."
     35      );
     36    }, pointerTestName + " event pointerType is correct.");
     37  }
     38 
     39  test(function () {
     40    assert_true(
     41      event instanceof event.target.ownerDocument.defaultView.PointerEvent,
     42      "event is a PointerEvent event"
     43    );
     44  }, pointerTestName + " event is a PointerEvent event");
     45 
     46  // Check attributes for conformance to WebIDL:
     47  // * attribute exists
     48  // * has proper type
     49  // * if the attribute is "readonly", it cannot be changed
     50  // TA: 1.1, 1.2
     51  var idl_type_check = {
     52    long(v) {
     53      return typeof v === "number" && Math.round(v) === v;
     54    },
     55    float(v) {
     56      return typeof v === "number";
     57    },
     58    string(v) {
     59      return typeof v === "string";
     60    },
     61    boolean(v) {
     62      return typeof v === "boolean";
     63    },
     64  };
     65  [
     66    ["readonly", "long", "pointerId"],
     67    ["readonly", "float", "width"],
     68    ["readonly", "float", "height"],
     69    ["readonly", "float", "pressure"],
     70    ["readonly", "long", "tiltX"],
     71    ["readonly", "long", "tiltY"],
     72    ["readonly", "string", "pointerType"],
     73    ["readonly", "boolean", "isPrimary"],
     74    ["readonly", "long", "detail", 0],
     75  ].forEach(function (attr) {
     76    var readonly = attr[0];
     77    var type = attr[1];
     78    var name = attr[2];
     79    var value = attr[3];
     80 
     81    // existence check
     82    test(
     83      function () {
     84        assert_true(
     85          name in event,
     86          name + " attribute in " + event.type + " event"
     87        );
     88      },
     89      pointerTestName + "." + name + " attribute exists"
     90    );
     91 
     92    // readonly check
     93    if (readonly === "readonly") {
     94      test(
     95        function () {
     96          assert_readonly(
     97            event.type,
     98            name,
     99            event.type + "." + name + " cannot be changed"
    100          );
    101        },
    102        pointerTestName + "." + name + " is readonly"
    103      );
    104    }
    105 
    106    // type check
    107    test(
    108      function () {
    109        assert_true(
    110          idl_type_check[type](event[name]),
    111          name + " attribute of type " + type
    112        );
    113      },
    114      pointerTestName +
    115        "." +
    116        name +
    117        " IDL type " +
    118        type +
    119        " (JS type was " +
    120        typeof event[name] +
    121        ")"
    122    );
    123 
    124    // value check if defined
    125    if (value != undefined) {
    126      test(
    127        function () {
    128          assert_equals(event[name], value, name + " attribute value");
    129        },
    130        pointerTestName + "." + name + " value is " + value + "."
    131      );
    132    }
    133  });
    134 
    135  // Check the pressure value
    136  // TA: 1.6, 1.7, 1.8
    137  test(function () {
    138    // TA: 1.6
    139    assert_greater_than_equal(
    140      event.pressure,
    141      0,
    142      "pressure is greater than or equal to 0"
    143    );
    144    assert_less_than_equal(
    145      event.pressure,
    146      1,
    147      "pressure is less than or equal to 1"
    148    );
    149 
    150    if (event.type === "pointerup") {
    151      assert_equals(event.pressure, 0, "pressure is 0 during pointerup");
    152    }
    153 
    154    // TA: 1.7, 1.8
    155    if (event.pointerType === "mouse") {
    156      if (event.buttons === 0) {
    157        assert_equals(
    158          event.pressure,
    159          0,
    160          "pressure is 0 for mouse with no buttons pressed"
    161        );
    162      } else {
    163        assert_equals(
    164          event.pressure,
    165          0.5,
    166          "pressure is 0.5 for mouse with a button pressed"
    167        );
    168      }
    169    }
    170  }, pointerTestName + ".pressure value is valid");
    171 
    172  // Check mouse-specific properties
    173  if (event.pointerType === "mouse") {
    174    // TA: 1.9, 1.10, 1.13
    175    test(function () {
    176      assert_equals(event.width, 1, "width of mouse should be 1");
    177      assert_equals(event.height, 1, "height of mouse should be 1");
    178      assert_equals(event.tiltX, 0, event.type + ".tiltX is 0 for mouse");
    179      assert_equals(event.tiltY, 0, event.type + ".tiltY is 0 for mouse");
    180      assert_true(event.isPrimary, event.type + ".isPrimary is true for mouse");
    181    }, pointerTestName + " properties for pointerType = mouse");
    182    // Check properties for pointers other than mouse
    183  }
    184 }
    185 
    186 function showPointerTypes() {
    187  var complete_notice = document.getElementById("complete-notice");
    188  var pointertype_log = document.getElementById("pointertype-log");
    189  var pointertypes = Object.keys(detected_pointertypes);
    190  pointertype_log.innerHTML = pointertypes.length
    191    ? pointertypes.join(",")
    192    : "(none)";
    193  complete_notice.style.display = "block";
    194 }
    195 
    196 function showLoggedEvents() {
    197  var event_log_elem = document.getElementById("event-log");
    198  event_log_elem.innerHTML = event_log.length ? event_log.join(", ") : "(none)";
    199 
    200  var complete_notice = document.getElementById("complete-notice");
    201  complete_notice.style.display = "block";
    202 }
    203 
    204 function log(msg, el) {
    205  if (++count > 10) {
    206    count = 0;
    207    el.innerHTML = " ";
    208  }
    209  el.innerHTML = msg + "; " + el.innerHTML;
    210 }
    211 
    212 function failOnScroll() {
    213  assert_true(false, "scroll received while shouldn't");
    214 }
    215 
    216 function updateDescriptionNextStep() {
    217  document.getElementById("desc").innerHTML =
    218    "Test Description: Try to scroll text RIGHT.";
    219 }
    220 
    221 function updateDescriptionComplete() {
    222  document.getElementById("desc").innerHTML = "Test Description: Test complete";
    223 }
    224 
    225 function updateDescriptionSecondStepTouchActionElement(
    226  target,
    227  scrollReturnInterval
    228 ) {
    229  window.setTimeout(function () {
    230    objectScroller(target, "up", 0);
    231  }, scrollReturnInterval);
    232  document.getElementById("desc").innerHTML =
    233    "Test Description: Try to scroll element RIGHT moving your outside of the red border";
    234 }
    235 
    236 function updateDescriptionThirdStepTouchActionElement(
    237  target,
    238  scrollReturnInterval,
    239  callback = null
    240 ) {
    241  window.setTimeout(function () {
    242    objectScroller(target, "left", 0);
    243    if (callback) {
    244      callback();
    245    }
    246  }, scrollReturnInterval);
    247  document.getElementById("desc").innerHTML =
    248    "Test Description: Try to scroll element DOWN then RIGHT starting your touch inside of the element. Then tap complete button";
    249 }
    250 
    251 function updateDescriptionFourthStepTouchActionElement(
    252  target,
    253  scrollReturnInterval
    254 ) {
    255  document.getElementById("desc").innerHTML =
    256    "Test Description: Try to scroll element RIGHT starting your touch inside of the element";
    257 }
    258 
    259 function objectScroller(target, direction, value) {
    260  if (direction == "up") {
    261    target.scrollTop = 0;
    262  } else if (direction == "left") {
    263    target.scrollLeft = 0;
    264  }
    265 }
    266 
    267 function sPointerCapture(e) {
    268  try {
    269    target0.setPointerCapture(e.pointerId);
    270  } catch (ex) {}
    271 }
    272 
    273 function rPointerCapture(e) {
    274  try {
    275    captureButton.value = "Set Capture";
    276    target0.releasePointerCapture(e.pointerId);
    277  } catch (ex) {}
    278 }
    279 
    280 var globalPointerEventTest = null;
    281 var expectedPointerType = null;
    282 const ALL_POINTERS = ["mouse", "touch", "pen"];
    283 const HOVERABLE_POINTERS = ["mouse", "pen"];
    284 const NOHOVER_POINTERS = ["touch"];
    285 
    286 function MultiPointerTypeTest(testName, types) {
    287  this.testName = testName;
    288  this.types = types;
    289  this.currentTypeIndex = 0;
    290  this.currentTest = null;
    291  this.createNextTest();
    292 }
    293 
    294 MultiPointerTypeTest.prototype.skip = function () {
    295  var prevTest = this.currentTest;
    296  this.createNextTest();
    297  prevTest.timeout();
    298 };
    299 
    300 MultiPointerTypeTest.prototype.done = function () {
    301  var prevTest = this.currentTest;
    302  this.createNextTest();
    303  if (prevTest != null) {
    304    prevTest.done();
    305  }
    306 };
    307 
    308 MultiPointerTypeTest.prototype.step = function (stepFunction) {
    309  this.currentTest.step(stepFunction);
    310 };
    311 
    312 MultiPointerTypeTest.prototype.createNextTest = function () {
    313  if (this.currentTypeIndex < this.types.length) {
    314    var pointerTypeDescription = document.getElementById(
    315      "pointerTypeDescription"
    316    );
    317    document.getElementById("pointerTypeDescription").innerHTML =
    318      "Follow the test instructions with <span style='color: red'>" +
    319      this.types[this.currentTypeIndex] +
    320      "</span>. If you don't have the device <a href='javascript:;' onclick='globalPointerEventTest.skip()'>skip it</a>.";
    321    this.currentTest = async_test(
    322      this.types[this.currentTypeIndex] + " " + this.testName
    323    );
    324    expectedPointerType = this.types[this.currentTypeIndex];
    325    this.currentTypeIndex++;
    326  } else {
    327    document.getElementById("pointerTypeDescription").innerHTML = "";
    328  }
    329  resetTestState();
    330 };
    331 
    332 function setup_pointerevent_test(testName, supportedPointerTypes) {
    333  return (globalPointerEventTest = new MultiPointerTypeTest(
    334    testName,
    335    supportedPointerTypes
    336  ));
    337 }
    338 
    339 function checkPointerEventType(event) {
    340  assert_equals(
    341    event.pointerType,
    342    expectedPointerType,
    343    "pointerType should be the same as the requested device."
    344  );
    345 }