test: fix a bunch of flaky tests related to emittedOnce (#15871)

This commit is contained in:
Jeremy Apthorp 2018-11-28 17:52:19 -08:00 committed by GitHub
parent ae266e2e03
commit 30109d64f2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 33 deletions

View file

@ -360,7 +360,6 @@ describe('BrowserWindow module', () => {
it('is emitted after will-navigate on redirects', (done) => { it('is emitted after will-navigate on redirects', (done) => {
let navigateCalled = false let navigateCalled = false
w.loadURL(`${server.url}/navigate-302`)
w.webContents.on('will-navigate', () => { w.webContents.on('will-navigate', () => {
navigateCalled = true navigateCalled = true
}) })
@ -368,6 +367,7 @@ describe('BrowserWindow module', () => {
expect(navigateCalled).to.equal(true, 'should have called will-navigate first') expect(navigateCalled).to.equal(true, 'should have called will-navigate first')
done() done()
}) })
w.loadURL(`${server.url}/navigate-302`)
}) })
it('is emitted before did-stop-loading on redirects', (done) => { it('is emitted before did-stop-loading on redirects', (done) => {
@ -509,8 +509,9 @@ describe('BrowserWindow module', () => {
height: 400, height: 400,
transparent: true transparent: true
}) })
const p = emittedOnce(w, 'ready-to-show')
w.loadURL('data:text/html,<html><body background-color: rgba(255,255,255,0)></body></html>') w.loadURL('data:text/html,<html><body background-color: rgba(255,255,255,0)></body></html>')
await emittedOnce(w, 'ready-to-show') await p
w.show() w.show()
const image = await w.capturePage() const image = await w.capturePage()
@ -1312,8 +1313,9 @@ describe('BrowserWindow module', () => {
preload: preload preload: preload
} }
}) })
const p = emittedOnce(ipcMain, 'answer')
w.loadFile(path.join(fixtures, 'api', 'preload.html')) w.loadFile(path.join(fixtures, 'api', 'preload.html'))
const [, test] = await emittedOnce(ipcMain, 'answer') const [, test] = await p
expect(test).to.be.an('object') expect(test).to.be.an('object')
expect(test.atPreload).to.be.an('array') expect(test.atPreload).to.be.an('array')
expect(test.atLoad).to.be.an('array') expect(test.atLoad).to.be.an('array')
@ -1428,8 +1430,9 @@ describe('BrowserWindow module', () => {
sandbox sandbox
} }
}) })
const p = emittedOnce(ipcMain, 'remote')
w.loadFile(path.join(fixtures, 'api', 'blank.html')) w.loadFile(path.join(fixtures, 'api', 'blank.html'))
const [, remote] = await emittedOnce(ipcMain, 'remote') const [, remote] = await p
expect(remote).to.equal('object') expect(remote).to.equal('object')
}) })
@ -1443,8 +1446,9 @@ describe('BrowserWindow module', () => {
enableRemoteModule: false enableRemoteModule: false
} }
}) })
const p = emittedOnce(ipcMain, 'remote')
w.loadFile(path.join(fixtures, 'api', 'blank.html')) w.loadFile(path.join(fixtures, 'api', 'blank.html'))
const [, remote] = await emittedOnce(ipcMain, 'remote') const [, remote] = await p
expect(remote).to.equal('undefined') expect(remote).to.equal('undefined')
}) })
}) })
@ -1533,7 +1537,6 @@ describe('BrowserWindow module', () => {
}) })
const htmlPath = path.join(fixtures, 'api', 'sandbox.html?exit-event') const htmlPath = path.join(fixtures, 'api', 'sandbox.html?exit-event')
const pageUrl = 'file://' + htmlPath const pageUrl = 'file://' + htmlPath
w.loadURL(pageUrl)
ipcMain.once('answer', function (event, url) { ipcMain.once('answer', function (event, url) {
let expectedUrl = pageUrl let expectedUrl = pageUrl
if (process.platform === 'win32') { if (process.platform === 'win32') {
@ -1542,6 +1545,7 @@ describe('BrowserWindow module', () => {
assert.strictEqual(url, expectedUrl) assert.strictEqual(url, expectedUrl)
done() done()
}) })
w.loadURL(pageUrl)
}) })
it('should open windows in same domain with cross-scripting enabled', (done) => { it('should open windows in same domain with cross-scripting enabled', (done) => {
@ -1556,7 +1560,6 @@ describe('BrowserWindow module', () => {
ipcRenderer.send('set-web-preferences-on-next-new-window', w.webContents.id, 'preload', preload) ipcRenderer.send('set-web-preferences-on-next-new-window', w.webContents.id, 'preload', preload)
const htmlPath = path.join(fixtures, 'api', 'sandbox.html?window-open') const htmlPath = path.join(fixtures, 'api', 'sandbox.html?window-open')
const pageUrl = 'file://' + htmlPath const pageUrl = 'file://' + htmlPath
w.loadURL(pageUrl)
w.webContents.once('new-window', (e, url, frameName, disposition, options) => { w.webContents.once('new-window', (e, url, frameName, disposition, options) => {
let expectedUrl = pageUrl let expectedUrl = pageUrl
if (process.platform === 'win32') { if (process.platform === 'win32') {
@ -1571,6 +1574,7 @@ describe('BrowserWindow module', () => {
done() done()
}) })
}) })
w.loadURL(pageUrl)
}) })
it('should open windows in another domain with cross-scripting disabled', async () => { it('should open windows in another domain with cross-scripting disabled', async () => {
@ -1583,24 +1587,27 @@ describe('BrowserWindow module', () => {
}) })
ipcRenderer.send('set-web-preferences-on-next-new-window', w.webContents.id, 'preload', preload) ipcRenderer.send('set-web-preferences-on-next-new-window', w.webContents.id, 'preload', preload)
const browserWindowCreated = emittedOnce(app, 'browser-window-created')
const childLoaded = emittedOnce(ipcMain, 'child-loaded')
w.loadFile(path.join(fixtures, 'api', 'sandbox.html'), { search: 'window-open-external' }) w.loadFile(path.join(fixtures, 'api', 'sandbox.html'), { search: 'window-open-external' })
const expectedPopupUrl = 'http://www.google.com/#q=electron' // Set in the "sandbox.html". const expectedPopupUrl = 'http://www.google.com/#q=electron' // Set in the "sandbox.html".
// The page is going to open a popup that it won't be able to close. // The page is going to open a popup that it won't be able to close.
// We have to close it from here later. // We have to close it from here later.
// XXX(alexeykuzmin): It will leak if the test fails too soon. // XXX(alexeykuzmin): It will leak if the test fails too soon.
const [, popupWindow] = await emittedOnce(app, 'browser-window-created') const [, popupWindow] = await browserWindowCreated
// Wait for a message from the popup's preload script. // Wait for a message from the popup's preload script.
const [, openerIsNull, html, locationHref] = await emittedOnce(ipcMain, 'child-loaded') const [, openerIsNull, html, locationHref] = await childLoaded
expect(openerIsNull).to.be.true('window.opener is not null') expect(openerIsNull).to.be.true('window.opener is not null')
expect(html).to.equal(`<h1>${expectedPopupUrl}</h1>`, expect(html).to.equal(`<h1>${expectedPopupUrl}</h1>`,
'looks like a http: request has not been intercepted locally') 'looks like a http: request has not been intercepted locally')
expect(locationHref).to.equal(expectedPopupUrl) expect(locationHref).to.equal(expectedPopupUrl)
// Ask the page to access the popup. // Ask the page to access the popup.
const answer = emittedOnce(ipcMain, 'answer')
w.webContents.send('touch-the-popup') w.webContents.send('touch-the-popup')
const [, exceptionMessage] = await emittedOnce(ipcMain, 'answer') const [, exceptionMessage] = await answer
// We don't need the popup anymore, and its parent page can't close it, // 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. // so let's close it from here before we run any checks.
@ -1860,10 +1867,12 @@ describe('BrowserWindow module', () => {
webviewTag: true 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')) w.loadFile(path.join(fixtures, 'pages', 'webview-did-attach-event.html'))
const [, webContents] = await emittedOnce(w.webContents, 'did-attach-webview') const [, webContents] = await didAttachWebview
const [, id] = await emittedOnce(ipcMain, 'webview-dom-ready') const [, id] = await webviewDomReady
expect(webContents.id).to.equal(id) expect(webContents.id).to.equal(id)
}) })
}) })
@ -3197,13 +3206,13 @@ describe('BrowserWindow module', () => {
showLastDevToolsPanel() showLastDevToolsPanel()
w.loadURL('about:blank')
w.webContents.openDevTools({ mode: 'bottom' })
ipcMain.once('answer', function (event, message) { ipcMain.once('answer', function (event, message) {
assert.strictEqual(message.runtimeId, 'foo') assert.strictEqual(message.runtimeId, 'foo')
done() done()
}) })
w.loadURL('about:blank')
w.webContents.openDevTools({ mode: 'bottom' })
}) })
it('serializes the registered extensions on quit', () => { it('serializes the registered extensions on quit', () => {
@ -3417,32 +3426,39 @@ describe('BrowserWindow module', () => {
}) })
it('separates the page context from the Electron/preload context', async () => { it('separates the page context from the Electron/preload context', async () => {
const p = emittedOnce(ipcMain, 'isolated-world')
iw.loadFile(path.join(fixtures, 'api', 'isolated.html')) iw.loadFile(path.join(fixtures, 'api', 'isolated.html'))
const [, data] = await emittedOnce(ipcMain, 'isolated-world') const [, data] = await p
assert.deepStrictEqual(data, expectedContextData) assert.deepStrictEqual(data, expectedContextData)
}) })
it('recreates the contexts on reload', async () => { it('recreates the contexts on reload', async () => {
const loaded = emittedOnce(iw.webContents, 'did-finish-load')
iw.loadFile(path.join(fixtures, 'api', 'isolated.html')) iw.loadFile(path.join(fixtures, 'api', 'isolated.html'))
await emittedOnce(iw.webContents, 'did-finish-load') await loaded
const isolatedWorld = emittedOnce(ipcMain, 'isolated-world')
iw.webContents.reload() iw.webContents.reload()
const [, data] = await emittedOnce(ipcMain, 'isolated-world') const [, data] = await isolatedWorld
assert.deepStrictEqual(data, expectedContextData) assert.deepStrictEqual(data, expectedContextData)
}) })
it('enables context isolation on child windows', async () => { it('enables context isolation on child windows', async () => {
const browserWindowCreated = emittedOnce(app, 'browser-window-created')
iw.loadFile(path.join(fixtures, 'pages', 'window-open.html')) iw.loadFile(path.join(fixtures, 'pages', 'window-open.html'))
const [, window] = await emittedOnce(app, 'browser-window-created') const [, window] = await browserWindowCreated
assert.ok(window.webContents.getLastWebPreferences().contextIsolation) assert.ok(window.webContents.getLastWebPreferences().contextIsolation)
}) })
it('separates the page context from the Electron/preload context with sandbox on', async () => { it('separates the page context from the Electron/preload context with sandbox on', async () => {
const p = emittedOnce(ipcMain, 'isolated-world')
ws.loadFile(path.join(fixtures, 'api', 'isolated.html')) ws.loadFile(path.join(fixtures, 'api', 'isolated.html'))
const [, data] = await emittedOnce(ipcMain, 'isolated-world') const [, data] = await p
assert.deepStrictEqual(data, expectedContextData) assert.deepStrictEqual(data, expectedContextData)
}) })
it('recreates the contexts on reload with sandbox on', async () => { it('recreates the contexts on reload with sandbox on', async () => {
const loaded = emittedOnce(ws.webContents, 'did-finish-load')
ws.loadFile(path.join(fixtures, 'api', 'isolated.html')) ws.loadFile(path.join(fixtures, 'api', 'isolated.html'))
await emittedOnce(ws.webContents, 'did-finish-load') await loaded
const isolatedWorld = emittedOnce(ipcMain, 'isolated-world')
ws.webContents.reload() ws.webContents.reload()
const [, data] = await emittedOnce(ipcMain, 'isolated-world') const [, data] = await isolatedWorld
assert.deepStrictEqual(data, expectedContextData) assert.deepStrictEqual(data, expectedContextData)
}) })
it('supports fetch api', async () => { it('supports fetch api', async () => {
@ -3453,12 +3469,14 @@ describe('BrowserWindow module', () => {
preload: path.join(fixtures, 'api', 'isolated-fetch-preload.js') preload: path.join(fixtures, 'api', 'isolated-fetch-preload.js')
} }
}) })
const p = emittedOnce(ipcMain, 'isolated-fetch-error')
fetchWindow.loadURL('about:blank') fetchWindow.loadURL('about:blank')
const [, error] = await emittedOnce(ipcMain, 'isolated-fetch-error') const [, error] = await p
fetchWindow.destroy() fetchWindow.destroy()
assert.strictEqual(error, 'Failed to fetch') assert.strictEqual(error, 'Failed to fetch')
}) })
it('doesn\'t break ipc serialization', async () => { it('doesn\'t break ipc serialization', async () => {
const p = emittedOnce(ipcMain, 'isolated-world')
iw.loadURL('about:blank') iw.loadURL('about:blank')
iw.webContents.executeJavaScript(` iw.webContents.executeJavaScript(`
const opened = window.open() const opened = window.open()
@ -3466,7 +3484,7 @@ describe('BrowserWindow module', () => {
opened.close() opened.close()
window.postMessage({openedLocation}, '*') window.postMessage({openedLocation}, '*')
`) `)
const [, data] = await emittedOnce(ipcMain, 'isolated-world') const [, data] = await p
assert.strictEqual(data.pageContext.openedLocation, '') assert.strictEqual(data.pageContext.openedLocation, '')
}) })
}) })

