summaryrefslogtreecommitdiffstats
path: root/dom/tests/mochitest/webcomponents/test_event_stopping.html
blob: 970e44039fd812b85e2d1cabfdffd3b5cd272228 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=887541
-->
<head>
  <title>Test for event model in web components</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=887541">Bug 887541</a>
<script>

var els = SpecialPowers.Cc["@mozilla.org/eventlistenerservice;1"]
            .getService(SpecialPowers.Ci.nsIEventListenerService);

function eventListener(e) {
  eventChain.push(this);
}

function isEventChain(actual, expected, msg) {
  is(actual.length, expected.length, msg);
  for (var i = 0; i < expected.length; i++) {
    is(actual[i], expected[i], msg + " at " + i);
  }

  if (0 < actual.length) {
    var chain = els.getEventTargetChainFor(actual[0], false); // Events should be dispatched on actual[0].
    ok(expected.length < chain.length, "There should be additional chrome event targets.");
  }
}

/*
 * <div elemOne> ------ <shadow-root shadowOne>
 *                                 |
 *                          <span elemTwo>
 *                                 |
 *                         <span elemThree>
 */

var elemOne = document.createElement("div");
var elemTwo = document.createElement("span");
var elemThree = document.createElement("span");
var shadowOne = elemOne.createShadowRoot();

shadowOne.appendChild(elemTwo);
elemTwo.appendChild(elemThree);

// Test stopping "abort" event.

elemOne.addEventListener("abort", eventListener);
elemTwo.addEventListener("abort", eventListener);
elemThree.addEventListener("abort", eventListener);
shadowOne.addEventListener("abort", eventListener);

var eventChain = [];

var customEvent = new CustomEvent("abort", { "bubbles" : true });
elemThree.dispatchEvent(customEvent);
isEventChain(eventChain, [elemThree, elemTwo, shadowOne], "Test that abort event is stopped at shadow root.");

// Test stopping "error" event.

elemOne.addEventListener("error", eventListener);
elemTwo.addEventListener("error", eventListener);
elemThree.addEventListener("error", eventListener);
shadowOne.addEventListener("error", eventListener);

eventChain = [];

customEvent = new CustomEvent("error", { "bubbles" : true });
elemThree.dispatchEvent(customEvent);
isEventChain(eventChain, [elemThree, elemTwo, shadowOne], "Test that error event is stopped at shadow root.");

// Test stopping "select" event.

elemOne.addEventListener("select", eventListener);
elemTwo.addEventListener("select", eventListener);
elemThree.addEventListener("select", eventListener);
shadowOne.addEventListener("select", eventListener);

eventChain = [];

customEvent = new CustomEvent("select", { "bubbles" : true });
elemThree.dispatchEvent(customEvent);
isEventChain(eventChain, [elemThree, elemTwo, shadowOne], "Test that select event is stopped at shadow root.");

// Test stopping "change" event.

elemOne.addEventListener("change", eventListener);
elemTwo.addEventListener("change", eventListener);
elemThree.addEventListener("change", eventListener);
shadowOne.addEventListener("change", eventListener);

eventChain = [];

customEvent = new CustomEvent("change", { "bubbles" : true });
elemThree.dispatchEvent(customEvent);

// Test stopping "reset" event.

elemOne.addEventListener("reset", eventListener);
elemTwo.addEventListener("reset", eventListener);
elemThree.addEventListener("reset", eventListener);
shadowOne.addEventListener("reset", eventListener);

eventChain = [];

customEvent = new CustomEvent("reset", { "bubbles" : true });
elemThree.dispatchEvent(customEvent);
isEventChain(eventChain, [elemThree, elemTwo, shadowOne], "Test that reset event is stopped at shadow root.");

// Test stopping "load" event.

elemOne.addEventListener("load", eventListener);
elemTwo.addEventListener("load", eventListener);
elemThree.addEventListener("load", eventListener);
shadowOne.addEventListener("load", eventListener);

eventChain = [];

customEvent = new CustomEvent("load", { "bubbles" : true });
elemThree.dispatchEvent(customEvent);
isEventChain(eventChain, [elemThree, elemTwo, shadowOne], "Test that load event is stopped at shadow root.");

// Test stopping "resize" event.

elemOne.addEventListener("resize", eventListener);
elemTwo.addEventListener("resize", eventListener);
elemThree.addEventListener("resize", eventListener);
shadowOne.addEventListener("resize", eventListener);

eventChain = [];

customEvent = new CustomEvent("resize", { "bubbles" : true });
elemThree.dispatchEvent(customEvent);
isEventChain(eventChain, [elemThree, elemTwo, shadowOne], "Test that resize event is stopped at shadow root.");

// Test stopping "scroll" event.

elemOne.addEventListener("scroll", eventListener);
elemTwo.addEventListener("scroll", eventListener);
elemThree.addEventListener("scroll", eventListener);
shadowOne.addEventListener("scroll", eventListener);

eventChain = [];

customEvent = new CustomEvent("scroll", { "bubbles" : true });
elemThree.dispatchEvent(customEvent);
isEventChain(eventChain, [elemThree, elemTwo, shadowOne], "Test that scroll event is stopped at shadow root.");

// Test stopping "selectstart" event.

elemOne.addEventListener("selectstart", eventListener);
elemTwo.addEventListener("selectstart", eventListener);
elemThree.addEventListener("selectstart", eventListener);
shadowOne.addEventListener("selectstart", eventListener);

eventChain = [];

customEvent = new CustomEvent("selectstart", { "bubbles" : true });
elemThree.dispatchEvent(customEvent);
isEventChain(eventChain, [elemThree, elemTwo, shadowOne], "Test that selectstart event is stopped at shadow root.");

</script>
</body>
</html>