2016-03-25 20:03:49 +00:00
|
|
|
'use strict'
|
2016-01-23 11:35:30 +00:00
|
|
|
|
2016-03-25 20:03:49 +00:00
|
|
|
const assert = require('assert')
|
|
|
|
const fs = require('fs')
|
|
|
|
const path = require('path')
|
|
|
|
const os = require('os')
|
2016-10-12 17:26:53 +00:00
|
|
|
const qs = require('querystring')
|
2016-04-18 17:33:56 +00:00
|
|
|
const http = require('http')
|
2016-08-03 19:47:53 +00:00
|
|
|
const {closeWindow} = require('./window-helpers')
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-11-11 18:13:06 +00:00
|
|
|
const {ipcRenderer, remote, screen} = require('electron')
|
2017-11-22 22:37:46 +00:00
|
|
|
const {app, ipcMain, BrowserWindow, BrowserView, protocol, webContents} = remote
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-25 20:03:49 +00:00
|
|
|
const isCI = remote.getGlobal('isCi')
|
2017-05-25 23:40:15 +00:00
|
|
|
const nativeModulesEnabled = remote.getGlobal('nativeModulesEnabled')
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('BrowserWindow module', () => {
|
|
|
|
const fixtures = path.resolve(__dirname, 'fixtures')
|
|
|
|
let w = null
|
|
|
|
let ws = null
|
|
|
|
let server
|
|
|
|
let postData
|
|
|
|
|
|
|
|
before((done) => {
|
2016-10-12 17:26:53 +00:00
|
|
|
const filePath = path.join(fixtures, 'pages', 'a.html')
|
|
|
|
const fileStats = fs.statSync(filePath)
|
|
|
|
postData = [
|
|
|
|
{
|
2016-11-11 17:22:45 +00:00
|
|
|
type: 'rawData',
|
|
|
|
bytes: new Buffer('username=test&file=')
|
2016-10-12 17:26:53 +00:00
|
|
|
},
|
|
|
|
{
|
2016-11-11 17:22:45 +00:00
|
|
|
type: 'file',
|
|
|
|
filePath: filePath,
|
|
|
|
offset: 0,
|
|
|
|
length: fileStats.size,
|
|
|
|
modificationTime: fileStats.mtime.getTime() / 1000
|
2016-10-12 17:26:53 +00:00
|
|
|
}
|
|
|
|
]
|
2017-11-01 14:48:09 +00:00
|
|
|
server = http.createServer((req, res) => {
|
2016-10-12 17:26:53 +00:00
|
|
|
function respond () {
|
|
|
|
if (req.method === 'POST') {
|
|
|
|
let body = ''
|
|
|
|
req.on('data', (data) => {
|
2017-11-01 14:48:09 +00:00
|
|
|
if (data) body += data
|
2016-10-12 17:26:53 +00:00
|
|
|
})
|
|
|
|
req.on('end', () => {
|
|
|
|
let parsedData = qs.parse(body)
|
|
|
|
fs.readFile(filePath, (err, data) => {
|
|
|
|
if (err) return
|
|
|
|
if (parsedData.username === 'test' &&
|
|
|
|
parsedData.file === data.toString()) {
|
|
|
|
res.end()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
res.end()
|
|
|
|
}
|
|
|
|
}
|
2016-04-20 05:05:09 +00:00
|
|
|
setTimeout(respond, req.url.includes('slow') ? 200 : 0)
|
2016-06-29 16:37:10 +00:00
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
server.listen(0, '127.0.0.1', () => {
|
|
|
|
server.url = `http://127.0.0.1:${server.address().port}`
|
2016-04-20 05:05:09 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
after(() => {
|
2016-04-20 05:05:09 +00:00
|
|
|
server.close()
|
|
|
|
server = null
|
|
|
|
})
|
2016-02-09 18:12:45 +00:00
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
beforeEach(() => {
|
2016-02-09 18:12:45 +00:00
|
|
|
w = new BrowserWindow({
|
2016-01-12 02:40:23 +00:00
|
|
|
show: false,
|
|
|
|
width: 400,
|
2016-06-26 02:53:58 +00:00
|
|
|
height: 400,
|
|
|
|
webPreferences: {
|
|
|
|
backgroundThrottling: false
|
|
|
|
}
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
2016-02-09 18:12:45 +00:00
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
afterEach(() => {
|
|
|
|
return closeWindow(w).then(() => { w = null })
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('BrowserWindow.close()', () => {
|
2017-04-05 06:30:36 +00:00
|
|
|
let server
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
before((done) => {
|
2017-04-05 06:30:36 +00:00
|
|
|
server = http.createServer((request, response) => {
|
|
|
|
switch (request.url) {
|
|
|
|
case '/404':
|
|
|
|
response.statusCode = '404'
|
|
|
|
response.end()
|
|
|
|
break
|
|
|
|
case '/301':
|
|
|
|
response.statusCode = '301'
|
|
|
|
response.setHeader('Location', '/200')
|
|
|
|
response.end()
|
|
|
|
break
|
|
|
|
case '/200':
|
|
|
|
response.statusCode = '200'
|
|
|
|
response.end('hello')
|
|
|
|
break
|
2017-04-05 11:27:33 +00:00
|
|
|
case '/title':
|
|
|
|
response.statusCode = '200'
|
|
|
|
response.end('<title>Hello</title>')
|
|
|
|
break
|
2017-04-05 06:30:36 +00:00
|
|
|
default:
|
|
|
|
done('unsupported endpoint')
|
|
|
|
}
|
|
|
|
}).listen(0, '127.0.0.1', () => {
|
|
|
|
server.url = 'http://127.0.0.1:' + server.address().port
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
after(() => {
|
2017-04-05 06:30:36 +00:00
|
|
|
server.close()
|
|
|
|
server = null
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
it('should emit unload handler', (done) => {
|
|
|
|
w.webContents.on('did-finish-load', () => { w.close() })
|
|
|
|
w.once('closed', () => {
|
|
|
|
const test = path.join(fixtures, 'api', 'unload')
|
|
|
|
const content = fs.readFileSync(test)
|
2016-03-25 20:03:49 +00:00
|
|
|
fs.unlinkSync(test)
|
|
|
|
assert.equal(String(content), 'unload')
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
w.loadURL('file://' + path.join(fixtures, 'api', 'unload.html'))
|
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
it('should emit beforeunload handler', (done) => {
|
|
|
|
w.once('onbeforeunload', () => { done() })
|
|
|
|
w.webContents.on('did-finish-load', () => { w.close() })
|
|
|
|
w.loadURL(`file://${path.join(fixtures, 'api', 'beforeunload-false.html')}`)
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
it('should not crash when invoked synchronously inside navigation observer', (done) => {
|
2017-04-05 06:30:36 +00:00
|
|
|
const events = [
|
|
|
|
{ name: 'did-start-loading', url: `${server.url}/200` },
|
|
|
|
{ name: 'did-get-redirect-request', url: `${server.url}/301` },
|
|
|
|
{ name: 'did-get-response-details', url: `${server.url}/200` },
|
|
|
|
{ name: 'dom-ready', url: `${server.url}/200` },
|
2017-04-05 11:27:33 +00:00
|
|
|
{ name: 'page-title-updated', url: `${server.url}/title` },
|
2017-04-05 06:30:36 +00:00
|
|
|
{ name: 'did-stop-loading', url: `${server.url}/200` },
|
|
|
|
{ name: 'did-finish-load', url: `${server.url}/200` },
|
|
|
|
{ name: 'did-frame-finish-load', url: `${server.url}/200` },
|
|
|
|
{ name: 'did-fail-load', url: `${server.url}/404` }
|
|
|
|
]
|
|
|
|
const responseEvent = 'window-webContents-destroyed'
|
|
|
|
|
|
|
|
function* genNavigationEvent () {
|
|
|
|
let eventOptions = null
|
|
|
|
while ((eventOptions = events.shift()) && events.length) {
|
|
|
|
let w = new BrowserWindow({show: false})
|
|
|
|
eventOptions.id = w.id
|
|
|
|
eventOptions.responseEvent = responseEvent
|
|
|
|
ipcRenderer.send('test-webcontents-navigation-observer', eventOptions)
|
|
|
|
yield 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let gen = genNavigationEvent()
|
2017-11-01 14:48:09 +00:00
|
|
|
ipcRenderer.on(responseEvent, () => {
|
2017-04-05 06:30:36 +00:00
|
|
|
if (!gen.next().value) done()
|
|
|
|
})
|
|
|
|
gen.next()
|
|
|
|
})
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('window.close()', () => {
|
|
|
|
it('should emit unload handler', (done) => {
|
|
|
|
w.once('closed', () => {
|
|
|
|
const test = path.join(fixtures, 'api', 'close')
|
|
|
|
const content = fs.readFileSync(test)
|
2016-03-25 20:03:49 +00:00
|
|
|
fs.unlinkSync(test)
|
|
|
|
assert.equal(String(content), 'close')
|
|
|
|
done()
|
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
w.loadURL(`file://${path.join(fixtures, 'api', 'close.html')}`)
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
it('should emit beforeunload handler', (done) => {
|
|
|
|
w.once('onbeforeunload', () => { done() })
|
|
|
|
w.loadURL(`file://${path.join(fixtures, 'api', 'close-beforeunload-false.html')}`)
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('BrowserWindow.destroy()', () => {
|
|
|
|
it('prevents users to access methods of webContents', () => {
|
2016-12-06 00:12:15 +00:00
|
|
|
const contents = w.webContents
|
2016-03-25 20:03:49 +00:00
|
|
|
w.destroy()
|
2017-11-01 14:48:09 +00:00
|
|
|
assert.throws(() => {
|
2016-12-06 00:12:15 +00:00
|
|
|
contents.getId()
|
2016-03-29 00:16:08 +00:00
|
|
|
}, /Object has been destroyed/)
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('BrowserWindow.loadURL(url)', () => {
|
|
|
|
it('should emit did-start-loading event', (done) => {
|
|
|
|
w.webContents.on('did-start-loading', () => { done() })
|
2016-03-25 20:03:49 +00:00
|
|
|
w.loadURL('about:blank')
|
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
it('should emit ready-to-show event', (done) => {
|
|
|
|
w.on('ready-to-show', () => { done() })
|
2016-10-31 19:40:49 +00:00
|
|
|
w.loadURL('about:blank')
|
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
it('should emit did-get-response-details event', (done) => {
|
2016-04-08 18:19:36 +00:00
|
|
|
// expected {fileName: resourceType} pairs
|
2017-11-01 14:48:09 +00:00
|
|
|
const expectedResources = {
|
2016-04-08 18:19:36 +00:00
|
|
|
'did-get-response-details.html': 'mainFrame',
|
|
|
|
'logo.png': 'image'
|
|
|
|
}
|
2017-11-01 14:48:09 +00:00
|
|
|
let responses = 0
|
|
|
|
w.webContents.on('did-get-response-details', (event, status, newUrl, oldUrl, responseCode, method, referrer, headers, resourceType) => {
|
|
|
|
responses += 1
|
|
|
|
const fileName = newUrl.slice(newUrl.lastIndexOf('/') + 1)
|
|
|
|
const expectedType = expectedResources[fileName]
|
2016-04-08 18:19:36 +00:00
|
|
|
assert(!!expectedType, `Unexpected response details for ${newUrl}`)
|
|
|
|
assert(typeof status === 'boolean', 'status should be boolean')
|
|
|
|
assert.equal(responseCode, 200)
|
|
|
|
assert.equal(method, 'GET')
|
|
|
|
assert(typeof referrer === 'string', 'referrer should be string')
|
|
|
|
assert(!!headers, 'headers should be present')
|
|
|
|
assert(typeof headers === 'object', 'headers should be object')
|
|
|
|
assert.equal(resourceType, expectedType, 'Incorrect resourceType')
|
2017-11-01 14:48:09 +00:00
|
|
|
if (responses === Object.keys(expectedResources).length) done()
|
2016-04-08 18:19:36 +00:00
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
w.loadURL(`file://${path.join(fixtures, 'pages', 'did-get-response-details.html')}`)
|
2016-04-08 18:19:36 +00:00
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
it('should emit did-fail-load event for files that do not exist', (done) => {
|
|
|
|
w.webContents.on('did-fail-load', (event, code, desc, url, isMainFrame) => {
|
2016-03-25 20:03:49 +00:00
|
|
|
assert.equal(code, -6)
|
2016-05-21 04:51:15 +00:00
|
|
|
assert.equal(desc, 'ERR_FILE_NOT_FOUND')
|
2016-04-05 02:24:58 +00:00
|
|
|
assert.equal(isMainFrame, true)
|
2016-03-25 20:03:49 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
w.loadURL('file://a.txt')
|
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
it('should emit did-fail-load event for invalid URL', (done) => {
|
|
|
|
w.webContents.on('did-fail-load', (event, code, desc, url, isMainFrame) => {
|
2016-03-25 20:03:49 +00:00
|
|
|
assert.equal(desc, 'ERR_INVALID_URL')
|
|
|
|
assert.equal(code, -300)
|
2016-04-05 02:24:58 +00:00
|
|
|
assert.equal(isMainFrame, true)
|
2016-03-25 20:03:49 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
w.loadURL('http://example:port')
|
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
it('should set `mainFrame = false` on did-fail-load events in iframes', (done) => {
|
|
|
|
w.webContents.on('did-fail-load', (event, code, desc, url, isMainFrame) => {
|
2016-04-05 02:24:58 +00:00
|
|
|
assert.equal(isMainFrame, false)
|
|
|
|
done()
|
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
w.loadURL(`file://${path.join(fixtures, 'api', 'did-fail-load-iframe.html')}`)
|
2016-04-05 02:24:58 +00:00
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
it('does not crash in did-fail-provisional-load handler', (done) => {
|
|
|
|
w.webContents.once('did-fail-provisional-load', () => {
|
2016-04-30 09:21:18 +00:00
|
|
|
w.loadURL('http://127.0.0.1:11111')
|
2016-04-13 10:39:11 +00:00
|
|
|
done()
|
|
|
|
})
|
2016-04-30 09:21:18 +00:00
|
|
|
w.loadURL('http://127.0.0.1:11111')
|
2016-04-13 10:39:11 +00:00
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
it('should emit did-fail-load event for URL exceeding character limit', (done) => {
|
|
|
|
w.webContents.on('did-fail-load', (event, code, desc, url, isMainFrame) => {
|
2017-01-23 14:27:47 +00:00
|
|
|
assert.equal(desc, 'ERR_INVALID_URL')
|
|
|
|
assert.equal(code, -300)
|
|
|
|
assert.equal(isMainFrame, true)
|
|
|
|
done()
|
|
|
|
})
|
2017-01-25 08:50:33 +00:00
|
|
|
const data = new Buffer(2 * 1024 * 1024).toString('base64')
|
|
|
|
w.loadURL(`data:image/png;base64,${data}`)
|
2017-01-23 14:27:47 +00:00
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('POST navigations', () => {
|
|
|
|
afterEach(() => { w.webContents.session.webRequest.onBeforeSendHeaders(null) })
|
2016-11-11 18:13:06 +00:00
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
it('supports specifying POST data', (done) => {
|
2016-11-11 18:13:06 +00:00
|
|
|
w.webContents.on('did-finish-load', () => done())
|
|
|
|
w.loadURL(server.url, {postData: postData})
|
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
it('sets the content type header on URL encoded forms', (done) => {
|
2016-11-11 18:13:06 +00:00
|
|
|
w.webContents.on('did-finish-load', () => {
|
|
|
|
w.webContents.session.webRequest.onBeforeSendHeaders((details, callback) => {
|
|
|
|
assert.equal(details.requestHeaders['content-type'], 'application/x-www-form-urlencoded')
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
w.webContents.executeJavaScript(`
|
|
|
|
form = document.createElement('form')
|
2016-12-19 19:25:25 +00:00
|
|
|
document.body.appendChild(form)
|
2016-11-11 18:13:06 +00:00
|
|
|
form.method = 'POST'
|
|
|
|
form.target = '_blank'
|
|
|
|
form.submit()
|
|
|
|
`)
|
|
|
|
})
|
|
|
|
w.loadURL(server.url)
|
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
it('sets the content type header on multi part forms', (done) => {
|
2016-11-11 18:13:06 +00:00
|
|
|
w.webContents.on('did-finish-load', () => {
|
|
|
|
w.webContents.session.webRequest.onBeforeSendHeaders((details, callback) => {
|
|
|
|
assert(details.requestHeaders['content-type'].startsWith('multipart/form-data; boundary=----WebKitFormBoundary'))
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
w.webContents.executeJavaScript(`
|
|
|
|
form = document.createElement('form')
|
2016-12-19 19:25:25 +00:00
|
|
|
document.body.appendChild(form)
|
2016-11-11 18:13:06 +00:00
|
|
|
form.method = 'POST'
|
|
|
|
form.target = '_blank'
|
|
|
|
form.enctype = 'multipart/form-data'
|
|
|
|
file = document.createElement('input')
|
|
|
|
file.type = 'file'
|
|
|
|
file.name = 'file'
|
|
|
|
form.appendChild(file)
|
|
|
|
form.submit()
|
|
|
|
`)
|
|
|
|
})
|
|
|
|
w.loadURL(server.url)
|
|
|
|
})
|
2016-10-12 17:26:53 +00:00
|
|
|
})
|
2017-03-02 22:37:09 +00:00
|
|
|
|
2017-03-02 20:14:18 +00:00
|
|
|
it('should support support base url for data urls', (done) => {
|
2017-11-01 14:48:09 +00:00
|
|
|
ipcMain.once('answer', (event, test) => {
|
2017-03-02 20:14:18 +00:00
|
|
|
assert.equal(test, 'test')
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
w.loadURL('data:text/html,<script src="loaded-from-dataurl.js"></script>', {baseURLForDataURL: `file://${path.join(fixtures, 'api')}${path.sep}`})
|
|
|
|
})
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('will-navigate event', () => {
|
2016-12-21 18:00:27 +00:00
|
|
|
it('allows the window to be closed from the event listener', (done) => {
|
|
|
|
ipcRenderer.send('close-on-will-navigate', w.id)
|
2017-11-01 14:48:09 +00:00
|
|
|
ipcRenderer.once('closed-on-will-navigate', () => { done() })
|
|
|
|
w.loadURL(`file://${fixtures}/pages/will-navigate.html`)
|
2016-12-21 18:00:27 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('BrowserWindow.show()', () => {
|
2017-11-15 21:05:46 +00:00
|
|
|
before(function () {
|
|
|
|
if (isCI) {
|
|
|
|
this.skip()
|
|
|
|
}
|
|
|
|
})
|
2016-02-09 18:12:45 +00:00
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
it('should focus on window', () => {
|
2016-03-25 20:03:49 +00:00
|
|
|
w.show()
|
|
|
|
assert(w.isFocused())
|
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
it('should make the window visible', () => {
|
2016-03-25 20:03:49 +00:00
|
|
|
w.show()
|
|
|
|
assert(w.isVisible())
|
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
it('emits when window is shown', (done) => {
|
|
|
|
w.once('show', () => {
|
2016-03-25 20:03:49 +00:00
|
|
|
assert.equal(w.isVisible(), true)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
w.show()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('BrowserWindow.hide()', () => {
|
2017-11-15 21:05:46 +00:00
|
|
|
before(function () {
|
|
|
|
if (isCI) {
|
|
|
|
this.skip()
|
|
|
|
}
|
|
|
|
})
|
2016-03-08 19:11:17 +00:00
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
it('should defocus on window', () => {
|
2016-03-25 20:03:49 +00:00
|
|
|
w.hide()
|
|
|
|
assert(!w.isFocused())
|
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
it('should make the window not visible', () => {
|
2016-03-25 20:03:49 +00:00
|
|
|
w.show()
|
|
|
|
w.hide()
|
|
|
|
assert(!w.isVisible())
|
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
it('emits when window is hidden', (done) => {
|
2016-03-25 20:03:49 +00:00
|
|
|
w.show()
|
2017-11-01 14:48:09 +00:00
|
|
|
w.once('hide', () => {
|
2016-03-25 20:03:49 +00:00
|
|
|
assert.equal(w.isVisible(), false)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
w.hide()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('BrowserWindow.showInactive()', () => {
|
|
|
|
it('should not focus on window', () => {
|
2016-03-25 20:03:49 +00:00
|
|
|
w.showInactive()
|
|
|
|
assert(!w.isFocused())
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('BrowserWindow.focus()', () => {
|
|
|
|
it('does not make the window become visible', () => {
|
2016-03-25 20:03:49 +00:00
|
|
|
assert.equal(w.isVisible(), false)
|
|
|
|
w.focus()
|
|
|
|
assert.equal(w.isVisible(), false)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('BrowserWindow.blur()', () => {
|
|
|
|
it('removes focus from window', () => {
|
2016-03-25 20:03:49 +00:00
|
|
|
w.blur()
|
|
|
|
assert(!w.isFocused())
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('BrowserWindow.capturePage(rect, callback)', () => {
|
|
|
|
it('calls the callback with a Buffer', (done) => {
|
2016-02-09 18:12:45 +00:00
|
|
|
w.capturePage({
|
2016-01-12 02:40:23 +00:00
|
|
|
x: 0,
|
|
|
|
y: 0,
|
|
|
|
width: 100,
|
|
|
|
height: 100
|
2017-11-01 14:48:09 +00:00
|
|
|
}, (image) => {
|
2016-03-25 20:03:49 +00:00
|
|
|
assert.equal(image.isEmpty(), true)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('BrowserWindow.setSize(width, height)', () => {
|
|
|
|
it('sets the window size', (done) => {
|
|
|
|
const size = [300, 400]
|
|
|
|
w.once('resize', () => {
|
2016-09-08 20:46:55 +00:00
|
|
|
assertBoundsEqual(w.getSize(), size)
|
2016-03-25 20:03:49 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
w.setSize(size[0], size[1])
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('BrowserWindow.setMinimum/MaximumSize(width, height)', () => {
|
|
|
|
it('sets the maximum and minimum size of the window', () => {
|
2016-07-12 17:19:34 +00:00
|
|
|
assert.deepEqual(w.getMinimumSize(), [0, 0])
|
|
|
|
assert.deepEqual(w.getMaximumSize(), [0, 0])
|
|
|
|
|
|
|
|
w.setMinimumSize(100, 100)
|
2016-09-08 20:46:55 +00:00
|
|
|
assertBoundsEqual(w.getMinimumSize(), [100, 100])
|
|
|
|
assertBoundsEqual(w.getMaximumSize(), [0, 0])
|
2016-07-12 17:19:34 +00:00
|
|
|
|
|
|
|
w.setMaximumSize(900, 600)
|
2016-09-08 20:46:55 +00:00
|
|
|
assertBoundsEqual(w.getMinimumSize(), [100, 100])
|
|
|
|
assertBoundsEqual(w.getMaximumSize(), [900, 600])
|
2016-07-12 17:19:34 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('BrowserWindow.setAspectRatio(ratio)', () => {
|
|
|
|
it('resets the behaviour when passing in 0', (done) => {
|
|
|
|
const size = [300, 400]
|
2016-06-29 16:37:10 +00:00
|
|
|
w.setAspectRatio(1 / 2)
|
2016-05-29 12:47:42 +00:00
|
|
|
w.setAspectRatio(0)
|
2017-11-01 14:48:09 +00:00
|
|
|
w.once('resize', () => {
|
2016-09-08 20:46:55 +00:00
|
|
|
assertBoundsEqual(w.getSize(), size)
|
2016-05-29 12:47:42 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
w.setSize(size[0], size[1])
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('BrowserWindow.setPosition(x, y)', () => {
|
|
|
|
it('sets the window position', (done) => {
|
|
|
|
const pos = [10, 10]
|
|
|
|
w.once('move', () => {
|
|
|
|
const newPos = w.getPosition()
|
2016-03-25 20:03:49 +00:00
|
|
|
assert.equal(newPos[0], pos[0])
|
|
|
|
assert.equal(newPos[1], pos[1])
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
w.setPosition(pos[0], pos[1])
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('BrowserWindow.setContentSize(width, height)', () => {
|
|
|
|
it('sets the content size', () => {
|
|
|
|
const size = [400, 400]
|
2016-03-25 20:03:49 +00:00
|
|
|
w.setContentSize(size[0], size[1])
|
|
|
|
var after = w.getContentSize()
|
|
|
|
assert.equal(after[0], size[0])
|
|
|
|
assert.equal(after[1], size[1])
|
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
it('works for a frameless window', () => {
|
2016-03-25 20:03:49 +00:00
|
|
|
w.destroy()
|
2016-01-12 02:40:23 +00:00
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
frame: false,
|
2016-08-04 19:54:45 +00:00
|
|
|
width: 400,
|
|
|
|
height: 400
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
const size = [400, 400]
|
2016-03-25 20:03:49 +00:00
|
|
|
w.setContentSize(size[0], size[1])
|
2017-11-01 14:48:09 +00:00
|
|
|
const after = w.getContentSize()
|
2016-03-25 20:03:49 +00:00
|
|
|
assert.equal(after[0], size[0])
|
|
|
|
assert.equal(after[1], size[1])
|
2016-08-04 19:34:09 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('BrowserWindow.setContentBounds(bounds)', () => {
|
|
|
|
it('sets the content size and position', (done) => {
|
|
|
|
const bounds = {x: 10, y: 10, width: 250, height: 250}
|
|
|
|
w.once('resize', () => {
|
2016-09-08 20:46:55 +00:00
|
|
|
assertBoundsEqual(w.getContentBounds(), bounds)
|
2016-08-04 19:54:45 +00:00
|
|
|
done()
|
|
|
|
})
|
2016-08-04 19:34:09 +00:00
|
|
|
w.setContentBounds(bounds)
|
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
it('works for a frameless window', (done) => {
|
2016-08-04 19:34:09 +00:00
|
|
|
w.destroy()
|
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
frame: false,
|
|
|
|
width: 300,
|
|
|
|
height: 300
|
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
const bounds = {x: 10, y: 10, width: 250, height: 250}
|
|
|
|
w.once('resize', () => {
|
2016-08-04 19:54:45 +00:00
|
|
|
assert.deepEqual(w.getContentBounds(), bounds)
|
|
|
|
done()
|
|
|
|
})
|
2016-08-04 19:34:09 +00:00
|
|
|
w.setContentBounds(bounds)
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('BrowserWindow.setProgressBar(progress)', () => {
|
|
|
|
it('sets the progress', () => {
|
|
|
|
assert.doesNotThrow(() => {
|
2016-06-16 21:57:07 +00:00
|
|
|
if (process.platform === 'darwin') {
|
|
|
|
app.dock.setIcon(path.join(fixtures, 'assets', 'logo.png'))
|
|
|
|
}
|
2016-06-29 16:37:10 +00:00
|
|
|
w.setProgressBar(0.5)
|
2016-06-16 22:01:37 +00:00
|
|
|
|
|
|
|
if (process.platform === 'darwin') {
|
|
|
|
app.dock.setIcon(null)
|
|
|
|
}
|
|
|
|
w.setProgressBar(-1)
|
2016-06-16 21:57:07 +00:00
|
|
|
})
|
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
it('sets the progress using "paused" mode', () => {
|
|
|
|
assert.doesNotThrow(() => {
|
2016-08-08 22:44:48 +00:00
|
|
|
w.setProgressBar(0.5, {mode: 'paused'})
|
|
|
|
})
|
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
it('sets the progress using "error" mode', () => {
|
|
|
|
assert.doesNotThrow(() => {
|
2016-08-08 22:44:48 +00:00
|
|
|
w.setProgressBar(0.5, {mode: 'error'})
|
|
|
|
})
|
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
it('sets the progress using "normal" mode', () => {
|
|
|
|
assert.doesNotThrow(() => {
|
2016-08-08 22:44:48 +00:00
|
|
|
w.setProgressBar(0.5, {mode: 'normal'})
|
|
|
|
})
|
|
|
|
})
|
2016-06-16 21:57:07 +00:00
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('BrowserWindow.setAlwaysOnTop(flag, level)', () => {
|
|
|
|
it('sets the window as always on top', () => {
|
2016-09-28 16:23:52 +00:00
|
|
|
assert.equal(w.isAlwaysOnTop(), false)
|
2016-12-06 21:48:40 +00:00
|
|
|
w.setAlwaysOnTop(true, 'screen-saver')
|
2016-09-28 16:23:52 +00:00
|
|
|
assert.equal(w.isAlwaysOnTop(), true)
|
|
|
|
w.setAlwaysOnTop(false)
|
|
|
|
assert.equal(w.isAlwaysOnTop(), false)
|
|
|
|
w.setAlwaysOnTop(true)
|
|
|
|
assert.equal(w.isAlwaysOnTop(), true)
|
|
|
|
})
|
2017-11-15 21:05:46 +00:00
|
|
|
it('raises an error when relativeLevel is out of bounds', function () {
|
|
|
|
if (process.platform !== 'darwin') {
|
|
|
|
// FIXME(alexeykuzmin): Skip the test instead of marking it as passed.
|
|
|
|
// afterEach hook won't be run if a test is skipped dynamically.
|
|
|
|
// If afterEach isn't run current window won't be destroyed
|
|
|
|
// and the next test will fail on assertion in `closeWindow()`.
|
|
|
|
// this.skip()
|
|
|
|
return
|
|
|
|
}
|
2017-01-26 03:39:57 +00:00
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
assert.throws(() => {
|
2017-01-27 03:12:10 +00:00
|
|
|
w.setAlwaysOnTop(true, '', -2147483644)
|
2017-01-26 03:39:57 +00:00
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
assert.throws(() => {
|
2017-01-26 03:39:57 +00:00
|
|
|
w.setAlwaysOnTop(true, '', 2147483632)
|
|
|
|
})
|
|
|
|
})
|
2016-09-28 16:23:52 +00:00
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('BrowserWindow.alwaysOnTop() resets level on minimize', () => {
|
2017-11-15 21:05:46 +00:00
|
|
|
before(function () {
|
|
|
|
if (process.platform !== 'darwin') {
|
|
|
|
this.skip()
|
|
|
|
}
|
|
|
|
})
|
2017-06-07 16:28:50 +00:00
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
it('resets the windows level on minimize', () => {
|
2017-06-07 16:28:50 +00:00
|
|
|
assert.equal(w.isAlwaysOnTop(), false)
|
|
|
|
w.setAlwaysOnTop(true, 'screen-saver')
|
|
|
|
assert.equal(w.isAlwaysOnTop(), true)
|
|
|
|
w.minimize()
|
|
|
|
assert.equal(w.isAlwaysOnTop(), false)
|
|
|
|
w.restore()
|
|
|
|
assert.equal(w.isAlwaysOnTop(), true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2016-11-28 19:38:40 +00:00
|
|
|
describe('BrowserWindow.setAutoHideCursor(autoHide)', () => {
|
2017-11-15 21:05:46 +00:00
|
|
|
describe('on macOS', () => {
|
|
|
|
before(function () {
|
|
|
|
if (process.platform !== 'darwin') {
|
|
|
|
this.skip()
|
|
|
|
}
|
2016-11-28 19:38:40 +00:00
|
|
|
})
|
2017-11-15 21:05:46 +00:00
|
|
|
|
|
|
|
it('allows changing cursor auto-hiding', () => {
|
|
|
|
assert.doesNotThrow(() => {
|
|
|
|
w.setAutoHideCursor(false)
|
|
|
|
w.setAutoHideCursor(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('on non-macOS platforms', () => {
|
|
|
|
before(function () {
|
|
|
|
if (process.platform === 'darwin') {
|
|
|
|
this.skip()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
it('is not available', () => {
|
|
|
|
assert.ok(!w.setAutoHideCursor)
|
2016-11-28 19:38:40 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-08-21 04:46:10 +00:00
|
|
|
describe('BrowserWindow.selectPreviousTab()', () => {
|
2017-11-15 21:05:46 +00:00
|
|
|
before(function () {
|
|
|
|
if (process.platform !== 'darwin') {
|
|
|
|
this.skip()
|
|
|
|
}
|
|
|
|
})
|
2017-08-21 04:46:10 +00:00
|
|
|
|
2017-11-15 21:05:46 +00:00
|
|
|
it('does not throw', () => {
|
2017-08-21 04:46:10 +00:00
|
|
|
assert.doesNotThrow(() => {
|
|
|
|
w.selectPreviousTab()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('BrowserWindow.selectNextTab()', () => {
|
2017-11-15 21:05:46 +00:00
|
|
|
before(function () {
|
|
|
|
if (process.platform !== 'darwin') {
|
|
|
|
this.skip()
|
|
|
|
}
|
|
|
|
})
|
2017-08-21 04:46:10 +00:00
|
|
|
|
2017-11-15 21:05:46 +00:00
|
|
|
it('does not throw', () => {
|
2017-08-21 04:46:10 +00:00
|
|
|
assert.doesNotThrow(() => {
|
|
|
|
w.selectNextTab()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('BrowserWindow.mergeAllWindows()', () => {
|
2017-11-15 21:05:46 +00:00
|
|
|
before(function () {
|
|
|
|
if (process.platform !== 'darwin') {
|
|
|
|
this.skip()
|
|
|
|
}
|
|
|
|
})
|
2017-08-21 04:46:10 +00:00
|
|
|
|
2017-11-15 21:05:46 +00:00
|
|
|
it('does not throw', () => {
|
2017-08-21 04:46:10 +00:00
|
|
|
assert.doesNotThrow(() => {
|
|
|
|
w.mergeAllWindows()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('BrowserWindow.moveTabToNewWindow()', () => {
|
2017-11-15 21:05:46 +00:00
|
|
|
before(function () {
|
|
|
|
if (process.platform !== 'darwin') {
|
|
|
|
this.skip()
|
|
|
|
}
|
|
|
|
})
|
2017-08-21 04:46:10 +00:00
|
|
|
|
2017-11-15 21:05:46 +00:00
|
|
|
it('does not throw', () => {
|
2017-08-21 04:46:10 +00:00
|
|
|
assert.doesNotThrow(() => {
|
|
|
|
w.moveTabToNewWindow()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('BrowserWindow.toggleTabBar()', () => {
|
2017-11-15 21:05:46 +00:00
|
|
|
before(function () {
|
|
|
|
if (process.platform !== 'darwin') {
|
|
|
|
this.skip()
|
|
|
|
}
|
|
|
|
})
|
2017-08-21 04:46:10 +00:00
|
|
|
|
2017-11-15 21:05:46 +00:00
|
|
|
it('does not throw', () => {
|
2017-08-21 04:46:10 +00:00
|
|
|
assert.doesNotThrow(() => {
|
|
|
|
w.toggleTabBar()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-15 21:05:46 +00:00
|
|
|
// FIXME(alexeykuzmin): Fails on Mac.
|
|
|
|
xdescribe('BrowserWindow.addTabbedWindow()', () => {
|
|
|
|
before(function () {
|
|
|
|
if (process.platform !== 'darwin') {
|
|
|
|
this.skip()
|
|
|
|
}
|
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
|
2017-11-15 21:05:46 +00:00
|
|
|
it('does not throw', (done) => {
|
2017-10-04 15:35:41 +00:00
|
|
|
const tabbedWindow = new BrowserWindow({})
|
2017-09-13 19:15:14 +00:00
|
|
|
assert.doesNotThrow(() => {
|
2017-10-04 15:35:41 +00:00
|
|
|
w.addTabbedWindow(tabbedWindow)
|
2017-09-13 19:15:14 +00:00
|
|
|
})
|
2017-10-04 15:35:41 +00:00
|
|
|
closeWindow(tabbedWindow).then(done)
|
2017-09-13 19:15:14 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('BrowserWindow.setVibrancy(type)', () => {
|
|
|
|
it('allows setting, changing, and removing the vibrancy', () => {
|
|
|
|
assert.doesNotThrow(() => {
|
2016-11-10 19:36:21 +00:00
|
|
|
w.setVibrancy('light')
|
|
|
|
w.setVibrancy('dark')
|
|
|
|
w.setVibrancy(null)
|
|
|
|
w.setVibrancy('ultra-dark')
|
|
|
|
w.setVibrancy('')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('BrowserWindow.setAppDetails(options)', () => {
|
2017-11-15 21:05:46 +00:00
|
|
|
before(function () {
|
|
|
|
if (process.platform !== 'win32') {
|
|
|
|
this.skip()
|
|
|
|
}
|
|
|
|
})
|
2016-11-28 22:30:43 +00:00
|
|
|
|
2017-11-15 21:05:46 +00:00
|
|
|
it('supports setting the app details', () => {
|
2016-11-18 20:28:24 +00:00
|
|
|
const iconPath = path.join(fixtures, 'assets', 'icon.ico')
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
assert.doesNotThrow(() => {
|
2016-11-18 20:28:24 +00:00
|
|
|
w.setAppDetails({appId: 'my.app.id'})
|
|
|
|
w.setAppDetails({appIconPath: iconPath, appIconIndex: 0})
|
|
|
|
w.setAppDetails({appIconPath: iconPath})
|
|
|
|
w.setAppDetails({relaunchCommand: 'my-app.exe arg1 arg2', relaunchDisplayName: 'My app name'})
|
|
|
|
w.setAppDetails({relaunchCommand: 'my-app.exe arg1 arg2'})
|
|
|
|
w.setAppDetails({relaunchDisplayName: 'My app name'})
|
|
|
|
w.setAppDetails({
|
|
|
|
appId: 'my.app.id',
|
|
|
|
appIconPath: iconPath,
|
|
|
|
appIconIndex: 0,
|
|
|
|
relaunchCommand: 'my-app.exe arg1 arg2',
|
|
|
|
relaunchDisplayName: 'My app name'
|
|
|
|
})
|
|
|
|
w.setAppDetails({})
|
|
|
|
})
|
2016-11-28 22:30:43 +00:00
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
assert.throws(() => {
|
2016-11-18 20:28:24 +00:00
|
|
|
w.setAppDetails()
|
2016-11-28 22:30:43 +00:00
|
|
|
}, /Insufficient number of arguments\./)
|
2016-11-18 20:28:24 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('BrowserWindow.fromId(id)', () => {
|
|
|
|
it('returns the window with id', () => {
|
2016-03-25 20:03:49 +00:00
|
|
|
assert.equal(w.id, BrowserWindow.fromId(w.id).id)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('BrowserWindow.fromWebContents(webContents)', () => {
|
2016-12-06 00:12:15 +00:00
|
|
|
let contents = null
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
beforeEach(() => { contents = webContents.create({}) })
|
2016-12-06 00:12:15 +00:00
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
afterEach(() => { contents.destroy() })
|
2016-12-06 00:12:15 +00:00
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
it('returns the window with the webContents', () => {
|
2016-12-06 00:12:15 +00:00
|
|
|
assert.equal(BrowserWindow.fromWebContents(w.webContents).id, w.id)
|
|
|
|
assert.equal(BrowserWindow.fromWebContents(contents), undefined)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('BrowserWindow.fromDevToolsWebContents(webContents)', () => {
|
2016-12-06 00:16:17 +00:00
|
|
|
let contents = null
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
beforeEach(() => { contents = webContents.create({}) })
|
2016-12-06 00:16:17 +00:00
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
afterEach(() => { contents.destroy() })
|
2016-12-06 00:16:17 +00:00
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
it('returns the window with the webContents', (done) => {
|
2016-12-06 00:16:17 +00:00
|
|
|
w.webContents.once('devtools-opened', () => {
|
|
|
|
assert.equal(BrowserWindow.fromDevToolsWebContents(w.devToolsWebContents).id, w.id)
|
|
|
|
assert.equal(BrowserWindow.fromDevToolsWebContents(w.webContents), undefined)
|
|
|
|
assert.equal(BrowserWindow.fromDevToolsWebContents(contents), undefined)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
w.webContents.openDevTools()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-22 22:37:46 +00:00
|
|
|
describe('BrowserWindow.fromBrowserView(browserView)', () => {
|
|
|
|
let bv = null
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
bv = new BrowserView()
|
|
|
|
w.setBrowserView(bv)
|
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
w.setBrowserView(null)
|
|
|
|
bv.destroy()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('returns the window with the browserView', () => {
|
|
|
|
assert.equal(BrowserWindow.fromBrowserView(bv).id, w.id)
|
|
|
|
})
|
2017-11-22 22:38:08 +00:00
|
|
|
|
|
|
|
it('returns undefined if not attached', () => {
|
|
|
|
w.setBrowserView(null)
|
|
|
|
assert.equal(BrowserWindow.fromBrowserView(bv), undefined)
|
2017-11-22 22:37:46 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('BrowserWindow.setOpacity(opacity)', () => {
|
|
|
|
it('make window with initial opacity', () => {
|
2017-09-29 02:26:02 +00:00
|
|
|
w.destroy()
|
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
width: 400,
|
|
|
|
height: 400,
|
|
|
|
opacity: 0.5
|
|
|
|
})
|
2017-10-02 15:08:10 +00:00
|
|
|
assert.equal(w.getOpacity(), 0.5)
|
2017-09-29 02:26:02 +00:00
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
it('allows setting the opacity', () => {
|
|
|
|
assert.doesNotThrow(() => {
|
2017-09-29 02:26:02 +00:00
|
|
|
w.setOpacity(0.0)
|
2017-10-02 15:08:10 +00:00
|
|
|
assert.equal(w.getOpacity(), 0.0)
|
2017-09-29 02:26:02 +00:00
|
|
|
w.setOpacity(0.5)
|
2017-10-02 15:08:10 +00:00
|
|
|
assert.equal(w.getOpacity(), 0.5)
|
2017-09-29 02:26:02 +00:00
|
|
|
w.setOpacity(1.0)
|
2017-10-02 15:08:10 +00:00
|
|
|
assert.equal(w.getOpacity(), 1.0)
|
2017-09-29 02:26:02 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('"useContentSize" option', () => {
|
|
|
|
it('make window created with content size when used', () => {
|
2016-03-25 20:03:49 +00:00
|
|
|
w.destroy()
|
2016-01-12 02:40:23 +00:00
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
width: 400,
|
|
|
|
height: 400,
|
|
|
|
useContentSize: true
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
const contentSize = w.getContentSize()
|
2016-03-25 20:03:49 +00:00
|
|
|
assert.equal(contentSize[0], 400)
|
|
|
|
assert.equal(contentSize[1], 400)
|
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
it('make window created with window size when not used', () => {
|
|
|
|
const size = w.getSize()
|
2016-03-25 20:03:49 +00:00
|
|
|
assert.equal(size[0], 400)
|
|
|
|
assert.equal(size[1], 400)
|
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
it('works for a frameless window', () => {
|
2016-03-25 20:03:49 +00:00
|
|
|
w.destroy()
|
2016-01-12 02:40:23 +00:00
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
frame: false,
|
|
|
|
width: 400,
|
|
|
|
height: 400,
|
|
|
|
useContentSize: true
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
const contentSize = w.getContentSize()
|
2016-03-25 20:03:49 +00:00
|
|
|
assert.equal(contentSize[0], 400)
|
|
|
|
assert.equal(contentSize[1], 400)
|
2017-11-01 14:48:09 +00:00
|
|
|
const size = w.getSize()
|
2016-03-25 20:03:49 +00:00
|
|
|
assert.equal(size[0], 400)
|
|
|
|
assert.equal(size[1], 400)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('"titleBarStyle" option', () => {
|
2017-11-15 21:05:46 +00:00
|
|
|
before(function () {
|
|
|
|
if (process.platform !== 'darwin') {
|
|
|
|
this.skip()
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parseInt(os.release().split('.')[0]) < 14) {
|
|
|
|
this.skip()
|
|
|
|
}
|
|
|
|
})
|
2016-02-09 18:12:45 +00:00
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
it('creates browser window with hidden title bar', () => {
|
2016-03-25 20:03:49 +00:00
|
|
|
w.destroy()
|
2016-01-12 02:40:23 +00:00
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
width: 400,
|
|
|
|
height: 400,
|
|
|
|
titleBarStyle: 'hidden'
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
const contentSize = w.getContentSize()
|
2016-03-25 20:03:49 +00:00
|
|
|
assert.equal(contentSize[1], 400)
|
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
it('creates browser window with hidden inset title bar', () => {
|
2016-03-25 20:03:49 +00:00
|
|
|
w.destroy()
|
2016-01-12 02:40:23 +00:00
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
width: 400,
|
|
|
|
height: 400,
|
|
|
|
titleBarStyle: 'hidden-inset'
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
const contentSize = w.getContentSize()
|
2016-03-25 20:03:49 +00:00
|
|
|
assert.equal(contentSize[1], 400)
|
|
|
|
})
|
|
|
|
})
|
2016-02-09 18:12:45 +00:00
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('enableLargerThanScreen" option', () => {
|
2017-11-15 21:05:46 +00:00
|
|
|
before(function () {
|
|
|
|
if (process.platform === 'linux') {
|
|
|
|
this.skip()
|
|
|
|
}
|
|
|
|
})
|
2016-02-09 18:12:45 +00:00
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
beforeEach(() => {
|
2016-03-25 20:03:49 +00:00
|
|
|
w.destroy()
|
2016-02-09 18:12:45 +00:00
|
|
|
w = new BrowserWindow({
|
2016-01-12 02:40:23 +00:00
|
|
|
show: true,
|
|
|
|
width: 400,
|
|
|
|
height: 400,
|
|
|
|
enableLargerThanScreen: true
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
it('can move the window out of screen', () => {
|
2016-03-25 20:03:49 +00:00
|
|
|
w.setPosition(-10, -10)
|
2017-11-01 14:48:09 +00:00
|
|
|
const after = w.getPosition()
|
2016-03-25 20:03:49 +00:00
|
|
|
assert.equal(after[0], -10)
|
|
|
|
assert.equal(after[1], -10)
|
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
it('can set the window larger than screen', () => {
|
|
|
|
const size = screen.getPrimaryDisplay().size
|
2016-03-25 20:03:49 +00:00
|
|
|
size.width += 100
|
|
|
|
size.height += 100
|
|
|
|
w.setSize(size.width, size.height)
|
2016-09-08 20:46:55 +00:00
|
|
|
assertBoundsEqual(w.getSize(), [size.width, size.height])
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('"zoomToPageWidth" option', () => {
|
2017-11-15 21:05:46 +00:00
|
|
|
before(function () {
|
|
|
|
if (process.platform !== 'darwin') {
|
|
|
|
this.skip()
|
|
|
|
}
|
|
|
|
})
|
2016-10-27 18:54:27 +00:00
|
|
|
|
2017-11-15 21:05:46 +00:00
|
|
|
it('sets the window width to the page width when used', () => {
|
2016-10-27 18:54:27 +00:00
|
|
|
w.destroy()
|
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
width: 500,
|
|
|
|
height: 400,
|
|
|
|
zoomToPageWidth: true
|
|
|
|
})
|
|
|
|
w.maximize()
|
|
|
|
assert.equal(w.getSize()[0], 500)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('"tabbingIdentifier" option', () => {
|
|
|
|
it('can be set on a window', () => {
|
2017-03-30 20:46:58 +00:00
|
|
|
w.destroy()
|
|
|
|
w = new BrowserWindow({
|
|
|
|
tabbingIdentifier: 'group1'
|
|
|
|
})
|
|
|
|
w.destroy()
|
|
|
|
w = new BrowserWindow({
|
|
|
|
tabbingIdentifier: 'group2',
|
|
|
|
frame: false
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('"webPreferences" option', () => {
|
|
|
|
afterEach(() => { ipcMain.removeAllListeners('answer') })
|
2016-03-25 20:03:49 +00:00
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('"preload" option', () => {
|
|
|
|
it('loads the script before other scripts in window', (done) => {
|
|
|
|
const preload = path.join(fixtures, 'module', 'set-global.js')
|
|
|
|
ipcMain.once('answer', (event, test) => {
|
2016-03-25 20:03:49 +00:00
|
|
|
assert.equal(test, 'preload')
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
w.destroy()
|
2016-01-12 02:40:23 +00:00
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
preload: preload
|
|
|
|
}
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
w.loadURL(`file://${path.join(fixtures, 'api', 'preload.html')}`)
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
it('can successfully delete the Buffer global', (done) => {
|
|
|
|
const preload = path.join(fixtures, 'module', 'delete-buffer.js')
|
|
|
|
ipcMain.once('answer', (event, test) => {
|
2016-09-08 17:34:09 +00:00
|
|
|
assert.equal(test.toString(), 'buffer')
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
w.destroy()
|
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
preload: preload
|
|
|
|
}
|
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
w.loadURL(`file://${path.join(fixtures, 'api', 'preload.html')}`)
|
2016-09-08 17:34:09 +00:00
|
|
|
})
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('"node-integration" option', () => {
|
|
|
|
it('disables node integration when specified to false', (done) => {
|
|
|
|
const preload = path.join(fixtures, 'module', 'send-later.js')
|
|
|
|
ipcMain.once('answer', (event, typeofProcess, typeofBuffer) => {
|
2017-02-06 21:54:41 +00:00
|
|
|
assert.equal(typeofProcess, 'undefined')
|
|
|
|
assert.equal(typeofBuffer, 'undefined')
|
2016-03-25 20:03:49 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
w.destroy()
|
2016-01-12 02:40:23 +00:00
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
preload: preload,
|
|
|
|
nodeIntegration: false
|
|
|
|
}
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
w.loadURL(`file://${path.join(fixtures, 'api', 'blank.html')}`)
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
2016-09-26 20:40:45 +00:00
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('"sandbox" option', () => {
|
2016-09-26 20:40:45 +00:00
|
|
|
function waitForEvents (emitter, events, callback) {
|
|
|
|
let count = events.length
|
|
|
|
for (let event of events) {
|
|
|
|
emitter.once(event, () => {
|
|
|
|
if (!--count) callback()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const preload = path.join(fixtures, 'module', 'preload-sandbox.js')
|
|
|
|
|
2016-09-30 15:36:51 +00:00
|
|
|
// http protocol to simulate accessing another domain. This is required
|
2016-09-26 20:40:45 +00:00
|
|
|
// because the code paths for cross domain popups is different.
|
|
|
|
function crossDomainHandler (request, callback) {
|
|
|
|
callback({
|
|
|
|
mimeType: 'text/html',
|
|
|
|
data: `<html><body><h1>${request.url}</h1></body></html>`
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
before((done) => {
|
|
|
|
protocol.interceptStringProtocol('http', crossDomainHandler, () => {
|
2016-09-26 20:40:45 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
after((done) => {
|
|
|
|
protocol.uninterceptProtocol('http', () => {
|
2016-09-26 20:40:45 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
it('exposes ipcRenderer to preload script', (done) => {
|
2016-09-26 20:40:45 +00:00
|
|
|
ipcMain.once('answer', function (event, test) {
|
|
|
|
assert.equal(test, 'preload')
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
w.destroy()
|
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
sandbox: true,
|
|
|
|
preload: preload
|
|
|
|
}
|
|
|
|
})
|
|
|
|
w.loadURL('file://' + path.join(fixtures, 'api', 'preload.html'))
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
it('exposes "exit" event to preload script', (done) => {
|
2016-09-26 20:40:45 +00:00
|
|
|
w.destroy()
|
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
sandbox: true,
|
|
|
|
preload: preload
|
|
|
|
}
|
|
|
|
})
|
|
|
|
let htmlPath = path.join(fixtures, 'api', 'sandbox.html?exit-event')
|
|
|
|
const pageUrl = 'file://' + htmlPath
|
|
|
|
w.loadURL(pageUrl)
|
|
|
|
ipcMain.once('answer', function (event, url) {
|
|
|
|
let expectedUrl = pageUrl
|
|
|
|
if (process.platform === 'win32') {
|
|
|
|
expectedUrl = 'file:///' + htmlPath.replace(/\\/g, '/')
|
|
|
|
}
|
|
|
|
assert.equal(url, expectedUrl)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
it('should open windows in same domain with cross-scripting enabled', (done) => {
|
2016-09-26 20:40:45 +00:00
|
|
|
w.destroy()
|
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
sandbox: true,
|
|
|
|
preload: preload
|
|
|
|
}
|
|
|
|
})
|
2017-07-11 00:12:40 +00:00
|
|
|
ipcRenderer.send('set-web-preferences-on-next-new-window', w.webContents.id, 'preload', preload)
|
2016-09-26 20:40:45 +00:00
|
|
|
let htmlPath = path.join(fixtures, 'api', 'sandbox.html?window-open')
|
|
|
|
const pageUrl = 'file://' + htmlPath
|
|
|
|
w.loadURL(pageUrl)
|
|
|
|
w.webContents.once('new-window', (e, url, frameName, disposition, options) => {
|
|
|
|
let expectedUrl = pageUrl
|
|
|
|
if (process.platform === 'win32') {
|
|
|
|
expectedUrl = 'file:///' + htmlPath.replace(/\\/g, '/')
|
|
|
|
}
|
|
|
|
assert.equal(url, expectedUrl)
|
|
|
|
assert.equal(frameName, 'popup!')
|
|
|
|
assert.equal(options.width, 500)
|
|
|
|
assert.equal(options.height, 600)
|
|
|
|
ipcMain.once('answer', function (event, html) {
|
|
|
|
assert.equal(html, '<h1>scripting from opener</h1>')
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
it('should open windows in another domain with cross-scripting disabled', (done) => {
|
2016-09-26 20:40:45 +00:00
|
|
|
w.destroy()
|
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
sandbox: true,
|
|
|
|
preload: preload
|
|
|
|
}
|
|
|
|
})
|
2017-07-11 00:12:40 +00:00
|
|
|
ipcRenderer.send('set-web-preferences-on-next-new-window', w.webContents.id, 'preload', preload)
|
2016-09-26 20:40:45 +00:00
|
|
|
let htmlPath = path.join(fixtures, 'api', 'sandbox.html?window-open-external')
|
|
|
|
const pageUrl = 'file://' + htmlPath
|
2016-09-30 15:54:19 +00:00
|
|
|
let popupWindow
|
2016-09-26 20:40:45 +00:00
|
|
|
w.loadURL(pageUrl)
|
|
|
|
w.webContents.once('new-window', (e, url, frameName, disposition, options) => {
|
|
|
|
assert.equal(url, 'http://www.google.com/#q=electron')
|
|
|
|
assert.equal(options.width, 505)
|
|
|
|
assert.equal(options.height, 605)
|
|
|
|
ipcMain.once('child-loaded', function (event, openerIsNull, html) {
|
|
|
|
assert(openerIsNull)
|
|
|
|
assert.equal(html, '<h1>http://www.google.com/#q=electron</h1>')
|
|
|
|
ipcMain.once('answer', function (event, exceptionMessage) {
|
|
|
|
assert(/Blocked a frame with origin/.test(exceptionMessage))
|
2016-09-30 15:50:41 +00:00
|
|
|
|
2016-09-30 15:54:19 +00:00
|
|
|
// FIXME this popup window should be closed in sandbox.html
|
2016-11-29 19:06:22 +00:00
|
|
|
closeWindow(popupWindow, {assertSingleWindow: false}).then(() => {
|
2016-09-30 15:54:19 +00:00
|
|
|
popupWindow = null
|
|
|
|
done()
|
|
|
|
})
|
2016-09-26 20:40:45 +00:00
|
|
|
})
|
|
|
|
w.webContents.send('child-loaded')
|
|
|
|
})
|
|
|
|
})
|
2016-09-30 15:50:41 +00:00
|
|
|
|
|
|
|
app.once('browser-window-created', function (event, window) {
|
2016-09-30 15:54:19 +00:00
|
|
|
popupWindow = window
|
2016-09-30 15:50:41 +00:00
|
|
|
})
|
2016-09-26 20:40:45 +00:00
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
it('should inherit the sandbox setting in opened windows', (done) => {
|
2017-07-10 23:16:00 +00:00
|
|
|
w.destroy()
|
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
sandbox: true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
const preloadPath = path.join(fixtures, 'api', 'new-window-preload.js')
|
|
|
|
ipcRenderer.send('set-web-preferences-on-next-new-window', w.webContents.id, 'preload', preloadPath)
|
|
|
|
ipcMain.once('answer', (event, args) => {
|
2017-07-10 23:23:04 +00:00
|
|
|
assert.equal(args.includes('--enable-sandbox'), true)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
w.loadURL(`file://${path.join(fixtures, 'api', 'new-window.html')}`)
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
it('should open windows with the options configured via new-window event listeners', (done) => {
|
2017-07-10 23:23:04 +00:00
|
|
|
w.destroy()
|
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
sandbox: true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
const preloadPath = path.join(fixtures, 'api', 'new-window-preload.js')
|
|
|
|
ipcRenderer.send('set-web-preferences-on-next-new-window', w.webContents.id, 'preload', preloadPath)
|
|
|
|
ipcRenderer.send('set-web-preferences-on-next-new-window', w.webContents.id, 'foo', 'bar')
|
|
|
|
ipcMain.once('answer', (event, args, webPreferences) => {
|
|
|
|
assert.equal(webPreferences.foo, 'bar')
|
2017-07-10 23:16:00 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
w.loadURL(`file://${path.join(fixtures, 'api', 'new-window.html')}`)
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
it('should set ipc event sender correctly', (done) => {
|
2016-09-26 20:40:45 +00:00
|
|
|
w.destroy()
|
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
sandbox: true,
|
|
|
|
preload: preload
|
|
|
|
}
|
|
|
|
})
|
|
|
|
let htmlPath = path.join(fixtures, 'api', 'sandbox.html?verify-ipc-sender')
|
|
|
|
const pageUrl = 'file://' + htmlPath
|
|
|
|
w.loadURL(pageUrl)
|
|
|
|
w.webContents.once('new-window', (e, url, frameName, disposition, options) => {
|
|
|
|
let parentWc = w.webContents
|
|
|
|
let childWc = options.webContents
|
|
|
|
assert.notEqual(parentWc, childWc)
|
|
|
|
ipcMain.once('parent-ready', function (event) {
|
|
|
|
assert.equal(parentWc, event.sender)
|
|
|
|
parentWc.send('verified')
|
|
|
|
})
|
|
|
|
ipcMain.once('child-ready', function (event) {
|
|
|
|
assert.equal(childWc, event.sender)
|
|
|
|
childWc.send('verified')
|
|
|
|
})
|
|
|
|
waitForEvents(ipcMain, [
|
|
|
|
'parent-answer',
|
|
|
|
'child-answer'
|
|
|
|
], done)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('event handling', () => {
|
|
|
|
it('works for window events', (done) => {
|
2016-09-26 20:40:45 +00:00
|
|
|
waitForEvents(w, [
|
|
|
|
'page-title-updated'
|
|
|
|
], done)
|
|
|
|
w.loadURL('file://' + path.join(fixtures, 'api', 'sandbox.html?window-events'))
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
it('works for stop events', (done) => {
|
2016-09-26 20:40:45 +00:00
|
|
|
waitForEvents(w.webContents, [
|
|
|
|
'did-navigate',
|
|
|
|
'did-fail-load',
|
|
|
|
'did-stop-loading'
|
|
|
|
], done)
|
|
|
|
w.loadURL('file://' + path.join(fixtures, 'api', 'sandbox.html?webcontents-stop'))
|
2017-08-02 06:06:06 +00:00
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
it('works for web contents events', (done) => {
|
2016-09-26 20:40:45 +00:00
|
|
|
waitForEvents(w.webContents, [
|
|
|
|
'did-finish-load',
|
|
|
|
'did-frame-finish-load',
|
|
|
|
'did-navigate-in-page',
|
|
|
|
'will-navigate',
|
|
|
|
'did-start-loading',
|
|
|
|
'did-stop-loading',
|
|
|
|
'did-frame-finish-load',
|
|
|
|
'dom-ready'
|
|
|
|
], done)
|
|
|
|
w.loadURL('file://' + path.join(fixtures, 'api', 'sandbox.html?webcontents-events'))
|
|
|
|
})
|
|
|
|
})
|
2016-12-08 01:33:28 +00:00
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
it('can get printer list', (done) => {
|
2017-05-09 02:38:03 +00:00
|
|
|
w.destroy()
|
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
sandbox: true,
|
|
|
|
preload: preload
|
|
|
|
}
|
|
|
|
})
|
|
|
|
w.loadURL('data:text/html,%3Ch1%3EHello%2C%20World!%3C%2Fh1%3E')
|
2017-11-01 14:48:09 +00:00
|
|
|
w.webContents.once('did-finish-load', () => {
|
2017-05-18 17:14:55 +00:00
|
|
|
const printers = w.webContents.getPrinters()
|
|
|
|
assert.equal(Array.isArray(printers), true)
|
2017-05-09 02:38:03 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
it('can print to PDF', (done) => {
|
2016-12-08 01:33:28 +00:00
|
|
|
w.destroy()
|
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
sandbox: true,
|
|
|
|
preload: preload
|
|
|
|
}
|
|
|
|
})
|
|
|
|
w.loadURL('data:text/html,%3Ch1%3EHello%2C%20World!%3C%2Fh1%3E')
|
2017-11-01 14:48:09 +00:00
|
|
|
w.webContents.once('did-finish-load', () => {
|
2016-12-08 01:33:28 +00:00
|
|
|
w.webContents.printToPDF({}, function (error, data) {
|
|
|
|
assert.equal(error, null)
|
|
|
|
assert.equal(data instanceof Buffer, true)
|
|
|
|
assert.notEqual(data.length, 0)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
2016-09-26 20:40:45 +00:00
|
|
|
})
|
2017-01-10 23:34:29 +00:00
|
|
|
|
|
|
|
it('supports calling preventDefault on new-window events', (done) => {
|
|
|
|
w.destroy()
|
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
2017-01-12 18:28:02 +00:00
|
|
|
sandbox: true
|
2017-01-10 23:34:29 +00:00
|
|
|
}
|
|
|
|
})
|
2017-08-29 06:45:16 +00:00
|
|
|
const initialWebContents = webContents.getAllWebContents().map((i) => i.id)
|
2017-01-10 23:34:29 +00:00
|
|
|
ipcRenderer.send('prevent-next-new-window', w.webContents.id)
|
|
|
|
w.webContents.once('new-window', () => {
|
2017-05-02 18:14:52 +00:00
|
|
|
// We need to give it some time so the windows get properly disposed (at least on OSX).
|
|
|
|
setTimeout(() => {
|
|
|
|
const currentWebContents = webContents.getAllWebContents().map((i) => i.id)
|
|
|
|
assert.deepEqual(currentWebContents, initialWebContents)
|
|
|
|
done()
|
2017-05-02 18:23:13 +00:00
|
|
|
}, 100)
|
2017-01-10 23:34:29 +00:00
|
|
|
})
|
|
|
|
w.loadURL('file://' + path.join(fixtures, 'pages', 'window-open.html'))
|
|
|
|
})
|
2017-05-01 13:17:15 +00:00
|
|
|
|
|
|
|
it('releases memory after popup is closed', (done) => {
|
|
|
|
w.destroy()
|
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
preload: preload,
|
|
|
|
sandbox: true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
w.loadURL('file://' + path.join(fixtures, 'api', 'sandbox.html?allocate-memory'))
|
|
|
|
ipcMain.once('answer', function (event, {bytesBeforeOpen, bytesAfterOpen, bytesAfterClose}) {
|
|
|
|
const memoryIncreaseByOpen = bytesAfterOpen - bytesBeforeOpen
|
|
|
|
const memoryDecreaseByClose = bytesAfterOpen - bytesAfterClose
|
|
|
|
// decreased memory should be less than increased due to factors we
|
|
|
|
// can't control, but given the amount of memory allocated in the
|
|
|
|
// fixture, we can reasonably expect decrease to be at least 70% of
|
|
|
|
// increase
|
|
|
|
assert(memoryDecreaseByClose > memoryIncreaseByOpen * 0.7)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
2017-05-15 12:17:07 +00:00
|
|
|
|
|
|
|
// see #9387
|
|
|
|
it('properly manages remote object references after page reload', (done) => {
|
|
|
|
w.destroy()
|
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
preload: preload,
|
|
|
|
sandbox: true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
w.loadURL('file://' + path.join(fixtures, 'api', 'sandbox.html?reload-remote'))
|
|
|
|
|
|
|
|
ipcMain.on('get-remote-module-path', (event) => {
|
|
|
|
event.returnValue = path.join(fixtures, 'module', 'hello.js')
|
|
|
|
})
|
|
|
|
|
|
|
|
let reload = false
|
|
|
|
ipcMain.on('reloaded', (event) => {
|
|
|
|
event.returnValue = reload
|
|
|
|
reload = !reload
|
|
|
|
})
|
|
|
|
|
|
|
|
ipcMain.once('reload', (event) => {
|
|
|
|
event.sender.reload()
|
|
|
|
})
|
|
|
|
|
|
|
|
ipcMain.once('answer', (event, arg) => {
|
|
|
|
ipcMain.removeAllListeners('reloaded')
|
|
|
|
ipcMain.removeAllListeners('get-remote-module-path')
|
|
|
|
assert.equal(arg, 'hi')
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('properly manages remote object references after page reload in child window', (done) => {
|
|
|
|
w.destroy()
|
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
preload: preload,
|
|
|
|
sandbox: true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
w.loadURL('file://' + path.join(fixtures, 'api', 'sandbox.html?reload-remote-child'))
|
|
|
|
|
|
|
|
ipcMain.on('get-remote-module-path', (event) => {
|
|
|
|
event.returnValue = path.join(fixtures, 'module', 'hello-child.js')
|
|
|
|
})
|
|
|
|
|
|
|
|
let reload = false
|
|
|
|
ipcMain.on('reloaded', (event) => {
|
|
|
|
event.returnValue = reload
|
|
|
|
reload = !reload
|
|
|
|
})
|
|
|
|
|
|
|
|
ipcMain.once('reload', (event) => {
|
|
|
|
event.sender.reload()
|
|
|
|
})
|
|
|
|
|
|
|
|
ipcMain.once('answer', (event, arg) => {
|
|
|
|
ipcMain.removeAllListeners('reloaded')
|
|
|
|
ipcMain.removeAllListeners('get-remote-module-path')
|
|
|
|
assert.equal(arg, 'hi child window')
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
2016-09-26 20:40:45 +00:00
|
|
|
})
|
2017-03-19 09:11:20 +00:00
|
|
|
|
2017-03-19 11:10:25 +00:00
|
|
|
describe('nativeWindowOpen option', () => {
|
2017-04-04 10:05:13 +00:00
|
|
|
beforeEach(() => {
|
2017-03-19 09:11:20 +00:00
|
|
|
w.destroy()
|
|
|
|
w = new BrowserWindow({
|
2017-04-06 02:44:20 +00:00
|
|
|
show: false,
|
2017-03-19 09:11:20 +00:00
|
|
|
webPreferences: {
|
|
|
|
nativeWindowOpen: true
|
|
|
|
}
|
|
|
|
})
|
2017-04-04 10:05:13 +00:00
|
|
|
})
|
2017-04-18 15:11:23 +00:00
|
|
|
|
2017-04-04 10:05:13 +00:00
|
|
|
it('opens window of about:blank with cross-scripting enabled', (done) => {
|
2017-04-06 02:43:57 +00:00
|
|
|
ipcMain.once('answer', (event, content) => {
|
2017-04-04 10:05:13 +00:00
|
|
|
assert.equal(content, 'Hello')
|
|
|
|
done()
|
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
w.loadURL(`file://${path.join(fixtures, 'api', 'native-window-open-blank.html')}`)
|
2017-04-04 10:05:13 +00:00
|
|
|
})
|
|
|
|
it('opens window of same domain with cross-scripting enabled', (done) => {
|
2017-04-06 02:43:57 +00:00
|
|
|
ipcMain.once('answer', (event, content) => {
|
2017-04-04 10:05:13 +00:00
|
|
|
assert.equal(content, 'Hello')
|
|
|
|
done()
|
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
w.loadURL(`file://${path.join(fixtures, 'api', 'native-window-open-file.html')}`)
|
2017-03-19 09:11:20 +00:00
|
|
|
})
|
2017-05-24 20:07:50 +00:00
|
|
|
it('blocks accessing cross-origin frames', (done) => {
|
|
|
|
ipcMain.once('answer', (event, content) => {
|
|
|
|
assert.equal(content, 'Blocked a frame with origin "file://" from accessing a cross-origin frame.')
|
|
|
|
done()
|
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
w.loadURL(`file://${path.join(fixtures, 'api', 'native-window-open-cross-origin.html')}`)
|
2017-05-24 20:07:50 +00:00
|
|
|
})
|
2017-05-24 18:30:25 +00:00
|
|
|
it('opens window from <iframe> tags', (done) => {
|
|
|
|
ipcMain.once('answer', (event, content) => {
|
|
|
|
assert.equal(content, 'Hello')
|
|
|
|
done()
|
|
|
|
})
|
2017-11-01 14:56:53 +00:00
|
|
|
w.loadURL(`file://${path.join(fixtures, 'api', 'native-window-open-iframe.html')}`)
|
2017-05-24 18:30:25 +00:00
|
|
|
})
|
2017-05-25 23:40:15 +00:00
|
|
|
it('loads native addons correctly after reload', (done) => {
|
|
|
|
if (!nativeModulesEnabled) return done()
|
|
|
|
|
|
|
|
ipcMain.once('answer', (event, content) => {
|
|
|
|
assert.equal(content, 'function')
|
2017-04-06 02:43:57 +00:00
|
|
|
ipcMain.once('answer', (event, content) => {
|
|
|
|
assert.equal(content, 'function')
|
2017-05-25 23:40:15 +00:00
|
|
|
done()
|
2017-04-06 02:43:57 +00:00
|
|
|
})
|
2017-05-25 23:40:15 +00:00
|
|
|
w.reload()
|
2017-04-06 02:43:57 +00:00
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
w.loadURL(`file://${path.join(fixtures, 'api', 'native-window-open-native-addon.html')}`)
|
2017-05-25 23:40:15 +00:00
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
it('should inherit the nativeWindowOpen setting in opened windows', (done) => {
|
2017-07-10 23:25:50 +00:00
|
|
|
w.destroy()
|
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
nativeWindowOpen: true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
const preloadPath = path.join(fixtures, 'api', 'new-window-preload.js')
|
|
|
|
ipcRenderer.send('set-web-preferences-on-next-new-window', w.webContents.id, 'preload', preloadPath)
|
|
|
|
ipcMain.once('answer', (event, args) => {
|
|
|
|
assert.equal(args.includes('--native-window-open'), true)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
w.loadURL(`file://${path.join(fixtures, 'api', 'new-window.html')}`)
|
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
it('should open windows with the options configured via new-window event listeners', (done) => {
|
2017-07-10 23:25:50 +00:00
|
|
|
w.destroy()
|
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
nativeWindowOpen: true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
const preloadPath = path.join(fixtures, 'api', 'new-window-preload.js')
|
|
|
|
ipcRenderer.send('set-web-preferences-on-next-new-window', w.webContents.id, 'preload', preloadPath)
|
|
|
|
ipcRenderer.send('set-web-preferences-on-next-new-window', w.webContents.id, 'foo', 'bar')
|
|
|
|
ipcMain.once('answer', (event, args, webPreferences) => {
|
|
|
|
assert.equal(webPreferences.foo, 'bar')
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
w.loadURL(`file://${path.join(fixtures, 'api', 'new-window.html')}`)
|
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
it('retains the original web preferences when window.location is changed to a new origin', async () => {
|
2017-07-10 23:59:49 +00:00
|
|
|
await serveFileFromProtocol('foo', path.join(fixtures, 'api', 'window-open-location-change.html'))
|
|
|
|
await serveFileFromProtocol('bar', path.join(fixtures, 'api', 'window-open-location-final.html'))
|
|
|
|
|
|
|
|
w.destroy()
|
|
|
|
w = new BrowserWindow({
|
|
|
|
show: true,
|
|
|
|
webPreferences: {
|
|
|
|
nodeIntegration: false,
|
|
|
|
nativeWindowOpen: true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
ipcRenderer.send('set-web-preferences-on-next-new-window', w.webContents.id, 'preload', path.join(fixtures, 'api', 'window-open-preload.js'))
|
|
|
|
ipcMain.once('answer', (event, args, typeofProcess) => {
|
|
|
|
assert.equal(args.includes('--node-integration=false'), true)
|
|
|
|
assert.equal(args.includes('--native-window-open'), true)
|
|
|
|
assert.equal(typeofProcess, 'undefined')
|
|
|
|
resolve()
|
|
|
|
})
|
|
|
|
w.loadURL(`file://${path.join(fixtures, 'api', 'window-open-location-open.html')}`)
|
|
|
|
})
|
|
|
|
})
|
2017-03-19 09:11:20 +00:00
|
|
|
})
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
|
2017-04-30 10:09:52 +00:00
|
|
|
describe('nativeWindowOpen + contextIsolation options', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
w.destroy()
|
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
nativeWindowOpen: true,
|
|
|
|
contextIsolation: true,
|
|
|
|
preload: path.join(fixtures, 'api', 'native-window-open-isolated-preload.js')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('opens window with cross-scripting enabled from isolated context', (done) => {
|
|
|
|
ipcMain.once('answer', (event, content) => {
|
|
|
|
assert.equal(content, 'Hello')
|
|
|
|
done()
|
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
w.loadURL(`file://${path.join(fixtures, 'api', 'native-window-open-isolated.html')}`)
|
2016-09-26 20:40:45 +00:00
|
|
|
})
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('beforeunload handler', () => {
|
|
|
|
it('returning undefined would not prevent close', (done) => {
|
|
|
|
w.once('closed', () => { done() })
|
2016-05-23 04:28:16 +00:00
|
|
|
w.loadURL('file://' + path.join(fixtures, 'api', 'close-beforeunload-undefined.html'))
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
it('returning false would prevent close', (done) => {
|
|
|
|
w.once('onbeforeunload', () => { done() })
|
|
|
|
w.loadURL(`file://${path.join(fixtures, 'api', 'close-beforeunload-false.html')}`)
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
it('returning empty string would prevent close', (done) => {
|
|
|
|
w.once('onbeforeunload', () => { done() })
|
|
|
|
w.loadURL(`file://${path.join(fixtures, 'api', 'close-beforeunload-empty-string.html')}`)
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
it('emits for each close attempt', (done) => {
|
|
|
|
let beforeUnloadCount = 0
|
|
|
|
w.on('onbeforeunload', () => {
|
|
|
|
beforeUnloadCount += 1
|
2017-04-25 23:14:34 +00:00
|
|
|
if (beforeUnloadCount < 3) {
|
|
|
|
w.close()
|
|
|
|
} else if (beforeUnloadCount === 3) {
|
|
|
|
done()
|
|
|
|
}
|
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
w.webContents.once('did-finish-load', () => { w.close() })
|
|
|
|
w.loadURL(`file://${path.join(fixtures, 'api', 'beforeunload-false-prevent3.html')}`)
|
2017-04-25 23:14:34 +00:00
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
it('emits for each reload attempt', (done) => {
|
|
|
|
let beforeUnloadCount = 0
|
|
|
|
w.on('onbeforeunload', () => {
|
|
|
|
beforeUnloadCount += 1
|
2017-04-25 23:14:34 +00:00
|
|
|
if (beforeUnloadCount < 3) {
|
|
|
|
w.reload()
|
|
|
|
} else if (beforeUnloadCount === 3) {
|
|
|
|
done()
|
|
|
|
}
|
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
w.webContents.once('did-finish-load', () => {
|
|
|
|
w.webContents.once('did-finish-load', () => {
|
2017-04-25 23:14:34 +00:00
|
|
|
assert.fail('Reload was not prevented')
|
|
|
|
})
|
|
|
|
w.reload()
|
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
w.loadURL(`file://${path.join(fixtures, 'api', 'beforeunload-false-prevent3.html')}`)
|
2017-04-25 23:14:34 +00:00
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
it('emits for each navigation attempt', (done) => {
|
|
|
|
let beforeUnloadCount = 0
|
|
|
|
w.on('onbeforeunload', () => {
|
|
|
|
beforeUnloadCount += 1
|
2017-04-25 23:14:34 +00:00
|
|
|
if (beforeUnloadCount < 3) {
|
|
|
|
w.loadURL('about:blank')
|
|
|
|
} else if (beforeUnloadCount === 3) {
|
|
|
|
done()
|
|
|
|
}
|
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
w.webContents.once('did-finish-load', () => {
|
|
|
|
w.webContents.once('did-finish-load', () => {
|
2017-04-25 23:14:34 +00:00
|
|
|
assert.fail('Navigation was not prevented')
|
|
|
|
})
|
|
|
|
w.loadURL('about:blank')
|
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
w.loadURL(`file://${path.join(fixtures, 'api', 'beforeunload-false-prevent3.html')}`)
|
2017-04-25 23:14:34 +00:00
|
|
|
})
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
|
2017-11-02 00:22:27 +00:00
|
|
|
describe('document.visibilityState/hidden', () => {
|
2017-11-01 14:48:09 +00:00
|
|
|
beforeEach(() => { w.destroy() })
|
2017-05-22 18:10:10 +00:00
|
|
|
|
2017-06-06 23:22:07 +00:00
|
|
|
function onVisibilityChange (callback) {
|
2017-11-01 14:48:09 +00:00
|
|
|
ipcMain.on('pong', (event, visibilityState, hidden) => {
|
2017-06-06 23:22:07 +00:00
|
|
|
if (event.sender.id === w.webContents.id) {
|
|
|
|
callback(visibilityState, hidden)
|
2017-05-22 18:10:10 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-06-06 23:22:07 +00:00
|
|
|
function onNextVisibilityChange (callback) {
|
2017-11-01 14:48:09 +00:00
|
|
|
ipcMain.once('pong', (event, visibilityState, hidden) => {
|
2017-06-06 23:22:07 +00:00
|
|
|
if (event.sender.id === w.webContents.id) {
|
|
|
|
callback(visibilityState, hidden)
|
2017-05-22 18:10:10 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
afterEach(() => { ipcMain.removeAllListeners('pong') })
|
2017-06-06 23:22:07 +00:00
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
it('visibilityState is initially visible despite window being hidden', (done) => {
|
2017-05-22 18:10:10 +00:00
|
|
|
w = new BrowserWindow({ show: false, width: 100, height: 100 })
|
|
|
|
|
|
|
|
let readyToShow = false
|
2017-11-01 14:48:09 +00:00
|
|
|
w.once('ready-to-show', () => {
|
2017-05-22 18:10:10 +00:00
|
|
|
readyToShow = true
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
onNextVisibilityChange((visibilityState, hidden) => {
|
2017-06-06 23:22:07 +00:00
|
|
|
assert.equal(readyToShow, false)
|
2017-05-22 18:10:10 +00:00
|
|
|
assert.equal(visibilityState, 'visible')
|
|
|
|
assert.equal(hidden, false)
|
|
|
|
|
2017-06-06 23:22:07 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
w.loadURL(`file://${path.join(fixtures, 'pages', 'visibilitychange.html')}`)
|
2017-06-06 23:22:07 +00:00
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
it('visibilityState changes when window is hidden', (done) => {
|
2017-06-06 23:22:07 +00:00
|
|
|
w = new BrowserWindow({width: 100, height: 100})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
onNextVisibilityChange((visibilityState, hidden) => {
|
2017-06-06 23:22:07 +00:00
|
|
|
assert.equal(visibilityState, 'visible')
|
|
|
|
assert.equal(hidden, false)
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
onNextVisibilityChange((visibilityState, hidden) => {
|
2017-06-06 23:22:07 +00:00
|
|
|
assert.equal(visibilityState, 'hidden')
|
|
|
|
assert.equal(hidden, true)
|
|
|
|
done()
|
2017-05-22 18:10:10 +00:00
|
|
|
})
|
|
|
|
|
2017-06-06 23:22:07 +00:00
|
|
|
w.hide()
|
2017-05-22 18:10:10 +00:00
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
w.loadURL(`file://${path.join(fixtures, 'pages', 'visibilitychange.html')}`)
|
2017-05-22 18:10:10 +00:00
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
it('visibilityState changes when window is shown', (done) => {
|
2017-06-06 23:22:07 +00:00
|
|
|
w = new BrowserWindow({width: 100, height: 100})
|
2017-05-22 18:10:10 +00:00
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
onNextVisibilityChange((visibilityState, hidden) => {
|
|
|
|
onVisibilityChange((visibilityState, hidden) => {
|
2017-06-06 23:22:07 +00:00
|
|
|
if (!hidden) {
|
|
|
|
assert.equal(visibilityState, 'visible')
|
|
|
|
done()
|
|
|
|
}
|
2017-05-22 18:10:10 +00:00
|
|
|
})
|
2017-06-06 23:22:07 +00:00
|
|
|
|
2017-05-22 18:10:10 +00:00
|
|
|
w.hide()
|
2017-06-06 23:22:07 +00:00
|
|
|
w.show()
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
w.loadURL(`file://${path.join(fixtures, 'pages', 'visibilitychange.html')}`)
|
2017-06-06 23:22:07 +00:00
|
|
|
})
|
2017-11-15 21:05:46 +00:00
|
|
|
it('visibilityState changes when window is shown inactive', function (done) {
|
|
|
|
if (isCI && process.platform === 'win32') {
|
|
|
|
// FIXME(alexeykuzmin): Skip the test instead of marking it as passed.
|
|
|
|
// afterEach hook won't be run if a test is skipped dynamically.
|
|
|
|
// If afterEach isn't run current window won't be destroyed
|
|
|
|
// and the next test will fail on assertion in `closeWindow()`.
|
|
|
|
// this.skip()
|
|
|
|
return done()
|
|
|
|
}
|
2017-06-06 23:45:46 +00:00
|
|
|
|
|
|
|
w = new BrowserWindow({width: 100, height: 100})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
onNextVisibilityChange((visibilityState, hidden) => {
|
|
|
|
onVisibilityChange((visibilityState, hidden) => {
|
2017-06-06 23:45:46 +00:00
|
|
|
if (!hidden) {
|
|
|
|
assert.equal(visibilityState, 'visible')
|
|
|
|
done()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
w.hide()
|
|
|
|
w.showInactive()
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
w.loadURL(`file://${path.join(fixtures, 'pages', 'visibilitychange.html')}`)
|
2017-06-06 23:45:46 +00:00
|
|
|
})
|
2017-11-15 21:05:46 +00:00
|
|
|
it('visibilityState changes when window is minimized', function (done) {
|
|
|
|
if (isCI && process.platform === 'linux') {
|
|
|
|
// FIXME(alexeykuzmin): Skip the test instead of marking it as passed.
|
|
|
|
// afterEach hook won't be run if a test is skipped dynamically.
|
|
|
|
// If afterEach isn't run current window won't be destroyed
|
|
|
|
// and the next test will fail on assertion in `closeWindow()`.
|
|
|
|
// this.skip()
|
|
|
|
return done()
|
|
|
|
}
|
2017-06-06 23:58:28 +00:00
|
|
|
|
2017-06-06 23:22:07 +00:00
|
|
|
w = new BrowserWindow({width: 100, height: 100})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
onNextVisibilityChange((visibilityState, hidden) => {
|
2017-06-06 23:22:07 +00:00
|
|
|
assert.equal(visibilityState, 'visible')
|
|
|
|
assert.equal(hidden, false)
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
onNextVisibilityChange((visibilityState, hidden) => {
|
2017-06-06 23:22:07 +00:00
|
|
|
assert.equal(visibilityState, 'hidden')
|
|
|
|
assert.equal(hidden, true)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
|
|
|
|
w.minimize()
|
2017-05-22 18:10:10 +00:00
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
w.loadURL(`file://${path.join(fixtures, 'pages', 'visibilitychange.html')}`)
|
2017-05-22 18:10:10 +00:00
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
it('visibilityState remains visible if backgroundThrottling is disabled', (done) => {
|
2017-05-22 18:10:10 +00:00
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
width: 100,
|
|
|
|
height: 100,
|
|
|
|
webPreferences: {
|
|
|
|
backgroundThrottling: false
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
onNextVisibilityChange((visibilityState, hidden) => {
|
2017-06-06 23:22:07 +00:00
|
|
|
assert.equal(visibilityState, 'visible')
|
|
|
|
assert.equal(hidden, false)
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
onNextVisibilityChange((visibilityState, hidden) => {
|
2017-06-06 23:22:07 +00:00
|
|
|
done(new Error(`Unexpected visibility change event. visibilityState: ${visibilityState} hidden: ${hidden}`))
|
2017-05-22 18:10:10 +00:00
|
|
|
})
|
2017-06-06 23:22:07 +00:00
|
|
|
})
|
2017-05-22 18:10:10 +00:00
|
|
|
|
2017-06-06 23:22:07 +00:00
|
|
|
w.once('show', () => {
|
|
|
|
w.once('hide', () => {
|
|
|
|
w.once('show', () => {
|
|
|
|
done()
|
2017-05-22 18:10:10 +00:00
|
|
|
})
|
2017-06-06 23:22:07 +00:00
|
|
|
w.show()
|
2017-05-22 18:10:10 +00:00
|
|
|
})
|
2017-06-06 23:22:07 +00:00
|
|
|
w.hide()
|
2017-05-22 18:10:10 +00:00
|
|
|
})
|
2017-06-06 23:22:07 +00:00
|
|
|
w.show()
|
2017-05-22 18:10:10 +00:00
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
w.loadURL(`file://${path.join(fixtures, 'pages', 'visibilitychange.html')}`)
|
2017-05-22 18:10:10 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('new-window event', () => {
|
2017-11-15 21:05:46 +00:00
|
|
|
before(function () {
|
|
|
|
if (isCI && process.platform === 'darwin') {
|
|
|
|
this.skip()
|
|
|
|
}
|
|
|
|
})
|
2016-02-09 18:12:45 +00:00
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
it('emits when window.open is called', (done) => {
|
|
|
|
w.webContents.once('new-window', (e, url, frameName, disposition, options, additionalFeatures) => {
|
2016-10-07 01:13:04 +00:00
|
|
|
e.preventDefault()
|
|
|
|
assert.equal(url, 'http://host/')
|
|
|
|
assert.equal(frameName, 'host')
|
|
|
|
assert.equal(additionalFeatures[0], 'this-is-not-a-standard-feature')
|
|
|
|
done()
|
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
w.loadURL(`file://${fixtures}/pages/window-open.html`)
|
2016-10-07 01:13:04 +00:00
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
it('emits when window.open is called with no webPreferences', (done) => {
|
2016-10-07 01:13:04 +00:00
|
|
|
w.destroy()
|
|
|
|
w = new BrowserWindow({ show: false })
|
|
|
|
w.webContents.once('new-window', function (e, url, frameName, disposition, options, additionalFeatures) {
|
2016-03-25 20:03:49 +00:00
|
|
|
e.preventDefault()
|
|
|
|
assert.equal(url, 'http://host/')
|
|
|
|
assert.equal(frameName, 'host')
|
2016-10-05 11:45:15 +00:00
|
|
|
assert.equal(additionalFeatures[0], 'this-is-not-a-standard-feature')
|
2016-03-25 20:03:49 +00:00
|
|
|
done()
|
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
w.loadURL(`file://${fixtures}/pages/window-open.html`)
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
it('emits when link with target is called', (done) => {
|
|
|
|
w.webContents.once('new-window', (e, url, frameName) => {
|
2016-03-25 20:03:49 +00:00
|
|
|
e.preventDefault()
|
|
|
|
assert.equal(url, 'http://host/')
|
|
|
|
assert.equal(frameName, 'target')
|
|
|
|
done()
|
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
w.loadURL(`file://${fixtures}/pages/target-name.html`)
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('maximize event', () => {
|
|
|
|
if (isCI) return
|
2016-02-09 18:12:45 +00:00
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
it('emits when window is maximized', (done) => {
|
|
|
|
w.once('maximize', () => { done() })
|
2016-03-25 20:03:49 +00:00
|
|
|
w.show()
|
|
|
|
w.maximize()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('unmaximize event', () => {
|
|
|
|
if (isCI) return
|
2016-02-09 18:12:45 +00:00
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
it('emits when window is unmaximized', (done) => {
|
|
|
|
w.once('unmaximize', () => { done() })
|
2016-03-25 20:03:49 +00:00
|
|
|
w.show()
|
|
|
|
w.maximize()
|
|
|
|
w.unmaximize()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('minimize event', () => {
|
2017-11-01 14:56:53 +00:00
|
|
|
if (isCI) return
|
2016-02-09 18:12:45 +00:00
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
it('emits when window is minimized', (done) => {
|
|
|
|
w.once('minimize', () => { done() })
|
2016-03-25 20:03:49 +00:00
|
|
|
w.show()
|
|
|
|
w.minimize()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('sheet-begin event', () => {
|
2017-04-03 16:44:26 +00:00
|
|
|
let sheet = null
|
|
|
|
|
2017-11-15 21:05:46 +00:00
|
|
|
before(function () {
|
|
|
|
if (process.platform !== 'darwin') {
|
|
|
|
this.skip()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
afterEach(() => {
|
|
|
|
return closeWindow(sheet, {assertSingleWindow: false}).then(() => { sheet = null })
|
2017-04-03 16:44:26 +00:00
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
it('emits when window opens a sheet', (done) => {
|
2017-04-03 16:44:26 +00:00
|
|
|
w.show()
|
2017-11-01 14:48:09 +00:00
|
|
|
w.once('sheet-begin', () => {
|
2017-04-03 16:44:26 +00:00
|
|
|
sheet.close()
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
sheet = new BrowserWindow({
|
|
|
|
modal: true,
|
|
|
|
parent: w
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('sheet-end event', () => {
|
2017-04-03 16:44:26 +00:00
|
|
|
let sheet = null
|
|
|
|
|
2017-11-15 21:05:46 +00:00
|
|
|
before(function () {
|
|
|
|
if (process.platform !== 'darwin') {
|
|
|
|
this.skip()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
afterEach(() => {
|
|
|
|
return closeWindow(sheet, {assertSingleWindow: false}).then(() => { sheet = null })
|
2017-04-03 16:44:26 +00:00
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
it('emits when window has closed a sheet', (done) => {
|
2017-04-03 16:44:26 +00:00
|
|
|
w.show()
|
|
|
|
sheet = new BrowserWindow({
|
|
|
|
modal: true,
|
|
|
|
parent: w
|
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
w.once('sheet-end', () => { done() })
|
2017-04-03 16:44:26 +00:00
|
|
|
sheet.close()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('beginFrameSubscription method', () => {
|
2017-11-15 21:05:46 +00:00
|
|
|
before(function () {
|
|
|
|
// This test is too slow, only test it on CI.
|
|
|
|
if (!isCI) {
|
|
|
|
this.skip()
|
|
|
|
}
|
2016-06-25 16:23:40 +00:00
|
|
|
|
2017-11-15 21:05:46 +00:00
|
|
|
// FIXME These specs crash on Linux when run in a docker container
|
|
|
|
if (isCI && process.platform === 'linux') {
|
|
|
|
this.skip()
|
|
|
|
}
|
|
|
|
})
|
2017-07-21 20:28:02 +00:00
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
it('subscribes to frame updates', (done) => {
|
2016-06-26 02:53:58 +00:00
|
|
|
let called = false
|
2017-11-01 14:48:09 +00:00
|
|
|
w.loadURL(`file://${fixtures}/api/frame-subscriber.html`)
|
|
|
|
w.webContents.on('dom-ready', () => {
|
2016-06-25 16:23:40 +00:00
|
|
|
w.webContents.beginFrameSubscription(function (data) {
|
2016-06-26 02:53:58 +00:00
|
|
|
// This callback might be called twice.
|
|
|
|
if (called) return
|
|
|
|
called = true
|
|
|
|
|
2016-06-25 16:23:40 +00:00
|
|
|
assert.notEqual(data.length, 0)
|
|
|
|
w.webContents.endFrameSubscription()
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
it('subscribes to frame updates (only dirty rectangle)', (done) => {
|
2016-06-26 02:53:58 +00:00
|
|
|
let called = false
|
2017-11-01 14:48:09 +00:00
|
|
|
w.loadURL(`file://${fixtures}/api/frame-subscriber.html`)
|
|
|
|
w.webContents.on('dom-ready', () => {
|
|
|
|
w.webContents.beginFrameSubscription(true, (data) => {
|
2016-06-26 02:53:58 +00:00
|
|
|
// This callback might be called twice.
|
|
|
|
if (called) return
|
|
|
|
called = true
|
|
|
|
|
2016-06-25 16:23:40 +00:00
|
|
|
assert.notEqual(data.length, 0)
|
|
|
|
w.webContents.endFrameSubscription()
|
|
|
|
done()
|
|
|
|
})
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
it('throws error when subscriber is not well defined', (done) => {
|
|
|
|
w.loadURL(`file://${fixtures}'/api/frame-subscriber.html`)
|
2016-06-26 02:53:58 +00:00
|
|
|
try {
|
2016-06-25 16:23:40 +00:00
|
|
|
w.webContents.beginFrameSubscription(true, true)
|
2016-06-29 16:37:10 +00:00
|
|
|
} catch (e) {
|
2016-06-25 16:23:40 +00:00
|
|
|
done()
|
|
|
|
}
|
|
|
|
})
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('savePage method', () => {
|
2016-03-25 20:03:49 +00:00
|
|
|
const savePageDir = path.join(fixtures, 'save_page')
|
|
|
|
const savePageHtmlPath = path.join(savePageDir, 'save_page.html')
|
|
|
|
const savePageJsPath = path.join(savePageDir, 'save_page_files', 'test.js')
|
|
|
|
const savePageCssPath = path.join(savePageDir, 'save_page_files', 'test.css')
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
after(() => {
|
2016-01-13 09:12:47 +00:00
|
|
|
try {
|
2016-03-25 20:03:49 +00:00
|
|
|
fs.unlinkSync(savePageCssPath)
|
|
|
|
fs.unlinkSync(savePageJsPath)
|
|
|
|
fs.unlinkSync(savePageHtmlPath)
|
|
|
|
fs.rmdirSync(path.join(savePageDir, 'save_page_files'))
|
|
|
|
fs.rmdirSync(savePageDir)
|
2016-01-13 09:12:47 +00:00
|
|
|
} catch (e) {
|
2016-01-19 19:31:47 +00:00
|
|
|
// Ignore error
|
2016-01-13 09:12:47 +00:00
|
|
|
}
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
it('should save page to disk', (done) => {
|
|
|
|
w.webContents.on('did-finish-load', () => {
|
2016-03-25 20:03:49 +00:00
|
|
|
w.webContents.savePage(savePageHtmlPath, 'HTMLComplete', function (error) {
|
|
|
|
assert.equal(error, null)
|
|
|
|
assert(fs.existsSync(savePageHtmlPath))
|
|
|
|
assert(fs.existsSync(savePageJsPath))
|
|
|
|
assert(fs.existsSync(savePageCssPath))
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
w.loadURL('file://' + fixtures + '/pages/save_page/index.html')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('BrowserWindow options argument is optional', () => {
|
|
|
|
it('should create a window with default size (800x600)', () => {
|
2016-03-25 20:03:49 +00:00
|
|
|
w.destroy()
|
|
|
|
w = new BrowserWindow()
|
2017-11-01 14:48:09 +00:00
|
|
|
const size = w.getSize()
|
2016-03-25 20:03:49 +00:00
|
|
|
assert.equal(size[0], 800)
|
|
|
|
assert.equal(size[1], 600)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('window states', () => {
|
|
|
|
it('does not resize frameless windows when states change', () => {
|
2016-08-15 19:30:26 +00:00
|
|
|
w.destroy()
|
|
|
|
w = new BrowserWindow({
|
|
|
|
frame: false,
|
|
|
|
width: 300,
|
|
|
|
height: 200,
|
|
|
|
show: false
|
|
|
|
})
|
|
|
|
|
|
|
|
w.setMinimizable(false)
|
|
|
|
w.setMinimizable(true)
|
|
|
|
assert.deepEqual(w.getSize(), [300, 200])
|
|
|
|
|
|
|
|
w.setResizable(false)
|
|
|
|
w.setResizable(true)
|
|
|
|
assert.deepEqual(w.getSize(), [300, 200])
|
|
|
|
|
|
|
|
w.setMaximizable(false)
|
|
|
|
w.setMaximizable(true)
|
|
|
|
assert.deepEqual(w.getSize(), [300, 200])
|
|
|
|
|
|
|
|
w.setFullScreenable(false)
|
|
|
|
w.setFullScreenable(true)
|
|
|
|
assert.deepEqual(w.getSize(), [300, 200])
|
|
|
|
|
|
|
|
w.setClosable(false)
|
|
|
|
w.setClosable(true)
|
|
|
|
assert.deepEqual(w.getSize(), [300, 200])
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('resizable state', () => {
|
|
|
|
it('can be changed with resizable option', () => {
|
2016-03-25 20:03:49 +00:00
|
|
|
w.destroy()
|
|
|
|
w = new BrowserWindow({show: false, resizable: false})
|
|
|
|
assert.equal(w.isResizable(), false)
|
2016-08-05 00:10:34 +00:00
|
|
|
|
|
|
|
if (process.platform === 'darwin') {
|
|
|
|
assert.equal(w.isMaximizable(), true)
|
|
|
|
}
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
it('can be changed with setResizable method', () => {
|
2016-03-25 20:03:49 +00:00
|
|
|
assert.equal(w.isResizable(), true)
|
|
|
|
w.setResizable(false)
|
|
|
|
assert.equal(w.isResizable(), false)
|
|
|
|
w.setResizable(true)
|
|
|
|
assert.equal(w.isResizable(), true)
|
|
|
|
})
|
2017-01-09 18:00:52 +00:00
|
|
|
|
2017-08-16 14:18:50 +00:00
|
|
|
it('works for a frameless window', () => {
|
2017-01-09 18:00:52 +00:00
|
|
|
w.destroy()
|
|
|
|
w = new BrowserWindow({show: false, frame: false})
|
|
|
|
assert.equal(w.isResizable(), true)
|
|
|
|
|
|
|
|
if (process.platform === 'win32') {
|
|
|
|
w.destroy()
|
|
|
|
w = new BrowserWindow({show: false, thickFrame: false})
|
|
|
|
assert.equal(w.isResizable(), false)
|
|
|
|
}
|
|
|
|
})
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2016-04-20 05:05:09 +00:00
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('loading main frame state', () => {
|
|
|
|
it('is true when the main frame is loading', (done) => {
|
|
|
|
w.webContents.on('did-start-loading', () => {
|
2016-04-20 05:05:09 +00:00
|
|
|
assert.equal(w.webContents.isLoadingMainFrame(), true)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
w.webContents.loadURL(server.url)
|
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
it('is false when only a subframe is loading', (done) => {
|
|
|
|
w.webContents.once('did-finish-load', () => {
|
2016-04-20 05:05:09 +00:00
|
|
|
assert.equal(w.webContents.isLoadingMainFrame(), false)
|
2017-11-01 14:48:09 +00:00
|
|
|
w.webContents.on('did-start-loading', () => {
|
2016-04-20 05:05:09 +00:00
|
|
|
assert.equal(w.webContents.isLoadingMainFrame(), false)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
w.webContents.executeJavaScript(`
|
|
|
|
var iframe = document.createElement('iframe')
|
|
|
|
iframe.src = '${server.url}/page2'
|
|
|
|
document.body.appendChild(iframe)
|
|
|
|
`)
|
|
|
|
})
|
|
|
|
w.webContents.loadURL(server.url)
|
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
it('is true when navigating to pages from the same origin', (done) => {
|
|
|
|
w.webContents.once('did-finish-load', () => {
|
2016-04-20 05:05:09 +00:00
|
|
|
assert.equal(w.webContents.isLoadingMainFrame(), false)
|
2017-11-01 14:48:09 +00:00
|
|
|
w.webContents.on('did-start-loading', () => {
|
2016-04-20 05:05:09 +00:00
|
|
|
assert.equal(w.webContents.isLoadingMainFrame(), true)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
w.webContents.loadURL(`${server.url}/page2`)
|
|
|
|
})
|
|
|
|
w.webContents.loadURL(server.url)
|
|
|
|
})
|
|
|
|
})
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('window states (excluding Linux)', () => {
|
2017-11-15 21:05:46 +00:00
|
|
|
// FIXME(alexeykuzmin): Skip the tests instead of using the `return` here.
|
|
|
|
// Why it cannot be done now:
|
|
|
|
// - `.skip()` called in the 'before' hook doesn't affect
|
|
|
|
// nested `describe`s.
|
|
|
|
// - `.skip()` called in the 'beforeEach' hook prevents 'afterEach'
|
|
|
|
// hook from being called.
|
2016-01-23 11:35:30 +00:00
|
|
|
// Not implemented on Linux.
|
2017-11-15 21:05:46 +00:00
|
|
|
if (process.platform === 'linux') {
|
|
|
|
return
|
|
|
|
}
|
2016-03-25 20:03:49 +00:00
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('movable state', () => {
|
|
|
|
it('can be changed with movable option', () => {
|
2016-03-25 20:03:49 +00:00
|
|
|
w.destroy()
|
|
|
|
w = new BrowserWindow({show: false, movable: false})
|
|
|
|
assert.equal(w.isMovable(), false)
|
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
it('can be changed with setMovable method', () => {
|
2016-03-25 20:03:49 +00:00
|
|
|
assert.equal(w.isMovable(), true)
|
|
|
|
w.setMovable(false)
|
|
|
|
assert.equal(w.isMovable(), false)
|
|
|
|
w.setMovable(true)
|
|
|
|
assert.equal(w.isMovable(), true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('minimizable state', () => {
|
|
|
|
it('can be changed with minimizable option', () => {
|
2016-03-25 20:03:49 +00:00
|
|
|
w.destroy()
|
|
|
|
w = new BrowserWindow({show: false, minimizable: false})
|
|
|
|
assert.equal(w.isMinimizable(), false)
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
it('can be changed with setMinimizable method', () => {
|
2016-03-25 20:03:49 +00:00
|
|
|
assert.equal(w.isMinimizable(), true)
|
|
|
|
w.setMinimizable(false)
|
|
|
|
assert.equal(w.isMinimizable(), false)
|
|
|
|
w.setMinimizable(true)
|
|
|
|
assert.equal(w.isMinimizable(), true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('maximizable state', () => {
|
|
|
|
it('can be changed with maximizable option', () => {
|
2016-03-25 20:03:49 +00:00
|
|
|
w.destroy()
|
|
|
|
w = new BrowserWindow({show: false, maximizable: false})
|
|
|
|
assert.equal(w.isMaximizable(), false)
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
it('can be changed with setMaximizable method', () => {
|
2016-03-25 20:03:49 +00:00
|
|
|
assert.equal(w.isMaximizable(), true)
|
|
|
|
w.setMaximizable(false)
|
|
|
|
assert.equal(w.isMaximizable(), false)
|
|
|
|
w.setMaximizable(true)
|
|
|
|
assert.equal(w.isMaximizable(), true)
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
it('is not affected when changing other states', () => {
|
2016-03-25 20:03:49 +00:00
|
|
|
w.setMaximizable(false)
|
|
|
|
assert.equal(w.isMaximizable(), false)
|
|
|
|
w.setMinimizable(false)
|
|
|
|
assert.equal(w.isMaximizable(), false)
|
|
|
|
w.setClosable(false)
|
|
|
|
assert.equal(w.isMaximizable(), false)
|
2016-08-04 22:06:49 +00:00
|
|
|
|
|
|
|
w.setMaximizable(true)
|
|
|
|
assert.equal(w.isMaximizable(), true)
|
|
|
|
w.setClosable(true)
|
|
|
|
assert.equal(w.isMaximizable(), true)
|
|
|
|
w.setFullScreenable(false)
|
|
|
|
assert.equal(w.isMaximizable(), true)
|
2017-11-23 19:58:37 +00:00
|
|
|
})
|
2017-11-23 20:24:50 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('maximizable state (Windows only)', () => {
|
|
|
|
// Only implemented on windows.
|
|
|
|
if (process.platform !== 'win32') return
|
2017-11-23 19:58:37 +00:00
|
|
|
|
|
|
|
it('is set to false when resizable state is set to false', () => {
|
2016-08-04 22:06:49 +00:00
|
|
|
w.setResizable(false)
|
2017-11-23 19:58:37 +00:00
|
|
|
assert.equal(w.isMaximizable(), false)
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('fullscreenable state', () => {
|
2017-11-15 21:05:46 +00:00
|
|
|
before(function () {
|
|
|
|
// Only implemented on macOS.
|
|
|
|
if (process.platform !== 'darwin') {
|
|
|
|
this.skip()
|
|
|
|
}
|
|
|
|
})
|
2016-03-25 20:03:49 +00:00
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
it('can be changed with fullscreenable option', () => {
|
2016-03-25 20:03:49 +00:00
|
|
|
w.destroy()
|
|
|
|
w = new BrowserWindow({show: false, fullscreenable: false})
|
|
|
|
assert.equal(w.isFullScreenable(), false)
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
it('can be changed with setFullScreenable method', () => {
|
2016-03-25 20:03:49 +00:00
|
|
|
assert.equal(w.isFullScreenable(), true)
|
|
|
|
w.setFullScreenable(false)
|
|
|
|
assert.equal(w.isFullScreenable(), false)
|
|
|
|
w.setFullScreenable(true)
|
|
|
|
assert.equal(w.isFullScreenable(), true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('kiosk state', () => {
|
2017-11-15 21:05:46 +00:00
|
|
|
before(function () {
|
|
|
|
// Only implemented on macOS.
|
|
|
|
if (process.platform !== 'darwin') {
|
|
|
|
this.skip()
|
|
|
|
}
|
|
|
|
})
|
2017-01-13 23:05:04 +00:00
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
it('can be changed with setKiosk method', (done) => {
|
2017-01-13 23:05:04 +00:00
|
|
|
w.destroy()
|
|
|
|
w = new BrowserWindow()
|
|
|
|
w.setKiosk(true)
|
|
|
|
assert.equal(w.isKiosk(), true)
|
2017-04-18 23:42:01 +00:00
|
|
|
|
|
|
|
w.once('enter-full-screen', () => {
|
|
|
|
w.setKiosk(false)
|
|
|
|
assert.equal(w.isKiosk(), false)
|
|
|
|
})
|
|
|
|
w.once('leave-full-screen', () => {
|
|
|
|
done()
|
|
|
|
})
|
2017-01-13 23:05:04 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('fullscreen state with resizable set', () => {
|
2017-11-15 21:05:46 +00:00
|
|
|
before(function () {
|
|
|
|
// Only implemented on macOS.
|
|
|
|
if (process.platform !== 'darwin') {
|
|
|
|
this.skip()
|
|
|
|
}
|
|
|
|
})
|
2017-06-20 20:11:28 +00:00
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
it('resizable flag should be set to true and restored', (done) => {
|
2017-06-20 20:11:28 +00:00
|
|
|
w.destroy()
|
|
|
|
w = new BrowserWindow({ resizable: false })
|
|
|
|
w.once('enter-full-screen', () => {
|
|
|
|
assert.equal(w.isResizable(), true)
|
|
|
|
w.setFullScreen(false)
|
|
|
|
})
|
|
|
|
w.once('leave-full-screen', () => {
|
|
|
|
assert.equal(w.isResizable(), false)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
w.setFullScreen(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('fullscreen state', () => {
|
2017-11-15 21:05:46 +00:00
|
|
|
before(function () {
|
|
|
|
// Only implemented on macOS.
|
|
|
|
if (process.platform !== 'darwin') {
|
|
|
|
this.skip()
|
|
|
|
}
|
|
|
|
})
|
2017-01-13 23:05:04 +00:00
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
it('can be changed with setFullScreen method', (done) => {
|
2017-01-13 23:05:04 +00:00
|
|
|
w.destroy()
|
|
|
|
w = new BrowserWindow()
|
2017-01-24 16:29:50 +00:00
|
|
|
w.once('enter-full-screen', () => {
|
2017-01-24 19:22:25 +00:00
|
|
|
assert.equal(w.isFullScreen(), true)
|
2017-01-24 16:29:50 +00:00
|
|
|
w.setFullScreen(false)
|
2017-01-24 19:22:25 +00:00
|
|
|
})
|
|
|
|
w.once('leave-full-screen', () => {
|
2017-01-24 16:29:50 +00:00
|
|
|
assert.equal(w.isFullScreen(), false)
|
|
|
|
done()
|
|
|
|
})
|
2017-01-13 23:05:04 +00:00
|
|
|
w.setFullScreen(true)
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
it('should not be changed by setKiosk method', (done) => {
|
2017-01-24 19:22:25 +00:00
|
|
|
w.destroy()
|
|
|
|
w = new BrowserWindow()
|
|
|
|
w.once('enter-full-screen', () => {
|
|
|
|
assert.equal(w.isFullScreen(), true)
|
|
|
|
w.setKiosk(true)
|
|
|
|
w.setKiosk(false)
|
|
|
|
assert.equal(w.isFullScreen(), true)
|
|
|
|
w.setFullScreen(false)
|
|
|
|
})
|
|
|
|
w.once('leave-full-screen', () => {
|
|
|
|
assert.equal(w.isFullScreen(), false)
|
|
|
|
done()
|
|
|
|
})
|
2017-01-13 23:05:04 +00:00
|
|
|
w.setFullScreen(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('closable state', () => {
|
|
|
|
it('can be changed with closable option', () => {
|
2016-03-25 20:03:49 +00:00
|
|
|
w.destroy()
|
|
|
|
w = new BrowserWindow({show: false, closable: false})
|
|
|
|
assert.equal(w.isClosable(), false)
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
it('can be changed with setClosable method', () => {
|
2016-03-25 20:03:49 +00:00
|
|
|
assert.equal(w.isClosable(), true)
|
|
|
|
w.setClosable(false)
|
|
|
|
assert.equal(w.isClosable(), false)
|
|
|
|
w.setClosable(true)
|
|
|
|
assert.equal(w.isClosable(), true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('hasShadow state', () => {
|
2016-01-23 12:03:56 +00:00
|
|
|
// On Window there is no shadow by default and it can not be changed
|
|
|
|
// dynamically.
|
2017-11-01 14:48:09 +00:00
|
|
|
it('can be changed with hasShadow option', () => {
|
2016-03-25 20:03:49 +00:00
|
|
|
w.destroy()
|
2016-03-29 00:16:08 +00:00
|
|
|
let hasShadow = process.platform !== 'darwin'
|
2016-03-25 20:03:49 +00:00
|
|
|
w = new BrowserWindow({show: false, hasShadow: hasShadow})
|
|
|
|
assert.equal(w.hasShadow(), hasShadow)
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
it('can be changed with setHasShadow method', () => {
|
2016-03-29 00:16:08 +00:00
|
|
|
if (process.platform !== 'darwin') return
|
2016-03-25 20:03:49 +00:00
|
|
|
|
|
|
|
assert.equal(w.hasShadow(), true)
|
|
|
|
w.setHasShadow(false)
|
|
|
|
assert.equal(w.hasShadow(), false)
|
|
|
|
w.setHasShadow(true)
|
|
|
|
assert.equal(w.hasShadow(), true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('BrowserWindow.restore()', () => {
|
|
|
|
it('should restore the previous window size', () => {
|
2016-11-22 21:26:39 +00:00
|
|
|
if (w != null) w.destroy()
|
|
|
|
|
|
|
|
w = new BrowserWindow({
|
|
|
|
minWidth: 800,
|
|
|
|
width: 800
|
|
|
|
})
|
|
|
|
|
|
|
|
const initialSize = w.getSize()
|
|
|
|
w.minimize()
|
|
|
|
w.restore()
|
|
|
|
assertBoundsEqual(w.getSize(), initialSize)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('BrowserWindow.unmaximize()', () => {
|
|
|
|
it('should restore the previous window position', () => {
|
2016-11-22 21:26:39 +00:00
|
|
|
if (w != null) w.destroy()
|
|
|
|
w = new BrowserWindow()
|
|
|
|
|
|
|
|
const initialPosition = w.getPosition()
|
|
|
|
w.maximize()
|
|
|
|
w.unmaximize()
|
|
|
|
assertBoundsEqual(w.getPosition(), initialPosition)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('BrowserWindow.setFullScreen(false)', () => {
|
2017-11-15 21:05:46 +00:00
|
|
|
before(function () {
|
|
|
|
// only applicable to windows: https://github.com/electron/electron/issues/6036
|
|
|
|
if (process.platform !== 'win32') {
|
|
|
|
this.skip()
|
|
|
|
}
|
|
|
|
})
|
2017-06-01 04:56:35 +00:00
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
it('should restore a normal visible window from a fullscreen startup state', (done) => {
|
|
|
|
w.webContents.once('did-finish-load', () => {
|
2017-06-01 04:56:35 +00:00
|
|
|
// start fullscreen and hidden
|
|
|
|
w.setFullScreen(true)
|
2017-11-01 14:48:09 +00:00
|
|
|
w.once('show', () => { w.setFullScreen(false) })
|
|
|
|
w.once('leave-full-screen', () => {
|
2017-06-01 04:56:35 +00:00
|
|
|
assert.equal(w.isVisible(), true)
|
|
|
|
assert.equal(w.isFullScreen(), false)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
w.show()
|
|
|
|
})
|
|
|
|
w.loadURL('about:blank')
|
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
it('should keep window hidden if already in hidden state', (done) => {
|
|
|
|
w.webContents.once('did-finish-load', () => {
|
2017-06-14 00:50:15 +00:00
|
|
|
w.once('leave-full-screen', () => {
|
2017-06-01 04:56:35 +00:00
|
|
|
assert.equal(w.isVisible(), false)
|
|
|
|
assert.equal(w.isFullScreen(), false)
|
|
|
|
done()
|
2017-06-14 00:50:15 +00:00
|
|
|
})
|
|
|
|
w.setFullScreen(false)
|
2017-06-01 04:56:35 +00:00
|
|
|
})
|
|
|
|
w.loadURL('about:blank')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('parent window', () => {
|
2016-06-17 07:57:18 +00:00
|
|
|
let c = null
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
beforeEach(() => {
|
2016-06-17 07:57:18 +00:00
|
|
|
if (c != null) c.destroy()
|
2016-06-19 03:10:25 +00:00
|
|
|
c = new BrowserWindow({show: false, parent: w})
|
2016-06-17 07:57:18 +00:00
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
afterEach(() => {
|
2016-06-17 07:57:18 +00:00
|
|
|
if (c != null) c.destroy()
|
|
|
|
c = null
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('parent option', () => {
|
|
|
|
it('sets parent window', () => {
|
2016-06-19 03:06:08 +00:00
|
|
|
assert.equal(c.getParentWindow(), w)
|
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
it('adds window to child windows of parent', () => {
|
2016-06-19 03:06:08 +00:00
|
|
|
assert.deepEqual(w.getChildWindows(), [c])
|
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
it('removes from child windows of parent when window is closed', (done) => {
|
2016-06-19 03:06:08 +00:00
|
|
|
c.once('closed', () => {
|
|
|
|
assert.deepEqual(w.getChildWindows(), [])
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
c.close()
|
|
|
|
})
|
2017-09-14 05:48:42 +00:00
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
it('should not affect the show option', () => {
|
2017-09-14 05:48:42 +00:00
|
|
|
assert.equal(c.isVisible(), false)
|
|
|
|
assert.equal(c.getParentWindow().isVisible(), false)
|
|
|
|
})
|
2016-06-19 03:06:08 +00:00
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('win.setParentWindow(parent)', () => {
|
2017-11-15 21:05:46 +00:00
|
|
|
before(function () {
|
|
|
|
if (process.platform === 'win32') {
|
|
|
|
this.skip()
|
|
|
|
}
|
|
|
|
})
|
2016-06-19 03:06:08 +00:00
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
beforeEach(() => {
|
2016-06-19 03:10:25 +00:00
|
|
|
if (c != null) c.destroy()
|
|
|
|
c = new BrowserWindow({show: false})
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
it('sets parent window', () => {
|
2016-06-17 07:57:18 +00:00
|
|
|
assert.equal(w.getParentWindow(), null)
|
|
|
|
assert.equal(c.getParentWindow(), null)
|
|
|
|
c.setParentWindow(w)
|
|
|
|
assert.equal(c.getParentWindow(), w)
|
|
|
|
c.setParentWindow(null)
|
|
|
|
assert.equal(c.getParentWindow(), null)
|
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
it('adds window to child windows of parent', () => {
|
2016-06-17 07:57:18 +00:00
|
|
|
assert.deepEqual(w.getChildWindows(), [])
|
|
|
|
c.setParentWindow(w)
|
|
|
|
assert.deepEqual(w.getChildWindows(), [c])
|
|
|
|
c.setParentWindow(null)
|
|
|
|
assert.deepEqual(w.getChildWindows(), [])
|
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
it('removes from child windows of parent when window is closed', (done) => {
|
2016-06-17 07:57:18 +00:00
|
|
|
c.once('closed', () => {
|
|
|
|
assert.deepEqual(w.getChildWindows(), [])
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
c.setParentWindow(w)
|
|
|
|
c.close()
|
|
|
|
})
|
|
|
|
})
|
2016-06-18 00:51:37 +00:00
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('modal option', () => {
|
2017-11-15 21:05:46 +00:00
|
|
|
before(function () {
|
|
|
|
// The isEnabled API is not reliable on macOS.
|
|
|
|
if (process.platform === 'darwin') {
|
|
|
|
this.skip()
|
|
|
|
}
|
|
|
|
})
|
2016-06-20 05:49:24 +00:00
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
beforeEach(() => {
|
2016-06-20 05:49:24 +00:00
|
|
|
if (c != null) c.destroy()
|
|
|
|
c = new BrowserWindow({show: false, parent: w, modal: true})
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
it('disables parent window', () => {
|
2016-06-18 00:51:37 +00:00
|
|
|
assert.equal(w.isEnabled(), true)
|
2016-06-20 05:49:24 +00:00
|
|
|
c.show()
|
2016-06-18 00:51:37 +00:00
|
|
|
assert.equal(w.isEnabled(), false)
|
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
it('enables parent window when closed', (done) => {
|
2016-06-18 00:51:37 +00:00
|
|
|
c.once('closed', () => {
|
|
|
|
assert.equal(w.isEnabled(), true)
|
|
|
|
done()
|
|
|
|
})
|
2016-06-20 05:49:24 +00:00
|
|
|
c.show()
|
2016-06-18 00:51:37 +00:00
|
|
|
c.close()
|
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
it('disables parent window recursively', () => {
|
2016-06-20 05:49:24 +00:00
|
|
|
let c2 = new BrowserWindow({show: false, parent: w, modal: true})
|
|
|
|
c.show()
|
2016-06-18 00:51:37 +00:00
|
|
|
assert.equal(w.isEnabled(), false)
|
2016-06-20 05:49:24 +00:00
|
|
|
c2.show()
|
|
|
|
assert.equal(w.isEnabled(), false)
|
|
|
|
c.destroy()
|
2016-06-18 00:51:37 +00:00
|
|
|
assert.equal(w.isEnabled(), false)
|
|
|
|
c2.destroy()
|
2016-06-20 05:49:24 +00:00
|
|
|
assert.equal(w.isEnabled(), true)
|
2016-06-18 00:51:37 +00:00
|
|
|
})
|
|
|
|
})
|
2016-06-17 07:57:18 +00:00
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('window.webContents.send(channel, args...)', () => {
|
|
|
|
it('throws an error when the channel is missing', () => {
|
|
|
|
assert.throws(() => {
|
2016-03-25 20:03:49 +00:00
|
|
|
w.webContents.send()
|
|
|
|
}, 'Missing required channel argument')
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
assert.throws(() => {
|
2016-03-25 20:03:49 +00:00
|
|
|
w.webContents.send(null)
|
|
|
|
}, 'Missing required channel argument')
|
|
|
|
})
|
|
|
|
})
|
2016-02-16 04:30:42 +00:00
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('extensions and dev tools extensions', () => {
|
2017-01-27 00:15:10 +00:00
|
|
|
let showPanelTimeoutId
|
2017-01-26 17:42:45 +00:00
|
|
|
|
|
|
|
const showLastDevToolsPanel = () => {
|
2017-11-01 14:48:09 +00:00
|
|
|
w.webContents.once('devtools-opened', () => {
|
|
|
|
const show = () => {
|
|
|
|
if (w == null || w.isDestroyed()) return
|
2017-01-26 17:56:40 +00:00
|
|
|
const {devToolsWebContents} = w
|
|
|
|
if (devToolsWebContents == null || devToolsWebContents.isDestroyed()) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
const showLastPanel = () => {
|
2017-01-24 23:44:01 +00:00
|
|
|
const lastPanelId = UI.inspectorView._tabbedPane._tabs.peekLast().id
|
|
|
|
UI.inspectorView.showPanel(lastPanelId)
|
2017-01-26 17:42:45 +00:00
|
|
|
}
|
2017-01-27 00:15:10 +00:00
|
|
|
devToolsWebContents.executeJavaScript(`(${showLastPanel})()`, false, () => {
|
|
|
|
showPanelTimeoutId = setTimeout(show, 100)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
showPanelTimeoutId = setTimeout(show, 100)
|
2017-01-26 17:42:45 +00:00
|
|
|
})
|
|
|
|
}
|
2016-05-17 21:59:33 +00:00
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
afterEach(() => {
|
2017-01-27 00:15:10 +00:00
|
|
|
clearTimeout(showPanelTimeoutId)
|
2017-01-26 17:42:45 +00:00
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('BrowserWindow.addDevToolsExtension', () => {
|
|
|
|
beforeEach(() => {
|
2016-05-17 21:56:00 +00:00
|
|
|
BrowserWindow.removeDevToolsExtension('foo')
|
2016-06-10 16:24:00 +00:00
|
|
|
assert.equal(BrowserWindow.getDevToolsExtensions().hasOwnProperty('foo'), false)
|
2016-05-17 21:59:33 +00:00
|
|
|
|
|
|
|
var extensionPath = path.join(__dirname, 'fixtures', 'devtools-extensions', 'foo')
|
|
|
|
BrowserWindow.addDevToolsExtension(extensionPath)
|
2016-06-10 16:24:00 +00:00
|
|
|
assert.equal(BrowserWindow.getDevToolsExtensions().hasOwnProperty('foo'), true)
|
2016-05-17 21:59:33 +00:00
|
|
|
|
2017-01-26 17:42:45 +00:00
|
|
|
showLastDevToolsPanel()
|
2016-05-17 21:59:33 +00:00
|
|
|
|
|
|
|
w.loadURL('about:blank')
|
2016-05-17 21:56:00 +00:00
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
it('throws errors for missing manifest.json files', () => {
|
|
|
|
assert.throws(() => {
|
2016-06-09 16:44:49 +00:00
|
|
|
BrowserWindow.addDevToolsExtension(path.join(__dirname, 'does-not-exist'))
|
|
|
|
}, /ENOENT: no such file or directory/)
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
it('throws errors for invalid manifest.json files', () => {
|
|
|
|
assert.throws(() => {
|
2016-06-09 16:44:49 +00:00
|
|
|
BrowserWindow.addDevToolsExtension(path.join(__dirname, 'fixtures', 'devtools-extensions', 'bad-manifest'))
|
|
|
|
}, /Unexpected token }/)
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('when the devtools is docked', () => {
|
|
|
|
it('creates the extension', (done) => {
|
2016-05-17 21:56:00 +00:00
|
|
|
w.webContents.openDevTools({mode: 'bottom'})
|
|
|
|
|
|
|
|
ipcMain.once('answer', function (event, message) {
|
2016-06-06 21:36:01 +00:00
|
|
|
assert.equal(message.runtimeId, 'foo')
|
2016-06-07 17:38:01 +00:00
|
|
|
assert.equal(message.tabId, w.webContents.id)
|
2016-06-08 21:57:20 +00:00
|
|
|
assert.equal(message.i18nString, 'foo - bar (baz)')
|
2016-06-28 20:01:51 +00:00
|
|
|
assert.deepEqual(message.storageItems, {
|
2016-11-15 11:41:23 +00:00
|
|
|
local: {
|
|
|
|
set: {hello: 'world', world: 'hello'},
|
|
|
|
remove: {world: 'hello'},
|
|
|
|
clear: {}
|
|
|
|
},
|
|
|
|
sync: {
|
|
|
|
set: {foo: 'bar', bar: 'foo'},
|
|
|
|
remove: {foo: 'bar'},
|
|
|
|
clear: {}
|
|
|
|
}
|
2016-06-28 20:01:51 +00:00
|
|
|
})
|
2016-05-17 21:56:00 +00:00
|
|
|
done()
|
|
|
|
})
|
2016-05-17 21:52:45 +00:00
|
|
|
})
|
2016-05-17 21:56:00 +00:00
|
|
|
})
|
2016-05-17 21:52:45 +00:00
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('when the devtools is undocked', () => {
|
|
|
|
it('creates the extension', (done) => {
|
2016-05-17 21:56:00 +00:00
|
|
|
w.webContents.openDevTools({mode: 'undocked'})
|
|
|
|
|
2016-06-06 21:36:01 +00:00
|
|
|
ipcMain.once('answer', function (event, message, extensionId) {
|
|
|
|
assert.equal(message.runtimeId, 'foo')
|
2016-06-07 17:38:01 +00:00
|
|
|
assert.equal(message.tabId, w.webContents.id)
|
2016-05-17 21:56:00 +00:00
|
|
|
done()
|
|
|
|
})
|
2016-05-17 21:52:45 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
it('works when used with partitions', (done) => {
|
2016-06-08 16:34:41 +00:00
|
|
|
if (w != null) {
|
|
|
|
w.destroy()
|
|
|
|
}
|
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
partition: 'temp'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
var extensionPath = path.join(__dirname, 'fixtures', 'devtools-extensions', 'foo')
|
|
|
|
BrowserWindow.removeDevToolsExtension('foo')
|
|
|
|
BrowserWindow.addDevToolsExtension(extensionPath)
|
|
|
|
|
2017-01-26 17:42:45 +00:00
|
|
|
showLastDevToolsPanel()
|
2016-06-08 16:34:41 +00:00
|
|
|
|
|
|
|
w.loadURL('about:blank')
|
|
|
|
w.webContents.openDevTools({mode: 'bottom'})
|
|
|
|
|
|
|
|
ipcMain.once('answer', function (event, message) {
|
2016-12-06 17:25:55 +00:00
|
|
|
assert.equal(message.runtimeId, 'foo')
|
2016-06-08 16:34:41 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
it('serializes the registered extensions on quit', () => {
|
2016-03-25 20:03:49 +00:00
|
|
|
var extensionName = 'foo'
|
|
|
|
var extensionPath = path.join(__dirname, 'fixtures', 'devtools-extensions', extensionName)
|
|
|
|
var serializedPath = path.join(app.getPath('userData'), 'DevTools Extensions')
|
|
|
|
|
|
|
|
BrowserWindow.addDevToolsExtension(extensionPath)
|
|
|
|
app.emit('will-quit')
|
|
|
|
assert.deepEqual(JSON.parse(fs.readFileSync(serializedPath)), [extensionPath])
|
|
|
|
|
|
|
|
BrowserWindow.removeDevToolsExtension(extensionName)
|
|
|
|
app.emit('will-quit')
|
|
|
|
assert.equal(fs.existsSync(serializedPath), false)
|
|
|
|
})
|
2017-07-05 15:21:06 +00:00
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('BrowserWindow.addExtension', () => {
|
|
|
|
beforeEach(() => {
|
2017-07-05 15:21:06 +00:00
|
|
|
BrowserWindow.removeExtension('foo')
|
|
|
|
assert.equal(BrowserWindow.getExtensions().hasOwnProperty('foo'), false)
|
|
|
|
|
|
|
|
var extensionPath = path.join(__dirname, 'fixtures', 'devtools-extensions', 'foo')
|
|
|
|
BrowserWindow.addExtension(extensionPath)
|
|
|
|
assert.equal(BrowserWindow.getExtensions().hasOwnProperty('foo'), true)
|
|
|
|
|
|
|
|
showLastDevToolsPanel()
|
|
|
|
|
|
|
|
w.loadURL('about:blank')
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
it('throws errors for missing manifest.json files', () => {
|
|
|
|
assert.throws(() => {
|
2017-07-05 15:21:06 +00:00
|
|
|
BrowserWindow.addExtension(path.join(__dirname, 'does-not-exist'))
|
|
|
|
}, /ENOENT: no such file or directory/)
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
it('throws errors for invalid manifest.json files', () => {
|
|
|
|
assert.throws(() => {
|
2017-07-05 15:21:06 +00:00
|
|
|
BrowserWindow.addExtension(path.join(__dirname, 'fixtures', 'devtools-extensions', 'bad-manifest'))
|
|
|
|
}, /Unexpected token }/)
|
|
|
|
})
|
|
|
|
})
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('window.webContents.executeJavaScript', () => {
|
|
|
|
const expected = 'hello, world!'
|
|
|
|
const expectedErrorMsg = 'woops!'
|
|
|
|
const code = `(() => "${expected}")()`
|
|
|
|
const asyncCode = `(() => new Promise(r => setTimeout(() => r("${expected}"), 500)))()`
|
|
|
|
const badAsyncCode = `(() => new Promise((r, e) => setTimeout(() => e("${expectedErrorMsg}"), 500)))()`
|
2017-11-18 18:59:09 +00:00
|
|
|
const errorTypes = new Set([
|
|
|
|
Error,
|
|
|
|
ReferenceError,
|
|
|
|
EvalError,
|
|
|
|
RangeError,
|
|
|
|
SyntaxError,
|
|
|
|
TypeError,
|
|
|
|
URIError
|
|
|
|
])
|
2016-03-25 20:03:49 +00:00
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
it('doesnt throw when no calback is provided', () => {
|
2016-03-25 20:03:49 +00:00
|
|
|
const result = ipcRenderer.sendSync('executeJavaScript', code, false)
|
|
|
|
assert.equal(result, 'success')
|
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
it('returns result when calback is provided', (done) => {
|
2016-03-25 20:03:49 +00:00
|
|
|
ipcRenderer.send('executeJavaScript', code, true)
|
|
|
|
ipcRenderer.once('executeJavaScript-response', function (event, result) {
|
|
|
|
assert.equal(result, expected)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
it('returns result if the code returns an asyncronous promise', (done) => {
|
2016-10-10 23:40:05 +00:00
|
|
|
ipcRenderer.send('executeJavaScript', asyncCode, true)
|
2017-11-01 14:48:09 +00:00
|
|
|
ipcRenderer.once('executeJavaScript-response', (event, result) => {
|
2016-10-10 23:40:05 +00:00
|
|
|
assert.equal(result, expected)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
it('resolves the returned promise with the result when a callback is specified', (done) => {
|
2016-10-11 05:47:09 +00:00
|
|
|
ipcRenderer.send('executeJavaScript', code, true)
|
2017-11-01 14:48:09 +00:00
|
|
|
ipcRenderer.once('executeJavaScript-promise-response', (event, result) => {
|
2016-10-11 05:47:09 +00:00
|
|
|
assert.equal(result, expected)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
it('resolves the returned promise with the result when no callback is specified', (done) => {
|
2017-03-06 18:49:23 +00:00
|
|
|
ipcRenderer.send('executeJavaScript', code, false)
|
2017-11-01 14:48:09 +00:00
|
|
|
ipcRenderer.once('executeJavaScript-promise-response', (event, result) => {
|
2017-03-06 18:49:23 +00:00
|
|
|
assert.equal(result, expected)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
it('resolves the returned promise with the result if the code returns an asyncronous promise', (done) => {
|
2016-10-11 05:47:09 +00:00
|
|
|
ipcRenderer.send('executeJavaScript', asyncCode, true)
|
2017-11-01 14:48:09 +00:00
|
|
|
ipcRenderer.once('executeJavaScript-promise-response', (event, result) => {
|
2016-10-11 05:47:09 +00:00
|
|
|
assert.equal(result, expected)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
it('rejects the returned promise if an async error is thrown', (done) => {
|
2016-10-11 05:47:09 +00:00
|
|
|
ipcRenderer.send('executeJavaScript', badAsyncCode, true)
|
2017-11-01 14:48:09 +00:00
|
|
|
ipcRenderer.once('executeJavaScript-promise-error', (event, error) => {
|
2016-10-11 05:47:09 +00:00
|
|
|
assert.equal(error, expectedErrorMsg)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
2017-11-18 08:51:27 +00:00
|
|
|
it('rejects the returned promise with an error if an Error.prototype is thrown', async () => {
|
2017-11-18 18:59:09 +00:00
|
|
|
for (const error in errorTypes) {
|
2017-11-18 08:51:27 +00:00
|
|
|
await new Promise((resolve) => {
|
2017-11-18 18:59:09 +00:00
|
|
|
ipcRenderer.send('executeJavaScript', `Promise.reject(new ${error.name}("Wamp-wamp")`, true)
|
2017-11-18 08:51:27 +00:00
|
|
|
ipcRenderer.once('executeJavaScript-promise-error-name', (event, name) => {
|
2017-11-18 18:59:09 +00:00
|
|
|
assert.equal(name, error.name)
|
2017-11-18 08:51:27 +00:00
|
|
|
resolve()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
it('works after page load and during subframe load', (done) => {
|
|
|
|
w.webContents.once('did-finish-load', () => {
|
2016-04-18 17:33:56 +00:00
|
|
|
// initiate a sub-frame load, then try and execute script during it
|
|
|
|
w.webContents.executeJavaScript(`
|
|
|
|
var iframe = document.createElement('iframe')
|
2016-04-20 05:05:09 +00:00
|
|
|
iframe.src = '${server.url}/slow'
|
2016-04-18 17:33:56 +00:00
|
|
|
document.body.appendChild(iframe)
|
2017-11-01 14:48:09 +00:00
|
|
|
`, () => {
|
|
|
|
w.webContents.executeJavaScript('console.log(\'hello\')', () => {
|
2016-04-18 17:33:56 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2016-04-20 05:05:09 +00:00
|
|
|
w.loadURL(server.url)
|
2016-04-18 17:33:56 +00:00
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
it('executes after page load', (done) => {
|
|
|
|
w.webContents.executeJavaScript(code, (result) => {
|
2016-04-27 20:24:08 +00:00
|
|
|
assert.equal(result, expected)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
w.loadURL(server.url)
|
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
it('works with result objects that have DOM class prototypes', (done) => {
|
|
|
|
w.webContents.executeJavaScript('document.location', (result) => {
|
2016-08-08 16:42:43 +00:00
|
|
|
assert.equal(result.origin, server.url)
|
|
|
|
assert.equal(result.protocol, 'http:')
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
w.loadURL(server.url)
|
|
|
|
})
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2016-07-31 15:10:53 +00:00
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('previewFile', () => {
|
2017-11-15 21:05:46 +00:00
|
|
|
before(function () {
|
|
|
|
if (process.platform !== 'darwin') {
|
|
|
|
this.skip()
|
|
|
|
}
|
|
|
|
})
|
2016-11-28 21:03:33 +00:00
|
|
|
|
2017-11-15 21:05:46 +00:00
|
|
|
it('opens the path in Quick Look on macOS', () => {
|
2017-11-01 14:48:09 +00:00
|
|
|
assert.doesNotThrow(() => {
|
2016-11-28 21:03:33 +00:00
|
|
|
w.previewFile(__filename)
|
|
|
|
w.closeFilePreview()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-07-28 00:08:24 +00:00
|
|
|
describe('contextIsolation option with and without sandbox option', () => {
|
2017-01-12 00:36:59 +00:00
|
|
|
const expectedContextData = {
|
|
|
|
preloadContext: {
|
|
|
|
preloadProperty: 'number',
|
|
|
|
pageProperty: 'undefined',
|
|
|
|
typeofRequire: 'function',
|
|
|
|
typeofProcess: 'object',
|
|
|
|
typeofArrayPush: 'function',
|
|
|
|
typeofFunctionApply: 'function'
|
|
|
|
},
|
|
|
|
pageContext: {
|
|
|
|
preloadProperty: 'undefined',
|
|
|
|
pageProperty: 'string',
|
|
|
|
typeofRequire: 'undefined',
|
|
|
|
typeofProcess: 'undefined',
|
|
|
|
typeofArrayPush: 'number',
|
|
|
|
typeofFunctionApply: 'boolean',
|
|
|
|
typeofPreloadExecuteJavaScriptProperty: 'number',
|
2017-05-22 18:10:10 +00:00
|
|
|
typeofOpenedWindow: 'object'
|
2017-01-12 00:36:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-09 23:18:47 +00:00
|
|
|
beforeEach(() => {
|
2016-12-13 19:47:54 +00:00
|
|
|
if (w != null) w.destroy()
|
2017-01-09 23:18:47 +00:00
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
contextIsolation: true,
|
|
|
|
preload: path.join(fixtures, 'api', 'isolated-preload.js')
|
|
|
|
}
|
|
|
|
})
|
2017-07-28 00:08:24 +00:00
|
|
|
if (ws != null) ws.destroy()
|
|
|
|
ws = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
sandbox: true,
|
|
|
|
contextIsolation: true,
|
|
|
|
preload: path.join(fixtures, 'api', 'isolated-preload.js')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
if (ws != null) ws.destroy()
|
2017-01-09 23:18:47 +00:00
|
|
|
})
|
2016-12-13 19:47:54 +00:00
|
|
|
|
2017-01-09 23:18:47 +00:00
|
|
|
it('separates the page context from the Electron/preload context', (done) => {
|
2016-12-13 19:47:54 +00:00
|
|
|
ipcMain.once('isolated-world', (event, data) => {
|
2017-01-12 00:36:59 +00:00
|
|
|
assert.deepEqual(data, expectedContextData)
|
2016-12-13 19:47:54 +00:00
|
|
|
done()
|
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
w.loadURL(`file://${fixtures}/api/isolated.html`)
|
2017-01-09 23:18:47 +00:00
|
|
|
})
|
|
|
|
it('recreates the contexts on reload', (done) => {
|
|
|
|
w.webContents.once('did-finish-load', () => {
|
|
|
|
ipcMain.once('isolated-world', (event, data) => {
|
2017-01-12 00:36:59 +00:00
|
|
|
assert.deepEqual(data, expectedContextData)
|
2017-01-09 23:18:47 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
w.webContents.reload()
|
2016-12-13 19:47:54 +00:00
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
w.loadURL(`file://${fixtures}/api/isolated.html`)
|
2016-12-13 19:47:54 +00:00
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
it('enables context isolation on child windows', (done) => {
|
|
|
|
app.once('browser-window-created', (event, window) => {
|
2017-01-10 17:37:38 +00:00
|
|
|
assert.equal(window.webContents.getWebPreferences().contextIsolation, true)
|
|
|
|
done()
|
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
w.loadURL(`file://${fixtures}/pages/window-open.html`)
|
2017-01-10 17:37:38 +00:00
|
|
|
})
|
2017-07-28 00:08:24 +00:00
|
|
|
it('separates the page context from the Electron/preload context with sandbox on', (done) => {
|
|
|
|
ipcMain.once('isolated-sandbox-world', (event, data) => {
|
|
|
|
assert.deepEqual(data, expectedContextData)
|
|
|
|
done()
|
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
w.loadURL(`file://${fixtures}/api/isolated.html`)
|
2017-07-28 00:08:24 +00:00
|
|
|
})
|
|
|
|
it('recreates the contexts on reload with sandbox on', (done) => {
|
|
|
|
w.webContents.once('did-finish-load', () => {
|
|
|
|
ipcMain.once('isolated-sandbox-world', (event, data) => {
|
|
|
|
assert.deepEqual(data, expectedContextData)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
w.webContents.reload()
|
|
|
|
})
|
2017-11-01 14:48:09 +00:00
|
|
|
w.loadURL(`file://${fixtures}/api/isolated.html`)
|
2017-07-28 00:08:24 +00:00
|
|
|
})
|
2016-12-13 19:47:54 +00:00
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('offscreen rendering', () => {
|
2017-06-29 19:26:45 +00:00
|
|
|
const isOffscreenRenderingDisabled = () => {
|
|
|
|
const contents = webContents.create({})
|
|
|
|
const disabled = typeof contents.isOffscreen !== 'function'
|
|
|
|
contents.destroy()
|
|
|
|
return disabled
|
|
|
|
}
|
|
|
|
|
|
|
|
// Offscreen rendering can be disabled in the build
|
|
|
|
if (isOffscreenRenderingDisabled()) return
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
beforeEach(() => {
|
2017-06-29 19:26:45 +00:00
|
|
|
if (w != null) w.destroy()
|
|
|
|
w = new BrowserWindow({
|
2017-05-13 00:14:30 +00:00
|
|
|
width: 100,
|
|
|
|
height: 100,
|
2016-07-31 15:10:53 +00:00
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
backgroundThrottling: false,
|
|
|
|
offscreen: true
|
|
|
|
}
|
|
|
|
})
|
2016-08-03 01:27:55 +00:00
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
it('creates offscreen window with correct size', (done) => {
|
2017-05-13 00:14:30 +00:00
|
|
|
w.webContents.once('paint', function (event, rect, data) {
|
2016-07-31 15:10:53 +00:00
|
|
|
assert.notEqual(data.length, 0)
|
2017-05-13 00:14:30 +00:00
|
|
|
let size = data.getSize()
|
|
|
|
assertWithinDelta(size.width, 100, 2, 'width')
|
|
|
|
assertWithinDelta(size.height, 100, 2, 'height')
|
2016-07-31 15:10:53 +00:00
|
|
|
done()
|
|
|
|
})
|
2016-08-03 01:27:55 +00:00
|
|
|
w.loadURL('file://' + fixtures + '/api/offscreen-rendering.html')
|
2016-07-31 15:10:53 +00:00
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('window.webContents.isOffscreen()', () => {
|
|
|
|
it('is true for offscreen type', () => {
|
2016-08-03 01:27:55 +00:00
|
|
|
w.loadURL('file://' + fixtures + '/api/offscreen-rendering.html')
|
|
|
|
assert.equal(w.webContents.isOffscreen(), true)
|
2016-07-31 15:10:53 +00:00
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
it('is false for regular window', () => {
|
2016-08-03 01:27:55 +00:00
|
|
|
let c = new BrowserWindow({show: false})
|
|
|
|
assert.equal(c.webContents.isOffscreen(), false)
|
|
|
|
c.destroy()
|
2016-07-31 15:10:53 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('window.webContents.isPainting()', () => {
|
|
|
|
it('returns whether is currently painting', (done) => {
|
2017-05-13 00:14:30 +00:00
|
|
|
w.webContents.once('paint', function (event, rect, data) {
|
2016-08-03 01:27:55 +00:00
|
|
|
assert.equal(w.webContents.isPainting(), true)
|
2016-07-31 15:10:53 +00:00
|
|
|
done()
|
|
|
|
})
|
2016-08-03 01:27:55 +00:00
|
|
|
w.loadURL('file://' + fixtures + '/api/offscreen-rendering.html')
|
2016-07-31 15:10:53 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('window.webContents.stopPainting()', () => {
|
|
|
|
it('stops painting', (done) => {
|
|
|
|
w.webContents.on('dom-ready', () => {
|
2016-08-03 01:27:55 +00:00
|
|
|
w.webContents.stopPainting()
|
|
|
|
assert.equal(w.webContents.isPainting(), false)
|
2016-07-31 15:10:53 +00:00
|
|
|
done()
|
|
|
|
})
|
2016-08-03 01:27:55 +00:00
|
|
|
w.loadURL('file://' + fixtures + '/api/offscreen-rendering.html')
|
2016-07-31 15:10:53 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('window.webContents.startPainting()', () => {
|
|
|
|
it('starts painting', (done) => {
|
|
|
|
w.webContents.on('dom-ready', () => {
|
2016-08-03 01:27:55 +00:00
|
|
|
w.webContents.stopPainting()
|
|
|
|
w.webContents.startPainting()
|
2017-05-13 00:14:30 +00:00
|
|
|
w.webContents.once('paint', function (event, rect, data) {
|
2016-08-03 01:27:55 +00:00
|
|
|
assert.equal(w.webContents.isPainting(), true)
|
2016-07-31 15:10:53 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
2016-08-03 01:27:55 +00:00
|
|
|
w.loadURL('file://' + fixtures + '/api/offscreen-rendering.html')
|
2016-07-31 15:10:53 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('window.webContents.getFrameRate()', () => {
|
|
|
|
it('has default frame rate', (done) => {
|
2017-05-13 00:14:30 +00:00
|
|
|
w.webContents.once('paint', function (event, rect, data) {
|
2016-08-03 01:27:55 +00:00
|
|
|
assert.equal(w.webContents.getFrameRate(), 60)
|
2016-07-31 15:10:53 +00:00
|
|
|
done()
|
|
|
|
})
|
2016-08-03 04:04:36 +00:00
|
|
|
w.loadURL('file://' + fixtures + '/api/offscreen-rendering.html')
|
2016-07-31 15:10:53 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-11-01 14:48:09 +00:00
|
|
|
describe('window.webContents.setFrameRate(frameRate)', () => {
|
|
|
|
it('sets custom frame rate', (done) => {
|
|
|
|
w.webContents.on('dom-ready', () => {
|
2016-08-03 01:27:55 +00:00
|
|
|
w.webContents.setFrameRate(30)
|
2017-05-13 00:14:30 +00:00
|
|
|
w.webContents.once('paint', function (event, rect, data) {
|
2016-08-03 01:27:55 +00:00
|
|
|
assert.equal(w.webContents.getFrameRate(), 30)
|
2016-07-31 15:10:53 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
2016-08-03 01:27:55 +00:00
|
|
|
w.loadURL('file://' + fixtures + '/api/offscreen-rendering.html')
|
2016-07-31 15:10:53 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2016-09-08 20:31:01 +00:00
|
|
|
|
2016-09-08 20:39:15 +00:00
|
|
|
const assertBoundsEqual = (actual, expect) => {
|
2016-09-08 23:24:11 +00:00
|
|
|
if (!isScaleFactorRounding()) {
|
2016-09-08 20:31:01 +00:00
|
|
|
assert.deepEqual(expect, actual)
|
2016-09-08 20:33:43 +00:00
|
|
|
} else if (Array.isArray(actual)) {
|
2016-09-08 20:39:15 +00:00
|
|
|
assertWithinDelta(actual[0], expect[0], 1, 'x')
|
|
|
|
assertWithinDelta(actual[1], expect[1], 1, 'y')
|
2016-09-08 20:31:01 +00:00
|
|
|
} else {
|
2016-09-08 20:39:15 +00:00
|
|
|
assertWithinDelta(actual.x, expect.x, 1, 'x')
|
|
|
|
assertWithinDelta(actual.y, expect.y, 1, 'y')
|
|
|
|
assertWithinDelta(actual.width, expect.width, 1, 'width')
|
|
|
|
assertWithinDelta(actual.height, expect.height, 1, 'height')
|
2016-09-08 20:31:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-08 20:39:15 +00:00
|
|
|
const assertWithinDelta = (actual, expect, delta, label) => {
|
2016-09-08 20:46:55 +00:00
|
|
|
const result = Math.abs(actual - expect)
|
2017-07-04 09:56:21 +00:00
|
|
|
assert.ok(result <= delta, `${label} value of ${actual} was not within ${delta} of ${expect}`)
|
2016-09-08 20:31:01 +00:00
|
|
|
}
|
|
|
|
|
2016-09-08 23:24:11 +00:00
|
|
|
// Is the display's scale factor possibly causing rounding of pixel coordinate
|
|
|
|
// values?
|
|
|
|
const isScaleFactorRounding = () => {
|
2016-09-08 20:31:01 +00:00
|
|
|
const {scaleFactor} = screen.getPrimaryDisplay()
|
2016-09-08 23:24:11 +00:00
|
|
|
// Return true if scale factor is non-integer value
|
2016-09-09 00:12:53 +00:00
|
|
|
if (Math.round(scaleFactor) !== scaleFactor) return true
|
2016-09-08 23:24:11 +00:00
|
|
|
// Return true if scale factor is odd number above 2
|
|
|
|
return scaleFactor > 2 && scaleFactor % 2 === 1
|
2016-09-08 20:31:01 +00:00
|
|
|
}
|
2017-07-10 23:59:49 +00:00
|
|
|
|
|
|
|
function serveFileFromProtocol (protocolName, filePath) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
protocol.registerBufferProtocol(protocolName, (request, callback) => {
|
|
|
|
callback({
|
|
|
|
mimeType: 'text/html',
|
|
|
|
data: fs.readFileSync(filePath)
|
|
|
|
})
|
|
|
|
}, (error) => {
|
|
|
|
if (error != null) {
|
|
|
|
reject(error)
|
|
|
|
} else {
|
|
|
|
resolve()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|