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

"use strict";

/**
 * Test the behavior of keypress shortcuts for the bookmarks toolbar.
 */

// Test that the bookmarks toolbar's visibility is toggled using the bookmarks-shortcut.
add_task(async function testBookmarksToolbarShortcut() {
  let blankTab = await BrowserTestUtils.openNewForegroundTab({
    gBrowser,
    opening: "example.com",
    waitForLoad: false,
  });

  info("Toggle toolbar visibility on");
  let toolbar = document.getElementById("PersonalToolbar");
  is(
    toolbar.getAttribute("collapsed"),
    "true",
    "Toolbar bar should already be collapsed"
  );

  EventUtils.synthesizeKey("b", { shiftKey: true, accelKey: true });
  toolbar = document.getElementById("PersonalToolbar");
  await BrowserTestUtils.waitForAttribute("collapsed", toolbar, "false");
  ok(true, "bookmarks toolbar is visible");

  await testIsBookmarksMenuItemStateChecked("always");

  info("Toggle toolbar visibility off");
  EventUtils.synthesizeKey("b", { shiftKey: true, accelKey: true });
  toolbar = document.getElementById("PersonalToolbar");
  await BrowserTestUtils.waitForAttribute("collapsed", toolbar, "true");
  ok(true, "bookmarks toolbar is not visible");

  await testIsBookmarksMenuItemStateChecked("never");

  await BrowserTestUtils.removeTab(blankTab);
});

// Test that the bookmarks library windows opens with the new keyboard shortcut.
add_task(async function testNewBookmarksLibraryShortcut() {
  let blankTab = await BrowserTestUtils.openNewForegroundTab({
    gBrowser,
    opening: "example.com",
    waitForLoad: false,
  });

  info("Check that the bookmarks library windows opens.");
  let bookmarksLibraryOpened = promiseOpenBookmarksLibrary();

  await EventUtils.synthesizeKey("o", { shiftKey: true, accelKey: true });

  let win = await bookmarksLibraryOpened;

  ok(true, "Bookmarks library successfully opened.");

  win.close();

  await BrowserTestUtils.removeTab(blankTab);
});

/**
 * Tests whether or not the bookmarks' menuitem state is checked.
 */
async function testIsBookmarksMenuItemStateChecked(expected) {
  info("Test that the toolbar menuitem state is correct.");
  let contextMenu = document.getElementById("toolbar-context-menu");
  let target = document.getElementById("PanelUI-menu-button");

  await openContextMenu(contextMenu, target);

  let menuitems = ["always", "never", "newtab"].map(e =>
    document.querySelector(`menuitem[data-visibility-enum="${e}"]`)
  );

  let checkedItem = menuitems.filter(m => m.getAttribute("checked") == "true");
  is(checkedItem.length, 1, "should have only one menuitem checked");
  is(
    checkedItem[0].dataset.visibilityEnum,
    expected,
    `checked menuitem should be ${expected}`
  );

  for (let menuitem of menuitems) {
    if (menuitem.dataset.visibilityEnum == expected) {
      ok(!menuitem.hasAttribute("key"), "dont show shortcut on current state");
    } else {
      is(
        menuitem.hasAttribute("key"),
        menuitem.dataset.visibilityEnum != "newtab",
        "shortcut is on the menuitem opposite of the current state excluding newtab"
      );
    }
  }

  await closeContextMenu(contextMenu);
}

/**
 * Returns a promise for opening the bookmarks library.
 */
async function promiseOpenBookmarksLibrary() {
  return BrowserTestUtils.domWindowOpened(null, async win => {
    await BrowserTestUtils.waitForEvent(win, "load");
    await TestUtils.waitForCondition(
      () =>
        win.document.documentURI ===
        "chrome://browser/content/places/places.xhtml"
    );
    return true;
  });
}

/**
 * Helper for opening the context menu.
 */
async function openContextMenu(contextMenu, target) {
  info("Opening context menu.");
  EventUtils.synthesizeMouseAtCenter(target, {
    type: "contextmenu",
  });
  await BrowserTestUtils.waitForPopupEvent(contextMenu, "shown");
  let bookmarksToolbarMenu = document.querySelector("#toggle_PersonalToolbar");
  let subMenu = bookmarksToolbarMenu.querySelector("menupopup");
  bookmarksToolbarMenu.openMenu(true);
  await BrowserTestUtils.waitForPopupEvent(subMenu, "shown");
}

/**
 * Helper for closing the context menu.
 */
async function closeContextMenu(contextMenu) {
  info("Closing context menu.");
  contextMenu.hidePopup();
  await BrowserTestUtils.waitForPopupEvent(contextMenu, "hidden");
}