From fa5d10bc0b4f3d65e120ec8942ac4791efbad33e Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Mon, 1 Jun 2020 19:17:21 +0900 Subject: [PATCH] test: wait for beforeunload handler to be installed --- spec-main/api-browser-window-spec.ts | 28 +++++++++++++------ spec-main/api-web-contents-spec.ts | 6 ++-- ...ng.html => beforeunload-empty-string.html} | 0 .../api/beforeunload-false-prevent3.html | 1 + .../fixtures/api/beforeunload-false.html | 0 ...fined.html => beforeunload-undefined.html} | 0 .../api/close-beforeunload-false.html | 15 ---------- 7 files changed, 24 insertions(+), 26 deletions(-) rename spec-main/fixtures/api/{close-beforeunload-empty-string.html => beforeunload-empty-string.html} (100%) rename {spec => spec-main}/fixtures/api/beforeunload-false.html (100%) rename spec-main/fixtures/api/{close-beforeunload-undefined.html => beforeunload-undefined.html} (100%) delete mode 100644 spec-main/fixtures/api/close-beforeunload-false.html diff --git a/spec-main/api-browser-window-spec.ts b/spec-main/api-browser-window-spec.ts index c4c8b9bd3ce4f..15b1122ed9933 100644 --- a/spec-main/api-browser-window-spec.ts +++ b/spec-main/api-browser-window-spec.ts @@ -101,7 +101,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'); }); @@ -186,7 +186,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'); }); @@ -2633,21 +2633,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); @@ -2659,7 +2659,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(); @@ -2677,7 +2681,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 @@ -2697,7 +2705,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 diff --git a/spec-main/api-web-contents-spec.ts b/spec-main/api-web-contents-spec.ts index 6e39d231a02d6..1cb26c66cf5cd 100644 --- a/spec-main/api-web-contents-spec.ts +++ b/spec-main/api-web-contents-spec.ts @@ -48,7 +48,7 @@ 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; @@ -56,7 +56,7 @@ describe('webContents module', () => { 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'); }); @@ -64,7 +64,7 @@ describe('webContents module', () => { 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; diff --git a/spec-main/fixtures/api/close-beforeunload-empty-string.html b/spec-main/fixtures/api/beforeunload-empty-string.html similarity index 100% rename from spec-main/fixtures/api/close-beforeunload-empty-string.html rename to spec-main/fixtures/api/beforeunload-empty-string.html diff --git a/spec-main/fixtures/api/beforeunload-false-prevent3.html b/spec-main/fixtures/api/beforeunload-false-prevent3.html index 39a744b8f0652..98a069ee3bd46 100644 --- a/spec-main/fixtures/api/beforeunload-false-prevent3.html +++ b/spec-main/fixtures/api/beforeunload-false-prevent3.html @@ -10,6 +10,7 @@ e.returnValue = ''; } }) + console.log('installed') } diff --git a/spec/fixtures/api/beforeunload-false.html b/spec-main/fixtures/api/beforeunload-false.html similarity index 100% rename from spec/fixtures/api/beforeunload-false.html rename to spec-main/fixtures/api/beforeunload-false.html diff --git a/spec-main/fixtures/api/close-beforeunload-undefined.html b/spec-main/fixtures/api/beforeunload-undefined.html similarity index 100% rename from spec-main/fixtures/api/close-beforeunload-undefined.html rename to spec-main/fixtures/api/beforeunload-undefined.html diff --git a/spec-main/fixtures/api/close-beforeunload-false.html b/spec-main/fixtures/api/close-beforeunload-false.html deleted file mode 100644 index 6401ccc26945d..0000000000000 --- a/spec-main/fixtures/api/close-beforeunload-false.html +++ /dev/null @@ -1,15 +0,0 @@ - - - - -