feat: add navigationHistory.getEntryAtIndex(int index) method (#41577)

This commit is contained in:
Alice Zhao 2024-03-21 14:59:23 -07:00 committed by GitHub
parent 1036d824fe
commit 00e3445f8a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 164 additions and 9 deletions

View file

@ -546,6 +546,94 @@ describe('webContents module', () => {
});
});
describe('navigationHistory', () => {
let w: BrowserWindow;
beforeEach(async () => {
w = new BrowserWindow({ show: false });
});
afterEach(closeAllWindows);
describe('navigationHistory.getEntryAtIndex(index) API ', () => {
it('should fetch default navigation entry when no urls are loaded', async () => {
const result = w.webContents.navigationHistory.getEntryAtIndex(0);
expect(result).to.deep.equal({ url: '', title: '' });
});
it('should fetch navigation entry given a valid index', async () => {
await w.loadURL('https://www.google.com');
w.webContents.on('did-finish-load', async () => {
const result = w.webContents.navigationHistory.getEntryAtIndex(0);
expect(result).to.deep.equal({ url: 'https://www.google.com/', title: 'Google' });
});
});
it('should return null given an invalid index larger than history length', async () => {
await w.loadURL('https://www.google.com');
w.webContents.on('did-finish-load', async () => {
const result = w.webContents.navigationHistory.getEntryAtIndex(5);
expect(result).to.be.null();
});
});
it('should return null given an invalid negative index', async () => {
await w.loadURL('https://www.google.com');
w.webContents.on('did-finish-load', async () => {
const result = w.webContents.navigationHistory.getEntryAtIndex(-1);
expect(result).to.be.null();
});
});
});
describe('navigationHistory.getActiveIndex() API', () => {
it('should return valid active index after a single page visit', async () => {
await w.loadURL('https://www.google.com');
w.webContents.on('did-finish-load', async () => {
expect(w.webContents.navigationHistory.getActiveIndex()).to.equal(0);
});
});
it('should return valid active index after a multiple page visits', async () => {
const loadPromise = once(w.webContents, 'did-finish-load');
await w.loadURL('https://www.github.com');
await loadPromise;
await w.loadURL('https://www.google.com');
await loadPromise;
await w.loadURL('about:blank');
await loadPromise;
expect(w.webContents.navigationHistory.getActiveIndex()).to.equal(2);
});
it('should return valid active index given no page visits', async () => {
expect(w.webContents.navigationHistory.getActiveIndex()).to.equal(0);
});
});
describe('navigationHistory.length() API', () => {
it('should return valid history length after a single page visit', async () => {
await w.loadURL('https://www.google.com');
w.webContents.on('did-finish-load', async () => {
expect(w.webContents.navigationHistory.length()).to.equal(1);
});
});
it('should return valid history length after a multiple page visits', async () => {
const loadPromise = once(w.webContents, 'did-finish-load');
await w.loadURL('https://www.github.com');
await loadPromise;
await w.loadURL('https://www.google.com');
await loadPromise;
await w.loadURL('about:blank');
await loadPromise;
expect(w.webContents.navigationHistory.length()).to.equal(3);
});
it('should return valid history length given no page visits', async () => {
// Note: Even if no navigation has committed, the history list will always start with an initial navigation entry
// Ref: https://source.chromium.org/chromium/chromium/src/+/main:ceontent/public/browser/navigation_controller.h;l=381
expect(w.webContents.navigationHistory.length()).to.equal(1);
});
});
});
describe('getFocusedWebContents() API', () => {
afterEach(closeAllWindows);