summaryrefslogtreecommitdiffstats
path: root/browser/components/customizableui/test/browser_toolbar_collapsed_states.js
blob: 23da60a7d546f012c49a5334870eff1fbfcf7763 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

/**
 * Checks that CustomizableUI reports the expected collapsed toolbar IDs.
 *
 * Note: on macOS, expectations for CustomizableUI.AREA_MENUBAR are
 * automatically skipped since that area isn't available on that platform.
 *
 * @param {string[]} The IDs of the expected collapsed toolbars.
 */
function assertCollapsedToolbarIds(expected) {
  if (AppConstants.platform == "macosx") {
    let menubarIndex = expected.indexOf(CustomizableUI.AREA_MENUBAR);
    if (menubarIndex != -1) {
      expected.splice(menubarIndex, 1);
    }
  }

  let collapsedIds = CustomizableUI.getCollapsedToolbarIds(window);
  Assert.equal(collapsedIds.size, expected.length);
  for (let expectedId of expected) {
    Assert.ok(
      collapsedIds.has(expectedId),
      `${expectedId} should be collapsed`
    );
  }
}

registerCleanupFunction(async () => {
  await CustomizableUI.reset();
});

/**
 * Tests that CustomizableUI.getCollapsedToolbarIds will return the IDs of
 * toolbars that are collapsed, or menubars that are autohidden.
 */
add_task(async function test_toolbar_collapsed_states() {
  // By default, we expect the menubar and the bookmarks toolbar to be
  // collapsed.
  assertCollapsedToolbarIds([
    CustomizableUI.AREA_BOOKMARKS,
    CustomizableUI.AREA_MENUBAR,
  ]);

  let bookmarksToolbar = document.getElementById(CustomizableUI.AREA_BOOKMARKS);
  // Make sure we're configured to show the bookmarks toolbar on about:newtab.
  setToolbarVisibility(bookmarksToolbar, "newtab");

  let newTab = await BrowserTestUtils.openNewForegroundTab({
    gBrowser,
    opening: "about:newtab",
    waitForLoad: false,
  });
  // Now that we've opened about:newtab, the bookmarks toolbar should now
  // be visible.
  assertCollapsedToolbarIds([CustomizableUI.AREA_MENUBAR]);
  await BrowserTestUtils.removeTab(newTab);

  // And with about:newtab closed again, the bookmarks toolbar should be
  // reported as collapsed.
  assertCollapsedToolbarIds([
    CustomizableUI.AREA_BOOKMARKS,
    CustomizableUI.AREA_MENUBAR,
  ]);

  // Make sure we're configured to show the bookmarks toolbar on about:newtab.
  setToolbarVisibility(bookmarksToolbar, "always");
  assertCollapsedToolbarIds([CustomizableUI.AREA_MENUBAR]);

  setToolbarVisibility(bookmarksToolbar, "never");
  assertCollapsedToolbarIds([
    CustomizableUI.AREA_BOOKMARKS,
    CustomizableUI.AREA_MENUBAR,
  ]);

  if (AppConstants.platform != "macosx") {
    // We'll still consider the menubar collapsed by default, even if it's being temporarily
    // shown via the alt key.
    let menubarActive = BrowserTestUtils.waitForEvent(
      window,
      "DOMMenuBarActive"
    );
    EventUtils.synthesizeKey("VK_ALT", {});
    await menubarActive;
    assertCollapsedToolbarIds([
      CustomizableUI.AREA_BOOKMARKS,
      CustomizableUI.AREA_MENUBAR,
    ]);
    let menubarInactive = BrowserTestUtils.waitForEvent(
      window,
      "DOMMenuBarInactive"
    );
    EventUtils.synthesizeKey("VK_ESCAPE", {});
    await menubarInactive;
    assertCollapsedToolbarIds([
      CustomizableUI.AREA_BOOKMARKS,
      CustomizableUI.AREA_MENUBAR,
    ]);

    let menubar = document.getElementById(CustomizableUI.AREA_MENUBAR);
    setToolbarVisibility(menubar, true);
    assertCollapsedToolbarIds([CustomizableUI.AREA_BOOKMARKS]);
    setToolbarVisibility(menubar, false);
    assertCollapsedToolbarIds([
      CustomizableUI.AREA_BOOKMARKS,
      CustomizableUI.AREA_MENUBAR,
    ]);
  }
});