|
@@ -1,12 +1,9 @@
|
|
|
import { expect } from 'chai';
|
|
|
-import * as path from 'node:path';
|
|
|
-import { ipcMain, BrowserWindow, WebContents, WebPreferences, webContents } from 'electron/main';
|
|
|
+import { ipcMain, BrowserWindow } from 'electron/main';
|
|
|
import { closeWindow } from './lib/window-helpers';
|
|
|
import { once } from 'node:events';
|
|
|
|
|
|
describe('ipcRenderer module', () => {
|
|
|
- const fixtures = path.join(__dirname, 'fixtures');
|
|
|
-
|
|
|
let w: BrowserWindow;
|
|
|
before(async () => {
|
|
|
w = new BrowserWindow({
|
|
@@ -135,81 +132,6 @@ describe('ipcRenderer module', () => {
|
|
|
});
|
|
|
});
|
|
|
|
|
|
- describe('sendTo()', () => {
|
|
|
- const generateSpecs = (description: string, webPreferences: WebPreferences) => {
|
|
|
- describe(description, () => {
|
|
|
- let contents: WebContents;
|
|
|
- const payload = 'Hello World!';
|
|
|
-
|
|
|
- before(async () => {
|
|
|
- contents = (webContents as typeof ElectronInternal.WebContents).create({
|
|
|
- preload: path.join(fixtures, 'module', 'preload-ipc-ping-pong.js'),
|
|
|
- ...webPreferences
|
|
|
- });
|
|
|
-
|
|
|
- await contents.loadURL('about:blank');
|
|
|
- });
|
|
|
-
|
|
|
- after(() => {
|
|
|
- contents.destroy();
|
|
|
- contents = null as unknown as WebContents;
|
|
|
- });
|
|
|
-
|
|
|
- it('sends message to WebContents', async () => {
|
|
|
- const data = await w.webContents.executeJavaScript(`new Promise(resolve => {
|
|
|
- const { ipcRenderer } = require('electron')
|
|
|
- ipcRenderer.sendTo(${contents.id}, 'ping', ${JSON.stringify(payload)})
|
|
|
- ipcRenderer.once('pong', (event, data) => resolve(data))
|
|
|
- })`);
|
|
|
- expect(data.payload).to.equal(payload);
|
|
|
- expect(data.senderIsMainFrame).to.be.true();
|
|
|
- });
|
|
|
-
|
|
|
- it('sends message to WebContents from a child frame', async () => {
|
|
|
- const frameCreated = once(w.webContents, 'frame-created') as Promise<[any, Electron.FrameCreatedDetails]>;
|
|
|
-
|
|
|
- const promise = w.webContents.executeJavaScript(`new Promise(resolve => {
|
|
|
- const iframe = document.createElement('iframe');
|
|
|
- iframe.src = 'data:text/html,';
|
|
|
- iframe.name = 'iframe';
|
|
|
- document.body.appendChild(iframe);
|
|
|
-
|
|
|
- const { ipcRenderer } = require('electron');
|
|
|
- ipcRenderer.once('pong', (event, data) => resolve(data));
|
|
|
- })`);
|
|
|
-
|
|
|
- const [, details] = await frameCreated;
|
|
|
- expect(details.frame.name).to.equal('iframe');
|
|
|
-
|
|
|
- await once(details.frame, 'dom-ready');
|
|
|
-
|
|
|
- details.frame.executeJavaScript(`new Promise(resolve => {
|
|
|
- const { ipcRenderer } = require('electron');
|
|
|
- ipcRenderer.sendTo(${contents.id}, 'ping', ${JSON.stringify(payload)});
|
|
|
- })`);
|
|
|
-
|
|
|
- const data = await promise;
|
|
|
- expect(data.payload).to.equal(payload);
|
|
|
- expect(data.senderIsMainFrame).to.be.false();
|
|
|
- });
|
|
|
-
|
|
|
- it('sends message on channel with non-ASCII characters to WebContents', async () => {
|
|
|
- const data = await w.webContents.executeJavaScript(`new Promise(resolve => {
|
|
|
- const { ipcRenderer } = require('electron')
|
|
|
- ipcRenderer.sendTo(${contents.id}, 'ping-æøåü', ${JSON.stringify(payload)})
|
|
|
- ipcRenderer.once('pong-æøåü', (event, data) => resolve(data))
|
|
|
- })`);
|
|
|
- expect(data).to.equal(payload);
|
|
|
- });
|
|
|
- });
|
|
|
- };
|
|
|
-
|
|
|
- generateSpecs('without sandbox', {});
|
|
|
- generateSpecs('with sandbox', { sandbox: true });
|
|
|
- generateSpecs('with contextIsolation', { contextIsolation: true });
|
|
|
- generateSpecs('with contextIsolation + sandbox', { contextIsolation: true, sandbox: true });
|
|
|
- });
|
|
|
-
|
|
|
describe('ipcRenderer.on', () => {
|
|
|
it('is not used for internals', async () => {
|
|
|
const result = await w.webContents.executeJavaScript(`
|