summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/fullscreen/browser_fullscreen_context_menu.js
blob: ec874f1a3f3181861da7c4ecfc87120e94753598 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

async function openContextMenu(itemElement, win = window) {
  let popupShownPromise = BrowserTestUtils.waitForEvent(
    itemElement.ownerDocument,
    "popupshown"
  );
  EventUtils.synthesizeMouseAtCenter(
    itemElement,
    {
      type: "contextmenu",
      button: 2,
    },
    win
  );
  let { target } = await popupShownPromise;
  return target;
}

async function testContextMenu() {
  await BrowserTestUtils.withNewTab("about:blank", async () => {
    let panelUIMenuButton = document.getElementById("PanelUI-menu-button");
    let contextMenu = await openContextMenu(panelUIMenuButton);
    let array1 = AppConstants.MENUBAR_CAN_AUTOHIDE
      ? [
          ".customize-context-moveToPanel",
          ".customize-context-removeFromToolbar",
          "#toolbarItemsMenuSeparator",
          "#toggle_toolbar-menubar",
          "#toggle_PersonalToolbar",
          "#viewToolbarsMenuSeparator",
          ".viewCustomizeToolbar",
        ]
      : [
          ".customize-context-moveToPanel",
          ".customize-context-removeFromToolbar",
          "#toolbarItemsMenuSeparator",
          "#toggle_PersonalToolbar",
          "#viewToolbarsMenuSeparator",
          ".viewCustomizeToolbar",
        ];
    let result1 = verifyContextMenu(contextMenu, array1);
    ok(!result1, "Expected no errors verifying context menu items");
    contextMenu.hidePopup();
    let onFullscreen = Promise.all([
      BrowserTestUtils.waitForEvent(window, "fullscreen"),
      BrowserTestUtils.waitForEvent(
        window,
        "sizemodechange",
        false,
        e => window.fullScreen
      ),
      BrowserTestUtils.waitForPopupEvent(contextMenu, "hidden"),
    ]);
    document.getElementById("View:FullScreen").doCommand();
    contextMenu.hidePopup();
    info("waiting for fullscreen");
    await onFullscreen;
    // make sure the toolbox is visible if it's autohidden
    document.getElementById("Browser:OpenLocation").doCommand();
    info("trigger the context menu");
    let contextMenu2 = await openContextMenu(panelUIMenuButton);
    info("context menu should be open, verify its menu items");
    let array2 = AppConstants.MENUBAR_CAN_AUTOHIDE
      ? [
          ".customize-context-moveToPanel",
          ".customize-context-removeFromToolbar",
          "#toolbarItemsMenuSeparator",
          "#toggle_toolbar-menubar",
          "#toggle_PersonalToolbar",
          "#viewToolbarsMenuSeparator",
          ".viewCustomizeToolbar",
          `menuseparator[contexttype="fullscreen"]`,
          `.fullscreen-context-autohide`,
          `menuitem[contexttype="fullscreen"]`,
        ]
      : [
          ".customize-context-moveToPanel",
          ".customize-context-removeFromToolbar",
          "#toolbarItemsMenuSeparator",
          "#toggle_PersonalToolbar",
          "#viewToolbarsMenuSeparator",
          ".viewCustomizeToolbar",
          `menuseparator[contexttype="fullscreen"]`,
          `.fullscreen-context-autohide`,
          `menuitem[contexttype="fullscreen"]`,
        ];
    let result2 = verifyContextMenu(contextMenu2, array2);
    ok(!result2, "Expected no errors verifying context menu items");
    let onExitFullscreen = Promise.all([
      BrowserTestUtils.waitForEvent(window, "fullscreen"),
      BrowserTestUtils.waitForEvent(
        window,
        "sizemodechange",
        false,
        e => !window.fullScreen
      ),
      BrowserTestUtils.waitForPopupEvent(contextMenu2, "hidden"),
    ]);
    document.getElementById("View:FullScreen").doCommand();
    contextMenu2.hidePopup();
    await onExitFullscreen;
  });
}

function verifyContextMenu(contextMenu, itemSelectors) {
  // Ignore hidden nodes
  let items = Array.from(contextMenu.children).filter(n =>
    BrowserTestUtils.is_visible(n)
  );
  let menuAsText = items
    .map(n => {
      return n.nodeName == "menuseparator"
        ? "---"
        : `${n.label} (${n.command})`;
    })
    .join("\n");
  info("Got actual context menu items: \n" + menuAsText);

  try {
    is(
      items.length,
      itemSelectors.length,
      "Context menu has the expected number of items"
    );
    for (let i = 0; i < items.length; i++) {
      let selector = itemSelectors[i];
      ok(
        items[i].matches(selector),
        `Item at ${i} matches expected selector: ${selector}`
      );
    }
  } catch (ex) {
    return ex;
  }
  return null;
}

add_task(testContextMenu);