Skip to content

Commit 26ecf63

Browse files
authored
test: remove a bunch of usage of the remote module (electron#21119)
1 parent 4f15364 commit 26ecf63

22 files changed

+124
-125
lines changed

spec-main/api-browser-window-spec.ts

Lines changed: 48 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,12 @@ describe('BrowserWindow module', () => {
8080
})
8181
it('should emit beforeunload handler', async () => {
8282
await w.loadFile(path.join(fixtures, 'api', 'beforeunload-false.html'))
83-
const beforeunload = emittedOnce(w, 'onbeforeunload')
83+
const beforeunload = new Promise(resolve => {
84+
ipcMain.once('onbeforeunload', (e) => {
85+
e.returnValue = null
86+
resolve()
87+
})
88+
})
8489
w.close()
8590
await beforeunload
8691
})
@@ -164,8 +169,9 @@ describe('BrowserWindow module', () => {
164169
expect(content).to.equal('close')
165170
})
166171
it('should emit beforeunload event', async () => {
167-
w.loadFile(path.join(fixtures, 'api', 'close-beforeunload-false.html'))
168-
await emittedOnce(w, 'onbeforeunload')
172+
w.loadFile(path.join(__dirname, 'fixtures', 'api', 'close-beforeunload-false.html'))
173+
const [e] = await emittedOnce(ipcMain, 'onbeforeunload')
174+
e.returnValue = null
169175
})
170176
})
171177

