Add char/keypress event tests

This commit is contained in:
Kevin Sawicki 2016-10-07 15:12:35 -07:00
parent e4b0577cb0
commit 99414065a9
2 changed files with 37 additions and 16 deletions

View file

@ -99,9 +99,14 @@ describe('webContents module', function () {
}) })
describe('sendInputEvent(event)', function () { describe('sendInputEvent(event)', function () {
it('can send keydown events', function (done) { beforeEach(function (done) {
w.loadURL('file://' + path.join(__dirname, 'fixtures', 'pages', 'onkeydown.html')) w.loadURL('file://' + path.join(__dirname, 'fixtures', 'pages', 'key-events.html'))
w.webContents.once('did-finish-load', function () {
done()
})
})
it('can send keydown events', function (done) {
ipcMain.once('keydown', function (event, key, code, keyCode, shiftKey, ctrlKey, altKey) { ipcMain.once('keydown', function (event, key, code, keyCode, shiftKey, ctrlKey, altKey) {
assert.equal(key, 'a') assert.equal(key, 'a')
assert.equal(code, 'KeyA') assert.equal(code, 'KeyA')
@ -111,15 +116,10 @@ describe('webContents module', function () {
assert.equal(altKey, false) assert.equal(altKey, false)
done() done()
}) })
w.webContents.sendInputEvent({type: 'keyDown', keyCode: 'A'})
w.webContents.once('did-finish-load', function () {
w.webContents.sendInputEvent({type: 'keyDown', keyCode: 'A'})
})
}) })
it('can send keydown events with modifiers', function (done) { it('can send keydown events with modifiers', function (done) {
w.loadURL('file://' + path.join(__dirname, 'fixtures', 'pages', 'onkeydown.html'))
ipcMain.once('keydown', function (event, key, code, keyCode, shiftKey, ctrlKey, altKey) { ipcMain.once('keydown', function (event, key, code, keyCode, shiftKey, ctrlKey, altKey) {
assert.equal(key, 'Z') assert.equal(key, 'Z')
assert.equal(code, 'KeyZ') assert.equal(code, 'KeyZ')
@ -129,15 +129,10 @@ describe('webContents module', function () {
assert.equal(altKey, false) assert.equal(altKey, false)
done() done()
}) })
w.webContents.sendInputEvent({type: 'keyDown', keyCode: 'Z', modifiers: ['shift', 'ctrl']})
w.webContents.once('did-finish-load', function () {
w.webContents.sendInputEvent({type: 'keyDown', keyCode: 'Z', modifiers: ['shift', 'ctrl']})
})
}) })
it('can send keydown events with special keys', function (done) { it('can send keydown events with special keys', function (done) {
w.loadURL('file://' + path.join(__dirname, 'fixtures', 'pages', 'onkeydown.html'))
ipcMain.once('keydown', function (event, key, code, keyCode, shiftKey, ctrlKey, altKey) { ipcMain.once('keydown', function (event, key, code, keyCode, shiftKey, ctrlKey, altKey) {
assert.equal(key, 'Tab') assert.equal(key, 'Tab')
assert.equal(code, 'Tab') assert.equal(code, 'Tab')
@ -147,10 +142,33 @@ describe('webContents module', function () {
assert.equal(altKey, true) assert.equal(altKey, true)
done() done()
}) })
w.webContents.sendInputEvent({type: 'keyDown', keyCode: 'Tab', modifiers: ['alt']})
})
w.webContents.once('did-finish-load', function () { it('can send char events', function (done) {
w.webContents.sendInputEvent({type: 'keyDown', keyCode: 'Tab', modifiers: ['alt']}) ipcMain.once('keypress', function (event, key, code, keyCode, shiftKey, ctrlKey, altKey) {
assert.equal(key, 'a')
assert.equal(code, 'KeyA')
assert.equal(keyCode, 65)
assert.equal(shiftKey, false)
assert.equal(ctrlKey, false)
assert.equal(altKey, false)
done()
}) })
w.webContents.sendInputEvent({type: 'char', keyCode: 'A'})
})
it('can send char events with modifiers', function (done) {
ipcMain.once('keypress', function (event, key, code, keyCode, shiftKey, ctrlKey, altKey) {
assert.equal(key, 'Z')
assert.equal(code, 'KeyZ')
assert.equal(keyCode, 90)
assert.equal(shiftKey, true)
assert.equal(ctrlKey, true)
assert.equal(altKey, false)
done()
})
w.webContents.sendInputEvent({type: 'char', keyCode: 'Z', modifiers: ['shift', 'ctrl']})
}) })
}) })
}) })

View file

@ -4,6 +4,9 @@
document.onkeydown = function (e) { document.onkeydown = function (e) {
require('electron').ipcRenderer.send('keydown', e.key, e.code, e.keyCode, e.shiftKey, e.ctrlKey, e.altKey) require('electron').ipcRenderer.send('keydown', e.key, e.code, e.keyCode, e.shiftKey, e.ctrlKey, e.altKey)
} }
document.onkeypress = function (e) {
require('electron').ipcRenderer.send('keypress', e.key, e.code, e.keyCode, e.shiftKey, e.ctrlKey, e.altKey)
}
</script> </script>
</body> </body>
</html> </html>