@@ -9,7 +9,7 @@ const { emittedOnce } = require('./events-helpers')
99const chai = require ( 'chai' )
1010const dirtyChai = require ( 'dirty-chai' )
1111
12- const { ipcRenderer, remote } = require ( 'electron' )
12+ const { ipcRenderer, remote, clipboard } = require ( 'electron' )
1313const { BrowserWindow, webContents, ipcMain, session } = remote
1414const { expect } = chai
1515
@@ -255,6 +255,61 @@ describe('webContents module', () => {
255255 } )
256256 } )
257257
258+ describe ( 'devtools window' , ( ) => {
259+ let testFn = it
260+ if ( process . platform === 'darwin' && isCi ) {
261+ testFn = it . skip
262+ }
263+ try {
264+ // We have other tests that check if native modules work, if we fail to require
265+ // robotjs let's skip this test to avoid false negatives
266+ require ( 'robotjs' )
267+ } catch ( err ) {
268+ testFn = it . skip
269+ }
270+
271+ testFn ( 'can receive and handle menu events' , async function ( ) {
272+ this . timeout ( 5000 )
273+ w . show ( )
274+ w . loadFile ( path . join ( fixtures , 'pages' , 'key-events.html' ) )
275+ // Ensure the devtools are loaded
276+ w . webContents . closeDevTools ( )
277+ const opened = emittedOnce ( w . webContents , 'devtools-opened' )
278+ w . webContents . openDevTools ( )
279+ await opened
280+ await emittedOnce ( w . webContents . devToolsWebContents , 'did-finish-load' )
281+ w . webContents . devToolsWebContents . focus ( )
282+
283+ // Focus an input field
284+ await w . webContents . devToolsWebContents . executeJavaScript (
285+ `const input = document.createElement('input');
286+ document.body.innerHTML = '';
287+ document.body.appendChild(input)
288+ input.focus();`
289+ )
290+
291+ // Write something to the clipboard
292+ clipboard . writeText ( 'test value' )
293+
294+ // Fake a paste request using robotjs to emulate a REAL keyboard paste event
295+ require ( 'robotjs' ) . keyTap ( 'v' , process . platform === 'darwin' ? [ 'command' ] : [ 'control' ] )
296+
297+ const start = Date . now ( )
298+ let val
299+
300+ // Check every now and again for the pasted value (paste is async)
301+ while ( val !== 'test value' && Date . now ( ) - start <= 1000 ) {
302+ val = await w . webContents . devToolsWebContents . executeJavaScript (
303+ `document.querySelector('input').value`
304+ )
305+ await new Promise ( resolve => setTimeout ( resolve , 10 ) )
306+ }
307+
308+ // Once we're done expect the paste to have been successful
309+ expect ( val ) . to . equal ( 'test value' , 'value should eventually become the pasted value' )
310+ } )
311+ } )
312+
258313 describe ( 'sendInputEvent(event)' , ( ) => {
259314 beforeEach ( ( done ) => {
260315 w . loadFile ( path . join ( fixtures , 'pages' , 'key-events.html' ) )
0 commit comments