summaryrefslogtreecommitdiffstats
path: root/devtools/server/tests/chrome/test_inspector-mutations-events.html
blob: b48952c4d93ee550a594d0c8a2de674ff85cc2df (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1157469
-->
<head>
  <meta charset="utf-8">
  <title>Test for Bug 1157469</title>
  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
  <script type="application/javascript" src="inspector-helpers.js"></script>
  <script type="application/javascript">
"use strict";

window.onload = function() {
  SimpleTest.waitForExplicitFinish();

  const prevPrefValue = Services.prefs.getBoolPref("devtools.chrome.enabled");
  Services.prefs.setBoolPref("devtools.chrome.enabled", true);

  let inspectee = null;
  let inspector = null;
  let walker = null;
  const eventListener1 = function() {};
  const eventListener2 = function() {};
  let eventNode1;
  let eventNode2;
  let eventFront1;
  let eventFront2;

  addAsyncTest(async function setup() {
    info("Setting up inspector and walker actors.");
    const url = document.getElementById("inspectorContent").href;
    const { target, doc } = await attachURL(url);
    inspectee = doc;
    inspector = await target.getFront("inspector");
    walker = inspector.walker;

    runNextTest();
  });

  addAsyncTest(async function setupEventTest() {
    eventNode1 = inspectee.querySelector("#a");
    eventNode2 = inspectee.querySelector("#b");

    eventFront1 = await walker.querySelector(walker.rootNode, "#a");
    eventFront2 = await walker.querySelector(walker.rootNode, "#b");

    runNextTest();
  });

  addAsyncTest(async function testChangeEventListenerOnSingleNode() {
    checkNodesHaveNoEventListener();

    info("add event listener on a single node");
    eventNode1.addEventListener("click", eventListener1);

    let mutations = await waitForMutations();
    is(mutations.length, 1, "one mutation expected");
    is(mutations[0].target, eventFront1, "mutation targets eventFront1");
    is(mutations[0].type, "events", "mutation type is events");
    is(mutations[0].hasEventListeners, true,
       "mutation target should have event listeners");
    is(eventFront1.hasEventListeners, true, "eventFront1 should have event listeners");

    info("remove event listener on a single node");
    eventNode1.removeEventListener("click", eventListener1);

    mutations = await waitForMutations();
    is(mutations.length, 1, "one mutation expected");
    is(mutations[0].target, eventFront1, "mutation targets eventFront1");
    is(mutations[0].type, "events", "mutation type is events");
    is(mutations[0].hasEventListeners, false,
       "mutation target should have no event listeners");
    is(eventFront1.hasEventListeners, false,
       "eventFront1 should have no event listeners");

    info("perform several event listener changes on a single node");
    eventNode1.addEventListener("click", eventListener1);
    eventNode1.addEventListener("click", eventListener2);
    eventNode1.removeEventListener("click", eventListener1);
    eventNode1.removeEventListener("click", eventListener2);

    mutations = await waitForMutations();
    is(mutations.length, 1, "one mutation expected");
    is(mutations[0].target, eventFront1, "mutation targets eventFront1");
    is(mutations[0].type, "events", "mutation type is events");
    is(mutations[0].hasEventListeners, false,
       "no event listener expected on mutation target");
    is(eventFront1.hasEventListeners, false, "no event listener expected on node");

    runNextTest();
  });

  addAsyncTest(async function testChangeEventsOnSeveralNodes() {
    checkNodesHaveNoEventListener();

    info("add event listeners on both nodes");
    eventNode1.addEventListener("click", eventListener1);
    eventNode2.addEventListener("click", eventListener2);

    let mutations = await waitForMutations();
    is(mutations.length, 2, "two mutations expected, one for each modified node");
    // first mutation
    is(mutations[0].target, eventFront1, "first mutation targets eventFront1");
    is(mutations[0].type, "events", "mutation type is events");
    is(mutations[0].hasEventListeners, true,
       "mutation target should have event listeners");
    is(eventFront1.hasEventListeners, true, "eventFront1 should have event listeners");
    // second mutation
    is(mutations[1].target, eventFront2, "second mutation targets eventFront2");
    is(mutations[1].type, "events", "mutation type is events");
    is(mutations[1].hasEventListeners, true,
       "mutation target should have event listeners");
    is(eventFront2.hasEventListeners, true, "eventFront1 should have event listeners");

    info("remove event listeners on both nodes");
    eventNode1.removeEventListener("click", eventListener1);
    eventNode2.removeEventListener("click", eventListener2);

    mutations = await waitForMutations();
    is(mutations.length, 2, "one mutation registered for event listener change");
    // first mutation
    is(mutations[0].target, eventFront1, "first mutation targets eventFront1");
    is(mutations[0].type, "events", "mutation type is events");
    is(mutations[0].hasEventListeners, false,
       "mutation target should have no event listeners");
    is(eventFront1.hasEventListeners, false,
       "eventFront2 should have no event listeners");
    // second mutation
    is(mutations[1].target, eventFront2, "second mutation targets eventFront2");
    is(mutations[1].type, "events", "mutation type is events");
    is(mutations[1].hasEventListeners, false,
       "mutation target should have no event listeners");
    is(eventFront2.hasEventListeners, false,
       "eventFront2 should have no event listeners");

    runNextTest();
  });

  addAsyncTest(async function testRemoveMissingEvent() {
    checkNodesHaveNoEventListener();

    info("try to remove an event listener not previously added");
    eventNode1.removeEventListener("click", eventListener1);

    info("set any attribute on the node to trigger a mutation");
    eventNode1.setAttribute("data-attr", "somevalue");

    const mutations = await waitForMutations();
    is(mutations.length, 1, "expect only one mutation");
    isnot(mutations.type, "events", "mutation type should not be events");

    Services.prefs.setBoolPref("devtools.chrome.enabled", prevPrefValue);
    runNextTest();
  });

  function checkNodesHaveNoEventListener() {
    is(eventFront1.hasEventListeners, false,
       "eventFront1 hasEventListeners should be false");
    is(eventFront2.hasEventListeners, false,
       "eventFront2 hasEventListeners should be false");
  }

  function waitForMutations() {
    return new Promise(resolve => {
      walker.once("mutations", mutations => {
        resolve(mutations);
      });
    });
  }

  runNextTest();
};
  </script>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1157469">Mozilla Bug 1157469</a>
<a id="inspectorContent" target="_blank" href="inspector-traversal-data.html">Test Document</a>
<p id="display"></p>
<div id="content" style="display: none">

</div>
<pre id="test">
</pre>
</body>
</html>