Browse Source

test: remove a bunch of usage of the remote module (#21119)

Jeremy Apthorp 5 years ago
parent
commit
26ecf63ab4

+ 48 - 27
spec-main/api-browser-window-spec.ts

@@ -80,7 +80,12 @@ describe('BrowserWindow module', () => {
     })
     it('should emit beforeunload handler', async () => {
       await w.loadFile(path.join(fixtures, 'api', 'beforeunload-false.html'))
-      const beforeunload = emittedOnce(w, 'onbeforeunload')
+      const beforeunload = new Promise(resolve => {
+        ipcMain.once('onbeforeunload', (e) => {
+          e.returnValue = null
+          resolve()
+        })
+      })
       w.close()
       await beforeunload
     })
@@ -164,8 +169,9 @@ describe('BrowserWindow module', () => {
       expect(content).to.equal('close')
     })
     it('should emit beforeunload event', async () => {
-      w.loadFile(path.join(fixtures, 'api', 'close-beforeunload-false.html'))
-      await emittedOnce(w, 'onbeforeunload')
+      w.loadFile(path.join(__dirname, 'fixtures', 'api', 'close-beforeunload-false.html'))
+      const [e] = await emittedOnce(ipcMain, 'onbeforeunload')
+      e.returnValue = null
     })
   })
 
