Bug 1868605 - Add a test to ensure changing the url ref in the address bar causes a HASHCHANGE load. r=dao

Differential Revision: https://phabricator.services.mozilla.com/D210489
This commit is contained in:
Marco Bonardo 2024-05-23 16:05:49 +00:00
parent e47137db7e
commit bc85923a41
2 changed files with 47 additions and 6 deletions

View file

@ -268,6 +268,39 @@ add_task(async function go_button_after_tab_switch() {
gBrowser.removeCurrentTab();
});
add_task(async function changing_ref_does_not_reload() {
// Load a page with ref, change the ref and confirm again, it should not
// cause a reload of the page.
for (let protocol of ["http://", "https://"]) {
let url = protocol + "example.com/#ref";
await BrowserTestUtils.withNewTab({ gBrowser, url }, async function () {
await ContentTask.spawn(gBrowser.selectedBrowser, null, () => {
let link = content.document.createElement("a");
link.textContent = "Click me";
link.name = "refmod";
link.setAttribute("name", "refmod");
content.document.body.append(link);
});
await UrlbarTestUtils.promisePopupOpen(window, () => {
EventUtils.synthesizeKey("l", { accelKey: true });
});
Assert.equal(
document.activeElement,
gURLBar.inputField,
"urlbar is focused"
);
EventUtils.synthesizeKey("KEY_ArrowRight", {});
EventUtils.sendString("mod");
let promise = promiseHashChangeLoad(url + "mod");
EventUtils.synthesizeKey("VK_RETURN");
await promise;
});
}
});
async function typeAndCommand(eventType, details = {}) {
await UrlbarTestUtils.inputIntoURLBar(window, TEST_VALUE);
await triggerCommand(eventType, details);
@ -309,6 +342,14 @@ function promiseLoadStarted() {
});
}
async function promiseHashChangeLoad(url) {
let { flags } = await BrowserTestUtils.waitForLocationChange(gBrowser, url);
Assert.ok(
flags & Ci.nsIWebProgressListener.LOCATION_CHANGE_HASHCHANGE,
"Only ref fragment was changed"
);
}
let gUserContextIdSerial = 1;
async function promiseOpenNewTab(url = "about:blank") {
let tab = BrowserTestUtils.addTab(gBrowser, url, {

View file

@ -822,25 +822,25 @@ export var BrowserTestUtils = {
*
* @param {tabbrowser} tabbrowser
* The tabbrowser to wait for the location change on.
* @param {string} url
* @param {string} [url]
* The string URL to look for. The URL must match the URL in the
* location bar exactly.
* @return {Promise}
* @resolves When onLocationChange fires.
* @resolves {webProgress, request, flags} When onLocationChange fires.
*/
waitForLocationChange(tabbrowser, url) {
return new Promise(resolve => {
let progressListener = {
onLocationChange(aBrowser, aWebProgress, aRequest, aLocationURI) {
onLocationChange(browser, webProgress, request, newURI, flags) {
if (
(url && aLocationURI.spec != url) ||
(!url && aLocationURI.spec == "about:blank")
(url && newURI.spec != url) ||
(!url && newURI.spec == "about:blank")
) {
return;
}
tabbrowser.removeTabsProgressListener(progressListener);
resolve();
resolve({ webProgress, request, flags });
},
};
tabbrowser.addTabsProgressListener(progressListener);