View file

@ -137,12 +137,14 @@ describe('webContents module', () => {
const oscillator = context.createOscillator() const oscillator = context.createOscillator()
oscillator.connect(context.destination) oscillator.connect(context.destination)
oscillator.start() oscillator.start()
let p = emittedOnce(webContents, '-audio-state-changed')
await context.resume() await context.resume()
const [, audible] = await emittedOnce(webContents, '-audio-state-changed') const [, audible] = await p
assert(webContents.isCurrentlyAudible() === audible) assert(webContents.isCurrentlyAudible() === audible)
expect(webContents.isCurrentlyAudible()).to.be.true() expect(webContents.isCurrentlyAudible()).to.be.true()
p = emittedOnce(webContents, '-audio-state-changed')
oscillator.stop() oscillator.stop()
await emittedOnce(webContents, '-audio-state-changed') await p
expect(webContents.isCurrentlyAudible()).to.be.false() expect(webContents.isCurrentlyAudible()).to.be.false()
oscillator.disconnect() oscillator.disconnect()
context.close() context.close()
@ -163,14 +165,16 @@ describe('webContents module', () => {
it('can show window with activation', async () => { it('can show window with activation', async () => {
w.show() w.show()
assert.strictEqual(w.isFocused(), true) assert.strictEqual(w.isFocused(), true)
const devtoolsOpened = emittedOnce(w.webContents, 'devtools-opened')
w.webContents.openDevTools({ mode: 'detach', activate: true }) w.webContents.openDevTools({ mode: 'detach', activate: true })
await emittedOnce(w.webContents, 'devtools-opened') await devtoolsOpened
assert.strictEqual(w.isFocused(), false) assert.strictEqual(w.isFocused(), false)
}) })
it('can show window without activation', async () => { it('can show window without activation', async () => {
const devtoolsOpened = emittedOnce(w.webContents, 'devtools-opened')
w.webContents.openDevTools({ mode: 'detach', activate: false }) w.webContents.openDevTools({ mode: 'detach', activate: false })
await emittedOnce(w.webContents, 'devtools-opened') await devtoolsOpened
assert.strictEqual(w.isDevToolsOpened(), true) assert.strictEqual(w.isDevToolsOpened(), true)
}) })
}) })
@ -496,7 +500,6 @@ describe('webContents module', () => {
}) })
it('can set the correct zoom level', (done) => { it('can set the correct zoom level', (done) => {
w.loadURL('about:blank')
w.webContents.on('did-finish-load', () => { w.webContents.on('did-finish-load', () => {
w.webContents.getZoomLevel((zoomLevel) => { w.webContents.getZoomLevel((zoomLevel) => {
assert.strictEqual(zoomLevel, 0.0) assert.strictEqual(zoomLevel, 0.0)
@ -508,6 +511,7 @@ describe('webContents module', () => {
}) })
}) })
}) })
w.loadURL('about:blank')
}) })
it('can persist zoom level across navigation', (done) => { it('can persist zoom level across navigation', (done) => {
@ -895,8 +899,9 @@ describe('webContents module', () => {
} }
}) })
const p = emittedOnce(w.webContents, 'did-finish-load')
w.loadURL('about:blank') w.loadURL('about:blank')
await emittedOnce(w.webContents, 'did-finish-load') await p
const filePath = path.join(remote.app.getPath('temp'), 'test.heapsnapshot') const filePath = path.join(remote.app.getPath('temp'), 'test.heapsnapshot')
@ -926,8 +931,9 @@ describe('webContents module', () => {
} }
}) })
const p = emittedOnce(w.webContents, 'did-finish-load')
w.loadURL('about:blank') w.loadURL('about:blank')
await emittedOnce(w.webContents, 'did-finish-load') await p
const promise = w.webContents.takeHeapSnapshot('') const promise = w.webContents.takeHeapSnapshot('')
return expect(promise).to.be.eventually.rejectedWith(Error, 'takeHeapSnapshot failed') return expect(promise).to.be.eventually.rejectedWith(Error, 'takeHeapSnapshot failed')
@ -979,12 +985,12 @@ describe('webContents module', () => {
sandbox: true sandbox: true
} }
}) })
w.loadURL('data:text/html,%3Ch1%3EHello%2C%20World!%3C%2Fh1%3E')
w.webContents.once('did-finish-load', () => { w.webContents.once('did-finish-load', () => {
const printers = w.webContents.getPrinters() const printers = w.webContents.getPrinters()
assert.strictEqual(Array.isArray(printers), true) assert.strictEqual(Array.isArray(printers), true)
done() done()
}) })
w.loadURL('data:text/html,%3Ch1%3EHello%2C%20World!%3C%2Fh1%3E')
}) })
}) })
@ -1006,7 +1012,6 @@ describe('webContents module', () => {
sandbox: true sandbox: true
} }
}) })
w.loadURL('data:text/html,%3Ch1%3EHello%2C%20World!%3C%2Fh1%3E')
w.webContents.once('did-finish-load', () => { w.webContents.once('did-finish-load', () => {
w.webContents.printToPDF({}, function (error, data) { w.webContents.printToPDF({}, function (error, data) {
assert.strictEqual(error, null) assert.strictEqual(error, null)
@ -1015,6 +1020,7 @@ describe('webContents module', () => {
done() done()
}) })
}) })
w.loadURL('data:text/html,%3Ch1%3EHello%2C%20World!%3C%2Fh1%3E')
}) })
}) })
}) })