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

"use strict";

/* import-globals-from helper-collapsibilities.js */
Services.scriptloader.loadSubScript(
  CHROME_URL_ROOT + "helper-collapsibilities.js",
  this
);

/**
 * Test context menu on about:devtools-toolbox page.
 */
add_task(async function () {
  info("Force all debug target panes to be expanded");
  prepareCollapsibilitiesTest();

  const { document, tab, window } = await openAboutDebugging();
  await selectThisFirefoxPage(document, window.AboutDebugging.store);
  const { devtoolsBrowser, devtoolsTab } = await openAboutDevtoolsToolbox(
    document,
    tab,
    window
  );

  info("Check whether the menu item which opens devtools is disabled");
  const rootDocument = devtoolsTab.ownerDocument;
  await assertContextMenu(
    rootDocument,
    devtoolsBrowser,
    ".debug-target-info",
    false
  );

  info("Force to select about:debugging page");
  await updateSelectedTab(gBrowser, tab, window.AboutDebugging.store);

  info("Check whether the menu item which opens devtools is enabled");
  await assertContextMenu(rootDocument, devtoolsBrowser, "#mount", true);

  await closeAboutDevtoolsToolbox(document, devtoolsTab, window);
  await removeTab(tab);
});

async function assertContextMenu(
  rootDocument,
  browser,
  targetSelector,
  shouldBeEnabled
) {
  if (shouldBeEnabled) {
    await assertContextMenuEnabled(rootDocument, browser, targetSelector);
  } else {
    await assertContextMenuDisabled(rootDocument, browser, targetSelector);
  }
}

async function assertContextMenuDisabled(
  rootDocument,
  browser,
  targetSelector
) {
  const contextMenu = rootDocument.getElementById("contentAreaContextMenu");
  let isPopupShown = false;
  const listener = () => {
    isPopupShown = true;
  };
  contextMenu.addEventListener("popupshown", listener);
  BrowserTestUtils.synthesizeMouseAtCenter(
    targetSelector,
    { type: "contextmenu" },
    browser
  );
  await wait(1000);
  ok(!isPopupShown, `Context menu should not be shown`);
  contextMenu.removeEventListener("popupshown", listener);
}

async function assertContextMenuEnabled(rootDocument, browser, targetSelector) {
  // Show content context menu.
  const contextMenu = rootDocument.getElementById("contentAreaContextMenu");
  const popupShownPromise = BrowserTestUtils.waitForEvent(
    contextMenu,
    "popupshown"
  );
  BrowserTestUtils.synthesizeMouseAtCenter(
    targetSelector,
    { type: "contextmenu" },
    browser
  );
  await popupShownPromise;
  ok(true, `Context menu should be shown`);

  // Hide content context menu.
  const popupHiddenPromise = BrowserTestUtils.waitForEvent(
    contextMenu,
    "popuphidden"
  );
  contextMenu.hidePopup();
  await popupHiddenPromise;
}