forked from electron/electron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi-ipc-main-spec.ts
92 lines (81 loc) · 2.69 KB
/
api-ipc-main-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
import { expect } from 'chai';
import * as path from 'node:path';
import * as cp from 'node:child_process';
import { closeAllWindows } from './lib/window-helpers';
import { defer } from './lib/spec-helpers';
import { ipcMain, BrowserWindow } from 'electron/main';
import { once } from 'node:events';
describe('ipc main module', () => {
const fixtures = path.join(__dirname, 'fixtures');
afterEach(closeAllWindows);
describe('ipc.sendSync', () => {
afterEach(() => { ipcMain.removeAllListeners('send-sync-message'); });
it('does not crash when reply is not sent and browser is destroyed', (done) => {
const w = new BrowserWindow({
show: false,
webPreferences: {
nodeIntegration: true,
contextIsolation: false
}
});
ipcMain.once('send-sync-message', (event) => {
event.returnValue = null;
done();
});
w.loadFile(path.join(fixtures, 'api', 'send-sync-message.html'));
});
it('does not crash when reply is sent by multiple listeners', (done) => {
const w = new BrowserWindow({
show: false,
webPreferences: {
nodeIntegration: true,
contextIsolation: false
}
});
ipcMain.on('send-sync-message', (event) => {
event.returnValue = null;
});
ipcMain.on('send-sync-message', (event) => {
event.returnValue = null;
done();
});
w.loadFile(path.join(fixtures, 'api', 'send-sync-message.html'));
});
});
describe('ipcMain.on', () => {
it('is not used for internals', async () => {
const appPath = path.join(fixtures, 'api', 'ipc-main-listeners');
const electronPath = process.execPath;
const appProcess = cp.spawn(electronPath, [appPath]);
let output = '';
appProcess.stdout.on('data', (data) => { output += data; });
await once(appProcess.stdout, 'end');
output = JSON.parse(output);
expect(output).to.deep.equal(['error']);
});
it('can be replied to', async () => {
ipcMain.on('test-echo', (e, arg) => {
e.reply('test-echo', arg);
});
defer(() => {
ipcMain.removeAllListeners('test-echo');
});
const w = new BrowserWindow({
show: false,
webPreferences: {
nodeIntegration: true,
contextIsolation: false
}
});
w.loadURL('about:blank');
const v = await w.webContents.executeJavaScript(`new Promise((resolve, reject) => {
const { ipcRenderer } = require('electron')
ipcRenderer.send('test-echo', 'hello')
ipcRenderer.on('test-echo', (e, v) => {
resolve(v)
})
})`);
expect(v).to.equal('hello');
});
});
});