electron/spec/api-debugger-spec.js

193 lines
5.4 KiB
JavaScript
Raw Normal View History

2016-03-25 20:03:49 +00:00
const assert = require('assert')
const http = require('http')
2016-03-25 20:03:49 +00:00
const path = require('path')
const {closeWindow} = require('./window-helpers')
2016-03-25 20:03:49 +00:00
const BrowserWindow = require('electron').remote.BrowserWindow
2016-01-22 08:47:23 +00:00
2017-10-27 00:35:33 +00:00
describe('debugger module', () => {
2017-10-27 00:32:04 +00:00
const fixtures = path.resolve(__dirname, 'fixtures')
let w = null
2017-10-27 00:32:04 +00:00
beforeEach(() => {
2016-01-22 08:47:23 +00:00
w = new BrowserWindow({
show: false,
width: 400,
height: 400
2016-03-25 20:03:49 +00:00
})
})
2017-10-27 00:32:04 +00:00
afterEach(() => closeWindow(w).then(() => { w = null }))
2016-01-22 08:47:23 +00:00
2017-10-27 00:32:04 +00:00
describe('debugger.attach', () => {
it('fails when devtools is already open', (done) => {
w.webContents.on('did-finish-load', () => {
2016-03-25 20:03:49 +00:00
w.webContents.openDevTools()
2016-01-22 08:47:23 +00:00
try {
2016-03-25 20:03:49 +00:00
w.webContents.debugger.attach()
2016-03-28 23:19:18 +00:00
} catch (err) {
2016-03-25 20:03:49 +00:00
assert(w.webContents.debugger.isAttached())
done()
2016-01-22 08:47:23 +00:00
}
2016-03-25 20:03:49 +00:00
})
2017-10-27 00:32:04 +00:00
w.webContents.loadURL(`file://${path.join(fixtures, 'pages', 'a.html')}`)
2016-03-25 20:03:49 +00:00
})
2016-01-22 08:47:23 +00:00
2017-10-27 00:32:04 +00:00
it('fails when protocol version is not supported', (done) => {
2016-01-22 08:47:23 +00:00
try {
2016-03-25 20:03:49 +00:00
w.webContents.debugger.attach('2.0')
2016-03-28 23:19:18 +00:00
} catch (err) {
2016-03-25 20:03:49 +00:00
assert(!w.webContents.debugger.isAttached())
done()
2016-01-22 08:47:23 +00:00
}
2016-03-25 20:03:49 +00:00
})
2016-01-22 08:47:23 +00:00
2017-10-27 00:32:04 +00:00
it('attaches when no protocol version is specified', (done) => {
2016-01-22 08:47:23 +00:00
try {
2016-03-25 20:03:49 +00:00
w.webContents.debugger.attach()
2016-03-28 23:19:18 +00:00
} catch (err) {
2017-10-27 00:32:04 +00:00
done(`unexpected error : ${err}`)
2016-01-22 08:47:23 +00:00
}
2016-03-25 20:03:49 +00:00
assert(w.webContents.debugger.isAttached())
done()
})
})
2016-01-22 08:47:23 +00:00
2017-10-27 00:32:04 +00:00
describe('debugger.detach', () => {
it('fires detach event', (done) => {
w.webContents.debugger.on('detach', (e, reason) => {
2016-03-25 20:03:49 +00:00
assert.equal(reason, 'target closed')
assert(!w.webContents.debugger.isAttached())
done()
})
2016-01-22 08:47:23 +00:00
try {
2016-03-25 20:03:49 +00:00
w.webContents.debugger.attach()
2016-03-28 23:19:18 +00:00
} catch (err) {
2017-10-27 00:32:04 +00:00
done(`unexpected error : ${err}`)
2016-01-22 08:47:23 +00:00
}
2016-03-25 20:03:49 +00:00
w.webContents.debugger.detach()
})
})
2016-01-22 08:47:23 +00:00
2017-10-27 00:32:04 +00:00
describe('debugger.sendCommand', () => {
let server
2017-10-27 00:32:04 +00:00
afterEach(() => {
if (server != null) {
server.close()
server = null
}
})
2017-10-27 00:32:04 +00:00
it('retuns response', (done) => {
2016-03-25 20:03:49 +00:00
w.webContents.loadURL('about:blank')
2016-01-22 08:47:23 +00:00
try {
2016-03-25 20:03:49 +00:00
w.webContents.debugger.attach()
2016-03-28 23:19:18 +00:00
} catch (err) {
return done('unexpected error : ' + err)
2016-03-25 20:03:49 +00:00
}
var callback = function (err, res) {
2016-03-30 21:06:50 +00:00
assert(!err.message)
2016-03-25 20:03:49 +00:00
assert(!res.wasThrown)
assert.equal(res.result.value, 6)
w.webContents.debugger.detach()
done()
2016-01-22 08:47:23 +00:00
}
const params = {
'expression': '4+2'
2016-03-25 20:03:49 +00:00
}
w.webContents.debugger.sendCommand('Runtime.evaluate', params, callback)
})
2016-01-22 08:47:23 +00:00
2017-10-27 00:32:04 +00:00
it('fires message event', (done) => {
const url = process.platform !== 'win32'
? `file://${path.join(fixtures, 'pages', 'a.html')}`
: 'file:///' + path.join(fixtures, 'pages', 'a.html').replace(/\\/g, '/')
2016-03-25 20:03:49 +00:00
w.webContents.loadURL(url)
2016-01-22 08:47:23 +00:00
try {
2016-03-25 20:03:49 +00:00
w.webContents.debugger.attach()
2016-03-28 23:19:18 +00:00
} catch (err) {
2016-03-25 20:03:49 +00:00
done('unexpected error : ' + err)
2016-01-22 08:47:23 +00:00
}
2017-10-27 00:32:04 +00:00
w.webContents.debugger.on('message', (e, method, params) => {
if (method === 'Console.messageAdded') {
assert.equal(params.message.level, 'log')
2016-03-25 20:03:49 +00:00
assert.equal(params.message.url, url)
assert.equal(params.message.text, 'a')
w.webContents.debugger.detach()
done()
2016-01-22 08:47:23 +00:00
}
2016-03-25 20:03:49 +00:00
})
w.webContents.debugger.sendCommand('Console.enable')
})
2016-01-22 08:47:23 +00:00
2017-10-27 00:32:04 +00:00
it('returns error message when command fails', (done) => {
2016-03-25 20:03:49 +00:00
w.webContents.loadURL('about:blank')
2016-01-22 08:47:23 +00:00
try {
2016-03-25 20:03:49 +00:00
w.webContents.debugger.attach()
2016-03-28 23:19:18 +00:00
} catch (err) {
2017-10-27 00:32:04 +00:00
done(`unexpected error : ${err}`)
2016-01-22 08:47:23 +00:00
}
2017-10-27 00:32:04 +00:00
w.webContents.debugger.sendCommand('Test', (err) => {
2016-03-25 20:03:49 +00:00
assert.equal(err.message, "'Test' wasn't found")
w.webContents.debugger.detach()
done()
})
})
2018-02-02 00:47:52 +00:00
it('handles valid unicode characters in message', (done) => {
try {
w.webContents.debugger.attach()
} catch (err) {
2017-10-27 00:32:04 +00:00
done(`unexpected error : ${err}`)
}
w.webContents.debugger.on('message', (event, method, params) => {
if (method === 'Network.loadingFinished') {
w.webContents.debugger.sendCommand('Network.getResponseBody', {
requestId: params.requestId
2018-02-02 00:47:52 +00:00
}, (_, data) => {
assert.equal(data.body, '\u0024')
done()
})
}
})
2018-02-02 00:47:52 +00:00
server = http.createServer((req, res) => {
res.setHeader('Content-Type', 'text/plain; charset=utf-8')
res.end('\u0024')
})
server.listen(0, '127.0.0.1', () => {
w.webContents.debugger.sendCommand('Network.enable')
w.loadURL(`http://127.0.0.1:${server.address().port}`)
})
})
it('does not crash for invalid unicode characters in message', (done) => {
try {
w.webContents.debugger.attach()
} catch (err) {
done(`unexpected error : ${err}`)
}
w.webContents.debugger.on('message', (event, method, params) => {
// loadingFinished indicates that page has been loaded and it did not
// crash because of invalid UTF-8 data
if (method === 'Network.loadingFinished') {
done()
}
})
server = http.createServer((req, res) => {
res.setHeader('Content-Type', 'text/plain; charset=utf-8')
res.end('\uFFFF')
})
server.listen(0, '127.0.0.1', () => {
w.webContents.debugger.sendCommand('Network.enable')
w.loadURL(`http://127.0.0.1:${server.address().port}`)
})
})
2016-03-25 20:03:49 +00:00
})
})