Bug 1674539 - Add a test for 'Show Other Bookmarks' menu item in toolbar context menu. r=jaws

Differential Revision: https://phabricator.services.mozilla.com/D97212
This commit is contained in:
Micah Tigley 2020-11-19 20:54:48 +00:00
Родитель 6a8271fa76
Коммит 8856b8cacb
1 изменённых файлов: 179 добавлений и 25 удалений

Просмотреть файл

@ -30,10 +30,7 @@ add_task(async function setup() {
let toolbar = document.getElementById("PersonalToolbar");
let wasCollapsed = toolbar.collapsed;
// Uncollapse the personal toolbar if needed.
if (wasCollapsed) {
await promiseSetToolbarVisibility(toolbar, true);
}
await setupBookmarksToolbar();
// Cleanup.
registerCleanupFunction(async () => {
@ -53,18 +50,18 @@ add_task(async function testShowingOtherBookmarksInToolbar() {
});
info("Check visibility of an empty Other Bookmarks folder.");
testIsOtherBookmarksHidden(true);
await testIsOtherBookmarksHidden(true);
info("Ensure folder appears in toolbar when a new bookmark is added.");
let bookmarks = await PlacesUtils.bookmarks.insertTree({
guid: PlacesUtils.bookmarks.unfiledGuid,
children: bookmarksInfo,
});
testIsOtherBookmarksHidden(false);
await testIsOtherBookmarksHidden(false);
info("Ensure folder disappears from toolbar when no bookmarks are present.");
await PlacesUtils.bookmarks.remove(bookmarks);
testIsOtherBookmarksHidden(true);
await testIsOtherBookmarksHidden(true);
});
// Test that folder visibility is correct when moving bookmarks to an empty
@ -79,7 +76,7 @@ add_task(async function testOtherBookmarksVisibilityWhenMovingBookmarks() {
guid: PlacesUtils.bookmarks.toolbarGuid,
children: bookmarksInfo,
});
testIsOtherBookmarksHidden(true);
await testIsOtherBookmarksHidden(true);
info("Move toolbar bookmarks to Other Bookmarks folder.");
await PlacesUtils.bookmarks.moveToFolder(
@ -87,7 +84,7 @@ add_task(async function testOtherBookmarksVisibilityWhenMovingBookmarks() {
PlacesUtils.bookmarks.unfiledGuid,
PlacesUtils.bookmarks.DEFAULT_INDEX
);
testIsOtherBookmarksHidden(false);
await testIsOtherBookmarksHidden(false);
info("Move bookmarks from Other Bookmarks back to the toolbar.");
await PlacesUtils.bookmarks.moveToFolder(
@ -95,7 +92,7 @@ add_task(async function testOtherBookmarksVisibilityWhenMovingBookmarks() {
PlacesUtils.bookmarks.toolbarGuid,
PlacesUtils.bookmarks.DEFAULT_INDEX
);
testIsOtherBookmarksHidden(true);
await testIsOtherBookmarksHidden(true);
});
// Test OtherBookmarksPopup in toolbar.
@ -110,7 +107,7 @@ add_task(async function testOtherBookmarksMenuPopup() {
children: bookmarksInfo,
});
testIsOtherBookmarksHidden(false);
await testIsOtherBookmarksHidden(false);
info("Check the popup menu has correct number of children.");
await openMenuPopup("#OtherBookmarksPopup", "#OtherBookmarks");
@ -159,30 +156,22 @@ add_task(async function testFolderPopup() {
});
add_task(async function testOnlyShowOtherFolderInBookmarksToolbar() {
testIsOtherBookmarksHidden(false);
await testIsOtherBookmarksHidden(false);
// Test that moving the personal-bookmarks widget out of the
// Bookmarks Toolbar will hide the "Other Bookmarks" folder.
let widgetId = "personal-bookmarks";
CustomizableUI.addWidgetToArea(widgetId, CustomizableUI.AREA_NAVBAR);
testIsOtherBookmarksHidden(true);
await testIsOtherBookmarksHidden(true);
CustomizableUI.reset();
testIsOtherBookmarksHidden(false);
await testIsOtherBookmarksHidden(false);
});
// Test that the menu popup updates when menu items are deleted from it while
// it's open.
add_task(async function testDeletingMenuItems() {
let toolbar = document.getElementById("PersonalToolbar");
let wasCollapsed = toolbar.collapsed;
// Uncollapse the personal toolbar if needed.
if (wasCollapsed) {
await promiseSetToolbarVisibility(toolbar, true);
}
await PlacesUtils.bookmarks.eraseEverything();
await setupBookmarksToolbar();
await SpecialPowers.pushPrefEnv({
set: [[BOOKMARKS_H2_2020_PREF, true]],
@ -243,14 +232,70 @@ add_task(async function no_errors_when_bookmarks_placed_in_palette() {
CustomizableUI.reset();
});
// Test "Show Other Bookmarks" menu item visibility in toolbar context menu.
add_task(async function testShowingOtherBookmarksContextMenuItem() {
await setupBookmarksToolbar();
await SpecialPowers.pushPrefEnv({
set: [[BOOKMARKS_H2_2020_PREF, true]],
});
info("Add bookmark to Other Bookmarks.");
let bookmark = await PlacesUtils.bookmarks.insertTree({
guid: PlacesUtils.bookmarks.unfiledGuid,
children: [{ title: "firefox", url: "http://example.com" }],
});
info("'Show Other Bookmarks' menu item should be checked by default.");
await testOtherBookmarksCheckedState(true);
info("Toggle off showing the Other Bookmarks folder.");
await selectShowOtherBookmarksMenuItem();
await testOtherBookmarksCheckedState(false);
await testIsOtherBookmarksHidden(true);
info("Toggle on showing the Other Bookmarks folder.");
await selectShowOtherBookmarksMenuItem();
await testOtherBookmarksCheckedState(true);
await testIsOtherBookmarksHidden(false);
info(
"Ensure 'Show Other Bookmarks' isn't shown when Other Bookmarks is empty."
);
await PlacesUtils.bookmarks.remove(bookmark);
await testIsOtherBookmarksMenuItemShown(false);
info("Add a bookmark to the empty Other Bookmarks folder.");
await PlacesUtils.bookmarks.insertTree({
guid: PlacesUtils.bookmarks.unfiledGuid,
children: [{ title: "firefox", url: "http://example.com" }],
});
await testIsOtherBookmarksMenuItemShown(true);
});
// Test 'Show Other Bookmarks' isn't shown when pref is false.
add_task(async function showOtherBookmarksMenuItemPrefDisabled() {
await setupBookmarksToolbar();
await SpecialPowers.pushPrefEnv({
set: [[BOOKMARKS_H2_2020_PREF, false]],
});
await testIsOtherBookmarksMenuItemShown(false);
});
/**
* Tests whether or not the "Other Bookmarks" folder is visible.
*/
async function testIsOtherBookmarksHidden(expected) {
info("Test whether or not the 'Other Bookmarks' folder is visible.");
let otherBookmarks = document.getElementById("OtherBookmarks");
await BrowserTestUtils.waitForAttribute("hidden", otherBookmarks, expected);
// Ensure the toolbar is visible.
let toolbar = document.getElementById("PersonalToolbar");
await promiseSetToolbarVisibility(toolbar, true);
await TestUtils.waitForCondition(() => {
let otherBookmarks = document.getElementById("OtherBookmarks");
return otherBookmarks.hidden === expected;
}, "Other Bookmarks folder failed to change hidden state.");
ok(true, `Other Bookmarks folder "hidden" state should be ${expected}.`);
}
@ -274,6 +319,59 @@ function testNumberOfMenuPopupChildren(selector, expected) {
);
}
/**
* Test helper for checking the 'checked' state of the "Show Other Bookmarks" menu item
* after selecting it from the context menu.
*
* @param {Boolean} expectedCheckedState
* Whether or not the menu item is checked.
*/
async function testOtherBookmarksCheckedState(expectedCheckedState) {
await openToolbarContextMenu();
info("Check 'Show Other Bookmarks' menu item state");
await openToolbarContextMenu();
let otherBookmarksMenuItem = document.querySelector(
"#show-other-bookmarks_PersonalToolbar"
);
is(
otherBookmarksMenuItem.getAttribute("checked"),
`${expectedCheckedState}`,
`Other Bookmarks item's checked state should be ${expectedCheckedState}`
);
await closeToolbarContextMenu();
}
/**
* Test helper for checking whether or not the 'Show Other Bookmarks' menu item
* appears in the toolbar's context menu.
*
* @param {Boolean} expected
* Whether or not the menu item appears in the toolbar conext menu.
*/
async function testIsOtherBookmarksMenuItemShown(expected) {
let toolbar = document.getElementById("PersonalToolbar");
let contextMenu = document.getElementById("placesContext");
EventUtils.synthesizeMouseAtCenter(toolbar, {
type: "contextmenu",
});
await BrowserTestUtils.waitForPopupEvent(contextMenu, "shown");
let otherBookmarksMenuItem = document.querySelector(
"#show-other-bookmarks_PersonalToolbar"
);
is(
!!otherBookmarksMenuItem,
expected,
"'Show Other Bookmarks' menu item appearance state is correct."
);
await closeToolbarContextMenu();
}
/**
* Helper for opening a menu popup.
*
@ -304,3 +402,59 @@ async function closeMenuPopup(popupSelector) {
popup.hidePopup();
await BrowserTestUtils.waitForPopupEvent(popup, "hidden");
}
/**
* Helper for opening the toolbar context menu.
*/
async function openToolbarContextMenu() {
let contextMenu = document.getElementById("placesContext");
let toolbar = document.getElementById("PersonalToolbar");
EventUtils.synthesizeMouseAtCenter(toolbar, {
type: "contextmenu",
});
await BrowserTestUtils.waitForPopupEvent(contextMenu, "shown");
}
/**
* Helper for closing the toolbar context menu.
*/
async function closeToolbarContextMenu() {
let contextMenu = document.getElementById("placesContext");
contextMenu.hidePopup();
await BrowserTestUtils.waitForPopupEvent(contextMenu, "hidden");
}
/**
* Helper for setting up the bookmarks toolbar state. This ensures the beginning
* of a task will always have the bookmark toolbar in a state that makes the
* Other Bookmarks folder testable.
*/
async function setupBookmarksToolbar() {
let toolbar = document.getElementById("PersonalToolbar");
let wasCollapsed = toolbar.collapsed;
// Uncollapse the personal toolbar if needed.
if (wasCollapsed) {
await promiseSetToolbarVisibility(toolbar, true);
}
await PlacesUtils.bookmarks.eraseEverything();
}
/**
* Helper for selecting the "Show Other Bookmarks" menu item from the bookmarks
* toolbar context menu.
*/
async function selectShowOtherBookmarksMenuItem() {
info("Select 'Show Other Bookmarks' menu item");
await openToolbarContextMenu();
let otherBookmarksMenuItem = document.querySelector(
"#show-other-bookmarks_PersonalToolbar"
);
let contextMenu = document.getElementById("placesContext");
EventUtils.synthesizeMouseAtCenter(otherBookmarksMenuItem, {});
await BrowserTestUtils.waitForPopupEvent(contextMenu, "hidden");
}