Skip to content
Permalink
Browse files

fix: move window.open postMessage test to main runner to fix flake (#…

  • Loading branch information...
nornagon authored and zcbenz committed Jun 11, 2019
1 parent 81ad355 commit e5d1e7b4da8aa41e376d9b2707071f5dea48be6d
@@ -1,7 +1,8 @@
import * as chai from 'chai'
import * as chaiAsPromised from 'chai-as-promised'
import { BrowserWindow, session } from 'electron'
import { BrowserWindow, session, ipcMain } from 'electron'
import { emittedOnce } from './events-helpers';
import { closeAllWindows } from './window-helpers';
import * as https from 'https';
import * as path from 'path';
import * as fs from 'fs';
@@ -70,3 +71,19 @@ describe('reporting api', () => {
}
})
})

describe('window.postMessage', () => {
afterEach(async () => {
await closeAllWindows()
})

it('sets the source and origin correctly', async () => {
const w = new BrowserWindow({show: false, webPreferences: {nodeIntegration: true}})
w.loadURL(`file://${fixturesPath}/pages/window-open-postMessage-driver.html`)
const [, message] = await emittedOnce(ipcMain, 'complete')
expect(message.data).to.equal('testing')
expect(message.origin).to.equal('file://')
expect(message.sourceEqualsOpener).to.equal(true)
expect(message.eventOrigin).to.equal('file://')
})
})
@@ -39,3 +39,9 @@ export const closeWindow = async (
}
}
}

export async function closeAllWindows() {
for (const w of BrowserWindow.getAllWindows()) {
await closeWindow(w, {assertNotWindows: false})
}
}
@@ -721,27 +721,6 @@ describe('chromium feature', () => {
})

describe('window.postMessage', () => {
it('sets the source and origin correctly', (done) => {
let b = null
listener = (event) => {
window.removeEventListener('message', listener)
b.close()
const message = JSON.parse(event.data)
expect(message.data).to.equal('testing')
expect(message.origin).to.equal('file://')
expect(message.sourceEqualsOpener).to.be.true()
expect(event.origin).to.equal('file://')
done()
}
window.addEventListener('message', listener)
app.once('browser-window-created', (event, { webContents }) => {
webContents.once('did-finish-load', () => {
b.postMessage('testing', '*')
})
})
b = window.open(`file://${fixtures}/pages/window-open-postMessage.html`, '', 'show=no')
})

it('throws an exception when the targetOrigin cannot be converted to a string', () => {
const b = window.open('')
expect(() => {
@@ -0,0 +1,10 @@
<script>
const child = window.open(`./window-open-postMessage.html`, '', 'show=no')
window.onmessage = (e) => {
if (e.data === 'ready') {
child.postMessage('testing', '*')
} else {
require('electron').ipcRenderer.send('complete', {eventOrigin: e.origin, ...JSON.parse(e.data)})
}
}
</script>
@@ -8,6 +8,7 @@
sourceEqualsOpener: e.source === window.opener
}), '*');
});
window.opener.postMessage("ready", "*")
</script>
</body>
</html>

0 comments on commit e5d1e7b

Please sign in to comment.
You can’t perform that action at this time.