Skip to content

Commit

Permalink
test: wait for beforeunload handler to be installed (#23874)
Browse files Browse the repository at this point in the history
  • Loading branch information
zcbenz committed Jun 2, 2020
1 parent fbf397e commit 5918dd6
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 26 deletions.
28 changes: 20 additions & 8 deletions spec-main/api-browser-window-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ describe('BrowserWindow module', () => {
});

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();
await emittedOnce(w.webContents, 'before-unload-fired');
});
Expand Down Expand Up @@ -193,7 +193,7 @@ describe('BrowserWindow module', () => {
});

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);
await emittedOnce(w.webContents, 'before-unload-fired');
});
Expand Down Expand Up @@ -2640,21 +2640,21 @@ describe('BrowserWindow module', () => {
afterEach(closeAllWindows);

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');
w.close();
await wait;
});

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();
const [, proceed] = await emittedOnce(w.webContents, 'before-unload-fired');
expect(proceed).to.equal(false);
});

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();
const [, proceed] = await emittedOnce(w.webContents, 'before-unload-fired');
expect(proceed).to.equal(false);
Expand All @@ -2666,7 +2666,11 @@ describe('BrowserWindow module', () => {
const destroyListener = () => { expect.fail('Close was not prevented'); };
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();
await emittedOnce(w.webContents, 'before-unload-fired');
w.close();
Expand All @@ -2684,7 +2688,11 @@ describe('BrowserWindow module', () => {
const navigationListener = () => { expect.fail('Reload was not prevented'); };
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();
// Chromium does not emit 'before-unload-fired' on WebContents for
// navigations, so we have to use other ways to know if beforeunload
Expand All @@ -2704,7 +2712,11 @@ describe('BrowserWindow module', () => {
const navigationListener = () => { expect.fail('Reload was not prevented'); };
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');
// Chromium does not emit 'before-unload-fired' on WebContents for
// navigations, so we have to use other ways to know if beforeunload
Expand Down
6 changes: 3 additions & 3 deletions spec-main/api-web-contents-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,23 @@ describe('webContents module', () => {
w.webContents.once('will-prevent-unload', () => {
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');
w.close();
await wait;
});

it('emits if beforeunload returns false', async () => {
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();
await emittedOnce(w.webContents, 'will-prevent-unload');
});

it('supports calling preventDefault on will-prevent-unload events', async () => {
const w = new BrowserWindow({ show: false });
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');
w.close();
await wait;
Expand Down
1 change: 1 addition & 0 deletions spec-main/fixtures/api/beforeunload-false-prevent3.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
e.returnValue = '';
}
})
console.log('installed')
}
</script>
</body>
Expand Down
File renamed without changes.
15 changes: 0 additions & 15 deletions spec-main/fixtures/api/close-beforeunload-false.html

This file was deleted.

0 comments on commit 5918dd6

Please sign in to comment.