2020-03-20 20:28:31 +00:00
|
|
|
import * as path from 'path';
|
|
|
|
import { expect } from 'chai';
|
|
|
|
import { closeAllWindows } from './window-helpers';
|
|
|
|
import { ifdescribe } from './spec-helpers';
|
2019-06-07 17:43:32 +00:00
|
|
|
|
2020-04-07 00:04:09 +00:00
|
|
|
import { ipcMain, BrowserWindow } from 'electron/main';
|
2020-03-20 20:28:31 +00:00
|
|
|
import { emittedOnce } from './events-helpers';
|
2020-05-18 16:29:24 +00:00
|
|
|
import { NativeImage } from 'electron/common';
|
2020-05-20 20:42:42 +00:00
|
|
|
import { serialize, deserialize } from '../lib/common/type-utils';
|
|
|
|
import { nativeImage } from 'electron';
|
2019-06-07 17:43:32 +00:00
|
|
|
|
2020-06-23 03:32:45 +00:00
|
|
|
const features = process._linkedBinding('electron_common_features');
|
2019-09-18 16:52:06 +00:00
|
|
|
|
2019-12-09 18:27:30 +00:00
|
|
|
const expectPathsEqual = (path1: string, path2: string) => {
|
|
|
|
if (process.platform === 'win32') {
|
2020-03-20 20:28:31 +00:00
|
|
|
path1 = path1.toLowerCase();
|
|
|
|
path2 = path2.toLowerCase();
|
2019-12-09 18:27:30 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(path1).to.equal(path2);
|
|
|
|
};
|
2019-06-07 17:43:32 +00:00
|
|
|
|
2019-12-09 18:27:30 +00:00
|
|
|
function makeRemotely (windowGetter: () => BrowserWindow) {
|
|
|
|
async function remotely (script: Function, ...args: any[]) {
|
|
|
|
// executeJavaScript obfuscates the error if the script throws, so catch any
|
|
|
|
// errors manually.
|
|
|
|
const assembledScript = `(async function() {
|
2019-06-07 17:43:32 +00:00
|
|
|
try {
|
2019-12-09 18:27:30 +00:00
|
|
|
return { result: await Promise.resolve((${script})(...${JSON.stringify(args)})) }
|
2019-06-07 17:43:32 +00:00
|
|
|
} catch (e) {
|
2019-12-09 18:27:30 +00:00
|
|
|
return { error: e.message, stack: e.stack }
|
2019-06-07 17:43:32 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
})()`;
|
|
|
|
const { result, error, stack } = await windowGetter().webContents.executeJavaScript(assembledScript);
|
2019-12-09 18:27:30 +00:00
|
|
|
if (error) {
|
2020-03-20 20:28:31 +00:00
|
|
|
const e = new Error(error);
|
|
|
|
e.stack = stack;
|
|
|
|
throw e;
|
2019-06-07 17:43:32 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
return result;
|
2019-12-09 18:27:30 +00:00
|
|
|
}
|
|
|
|
remotely.it = (...vars: any[]) => (name: string, fn: Function) => {
|
|
|
|
it(name, async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
await remotely(fn, ...vars);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
return remotely;
|
2019-12-09 18:27:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function makeWindow () {
|
2020-03-20 20:28:31 +00:00
|
|
|
let w: BrowserWindow;
|
2019-12-09 18:27:30 +00:00
|
|
|
before(async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, enableRemoteModule: true } });
|
|
|
|
await w.loadURL('about:blank');
|
2019-12-18 06:24:50 +00:00
|
|
|
await w.webContents.executeJavaScript(`{
|
2019-12-09 18:27:30 +00:00
|
|
|
const chai_1 = window.chai_1 = require('chai')
|
|
|
|
chai_1.use(require('chai-as-promised'))
|
|
|
|
chai_1.use(require('dirty-chai'))
|
2019-12-18 06:24:50 +00:00
|
|
|
null
|
2020-03-20 20:28:31 +00:00
|
|
|
}`);
|
|
|
|
});
|
|
|
|
after(closeAllWindows);
|
|
|
|
return () => w;
|
2019-12-09 18:27:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function makeEachWindow () {
|
2020-03-20 20:28:31 +00:00
|
|
|
let w: BrowserWindow;
|
2019-12-09 18:27:30 +00:00
|
|
|
beforeEach(async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, enableRemoteModule: true } });
|
|
|
|
await w.loadURL('about:blank');
|
2019-12-18 06:24:50 +00:00
|
|
|
await w.webContents.executeJavaScript(`{
|
2019-12-09 18:27:30 +00:00
|
|
|
const chai_1 = window.chai_1 = require('chai')
|
|
|
|
chai_1.use(require('chai-as-promised'))
|
|
|
|
chai_1.use(require('dirty-chai'))
|
2019-12-18 06:24:50 +00:00
|
|
|
null
|
2020-03-20 20:28:31 +00:00
|
|
|
}`);
|
|
|
|
});
|
|
|
|
afterEach(closeAllWindows);
|
|
|
|
return () => w;
|
2019-12-09 18:27:30 +00:00
|
|
|
}
|
|
|
|
|
2020-05-20 20:42:42 +00:00
|
|
|
describe('typeUtils serialization/deserialization', () => {
|
|
|
|
it('serializes and deserializes an empty NativeImage', () => {
|
|
|
|
const image = nativeImage.createEmpty();
|
|
|
|
const serializedImage = serialize(image);
|
|
|
|
const empty = deserialize(serializedImage);
|
|
|
|
|
|
|
|
expect(empty.isEmpty()).to.be.true();
|
|
|
|
expect(empty.getAspectRatio()).to.equal(1);
|
|
|
|
expect(empty.toDataURL()).to.equal('data:image/png;base64,');
|
|
|
|
expect(empty.toDataURL({ scaleFactor: 2.0 })).to.equal('data:image/png;base64,');
|
|
|
|
expect(empty.getSize()).to.deep.equal({ width: 0, height: 0 });
|
|
|
|
expect(empty.getBitmap()).to.be.empty();
|
|
|
|
expect(empty.getBitmap({ scaleFactor: 2.0 })).to.be.empty();
|
|
|
|
expect(empty.toBitmap()).to.be.empty();
|
|
|
|
expect(empty.toBitmap({ scaleFactor: 2.0 })).to.be.empty();
|
|
|
|
expect(empty.toJPEG(100)).to.be.empty();
|
|
|
|
expect(empty.toPNG()).to.be.empty();
|
|
|
|
expect(empty.toPNG({ scaleFactor: 2.0 })).to.be.empty();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('serializes and deserializes a non-empty NativeImage', () => {
|
|
|
|
const dataURL = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAIAAAD91JpzAAAAFklEQVQYlWP8//8/AwMDEwMDAwMDAwAkBgMBBMzldwAAAABJRU5ErkJggg==';
|
|
|
|
const image = nativeImage.createFromDataURL(dataURL);
|
|
|
|
const serializedImage = serialize(image);
|
|
|
|
const nonEmpty = deserialize(serializedImage);
|
|
|
|
|
|
|
|
expect(nonEmpty.isEmpty()).to.be.false();
|
|
|
|
expect(nonEmpty.getAspectRatio()).to.equal(1);
|
|
|
|
expect(nonEmpty.toDataURL()).to.not.be.empty();
|
|
|
|
expect(nonEmpty.toDataURL({ scaleFactor: 1.0 })).to.equal(dataURL);
|
|
|
|
expect(nonEmpty.getSize()).to.deep.equal({ width: 2, height: 2 });
|
|
|
|
expect(nonEmpty.getBitmap()).to.not.be.empty();
|
|
|
|
expect(nonEmpty.toPNG()).to.not.be.empty();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('serializes and deserializes a non-empty NativeImage with multiple representations', () => {
|
|
|
|
const image = nativeImage.createEmpty();
|
|
|
|
|
|
|
|
const dataURL1 = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAC0lEQVQYlWNgAAIAAAUAAdafFs0AAAAASUVORK5CYII=';
|
|
|
|
image.addRepresentation({ scaleFactor: 1.0, dataURL: dataURL1 });
|
|
|
|
|
|
|
|
const dataURL2 = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAIAAAD91JpzAAAAFklEQVQYlWP8//8/AwMDEwMDAwMDAwAkBgMBBMzldwAAAABJRU5ErkJggg==';
|
|
|
|
image.addRepresentation({ scaleFactor: 2.0, dataURL: dataURL2 });
|
|
|
|
|
|
|
|
const serializedImage = serialize(image);
|
|
|
|
const nonEmpty = deserialize(serializedImage);
|
|
|
|
|
|
|
|
expect(nonEmpty.isEmpty()).to.be.false();
|
|
|
|
expect(nonEmpty.getAspectRatio()).to.equal(1);
|
|
|
|
expect(nonEmpty.getSize()).to.deep.equal({ width: 1, height: 1 });
|
|
|
|
expect(nonEmpty.getBitmap()).to.not.be.empty();
|
|
|
|
expect(nonEmpty.getBitmap({ scaleFactor: 1.0 })).to.not.be.empty();
|
|
|
|
expect(nonEmpty.getBitmap({ scaleFactor: 2.0 })).to.not.be.empty();
|
|
|
|
expect(nonEmpty.toBitmap()).to.not.be.empty();
|
|
|
|
expect(nonEmpty.toBitmap({ scaleFactor: 1.0 })).to.not.be.empty();
|
|
|
|
expect(nonEmpty.toBitmap({ scaleFactor: 2.0 })).to.not.be.empty();
|
|
|
|
expect(nonEmpty.toPNG()).to.not.be.empty();
|
|
|
|
expect(nonEmpty.toPNG({ scaleFactor: 1.0 })).to.not.be.empty();
|
|
|
|
expect(nonEmpty.toPNG({ scaleFactor: 2.0 })).to.not.be.empty();
|
|
|
|
expect(nonEmpty.toDataURL()).to.not.be.empty();
|
|
|
|
expect(nonEmpty.toDataURL({ scaleFactor: 1.0 })).to.equal(dataURL1);
|
|
|
|
expect(nonEmpty.toDataURL({ scaleFactor: 2.0 })).to.equal(dataURL2);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('serializes and deserializes an Array', () => {
|
|
|
|
const array = [1, 2, 3, 4, 5];
|
|
|
|
const serialized = serialize(array);
|
|
|
|
const deserialized = deserialize(serialized);
|
|
|
|
|
|
|
|
expect(deserialized).to.deep.equal(array);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('serializes and deserializes a Buffer', () => {
|
|
|
|
const buffer = Buffer.from('hello world!', 'utf-8');
|
|
|
|
const serialized = serialize(buffer);
|
|
|
|
const deserialized = deserialize(serialized);
|
|
|
|
|
|
|
|
expect(deserialized).to.deep.equal(buffer);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('serializes and deserializes a Boolean', () => {
|
|
|
|
const bool = true;
|
|
|
|
const serialized = serialize(bool);
|
|
|
|
const deserialized = deserialize(serialized);
|
|
|
|
|
|
|
|
expect(deserialized).to.equal(bool);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('serializes and deserializes a Date', () => {
|
|
|
|
const date = new Date();
|
|
|
|
const serialized = serialize(date);
|
|
|
|
const deserialized = deserialize(serialized);
|
|
|
|
|
|
|
|
expect(deserialized).to.equal(date);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('serializes and deserializes a Number', () => {
|
|
|
|
const number = 42;
|
|
|
|
const serialized = serialize(number);
|
|
|
|
const deserialized = deserialize(serialized);
|
|
|
|
|
|
|
|
expect(deserialized).to.equal(number);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('serializes and deserializes a Regexp', () => {
|
|
|
|
const regex = new RegExp('ab+c');
|
|
|
|
const serialized = serialize(regex);
|
|
|
|
const deserialized = deserialize(serialized);
|
|
|
|
|
|
|
|
expect(deserialized).to.equal(regex);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('serializes and deserializes a String', () => {
|
|
|
|
const str = 'hello world';
|
|
|
|
const serialized = serialize(str);
|
|
|
|
const deserialized = deserialize(serialized);
|
|
|
|
|
|
|
|
expect(deserialized).to.equal(str);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('serializes and deserializes an Error', () => {
|
|
|
|
const err = new Error('oh crap');
|
|
|
|
const serialized = serialize(err);
|
|
|
|
const deserialized = deserialize(serialized);
|
|
|
|
|
|
|
|
expect(deserialized).to.equal(err);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('serializes and deserializes a simple Object', () => {
|
|
|
|
const obj = { hello: 'world', 'answer-to-everything': 42 };
|
|
|
|
const serialized = serialize(obj);
|
|
|
|
const deserialized = deserialize(serialized);
|
|
|
|
|
|
|
|
expect(deserialized).to.deep.equal(obj);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-12-09 18:27:30 +00:00
|
|
|
ifdescribe(features.isRemoteModuleEnabled())('remote module', () => {
|
2020-10-05 01:22:35 +00:00
|
|
|
const fixtures = path.join(__dirname, 'fixtures', 'remote');
|
2019-12-09 18:27:30 +00:00
|
|
|
|
|
|
|
describe('', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const w = makeWindow();
|
|
|
|
const remotely = makeRemotely(w);
|
2019-06-07 17:43:32 +00:00
|
|
|
|
2019-10-29 04:16:42 +00:00
|
|
|
describe('remote.getGlobal filtering', () => {
|
|
|
|
it('can return custom values', async () => {
|
2019-12-09 18:27:30 +00:00
|
|
|
w().webContents.once('remote-get-global', (event, name) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
event.returnValue = name;
|
|
|
|
});
|
|
|
|
expect(await remotely(() => require('electron').remote.getGlobal('test'))).to.equal('test');
|
|
|
|
});
|
2019-06-07 17:43:32 +00:00
|
|
|
|
2019-10-29 04:16:42 +00:00
|
|
|
it('throws when no returnValue set', async () => {
|
2019-12-09 18:27:30 +00:00
|
|
|
w().webContents.once('remote-get-global', (event) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
event.preventDefault();
|
|
|
|
});
|
|
|
|
await expect(remotely(() => require('electron').remote.getGlobal('test'))).to.eventually.be.rejected('Blocked remote.getGlobal(\'test\')');
|
|
|
|
});
|
|
|
|
});
|
2019-06-07 17:43:32 +00:00
|
|
|
|
2019-10-29 04:16:42 +00:00
|
|
|
describe('remote.getBuiltin filtering', () => {
|
|
|
|
it('can return custom values', async () => {
|
2019-12-09 18:27:30 +00:00
|
|
|
w().webContents.once('remote-get-builtin', (event, name) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
event.returnValue = name;
|
|
|
|
});
|
|
|
|
expect(await remotely(() => (require('electron').remote as any).getBuiltin('test'))).to.equal('test');
|
|
|
|
});
|
2019-06-07 17:43:32 +00:00
|
|
|
|
2019-10-29 04:16:42 +00:00
|
|
|
it('throws when no returnValue set', async () => {
|
2019-12-09 18:27:30 +00:00
|
|
|
w().webContents.once('remote-get-builtin', (event) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
event.preventDefault();
|
|
|
|
});
|
|
|
|
await expect(remotely(() => (require('electron').remote as any).getBuiltin('test'))).to.eventually.be.rejected('Blocked remote.getGlobal(\'test\')');
|
|
|
|
});
|
|
|
|
});
|
2019-06-07 17:43:32 +00:00
|
|
|
|
2019-10-29 04:16:42 +00:00
|
|
|
describe('remote.require filtering', () => {
|
|
|
|
it('can return custom values', async () => {
|
2019-12-09 18:27:30 +00:00
|
|
|
w().webContents.once('remote-require', (event, name) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
event.returnValue = name;
|
|
|
|
});
|
|
|
|
expect(await remotely(() => require('electron').remote.require('test'))).to.equal('test');
|
|
|
|
});
|
2019-06-07 17:43:32 +00:00
|
|
|
|
2019-10-29 04:16:42 +00:00
|
|
|
it('throws when no returnValue set', async () => {
|
2019-12-09 18:27:30 +00:00
|
|
|
w().webContents.once('remote-require', (event) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
event.preventDefault();
|
|
|
|
});
|
|
|
|
await expect(remotely(() => require('electron').remote.require('test'))).to.eventually.be.rejected('Blocked remote.require(\'test\')');
|
|
|
|
});
|
|
|
|
});
|
2019-06-07 17:43:32 +00:00
|
|
|
|
2019-10-29 04:16:42 +00:00
|
|
|
describe('remote.getCurrentWindow filtering', () => {
|
|
|
|
it('can return custom value', async () => {
|
2019-12-09 18:27:30 +00:00
|
|
|
w().webContents.once('remote-get-current-window', (e) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
e.returnValue = 'some window';
|
|
|
|
});
|
|
|
|
expect(await remotely(() => require('electron').remote.getCurrentWindow())).to.equal('some window');
|
|
|
|
});
|
2019-06-07 17:43:32 +00:00
|
|
|
|
2019-10-29 04:16:42 +00:00
|
|
|
it('throws when no returnValue set', async () => {
|
2019-12-09 18:27:30 +00:00
|
|
|
w().webContents.once('remote-get-current-window', (event) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
event.preventDefault();
|
|
|
|
});
|
|
|
|
await expect(remotely(() => require('electron').remote.getCurrentWindow())).to.eventually.be.rejected('Blocked remote.getCurrentWindow()');
|
|
|
|
});
|
|
|
|
});
|
2019-06-07 17:43:32 +00:00
|
|
|
|
2019-10-29 04:16:42 +00:00
|
|
|
describe('remote.getCurrentWebContents filtering', () => {
|
|
|
|
it('can return custom value', async () => {
|
2019-12-09 18:27:30 +00:00
|
|
|
w().webContents.once('remote-get-current-web-contents', (event) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
event.returnValue = 'some web contents';
|
|
|
|
});
|
|
|
|
expect(await remotely(() => require('electron').remote.getCurrentWebContents())).to.equal('some web contents');
|
|
|
|
});
|
2019-10-29 04:16:42 +00:00
|
|
|
|
|
|
|
it('throws when no returnValue set', async () => {
|
2019-12-09 18:27:30 +00:00
|
|
|
w().webContents.once('remote-get-current-web-contents', (event) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
event.preventDefault();
|
|
|
|
});
|
|
|
|
await expect(remotely(() => require('electron').remote.getCurrentWebContents())).to.eventually.be.rejected('Blocked remote.getCurrentWebContents()');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2019-06-07 17:43:32 +00:00
|
|
|
|
2019-12-09 18:27:30 +00:00
|
|
|
describe('remote references', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const w = makeEachWindow();
|
2019-12-09 18:27:30 +00:00
|
|
|
it('render-view-deleted is sent when page is destroyed', (done) => {
|
|
|
|
w().webContents.once('render-view-deleted' as any, () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
w().destroy();
|
|
|
|
});
|
2019-10-29 04:16:42 +00:00
|
|
|
|
2019-12-09 18:27:30 +00:00
|
|
|
// The ELECTRON_BROWSER_CONTEXT_RELEASE message relies on this to work.
|
|
|
|
it('message can be sent on exit when page is being navigated', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
after(() => { ipcMain.removeAllListeners('SENT_ON_EXIT'); });
|
2019-12-09 18:27:30 +00:00
|
|
|
w().webContents.once('did-finish-load', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
w().webContents.loadURL('about:blank');
|
|
|
|
});
|
2020-10-05 01:22:35 +00:00
|
|
|
w().loadFile(path.join(fixtures, 'send-on-exit.html'));
|
2020-03-20 20:28:31 +00:00
|
|
|
await emittedOnce(ipcMain, 'SENT_ON_EXIT');
|
|
|
|
});
|
|
|
|
});
|
2019-10-23 04:44:21 +00:00
|
|
|
|
2019-10-29 04:16:42 +00:00
|
|
|
describe('remote function in renderer', () => {
|
|
|
|
afterEach(() => {
|
2020-03-20 20:28:31 +00:00
|
|
|
ipcMain.removeAllListeners('done');
|
|
|
|
});
|
|
|
|
afterEach(closeAllWindows);
|
2019-10-29 04:16:42 +00:00
|
|
|
|
|
|
|
it('works when created in preload script', async () => {
|
2020-10-05 01:22:35 +00:00
|
|
|
const preload = path.join(fixtures, 'preload-remote-function.js');
|
2019-10-29 04:16:42 +00:00
|
|
|
const w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
2020-02-10 18:49:09 +00:00
|
|
|
preload,
|
|
|
|
enableRemoteModule: true
|
2019-10-29 04:16:42 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
w.loadURL('about:blank');
|
|
|
|
await emittedOnce(ipcMain, 'done');
|
|
|
|
});
|
|
|
|
});
|
2019-10-23 04:44:21 +00:00
|
|
|
|
2019-11-14 22:09:03 +00:00
|
|
|
describe('remote objects registry', () => {
|
2020-06-30 22:10:36 +00:00
|
|
|
it('does not dereference until the render view is deleted (regression)', async () => {
|
2019-11-14 22:09:03 +00:00
|
|
|
const w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
2020-02-10 18:49:09 +00:00
|
|
|
nodeIntegration: true,
|
|
|
|
enableRemoteModule: true
|
2019-11-14 22:09:03 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-11-14 22:09:03 +00:00
|
|
|
|
2020-06-30 22:10:36 +00:00
|
|
|
const message = emittedOnce(ipcMain, 'error-message');
|
2020-10-05 01:22:35 +00:00
|
|
|
w.loadFile(path.join(fixtures, 'render-view-deleted.html'));
|
2020-06-30 22:10:36 +00:00
|
|
|
const [, msg] = await message;
|
|
|
|
expect(msg).to.match(/^Cannot call method 'getURL' on missing remote object/);
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
});
|
2019-11-14 22:09:03 +00:00
|
|
|
|
2020-05-18 16:29:24 +00:00
|
|
|
describe('nativeImage serialization', () => {
|
|
|
|
const w = makeWindow();
|
|
|
|
const remotely = makeRemotely(w);
|
|
|
|
|
2020-05-28 16:43:15 +00:00
|
|
|
it('can serialize an empty nativeImage from renderer to main', async () => {
|
|
|
|
const getImageEmpty = (img: NativeImage) => img.isEmpty();
|
2020-05-18 16:29:24 +00:00
|
|
|
|
|
|
|
w().webContents.once('remote-get-global', (event) => {
|
2020-05-28 16:43:15 +00:00
|
|
|
event.returnValue = getImageEmpty;
|
2020-05-18 16:29:24 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
await expect(remotely(() => {
|
|
|
|
const emptyImage = require('electron').nativeImage.createEmpty();
|
|
|
|
return require('electron').remote.getGlobal('someFunction')(emptyImage);
|
|
|
|
})).to.eventually.be.true();
|
|
|
|
});
|
|
|
|
|
2020-05-28 16:43:15 +00:00
|
|
|
it('can serialize an empty nativeImage from main to renderer', async () => {
|
|
|
|
w().webContents.once('remote-get-global', (event) => {
|
|
|
|
const emptyImage = require('electron').nativeImage.createEmpty();
|
|
|
|
event.returnValue = emptyImage;
|
|
|
|
});
|
|
|
|
|
|
|
|
await expect(remotely(() => {
|
|
|
|
const image = require('electron').remote.getGlobal('someFunction');
|
|
|
|
return image.isEmpty();
|
|
|
|
})).to.eventually.be.true();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can serialize a non-empty nativeImage from renderer to main', async () => {
|
|
|
|
const getImageSize = (img: NativeImage) => img.getSize();
|
2020-05-18 16:29:24 +00:00
|
|
|
|
|
|
|
w().webContents.once('remote-get-global', (event) => {
|
2020-05-28 16:43:15 +00:00
|
|
|
event.returnValue = getImageSize;
|
2020-05-18 16:29:24 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
await expect(remotely(() => {
|
|
|
|
const { nativeImage } = require('electron');
|
|
|
|
const nonEmptyImage = nativeImage.createFromDataURL('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAIAAAD91JpzAAAAFklEQVQYlWP8//8/AwMDEwMDAwMDAwAkBgMBBMzldwAAAABJRU5ErkJggg==');
|
|
|
|
return require('electron').remote.getGlobal('someFunction')(nonEmptyImage);
|
|
|
|
})).to.eventually.deep.equal({ width: 2, height: 2 });
|
|
|
|
});
|
|
|
|
|
2020-05-28 16:43:15 +00:00
|
|
|
it('can serialize a non-empty nativeImage from main to renderer', async () => {
|
|
|
|
w().webContents.once('remote-get-global', (event) => {
|
|
|
|
const nonEmptyImage = nativeImage.createFromDataURL('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAIAAAD91JpzAAAAFklEQVQYlWP8//8/AwMDEwMDAwMDAwAkBgMBBMzldwAAAABJRU5ErkJggg==');
|
|
|
|
event.returnValue = nonEmptyImage;
|
|
|
|
});
|
|
|
|
|
|
|
|
await expect(remotely(() => {
|
|
|
|
const image = require('electron').remote.getGlobal('someFunction');
|
|
|
|
return image.getSize();
|
|
|
|
})).to.eventually.deep.equal({ width: 2, height: 2 });
|
|
|
|
});
|
|
|
|
|
2020-05-18 16:29:24 +00:00
|
|
|
it('can properly create a menu with an nativeImage icon in the renderer', async () => {
|
|
|
|
await expect(remotely(() => {
|
|
|
|
const { remote, nativeImage } = require('electron');
|
|
|
|
remote.Menu.buildFromTemplate([
|
|
|
|
{
|
|
|
|
label: 'hello',
|
|
|
|
icon: nativeImage.createEmpty()
|
|
|
|
}
|
|
|
|
]);
|
|
|
|
})).to.be.fulfilled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-10-29 04:16:42 +00:00
|
|
|
describe('remote listeners', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
afterEach(closeAllWindows);
|
2019-10-29 04:16:42 +00:00
|
|
|
|
|
|
|
it('detaches listeners subscribed to destroyed renderers, and shows a warning', async () => {
|
|
|
|
const w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
2020-02-10 18:49:09 +00:00
|
|
|
nodeIntegration: true,
|
|
|
|
enableRemoteModule: true
|
2019-10-29 04:16:42 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2020-10-05 01:22:35 +00:00
|
|
|
await w.loadFile(path.join(fixtures, 'remote-event-handler.html'));
|
2020-03-20 20:28:31 +00:00
|
|
|
w.webContents.reload();
|
|
|
|
await emittedOnce(w.webContents, 'did-finish-load');
|
2019-10-29 04:16:42 +00:00
|
|
|
|
|
|
|
const expectedMessage = [
|
|
|
|
'Attempting to call a function in a renderer window that has been closed or released.',
|
|
|
|
'Function provided here: remote-event-handler.html:11:33',
|
|
|
|
'Remote event names: remote-handler, other-remote-handler'
|
2020-03-20 20:28:31 +00:00
|
|
|
].join('\n');
|
2019-10-29 04:16:42 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(w.webContents.listenerCount('remote-handler')).to.equal(2);
|
|
|
|
let warnMessage: string | null = null;
|
|
|
|
const originalWarn = console.warn;
|
|
|
|
let warned: Function;
|
2020-02-25 02:11:06 +00:00
|
|
|
const warnPromise = new Promise(resolve => {
|
2020-03-20 20:28:31 +00:00
|
|
|
warned = resolve;
|
|
|
|
});
|
2019-10-29 04:16:42 +00:00
|
|
|
try {
|
2020-02-25 02:11:06 +00:00
|
|
|
console.warn = (message: string) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
warnMessage = message;
|
|
|
|
warned();
|
|
|
|
};
|
|
|
|
w.webContents.emit('remote-handler', { sender: w.webContents });
|
|
|
|
await warnPromise;
|
2019-10-29 04:16:42 +00:00
|
|
|
} finally {
|
2020-03-20 20:28:31 +00:00
|
|
|
console.warn = originalWarn;
|
2019-10-29 04:16:42 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(w.webContents.listenerCount('remote-handler')).to.equal(1);
|
|
|
|
expect(warnMessage).to.equal(expectedMessage);
|
|
|
|
});
|
|
|
|
});
|
2019-12-09 18:27:30 +00:00
|
|
|
|
|
|
|
describe('remote.require', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const w = makeWindow();
|
|
|
|
const remotely = makeRemotely(w);
|
2019-12-09 18:27:30 +00:00
|
|
|
|
|
|
|
remotely.it()('should returns same object for the same module', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const { remote } = require('electron');
|
|
|
|
const a = remote.require('electron');
|
|
|
|
const b = remote.require('electron');
|
|
|
|
expect(a).to.equal(b);
|
|
|
|
});
|
2019-12-09 18:27:30 +00:00
|
|
|
|
2020-10-05 01:22:35 +00:00
|
|
|
remotely.it(path.join(fixtures, 'id.js'))('should work when object contains id property', (module: string) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const { id } = require('electron').remote.require(module);
|
|
|
|
expect(id).to.equal(1127);
|
|
|
|
});
|
2019-12-09 18:27:30 +00:00
|
|
|
|
2020-10-05 01:22:35 +00:00
|
|
|
remotely.it(path.join(fixtures, 'no-prototype.js'))('should work when object has no prototype', (module: string) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const a = require('electron').remote.require(module);
|
|
|
|
expect(a.foo.bar).to.equal('baz');
|
|
|
|
expect(a.foo.baz).to.equal(false);
|
|
|
|
expect(a.bar).to.equal(1234);
|
|
|
|
expect(a.getConstructorName(Object.create(null))).to.equal('');
|
|
|
|
expect(a.getConstructorName(new (class {})())).to.equal('');
|
|
|
|
});
|
2019-12-09 18:27:30 +00:00
|
|
|
|
|
|
|
it('should search module from the user app', async () => {
|
|
|
|
expectPathsEqual(
|
2020-09-17 22:08:57 +00:00
|
|
|
path.normalize(await remotely(() => {
|
|
|
|
const { remote } = require('electron');
|
|
|
|
return (remote as any).process.mainModule.filename;
|
|
|
|
})),
|
2019-12-09 18:27:30 +00:00
|
|
|
path.resolve(__dirname, 'index.js')
|
2020-03-20 20:28:31 +00:00
|
|
|
);
|
2019-12-09 18:27:30 +00:00
|
|
|
expectPathsEqual(
|
2020-09-17 22:08:57 +00:00
|
|
|
path.normalize(await remotely(() => {
|
|
|
|
const { remote } = require('electron');
|
|
|
|
return (remote as any).process.mainModule.paths[0];
|
|
|
|
})),
|
2019-12-09 18:27:30 +00:00
|
|
|
path.resolve(__dirname, 'node_modules')
|
2020-03-20 20:28:31 +00:00
|
|
|
);
|
|
|
|
});
|
2019-12-09 18:27:30 +00:00
|
|
|
|
|
|
|
remotely.it(fixtures)('should work with function properties', (fixtures: string) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const path = require('path');
|
2019-12-09 18:27:30 +00:00
|
|
|
|
|
|
|
{
|
2020-10-05 01:22:35 +00:00
|
|
|
const a = require('electron').remote.require(path.join(fixtures, 'export-function-with-properties.js'));
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(typeof a).to.equal('function');
|
|
|
|
expect(a.bar).to.equal('baz');
|
2019-12-09 18:27:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2020-10-05 01:22:35 +00:00
|
|
|
const a = require('electron').remote.require(path.join(fixtures, 'function-with-properties.js'));
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(typeof a).to.equal('object');
|
|
|
|
expect(a.foo()).to.equal('hello');
|
|
|
|
expect(a.foo.bar).to.equal('baz');
|
|
|
|
expect(a.foo.nested.prop).to.equal('yes');
|
|
|
|
expect(a.foo.method1()).to.equal('world');
|
|
|
|
expect(a.foo.method1.prop1()).to.equal(123);
|
2019-12-09 18:27:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2020-10-05 01:22:35 +00:00
|
|
|
const a = require('electron').remote.require(path.join(fixtures, 'function-with-missing-properties.js')).setup();
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(a.bar()).to.equal(true);
|
|
|
|
expect(a.bar.baz).to.be.undefined();
|
2019-12-09 18:27:30 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-12-09 18:27:30 +00:00
|
|
|
|
|
|
|
remotely.it(fixtures)('should work with static class members', (fixtures: string) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const path = require('path');
|
2020-10-05 01:22:35 +00:00
|
|
|
const a = require('electron').remote.require(path.join(fixtures, 'remote-static.js'));
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(typeof a.Foo).to.equal('function');
|
|
|
|
expect(a.Foo.foo()).to.equal(3);
|
|
|
|
expect(a.Foo.bar).to.equal('baz');
|
|
|
|
expect(new a.Foo().baz()).to.equal(123);
|
|
|
|
});
|
2019-12-09 18:27:30 +00:00
|
|
|
|
|
|
|
remotely.it(fixtures)('includes the length of functions specified as arguments', (fixtures: string) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const path = require('path');
|
2020-10-05 01:22:35 +00:00
|
|
|
const a = require('electron').remote.require(path.join(fixtures, 'function-with-args.js'));
|
2019-12-09 18:27:30 +00:00
|
|
|
/* eslint-disable @typescript-eslint/no-unused-vars */
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(a((a: any, b: any, c: any) => {})).to.equal(3);
|
|
|
|
expect(a((a: any) => {})).to.equal(1);
|
|
|
|
expect(a((...args: any[]) => {})).to.equal(0);
|
2019-12-09 18:27:30 +00:00
|
|
|
/* eslint-enable @typescript-eslint/no-unused-vars */
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-12-09 18:27:30 +00:00
|
|
|
|
|
|
|
remotely.it(fixtures)('handles circular references in arrays and objects', (fixtures: string) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const path = require('path');
|
2020-10-05 01:22:35 +00:00
|
|
|
const a = require('electron').remote.require(path.join(fixtures, 'circular.js'));
|
2019-12-09 18:27:30 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
let arrayA: any[] = ['foo'];
|
|
|
|
const arrayB = [arrayA, 'bar'];
|
|
|
|
arrayA.push(arrayB);
|
2019-12-09 18:27:30 +00:00
|
|
|
expect(a.returnArgs(arrayA, arrayB)).to.deep.equal([
|
|
|
|
['foo', [null, 'bar']],
|
|
|
|
[['foo', null], 'bar']
|
2020-03-20 20:28:31 +00:00
|
|
|
]);
|
2019-12-09 18:27:30 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
let objectA: any = { foo: 'bar' };
|
|
|
|
const objectB = { baz: objectA };
|
|
|
|
objectA.objectB = objectB;
|
2019-12-09 18:27:30 +00:00
|
|
|
expect(a.returnArgs(objectA, objectB)).to.deep.equal([
|
|
|
|
{ foo: 'bar', objectB: { baz: null } },
|
|
|
|
{ baz: { foo: 'bar', objectB: null } }
|
2020-03-20 20:28:31 +00:00
|
|
|
]);
|
2019-12-09 18:27:30 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
arrayA = [1, 2, 3];
|
2019-12-09 18:27:30 +00:00
|
|
|
expect(a.returnArgs({ foo: arrayA }, { bar: arrayA })).to.deep.equal([
|
|
|
|
{ foo: [1, 2, 3] },
|
|
|
|
{ bar: [1, 2, 3] }
|
2020-03-20 20:28:31 +00:00
|
|
|
]);
|
2019-12-09 18:27:30 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
objectA = { foo: 'bar' };
|
2019-12-09 18:27:30 +00:00
|
|
|
expect(a.returnArgs({ foo: objectA }, { bar: objectA })).to.deep.equal([
|
|
|
|
{ foo: { foo: 'bar' } },
|
|
|
|
{ bar: { foo: 'bar' } }
|
2020-03-20 20:28:31 +00:00
|
|
|
]);
|
2019-12-09 18:27:30 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
arrayA = [];
|
|
|
|
arrayA.push(arrayA);
|
2019-12-09 18:27:30 +00:00
|
|
|
expect(a.returnArgs(arrayA)).to.deep.equal([
|
|
|
|
[null]
|
2020-03-20 20:28:31 +00:00
|
|
|
]);
|
2019-12-09 18:27:30 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
objectA = {};
|
|
|
|
objectA.foo = objectA;
|
|
|
|
objectA.bar = 'baz';
|
2019-12-09 18:27:30 +00:00
|
|
|
expect(a.returnArgs(objectA)).to.deep.equal([
|
|
|
|
{ foo: null, bar: 'baz' }
|
2020-03-20 20:28:31 +00:00
|
|
|
]);
|
2019-12-09 18:27:30 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
objectA = {};
|
|
|
|
objectA.foo = { bar: objectA };
|
|
|
|
objectA.bar = 'baz';
|
2019-12-09 18:27:30 +00:00
|
|
|
expect(a.returnArgs(objectA)).to.deep.equal([
|
|
|
|
{ foo: { bar: null }, bar: 'baz' }
|
2020-03-20 20:28:31 +00:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
2019-12-09 18:27:30 +00:00
|
|
|
|
|
|
|
describe('remote.createFunctionWithReturnValue', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const remotely = makeRemotely(makeWindow());
|
2019-12-09 18:27:30 +00:00
|
|
|
|
|
|
|
remotely.it(fixtures)('should be called in browser synchronously', async (fixtures: string) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const { remote } = require('electron');
|
|
|
|
const path = require('path');
|
|
|
|
const buf = Buffer.from('test');
|
2020-10-05 01:22:35 +00:00
|
|
|
const call = remote.require(path.join(fixtures, 'call.js'));
|
2020-03-20 20:28:31 +00:00
|
|
|
const result = call.call((remote as any).createFunctionWithReturnValue(buf));
|
|
|
|
expect(result).to.be.an.instanceOf(Uint8Array);
|
|
|
|
});
|
|
|
|
});
|
2019-12-09 18:27:30 +00:00
|
|
|
|
|
|
|
describe('remote modules', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const remotely = makeRemotely(makeWindow());
|
2019-12-09 18:27:30 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const mainModules = Object.keys(require('electron'));
|
2019-12-09 18:27:30 +00:00
|
|
|
remotely.it(mainModules)('includes browser process modules as properties', (mainModules: string[]) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const { remote } = require('electron');
|
|
|
|
const remoteModules = mainModules.filter(name => (remote as any)[name]);
|
|
|
|
expect(remoteModules).to.be.deep.equal(mainModules);
|
|
|
|
});
|
2019-12-09 18:27:30 +00:00
|
|
|
|
|
|
|
remotely.it(fixtures)('returns toString() of original function via toString()', (fixtures: string) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const path = require('path');
|
|
|
|
const { readText } = require('electron').remote.clipboard;
|
|
|
|
expect(readText.toString().startsWith('function')).to.be.true();
|
2019-12-09 18:27:30 +00:00
|
|
|
|
2020-10-05 01:22:35 +00:00
|
|
|
const { functionWithToStringProperty } = require('electron').remote.require(path.join(fixtures, 'to-string-non-function.js'));
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(functionWithToStringProperty.toString).to.equal('hello');
|
|
|
|
});
|
|
|
|
});
|
2019-12-09 18:27:30 +00:00
|
|
|
|
|
|
|
describe('remote object in renderer', () => {
|
2020-03-25 20:13:10 +00:00
|
|
|
const win = makeWindow();
|
|
|
|
const remotely = makeRemotely(win);
|
2019-12-09 18:27:30 +00:00
|
|
|
|
|
|
|
remotely.it(fixtures)('can change its properties', (fixtures: string) => {
|
2020-10-05 01:22:35 +00:00
|
|
|
const module = require('path').join(fixtures, 'property.js');
|
2020-03-20 20:28:31 +00:00
|
|
|
const property = require('electron').remote.require(module);
|
|
|
|
expect(property.property).to.equal(1127);
|
|
|
|
property.property = null;
|
|
|
|
expect(property.property).to.equal(null);
|
|
|
|
property.property = undefined;
|
|
|
|
expect(property.property).to.equal(undefined);
|
|
|
|
property.property = 1007;
|
|
|
|
expect(property.property).to.equal(1007);
|
|
|
|
|
|
|
|
expect(property.getFunctionProperty()).to.equal('foo-browser');
|
|
|
|
property.func.property = 'bar';
|
|
|
|
expect(property.getFunctionProperty()).to.equal('bar-browser');
|
|
|
|
property.func.property = 'foo'; // revert back
|
|
|
|
|
|
|
|
const property2 = require('electron').remote.require(module);
|
|
|
|
expect(property2.property).to.equal(1007);
|
|
|
|
|
|
|
|
property.property = 1127; // revert back
|
|
|
|
});
|
2019-12-09 18:27:30 +00:00
|
|
|
|
|
|
|
remotely.it(fixtures)('rethrows errors getting/setting properties', (fixtures: string) => {
|
2020-10-05 01:22:35 +00:00
|
|
|
const foo = require('electron').remote.require(require('path').join(fixtures, 'error-properties.js'));
|
2019-12-09 18:27:30 +00:00
|
|
|
|
|
|
|
expect(() => {
|
|
|
|
// eslint-disable-next-line
|
|
|
|
foo.bar
|
2020-03-20 20:28:31 +00:00
|
|
|
}).to.throw('getting error');
|
2019-12-09 18:27:30 +00:00
|
|
|
|
|
|
|
expect(() => {
|
2020-03-20 20:28:31 +00:00
|
|
|
foo.bar = 'test';
|
|
|
|
}).to.throw('setting error');
|
|
|
|
});
|
2019-12-09 18:27:30 +00:00
|
|
|
|
|
|
|
remotely.it(fixtures)('can set a remote property with a remote object', (fixtures: string) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const { remote } = require('electron');
|
2020-10-05 01:22:35 +00:00
|
|
|
const foo = remote.require(require('path').join(fixtures, 'remote-object-set.js'));
|
2020-03-20 20:28:31 +00:00
|
|
|
foo.bar = remote.getCurrentWindow();
|
|
|
|
});
|
2019-12-09 18:27:30 +00:00
|
|
|
|
|
|
|
remotely.it(fixtures)('can construct an object from its member', (fixtures: string) => {
|
2020-10-05 01:22:35 +00:00
|
|
|
const call = require('electron').remote.require(require('path').join(fixtures, 'call.js'));
|
2020-03-20 20:28:31 +00:00
|
|
|
const obj = new call.constructor();
|
|
|
|
expect(obj.test).to.equal('test');
|
|
|
|
});
|
2019-12-09 18:27:30 +00:00
|
|
|
|
|
|
|
remotely.it(fixtures)('can reassign and delete its member functions', (fixtures: string) => {
|
2020-10-05 01:22:35 +00:00
|
|
|
const remoteFunctions = require('electron').remote.require(require('path').join(fixtures, 'function.js'));
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(remoteFunctions.aFunction()).to.equal(1127);
|
2019-12-09 18:27:30 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
remoteFunctions.aFunction = () => { return 1234; };
|
|
|
|
expect(remoteFunctions.aFunction()).to.equal(1234);
|
2019-12-09 18:27:30 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(delete remoteFunctions.aFunction).to.equal(true);
|
|
|
|
});
|
2019-12-09 18:27:30 +00:00
|
|
|
|
|
|
|
remotely.it('is referenced by its members', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const stringify = require('electron').remote.getGlobal('JSON').stringify;
|
|
|
|
global.gc();
|
|
|
|
stringify({});
|
|
|
|
});
|
2020-03-25 20:13:10 +00:00
|
|
|
|
|
|
|
it('can handle objects without constructors', async () => {
|
|
|
|
win().webContents.once('remote-get-global', (event) => {
|
|
|
|
class Foo { bar () { return 'bar'; } }
|
|
|
|
Foo.prototype.constructor = undefined as any;
|
|
|
|
event.returnValue = new Foo();
|
|
|
|
});
|
|
|
|
expect(await remotely(() => require('electron').remote.getGlobal('test').bar())).to.equal('bar');
|
|
|
|
});
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-12-09 18:27:30 +00:00
|
|
|
|
|
|
|
describe('remote value in browser', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const remotely = makeRemotely(makeWindow());
|
2020-10-05 01:22:35 +00:00
|
|
|
const print = path.join(fixtures, 'print_name.js');
|
2019-12-09 18:27:30 +00:00
|
|
|
|
|
|
|
remotely.it(print)('preserves NaN', (print: string) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const printName = require('electron').remote.require(print);
|
|
|
|
expect(printName.getNaN()).to.be.NaN();
|
|
|
|
expect(printName.echo(NaN)).to.be.NaN();
|
|
|
|
});
|
2019-12-09 18:27:30 +00:00
|
|
|
|
|
|
|
remotely.it(print)('preserves Infinity', (print: string) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const printName = require('electron').remote.require(print);
|
|
|
|
expect(printName.getInfinity()).to.equal(Infinity);
|
|
|
|
expect(printName.echo(Infinity)).to.equal(Infinity);
|
|
|
|
});
|
2019-12-09 18:27:30 +00:00
|
|
|
|
|
|
|
remotely.it(print)('keeps its constructor name for objects', (print: string) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const printName = require('electron').remote.require(print);
|
|
|
|
const buf = Buffer.from('test');
|
|
|
|
expect(printName.print(buf)).to.equal('Buffer');
|
|
|
|
});
|
2019-12-09 18:27:30 +00:00
|
|
|
|
|
|
|
remotely.it(print)('supports instanceof Boolean', (print: string) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const printName = require('electron').remote.require(print);
|
|
|
|
const obj = Boolean(true);
|
|
|
|
expect(printName.print(obj)).to.equal('Boolean');
|
|
|
|
expect(printName.echo(obj)).to.deep.equal(obj);
|
|
|
|
});
|
2019-12-09 18:27:30 +00:00
|
|
|
|
|
|
|
remotely.it(print)('supports instanceof Number', (print: string) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const printName = require('electron').remote.require(print);
|
|
|
|
const obj = Number(42);
|
|
|
|
expect(printName.print(obj)).to.equal('Number');
|
|
|
|
expect(printName.echo(obj)).to.deep.equal(obj);
|
|
|
|
});
|
2019-12-09 18:27:30 +00:00
|
|
|
|
|
|
|
remotely.it(print)('supports instanceof String', (print: string) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const printName = require('electron').remote.require(print);
|
|
|
|
const obj = String('Hello World!');
|
|
|
|
expect(printName.print(obj)).to.equal('String');
|
|
|
|
expect(printName.echo(obj)).to.deep.equal(obj);
|
|
|
|
});
|
2019-12-09 18:27:30 +00:00
|
|
|
|
|
|
|
remotely.it(print)('supports instanceof Date', (print: string) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const printName = require('electron').remote.require(print);
|
|
|
|
const now = new Date();
|
|
|
|
expect(printName.print(now)).to.equal('Date');
|
|
|
|
expect(printName.echo(now)).to.deep.equal(now);
|
|
|
|
});
|
2019-12-09 18:27:30 +00:00
|
|
|
|
|
|
|
remotely.it(print)('supports instanceof RegExp', (print: string) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const printName = require('electron').remote.require(print);
|
|
|
|
const regexp = RegExp('.*');
|
|
|
|
expect(printName.print(regexp)).to.equal('RegExp');
|
|
|
|
expect(printName.echo(regexp)).to.deep.equal(regexp);
|
|
|
|
});
|
2019-12-09 18:27:30 +00:00
|
|
|
|
|
|
|
remotely.it(print)('supports instanceof Buffer', (print: string) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const printName = require('electron').remote.require(print);
|
|
|
|
const buffer = Buffer.from('test');
|
|
|
|
expect(buffer.equals(printName.echo(buffer))).to.be.true();
|
2019-12-09 18:27:30 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const objectWithBuffer = { a: 'foo', b: Buffer.from('bar') };
|
|
|
|
expect(objectWithBuffer.b.equals(printName.echo(objectWithBuffer).b)).to.be.true();
|
2019-12-09 18:27:30 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const arrayWithBuffer = [1, 2, Buffer.from('baz')];
|
|
|
|
expect((arrayWithBuffer[2] as Buffer).equals(printName.echo(arrayWithBuffer)[2])).to.be.true();
|
|
|
|
});
|
2019-12-09 18:27:30 +00:00
|
|
|
|
|
|
|
remotely.it(print)('supports instanceof ArrayBuffer', (print: string) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const printName = require('electron').remote.require(print);
|
|
|
|
const buffer = new ArrayBuffer(8);
|
|
|
|
const view = new DataView(buffer);
|
2019-12-09 18:27:30 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
view.setFloat64(0, Math.PI);
|
|
|
|
expect(printName.echo(buffer)).to.deep.equal(buffer);
|
|
|
|
expect(printName.print(buffer)).to.equal('ArrayBuffer');
|
|
|
|
});
|
2019-12-09 18:27:30 +00:00
|
|
|
|
|
|
|
const arrayTests: [string, number[]][] = [
|
|
|
|
['Int8Array', [1, 2, 3, 4]],
|
|
|
|
['Uint8Array', [1, 2, 3, 4]],
|
|
|
|
['Uint8ClampedArray', [1, 2, 3, 4]],
|
|
|
|
['Int16Array', [0x1234, 0x2345, 0x3456, 0x4567]],
|
|
|
|
['Uint16Array', [0x1234, 0x2345, 0x3456, 0x4567]],
|
|
|
|
['Int32Array', [0x12345678, 0x23456789]],
|
|
|
|
['Uint32Array', [0x12345678, 0x23456789]],
|
|
|
|
['Float32Array', [0.5, 1.0, 1.5]],
|
|
|
|
['Float64Array', [0.5, 1.0, 1.5]]
|
2020-03-20 20:28:31 +00:00
|
|
|
];
|
2019-12-09 18:27:30 +00:00
|
|
|
|
|
|
|
arrayTests.forEach(([arrayType, values]) => {
|
|
|
|
remotely.it(print, arrayType, values)(`supports instanceof ${arrayType}`, (print: string, arrayType: string, values: number[]) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const printName = require('electron').remote.require(print);
|
|
|
|
expect([...printName.typedArray(arrayType, values)]).to.deep.equal(values);
|
2019-12-09 18:27:30 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const int8values = new ((window as any)[arrayType])(values);
|
|
|
|
expect(printName.typedArray(arrayType, int8values)).to.deep.equal(int8values);
|
|
|
|
expect(printName.print(int8values)).to.equal(arrayType);
|
|
|
|
});
|
|
|
|
});
|
2019-12-09 18:27:30 +00:00
|
|
|
|
|
|
|
describe('constructing a Uint8Array', () => {
|
|
|
|
remotely.it()('does not crash', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const RUint8Array = require('electron').remote.getGlobal('Uint8Array');
|
2019-12-09 18:27:30 +00:00
|
|
|
new RUint8Array() // eslint-disable-line
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2019-12-09 18:27:30 +00:00
|
|
|
|
|
|
|
describe('remote promise', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const remotely = makeRemotely(makeWindow());
|
2019-12-09 18:27:30 +00:00
|
|
|
|
|
|
|
remotely.it(fixtures)('can be used as promise in each side', async (fixtures: string) => {
|
2020-10-05 01:22:35 +00:00
|
|
|
const promise = require('electron').remote.require(require('path').join(fixtures, 'promise.js'));
|
2020-03-20 20:28:31 +00:00
|
|
|
const value = await promise.twicePromise(Promise.resolve(1234));
|
|
|
|
expect(value).to.equal(2468);
|
|
|
|
});
|
2019-12-09 18:27:30 +00:00
|
|
|
|
|
|
|
remotely.it(fixtures)('handles rejections via catch(onRejected)', async (fixtures: string) => {
|
2020-10-05 01:22:35 +00:00
|
|
|
const promise = require('electron').remote.require(require('path').join(fixtures, 'rejected-promise.js'));
|
2019-12-09 18:27:30 +00:00
|
|
|
const error = await new Promise<Error>(resolve => {
|
2020-03-20 20:28:31 +00:00
|
|
|
promise.reject(Promise.resolve(1234)).catch(resolve);
|
|
|
|
});
|
|
|
|
expect(error.message).to.equal('rejected');
|
|
|
|
});
|
2019-12-09 18:27:30 +00:00
|
|
|
|
|
|
|
remotely.it(fixtures)('handles rejections via then(onFulfilled, onRejected)', async (fixtures: string) => {
|
2020-10-05 01:22:35 +00:00
|
|
|
const promise = require('electron').remote.require(require('path').join(fixtures, 'rejected-promise.js'));
|
2019-12-09 18:27:30 +00:00
|
|
|
const error = await new Promise<Error>(resolve => {
|
2020-03-20 20:28:31 +00:00
|
|
|
promise.reject(Promise.resolve(1234)).then(() => {}, resolve);
|
|
|
|
});
|
|
|
|
expect(error.message).to.equal('rejected');
|
|
|
|
});
|
2019-12-09 18:27:30 +00:00
|
|
|
|
|
|
|
it('does not emit unhandled rejection events in the main process', (done) => {
|
|
|
|
function onUnhandledRejection () {
|
2020-03-20 20:28:31 +00:00
|
|
|
done(new Error('Unexpected unhandledRejection event'));
|
2019-12-09 18:27:30 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
process.once('unhandledRejection', onUnhandledRejection);
|
2019-12-09 18:27:30 +00:00
|
|
|
|
|
|
|
remotely(async (fixtures: string) => {
|
2020-10-05 01:22:35 +00:00
|
|
|
const promise = require('electron').remote.require(require('path').join(fixtures, 'unhandled-rejection.js'));
|
2019-12-09 18:27:30 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
promise.reject().then(() => {
|
2020-03-20 20:28:31 +00:00
|
|
|
reject(new Error('Promise was not rejected'));
|
2019-12-09 18:27:30 +00:00
|
|
|
}).catch((error: Error) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
resolve(error);
|
|
|
|
});
|
|
|
|
});
|
2019-12-09 18:27:30 +00:00
|
|
|
}, fixtures).then(error => {
|
|
|
|
try {
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(error.message).to.equal('rejected');
|
|
|
|
done();
|
2019-12-09 18:27:30 +00:00
|
|
|
} catch (e) {
|
2020-03-20 20:28:31 +00:00
|
|
|
done(e);
|
2019-12-09 18:27:30 +00:00
|
|
|
} finally {
|
2020-03-20 20:28:31 +00:00
|
|
|
process.off('unhandledRejection', onUnhandledRejection);
|
2019-12-09 18:27:30 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
});
|
2019-12-09 18:27:30 +00:00
|
|
|
|
|
|
|
it('emits unhandled rejection events in the renderer process', (done) => {
|
|
|
|
remotely((module: string) => new Promise((resolve, reject) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const promise = require('electron').remote.require(module);
|
2019-12-09 18:27:30 +00:00
|
|
|
|
|
|
|
window.addEventListener('unhandledrejection', function handler (event) {
|
2020-03-20 20:28:31 +00:00
|
|
|
event.preventDefault();
|
|
|
|
window.removeEventListener('unhandledrejection', handler);
|
|
|
|
resolve(event.reason.message);
|
|
|
|
});
|
2019-12-09 18:27:30 +00:00
|
|
|
|
|
|
|
promise.reject().then(() => {
|
2020-03-20 20:28:31 +00:00
|
|
|
reject(new Error('Promise was not rejected'));
|
|
|
|
});
|
2020-10-05 01:22:35 +00:00
|
|
|
}), path.join(fixtures, 'unhandled-rejection.js')).then(
|
2019-12-09 18:27:30 +00:00
|
|
|
(message) => {
|
|
|
|
try {
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(message).to.equal('rejected');
|
|
|
|
done();
|
2019-12-09 18:27:30 +00:00
|
|
|
} catch (e) {
|
2020-03-20 20:28:31 +00:00
|
|
|
done(e);
|
2019-12-09 18:27:30 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
done
|
2020-03-20 20:28:31 +00:00
|
|
|
);
|
|
|
|
});
|
2019-12-09 18:27:30 +00:00
|
|
|
|
|
|
|
before(() => {
|
2020-03-20 20:28:31 +00:00
|
|
|
(global as any).returnAPromise = (value: any) => new Promise((resolve) => setTimeout(() => resolve(value), 100));
|
|
|
|
});
|
2019-12-09 18:27:30 +00:00
|
|
|
after(() => {
|
2020-03-20 20:28:31 +00:00
|
|
|
delete (global as any).returnAPromise;
|
|
|
|
});
|
2019-12-09 18:27:30 +00:00
|
|
|
remotely.it()('using a promise based method resolves correctly when global Promise is overridden', async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const { remote } = require('electron');
|
|
|
|
const original = global.Promise;
|
2019-12-09 18:27:30 +00:00
|
|
|
try {
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(await remote.getGlobal('returnAPromise')(123)).to.equal(123);
|
|
|
|
global.Promise = { resolve: () => ({}) } as any;
|
|
|
|
expect(await remote.getGlobal('returnAPromise')(456)).to.equal(456);
|
2019-12-09 18:27:30 +00:00
|
|
|
} finally {
|
2020-03-20 20:28:31 +00:00
|
|
|
global.Promise = original;
|
2019-12-09 18:27:30 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
});
|
2019-12-09 18:27:30 +00:00
|
|
|
|
|
|
|
describe('remote webContents', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const remotely = makeRemotely(makeWindow());
|
2019-12-09 18:27:30 +00:00
|
|
|
|
|
|
|
it('can return same object with different getters', async () => {
|
|
|
|
const equal = await remotely(() => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const { remote } = require('electron');
|
|
|
|
const contents1 = remote.getCurrentWindow().webContents;
|
|
|
|
const contents2 = remote.getCurrentWebContents();
|
|
|
|
return contents1 === contents2;
|
|
|
|
});
|
|
|
|
expect(equal).to.be.true();
|
|
|
|
});
|
|
|
|
});
|
2019-12-09 18:27:30 +00:00
|
|
|
|
|
|
|
describe('remote class', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const remotely = makeRemotely(makeWindow());
|
2019-12-09 18:27:30 +00:00
|
|
|
|
|
|
|
remotely.it(fixtures)('can get methods', (fixtures: string) => {
|
2020-10-05 01:22:35 +00:00
|
|
|
const { base } = require('electron').remote.require(require('path').join(fixtures, 'class.js'));
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(base.method()).to.equal('method');
|
|
|
|
});
|
2019-12-09 18:27:30 +00:00
|
|
|
|
|
|
|
remotely.it(fixtures)('can get properties', (fixtures: string) => {
|
2020-10-05 01:22:35 +00:00
|
|
|
const { base } = require('electron').remote.require(require('path').join(fixtures, 'class.js'));
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(base.readonly).to.equal('readonly');
|
|
|
|
});
|
2019-12-09 18:27:30 +00:00
|
|
|
|
|
|
|
remotely.it(fixtures)('can change properties', (fixtures: string) => {
|
2020-10-05 01:22:35 +00:00
|
|
|
const { base } = require('electron').remote.require(require('path').join(fixtures, 'class.js'));
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(base.value).to.equal('old');
|
|
|
|
base.value = 'new';
|
|
|
|
expect(base.value).to.equal('new');
|
|
|
|
base.value = 'old';
|
|
|
|
});
|
2019-12-09 18:27:30 +00:00
|
|
|
|
|
|
|
remotely.it(fixtures)('has unenumerable methods', (fixtures: string) => {
|
2020-10-05 01:22:35 +00:00
|
|
|
const { base } = require('electron').remote.require(require('path').join(fixtures, 'class.js'));
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(base).to.not.have.ownProperty('method');
|
|
|
|
expect(Object.getPrototypeOf(base)).to.have.ownProperty('method');
|
|
|
|
});
|
2019-12-09 18:27:30 +00:00
|
|
|
|
|
|
|
remotely.it(fixtures)('keeps prototype chain in derived class', (fixtures: string) => {
|
2020-10-05 01:22:35 +00:00
|
|
|
const { derived } = require('electron').remote.require(require('path').join(fixtures, 'class.js'));
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(derived.method()).to.equal('method');
|
|
|
|
expect(derived.readonly).to.equal('readonly');
|
|
|
|
expect(derived).to.not.have.ownProperty('method');
|
|
|
|
const proto = Object.getPrototypeOf(derived);
|
|
|
|
expect(proto).to.not.have.ownProperty('method');
|
|
|
|
expect(Object.getPrototypeOf(proto)).to.have.ownProperty('method');
|
|
|
|
});
|
2019-12-09 18:27:30 +00:00
|
|
|
|
|
|
|
remotely.it(fixtures)('is referenced by methods in prototype chain', (fixtures: string) => {
|
2020-10-05 01:22:35 +00:00
|
|
|
let { derived } = require('electron').remote.require(require('path').join(fixtures, 'class.js'));
|
2020-03-20 20:28:31 +00:00
|
|
|
const method = derived.method;
|
|
|
|
derived = null;
|
|
|
|
global.gc();
|
|
|
|
expect(method()).to.equal('method');
|
|
|
|
});
|
|
|
|
});
|
2019-12-09 18:27:30 +00:00
|
|
|
|
|
|
|
describe('remote exception', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const remotely = makeRemotely(makeWindow());
|
2019-12-09 18:27:30 +00:00
|
|
|
|
|
|
|
remotely.it(fixtures)('throws errors from the main process', (fixtures: string) => {
|
2020-10-05 01:22:35 +00:00
|
|
|
const throwFunction = require('electron').remote.require(require('path').join(fixtures, 'exception.js'));
|
2019-12-09 18:27:30 +00:00
|
|
|
expect(() => {
|
2020-03-20 20:28:31 +00:00
|
|
|
throwFunction();
|
|
|
|
}).to.throw(/undefined/);
|
|
|
|
});
|
2019-12-09 18:27:30 +00:00
|
|
|
|
|
|
|
remotely.it(fixtures)('tracks error cause', (fixtures: string) => {
|
2020-10-05 01:22:35 +00:00
|
|
|
const throwFunction = require('electron').remote.require(require('path').join(fixtures, 'exception.js'));
|
2019-12-09 18:27:30 +00:00
|
|
|
try {
|
2020-03-20 20:28:31 +00:00
|
|
|
throwFunction(new Error('error from main'));
|
|
|
|
expect.fail();
|
2019-12-09 18:27:30 +00:00
|
|
|
} catch (e) {
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(e.message).to.match(/Could not call remote function/);
|
|
|
|
expect(e.cause.message).to.equal('error from main');
|
2019-12-09 18:27:30 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
});
|
2020-06-12 21:19:20 +00:00
|
|
|
|
|
|
|
describe('gc behavior', () => {
|
|
|
|
const win = makeWindow();
|
|
|
|
const remotely = makeRemotely(win);
|
|
|
|
it('is resilient to gc happening between request and response', async () => {
|
|
|
|
const obj = { x: 'y' };
|
|
|
|
win().webContents.on('remote-get-global', (event) => {
|
|
|
|
event.returnValue = obj;
|
|
|
|
});
|
|
|
|
await remotely(() => {
|
2020-06-23 03:32:45 +00:00
|
|
|
const { ipc } = process._linkedBinding('electron_renderer_ipc');
|
2020-06-12 21:19:20 +00:00
|
|
|
const originalSendSync = ipc.sendSync.bind(ipc) as any;
|
|
|
|
ipc.sendSync = (...args: any[]): any => {
|
|
|
|
const ret = originalSendSync(...args);
|
|
|
|
(window as any).gc();
|
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
|
|
|
|
for (let i = 0; i < 100; i++) {
|
|
|
|
// eslint-disable-next-line
|
|
|
|
require('electron').remote.getGlobal('test').x;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|