@@ -1552,7 +1558,7 @@ describe('BrowserWindow module', () => {
         expect(test).to.eql('preload')
       })
       it('can successfully delete the Buffer global', async () => {
-        const preload = path.join(fixtures, 'module', 'delete-buffer.js')
+        const preload = path.join(__dirname, 'fixtures', 'module', 'delete-buffer.js')
         const w = new BrowserWindow({
           show: false,
           webPreferences: {
@@ -1676,7 +1682,7 @@ describe('BrowserWindow module', () => {
     describe('"enableRemoteModule" option', () => {
       const generateSpecs = (description: string, sandbox: boolean) => {
         describe(description, () => {
-          const preload = path.join(fixtures, 'module', 'preload-remote.js')
+          const preload = path.join(__dirname, 'fixtures', 'module', 'preload-remote.js')
 
           it('enables the remote module by default', async () => {
             const w = new BrowserWindow({
@@ -1794,7 +1800,7 @@ describe('BrowserWindow module', () => {
             preload
           }
         })
-        const htmlPath = path.join(fixtures, 'api', 'sandbox.html?exit-event')
+        const htmlPath = path.join(__dirname, 'fixtures', 'api', 'sandbox.html?exit-event')
         const pageUrl = 'file://' + htmlPath
         w.loadURL(pageUrl)
         const [, url] = await emittedOnce(ipcMain, 'answer')
@@ -1815,7 +1821,7 @@ describe('BrowserWindow module', () => {
         w.webContents.once('new-window', (event, url, frameName, disposition, options) => {
           options.webPreferences!.preload = preload
         })
-        const htmlPath = path.join(fixtures, 'api', 'sandbox.html?window-open')
+        const htmlPath = path.join(__dirname, 'fixtures', 'api', 'sandbox.html?window-open')
         const pageUrl = 'file://' + htmlPath
         const answer = emittedOnce(ipcMain, 'answer')
         w.loadURL(pageUrl)
@@ -1844,7 +1850,7 @@ describe('BrowserWindow module', () => {
           options.webPreferences!.preload = preload
         })
         w.loadFile(
-          path.join(fixtures, 'api', 'sandbox.html'),
+          path.join(__dirname, 'fixtures', 'api', 'sandbox.html'),
           { search: 'window-open-external' }
         )
 
@@ -1921,7 +1927,11 @@ describe('BrowserWindow module', () => {
           prefs.foo = 'bar'
         })
         w.loadFile(path.join(fixtures, 'api', 'new-window.html'))
-        const [, , webPreferences] = await emittedOnce(ipcMain, 'answer')
+        const [[, childWebContents]] = await Promise.all([
+          emittedOnce(app, 'web-contents-created'),
+          emittedOnce(ipcMain, 'answer')
+        ])
+        const webPreferences = (childWebContents as any).getLastWebPreferences()
         expect(webPreferences.foo).to.equal('bar')
       })
 
@@ -1952,7 +1962,7 @@ describe('BrowserWindow module', () => {
           'parent-answer',
           'child-answer'
         ], done)
-        w.loadFile(path.join(fixtures, 'api', 'sandbox.html'), { search: 'verify-ipc-sender' })
+        w.loadFile(path.join(__dirname, 'fixtures', 'api', 'sandbox.html'), { search: 'verify-ipc-sender' })
       })
 
       describe('event handling', () => {
@@ -1988,7 +1998,7 @@ describe('BrowserWindow module', () => {
             'did-frame-finish-load',
             'dom-ready'
           ], done)
-          w.loadFile(path.join(fixtures, 'api', 'sandbox.html'), { search: 'webcontents-events' })
+          w.loadFile(path.join(__dirname, 'fixtures', 'api', 'sandbox.html'), { search: 'webcontents-events' })
         })
       })
 
@@ -2021,7 +2031,7 @@ describe('BrowserWindow module', () => {
             sandbox: true
           }
         })
-        w.loadFile(path.join(fixtures, 'api', 'sandbox.html'), { search: 'reload-remote' })
+        w.loadFile(path.join(__dirname, 'fixtures', 'api', 'sandbox.html'), { search: 'reload-remote' })
 
         ipcMain.on('get-remote-module-path', (event) => {
           event.returnValue = path.join(fixtures, 'module', 'hello.js')
@@ -2057,7 +2067,7 @@ describe('BrowserWindow module', () => {
           options.webPreferences!.preload = preload
         })
 
-        w.loadFile(path.join(fixtures, 'api', 'sandbox.html'), { search: 'reload-remote-child' })
+        w.loadFile(path.join(__dirname, 'fixtures', 'api', 'sandbox.html'), { search: 'reload-remote-child' })
 
         ipcMain.on('get-remote-module-path', (event) => {
           event.returnValue = path.join(fixtures, 'module', 'hello-child.js')
@@ -2233,7 +2243,11 @@ describe('BrowserWindow module', () => {
           prefs.foo = 'bar'
         })
         w.loadFile(path.join(fixtures, 'api', 'new-window.html'))
-        const [, , webPreferences] = await emittedOnce(ipcMain, 'answer')
+        const [[, childWebContents]] = await Promise.all([
+          emittedOnce(app, 'web-contents-created'),
+          emittedOnce(ipcMain, 'answer')
+        ])
+        const webPreferences = (childWebContents as any).getLastWebPreferences()
         expect(webPreferences.foo).to.equal('bar')
       })
       it('should have nodeIntegration disabled in child windows', async () => {
@@ -2359,22 +2373,27 @@ describe('BrowserWindow module', () => {
     beforeEach(() => {
       w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true } })
     })
+    afterEach(() => {
+      ipcMain.removeAllListeners('onbeforeunload')
+    })
     afterEach(closeAllWindows)
     it('returning undefined would not prevent close', (done) => {
       w.once('closed', () => { done() })
-      w.loadFile(path.join(fixtures, 'api', 'close-beforeunload-undefined.html'))
+      w.loadFile(path.join(__dirname, 'fixtures', 'api', 'close-beforeunload-undefined.html'))
     })
-    it('returning false would prevent close', (done) => {
-      w.once('onbeforeunload' as any, () => { done() })
-      w.loadFile(path.join(fixtures, 'api', 'close-beforeunload-false.html'))
+    it('returning false would prevent close', async () => {
+      w.loadFile(path.join(__dirname, 'fixtures', 'api', 'close-beforeunload-false.html'))
+      const [e] = await emittedOnce(ipcMain, 'onbeforeunload')
+      e.returnValue = null
     })
     it('returning empty string would prevent close', (done) => {
-      w.once('onbeforeunload' as any, () => { done() })
-      w.loadFile(path.join(fixtures, 'api', 'close-beforeunload-empty-string.html'))
+      ipcMain.once('onbeforeunload', (e) => { e.returnValue = null; done() })
+      w.loadFile(path.join(__dirname, 'fixtures', 'api', 'close-beforeunload-empty-string.html'))
     })
     it('emits for each close attempt', (done) => {
       let beforeUnloadCount = 0
-      w.on('onbeforeunload' as any, () => {
+      ipcMain.on('onbeforeunload', (e) => {
+        e.returnValue = null
         beforeUnloadCount += 1
         if (beforeUnloadCount < 3) {
           w.close()
@@ -2382,12 +2401,13 @@ describe('BrowserWindow module', () => {
           done()
         }
       })
-      w.webContents.once('did-finish-load', () => { w.close() })
-      w.loadFile(path.join(fixtures, 'api', 'beforeunload-false-prevent3.html'))
+      w.webContents.once('did-finish-load', () => { w.webContents.executeJavaScript('window.close()', true) })
+      w.loadFile(path.join(__dirname, 'fixtures', 'api', 'beforeunload-false-prevent3.html'))
     })
     it('emits for each reload attempt', (done) => {
       let beforeUnloadCount = 0
-      w.on('onbeforeunload' as any, () => {
+      ipcMain.on('onbeforeunload', (e) => {
+        e.returnValue = null
         beforeUnloadCount += 1
         if (beforeUnloadCount < 3) {
           w.reload()
@@ -2401,11 +2421,12 @@ describe('BrowserWindow module', () => {
         })
         w.reload()
       })
-      w.loadFile(path.join(fixtures, 'api', 'beforeunload-false-prevent3.html'))
+      w.loadFile(path.join(__dirname, 'fixtures', 'api', 'beforeunload-false-prevent3.html'))
     })
     it('emits for each navigation attempt', (done) => {
       let beforeUnloadCount = 0
-      w.on('onbeforeunload' as any, () => {
+      ipcMain.on('onbeforeunload', (e) => {
+        e.returnValue = null
         beforeUnloadCount += 1
         if (beforeUnloadCount < 3) {
           w.loadURL('about:blank')
@@ -2419,7 +2440,7 @@ describe('BrowserWindow module', () => {
         })
         w.loadURL('about:blank')
       })
-      w.loadFile(path.join(fixtures, 'api', 'beforeunload-false-prevent3.html'))
+      w.loadFile(path.join(__dirname, 'fixtures', 'api', 'beforeunload-false-prevent3.html'))
     })
   })
 

+ 0 - 18
spec-main/api-ipc-main-spec.ts

@@ -45,24 +45,6 @@ describe('ipc main module', () => {
     })
   })
 
-  describe('remote objects registry', () => {
-    it('does not dereference until the render view is deleted (regression)', (done) => {
-      const w = new BrowserWindow({
-        show: false,
-        webPreferences: {
-          nodeIntegration: true
-        }
-      })
-
-      ipcMain.once('error-message', (event, message) => {
-        expect(message).to.match(/^Cannot call method 'getURL' on missing remote object/)
-        done()
-      })
-
-      w.loadFile(path.join(fixtures, 'api', 'render-view-deleted.html'))
-    })
-  })
-
   describe('ipcMain.on', () => {
     it('is not used for internals', async () => {
       const appPath = path.join(fixtures, 'api', 'ipc-main-listeners')

+ 19 - 1
spec-main/api-remote-spec.ts

@@ -158,6 +158,24 @@ ifdescribe(features.isRemoteModuleEnabled())('remote module', () => {
     })
   })
 
+  describe('remote objects registry', () => {
+    it('does not dereference until the render view is deleted (regression)', (done) => {
+      const w = new BrowserWindow({
+        show: false,
+        webPreferences: {
+          nodeIntegration: true
+        }
+      })
+
+      ipcMain.once('error-message', (event, message) => {
+        expect(message).to.match(/^Cannot call method 'getURL' on missing remote object/)
+        done()
+      })
+
+      w.loadFile(path.join(fixtures, 'api', 'render-view-deleted.html'))
+    })
+  })
+
   describe('remote listeners', () => {
     afterEach(closeAllWindows)
 
@@ -168,7 +186,7 @@ ifdescribe(features.isRemoteModuleEnabled())('remote module', () => {
           nodeIntegration: true
         }
       })
-      await w.loadFile(path.join(__dirname, '..', 'spec', 'fixtures', 'api', 'remote-event-handler.html'))
+      await w.loadFile(path.join(fixtures, 'api', 'remote-event-handler.html'))
       w.webContents.reload()
       await emittedOnce(w.webContents, 'did-finish-load')
 

+ 18 - 14
spec-main/api-web-contents-spec.ts

@@ -47,20 +47,20 @@ describe('webContents module', () => {
       w.webContents.once('will-prevent-unload', () => {
         expect.fail('should not have fired')
       })
-      w.loadFile(path.join(fixturesPath, 'api', 'close-beforeunload-undefined.html'))
+      w.loadFile(path.join(__dirname, 'fixtures', 'api', 'close-beforeunload-undefined.html'))
     })
 
-    it('emits if beforeunload returns false', (done) => {
+    it('emits if beforeunload returns false', async () => {
       const w = new BrowserWindow({ show: false })
-      w.webContents.once('will-prevent-unload', () => done())
-      w.loadFile(path.join(fixturesPath, 'api', 'close-beforeunload-false.html'))
+      w.loadFile(path.join(__dirname, 'fixtures', 'api', 'close-beforeunload-false.html'))
+      await emittedOnce(w.webContents, 'will-prevent-unload')
     })
 
-    it('supports calling preventDefault on will-prevent-unload events', (done) => {
+    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())
-      w.once('closed', () => done())
-      w.loadFile(path.join(fixturesPath, 'api', 'close-beforeunload-false.html'))
+      w.loadFile(path.join(__dirname, 'fixtures', 'api', 'close-beforeunload-false.html'))
+      await emittedOnce(w, 'closed')
     })
   })
 
@@ -695,15 +695,19 @@ describe('webContents module', () => {
   describe('focus()', () => {
     describe('when the web contents is hidden', () => {
       afterEach(closeAllWindows)
-      it('does not blur the focused window', (done) => {
+      it('does not blur the focused window', async () => {
         const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true } })
-        ipcMain.once('answer', (event, parentFocused, childFocused) => {
-          expect(parentFocused).to.be.true()
-          expect(childFocused).to.be.false()
-          done()
-        })
         w.show()
-        w.loadFile(path.join(fixturesPath, 'pages', 'focus-web-contents.html'))
+        await w.loadURL('about:blank')
+        w.focus()
+        const child = new BrowserWindow({ show: false })
+        child.loadURL('about:blank')
+        child.webContents.focus()
+        const currentFocused = w.isFocused()
+        const childFocused = child.isFocused()
+        child.close()
+        expect(currentFocused).to.be.true()
+        expect(childFocused).to.be.false()
       })
     })
   })

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

@@ -5,7 +5,7 @@
   var unloadPreventedCount = 0;
   window.onbeforeunload = function() {
     setTimeout(function() {
-      require('electron').remote.getCurrentWindow().emit('onbeforeunload');
+      require('electron').ipcRenderer.sendSync('onbeforeunload')
     }, 0);
     if (unloadPreventedCount < 3) {
       unloadPreventedCount++;

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

@@ -5,7 +5,7 @@
   var unloadPrevented = false;
   window.onbeforeunload = function() {
     setTimeout(function() {
-      require('electron').remote.getCurrentWindow().emit('onbeforeunload');
+      require('electron').ipcRenderer.sendSync('onbeforeunload');
     }, 0);
 
     if (!unloadPrevented) {

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

@@ -5,7 +5,7 @@
   var unloadPrevented = false;
   window.onbeforeunload = function() {
     setTimeout(function() {
-      require('electron').remote.getCurrentWindow().emit('onbeforeunload');
+      require('electron').ipcRenderer.sendSync('onbeforeunload');
     }, 0);
     if (!unloadPrevented) {
       unloadPrevented = true;

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


+ 0 - 0
spec/fixtures/api/remote-event-handler.html → spec-main/fixtures/api/remote-event-handler.html


+ 0 - 0
spec/fixtures/api/render-view-deleted.html → spec-main/fixtures/api/render-view-deleted.html


+ 0 - 0
spec/fixtures/api/sandbox.html → spec-main/fixtures/api/sandbox.html


+ 0 - 0
spec/fixtures/module/delete-buffer.js → spec-main/fixtures/module/delete-buffer.js


+ 0 - 0
spec/fixtures/module/preload-remote.js → spec-main/fixtures/module/preload-remote.js


+ 0 - 0
spec/fixtures/module/print_name.js → spec-main/fixtures/module/print_name.js


+ 16 - 0
spec-main/fixtures/pages/webview-devtools.html

@@ -0,0 +1,16 @@
+<!DOCTYPE html>
+<html>
+  <head>
+    <meta charset="utf-8">
+  </head>
+  <body>
+    <webview nodeintegration src="./a.html"></webview>
+    <script>
+        var wv = document.querySelector('webview')
+        wv.addEventListener('dom-ready', () => {
+          wv.openDevTools()
+        })
+      </script>
+    </script>
+  </body>
+</html>

+ 15 - 1
spec-main/webview-spec.ts

@@ -164,7 +164,21 @@ describe('<webview> tag', function () {
     const extensionPath = path.join(fixtures, 'devtools-extensions', 'foo')
     BrowserWindow.addDevToolsExtension(extensionPath)
 
-    w.loadFile(path.join(fixtures, 'pages', 'webview-devtools.html'))
+    w.loadFile(path.join(__dirname, 'fixtures', 'pages', 'webview-devtools.html'))
+    app.once('web-contents-created', (e, webContents) => {
+      webContents.on('devtools-opened', function () {
+        const showPanelIntervalId = setInterval(function () {
+          if (!webContents.isDestroyed() && webContents.devToolsWebContents) {
+            webContents.devToolsWebContents.executeJavaScript('(' + function () {
+              const lastPanelId: any = (window as any).UI.inspectorView._tabbedPane._tabs.peekLast().id;
+              (window as any).UI.inspectorView.showPanel(lastPanelId)
+            }.toString() + ')()')
+          } else {
+            clearInterval(showPanelIntervalId)
+          }
+        }, 100)
+      })
+    })
 
     const [, { runtimeId, tabId }] = await emittedOnce(ipcMain, 'answer')
     expect(runtimeId).to.equal('foo')

+ 1 - 1
spec/api-remote-spec.js

@@ -233,7 +233,7 @@ ifdescribe(features.isRemoteModuleEnabled())('remote module', () => {
   })
 
   describe('remote value in browser', () => {
-    const print = path.join(fixtures, 'module', 'print_name.js')
+    const print = path.join(__dirname, '..', 'spec-main', 'fixtures', 'module', 'print_name.js')
     const printName = remote.require(print)
 
     it('preserves NaN', () => {

+ 1 - 1
spec/fixtures/api/beforeunload-false.html

@@ -5,7 +5,7 @@
   var unloadPrevented = false;
   window.onbeforeunload = function() {
     setTimeout(function() {
-      require('electron').remote.getCurrentWindow().emit('onbeforeunload');
+      require('electron').ipcRenderer.sendSync('onbeforeunload');
     }, 0);
     if (!unloadPrevented) {
       unloadPrevented = true;

+ 2 - 2
spec/fixtures/api/new-window-preload.js

@@ -1,4 +1,4 @@
-const { ipcRenderer, remote } = require('electron')
+const { ipcRenderer } = require('electron')
 
-ipcRenderer.send('answer', process.argv, remote.getCurrentWindow().webContents.getWebPreferences())
+ipcRenderer.send('answer', process.argv)
 window.close()

+ 0 - 24
spec/fixtures/pages/focus-web-contents.html

@@ -1,24 +0,0 @@
-<!DOCTYPE html>
-<html>
-  <head>
-    <meta charset="utf-8">
-    <title></title>
-    <script>
-      const {ipcRenderer, remote} = require('electron')
-
-      remote.getCurrentWindow().focus()
-
-      const child = new remote.BrowserWindow({show: false})
-      child.loadURL('about:blank')
-      child.webContents.focus()
-
-      const currentFocused = remote.getCurrentWindow().isFocused()
-      const childFocused = child.isFocused()
-      child.close()
-      ipcRenderer.send('answer', currentFocused, childFocused)
-    </script>
-  </head>
-  <body>
-
-  </body>
-</html>

+ 0 - 31
spec/fixtures/pages/webview-devtools.html

@@ -1,31 +0,0 @@
-<!DOCTYPE html>
-<html>
-  <head>
-    <meta charset="utf-8">
-  </head>
-  <body>
-    <webview nodeintegration src="./a.html"></webview>
-    <script>
-        var wv = document.querySelector('webview')
-        wv.addEventListener('dom-ready', () => {
-          const { remote } = require('electron')
-          const webContents = remote.webContents.fromId(wv.getWebContentsId())
-          webContents.on('devtools-opened', function () {
-            var showPanelIntevalId = setInterval(function () {
-              if (webContents.devToolsWebContents) {
-                webContents.devToolsWebContents.executeJavaScript('(' + (function () {
-                  var lastPanelId = UI.inspectorView._tabbedPane._tabs.peekLast().id
-                  UI.inspectorView.showPanel(lastPanelId)
-                }).toString() + ')()')
-              } else {
-                clearInterval(showPanelIntevalId)
-              }
-            }, 100)
-          })
-
-          wv.openDevTools()
-        })
-      </script>
-    </script>
-  </body>
-</html>

+ 1 - 2
spec/fixtures/pages/window-open-size.html

@@ -1,8 +1,7 @@
 <html>
 <body>
 <script type="text/javascript" charset="utf-8">
-  var size = require('electron').remote.getCurrentWindow().getSize();
-  window.opener.postMessage('size: ' + size[0] + ' ' + size[1], '*')
+  window.opener.postMessage('size: ' + outerWidth + ' ' + outerHeight, '*')
 </script>
 </body>
 </html>