2016-03-25 20:03:49 +00:00
|
|
|
const assert = require('assert')
|
2018-07-10 19:43:39 +00:00
|
|
|
const chai = require('chai')
|
|
|
|
const dirtyChai = require('dirty-chai')
|
2016-03-25 20:03:49 +00:00
|
|
|
const path = require('path')
|
2018-09-13 16:10:51 +00:00
|
|
|
const { closeWindow } = require('./window-helpers')
|
|
|
|
const { remote, webFrame } = require('electron')
|
|
|
|
const { BrowserWindow, protocol, ipcMain } = remote
|
|
|
|
const { emittedOnce } = require('./events-helpers')
|
2018-07-10 19:43:39 +00:00
|
|
|
|
2018-09-13 16:10:51 +00:00
|
|
|
const { expect } = chai
|
2018-07-10 19:43:39 +00:00
|
|
|
chai.use(dirtyChai)
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2017-11-23 22:22:43 +00:00
|
|
|
/* Most of the APIs here don't use standard callbacks */
|
|
|
|
/* eslint-disable standard/no-callback-literal */
|
|
|
|
|
2016-03-25 20:03:49 +00:00
|
|
|
describe('webFrame module', function () {
|
|
|
|
var fixtures = path.resolve(__dirname, 'fixtures')
|
2016-11-29 20:30:40 +00:00
|
|
|
var w = null
|
|
|
|
|
|
|
|
afterEach(function () {
|
|
|
|
return closeWindow(w).then(function () { w = null })
|
|
|
|
})
|
2016-11-22 16:43:14 +00:00
|
|
|
|
2016-03-25 20:03:49 +00:00
|
|
|
describe('webFrame.registerURLSchemeAsPrivileged', function () {
|
2016-10-18 20:52:41 +00:00
|
|
|
it('supports fetch api by default', function (done) {
|
2016-03-25 20:03:49 +00:00
|
|
|
var url = 'file://' + fixtures + '/assets/logo.png'
|
2016-03-30 21:56:30 +00:00
|
|
|
window.fetch(url).then(function (response) {
|
2016-03-25 20:03:49 +00:00
|
|
|
assert(response.ok)
|
|
|
|
done()
|
|
|
|
}).catch(function (err) {
|
|
|
|
done('unexpected error : ' + err)
|
|
|
|
})
|
|
|
|
})
|
2016-08-11 17:31:10 +00:00
|
|
|
|
2016-10-18 20:52:41 +00:00
|
|
|
it('allows CORS requests by default', function (done) {
|
|
|
|
allowsCORSRequests(200, `<html>
|
|
|
|
<script>
|
|
|
|
const {ipcRenderer, webFrame} = require('electron')
|
|
|
|
webFrame.registerURLSchemeAsPrivileged('cors1')
|
|
|
|
fetch('cors1://myhost').then(function (response) {
|
|
|
|
ipcRenderer.send('response', response.status)
|
|
|
|
}).catch(function (response) {
|
|
|
|
ipcRenderer.send('response', 'failed')
|
|
|
|
})
|
|
|
|
</script>
|
|
|
|
</html>`, done)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('allows CORS and fetch requests when specified', function (done) {
|
|
|
|
allowsCORSRequests(200, `<html>
|
|
|
|
<script>
|
|
|
|
const {ipcRenderer, webFrame} = require('electron')
|
|
|
|
webFrame.registerURLSchemeAsPrivileged('cors2', { supportFetchAPI: true, corsEnabled: true })
|
|
|
|
fetch('cors2://myhost').then(function (response) {
|
|
|
|
ipcRenderer.send('response', response.status)
|
|
|
|
}).catch(function (response) {
|
|
|
|
ipcRenderer.send('response', 'failed')
|
|
|
|
})
|
|
|
|
</script>
|
|
|
|
</html>`, done)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('allows CORS and fetch requests when half-specified', function (done) {
|
|
|
|
allowsCORSRequests(200, `<html>
|
|
|
|
<script>
|
|
|
|
const {ipcRenderer, webFrame} = require('electron')
|
|
|
|
webFrame.registerURLSchemeAsPrivileged('cors3', { supportFetchAPI: true })
|
|
|
|
fetch('cors3://myhost').then(function (response) {
|
|
|
|
ipcRenderer.send('response', response.status)
|
|
|
|
}).catch(function (response) {
|
|
|
|
ipcRenderer.send('response', 'failed')
|
|
|
|
})
|
|
|
|
</script>
|
|
|
|
</html>`, done)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('disallows CORS, but allows fetch requests, when specified', function (done) {
|
|
|
|
allowsCORSRequests('failed', `<html>
|
|
|
|
<script>
|
|
|
|
const {ipcRenderer, webFrame} = require('electron')
|
|
|
|
webFrame.registerURLSchemeAsPrivileged('cors4', { supportFetchAPI: true, corsEnabled: false })
|
|
|
|
fetch('cors4://myhost').then(function (response) {
|
|
|
|
ipcRenderer.send('response', response.status)
|
|
|
|
}).catch(function (response) {
|
|
|
|
ipcRenderer.send('response', 'failed')
|
|
|
|
})
|
|
|
|
</script>
|
|
|
|
</html>`, done)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('allows CORS, but disallows fetch requests, when specified', function (done) {
|
|
|
|
allowsCORSRequests('failed', `<html>
|
|
|
|
<script>
|
|
|
|
const {ipcRenderer, webFrame} = require('electron')
|
|
|
|
webFrame.registerURLSchemeAsPrivileged('cors5', { supportFetchAPI: false, corsEnabled: true })
|
|
|
|
fetch('cors5://myhost').then(function (response) {
|
|
|
|
ipcRenderer.send('response', response.status)
|
|
|
|
}).catch(function (response) {
|
|
|
|
ipcRenderer.send('response', 'failed')
|
|
|
|
})
|
|
|
|
</script>
|
|
|
|
</html>`, done)
|
|
|
|
})
|
|
|
|
|
|
|
|
var runNumber = 1
|
|
|
|
function allowsCORSRequests (expected, content, done) {
|
|
|
|
const standardScheme = remote.getGlobal('standardScheme') + runNumber
|
|
|
|
const corsScheme = 'cors' + runNumber
|
|
|
|
runNumber++
|
|
|
|
|
2016-08-11 17:31:10 +00:00
|
|
|
const url = standardScheme + '://fake-host'
|
2018-09-13 16:10:51 +00:00
|
|
|
w = new BrowserWindow({ show: false })
|
2016-08-11 17:31:10 +00:00
|
|
|
after(function (done) {
|
2016-10-18 20:52:41 +00:00
|
|
|
protocol.unregisterProtocol(corsScheme, function () {
|
2016-08-11 17:31:10 +00:00
|
|
|
protocol.unregisterProtocol(standardScheme, function () {
|
2016-11-29 20:30:40 +00:00
|
|
|
done()
|
2016-08-11 17:31:10 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
const handler = function (request, callback) {
|
2018-09-13 16:10:51 +00:00
|
|
|
callback({ data: content, mimeType: 'text/html' })
|
2016-08-11 17:31:10 +00:00
|
|
|
}
|
|
|
|
protocol.registerStringProtocol(standardScheme, handler, function (error) {
|
|
|
|
if (error) return done(error)
|
|
|
|
})
|
|
|
|
|
2016-10-18 20:52:41 +00:00
|
|
|
protocol.registerStringProtocol(corsScheme, function (request, callback) {
|
2016-08-11 17:31:10 +00:00
|
|
|
callback('')
|
|
|
|
}, function (error) {
|
|
|
|
if (error) return done(error)
|
|
|
|
ipcMain.once('response', function (event, status) {
|
2018-09-13 16:10:51 +00:00
|
|
|
assert.strictEqual(status, expected)
|
2016-08-11 17:31:10 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
w.loadURL(url)
|
|
|
|
})
|
2016-10-18 20:52:41 +00:00
|
|
|
}
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2016-11-22 16:43:14 +00:00
|
|
|
|
|
|
|
it('supports setting the visual and layout zoom level limits', function () {
|
|
|
|
assert.doesNotThrow(function () {
|
|
|
|
webFrame.setVisualZoomLevelLimits(1, 50)
|
|
|
|
webFrame.setLayoutZoomLevelLimits(0, 25)
|
|
|
|
})
|
|
|
|
})
|
2018-07-10 19:43:39 +00:00
|
|
|
|
|
|
|
it('calls a spellcheck provider', async () => {
|
2018-09-13 16:10:51 +00:00
|
|
|
w = new BrowserWindow({ show: false })
|
2018-09-04 14:50:53 +00:00
|
|
|
w.loadFile(path.join(fixtures, 'pages', 'webframe-spell-check.html'))
|
2018-07-10 19:43:39 +00:00
|
|
|
await emittedOnce(w.webContents, 'did-finish-load')
|
|
|
|
|
|
|
|
const spellCheckerFeedback = emittedOnce(ipcMain, 'spec-spell-check')
|
|
|
|
const misspelledWord = 'spleling'
|
|
|
|
for (const keyCode of [...misspelledWord, ' ']) {
|
2018-09-13 16:10:51 +00:00
|
|
|
w.webContents.sendInputEvent({ type: 'char', keyCode })
|
2018-07-10 19:43:39 +00:00
|
|
|
}
|
|
|
|
const [, text] = await spellCheckerFeedback
|
|
|
|
expect(text).to.equal(misspelledWord)
|
|
|
|
})
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|