browser_history_menu.js (5502B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 // This test verifies that the back forward button long-press menu and context menu 5 // shows the correct history items. 6 7 add_task(async function mousedown_back() { 8 await testBackForwardMenu(false); 9 }); 10 11 add_task(async function contextmenu_back() { 12 await testBackForwardMenu(true); 13 }); 14 15 async function openHistoryMenu(useContextMenu) { 16 let backButton = document.getElementById("back-button"); 17 let rect = backButton.getBoundingClientRect(); 18 19 info("waiting for the history menu to open"); 20 21 let popupShownPromise = BrowserTestUtils.waitForEvent( 22 useContextMenu ? document.getElementById("backForwardMenu") : backButton, 23 "popupshown" 24 ); 25 if (useContextMenu) { 26 EventUtils.synthesizeMouseAtCenter(backButton, { 27 type: "contextmenu", 28 button: 2, 29 }); 30 } else { 31 EventUtils.synthesizeMouseAtCenter(backButton, { type: "mousedown" }); 32 } 33 34 EventUtils.synthesizeMouse(backButton, rect.width / 2, rect.height, { 35 type: "mouseup", 36 }); 37 let popupEvent = await popupShownPromise; 38 39 ok(true, "history menu opened"); 40 41 return popupEvent; 42 } 43 44 async function testBackForwardMenu(useContextMenu) { 45 let tab = await BrowserTestUtils.openNewForegroundTab( 46 gBrowser, 47 // eslint-disable-next-line @microsoft/sdl/no-insecure-url 48 "http://example.com" 49 ); 50 51 for (let iter = 2; iter <= 4; iter++) { 52 // Iterate three times. For the first two times through the loop, add a new history item. 53 // But for the last iteration, go back in the history instead. 54 await SpecialPowers.spawn( 55 gBrowser.selectedBrowser, 56 [iter], 57 async function (iterChild) { 58 if (iterChild == 4) { 59 let popStatePromise = new Promise(function (resolve) { 60 content.onpopstate = resolve; 61 }); 62 content.history.back(); 63 await popStatePromise; 64 } else { 65 // With the back-button intervention enabled, we need to make sure to 66 // trigger a user activation on each history entry, or they won't 67 // show in the menu. 68 content.document.notifyUserGestureActivation(); 69 content.history.pushState({}, "" + iterChild, iterChild + ".html"); 70 } 71 } 72 ); 73 74 // Wait for the session data to be flushed before continuing the test 75 await new Promise(resolve => 76 SessionStore.getSessionHistory(gBrowser.selectedTab, resolve) 77 ); 78 79 let popupEvent = await openHistoryMenu(useContextMenu); 80 81 // Wait for the session data to be flushed before continuing the test 82 await new Promise(resolve => 83 SessionStore.getSessionHistory(gBrowser.selectedTab, resolve) 84 ); 85 86 is( 87 popupEvent.target.children.length, 88 iter > 3 ? 3 : iter, 89 "Correct number of history items" 90 ); 91 92 let node = popupEvent.target.lastElementChild; 93 // eslint-disable-next-line @microsoft/sdl/no-insecure-url 94 is(node.getAttribute("uri"), "http://example.com/", "'1' item uri"); 95 is(node.getAttribute("index"), "0", "'1' item index"); 96 is( 97 node.getAttribute("historyindex"), 98 iter == 3 ? "-2" : "-1", 99 "'1' item historyindex" 100 ); 101 102 node = node.previousElementSibling; 103 // eslint-disable-next-line @microsoft/sdl/no-insecure-url 104 is(node.getAttribute("uri"), "http://example.com/2.html", "'2' item uri"); 105 is(node.getAttribute("index"), "1", "'2' item index"); 106 is( 107 node.getAttribute("historyindex"), 108 iter == 3 ? "-1" : "0", 109 "'2' item historyindex" 110 ); 111 112 if (iter >= 3) { 113 node = node.previousElementSibling; 114 // eslint-disable-next-line @microsoft/sdl/no-insecure-url 115 is(node.getAttribute("uri"), "http://example.com/3.html", "'3' item uri"); 116 is(node.getAttribute("index"), "2", "'3' item index"); 117 is( 118 node.getAttribute("historyindex"), 119 iter == 4 ? "1" : "0", 120 "'3' item historyindex" 121 ); 122 } 123 124 // Close the popup, but on the last iteration, click on one of the history items 125 // to ensure it opens in a new tab. 126 let popupHiddenPromise = BrowserTestUtils.waitForEvent( 127 popupEvent.target, 128 "popuphidden" 129 ); 130 131 if (iter < 4) { 132 popupEvent.target.hidePopup(); 133 } else { 134 let newTabPromise = BrowserTestUtils.waitForNewTab( 135 gBrowser, 136 // eslint-disable-next-line @microsoft/sdl/no-insecure-url 137 url => url == "http://example.com/" 138 ); 139 140 popupEvent.target.activateItem(popupEvent.target.children[2], { 141 button: 1, 142 }); 143 144 let newtab = await newTabPromise; 145 gBrowser.removeTab(newtab); 146 } 147 148 await popupHiddenPromise; 149 } 150 151 gBrowser.removeTab(tab); 152 } 153 154 // Make sure that the history popup appears after navigating around in a preferences page. 155 add_task(async function test_preferences_page() { 156 let tab = await BrowserTestUtils.openNewForegroundTab( 157 gBrowser, 158 "about:preferences" 159 ); 160 161 openPreferences("search"); 162 let popupEvent = await openHistoryMenu(true); 163 164 // Wait for the session data to be flushed before continuing the test 165 await new Promise(resolve => 166 SessionStore.getSessionHistory(gBrowser.selectedTab, resolve) 167 ); 168 169 is(popupEvent.target.children.length, 2, "Correct number of history items"); 170 171 let popupHiddenPromise = BrowserTestUtils.waitForEvent( 172 popupEvent.target, 173 "popuphidden" 174 ); 175 popupEvent.target.hidePopup(); 176 await popupHiddenPromise; 177 178 gBrowser.removeTab(tab); 179 });