import * as chai from 'chai' import * as chaiAsPromised from 'chai-as-promised' import * as path from 'path' import * as fs from 'fs' import * as os from 'os' import * as qs from 'querystring' import * as http from 'http' import { AddressInfo } from 'net' import { app, BrowserWindow, BrowserView, ipcMain, OnBeforeSendHeadersListenerDetails, protocol, screen, webContents, session, WebContents } from 'electron' import { emittedOnce } from './events-helpers' import { ifit, ifdescribe } from './spec-helpers' import { closeWindow } from './window-helpers' import dirtyChai = require('dirty-chai') const { expect } = chai chai.use(chaiAsPromised) chai.use(dirtyChai) const fixtures = path.resolve(__dirname, '..', 'spec', 'fixtures') // Is the display's scale factor possibly causing rounding of pixel coordinate // values? const isScaleFactorRounding = () => { const { scaleFactor } = screen.getPrimaryDisplay() // Return true if scale factor is non-integer value if (Math.round(scaleFactor) !== scaleFactor) return true // Return true if scale factor is odd number above 2 return scaleFactor > 2 && scaleFactor % 2 === 1 } const expectBoundsEqual = (actual: any, expected: any) => { if (!isScaleFactorRounding()) { expect(expected).to.deep.equal(actual) } else if (Array.isArray(actual)) { expect(actual[0]).to.be.closeTo(expected[0], 1) expect(actual[1]).to.be.closeTo(expected[1], 1) } else { expect(actual.x).to.be.closeTo(expected.x, 1) expect(actual.y).to.be.closeTo(expected.y, 1) expect(actual.width).to.be.closeTo(expected.width, 1) expect(actual.height).to.be.closeTo(expected.height, 1) } } const closeAllWindows = async () => { for (const w of BrowserWindow.getAllWindows()) { await closeWindow(w, { assertNotWindows: false }) } } describe('BrowserWindow module', () => { describe('BrowserWindow constructor', () => { it('allows passing void 0 as the webContents', async () => { expect(() => { const w = new BrowserWindow({ show: false, // apparently void 0 had different behaviour from undefined in the // issue that this test is supposed to catch. webContents: void 0 } as any) w.destroy() }).not.to.throw() }) }) describe('BrowserWindow.close()', () => { let w = null as unknown as BrowserWindow beforeEach(() => { w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true } }) }) afterEach(async () => { await closeWindow(w) w = null as unknown as BrowserWindow }) it('should emit unload handler', async () => { await w.loadFile(path.join(fixtures, 'api', 'unload.html')) const closed = emittedOnce(w, 'closed') w.close() await closed const test = path.join(fixtures, 'api', 'unload') const content = fs.readFileSync(test) fs.unlinkSync(test) expect(String(content)).to.equal('unload') }) it('should emit beforeunload handler', async () => { await w.loadFile(path.join(fixtures, 'api', 'beforeunload-false.html')) const beforeunload = emittedOnce(w, 'onbeforeunload') w.close() await beforeunload }) describe('when invoked synchronously inside navigation observer', () => { let server: http.Server = null as unknown as http.Server let url: string = null as unknown as string before((done) => { server = http.createServer((request, response) => { switch (request.url) { case '/net-error': response.destroy() break case '/301': response.statusCode = 301 response.setHeader('Location', '/200') response.end() break case '/200': response.statusCode = 200 response.end('hello') break case '/title': response.statusCode = 200 response.end('Hello') break default: throw new Error(`unsupported endpoint: ${request.url}`) } }).listen(0, '127.0.0.1', () => { url = 'http://127.0.0.1:' + (server.address() as AddressInfo).port done() }) }) after(() => { server.close() }) const events = [ { name: 'did-start-loading', path: '/200' }, { name: 'dom-ready', path: '/200' }, { name: 'page-title-updated', path: '/title' }, { name: 'did-stop-loading', path: '/200' }, { name: 'did-finish-load', path: '/200' }, { name: 'did-frame-finish-load', path: '/200' }, { name: 'did-fail-load', path: '/net-error' } ] for (const { name, path } of events) { it(`should not crash when closed during ${name}`, async () => { const w = new BrowserWindow({ show: false }) w.webContents.once((name as any), () => { w.close() }) const destroyed = emittedOnce(w.webContents, 'destroyed') w.webContents.loadURL(url + path) await destroyed }) } }) }) describe('window.close()', () => { let w = null as unknown as BrowserWindow beforeEach(() => { w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true } }) }) afterEach(async () => { await closeWindow(w) w = null as unknown as BrowserWindow }) it('should emit unload event', async () => { w.loadFile(path.join(fixtures, 'api', 'close.html')) await emittedOnce(w, 'closed') const test = path.join(fixtures, 'api', 'close') const content = fs.readFileSync(test).toString() fs.unlinkSync(test) 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') }) }) describe('BrowserWindow.destroy()', () => { let w = null as unknown as BrowserWindow beforeEach(() => { w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true } }) }) afterEach(async () => { await closeWindow(w) w = null as unknown as BrowserWindow }) it('prevents users to access methods of webContents', async () => { const contents = w.webContents w.destroy() await new Promise(setImmediate) expect(() => { contents.getProcessId() }).to.throw('Object has been destroyed') }) it('should not crash when destroying windows with pending events', () => { const focusListener = () => {} app.on('browser-window-focus', focusListener) const windowCount = 3 const windowOptions = { show: false, width: 400, height: 400, webPreferences: { backgroundThrottling: false } } const windows = Array.from(Array(windowCount)).map(() => new BrowserWindow(windowOptions)) windows.forEach(win => win.show()) windows.forEach(win => win.focus()) windows.forEach(win => win.destroy()) app.removeListener('browser-window-focus', focusListener) }) }) describe('BrowserWindow.loadURL(url)', () => { let w = null as unknown as BrowserWindow const scheme = 'other' const srcPath = path.join(fixtures, 'api', 'loaded-from-dataurl.js') before((done) => { protocol.registerFileProtocol(scheme, (request, callback) => { callback(srcPath) }, (error) => done(error)) }) after(() => { protocol.unregisterProtocol(scheme) }) beforeEach(() => { w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true } }) }) afterEach(async () => { await closeWindow(w) w = null as unknown as BrowserWindow }) let server = null as unknown as http.Server let url = null as unknown as string let postData = null as any before((done) => { const filePath = path.join(fixtures, 'pages', 'a.html') const fileStats = fs.statSync(filePath) postData = [ { type: 'rawData', bytes: Buffer.from('username=test&file=') }, { type: 'file', filePath: filePath, offset: 0, length: fileStats.size, modificationTime: fileStats.mtime.getTime() / 1000 } ] server = http.createServer((req, res) => { function respond () { if (req.method === 'POST') { let body = '' req.on('data', (data) => { if (data) body += data }) req.on('end', () => { const parsedData = qs.parse(body) fs.readFile(filePath, (err, data) => { if (err) return if (parsedData.username === 'test' && parsedData.file === data.toString()) { res.end() } }) }) } else if (req.url === '/302') { res.setHeader('Location', '/200') res.statusCode = 302 res.end() } else { res.end() } } setTimeout(respond, req.url && req.url.includes('slow') ? 200 : 0) }) server.listen(0, '127.0.0.1', () => { url = `http://127.0.0.1:${(server.address() as AddressInfo).port}` done() }) }) after(() => { server.close() }) it('should emit did-start-loading event', (done) => { w.webContents.on('did-start-loading', () => { done() }) w.loadURL('about:blank') }) it('should emit ready-to-show event', (done) => { w.on('ready-to-show', () => { done() }) w.loadURL('about:blank') }) it('should emit did-fail-load event for files that do not exist', (done) => { w.webContents.on('did-fail-load', (event, code, desc, url, isMainFrame) => { expect(code).to.equal(-6) expect(desc).to.equal('ERR_FILE_NOT_FOUND') expect(isMainFrame).to.equal(true) done() }) w.loadURL('file://a.txt') }) it('should emit did-fail-load event for invalid URL', (done) => { w.webContents.on('did-fail-load', (event, code, desc, url, isMainFrame) => { expect(desc).to.equal('ERR_INVALID_URL') expect(code).to.equal(-300) expect(isMainFrame).to.equal(true) done() }) w.loadURL('http://example:port') }) it('should set `mainFrame = false` on did-fail-load events in iframes', (done) => { w.webContents.on('did-fail-load', (event, code, desc, url, isMainFrame) => { expect(isMainFrame).to.equal(false) done() }) w.loadFile(path.join(fixtures, 'api', 'did-fail-load-iframe.html')) }) it('does not crash in did-fail-provisional-load handler', (done) => { w.webContents.once('did-fail-provisional-load', () => { w.loadURL('http://127.0.0.1:11111') done() }) w.loadURL('http://127.0.0.1:11111') }) it('should emit did-fail-load event for URL exceeding character limit', (done) => { w.webContents.on('did-fail-load', (event, code, desc, url, isMainFrame) => { expect(desc).to.equal('ERR_INVALID_URL') expect(code).to.equal(-300) expect(isMainFrame).to.equal(true) done() }) const data = Buffer.alloc(2 * 1024 * 1024).toString('base64') w.loadURL(`data:image/png;base64,${data}`) }) it('should return a promise', () => { const p = w.loadURL('about:blank') expect(p).to.have.property('then') }) it('should return a promise that resolves', async () => { await expect(w.loadURL('about:blank')).to.eventually.be.fulfilled() }) it('should return a promise that rejects on a load failure', async () => { const data = Buffer.alloc(2 * 1024 * 1024).toString('base64') const p = w.loadURL(`data:image/png;base64,${data}`) await expect(p).to.eventually.be.rejected }) it('should return a promise that resolves even if pushState occurs during navigation', async () => { const p = w.loadURL('data:text/html,') await expect(p).to.eventually.be.fulfilled }) // FIXME(robo/nornagon): re-enable these once service workers work describe.skip('POST navigations', () => { afterEach(() => { w.webContents.session.webRequest.onBeforeSendHeaders(null) }) it('supports specifying POST data', async () => { await w.loadURL(url, { postData }) }) it('sets the content type header on URL encoded forms', async () => { await w.loadURL(url) const requestDetails: Promise = new Promise(resolve => { w.webContents.session.webRequest.onBeforeSendHeaders((details) => { resolve(details) }) }) w.webContents.executeJavaScript(` form = document.createElement('form') document.body.appendChild(form) form.method = 'POST' form.submit() `) const details = await requestDetails expect(details.requestHeaders['Content-Type']).to.equal('application/x-www-form-urlencoded') }) it('sets the content type header on multi part forms', async () => { await w.loadURL(url) const requestDetails: Promise = new Promise(resolve => { w.webContents.session.webRequest.onBeforeSendHeaders((details) => { resolve(details) }) }) w.webContents.executeJavaScript(` form = document.createElement('form') document.body.appendChild(form) form.method = 'POST' form.enctype = 'multipart/form-data' file = document.createElement('input') file.type = 'file' file.name = 'file' form.appendChild(file) form.submit() `) const details = await requestDetails expect(details.requestHeaders['Content-Type'].startsWith('multipart/form-data; boundary=----WebKitFormBoundary')).to.equal(true) }) }) it('should support support base url for data urls', (done) => { ipcMain.once('answer', (event, test) => { expect(test).to.equal('test') done() }) w.loadURL('data:text/html,', { baseURLForDataURL: `other://${path.join(fixtures, 'api')}${path.sep}` }) }) }) describe('navigation events', () => { let w = null as unknown as BrowserWindow beforeEach(() => { w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true } }) }) afterEach(async () => { await closeWindow(w) w = null as unknown as BrowserWindow }) describe('will-navigate event', () => { it('allows the window to be closed from the event listener', (done) => { w.webContents.once('will-navigate', () => { w.close() done() }) w.loadFile(path.join(fixtures, 'pages', 'will-navigate.html')) }) }) describe('will-redirect event', () => { let server = null as unknown as http.Server let url = null as unknown as string before((done) => { server = http.createServer((req, res) => { if (req.url === '/302') { res.setHeader('Location', '/200') res.statusCode = 302 res.end() } else if (req.url === '/navigate-302') { res.end(``) } else { res.end() } }) server.listen(0, '127.0.0.1', () => { url = `http://127.0.0.1:${(server.address() as AddressInfo).port}` done() }) }) after(() => { server.close() }) it('is emitted on redirects', (done) => { w.webContents.on('will-redirect', () => { done() }) w.loadURL(`${url}/302`) }) it('is emitted after will-navigate on redirects', (done) => { let navigateCalled = false w.webContents.on('will-navigate', () => { navigateCalled = true }) w.webContents.on('will-redirect', () => { expect(navigateCalled).to.equal(true, 'should have called will-navigate first') done() }) w.loadURL(`${url}/navigate-302`) }) it('is emitted before did-stop-loading on redirects', (done) => { let stopCalled = false w.webContents.on('did-stop-loading', () => { stopCalled = true }) w.webContents.on('will-redirect', () => { expect(stopCalled).to.equal(false, 'should not have called did-stop-loading first') done() }) w.loadURL(`${url}/302`) }) it('allows the window to be closed from the event listener', (done) => { w.webContents.once('will-redirect', () => { w.close() done() }) w.loadURL(`${url}/302`) }) it('can be prevented', (done) => { w.webContents.once('will-redirect', (event) => { event.preventDefault() }) w.webContents.on('will-navigate', (e, u) => { expect(u).to.equal(`${url}/302`) }) w.webContents.on('did-stop-loading', () => { expect(w.webContents.getURL()).to.equal( `${url}/navigate-302`, 'url should not have changed after navigation event' ) done() }) w.webContents.on('will-redirect', (e, u) => { expect(u).to.equal(`${url}/200`) }) w.loadURL(`${url}/navigate-302`) }) }) }) describe('focus and visibility', () => { let w = null as unknown as BrowserWindow beforeEach(() => { w = new BrowserWindow({ show: false }) }) afterEach(async () => { await closeWindow(w) w = null as unknown as BrowserWindow }) describe('BrowserWindow.show()', () => { it('should focus on window', () => { w.show() expect(w.isFocused()).to.equal(true) }) it('should make the window visible', () => { w.show() expect(w.isVisible()).to.equal(true) }) it('emits when window is shown', (done) => { w.once('show', () => { expect(w.isVisible()).to.equal(true) done() }) w.show() }) }) describe('BrowserWindow.hide()', () => { it('should defocus on window', () => { w.hide() expect(w.isFocused()).to.equal(false) }) it('should make the window not visible', () => { w.show() w.hide() expect(w.isVisible()).to.equal(false) }) it('emits when window is hidden', async () => { const shown = emittedOnce(w, 'show') w.show() await shown const hidden = emittedOnce(w, 'hide') w.hide() await hidden expect(w.isVisible()).to.equal(false) }) }) describe('BrowserWindow.showInactive()', () => { it('should not focus on window', () => { w.showInactive() expect(w.isFocused()).to.equal(false) }) }) describe('BrowserWindow.focus()', () => { it('does not make the window become visible', () => { expect(w.isVisible()).to.equal(false) w.focus() expect(w.isVisible()).to.equal(false) }) }) describe('BrowserWindow.blur()', () => { it('removes focus from window', () => { w.blur() expect(w.isFocused()).to.equal(false) }) }) describe('BrowserWindow.getFocusedWindow()', () => { it('returns the opener window when dev tools window is focused', (done) => { w.show() w.webContents.once('devtools-focused', () => { expect(BrowserWindow.getFocusedWindow()).to.equal(w) done() }) w.webContents.openDevTools({ mode: 'undocked' }) }) }) describe('BrowserWindow.moveTop()', () => { it('should not steal focus', async () => { const posDelta = 50 const wShownInactive = emittedOnce(w, 'show') w.showInactive() await wShownInactive expect(w.isFocused()).to.equal(false) const otherWindow = new BrowserWindow({ show: false, title: 'otherWindow' }) const otherWindowShown = emittedOnce(otherWindow, 'show') const otherWindowFocused = emittedOnce(otherWindow, 'focus') otherWindow.show() await otherWindowShown await otherWindowFocused expect(otherWindow.isFocused()).to.equal(true) w.moveTop() const wPos = w.getPosition() const wMoving = emittedOnce(w, 'move') w.setPosition(wPos[0] + posDelta, wPos[1] + posDelta) await wMoving expect(w.isFocused()).to.equal(false) expect(otherWindow.isFocused()).to.equal(true) const wFocused = emittedOnce(w, 'focus') w.focus() await wFocused expect(w.isFocused()).to.equal(true) otherWindow.moveTop() const otherWindowPos = otherWindow.getPosition() const otherWindowMoving = emittedOnce(otherWindow, 'move') otherWindow.setPosition(otherWindowPos[0] + posDelta, otherWindowPos[1] + posDelta) await otherWindowMoving expect(otherWindow.isFocused()).to.equal(false) expect(w.isFocused()).to.equal(true) await closeWindow(otherWindow, { assertNotWindows: false }) expect(BrowserWindow.getAllWindows()).to.have.lengthOf(1) }) }) describe('BrowserWindow.moveAbove(mediaSourceId)', () => { it('should throw an exception if wrong formatting', async () => { const fakeSourceIds = [ 'none', 'screen:0', 'window:fake', 'window:1234', 'foobar:1:2' ] fakeSourceIds.forEach((sourceId) => { expect(() => { w.moveAbove(sourceId) }).to.throw(/Invalid media source id/) }) }) it('should throw an exception if wrong type', async () => { const fakeSourceIds = [null as any, 123 as any] fakeSourceIds.forEach((sourceId) => { expect(() => { w.moveAbove(sourceId) }).to.throw(/Error processing argument at index 0 */) }) }) it('should throw an exception if invalid window', async () => { // It is very unlikely that these window id exist. const fakeSourceIds = ['window:99999999:0', 'window:123456:1', 'window:123456:9'] fakeSourceIds.forEach((sourceId) => { expect(() => { w.moveAbove(sourceId) }).to.throw(/Invalid media source id/) }) }) it('should not throw an exception', async () => { const w2 = new BrowserWindow({ show: false, title: 'window2' }) const w2Shown = emittedOnce(w2, 'show') w2.show() await w2Shown expect(() => { w.moveAbove(w2.getMediaSourceId()) }).to.not.throw() await closeWindow(w2, { assertNotWindows: false }) }) }) }) describe('sizing', () => { let w = null as unknown as BrowserWindow beforeEach(() => { w = new BrowserWindow({ show: false, width: 400, height: 400 }) }) afterEach(async () => { await closeWindow(w) w = null as unknown as BrowserWindow }) describe('BrowserWindow.setBounds(bounds[, animate])', () => { it('sets the window bounds with full bounds', () => { const fullBounds = { x: 440, y: 225, width: 500, height: 400 } w.setBounds(fullBounds) expectBoundsEqual(w.getBounds(), fullBounds) }) it('sets the window bounds with partial bounds', () => { const fullBounds = { x: 440, y: 225, width: 500, height: 400 } w.setBounds(fullBounds) const boundsUpdate = { width: 200 } w.setBounds(boundsUpdate as any) const expectedBounds = Object.assign(fullBounds, boundsUpdate) expectBoundsEqual(w.getBounds(), expectedBounds) }) }) describe('BrowserWindow.setSize(width, height)', () => { it('sets the window size', async () => { const size = [300, 400] const resized = emittedOnce(w, 'resize') w.setSize(size[0], size[1]) await resized expectBoundsEqual(w.getSize(), size) }) }) describe('BrowserWindow.setMinimum/MaximumSize(width, height)', () => { it('sets the maximum and minimum size of the window', () => { expect(w.getMinimumSize()).to.deep.equal([0, 0]) expect(w.getMaximumSize()).to.deep.equal([0, 0]) w.setMinimumSize(100, 100) expectBoundsEqual(w.getMinimumSize(), [100, 100]) expectBoundsEqual(w.getMaximumSize(), [0, 0]) w.setMaximumSize(900, 600) expectBoundsEqual(w.getMinimumSize(), [100, 100]) expectBoundsEqual(w.getMaximumSize(), [900, 600]) }) }) describe('BrowserWindow.setAspectRatio(ratio)', () => { it('resets the behaviour when passing in 0', (done) => { const size = [300, 400] w.setAspectRatio(1 / 2) w.setAspectRatio(0) w.once('resize', () => { expectBoundsEqual(w.getSize(), size) done() }) w.setSize(size[0], size[1]) }) }) describe('BrowserWindow.setPosition(x, y)', () => { it('sets the window position', (done) => { const pos = [10, 10] w.once('move', () => { const newPos = w.getPosition() expect(newPos).to.deep.equal(pos) done() }) w.setPosition(pos[0], pos[1]) }) }) describe('BrowserWindow.setContentSize(width, height)', () => { it('sets the content size', (done) => { // NB. The CI server has a very small screen. Attempting to size the window // larger than the screen will limit the window's size to the screen and // cause the test to fail. const size = [456, 567] w.setContentSize(size[0], size[1]) setImmediate(() => { const after = w.getContentSize() expect(after).to.deep.equal(size) done() }) }) it('works for a frameless window', (done) => { w.destroy() w = new BrowserWindow({ show: false, frame: false, width: 400, height: 400 }) const size = [456, 567] w.setContentSize(size[0], size[1]) setImmediate(() => { const after = w.getContentSize() expect(after).to.deep.equal(size) done() }) }) }) describe('BrowserWindow.setContentBounds(bounds)', () => { it('sets the content size and position', (done) => { const bounds = { x: 10, y: 10, width: 250, height: 250 } w.once('resize', () => { setTimeout(() => { expectBoundsEqual(w.getContentBounds(), bounds) done() }) }) w.setContentBounds(bounds) }) it('works for a frameless window', (done) => { w.destroy() w = new BrowserWindow({ show: false, frame: false, width: 300, height: 300 }) const bounds = { x: 10, y: 10, width: 250, height: 250 } w.once('resize', () => { setTimeout(() => { expect(w.getContentBounds()).to.deep.equal(bounds) done() }) }) w.setContentBounds(bounds) }) }) describe(`BrowserWindow.getNormalBounds()`, () => { describe(`Normal state`, () => { it(`checks normal bounds after resize`, (done) => { const size = [300, 400] w.once('resize', () => { expectBoundsEqual(w.getNormalBounds(), w.getBounds()) done() }) w.setSize(size[0], size[1]) }) it(`checks normal bounds after move`, (done) => { const pos = [10, 10] w.once('move', () => { expectBoundsEqual(w.getNormalBounds(), w.getBounds()) done() }) w.setPosition(pos[0], pos[1]) }) }) ifdescribe(process.platform !== 'linux')(`Maximized state`, () => { it(`checks normal bounds when maximized`, (done) => { const bounds = w.getBounds() w.once('maximize', () => { expectBoundsEqual(w.getNormalBounds(), bounds) done() }) w.show() w.maximize() }) it(`checks normal bounds when unmaximized`, (done) => { const bounds = w.getBounds() w.once('maximize', () => { w.unmaximize() }) w.once('unmaximize', () => { expectBoundsEqual(w.getNormalBounds(), bounds) done() }) w.show() w.maximize() }) }) ifdescribe(process.platform !== 'linux')(`Minimized state`, () => { it(`checks normal bounds when minimized`, (done) => { const bounds = w.getBounds() w.once('minimize', () => { expectBoundsEqual(w.getNormalBounds(), bounds) done() }) w.show() w.minimize() }) it(`checks normal bounds when restored`, (done) => { const bounds = w.getBounds() w.once('minimize', () => { w.restore() }) w.once('restore', () => { expectBoundsEqual(w.getNormalBounds(), bounds) done() }) w.show() w.minimize() }) }) ifdescribe(process.platform === 'win32')(`Fullscreen state`, () => { it(`checks normal bounds when fullscreen'ed`, (done) => { const bounds = w.getBounds() w.once('enter-full-screen', () => { expectBoundsEqual(w.getNormalBounds(), bounds) done() }) w.show() w.setFullScreen(true) }) it(`checks normal bounds when unfullscreen'ed`, (done) => { const bounds = w.getBounds() w.once('enter-full-screen', () => { w.setFullScreen(false) }) w.once('leave-full-screen', () => { expectBoundsEqual(w.getNormalBounds(), bounds) done() }) w.show() w.setFullScreen(true) }) }) }) }) ifdescribe(process.platform === 'darwin')('tabbed windows', () => { let w = null as unknown as BrowserWindow beforeEach(() => { w = new BrowserWindow({ show: false }) }) afterEach(async () => { await closeWindow(w) w = null as unknown as BrowserWindow }) describe('BrowserWindow.selectPreviousTab()', () => { it('does not throw', () => { expect(() => { w.selectPreviousTab() }).to.not.throw() }) }) describe('BrowserWindow.selectNextTab()', () => { it('does not throw', () => { expect(() => { w.selectNextTab() }).to.not.throw() }) }) describe('BrowserWindow.mergeAllWindows()', () => { it('does not throw', () => { expect(() => { w.mergeAllWindows() }).to.not.throw() }) }) describe('BrowserWindow.moveTabToNewWindow()', () => { it('does not throw', () => { expect(() => { w.moveTabToNewWindow() }).to.not.throw() }) }) describe('BrowserWindow.toggleTabBar()', () => { it('does not throw', () => { expect(() => { w.toggleTabBar() }).to.not.throw() }) }) describe('BrowserWindow.addTabbedWindow()', () => { it('does not throw', async () => { const tabbedWindow = new BrowserWindow({}) expect(() => { w.addTabbedWindow(tabbedWindow) }).to.not.throw() expect(BrowserWindow.getAllWindows()).to.have.lengthOf(2) // w + tabbedWindow await closeWindow(tabbedWindow, { assertNotWindows: false }) expect(BrowserWindow.getAllWindows()).to.have.lengthOf(1) // w }) it('throws when called on itself', () => { expect(() => { w.addTabbedWindow(w) }).to.throw('AddTabbedWindow cannot be called by a window on itself.') }) }) }) describe('autoHideMenuBar property', () => { afterEach(closeAllWindows) it('exists', () => { const w = new BrowserWindow({ show: false }) expect(w).to.have.property('autoHideMenuBar') // TODO(codebytere): remove when propertyification is complete expect(w.setAutoHideMenuBar).to.be.a('function') expect(w.isMenuBarAutoHide).to.be.a('function') }) }) describe('BrowserWindow.capturePage(rect)', () => { afterEach(closeAllWindows) it('returns a Promise with a Buffer', async () => { const w = new BrowserWindow({ show: false }) const image = await w.capturePage({ x: 0, y: 0, width: 100, height: 100 }) expect(image.isEmpty()).to.equal(true) }) it('preserves transparency', async () => { const w = new BrowserWindow({ show: false, transparent: true }) w.loadURL('about:blank') await emittedOnce(w, 'ready-to-show') w.show() const image = await w.capturePage() const imgBuffer = image.toPNG() // Check the 25th byte in the PNG. // Values can be 0,2,3,4, or 6. We want 6, which is RGB + Alpha expect(imgBuffer[25]).to.equal(6) }) }) describe('BrowserWindow.setProgressBar(progress)', () => { let w = null as unknown as BrowserWindow before(() => { w = new BrowserWindow({ show: false }) }) after(async () => { await closeWindow(w) w = null as unknown as BrowserWindow }) it('sets the progress', () => { expect(() => { if (process.platform === 'darwin') { app.dock.setIcon(path.join(fixtures, 'assets', 'logo.png')) } w.setProgressBar(0.5) if (process.platform === 'darwin') { app.dock.setIcon(null as any) } w.setProgressBar(-1) }).to.not.throw() }) it('sets the progress using "paused" mode', () => { expect(() => { w.setProgressBar(0.5, { mode: 'paused' }) }).to.not.throw() }) it('sets the progress using "error" mode', () => { expect(() => { w.setProgressBar(0.5, { mode: 'error' }) }).to.not.throw() }) it('sets the progress using "normal" mode', () => { expect(() => { w.setProgressBar(0.5, { mode: 'normal' }) }).to.not.throw() }) }) describe('BrowserWindow.setAlwaysOnTop(flag, level)', () => { let w = null as unknown as BrowserWindow beforeEach(() => { w = new BrowserWindow({ show: false }) }) afterEach(async () => { await closeWindow(w) w = null as unknown as BrowserWindow }) it('sets the window as always on top', () => { expect(w.isAlwaysOnTop()).to.be.false('is alwaysOnTop') w.setAlwaysOnTop(true, 'screen-saver') expect(w.isAlwaysOnTop()).to.be.true('is not alwaysOnTop') w.setAlwaysOnTop(false) expect(w.isAlwaysOnTop()).to.be.false('is alwaysOnTop') w.setAlwaysOnTop(true) expect(w.isAlwaysOnTop()).to.be.true('is not alwaysOnTop') }) ifit(process.platform === 'darwin')('resets the windows level on minimize', () => { expect(w.isAlwaysOnTop()).to.be.false('is alwaysOnTop') w.setAlwaysOnTop(true, 'screen-saver') expect(w.isAlwaysOnTop()).to.be.true('is not alwaysOnTop') w.minimize() expect(w.isAlwaysOnTop()).to.be.false('is alwaysOnTop') w.restore() expect(w.isAlwaysOnTop()).to.be.true('is not alwaysOnTop') }) it('causes the right value to be emitted on `always-on-top-changed`', (done) => { w.on('always-on-top-changed', (e, alwaysOnTop) => { expect(alwaysOnTop).to.be.true('is not alwaysOnTop') done() }) expect(w.isAlwaysOnTop()).to.be.false('is alwaysOnTop') w.setAlwaysOnTop(true) }) }) describe('preconnect feature', () => { let w = null as unknown as BrowserWindow let server = null as unknown as http.Server let url = null as unknown as string let connections = 0 beforeEach(async () => { connections = 0 server = http.createServer((req, res) => { if (req.url === '/link') { res.setHeader('Content-type', 'text/html') res.end(`foo`) return } res.end() }) server.on('connection', () => { connections++ }) await new Promise(resolve => server.listen(0, '127.0.0.1', () => resolve())) url = `http://127.0.0.1:${(server.address() as AddressInfo).port}` }) afterEach(async () => { server.close() await closeWindow(w) w = null as unknown as BrowserWindow server = null as unknown as http.Server }) it('calling preconnect() connects to the server', (done) => { w = new BrowserWindow({ show: false }) w.webContents.on('did-start-navigation', (event, url) => { w.webContents.session.preconnect({ url, numSockets: 4 }) }) w.webContents.on('did-finish-load', () => { expect(connections).to.equal(4) done() }) w.loadURL(url) }) it('does not preconnect unless requested', async () => { w = new BrowserWindow({ show: false }) await w.loadURL(url) expect(connections).to.equal(1) }) it('parses ', async () => { w = new BrowserWindow({ show: true }) const p = emittedOnce(w.webContents.session, 'preconnect') w.loadURL(url + '/link') const [, preconnectUrl, allowCredentials] = await p expect(preconnectUrl).to.equal('http://example.com/') expect(allowCredentials).to.be.true('allowCredentials') }) }) describe('BrowserWindow.setAutoHideCursor(autoHide)', () => { let w = null as unknown as BrowserWindow beforeEach(() => { w = new BrowserWindow({ show: false }) }) afterEach(async () => { await closeWindow(w) w = null as unknown as BrowserWindow }) ifit(process.platform === 'darwin')('on macOS', () => { it('allows changing cursor auto-hiding', () => { expect(() => { w.setAutoHideCursor(false) w.setAutoHideCursor(true) }).to.not.throw() }) }) ifit(process.platform !== 'darwin')('on non-macOS platforms', () => { it('is not available', () => { expect(w.setAutoHideCursor).to.be.undefined('setAutoHideCursor function') }) }) }) ifdescribe(process.platform === 'darwin')('BrowserWindow.setWindowButtonVisibility()', () => { afterEach(closeAllWindows) it('does not throw', () => { const w = new BrowserWindow({ show: false }) expect(() => { w.setWindowButtonVisibility(true) w.setWindowButtonVisibility(false) }).to.not.throw() }) it('throws with custom title bar buttons', () => { expect(() => { const w = new BrowserWindow({ show: false, titleBarStyle: 'customButtonsOnHover', frame: false }) w.setWindowButtonVisibility(true) }).to.throw('Not supported for this window') }) }) ifdescribe(process.platform === 'darwin')('BrowserWindow.setVibrancy(type)', () => { afterEach(closeAllWindows) it('allows setting, changing, and removing the vibrancy', () => { const w = new BrowserWindow({ show: false }) expect(() => { w.setVibrancy('light') w.setVibrancy('dark') w.setVibrancy(null) w.setVibrancy('ultra-dark') w.setVibrancy('' as any) }).to.not.throw() }) }) ifdescribe(process.platform === 'win32')('BrowserWindow.setAppDetails(options)', () => { afterEach(closeAllWindows) it('supports setting the app details', () => { const w = new BrowserWindow({ show: false }) const iconPath = path.join(fixtures, 'assets', 'icon.ico') expect(() => { w.setAppDetails({ appId: 'my.app.id' }) w.setAppDetails({ appIconPath: iconPath, appIconIndex: 0 }) w.setAppDetails({ appIconPath: iconPath }) w.setAppDetails({ relaunchCommand: 'my-app.exe arg1 arg2', relaunchDisplayName: 'My app name' }) w.setAppDetails({ relaunchCommand: 'my-app.exe arg1 arg2' }) w.setAppDetails({ relaunchDisplayName: 'My app name' }) w.setAppDetails({ appId: 'my.app.id', appIconPath: iconPath, appIconIndex: 0, relaunchCommand: 'my-app.exe arg1 arg2', relaunchDisplayName: 'My app name' }) w.setAppDetails({}) }).to.not.throw() expect(() => { (w.setAppDetails as any)() }).to.throw('Insufficient number of arguments.') }) }) describe('BrowserWindow.fromId(id)', () => { afterEach(closeAllWindows) it('returns the window with id', () => { const w = new BrowserWindow({ show: false }) expect(BrowserWindow.fromId(w.id).id).to.equal(w.id) }) }) describe('BrowserWindow.fromWebContents(webContents)', () => { afterEach(closeAllWindows) it('returns the window with the webContents', () => { const w = new BrowserWindow({ show: false }) const found = BrowserWindow.fromWebContents(w.webContents) expect(found!.id).to.equal(w.id) }) it('returns null for webContents without a BrowserWindow', () => { const contents = (webContents as any).create({}) try { expect(BrowserWindow.fromWebContents(contents)).to.be.null('BrowserWindow.fromWebContents(contents)') } finally { contents.destroy() } }) }) describe('BrowserWindow.openDevTools()', () => { afterEach(closeAllWindows) it('does not crash for frameless window', () => { const w = new BrowserWindow({ show: false, frame: false }) w.webContents.openDevTools() }) }) describe('BrowserWindow.fromBrowserView(browserView)', () => { afterEach(closeAllWindows) it('returns the window with the browserView', () => { const w = new BrowserWindow({ show: false }) const bv = new BrowserView() w.setBrowserView(bv) expect(BrowserWindow.fromBrowserView(bv)!.id).to.equal(w.id) // if BrowserView isn't explicitly destroyed, it will crash in GC later bv.destroy() }) it('returns undefined if not attached', () => { const bv = new BrowserView() expect(BrowserWindow.fromBrowserView(bv)).to.be.null('BrowserWindow associated with bv') // if BrowserView isn't explicitly destroyed, it will crash in GC later bv.destroy() }) }) describe('BrowserWindow.setOpacity(opacity)', () => { afterEach(closeAllWindows) ifdescribe(process.platform !== 'linux')(('Windows and Mac'), () => { it('make window with initial opacity', () => { const w = new BrowserWindow({ show: false, opacity: 0.5 }) expect(w.getOpacity()).to.equal(0.5) }) it('allows setting the opacity', () => { const w = new BrowserWindow({ show: false }) expect(() => { w.setOpacity(0.0) expect(w.getOpacity()).to.equal(0.0) w.setOpacity(0.5) expect(w.getOpacity()).to.equal(0.5) w.setOpacity(1.0) expect(w.getOpacity()).to.equal(1.0) }).to.not.throw() }) it('clamps opacity to [0.0...1.0]', () => { const w = new BrowserWindow({ show: false, opacity: 0.5 }) w.setOpacity(100) expect(w.getOpacity()).to.equal(1.0) w.setOpacity(-100) expect(w.getOpacity()).to.equal(0.0) }) }) ifdescribe(process.platform === 'linux')(('Linux'), () => { it('sets 1 regardless of parameter', () => { const w = new BrowserWindow({ show: false }) w.setOpacity(0) expect(w.getOpacity()).to.equal(1.0) w.setOpacity(0.5) expect(w.getOpacity()).to.equal(1.0) }) }) }) describe('BrowserWindow.setShape(rects)', () => { afterEach(closeAllWindows) it('allows setting shape', () => { const w = new BrowserWindow({ show: false }) expect(() => { w.setShape([]) w.setShape([{ x: 0, y: 0, width: 100, height: 100 }]) w.setShape([{ x: 0, y: 0, width: 100, height: 100 }, { x: 0, y: 200, width: 1000, height: 100 }]) w.setShape([]) }).to.not.throw() }) }) describe('"useContentSize" option', () => { afterEach(closeAllWindows) it('make window created with content size when used', () => { const w = new BrowserWindow({ show: false, width: 400, height: 400, useContentSize: true }) const contentSize = w.getContentSize() expect(contentSize).to.deep.equal([400, 400]) }) it('make window created with window size when not used', () => { const w = new BrowserWindow({ show: false, width: 400, height: 400 }) const size = w.getSize() expect(size).to.deep.equal([400, 400]) }) it('works for a frameless window', () => { const w = new BrowserWindow({ show: false, frame: false, width: 400, height: 400, useContentSize: true }) const contentSize = w.getContentSize() expect(contentSize).to.deep.equal([400, 400]) const size = w.getSize() expect(size).to.deep.equal([400, 400]) }) }) ifdescribe(process.platform === 'darwin' && parseInt(os.release().split('.')[0]) >= 14)('"titleBarStyle" option', () => { afterEach(closeAllWindows) it('creates browser window with hidden title bar', () => { const w = new BrowserWindow({ show: false, width: 400, height: 400, titleBarStyle: 'hidden' }) const contentSize = w.getContentSize() expect(contentSize).to.deep.equal([400, 400]) }) it('creates browser window with hidden inset title bar', () => { const w = new BrowserWindow({ show: false, width: 400, height: 400, titleBarStyle: 'hiddenInset' }) const contentSize = w.getContentSize() expect(contentSize).to.deep.equal([400, 400]) }) }) ifdescribe(process.platform === 'darwin')('"enableLargerThanScreen" option', () => { afterEach(closeAllWindows) it('can move the window out of screen', () => { const w = new BrowserWindow({ show: true, enableLargerThanScreen: true }) w.setPosition(-10, -10) const after = w.getPosition() expect(after).to.deep.equal([-10, -10]) }) it('without it, cannot move the window out of screen', () => { const w = new BrowserWindow({ show: true, enableLargerThanScreen: false }) w.setPosition(-10, -10) const after = w.getPosition() expect(after[1]).to.be.at.least(0) }) it('can set the window larger than screen', () => { const w = new BrowserWindow({ show: true, enableLargerThanScreen: true }) const size = screen.getPrimaryDisplay().size size.width += 100 size.height += 100 w.setSize(size.width, size.height) expectBoundsEqual(w.getSize(), [size.width, size.height]) }) it('without it, cannot set the window larger than screen', () => { const w = new BrowserWindow({ show: true, enableLargerThanScreen: false }) const size = screen.getPrimaryDisplay().size size.width += 100 size.height += 100 w.setSize(size.width, size.height) expect(w.getSize()[1]).to.at.most(screen.getPrimaryDisplay().size.height) }) }) ifdescribe(process.platform === 'darwin')('"zoomToPageWidth" option', () => { afterEach(closeAllWindows) it('sets the window width to the page width when used', () => { const w = new BrowserWindow({ show: false, width: 500, height: 400, zoomToPageWidth: true }) w.maximize() expect(w.getSize()[0]).to.equal(500) }) }) describe('"tabbingIdentifier" option', () => { afterEach(closeAllWindows) it('can be set on a window', () => { expect(() => { /* eslint-disable no-new */ new BrowserWindow({ tabbingIdentifier: 'group1' }) new BrowserWindow({ tabbingIdentifier: 'group2', frame: false }) /* eslint-enable no-new */ }).not.to.throw() }) }) describe('"webPreferences" option', () => { afterEach(() => { ipcMain.removeAllListeners('answer') }) afterEach(closeAllWindows) describe('"preload" option', () => { const doesNotLeakSpec = (name: string, webPrefs: {nodeIntegration: boolean, sandbox: boolean, contextIsolation: boolean}) => { it(name, async () => { const w = new BrowserWindow({ webPreferences: { ...webPrefs, preload: path.resolve(fixtures, 'module', 'empty.js') }, show: false }) w.loadFile(path.join(fixtures, 'api', 'no-leak.html')) const [, result] = await emittedOnce(ipcMain, 'leak-result') expect(result).to.have.property('require', 'undefined') expect(result).to.have.property('exports', 'undefined') expect(result).to.have.property('windowExports', 'undefined') expect(result).to.have.property('windowPreload', 'undefined') expect(result).to.have.property('windowRequire', 'undefined') }) } doesNotLeakSpec('does not leak require', { nodeIntegration: false, sandbox: false, contextIsolation: false }) doesNotLeakSpec('does not leak require when sandbox is enabled', { nodeIntegration: false, sandbox: true, contextIsolation: false }) doesNotLeakSpec('does not leak require when context isolation is enabled', { nodeIntegration: false, sandbox: false, contextIsolation: true }) doesNotLeakSpec('does not leak require when context isolation and sandbox are enabled', { nodeIntegration: false, sandbox: true, contextIsolation: true }) it('loads the script before other scripts in window', async () => { const preload = path.join(fixtures, 'module', 'set-global.js') const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, preload } }) w.loadFile(path.join(fixtures, 'api', 'preload.html')) const [, test] = await emittedOnce(ipcMain, 'answer') expect(test).to.eql('preload') }) it('can successfully delete the Buffer global', async () => { const preload = path.join(fixtures, 'module', 'delete-buffer.js') const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, preload } }) w.loadFile(path.join(fixtures, 'api', 'preload.html')) const [, test] = await emittedOnce(ipcMain, 'answer') expect(test).to.eql(Buffer.from('buffer')) }) it('has synchronous access to all eventual window APIs', async () => { const preload = path.join(fixtures, 'module', 'access-blink-apis.js') const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, preload } }) w.loadFile(path.join(fixtures, 'api', 'preload.html')) const [, test] = await emittedOnce(ipcMain, 'answer') expect(test).to.be.an('object') expect(test.atPreload).to.be.an('array') expect(test.atLoad).to.be.an('array') expect(test.atPreload).to.deep.equal(test.atLoad, 'should have access to the same window APIs') }) }) describe('session preload scripts', function () { const preloads = [ path.join(fixtures, 'module', 'set-global-preload-1.js'), path.join(fixtures, 'module', 'set-global-preload-2.js'), path.relative(process.cwd(), path.join(fixtures, 'module', 'set-global-preload-3.js')) ] const defaultSession = session.defaultSession beforeEach(() => { expect(defaultSession.getPreloads()).to.deep.equal([]) defaultSession.setPreloads(preloads) }) afterEach(() => { defaultSession.setPreloads([]) }) it('can set multiple session preload script', () => { expect(defaultSession.getPreloads()).to.deep.equal(preloads) }) const generateSpecs = (description: string, sandbox: boolean) => { describe(description, () => { it('loads the script before other scripts in window including normal preloads', async () => { const w = new BrowserWindow({ show: false, webPreferences: { sandbox, preload: path.join(fixtures, 'module', 'get-global-preload.js') } }) w.loadURL('about:blank') const [, preload1, preload2, preload3] = await emittedOnce(ipcMain, 'vars') expect(preload1).to.equal('preload-1') expect(preload2).to.equal('preload-1-2') expect(preload3).to.be.undefined('preload 3') }) }) } generateSpecs('without sandbox', false) generateSpecs('with sandbox', true) }) describe('"additionalArguments" option', () => { it('adds extra args to process.argv in the renderer process', async () => { const preload = path.join(fixtures, 'module', 'check-arguments.js') const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, preload, additionalArguments: ['--my-magic-arg'] } }) w.loadFile(path.join(fixtures, 'api', 'blank.html')) const [, argv] = await emittedOnce(ipcMain, 'answer') expect(argv).to.include('--my-magic-arg') }) it('adds extra value args to process.argv in the renderer process', async () => { const preload = path.join(fixtures, 'module', 'check-arguments.js') const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, preload, additionalArguments: ['--my-magic-arg=foo'] } }) w.loadFile(path.join(fixtures, 'api', 'blank.html')) const [, argv] = await emittedOnce(ipcMain, 'answer') expect(argv).to.include('--my-magic-arg=foo') }) }) describe('"node-integration" option', () => { it('disables node integration by default', async () => { const preload = path.join(fixtures, 'module', 'send-later.js') const w = new BrowserWindow({ show: false, webPreferences: { preload } }) w.loadFile(path.join(fixtures, 'api', 'blank.html')) const [, typeofProcess, typeofBuffer] = await emittedOnce(ipcMain, 'answer') expect(typeofProcess).to.equal('undefined') expect(typeofBuffer).to.equal('undefined') }) }) describe('"enableRemoteModule" option', () => { const generateSpecs = (description: string, sandbox: boolean) => { describe(description, () => { const preload = path.join(fixtures, 'module', 'preload-remote.js') it('enables the remote module by default', async () => { const w = new BrowserWindow({ show: false, webPreferences: { preload, sandbox } }) const p = emittedOnce(ipcMain, 'remote') w.loadFile(path.join(fixtures, 'api', 'blank.html')) const [, remote] = await p expect(remote).to.equal('object') }) it('disables the remote module when false', async () => { const w = new BrowserWindow({ show: false, webPreferences: { preload, sandbox, enableRemoteModule: false } }) const p = emittedOnce(ipcMain, 'remote') w.loadFile(path.join(fixtures, 'api', 'blank.html')) const [, remote] = await p expect(remote).to.equal('undefined') }) }) } generateSpecs('without sandbox', false) generateSpecs('with sandbox', true) }) describe('"sandbox" option', () => { function waitForEvents (emitter: {once: Function}, events: string[], callback: () => void) { let count = events.length for (const event of events) { emitter.once(event, () => { if (!--count) callback() }) } } const preload = path.join(fixtures, 'module', 'preload-sandbox.js') let server: http.Server = null as unknown as http.Server let serverUrl: string = null as unknown as string before((done) => { server = http.createServer((request, response) => { switch (request.url) { case '/cross-site': response.end(`

