diff --git a/spec-main/api-browser-window-spec.ts b/spec-main/api-browser-window-spec.ts index 3e18e69af946..e944cde174a5 100644 --- a/spec-main/api-browser-window-spec.ts +++ b/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')) }) }) diff --git a/spec-main/api-ipc-main-spec.ts b/spec-main/api-ipc-main-spec.ts index 7f963b3e8d82..cf2265dee4f3 100644 --- a/spec-main/api-ipc-main-spec.ts +++ b/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') diff --git a/spec-main/api-remote-spec.ts b/spec-main/api-remote-spec.ts index f8a9cd119410..4dcf7ce60838 100644 --- a/spec-main/api-remote-spec.ts +++ b/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') diff --git a/spec-main/api-web-contents-spec.ts b/spec-main/api-web-contents-spec.ts index eb77f337d31e..a15be8c4404d 100644 --- a/spec-main/api-web-contents-spec.ts +++ b/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() }) }) }) diff --git a/spec/fixtures/api/beforeunload-false-prevent3.html b/spec-main/fixtures/api/beforeunload-false-prevent3.html similarity index 82% rename from spec/fixtures/api/beforeunload-false-prevent3.html rename to spec-main/fixtures/api/beforeunload-false-prevent3.html index 6ed2a7d1aa4e..0c966734b860 100644 --- a/spec/fixtures/api/beforeunload-false-prevent3.html +++ b/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++; diff --git a/spec/fixtures/api/close-beforeunload-empty-string.html b/spec-main/fixtures/api/close-beforeunload-empty-string.html similarity index 83% rename from spec/fixtures/api/close-beforeunload-empty-string.html rename to spec-main/fixtures/api/close-beforeunload-empty-string.html index 644f4f4530bc..9ae9c418fb30 100644 --- a/spec/fixtures/api/close-beforeunload-empty-string.html +++ b/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) { diff --git a/spec/fixtures/api/close-beforeunload-false.html b/spec-main/fixtures/api/close-beforeunload-false.html similarity index 85% rename from spec/fixtures/api/close-beforeunload-false.html rename to spec-main/fixtures/api/close-beforeunload-false.html index 5f519ab5598b..075b97cf68eb 100644 --- a/spec/fixtures/api/close-beforeunload-false.html +++ b/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; diff --git a/spec/fixtures/api/close-beforeunload-undefined.html b/spec-main/fixtures/api/close-beforeunload-undefined.html similarity index 100% rename from spec/fixtures/api/close-beforeunload-undefined.html rename to spec-main/fixtures/api/close-beforeunload-undefined.html diff --git a/spec/fixtures/api/remote-event-handler.html b/spec-main/fixtures/api/remote-event-handler.html similarity index 100% rename from spec/fixtures/api/remote-event-handler.html rename to spec-main/fixtures/api/remote-event-handler.html diff --git a/spec/fixtures/api/render-view-deleted.html b/spec-main/fixtures/api/render-view-deleted.html similarity index 100% rename from spec/fixtures/api/render-view-deleted.html rename to spec-main/fixtures/api/render-view-deleted.html diff --git a/spec/fixtures/api/sandbox.html b/spec-main/fixtures/api/sandbox.html similarity index 100% rename from spec/fixtures/api/sandbox.html rename to spec-main/fixtures/api/sandbox.html diff --git a/spec/fixtures/module/delete-buffer.js b/spec-main/fixtures/module/delete-buffer.js similarity index 100% rename from spec/fixtures/module/delete-buffer.js rename to spec-main/fixtures/module/delete-buffer.js diff --git a/spec/fixtures/module/preload-remote.js b/spec-main/fixtures/module/preload-remote.js similarity index 100% rename from spec/fixtures/module/preload-remote.js rename to spec-main/fixtures/module/preload-remote.js diff --git a/spec/fixtures/module/print_name.js b/spec-main/fixtures/module/print_name.js similarity index 100% rename from spec/fixtures/module/print_name.js rename to spec-main/fixtures/module/print_name.js diff --git a/spec-main/fixtures/pages/webview-devtools.html b/spec-main/fixtures/pages/webview-devtools.html new file mode 100644 index 000000000000..ebbf952c300c --- /dev/null +++ b/spec-main/fixtures/pages/webview-devtools.html @@ -0,0 +1,16 @@ + + + + + + + + + + + diff --git a/spec-main/webview-spec.ts b/spec-main/webview-spec.ts index bf7a3058ea04..28b4d2526c67 100644 --- a/spec-main/webview-spec.ts +++ b/spec-main/webview-spec.ts @@ -164,7 +164,21 @@ describe(' 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') diff --git a/spec/api-remote-spec.js b/spec/api-remote-spec.js index b87c3112a05f..38256d0c1d2e 100644 --- a/spec/api-remote-spec.js +++ b/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', () => { diff --git a/spec/fixtures/api/beforeunload-false.html b/spec/fixtures/api/beforeunload-false.html index 4ba1867ce63d..71c0d319c0f0 100644 --- a/spec/fixtures/api/beforeunload-false.html +++ b/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; diff --git a/spec/fixtures/api/new-window-preload.js b/spec/fixtures/api/new-window-preload.js index 6d9add91f040..cd681ac12a79 100644 --- a/spec/fixtures/api/new-window-preload.js +++ b/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() diff --git a/spec/fixtures/pages/focus-web-contents.html b/spec/fixtures/pages/focus-web-contents.html deleted file mode 100644 index 83675bc1d3e0..000000000000 --- a/spec/fixtures/pages/focus-web-contents.html +++ /dev/null @@ -1,24 +0,0 @@ - - - - - - - - - - - diff --git a/spec/fixtures/pages/webview-devtools.html b/spec/fixtures/pages/webview-devtools.html deleted file mode 100644 index fb54b9b18aeb..000000000000 --- a/spec/fixtures/pages/webview-devtools.html +++ /dev/null @@ -1,31 +0,0 @@ - - - - - - - - - - - diff --git a/spec/fixtures/pages/window-open-size.html b/spec/fixtures/pages/window-open-size.html index 93b5039f7966..2d42fb36101a 100644 --- a/spec/fixtures/pages/window-open-size.html +++ b/spec/fixtures/pages/window-open-size.html @@ -1,8 +1,7 @@