forked from electron/electron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi-remote-spec.ts
194 lines (171 loc) · 6.95 KB
/
api-remote-spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import * as path from 'path'
import { expect } from 'chai'
import { closeWindow, closeAllWindows } from './window-helpers'
import { ifdescribe } from './spec-helpers';
import { ipcMain, BrowserWindow } from 'electron'
import { emittedOnce } from './events-helpers';
const features = process.electronBinding('features')
ifdescribe(features.isRemoteModuleEnabled())('remote module', () => {
const fixtures = path.join(__dirname, 'fixtures')
describe('', () => {
let w = null as unknown as BrowserWindow
beforeEach(async () => {
w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true } })
w.loadURL('about:blank')
})
afterEach(async () => {
await closeWindow(w)
})
async function remotely(script: string) {
// executeJavaScript never returns if the script throws an error, so catch
// any errors manually.
const assembledScript = `(function() {
try {
return { result: ${script} }
} catch (e) {
return { error: e.message }
}
})()`
const { result, error } = await w.webContents.executeJavaScript(assembledScript)
if (error) {
throw new Error(error)
}
return result
}
describe('remote.getGlobal filtering', () => {
it('can return custom values', async () => {
w.webContents.once('remote-get-global', (event, name) => {
event.returnValue = name
})
expect(await remotely(`require('electron').remote.getGlobal('test')`)).to.equal('test')
})
it('throws when no returnValue set', async () => {
w.webContents.once('remote-get-global', (event, name) => {
event.preventDefault()
})
await expect(remotely(`require('electron').remote.getGlobal('test')`)).to.eventually.be.rejected(`Blocked remote.getGlobal('test')`)
})
})
describe('remote.getBuiltin filtering', () => {
it('can return custom values', async () => {
w.webContents.once('remote-get-builtin', (event, name) => {
event.returnValue = name
})
expect(await remotely(`require('electron').remote.getBuiltin('test')`)).to.equal('test')
})
it('throws when no returnValue set', async () => {
w.webContents.once('remote-get-builtin', (event, name) => {
event.preventDefault()
})
await expect(remotely(`require('electron').remote.getBuiltin('test')`)).to.eventually.be.rejected(`Blocked remote.getGlobal('test')`)
})
})
describe('remote.require filtering', () => {
it('can return custom values', async () => {
w.webContents.once('remote-require', (event, name) => {
event.returnValue = name
})
expect(await remotely(`require('electron').remote.require('test')`)).to.equal('test')
})
it('throws when no returnValue set', async () => {
w.webContents.once('remote-require', (event, name) => {
event.preventDefault()
})
await expect(remotely(`require('electron').remote.require('test')`)).to.eventually.be.rejected(`Blocked remote.require('test')`)
})
})
describe('remote.getCurrentWindow filtering', () => {
it('can return custom value', async () => {
w.webContents.once('remote-get-current-window', (e) => {
e.returnValue = 'some window'
})
expect(await remotely(`require('electron').remote.getCurrentWindow()`)).to.equal('some window')
})
it('throws when no returnValue set', async () => {
w.webContents.once('remote-get-current-window', (event) => {
event.preventDefault()
})
await expect(remotely(`require('electron').remote.getCurrentWindow()`)).to.eventually.be.rejected(`Blocked remote.getCurrentWindow()`)
})
})
describe('remote.getCurrentWebContents filtering', () => {
it('can return custom value', async () => {
w.webContents.once('remote-get-current-web-contents', (event) => {
event.returnValue = 'some web contents'
})
expect(await remotely(`require('electron').remote.getCurrentWebContents()`)).to.equal('some web contents')
})
it('throws when no returnValue set', async () => {
w.webContents.once('remote-get-current-web-contents', (event) => {
event.preventDefault()
})
await expect(remotely(`require('electron').remote.getCurrentWebContents()`)).to.eventually.be.rejected(`Blocked remote.getCurrentWebContents()`)
})
})
describe('remote references', () => {
it('render-view-deleted is sent when page is destroyed', (done) => {
w.webContents.once('render-view-deleted' as any, () => {
done()
})
w.destroy()
})
// The ELECTRON_BROWSER_CONTEXT_RELEASE message relies on this to work.
it('message can be sent on exit when page is being navigated', async () => {
after(() => { ipcMain.removeAllListeners('SENT_ON_EXIT') })
await emittedOnce(w.webContents, 'did-finish-load')
w.webContents.once('did-finish-load', () => {
w.webContents.loadURL('about:blank')
})
w.loadFile(path.join(fixtures, 'api', 'send-on-exit.html'))
await emittedOnce(ipcMain, 'SENT_ON_EXIT')
})
})
})
describe('remote function in renderer', () => {
afterEach(() => {
ipcMain.removeAllListeners('done')
})
afterEach(closeAllWindows)
it('works when created in preload script', async () => {
const preload = path.join(fixtures, 'module', 'preload-remote-function.js')
const w = new BrowserWindow({
show: false,
webPreferences: {
preload
}
})
w.loadURL('about:blank')
await emittedOnce(ipcMain, 'done')
})
})
describe('remote listeners', () => {
afterEach(closeAllWindows)
it('detaches listeners subscribed to destroyed renderers, and shows a warning', async () => {
const w = new BrowserWindow({
show: false,
webPreferences: {
nodeIntegration: true
}
})
await w.loadFile(path.join(__dirname, '..', 'spec', 'fixtures', 'api', 'remote-event-handler.html'))
w.webContents.reload()
await emittedOnce(w.webContents, 'did-finish-load')
const expectedMessage = [
'Attempting to call a function in a renderer window that has been closed or released.',
'Function provided here: remote-event-handler.html:11:33',
'Remote event names: remote-handler, other-remote-handler'
].join('\n')
expect(w.webContents.listenerCount('remote-handler')).to.equal(2)
let warnMessage: string | null = null
let originalWarn = console.warn
try {
console.warn = (message: string) => warnMessage = message
w.webContents.emit('remote-handler', { sender: w.webContents })
} finally {
console.warn = originalWarn
}
expect(w.webContents.listenerCount('remote-handler')).to.equal(1)
expect(warnMessage).to.equal(expectedMessage)
})
})
})