2019-07-15 11:23:59 +03:00
|
|
|
import { expect } from 'chai';
|
2023-06-15 17:42:27 +03:00
|
|
|
import * as path from 'node:path';
|
|
|
|
import * as cp from 'node:child_process';
|
2023-01-26 00:01:25 +03:00
|
|
|
import { closeAllWindows } from './lib/window-helpers';
|
|
|
|
import { defer } from './lib/spec-helpers';
|
2020-04-07 03:04:09 +03:00
|
|
|
import { ipcMain, BrowserWindow } from 'electron/main';
|
2023-06-15 17:42:27 +03:00
|
|
|
import { once } from 'node:events';
|
2017-12-01 23:57:41 +03:00
|
|
|
|
2017-12-02 00:01:03 +03:00
|
|
|
describe('ipc main module', () => {
|
2020-01-09 22:50:56 +03:00
|
|
|
const fixtures = path.join(__dirname, 'fixtures');
|
2017-12-01 23:57:41 +03:00
|
|
|
|
2019-07-15 11:23:59 +03:00
|
|
|
afterEach(closeAllWindows);
|
2017-12-01 23:57:41 +03:00
|
|
|
|
|
|
|
describe('ipc.sendSync', () => {
|
|
|
|
afterEach(() => { ipcMain.removeAllListeners('send-sync-message'); });
|
|
|
|
|
|
|
|
it('does not crash when reply is not sent and browser is destroyed', (done) => {
|
2019-07-15 11:23:59 +03:00
|
|
|
const w = new BrowserWindow({
|
2019-01-07 22:19:27 +03:00
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
2021-03-02 00:52:29 +03:00
|
|
|
nodeIntegration: true,
|
|
|
|
contextIsolation: false
|
2019-01-07 22:19:27 +03:00
|
|
|
}
|
|
|
|
});
|
2017-12-01 23:57:41 +03:00
|
|
|
ipcMain.once('send-sync-message', (event) => {
|
|
|
|
event.returnValue = null;
|
|
|
|
done();
|
|
|
|
});
|
2018-09-04 17:50:53 +03:00
|
|
|
w.loadFile(path.join(fixtures, 'api', 'send-sync-message.html'));
|
2017-12-01 23:57:41 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('does not crash when reply is sent by multiple listeners', (done) => {
|
2019-07-15 11:23:59 +03:00
|
|
|
const w = new BrowserWindow({
|
2019-01-07 22:19:27 +03:00
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
2021-03-02 00:52:29 +03:00
|
|
|
nodeIntegration: true,
|
|
|
|
contextIsolation: false
|
2019-01-07 22:19:27 +03:00
|
|
|
}
|
|
|
|
});
|
2017-12-01 23:57:41 +03:00
|
|
|
ipcMain.on('send-sync-message', (event) => {
|
|
|
|
event.returnValue = null;
|
|
|
|
});
|
|
|
|
ipcMain.on('send-sync-message', (event) => {
|
|
|
|
event.returnValue = null;
|
|
|
|
done();
|
|
|
|
});
|
2018-09-04 17:50:53 +03:00
|
|
|
w.loadFile(path.join(fixtures, 'api', 'send-sync-message.html'));
|
2017-12-01 23:57:41 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-10-06 14:48:00 +03:00
|
|
|
describe('ipcMain.on', () => {
|
|
|
|
it('is not used for internals', async () => {
|
2019-07-15 11:23:59 +03:00
|
|
|
const appPath = path.join(fixtures, 'api', 'ipc-main-listeners');
|
|
|
|
const electronPath = process.execPath;
|
2018-10-06 14:48:00 +03:00
|
|
|
const appProcess = cp.spawn(electronPath, [appPath]);
|
|
|
|
|
|
|
|
let output = '';
|
|
|
|
appProcess.stdout.on('data', (data) => { output += data; });
|
|
|
|
|
2023-02-24 02:53:53 +03:00
|
|
|
await once(appProcess.stdout, 'end');
|
2018-10-06 14:48:00 +03:00
|
|
|
|
|
|
|
output = JSON.parse(output);
|
|
|
|
expect(output).to.deep.equal(['error']);
|
|
|
|
});
|
2020-12-09 23:48:16 +03:00
|
|
|
|
|
|
|
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: {
|
2021-03-02 00:52:29 +03:00
|
|
|
nodeIntegration: true,
|
|
|
|
contextIsolation: false
|
2020-12-09 23:48:16 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
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');
|
|
|
|
});
|
2018-10-06 14:48:00 +03:00
|
|
|
});
|
2017-12-01 23:57:41 +03:00
|
|
|
});
|