test: wait for beforeunload handler to be installed (#23874)
This commit is contained in:
parent
fbf397e15d
commit
5918dd6e65
7 changed files with 24 additions and 26 deletions
|
@ -108,7 +108,7 @@ describe('BrowserWindow module', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should emit beforeunload handler', async () => {
|
it('should emit beforeunload handler', async () => {
|
||||||
await w.loadFile(path.join(fixtures, 'api', 'beforeunload-false.html'));
|
await w.loadFile(path.join(__dirname, 'fixtures', 'api', 'beforeunload-false.html'));
|
||||||
w.close();
|
w.close();
|
||||||
await emittedOnce(w.webContents, 'before-unload-fired');
|
await emittedOnce(w.webContents, 'before-unload-fired');
|
||||||
});
|
});
|
||||||
|
@ -193,7 +193,7 @@ describe('BrowserWindow module', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should emit beforeunload event', async function () {
|
it('should emit beforeunload event', async function () {
|
||||||
await w.loadFile(path.join(__dirname, 'fixtures', 'api', 'close-beforeunload-false.html'));
|
await w.loadFile(path.join(__dirname, 'fixtures', 'api', 'beforeunload-false.html'));
|
||||||
w.webContents.executeJavaScript('window.close()', true);
|
w.webContents.executeJavaScript('window.close()', true);
|
||||||
await emittedOnce(w.webContents, 'before-unload-fired');
|
await emittedOnce(w.webContents, 'before-unload-fired');
|
||||||
});
|
});
|
||||||
|
@ -2640,21 +2640,21 @@ describe('BrowserWindow module', () => {
|
||||||
afterEach(closeAllWindows);
|
afterEach(closeAllWindows);
|
||||||
|
|
||||||
it('returning undefined would not prevent close', async () => {
|
it('returning undefined would not prevent close', async () => {
|
||||||
await w.loadFile(path.join(__dirname, 'fixtures', 'api', 'close-beforeunload-undefined.html'));
|
await w.loadFile(path.join(__dirname, 'fixtures', 'api', 'beforeunload-undefined.html'));
|
||||||
const wait = emittedOnce(w, 'closed');
|
const wait = emittedOnce(w, 'closed');
|
||||||
w.close();
|
w.close();
|
||||||
await wait;
|
await wait;
|
||||||
});
|
});
|
||||||
|
|
||||||
it('returning false would prevent close', async () => {
|
it('returning false would prevent close', async () => {
|
||||||
await w.loadFile(path.join(__dirname, 'fixtures', 'api', 'close-beforeunload-false.html'));
|
await w.loadFile(path.join(__dirname, 'fixtures', 'api', 'beforeunload-false.html'));
|
||||||
w.close();
|
w.close();
|
||||||
const [, proceed] = await emittedOnce(w.webContents, 'before-unload-fired');
|
const [, proceed] = await emittedOnce(w.webContents, 'before-unload-fired');
|
||||||
expect(proceed).to.equal(false);
|
expect(proceed).to.equal(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('returning empty string would prevent close', async () => {
|
it('returning empty string would prevent close', async () => {
|
||||||
await w.loadFile(path.join(__dirname, 'fixtures', 'api', 'close-beforeunload-empty-string.html'));
|
await w.loadFile(path.join(__dirname, 'fixtures', 'api', 'beforeunload-empty-string.html'));
|
||||||
w.close();
|
w.close();
|
||||||
const [, proceed] = await emittedOnce(w.webContents, 'before-unload-fired');
|
const [, proceed] = await emittedOnce(w.webContents, 'before-unload-fired');
|
||||||
expect(proceed).to.equal(false);
|
expect(proceed).to.equal(false);
|
||||||
|
@ -2666,7 +2666,11 @@ describe('BrowserWindow module', () => {
|
||||||
const destroyListener = () => { expect.fail('Close was not prevented'); };
|
const destroyListener = () => { expect.fail('Close was not prevented'); };
|
||||||
w.webContents.once('destroyed', destroyListener);
|
w.webContents.once('destroyed', destroyListener);
|
||||||
|
|
||||||
await w.webContents.executeJavaScript('installBeforeUnload(2)', true);
|
w.webContents.executeJavaScript('installBeforeUnload(2)', true);
|
||||||
|
// The renderer needs to report the status of beforeunload handler
|
||||||
|
// back to main process, so wait for next console message, which means
|
||||||
|
// the SuddenTerminationStatus message have been flushed.
|
||||||
|
await emittedOnce(w.webContents, 'console-message');
|
||||||
w.close();
|
w.close();
|
||||||
await emittedOnce(w.webContents, 'before-unload-fired');
|
await emittedOnce(w.webContents, 'before-unload-fired');
|
||||||
w.close();
|
w.close();
|
||||||
|
@ -2684,7 +2688,11 @@ describe('BrowserWindow module', () => {
|
||||||
const navigationListener = () => { expect.fail('Reload was not prevented'); };
|
const navigationListener = () => { expect.fail('Reload was not prevented'); };
|
||||||
w.webContents.once('did-start-navigation', navigationListener);
|
w.webContents.once('did-start-navigation', navigationListener);
|
||||||
|
|
||||||
await w.webContents.executeJavaScript('installBeforeUnload(2)', true);
|
w.webContents.executeJavaScript('installBeforeUnload(2)', true);
|
||||||
|
// The renderer needs to report the status of beforeunload handler
|
||||||
|
// back to main process, so wait for next console message, which means
|
||||||
|
// the SuddenTerminationStatus message have been flushed.
|
||||||
|
await emittedOnce(w.webContents, 'console-message');
|
||||||
w.reload();
|
w.reload();
|
||||||
// Chromium does not emit 'before-unload-fired' on WebContents for
|
// Chromium does not emit 'before-unload-fired' on WebContents for
|
||||||
// navigations, so we have to use other ways to know if beforeunload
|
// navigations, so we have to use other ways to know if beforeunload
|
||||||
|
@ -2704,7 +2712,11 @@ describe('BrowserWindow module', () => {
|
||||||
const navigationListener = () => { expect.fail('Reload was not prevented'); };
|
const navigationListener = () => { expect.fail('Reload was not prevented'); };
|
||||||
w.webContents.once('did-start-navigation', navigationListener);
|
w.webContents.once('did-start-navigation', navigationListener);
|
||||||
|
|
||||||
await w.webContents.executeJavaScript('installBeforeUnload(2)', true);
|
w.webContents.executeJavaScript('installBeforeUnload(2)', true);
|
||||||
|
// The renderer needs to report the status of beforeunload handler
|
||||||
|
// back to main process, so wait for next console message, which means
|
||||||
|
// the SuddenTerminationStatus message have been flushed.
|
||||||
|
await emittedOnce(w.webContents, 'console-message');
|
||||||
w.loadURL('about:blank');
|
w.loadURL('about:blank');
|
||||||
// Chromium does not emit 'before-unload-fired' on WebContents for
|
// Chromium does not emit 'before-unload-fired' on WebContents for
|
||||||
// navigations, so we have to use other ways to know if beforeunload
|
// navigations, so we have to use other ways to know if beforeunload
|
||||||
|
|
|
@ -48,7 +48,7 @@ describe('webContents module', () => {
|
||||||
w.webContents.once('will-prevent-unload', () => {
|
w.webContents.once('will-prevent-unload', () => {
|
||||||
expect.fail('should not have fired');
|
expect.fail('should not have fired');
|
||||||
});
|
});
|
||||||
await w.loadFile(path.join(__dirname, 'fixtures', 'api', 'close-beforeunload-undefined.html'));
|
await w.loadFile(path.join(__dirname, 'fixtures', 'api', 'beforeunload-undefined.html'));
|
||||||
const wait = emittedOnce(w, 'closed');
|
const wait = emittedOnce(w, 'closed');
|
||||||
w.close();
|
w.close();
|
||||||
await wait;
|
await wait;
|
||||||
|
@ -56,7 +56,7 @@ describe('webContents module', () => {
|
||||||
|
|
||||||
it('emits if beforeunload returns false', async () => {
|
it('emits if beforeunload returns false', async () => {
|
||||||
const w = new BrowserWindow({ show: false });
|
const w = new BrowserWindow({ show: false });
|
||||||
await w.loadFile(path.join(__dirname, 'fixtures', 'api', 'close-beforeunload-false.html'));
|
await w.loadFile(path.join(__dirname, 'fixtures', 'api', 'beforeunload-false.html'));
|
||||||
w.close();
|
w.close();
|
||||||
await emittedOnce(w.webContents, 'will-prevent-unload');
|
await emittedOnce(w.webContents, 'will-prevent-unload');
|
||||||
});
|
});
|
||||||
|
@ -64,7 +64,7 @@ describe('webContents module', () => {
|
||||||
it('supports calling preventDefault on will-prevent-unload events', async () => {
|
it('supports calling preventDefault on will-prevent-unload events', async () => {
|
||||||
const w = new BrowserWindow({ show: false });
|
const w = new BrowserWindow({ show: false });
|
||||||
w.webContents.once('will-prevent-unload', event => event.preventDefault());
|
w.webContents.once('will-prevent-unload', event => event.preventDefault());
|
||||||
await w.loadFile(path.join(__dirname, 'fixtures', 'api', 'close-beforeunload-false.html'));
|
await w.loadFile(path.join(__dirname, 'fixtures', 'api', 'beforeunload-false.html'));
|
||||||
const wait = emittedOnce(w, 'closed');
|
const wait = emittedOnce(w, 'closed');
|
||||||
w.close();
|
w.close();
|
||||||
await wait;
|
await wait;
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
e.returnValue = '';
|
e.returnValue = '';
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
console.log('installed')
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
|
|
|
@ -1,15 +0,0 @@
|
||||||
<html>
|
|
||||||
<body>
|
|
||||||
<script type="text/javascript" charset="utf-8">
|
|
||||||
// Only prevent unload on the first window close
|
|
||||||
var unloadPrevented = false;
|
|
||||||
window.onbeforeunload = function() {
|
|
||||||
if (!unloadPrevented) {
|
|
||||||
unloadPrevented = true;
|
|
||||||
console.log('prevent')
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
</script>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
Loading…
Add table
Add a link
Reference in a new issue