Bug 1609627 - [remote] Implement Page.navigateToHistoryEntry. r=remote-protocol-reviewers,maja_zf

Differential Revision: https://phabricator.services.mozilla.com/D60122

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Henrik Skupin 2020-01-20 18:59:08 +00:00
Родитель 4645068db6
Коммит de8109653e
4 изменённых файлов: 167 добавлений и 0 удалений

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

@ -343,6 +343,22 @@ class Page extends ContentProcessDomain {
};
}
_getIndexForHistoryEntryId(id) {
const sessionHistory = this.docShell
.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsISHistory);
for (let index = 0; index < sessionHistory.count; index++) {
const entry = sessionHistory.getEntryAtIndex(index);
if (entry.ID == id) {
return index;
}
}
return null;
}
_getScrollbarSize() {
const scrollbarHeight = {};
const scrollbarWidth = {};

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

@ -259,6 +259,29 @@ class Page extends Domain {
await this._dialogHandler.handleJavaScriptDialog({ accept, promptText });
}
/**
* Navigates current page to the given history entry.
*
* @param {Object} options
* @param {number} options.entryId
* Unique id of the entry to navigate to.
*/
async navigateToHistoryEntry(options = {}) {
const { entryId } = options;
const index = await this.executeInChild(
"_getIndexForHistoryEntryId",
entryId
);
if (index == null) {
throw new Error("No entry with passed id");
}
const { window } = this.session.target;
window.gBrowser.gotoIndex(index);
}
/**
* Print page as PDF.
*

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

@ -22,6 +22,7 @@ support-files =
[browser_javascriptDialog_otherTarget.js]
[browser_javascriptDialog_prompt.js]
[browser_lifecycleEvent.js]
[browser_navigateToHistoryEntry.js]
[browser_printToPDF.js]
[browser_reload.js]
[browser_runtimeEvents.js]

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

@ -0,0 +1,127 @@
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
add_task(async function toUnknownEntryId({ client }) {
const { Page } = client;
let errorThrown = false;
try {
await Page.navigateToHistoryEntry({ entryId: 42 });
} catch (e) {
errorThrown = true;
}
ok(errorThrown, "Unknown error id raised error");
});
add_task(async function toSameEntry({ client }) {
const { Page } = client;
const data = generateHistoryData(1);
for (const entry of data) {
await loadURL(entry.userTypedURL);
}
const { currentIndex, entries } = await Page.getNavigationHistory();
await Page.navigateToHistoryEntry({ entryId: entries[currentIndex].id });
const history = await Page.getNavigationHistory();
assertHistoryEntries(history, data, 0);
is(
gBrowser.selectedBrowser.currentURI.spec,
data[0].url,
"Expected URL loaded"
);
});
add_task(async function oneEntryBackInHistory({ client }) {
const { Page } = client;
const data = generateHistoryData(3);
for (const entry of data) {
await loadURL(entry.userTypedURL);
}
const { currentIndex, entries } = await Page.getNavigationHistory();
await Page.navigateToHistoryEntry({ entryId: entries[currentIndex - 1].id });
const history = await Page.getNavigationHistory();
assertHistoryEntries(history, data, currentIndex - 1);
is(
gBrowser.selectedBrowser.currentURI.spec,
data[currentIndex - 1].url,
"Expected URL loaded"
);
});
add_task(async function oneEntryForwardInHistory({ client }) {
const { Page } = client;
const data = generateHistoryData(3);
for (const entry of data) {
await loadURL(entry.userTypedURL);
}
gBrowser.gotoIndex(0);
const { currentIndex, entries } = await Page.getNavigationHistory();
await Page.navigateToHistoryEntry({ entryId: entries[currentIndex + 1].id });
const history = await Page.getNavigationHistory();
assertHistoryEntries(history, data, currentIndex + 1);
is(
gBrowser.selectedBrowser.currentURI.spec,
data[currentIndex + 1].url,
"Expected URL loaded"
);
});
add_task(async function toFirstEntryInHistory({ client }) {
const { Page } = client;
const data = generateHistoryData(3);
for (const entry of data) {
await loadURL(entry.userTypedURL);
}
const { entries } = await Page.getNavigationHistory();
await Page.navigateToHistoryEntry({ entryId: entries[0].id });
const history = await Page.getNavigationHistory();
assertHistoryEntries(history, data, 0);
is(
gBrowser.selectedBrowser.currentURI.spec,
data[0].url,
"Expected URL loaded"
);
});
add_task(async function toLastEntryInHistory({ client }) {
const { Page } = client;
const data = generateHistoryData(3);
for (const entry of data) {
await loadURL(entry.userTypedURL);
}
gBrowser.gotoIndex(0);
const { entries } = await Page.getNavigationHistory();
await Page.navigateToHistoryEntry({
entryId: entries[entries.length - 1].id,
});
const history = await Page.getNavigationHistory();
assertHistoryEntries(history, data, data.length - 1);
is(
gBrowser.selectedBrowser.currentURI.spec,
data[data.length - 1].url,
"Expected URL loaded"
);
});