browser_bookmarks_shortcut.js (4487B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 add_setup(async function () { 7 await SpecialPowers.pushPrefEnv({ 8 set: [["test.wait300msAfterTabSwitch", true]], 9 }); 10 }); 11 12 /** 13 * Test the behavior of keypress shortcuts for the bookmarks toolbar. 14 */ 15 16 // Test that the bookmarks toolbar's visibility is toggled using the bookmarks-shortcut. 17 add_task(async function testBookmarksToolbarShortcut() { 18 let blankTab = await BrowserTestUtils.openNewForegroundTab({ 19 gBrowser, 20 opening: "example.com", 21 waitForLoad: false, 22 }); 23 24 info("Toggle toolbar visibility on"); 25 let toolbar = document.getElementById("PersonalToolbar"); 26 ok( 27 toolbar.hasAttribute("collapsed"), 28 "Toolbar bar should already be collapsed" 29 ); 30 31 EventUtils.synthesizeKey("b", { shiftKey: true, accelKey: true }); 32 toolbar = document.getElementById("PersonalToolbar"); 33 await BrowserTestUtils.waitForAttributeRemoval("collapsed", toolbar); 34 ok(true, "bookmarks toolbar is visible"); 35 36 await testIsBookmarksMenuItemStateChecked("always"); 37 38 info("Toggle toolbar visibility off"); 39 EventUtils.synthesizeKey("b", { shiftKey: true, accelKey: true }); 40 toolbar = document.getElementById("PersonalToolbar"); 41 await BrowserTestUtils.waitForAttribute("collapsed", toolbar, ""); 42 ok(true, "bookmarks toolbar is not visible"); 43 44 await testIsBookmarksMenuItemStateChecked("never"); 45 46 await BrowserTestUtils.removeTab(blankTab); 47 }); 48 49 // Test that the bookmarks library windows opens with the new keyboard shortcut. 50 add_task(async function testNewBookmarksLibraryShortcut() { 51 let blankTab = await BrowserTestUtils.openNewForegroundTab({ 52 gBrowser, 53 opening: "example.com", 54 waitForLoad: false, 55 }); 56 57 info("Check that the bookmarks library windows opens."); 58 let bookmarksLibraryOpened = promiseOpenBookmarksLibrary(); 59 60 await EventUtils.synthesizeKey("o", { shiftKey: true, accelKey: true }); 61 62 let win = await bookmarksLibraryOpened; 63 64 ok(true, "Bookmarks library successfully opened."); 65 66 win.close(); 67 68 await BrowserTestUtils.removeTab(blankTab); 69 }); 70 71 /** 72 * Tests whether or not the bookmarks' menuitem state is checked. 73 */ 74 async function testIsBookmarksMenuItemStateChecked(expected) { 75 info("Test that the toolbar menuitem state is correct."); 76 let contextMenu = document.getElementById("toolbar-context-menu"); 77 let target = document.getElementById("PanelUI-menu-button"); 78 79 await openContextMenu(contextMenu, target); 80 81 let menuitems = ["always", "never", "newtab"].map(e => 82 document.querySelector(`menuitem[data-visibility-enum="${e}"]`) 83 ); 84 85 let checkedItem = menuitems.filter(m => m.hasAttribute("checked")); 86 is(checkedItem.length, 1, "should have only one menuitem checked"); 87 is( 88 checkedItem[0].dataset.visibilityEnum, 89 expected, 90 `checked menuitem should be ${expected}` 91 ); 92 93 for (let menuitem of menuitems) { 94 if (menuitem.dataset.visibilityEnum == expected) { 95 ok(!menuitem.hasAttribute("key"), "dont show shortcut on current state"); 96 } else { 97 is( 98 menuitem.hasAttribute("key"), 99 menuitem.dataset.visibilityEnum != "newtab", 100 "shortcut is on the menuitem opposite of the current state excluding newtab" 101 ); 102 } 103 } 104 105 await closeContextMenu(contextMenu); 106 } 107 108 /** 109 * Returns a promise for opening the bookmarks library. 110 */ 111 async function promiseOpenBookmarksLibrary() { 112 return BrowserTestUtils.domWindowOpened(null, async win => { 113 await BrowserTestUtils.waitForEvent(win, "load"); 114 await TestUtils.waitForCondition( 115 () => 116 win.document.documentURI === 117 "chrome://browser/content/places/places.xhtml" 118 ); 119 return true; 120 }); 121 } 122 123 /** 124 * Helper for opening the context menu. 125 */ 126 async function openContextMenu(contextMenu, target) { 127 info("Opening context menu."); 128 EventUtils.synthesizeMouseAtCenter(target, { 129 type: "contextmenu", 130 }); 131 await BrowserTestUtils.waitForPopupEvent(contextMenu, "shown"); 132 let bookmarksToolbarMenu = document.querySelector("#toggle_PersonalToolbar"); 133 let subMenu = bookmarksToolbarMenu.querySelector("menupopup"); 134 bookmarksToolbarMenu.openMenu(true); 135 await BrowserTestUtils.waitForPopupEvent(subMenu, "shown"); 136 } 137 138 /** 139 * Helper for closing the context menu. 140 */ 141 async function closeContextMenu(contextMenu) { 142 info("Closing context menu."); 143 contextMenu.hidePopup(); 144 await BrowserTestUtils.waitForPopupEvent(contextMenu, "hidden"); 145 }