Browse Source

test: wait for beforeunload handler to be installed (#23874)

Cheng Zhao 4 years ago
parent
commit
5918dd6e65

+ 20 - 8
spec-main/api-browser-window-spec.ts

@@ -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');
     });
@@ -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');
     });
@@ -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);
@@ -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();
@@ -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
@@ -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

+ 3 - 3
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;

+ 0 - 0
spec-main/fixtures/api/close-beforeunload-empty-string.html → spec-main/fixtures/api/beforeunload-empty-string.html


+ 1 - 0
spec-main/fixtures/api/beforeunload-false-prevent3.html

@@ -10,6 +10,7 @@
         e.returnValue = '';
       }
     })
+    console.log('installed')
   }
 </script>
 </body>

+ 0 - 0
spec/fixtures/api/beforeunload-false.html → spec-main/fixtures/api/beforeunload-false.html


+ 0 - 0
spec-main/fixtures/api/close-beforeunload-undefined.html → spec-main/fixtures/api/beforeunload-undefined.html


+ 0 - 15
spec-main/fixtures/api/close-beforeunload-false.html

@@ -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>