Add keydown sendInputEvent specs
authorKevin Sawicki <kevinsawicki@gmail.com>
Fri, 7 Oct 2016 21:44:35 +0000 (14:44 -0700)
committerKevin Sawicki <kevinsawicki@gmail.com>
Mon, 10 Oct 2016 16:35:43 +0000 (09:35 -0700)
spec/api-web-contents-spec.js
spec/fixtures/pages/onkeydown.html [new file with mode: 0644]

index 7e77c39bdb558cac24c3987feb190464a5b0236e..3a28ff36dd56c09fa512d6121f76e5d67eeee08b 100644 (file)
@@ -5,7 +5,7 @@ const path = require('path')
 const {closeWindow} = require('./window-helpers')
 
 const {remote} = require('electron')
-const {BrowserWindow, webContents} = remote
+const {BrowserWindow, webContents, ipcMain} = remote
 
 const isCi = remote.getGlobal('isCi')
 
@@ -97,4 +97,60 @@ describe('webContents module', function () {
       })
     })
   })
+
+  describe('sendInputEvent(event)', function () {
+    it('can send keydown events', function (done) {
+      w.loadURL('file://' + path.join(__dirname, 'fixtures', 'pages', 'onkeydown.html'))
+
+      ipcMain.once('keydown', function (event, key, code, keyCode, shiftKey, ctrlKey, altKey) {
+        assert.equal(key, '')
+        assert.equal(code, '')
+        assert.equal(keyCode, 65)
+        assert.equal(shiftKey, false)
+        assert.equal(ctrlKey, false)
+        assert.equal(altKey, false)
+        done()
+      })
+
+      w.webContents.once('did-finish-load', function () {
+        w.webContents.sendInputEvent({type: 'keyDown', keyCode: 'A'})
+      })
+    })
+
+    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) {
+        assert.equal(key, '')
+        assert.equal(code, '')
+        assert.equal(keyCode, 90)
+        assert.equal(shiftKey, true)
+        assert.equal(ctrlKey, true)
+        assert.equal(altKey, false)
+        done()
+      })
+
+      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) {
+      w.loadURL('file://' + path.join(__dirname, 'fixtures', 'pages', 'onkeydown.html'))
+
+      ipcMain.once('keydown', function (event, key, code, keyCode, shiftKey, ctrlKey, altKey) {
+        assert.equal(key, '')
+        assert.equal(code, '')
+        assert.equal(keyCode, 9)
+        assert.equal(shiftKey, false)
+        assert.equal(ctrlKey, false)
+        assert.equal(altKey, true)
+        done()
+      })
+
+      w.webContents.once('did-finish-load', function () {
+        w.webContents.sendInputEvent({type: 'keyDown', keyCode: 'Tab', modifiers: ['alt']})
+      })
+    })
+  })
 })
diff --git a/spec/fixtures/pages/onkeydown.html b/spec/fixtures/pages/onkeydown.html
new file mode 100644 (file)
index 0000000..859893e
--- /dev/null
@@ -0,0 +1,9 @@
+<html>
+<body>
+<script type="text/javascript" charset="utf-8">
+document.onkeydown = function (e) {
+  require('electron').ipcRenderer.send('keydown', e.key, e.code, e.keyCode, e.shiftKey, e.ctrlKey, e.altKey)
+}
+</script>
+</body>
+</html>