summaryrefslogtreecommitdiffstats
path: root/devtools/client/inspector/markup/test/browser_markup_events_toggle.js
blob: 5e1e4372984fa1619a045dc687ac230ad165c6e8 (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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/* Any copyright is dedicated to the Public Domain.
 http://creativecommons.org/publicdomain/zero/1.0/ */
/* import-globals-from helper_events_test_runner.js */

"use strict";

// Test that event listeners can be disabled and re-enabled from the markup view event bubble.

const TEST_URL = URL_ROOT_SSL + "doc_markup_events_toggle.html";

loadHelperScript("helper_events_test_runner.js");

add_task(async function () {
  const { inspector, toolbox } = await openInspectorForURL(TEST_URL);
  const { resourceCommand } = toolbox.commands;
  await inspector.markup.expandAll();
  await selectNode("#target", inspector);

  info(
    "Click on the target element to make sure the event listeners are properly set"
  );
  // There's a "mouseup" event listener that is `console.info` (so we can check "native" events).
  // In order to know if it was called, we listen for the next console.info resource.
  let { onResource: onConsoleInfoMessage } =
    await resourceCommand.waitForNextResource(
      resourceCommand.TYPES.CONSOLE_MESSAGE,
      {
        ignoreExistingResources: true,
        predicate(resource) {
          return resource.message.level == "info";
        },
      }
    );
  await safeSynthesizeMouseEventAtCenterInContentPage("#target");

  let data = await getTargetElementHandledEventData();
  is(data.click, 1, `target handled one "click" event`);
  is(data.mousedown, 1, `target handled one "mousedown" event`);
  await onConsoleInfoMessage;
  ok(true, `the "mouseup" event listener (console.info) was called`);

  info("Check that the event tooltip has the expected content");
  const container = await getContainerForSelector("#target", inspector);
  const eventTooltipBadge = container.elt.querySelector(
    ".inspector-badge.interactive[data-event]"
  );
  ok(eventTooltipBadge, "The event tooltip badge is displayed");

  const tooltip = inspector.markup.eventDetailsTooltip;
  let onTooltipShown = tooltip.once("shown");
  eventTooltipBadge.click();
  await onTooltipShown;
  ok(true, "The tooltip is shown");

  Assert.deepEqual(
    getAsciiHeadersViz(tooltip),
    ["click [x]", "mousedown [x]", "mouseup [x]"],
    "The expected events are displayed, all enabled"
  );
  ok(
    !eventTooltipBadge.classList.contains("has-disabled-events"),
    "The event badge does not have the has-disabled-events class"
  );

  const [clickHeader, mousedownHeader, mouseupHeader] =
    getHeadersInEventTooltip(tooltip);

  info("Uncheck the mousedown event checkbox");
  await toggleEventListenerCheckbox(tooltip, mousedownHeader);
  Assert.deepEqual(
    getAsciiHeadersViz(tooltip),
    ["click [x]", "mousedown []", "mouseup [x]"],
    "mousedown checkbox was unchecked"
  );
  ok(
    eventTooltipBadge.classList.contains("has-disabled-events"),
    "Unchecking an event applied the has-disabled-events class to the badge"
  );
  await safeSynthesizeMouseEventAtCenterInContentPage("#target");
  data = await getTargetElementHandledEventData();
  is(data.click, 2, `target handled another "click" event…`);
  is(data.mousedown, 1, `… but not a mousedown one`);

  info(
    "Check that the event badge style is reset when re-enabling all disabled events"
  );
  await toggleEventListenerCheckbox(tooltip, mousedownHeader);
  Assert.deepEqual(
    getAsciiHeadersViz(tooltip),
    ["click [x]", "mousedown [x]", "mouseup [x]"],
    "mousedown checkbox is checked again"
  );
  ok(
    !eventTooltipBadge.classList.contains("has-disabled-events"),
    "The event badge does not have the has-disabled-events class after re-enabling disabled event"
  );
  info("Disable mousedown again for the rest of the test");
  await toggleEventListenerCheckbox(tooltip, mousedownHeader);
  Assert.deepEqual(
    getAsciiHeadersViz(tooltip),
    ["click [x]", "mousedown []", "mouseup [x]"],
    "mousedown checkbox is unchecked again"
  );

  info("Uncheck the click event checkbox");
  await toggleEventListenerCheckbox(tooltip, clickHeader);
  Assert.deepEqual(
    getAsciiHeadersViz(tooltip),
    ["click []", "mousedown []", "mouseup [x]"],
    "click checkbox was unchecked"
  );
  ok(
    eventTooltipBadge.classList.contains("has-disabled-events"),
    "event badge still has the has-disabled-events class"
  );
  await safeSynthesizeMouseEventAtCenterInContentPage("#target");
  data = await getTargetElementHandledEventData();
  is(data.click, 2, `click event listener was disabled`);
  is(data.mousedown, 1, `and mousedown still is disabled as well`);

  info("Uncheck the mouseup event checkbox");
  await toggleEventListenerCheckbox(tooltip, mouseupHeader);
  Assert.deepEqual(
    getAsciiHeadersViz(tooltip),
    ["click []", "mousedown []", "mouseup []"],
    "mouseup checkbox was unchecked"
  );

  ({ onResource: onConsoleInfoMessage } =
    await resourceCommand.waitForNextResource(
      resourceCommand.TYPES.CONSOLE_MESSAGE,
      {
        ignoreExistingResources: true,
        predicate(resource) {
          return resource.message.level == "info";
        },
      }
    ));
  const onTimeout = wait(500).then(() => "TIMEOUT");
  await safeSynthesizeMouseEventAtCenterInContentPage("#target");
  const raceResult = await Promise.race([onConsoleInfoMessage, onTimeout]);
  is(
    raceResult,
    "TIMEOUT",
    "The mouseup event didn't trigger a console.info call, meaning the event listener was disabled"
  );

  info("Re-enable the mousedown event");
  await toggleEventListenerCheckbox(tooltip, mousedownHeader);
  Assert.deepEqual(
    getAsciiHeadersViz(tooltip),
    ["click []", "mousedown [x]", "mouseup []"],
    "mousedown checkbox is checked again"
  );
  ok(
    eventTooltipBadge.classList.contains("has-disabled-events"),
    "event badge still has the has-disabled-events class"
  );
  await safeSynthesizeMouseEventAtCenterInContentPage("#target");
  data = await getTargetElementHandledEventData();
  is(data.click, 2, `no additional "click" event were handled`);
  is(
    data.mousedown,
    2,
    `but we did get a new "mousedown", the event listener was re-enabled`
  );

  info("Hide the tooltip and show it again");
  const tooltipHidden = tooltip.once("hidden");
  tooltip.hide();
  await tooltipHidden;

  onTooltipShown = tooltip.once("shown");
  eventTooltipBadge.click();
  await onTooltipShown;
  ok(true, "The tooltip is shown again");

  Assert.deepEqual(
    getAsciiHeadersViz(tooltip),
    ["click []", "mousedown [x]", "mouseup []"],
    "Only mousedown checkbox is checked"
  );

  info("Re-enable mouseup events");
  await toggleEventListenerCheckbox(
    tooltip,
    getHeadersInEventTooltip(tooltip).at(-1)
  );
  Assert.deepEqual(
    getAsciiHeadersViz(tooltip),
    ["click []", "mousedown [x]", "mouseup [x]"],
    "mouseup is checked again"
  );

  ({ onResource: onConsoleInfoMessage } =
    await resourceCommand.waitForNextResource(
      resourceCommand.TYPES.CONSOLE_MESSAGE,
      {
        ignoreExistingResources: true,
        predicate(resource) {
          return resource.message.level == "info";
        },
      }
    ));
  await safeSynthesizeMouseEventAtCenterInContentPage("#target");
  await onConsoleInfoMessage;
  ok(true, "The mouseup event was re-enabled");
  data = await getTargetElementHandledEventData();
  is(data.click, 2, `"click" is still disabled`);
  is(
    data.mousedown,
    3,
    `we received a new "mousedown" event as part of the click`
  );

  info("Close DevTools to check that event listeners are re-enabled");
  await closeToolbox();
  await safeSynthesizeMouseEventAtCenterInContentPage("#target");
  data = await getTargetElementHandledEventData();
  is(
    data.click,
    3,
    `a new "click" event was handled after the devtools was closed`
  );
  is(
    data.mousedown,
    4,
    `a new "mousedown" event was handled after the devtools was closed`
  );
});

function getHeadersInEventTooltip(tooltip) {
  return Array.from(tooltip.panel.querySelectorAll(".event-header"));
}

/**
 * Get an array of string representing a header in its state, e.g.
 * [
 *   "click [x]",
 *   "mousedown []",
 * ]
 *
 * represents an event tooltip with a click and a mousedown event, where the mousedown
 * event has been disabled.
 *
 * @param {EventTooltip} tooltip
 * @returns Array<String>
 */
function getAsciiHeadersViz(tooltip) {
  return getHeadersInEventTooltip(tooltip).map(
    el =>
      `${el.querySelector(".event-tooltip-event-type").textContent} [${
        getHeaderCheckbox(el).checked ? "x" : ""
      }]`
  );
}

function getHeaderCheckbox(headerEl) {
  return headerEl.querySelector("input[type=checkbox]");
}

async function toggleEventListenerCheckbox(tooltip, headerEl) {
  const onEventToggled = tooltip.eventTooltip.once(
    "event-tooltip-listener-toggled"
  );
  const checkbox = getHeaderCheckbox(headerEl);
  const previousValue = checkbox.checked;
  EventUtils.synthesizeMouseAtCenter(
    getHeaderCheckbox(headerEl),
    {},
    headerEl.ownerGlobal
  );
  await onEventToggled;
  is(checkbox.checked, !previousValue, "The checkbox was toggled");
  is(
    headerEl.classList.contains("content-expanded"),
    false,
    "Clicking on the checkbox did not expand the header"
  );
}

/**
 * @returns Promise<Object> The object keys are event names (e.g. "click", "mousedown"), and
 *          the values are number representing the number of time the event was handled.
 *          Note that "mouseup" isn't handled here.
 */
function getTargetElementHandledEventData() {
  return SpecialPowers.spawn(gBrowser.selectedBrowser, [], function () {
    // In doc_markup_events_toggle.html , we count the events handled by the target in
    // a stringified object in dataset.handledEvents.
    return JSON.parse(
      content.document.getElementById("target").dataset.handledEvents
    );
  });
}