| 
									
										
										
										
											2016-07-14 09:10:40 -07:00
										 |  |  | 'use strict' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const assert = require('assert') | 
					
						
							|  |  |  | const path = require('path') | 
					
						
							| 
									
										
										
										
											2016-08-03 12:47:53 -07:00
										 |  |  | const {closeWindow} = require('./window-helpers') | 
					
						
							| 
									
										
										
										
											2016-07-14 09:10:40 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-14 09:17:16 -07:00
										 |  |  | const {remote} = require('electron') | 
					
						
							| 
									
										
										
										
											2016-10-07 14:44:35 -07:00
										 |  |  | const {BrowserWindow, webContents, ipcMain} = remote | 
					
						
							| 
									
										
										
										
											2016-07-14 09:17:16 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-14 09:33:16 -07:00
										 |  |  | const isCi = remote.getGlobal('isCi') | 
					
						
							| 
									
										
										
										
											2016-07-14 09:10:40 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | describe('webContents module', function () { | 
					
						
							| 
									
										
										
										
											2016-07-14 09:33:16 -07:00
										 |  |  |   const fixtures = path.resolve(__dirname, 'fixtures') | 
					
						
							|  |  |  |   let w | 
					
						
							| 
									
										
										
										
											2016-07-14 09:10:40 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   beforeEach(function () { | 
					
						
							|  |  |  |     w = new BrowserWindow({ | 
					
						
							|  |  |  |       show: false, | 
					
						
							|  |  |  |       width: 400, | 
					
						
							|  |  |  |       height: 400, | 
					
						
							|  |  |  |       webPreferences: { | 
					
						
							|  |  |  |         backgroundThrottling: false | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   afterEach(function () { | 
					
						
							| 
									
										
										
										
											2016-08-03 12:47:53 -07:00
										 |  |  |     return closeWindow(w).then(function () { w = null }) | 
					
						
							| 
									
										
										
										
											2016-07-14 09:10:40 -07:00
										 |  |  |   }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe('getAllWebContents() API', function () { | 
					
						
							|  |  |  |     it('returns an array of web contents', function (done) { | 
					
						
							|  |  |  |       w.webContents.on('devtools-opened', function () { | 
					
						
							| 
									
										
										
										
											2016-07-14 09:37:09 -07:00
										 |  |  |         const all = webContents.getAllWebContents().sort(function (a, b) { | 
					
						
							|  |  |  |           return a.getId() - b.getId() | 
					
						
							|  |  |  |         }) | 
					
						
							| 
									
										
										
										
											2016-07-14 09:10:40 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-16 16:19:05 -07:00
										 |  |  |         assert.ok(all.length >= 4) | 
					
						
							| 
									
										
										
										
											2016-07-14 09:37:09 -07:00
										 |  |  |         assert.equal(all[0].getType(), 'window') | 
					
						
							| 
									
										
										
										
											2016-08-16 16:19:05 -07:00
										 |  |  |         assert.equal(all[all.length - 2].getType(), 'remote') | 
					
						
							|  |  |  |         assert.equal(all[all.length - 1].getType(), 'webview') | 
					
						
							| 
									
										
										
										
											2016-07-14 09:10:40 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         done() | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       w.loadURL('file://' + path.join(fixtures, 'pages', 'webview-zoom-factor.html')) | 
					
						
							|  |  |  |       w.webContents.openDevTools() | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |   }) | 
					
						
							| 
									
										
										
										
											2016-07-14 09:17:16 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   describe('getFocusedWebContents() API', function () { | 
					
						
							|  |  |  |     it('returns the focused web contents', function (done) { | 
					
						
							| 
									
										
										
										
											2016-08-23 10:06:05 -07:00
										 |  |  |       if (isCi) return done() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-14 09:33:16 -07:00
										 |  |  |       const specWebContents = remote.getCurrentWebContents() | 
					
						
							| 
									
										
										
										
											2016-07-14 09:17:16 -07:00
										 |  |  |       assert.equal(specWebContents.getId(), webContents.getFocusedWebContents().getId()) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-21 17:08:43 +09:00
										 |  |  |       specWebContents.once('devtools-opened', function () { | 
					
						
							| 
									
										
										
										
											2016-07-14 09:17:16 -07:00
										 |  |  |         assert.equal(specWebContents.devToolsWebContents.getId(), webContents.getFocusedWebContents().getId()) | 
					
						
							|  |  |  |         specWebContents.closeDevTools() | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-21 17:08:43 +09:00
										 |  |  |       specWebContents.once('devtools-closed', function () { | 
					
						
							| 
									
										
										
										
											2016-07-14 09:17:16 -07:00
										 |  |  |         assert.equal(specWebContents.getId(), webContents.getFocusedWebContents().getId()) | 
					
						
							|  |  |  |         done() | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       specWebContents.openDevTools() | 
					
						
							|  |  |  |     }) | 
					
						
							| 
									
										
										
										
											2016-08-23 10:06:05 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     it('does not crash when called on a detached dev tools window', function (done) { | 
					
						
							|  |  |  |       const specWebContents = w.webContents | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       specWebContents.once('devtools-opened', function () { | 
					
						
							|  |  |  |         assert.doesNotThrow(function () { | 
					
						
							|  |  |  |           webContents.getFocusedWebContents() | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |         specWebContents.closeDevTools() | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       specWebContents.once('devtools-closed', function () { | 
					
						
							|  |  |  |         assert.doesNotThrow(function () { | 
					
						
							|  |  |  |           webContents.getFocusedWebContents() | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |         done() | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       specWebContents.openDevTools({mode: 'detach'}) | 
					
						
							|  |  |  |       w.inspectElement(100, 100) | 
					
						
							|  |  |  |     }) | 
					
						
							| 
									
										
										
										
											2016-07-14 09:17:16 -07:00
										 |  |  |   }) | 
					
						
							| 
									
										
										
										
											2016-08-15 14:03:31 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   describe('isFocused() API', function () { | 
					
						
							|  |  |  |     it('returns false when the window is hidden', function () { | 
					
						
							|  |  |  |       BrowserWindow.getAllWindows().forEach(function (window) { | 
					
						
							|  |  |  |         assert.equal(!window.isVisible() && window.webContents.isFocused(), false) | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |   }) | 
					
						
							| 
									
										
										
										
											2016-10-07 14:44:35 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   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) { | 
					
						
							| 
									
										
										
										
											2016-10-07 14:49:21 -07:00
										 |  |  |         assert.equal(key, 'a') | 
					
						
							|  |  |  |         assert.equal(code, 'KeyA') | 
					
						
							| 
									
										
										
										
											2016-10-07 14:44:35 -07:00
										 |  |  |         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) { | 
					
						
							| 
									
										
										
										
											2016-10-07 14:49:21 -07:00
										 |  |  |         assert.equal(key, 'Z') | 
					
						
							|  |  |  |         assert.equal(code, 'KeyZ') | 
					
						
							| 
									
										
										
										
											2016-10-07 14:44:35 -07:00
										 |  |  |         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) { | 
					
						
							| 
									
										
										
										
											2016-10-07 14:49:21 -07:00
										 |  |  |         assert.equal(key, 'Tab') | 
					
						
							|  |  |  |         assert.equal(code, 'Tab') | 
					
						
							| 
									
										
										
										
											2016-10-07 14:44:35 -07:00
										 |  |  |         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']}) | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |   }) | 
					
						
							| 
									
										
										
										
											2016-07-14 09:10:40 -07:00
										 |  |  | }) |