summaryrefslogtreecommitdiffstats
path: root/devtools/client/framework/test/helper_enable_devtools_popup.js
blob: da5d9c0d3789e4c69a48a8ca6d6f9c89464f2970 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

/* import-globals-from ../../shared/test/shared-head.js */

const { listenOnce } = require("resource://devtools/shared/async-utils.js");

/**
 * Helpers dedicated to the browser_enable_devtools_popup* tests.
 * Those tests usually test the same exact things, but using different
 * configurations.
 */

function openDevToolsWithKey(key, modifiers) {
  const onToolboxReady = gDevTools.once("toolbox-ready");
  EventUtils.synthesizeKey(key, modifiers);
  return onToolboxReady;
}
/* exported openDevToolsWithKey */

function closeDevToolsWithKey(toolbox, key, modifiers) {
  const onToolboxDestroyed = toolbox.once("destroyed");
  EventUtils.synthesizeKey(key, modifiers);
  return onToolboxDestroyed;
}
/* exported closeDevToolsWithKey */

/**
 * The popup element might still be in its template wrapper.
 */
function unwrapEnableDevToolsPopup(tab) {
  const panelWrapper = tab.ownerDocument.getElementById(
    "wrapper-enable-devtools-popup"
  );
  if (panelWrapper) {
    info("Unwrapping enable devtools popup");
    panelWrapper.replaceWith(panelWrapper.content);
  }
}

/**
 * Test if F12 is currently disabled:
 * - press F12 -> popup is displayed
 * - press F12 again -> popup is hidden
 * - no toolbox was opened during the process
 */
async function checkF12IsDisabled(tab) {
  unwrapEnableDevToolsPopup(tab);

  const popup = tab.ownerDocument.getElementById("enable-devtools-popup");
  is(popup.state, "closed", "The enable devtools popup is initially hidden");

  const failOnToolboxReady = () => {
    ok(false, "The devtools toolbox should not open");
  };
  gDevTools.on("toolbox-ready", failOnToolboxReady);

  info("Press F12 and wait for the enable devtools popup to be displayed");
  const onPopupShown = listenOnce(popup, "popupshown");
  EventUtils.synthesizeKey("VK_F12");
  await onPopupShown;
  is(popup.state, "open", "The enable devtools popup is now visible");

  info("Press F12 again and wait for the enable devtools popup to hide");
  const onPopupHidden = listenOnce(popup, "popuphidden");
  EventUtils.synthesizeKey("VK_F12");
  await onPopupHidden;
  is(popup.state, "closed", "The enable devtools popup is hidden again");

  gDevTools.off("toolbox-ready", failOnToolboxReady);
}
/* exported checkF12IsDisabled */

/**
 * Test that DevTools can be open with another keyboard shortcut than F12.
 * The enable-devtools popup should not be displayed.
 */
async function openDevToolsWithInspectorKey(tab) {
  unwrapEnableDevToolsPopup(tab);

  info("Open DevTools via another shortcut (only F12 should be disabled)");
  const popup = tab.ownerDocument.getElementById("enable-devtools-popup");

  // We are going to use F12 but the popup should never show up.
  const failOnPopupShown = () => {
    ok(false, "The enable devtools popup should not be displayed");
  };
  popup.addEventListener("popupshown", failOnPopupShown);

  const toolbox = await openDevToolsWithKey("I", {
    accelKey: true,
    shiftKey: !navigator.userAgent.match(/Mac/),
    altKey: navigator.userAgent.match(/Mac/),
  });

  is(popup.state, "closed", "The enable devtools popup is still hidden");
  popup.removeEventListener("popupshown", failOnPopupShown);

  return toolbox;
}
/* exported openDevToolsWithInspectorKey */

/**
 * Test that the toolbox can be closed with F12, without triggering the popup.
 */
async function closeDevToolsWithF12(tab, toolbox) {
  unwrapEnableDevToolsPopup(tab);

  const popup = tab.ownerDocument.getElementById("enable-devtools-popup");

  // We are going to use F12 but the popup should never show up.
  const failOnPopupShown = () => {
    ok(false, "The enable devtools popup should not be displayed");
  };
  popup.addEventListener("popupshown", failOnPopupShown);

  info("Press F12 and wait for the toolbox to be destroyed");
  await closeDevToolsWithKey(toolbox, "VK_F12");
  is(popup.state, "closed", "The enable devtools popup is still hidden");

  popup.removeEventListener("popupshown", failOnPopupShown);
}
/* exported closeDevToolsWithF12 */

/**
 * Test if F12 is enabled:
 * - press F12 -> toolbox opens
 * - press F12 -> toolbox closes
 * - no enable devtools popup was opened during the process
 */
async function checkF12IsEnabled(tab) {
  unwrapEnableDevToolsPopup(tab);

  const popup = tab.ownerDocument.getElementById("enable-devtools-popup");

  // We are going to use F12 several times, but the popup should never show up.
  // Add a listener on popupshown to make sure this doesn't happen
  const failOnPopupShown = () => {
    ok(false, "The enable devtools popup should not be displayed");
  };
  popup.addEventListener("popupshown", failOnPopupShown);

  info("Check that F12 can now open the toolbox.");
  const toolbox = await openDevToolsWithKey("VK_F12");
  is(popup.state, "closed", "The enable devtools popup is still hidden");

  info("Press F12 and wait for the toolbox to be destroyed");
  await closeDevToolsWithKey(toolbox, "VK_F12");
  is(popup.state, "closed", "The enable devtools popup is still hidden");

  // cleanup
  popup.removeEventListener("popupshown", failOnPopupShown);
}
/* exported checkF12IsEnabled */