@@ -1552,7 +1558,7 @@ describe('BrowserWindow module', () => {
15521558
expect(test).to.eql('preload')
15531559
})
15541560
it('can successfully delete the Buffer global', async () => {
1555-
const preload = path.join(fixtures, 'module', 'delete-buffer.js')
1561+
const preload = path.join(__dirname, 'fixtures', 'module', 'delete-buffer.js')
15561562
const w = new BrowserWindow({
15571563
show: false,
15581564
webPreferences: {
@@ -1676,7 +1682,7 @@ describe('BrowserWindow module', () => {
16761682
describe('"enableRemoteModule" option', () => {
16771683
const generateSpecs = (description: string, sandbox: boolean) => {
16781684
describe(description, () => {
1679-
const preload = path.join(fixtures, 'module', 'preload-remote.js')
1685+
const preload = path.join(__dirname, 'fixtures', 'module', 'preload-remote.js')
16801686

16811687
it('enables the remote module by default', async () => {
16821688
const w = new BrowserWindow({
@@ -1794,7 +1800,7 @@ describe('BrowserWindow module', () => {
17941800
preload
17951801
}
17961802
})
1797-
const htmlPath = path.join(fixtures, 'api', 'sandbox.html?exit-event')
1803+
const htmlPath = path.join(__dirname, 'fixtures', 'api', 'sandbox.html?exit-event')
17981804
const pageUrl = 'file://' + htmlPath
17991805
w.loadURL(pageUrl)
18001806
const [, url] = await emittedOnce(ipcMain, 'answer')
@@ -1815,7 +1821,7 @@ describe('BrowserWindow module', () => {
18151821
w.webContents.once('new-window', (event, url, frameName, disposition, options) => {
18161822
options.webPreferences!.preload = preload
18171823
})
1818-
const htmlPath = path.join(fixtures, 'api', 'sandbox.html?window-open')
1824+
const htmlPath = path.join(__dirname, 'fixtures', 'api', 'sandbox.html?window-open')
18191825
const pageUrl = 'file://' + htmlPath
18201826
const answer = emittedOnce(ipcMain, 'answer')
18211827
w.loadURL(pageUrl)
@@ -1844,7 +1850,7 @@ describe('BrowserWindow module', () => {
18441850
options.webPreferences!.preload = preload
18451851
})
18461852
w.loadFile(
1847-
path.join(fixtures, 'api', 'sandbox.html'),
1853+
path.join(__dirname, 'fixtures', 'api', 'sandbox.html'),
18481854
{ search: 'window-open-external' }
18491855
)
18501856

@@ -1921,7 +1927,11 @@ describe('BrowserWindow module', () => {
19211927
prefs.foo = 'bar'
19221928
})
19231929
w.loadFile(path.join(fixtures, 'api', 'new-window.html'))
1924-
const [, , webPreferences] = await emittedOnce(ipcMain, 'answer')
1930+
const [[, childWebContents]] = await Promise.all([
1931+
emittedOnce(app, 'web-contents-created'),
1932+
emittedOnce(ipcMain, 'answer')
1933+
])
1934+
const webPreferences = (childWebContents as any).getLastWebPreferences()
19251935
expect(webPreferences.foo).to.equal('bar')
19261936
})
19271937

@@ -1952,7 +1962,7 @@ describe('BrowserWindow module', () => {
19521962
'parent-answer',
19531963
'child-answer'
19541964
], done)
1955-
w.loadFile(path.join(fixtures, 'api', 'sandbox.html'), { search: 'verify-ipc-sender' })
1965+
w.loadFile(path.join(__dirname, 'fixtures', 'api', 'sandbox.html'), { search: 'verify-ipc-sender' })
19561966
})
19571967

19581968
describe('event handling', () => {
@@ -1988,7 +1998,7 @@ describe('BrowserWindow module', () => {
19881998
'did-frame-finish-load',
19891999
'dom-ready'
19902000
], done)
1991-
w.loadFile(path.join(fixtures, 'api', 'sandbox.html'), { search: 'webcontents-events' })
2001+
w.loadFile(path.join(__dirname, 'fixtures', 'api', 'sandbox.html'), { search: 'webcontents-events' })
19922002
})
19932003
})
19942004

@@ -2021,7 +2031,7 @@ describe('BrowserWindow module', () => {
20212031
sandbox: true
20222032
}
20232033
})
2024-
w.loadFile(path.join(fixtures, 'api', 'sandbox.html'), { search: 'reload-remote' })
2034+
w.loadFile(path.join(__dirname, 'fixtures', 'api', 'sandbox.html'), { search: 'reload-remote' })
20252035

20262036
ipcMain.on('get-remote-module-path', (event) => {
20272037
event.returnValue = path.join(fixtures, 'module', 'hello.js')
@@ -2057,7 +2067,7 @@ describe('BrowserWindow module', () => {
20572067
options.webPreferences!.preload = preload
20582068
})
20592069

2060-
w.loadFile(path.join(fixtures, 'api', 'sandbox.html'), { search: 'reload-remote-child' })
2070+
w.loadFile(path.join(__dirname, 'fixtures', 'api', 'sandbox.html'), { search: 'reload-remote-child' })
20612071

20622072
ipcMain.on('get-remote-module-path', (event) => {
20632073
event.returnValue = path.join(fixtures, 'module', 'hello-child.js')
@@ -2233,7 +2243,11 @@ describe('BrowserWindow module', () => {
22332243
prefs.foo = 'bar'
22342244
})
22352245
w.loadFile(path.join(fixtures, 'api', 'new-window.html'))
2236-
const [, , webPreferences] = await emittedOnce(ipcMain, 'answer')
2246+
const [[, childWebContents]] = await Promise.all([
2247+
emittedOnce(app, 'web-contents-created'),
2248+
emittedOnce(ipcMain, 'answer')
2249+
])
2250+
const webPreferences = (childWebContents as any).getLastWebPreferences()
22372251
expect(webPreferences.foo).to.equal('bar')
22382252
})
22392253
it('should have nodeIntegration disabled in child windows', async () => {
@@ -2359,35 +2373,41 @@ describe('BrowserWindow module', () => {
23592373
beforeEach(() => {
23602374
w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true } })
23612375
})
2376+
afterEach(() => {
2377+
ipcMain.removeAllListeners('onbeforeunload')
2378+
})
23622379
afterEach(closeAllWindows)
23632380
it('returning undefined would not prevent close', (done) => {
23642381
w.once('closed', () => { done() })
2365-
w.loadFile(path.join(fixtures, 'api', 'close-beforeunload-undefined.html'))
2382+
w.loadFile(path.join(__dirname, 'fixtures', 'api', 'close-beforeunload-undefined.html'))
23662383
})
2367-
it('returning false would prevent close', (done) => {
2368-
w.once('onbeforeunload' as any, () => { done() })
2369-
w.loadFile(path.join(fixtures, 'api', 'close-beforeunload-false.html'))
2384+
it('returning false would prevent close', async () => {
2385+
w.loadFile(path.join(__dirname, 'fixtures', 'api', 'close-beforeunload-false.html'))
2386+
const [e] = await emittedOnce(ipcMain, 'onbeforeunload')
2387+
e.returnValue = null
23702388
})
23712389
it('returning empty string would prevent close', (done) => {
2372-
w.once('onbeforeunload' as any, () => { done() })
2373-
w.loadFile(path.join(fixtures, 'api', 'close-beforeunload-empty-string.html'))
2390+
ipcMain.once('onbeforeunload', (e) => { e.returnValue = null; done() })
2391+
w.loadFile(path.join(__dirname, 'fixtures', 'api', 'close-beforeunload-empty-string.html'))
23742392
})
23752393
it('emits for each close attempt', (done) => {
23762394
let beforeUnloadCount = 0
2377-
w.on('onbeforeunload' as any, () => {
2395+
ipcMain.on('onbeforeunload', (e) => {
2396+
e.returnValue = null
23782397
beforeUnloadCount += 1
23792398
if (beforeUnloadCount < 3) {
23802399
w.close()
23812400
} else if (beforeUnloadCount === 3) {
23822401
done()
23832402
}
23842403
})
2385-
w.webContents.once('did-finish-load', () => { w.close() })
2386-
w.loadFile(path.join(fixtures, 'api', 'beforeunload-false-prevent3.html'))
2404+
w.webContents.once('did-finish-load', () => { w.webContents.executeJavaScript('window.close()', true) })
2405+
w.loadFile(path.join(__dirname, 'fixtures', 'api', 'beforeunload-false-prevent3.html'))
23872406
})
23882407
it('emits for each reload attempt', (done) => {
23892408
let beforeUnloadCount = 0
2390-
w.on('onbeforeunload' as any, () => {
2409+
ipcMain.on('onbeforeunload', (e) => {
2410+
e.returnValue = null
23912411
beforeUnloadCount += 1
23922412
if (beforeUnloadCount < 3) {
23932413
w.reload()
@@ -2401,11 +2421,12 @@ describe('BrowserWindow module', () => {
24012421
})
24022422
w.reload()
24032423
})
2404-
w.loadFile(path.join(fixtures, 'api', 'beforeunload-false-prevent3.html'))
2424+
w.loadFile(path.join(__dirname, 'fixtures', 'api', 'beforeunload-false-prevent3.html'))
24052425
})
24062426
it('emits for each navigation attempt', (done) => {
24072427
let beforeUnloadCount = 0
2408-
w.on('onbeforeunload' as any, () => {
2428+
ipcMain.on('onbeforeunload', (e) => {
2429+
e.returnValue = null
24092430
beforeUnloadCount += 1
24102431
if (beforeUnloadCount < 3) {
24112432
w.loadURL('about:blank')
@@ -2419,7 +2440,7 @@ describe('BrowserWindow module', () => {
24192440
})
24202441
w.loadURL('about:blank')
24212442
})
2422-
w.loadFile(path.join(fixtures, 'api', 'beforeunload-false-prevent3.html'))
2443+
w.loadFile(path.join(__dirname, 'fixtures', 'api', 'beforeunload-false-prevent3.html'))
24232444
})
24242445
})
24252446

spec-main/api-ipc-main-spec.ts

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,24 +45,6 @@ describe('ipc main module', () => {
4545
})
4646
})
4747

48-
describe('remote objects registry', () => {
49-
it('does not dereference until the render view is deleted (regression)', (done) => {
50-
const w = new BrowserWindow({
51-
show: false,
52-
webPreferences: {
53-
nodeIntegration: true
54-
}
55-
})
56-
57-
ipcMain.once('error-message', (event, message) => {
58-
expect(message).to.match(/^Cannot call method 'getURL' on missing remote object/)
59-
done()
60-
})
61-
62-
w.loadFile(path.join(fixtures, 'api', 'render-view-deleted.html'))
63-
})
64-
})
65-
6648
describe('ipcMain.on', () => {
6749
it('is not used for internals', async () => {
6850
const appPath = path.join(fixtures, 'api', 'ipc-main-listeners')

spec-main/api-remote-spec.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,24 @@ ifdescribe(features.isRemoteModuleEnabled())('remote module', () => {
158158
})
159159
})
160160

161+
describe('remote objects registry', () => {
162+
it('does not dereference until the render view is deleted (regression)', (done) => {
163+
const w = new BrowserWindow({
164+
show: false,
165+
webPreferences: {
166+
nodeIntegration: true
167+
}
168+
})
169+
170+
ipcMain.once('error-message', (event, message) => {
171+
expect(message).to.match(/^Cannot call method 'getURL' on missing remote object/)
172+
done()
173+
})
174+
175+
w.loadFile(path.join(fixtures, 'api', 'render-view-deleted.html'))
176+
})
177+
})
178+
161179
describe('remote listeners', () => {
162180
afterEach(closeAllWindows)
163181

@@ -168,7 +186,7 @@ ifdescribe(features.isRemoteModuleEnabled())('remote module', () => {
168186
nodeIntegration: true
169187
}
170188
})
171-
await w.loadFile(path.join(__dirname, '..', 'spec', 'fixtures', 'api', 'remote-event-handler.html'))
189+
await w.loadFile(path.join(fixtures, 'api', 'remote-event-handler.html'))
172190
w.webContents.reload()
173191
await emittedOnce(w.webContents, 'did-finish-load')
174192

spec-main/api-web-contents-spec.ts

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,20 @@ describe('webContents module', () => {
4747
w.webContents.once('will-prevent-unload', () => {
4848
expect.fail('should not have fired')
4949
})
50-
w.loadFile(path.join(fixturesPath, 'api', 'close-beforeunload-undefined.html'))
50+
w.loadFile(path.join(__dirname, 'fixtures', 'api', 'close-beforeunload-undefined.html'))
5151
})
5252

53-
it('emits if beforeunload returns false', (done) => {
53+
it('emits if beforeunload returns false', async () => {
5454
const w = new BrowserWindow({ show: false })
55-
w.webContents.once('will-prevent-unload', () => done())
56-
w.loadFile(path.join(fixturesPath, 'api', 'close-beforeunload-false.html'))
55+
w.loadFile(path.join(__dirname, 'fixtures', 'api', 'close-beforeunload-false.html'))
56+
await emittedOnce(w.webContents, 'will-prevent-unload')
5757
})
5858

59-
it('supports calling preventDefault on will-prevent-unload events', (done) => {
59+
it('supports calling preventDefault on will-prevent-unload events', async () => {
6060
const w = new BrowserWindow({ show: false })
6161
w.webContents.once('will-prevent-unload', event => event.preventDefault())
62-
w.once('closed', () => done())
63-
w.loadFile(path.join(fixturesPath, 'api', 'close-beforeunload-false.html'))
62+
w.loadFile(path.join(__dirname, 'fixtures', 'api', 'close-beforeunload-false.html'))
63+
await emittedOnce(w, 'closed')
6464
})
6565
})
6666

@@ -695,15 +695,19 @@ describe('webContents module', () => {
695695
describe('focus()', () => {
696696
describe('when the web contents is hidden', () => {
697697
afterEach(closeAllWindows)
698-
it('does not blur the focused window', (done) => {
698+
it('does not blur the focused window', async () => {
699699
const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true } })
700-
ipcMain.once('answer', (event, parentFocused, childFocused) => {
701-
expect(parentFocused).to.be.true()
702-
expect(childFocused).to.be.false()
703-
done()
704-
})
705700
w.show()
706-
w.loadFile(path.join(fixturesPath, 'pages', 'focus-web-contents.html'))
701+
await w.loadURL('about:blank')
702+
w.focus()
703+
const child = new BrowserWindow({ show: false })
704+
child.loadURL('about:blank')
705+
child.webContents.focus()
706+
const currentFocused = w.isFocused()
707+
const childFocused = child.isFocused()
708+
child.close()
709+
expect(currentFocused).to.be.true()
710+
expect(childFocused).to.be.false()
707711
})
708712
})
709713
})

spec/fixtures/api/beforeunload-false-prevent3.html renamed to spec-main/fixtures/api/beforeunload-false-prevent3.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
var unloadPreventedCount = 0;
66
window.onbeforeunload = function() {
77
setTimeout(function() {
8-
require('electron').remote.getCurrentWindow().emit('onbeforeunload');
8+
require('electron').ipcRenderer.sendSync('onbeforeunload')
99
}, 0);
1010
if (unloadPreventedCount < 3) {
1111
unloadPreventedCount++;

spec/fixtures/api/close-beforeunload-empty-string.html renamed to spec-main/fixtures/api/close-beforeunload-empty-string.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
var unloadPrevented = false;
66
window.onbeforeunload = function() {
77
setTimeout(function() {
8-
require('electron').remote.getCurrentWindow().emit('onbeforeunload');
8+
require('electron').ipcRenderer.sendSync('onbeforeunload');
99
}, 0);
1010

1111
if (!unloadPrevented) {

spec/fixtures/api/close-beforeunload-false.html renamed to spec-main/fixtures/api/close-beforeunload-false.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
var unloadPrevented = false;
66
window.onbeforeunload = function() {
77
setTimeout(function() {
8-
require('electron').remote.getCurrentWindow().emit('onbeforeunload');
8+
require('electron').ipcRenderer.sendSync('onbeforeunload');
99
}, 0);
1010
if (!unloadPrevented) {
1111
unloadPrevented = true;
File renamed without changes.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
</head>
6+
<body>
7+
<webview nodeintegration src="./a.html"></webview>
8+
<script>
9+
var wv = document.querySelector('webview')
10+
wv.addEventListener('dom-ready', () => {
11+
wv.openDevTools()
12+
})
13+
</script>
14+
</script>
15+
</body>
16+
</html>

spec-main/webview-spec.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,21 @@ describe('<webview> tag', function () {
164164
const extensionPath = path.join(fixtures, 'devtools-extensions', 'foo')
165165
BrowserWindow.addDevToolsExtension(extensionPath)
166166

167-
w.loadFile(path.join(fixtures, 'pages', 'webview-devtools.html'))
167+
w.loadFile(path.join(__dirname, 'fixtures', 'pages', 'webview-devtools.html'))
168+
app.once('web-contents-created', (e, webContents) => {
169+
webContents.on('devtools-opened', function () {
170+
const showPanelIntervalId = setInterval(function () {
171+
if (!webContents.isDestroyed() && webContents.devToolsWebContents) {
172+
webContents.devToolsWebContents.executeJavaScript('(' + function () {
173+
const lastPanelId: any = (window as any).UI.inspectorView._tabbedPane._tabs.peekLast().id;
174+
(window as any).UI.inspectorView.showPanel(lastPanelId)
175+
}.toString() + ')()')
176+
} else {
177+
clearInterval(showPanelIntervalId)
178+
}
179+
}, 100)
180+
})
181+
})
168182

169183
const [, { runtimeId, tabId }] = await emittedOnce(ipcMain, 'answer')
170184
expect(runtimeId).to.equal('foo')

spec/api-remote-spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ ifdescribe(features.isRemoteModuleEnabled())('remote module', () => {
233233
})
234234

235235
describe('remote value in browser', () => {
236-
const print = path.join(fixtures, 'module', 'print_name.js')
236+
const print = path.join(__dirname, '..', 'spec-main', 'fixtures', 'module', 'print_name.js')
237237
const printName = remote.require(print)
238238

239239
it('preserves NaN', () => {

0 commit comments

Comments
 (0)