This commit is contained in:
deepak1556 2017-04-05 12:00:36 +05:30 committed by Cheng Zhao
parent 9e3b8ade12
commit e4524ce42e
3 changed files with 150 additions and 0 deletions

View file

@ -86,6 +86,38 @@ describe('BrowserWindow module', function () {
})
describe('BrowserWindow.close()', function () {
let server
before(function (done) {
server = http.createServer((request, response) => {
switch (request.url) {
case '/404':
response.statusCode = '404'
response.end()
break
case '/301':
response.statusCode = '301'
response.setHeader('Location', '/200')
response.end()
break
case '/200':
response.statusCode = '200'
response.end('hello')
break
default:
done('unsupported endpoint')
}
}).listen(0, '127.0.0.1', () => {
server.url = 'http://127.0.0.1:' + server.address().port
done()
})
})
after(function () {
server.close()
server = null
})
it('should emit unload handler', function (done) {
w.webContents.on('did-finish-load', function () {
w.close()
@ -109,6 +141,37 @@ describe('BrowserWindow module', function () {
})
w.loadURL('file://' + path.join(fixtures, 'api', 'beforeunload-false.html'))
})
it('should not crash when invoked synchronously inside navigation observer', function (done) {
const events = [
{ name: 'did-start-loading', url: `${server.url}/200` },
{ name: 'did-get-redirect-request', url: `${server.url}/301` },
{ name: 'did-get-response-details', url: `${server.url}/200` },
{ name: 'dom-ready', url: `${server.url}/200` },
{ name: 'did-stop-loading', url: `${server.url}/200` },
{ name: 'did-finish-load', url: `${server.url}/200` },
{ name: 'did-frame-finish-load', url: `${server.url}/200` },
{ name: 'did-fail-load', url: `${server.url}/404` }
]
const responseEvent = 'window-webContents-destroyed'
function* genNavigationEvent () {
let eventOptions = null
while ((eventOptions = events.shift()) && events.length) {
let w = new BrowserWindow({show: false})
eventOptions.id = w.id
eventOptions.responseEvent = responseEvent
ipcRenderer.send('test-webcontents-navigation-observer', eventOptions)
yield 1
}
}
let gen = genNavigationEvent()
ipcRenderer.on(responseEvent, function () {
if (!gen.next().value) done()
})
gen.next()
})
})
describe('window.close()', function () {

View file

@ -542,4 +542,70 @@ describe('webContents module', function () {
})
})
})
describe('destroy()', () => {
let server
before(function (done) {
server = http.createServer((request, response) => {
switch (request.url) {
case '/404':
response.statusCode = '404'
response.end()
break
case '/301':
response.statusCode = '301'
response.setHeader('Location', '/200')
response.end()
break
case '/200':
response.statusCode = '200'
response.end('hello')
break
default:
done('unsupported endpoint')
}
}).listen(0, '127.0.0.1', () => {
server.url = 'http://127.0.0.1:' + server.address().port
done()
})
})
after(function () {
server.close()
server = null
})
it('should not crash when invoked synchronously inside navigation observer', (done) => {
const events = [
{ name: 'did-start-loading', url: `${server.url}/200` },
{ name: 'did-get-redirect-request', url: `${server.url}/301` },
{ name: 'did-get-response-details', url: `${server.url}/200` },
{ name: 'dom-ready', url: `${server.url}/200` },
{ name: 'did-stop-loading', url: `${server.url}/200` },
{ name: 'did-finish-load', url: `${server.url}/200` },
// FIXME: Multiple Emit calls inside an observer assume that object
// will be alive till end of the observer. Synchronous `destroy` api
// violates this contract and crashes.
// { name: 'did-frame-finish-load', url: `${server.url}/200` },
{ name: 'did-fail-load', url: `${server.url}/404` }
]
const responseEvent = 'webcontents-destroyed'
function* genNavigationEvent () {
let eventOptions = null
while ((eventOptions = events.shift()) && events.length) {
eventOptions.responseEvent = responseEvent
ipcRenderer.send('test-webcontents-navigation-observer', eventOptions)
yield 1
}
}
let gen = genNavigationEvent()
ipcRenderer.on(responseEvent, () => {
if (!gen.next().value) done()
})
gen.next()
})
})
})

View file

@ -338,6 +338,27 @@ ipcMain.on('crash-service-pid', (event, pid) => {
event.returnValue = null
})
ipcMain.on('test-webcontents-navigation-observer', (event, options) => {
let contents = null
let destroy = () => {}
if (options.id) {
const w = BrowserWindow.fromId(options.id)
contents = w.webContents
destroy = () => w.close()
} else {
contents = webContents.create()
destroy = () => contents.destroy()
}
contents.once(options.name, () => destroy())
contents.once('destroyed', () => {
event.sender.send(options.responseEvent)
})
contents.loadURL(options.url)
})
// Suspend listeners until the next event and then restore them
const suspendListeners = (emitter, eventName, callback) => {
const listeners = emitter.listeners(eventName)