tor-browser

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

capture-controller-event-target.https.window.js (1814B)


      1 'use strict';
      2 
      3 const controller = new CaptureController();
      4 const type = 'my-event-type';
      5 const listeners = {};
      6 const listener_count = 10;
      7 for (let i = 0; i < listener_count; i++) {
      8  listeners[i] = {
      9    callback: (event) => {
     10      assert_equals(event.type, type, `Event type sent to listener ${i}`);
     11      listeners[i].execution_count++;
     12    }
     13  };
     14 }
     15 
     16 test(() => {
     17  for (const i in listeners) {
     18    listeners[i].execution_count = 0;
     19    controller.addEventListener(type, listeners[i].callback);
     20  }
     21  controller.dispatchEvent(new Event(type));
     22  for (const i in listeners) {
     23    assert_equals(
     24        listeners[i].execution_count, 1,
     25        `Callback execution count for listener ${i}`);
     26  }
     27 }, 'Registering listeners on CaptureController and dispatching an event.');
     28 
     29 test(() => {
     30  for (const i in listeners) {
     31    listeners[i].execution_count = 0;
     32  }
     33  controller.dispatchEvent(new Event(type));
     34  controller.dispatchEvent(new Event(type));
     35  controller.dispatchEvent(new Event(type));
     36  for (const i in listeners) {
     37    assert_equals(
     38        listeners[i].execution_count, 3,
     39        `Callback execution count for listener ${i}`);
     40  }
     41 }, 'Dispatching an multiple events to CaptureController.');
     42 
     43 test(() => {
     44  for (const i in listeners) {
     45    listeners[i].execution_count = 0;
     46    if (i % 3) {
     47      listeners[i].removed = false;
     48    } else {
     49      listeners[i].removed = true;
     50      controller.removeEventListener(type, listeners[i].callback);
     51    };
     52  }
     53  controller.dispatchEvent(new Event(type));
     54  controller.dispatchEvent(new Event(type));
     55  for (const i in listeners) {
     56    assert_equals(
     57        listeners[i].execution_count, listeners[i].removed ? 0 : 2,
     58        `Callback execution count for listener ${i}`);
     59  }
     60 }, 'Unregistering listeners from CaptureController and dispatching an event.');