${request.url}

`) break default: throw new Error(`unsupported endpoint: ${request.url}`) } }).listen(0, '127.0.0.1', () => { serverUrl = 'http://127.0.0.1:' + (server.address() as AddressInfo).port done() }) }) after(() => { server.close() }) it('exposes ipcRenderer to preload script', async () => { const w = new BrowserWindow({ show: false, webPreferences: { sandbox: true, preload } }) w.loadFile(path.join(fixtures, 'api', 'preload.html')) const [, test] = await emittedOnce(ipcMain, 'answer') expect(test).to.equal('preload') }) it('exposes ipcRenderer to preload script (path has special chars)', async () => { const preloadSpecialChars = path.join(fixtures, 'module', 'preload-sandboxæø åü.js') const w = new BrowserWindow({ show: false, webPreferences: { sandbox: true, preload: preloadSpecialChars } }) w.loadFile(path.join(fixtures, 'api', 'preload.html')) const [, test] = await emittedOnce(ipcMain, 'answer') expect(test).to.equal('preload') }) it('exposes "loaded" event to preload script', async () => { const w = new BrowserWindow({ show: false, webPreferences: { sandbox: true, preload } }) w.loadURL('about:blank') await emittedOnce(ipcMain, 'process-loaded') }) it('exposes "exit" event to preload script', async () => { const w = new BrowserWindow({ show: false, webPreferences: { sandbox: true, preload } }) const htmlPath = path.join(fixtures, 'api', 'sandbox.html?exit-event') const pageUrl = 'file://' + htmlPath w.loadURL(pageUrl) const [, url] = await emittedOnce(ipcMain, 'answer') const expectedUrl = process.platform === 'win32' ? 'file:///' + htmlPath.replace(/\\/g, '/') : pageUrl expect(url).to.equal(expectedUrl) }) it('should open windows in same domain with cross-scripting enabled', async () => { const w = new BrowserWindow({ show: false, webPreferences: { sandbox: true, preload } }) 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 pageUrl = 'file://' + htmlPath const answer = emittedOnce(ipcMain, 'answer') w.loadURL(pageUrl) const [, url, frameName, , options] = await emittedOnce(w.webContents, 'new-window') const expectedUrl = process.platform === 'win32' ? 'file:///' + htmlPath.replace(/\\/g, '/') : pageUrl expect(url).to.equal(expectedUrl) expect(frameName).to.equal('popup!') expect(options.width).to.equal(500) expect(options.height).to.equal(600) const [, html] = await answer expect(html).to.equal('

scripting from opener

') }) it('should open windows in another domain with cross-scripting disabled', async () => { const w = new BrowserWindow({ show: false, webPreferences: { sandbox: true, preload } }) w.webContents.once('new-window', (event, url, frameName, disposition, options) => { options.webPreferences!.preload = preload }) w.loadFile( path.join(fixtures, 'api', 'sandbox.html'), { search: 'window-open-external' } ) // Wait for a message from the main window saying that it's ready. await emittedOnce(ipcMain, 'opener-loaded') // Ask the opener to open a popup with window.opener. const expectedPopupUrl = `${serverUrl}/cross-site` // Set in "sandbox.html". w.webContents.send('open-the-popup', expectedPopupUrl) // The page is going to open a popup that it won't be able to close. // We have to close it from here later. const [, popupWindow] = await emittedOnce(app, 'browser-window-created') // Ask the popup window for details. const detailsAnswer = emittedOnce(ipcMain, 'child-loaded') popupWindow.webContents.send('provide-details') const [, openerIsNull, , locationHref] = await detailsAnswer expect(openerIsNull).to.be.false('window.opener is null') expect(locationHref).to.equal(expectedPopupUrl) // Ask the page to access the popup. const touchPopupResult = emittedOnce(ipcMain, 'answer') w.webContents.send('touch-the-popup') const [, popupAccessMessage] = await touchPopupResult // Ask the popup to access the opener. const touchOpenerResult = emittedOnce(ipcMain, 'answer') popupWindow.webContents.send('touch-the-opener') const [, openerAccessMessage] = await touchOpenerResult // We don't need the popup anymore, and its parent page can't close it, // so let's close it from here before we run any checks. await closeWindow(popupWindow, { assertNotWindows: false }) expect(popupAccessMessage).to.be.a('string', `child's .document is accessible from its parent window`) expect(popupAccessMessage).to.match(/^Blocked a frame with origin/) expect(openerAccessMessage).to.be.a('string', `opener .document is accessible from a popup window`) expect(openerAccessMessage).to.match(/^Blocked a frame with origin/) }) it('should inherit the sandbox setting in opened windows', async () => { const w = new BrowserWindow({ show: false, webPreferences: { sandbox: true } }) const preloadPath = path.join(fixtures, 'api', 'new-window-preload.js') w.webContents.once('new-window', (event, url, frameName, disposition, options) => { options.webPreferences!.preload = preloadPath }) w.loadFile(path.join(fixtures, 'api', 'new-window.html')) const [, args] = await emittedOnce(ipcMain, 'answer') expect(args).to.include('--enable-sandbox') }) it('should open windows with the options configured via new-window event listeners', async () => { const w = new BrowserWindow({ show: false, webPreferences: { sandbox: true } }) const preloadPath = path.join(fixtures, 'api', 'new-window-preload.js') w.webContents.once('new-window', (event, url, frameName, disposition, options) => { options.webPreferences!.preload = preloadPath const prefs = options.webPreferences as any prefs.foo = 'bar' }) w.loadFile(path.join(fixtures, 'api', 'new-window.html')) const [, , webPreferences] = await emittedOnce(ipcMain, 'answer') expect(webPreferences.foo).to.equal('bar') }) it('should set ipc event sender correctly', (done) => { const w = new BrowserWindow({ show: false, webPreferences: { sandbox: true, preload } }) let childWc: WebContents | null = null w.webContents.on('new-window', (event, url, frameName, disposition, options) => { options.webPreferences!.preload = preload childWc = (options as any).webContents expect(w.webContents).to.not.equal(childWc) }) ipcMain.once('parent-ready', function (event) { expect(event.sender).to.equal(w.webContents, 'sender should be the parent') event.sender.send('verified') }) ipcMain.once('child-ready', function (event) { expect(childWc).to.not.be.null('child webcontents should be available') expect(event.sender).to.equal(childWc, 'sender should be the child') event.sender.send('verified') }) waitForEvents(ipcMain, [ 'parent-answer', 'child-answer' ], done) w.loadFile(path.join(fixtures, 'api', 'sandbox.html'), { search: 'verify-ipc-sender' }) }) describe('event handling', () => { let w: BrowserWindow = null as unknown as BrowserWindow beforeEach(() => { w = new BrowserWindow({ show: false, webPreferences: { sandbox: true } }) }) it('works for window events', (done) => { waitForEvents(w, [ 'page-title-updated' ], done) w.loadURL(`data:text/html,`) }) it('works for stop events', (done) => { waitForEvents(w.webContents, [ 'did-navigate', 'did-fail-load', 'did-stop-loading' ], done) w.loadURL(`data:text/html,`) }) it('works for web contents events', (done) => { waitForEvents(w.webContents, [ 'did-finish-load', 'did-frame-finish-load', 'did-navigate-in-page', // TODO(nornagon): sandboxed pages should also emit will-navigate // 'will-navigate', 'did-start-loading', 'did-stop-loading', 'did-frame-finish-load', 'dom-ready' ], done) w.loadFile(path.join(fixtures, 'api', 'sandbox.html'), { search: 'webcontents-events' }) }) }) it('supports calling preventDefault on new-window events', (done) => { const w = new BrowserWindow({ show: false, webPreferences: { sandbox: true } }) const initialWebContents = webContents.getAllWebContents().map((i) => i.id) w.webContents.once('new-window', (e) => { e.preventDefault() // We need to give it some time so the windows get properly disposed (at least on OSX). setTimeout(() => { const currentWebContents = webContents.getAllWebContents().map((i) => i.id) expect(currentWebContents).to.deep.equal(initialWebContents) done() }, 100) }) w.loadFile(path.join(fixtures, 'pages', 'window-open.html')) }) // see #9387 it('properly manages remote object references after page reload', (done) => { const w = new BrowserWindow({ show: false, webPreferences: { preload, sandbox: true } }) w.loadFile(path.join(fixtures, 'api', 'sandbox.html'), { search: 'reload-remote' }) ipcMain.on('get-remote-module-path', (event) => { event.returnValue = path.join(fixtures, 'module', 'hello.js') }) let reload = false ipcMain.on('reloaded', (event) => { event.returnValue = reload reload = !reload }) ipcMain.once('reload', (event) => { event.sender.reload() }) ipcMain.once('answer', (event, arg) => { ipcMain.removeAllListeners('reloaded') ipcMain.removeAllListeners('get-remote-module-path') expect(arg).to.equal('hi') done() }) }) it('properly manages remote object references after page reload in child window', (done) => { const w = new BrowserWindow({ show: false, webPreferences: { preload, sandbox: true } }) w.webContents.once('new-window', (event, url, frameName, disposition, options) => { options.webPreferences!.preload = preload }) w.loadFile(path.join(fixtures, 'api', 'sandbox.html'), { search: 'reload-remote-child' }) ipcMain.on('get-remote-module-path', (event) => { event.returnValue = path.join(fixtures, 'module', 'hello-child.js') }) let reload = false ipcMain.on('reloaded', (event) => { event.returnValue = reload reload = !reload }) ipcMain.once('reload', (event) => { event.sender.reload() }) ipcMain.once('answer', (event, arg) => { ipcMain.removeAllListeners('reloaded') ipcMain.removeAllListeners('get-remote-module-path') expect(arg).to.equal('hi child window') done() }) }) it('validates process APIs access in sandboxed renderer', async () => { const w = new BrowserWindow({ show: false, webPreferences: { sandbox: true, preload } }) w.webContents.once('preload-error', (event, preloadPath, error) => { throw error }) process.env.sandboxmain = 'foo' w.loadFile(path.join(fixtures, 'api', 'preload.html')) const [, test] = await emittedOnce(ipcMain, 'answer') expect(test.hasCrash).to.be.true('has crash') expect(test.hasHang).to.be.true('has hang') expect(test.heapStatistics).to.be.an('object') expect(test.blinkMemoryInfo).to.be.an('object') expect(test.processMemoryInfo).to.be.an('object') expect(test.systemVersion).to.be.a('string') expect(test.cpuUsage).to.be.an('object') expect(test.ioCounters).to.be.an('object') expect(test.arch).to.equal(process.arch) expect(test.platform).to.equal(process.platform) expect(test.env).to.deep.equal(process.env) expect(test.execPath).to.equal(process.helperExecPath) expect(test.sandboxed).to.be.true('sandboxed') expect(test.type).to.equal('renderer') expect(test.version).to.equal(process.version) expect(test.versions).to.deep.equal(process.versions) if (process.platform === 'linux' && test.osSandbox) { expect(test.creationTime).to.be.null('creation time') expect(test.systemMemoryInfo).to.be.null('system memory info') } else { expect(test.creationTime).to.be.a('number') expect(test.systemMemoryInfo).to.be.an('object') } }) it('webview in sandbox renderer', async () => { const w = new BrowserWindow({ show: false, webPreferences: { sandbox: true, preload, webviewTag: true } }) const didAttachWebview = emittedOnce(w.webContents, 'did-attach-webview') const webviewDomReady = emittedOnce(ipcMain, 'webview-dom-ready') w.loadFile(path.join(fixtures, 'pages', 'webview-did-attach-event.html')) const [, webContents] = await didAttachWebview const [, id] = await webviewDomReady expect(webContents.id).to.equal(id) }) }) describe('nativeWindowOpen option', () => { let w: BrowserWindow = null as unknown as BrowserWindow beforeEach(() => { w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, nativeWindowOpen: true, // tests relies on preloads in opened windows nodeIntegrationInSubFrames: true } }) }) it('opens window of about:blank with cross-scripting enabled', (done) => { ipcMain.once('answer', (event, content) => { expect(content).to.equal('Hello') done() }) w.loadFile(path.join(fixtures, 'api', 'native-window-open-blank.html')) }) it('opens window of same domain with cross-scripting enabled', (done) => { ipcMain.once('answer', (event, content) => { expect(content).to.equal('Hello') done() }) w.loadFile(path.join(fixtures, 'api', 'native-window-open-file.html')) }) it('blocks accessing cross-origin frames', (done) => { ipcMain.once('answer', (event, content) => { expect(content).to.equal('Blocked a frame with origin "file://" from accessing a cross-origin frame.') done() }) w.loadFile(path.join(fixtures, 'api', 'native-window-open-cross-origin.html')) }) it('opens window from