fix: beforeunload and unload firing in BrowserViews (#28382)

* fix: beforeunload and unload firing in BrowserViews

* Ensure UserGesture is sent for BV webContents

* spec: add tests

* refactor: clean up logic

* spec: fixup specs

* docs: document event behavior for BrowserViews
This commit is contained in:
Shelley Vohr 2021-04-07 07:16:10 +00:00 committed by GitHub
parent e454bded3c
commit 7d04f729d8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 56 additions and 9 deletions

View file

@ -3,7 +3,7 @@ import { AddressInfo } from 'net';
import * as path from 'path';
import * as fs from 'fs';
import * as http from 'http';
import { BrowserWindow, ipcMain, webContents, session, WebContents, app } from 'electron/main';
import { BrowserWindow, ipcMain, webContents, session, WebContents, app, BrowserView } from 'electron/main';
import { clipboard } from 'electron/common';
import { emittedOnce } from './events-helpers';
import { closeAllWindows } from './window-helpers';
@ -49,7 +49,7 @@ describe('webContents module', () => {
describe('will-prevent-unload event', function () {
afterEach(closeAllWindows);
it('does not emit if beforeunload returns undefined', async () => {
it('does not emit if beforeunload returns undefined in a BrowserWindow', async () => {
const w = new BrowserWindow({ show: false });
w.webContents.once('will-prevent-unload', () => {
expect.fail('should not have fired');
@ -60,14 +60,41 @@ describe('webContents module', () => {
await wait;
});
it('emits if beforeunload returns false', async () => {
it('does not emit if beforeunload returns undefined in a BrowserView', async () => {
const w = new BrowserWindow({ show: false });
const view = new BrowserView();
w.setBrowserView(view);
view.setBounds(w.getBounds());
view.webContents.once('will-prevent-unload', () => {
expect.fail('should not have fired');
});
await view.webContents.loadFile(path.join(__dirname, 'fixtures', 'api', 'beforeunload-undefined.html'));
const wait = emittedOnce(w, 'closed');
w.close();
await wait;
});
it('emits if beforeunload returns false in a BrowserWindow', async () => {
const w = new BrowserWindow({ show: false });
await w.loadFile(path.join(__dirname, 'fixtures', 'api', 'beforeunload-false.html'));
w.close();
await emittedOnce(w.webContents, 'will-prevent-unload');
});
it('supports calling preventDefault on will-prevent-unload events', async () => {
it('emits if beforeunload returns false in a BrowserView', async () => {
const w = new BrowserWindow({ show: false });
const view = new BrowserView();
w.setBrowserView(view);
view.setBounds(w.getBounds());
await view.webContents.loadFile(path.join(__dirname, 'fixtures', 'api', 'beforeunload-false.html'));
w.close();
await emittedOnce(view.webContents, 'will-prevent-unload');
});
it('supports calling preventDefault on will-prevent-unload events in a BrowserWindow', async () => {
const w = new BrowserWindow({ show: false });
w.webContents.once('will-prevent-unload', event => event.preventDefault());
await w.loadFile(path.join(__dirname, 'fixtures', 'api', 'beforeunload-false.html'));