electron/spec/api-ipc-main-spec.ts
Jeremy Rose db7c92fd57
test: drop now-empty remote runner (#35343)
* test: drop the now-empty remote runner from CI

* move fixtures to spec-main

* remove remote runner

* fix stuff

* remove global-paths hack

* move ts-smoke to spec/

* fix test after merge

* rename spec-main to spec

* no need to ignore spec/node_modules twice

* simplify spec-runner a little

* no need to hash pj/yl twice

* undo lint change to verify-mksnapshot.py

* excessive ..

* update electron_woa_testing.yml

* don't search for test-results-remote.xml

it is never produced now
2022-08-16 15:23:13 -04:00

92 lines
2.7 KiB
TypeScript

import { expect } from 'chai';
import * as path from 'path';
import * as cp from 'child_process';
import { closeAllWindows } from './window-helpers';
import { emittedOnce } from './events-helpers';
import { defer } from './spec-helpers';
import { ipcMain, BrowserWindow } from 'electron/main';
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 emittedOnce(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');
});
});
});