summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/dom/events/remove-all-listeners.html
blob: 3a2a751a146bca0fc30144c997fda4cd16242294 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<!doctype html>
<title>Various edge cases where listeners are removed during iteration</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<div id="log"></div>
<script>

test(function() {
  var type = "foo";
  var target = document.createElement("div");

  var listener1CallCount = 0;
  var listener2CallCount = 0;
  var listener3CallCount = 0;
  function listener1() {
    listener1CallCount++;
    target.removeEventListener(type, listener1);
    target.removeEventListener(type, listener2);
    target.addEventListener(type, listener3);
  }
  function listener2() {
    listener2CallCount++;
  }
  function listener3() {
    listener3CallCount++;
  }

  target.addEventListener(type, listener1);
  target.addEventListener(type, listener2);

  // Dispatch the event. Only listener1 should be called because
  // it removes listener2. And listener3 is added when we've already
  // started iterating, so it shouldn't be called either.
  target.dispatchEvent(new Event(type));
  assert_equals(listener1CallCount, 1);
  assert_equals(listener2CallCount, 0);
  assert_equals(listener3CallCount, 0);

  // Now that only listener3 is set, dispatch another event. Only
  // listener3 should be called.
  target.dispatchEvent(new Event(type));
  assert_equals(listener1CallCount, 1);
  assert_equals(listener2CallCount, 0);
  assert_equals(listener3CallCount, 1);
}, "Removing all listeners and then adding a new one should work.");

test(function() {
  var type = "foo";
  var target = document.createElement("div");

  var listener1CallCount = 0;
  var listener2CallCount = 0;
  var listener3CallCount = 0;
  function listener1() {
    listener1CallCount++;
    // Recursively dispatch another event from this listener.
    // This will only call listener2 because listener1 is a "once" listener.
    target.dispatchEvent(new Event(type));
    assert_equals(listener1CallCount, 1);
    assert_equals(listener2CallCount, 1);
    assert_equals(listener3CallCount, 0);

    // Now all listeners are removed - the two "once" listeners have already both
    // been called once. Add another listener.
    target.addEventListener(type, listener3);
  }
  function listener2() {
    listener2CallCount++;
  }
  function listener3() {
    listener3CallCount++;
  }

  // Add two "once" listeners.
  target.addEventListener(type, listener1, { once: true });
  target.addEventListener(type, listener2, { once: true });

  // Dispatch the event.
  target.dispatchEvent(new Event(type));

  // The listener call counts should still match what they were
  // at the end of listener1.
  assert_equals(listener1CallCount, 1);
  assert_equals(listener2CallCount, 1);
  assert_equals(listener3CallCount, 0);

  // Now that only listener3 is set, dispatch another event. Only
  // listener3 should be called.
  target.dispatchEvent(new Event(type));
  assert_equals(listener1CallCount, 1);
  assert_equals(listener2CallCount, 1);
  assert_equals(listener3CallCount, 1);
}, "Nested usage of once listeners should work.");

</script>