api-remote-spec.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. 'use strict';
  2. const chai = require('chai');
  3. const dirtyChai = require('dirty-chai');
  4. const path = require('path');
  5. const { closeWindow } = require('./window-helpers');
  6. const { resolveGetters } = require('./expect-helpers');
  7. const { remote, ipcRenderer } = require('electron');
  8. const { ipcMain, BrowserWindow } = remote;
  9. const { expect } = chai;
  10. chai.use(dirtyChai);
  11. const comparePaths = (path1, path2) => {
  12. if (process.platform === 'win32') {
  13. path1 = path1.toLowerCase();
  14. path2 = path2.toLowerCase();
  15. }
  16. expect(path1).to.equal(path2);
  17. };
  18. describe('remote module', () => {
  19. const fixtures = path.join(__dirname, 'fixtures');
  20. describe('remote.require', () => {
  21. it('should returns same object for the same module', () => {
  22. const dialog1 = remote.require('electron');
  23. const dialog2 = remote.require('electron');
  24. expect(dialog1).to.equal(dialog2);
  25. });
  26. it('should work when object contains id property', () => {
  27. const a = remote.require(path.join(fixtures, 'module', 'id.js'));
  28. expect(a.id).to.equal(1127);
  29. });
  30. it('should work when object has no prototype', () => {
  31. const a = remote.require(path.join(fixtures, 'module', 'no-prototype.js'));
  32. expect(a.foo.constructor.name).to.equal('');
  33. expect(a.foo.bar).to.equal('baz');
  34. expect(a.foo.baz).to.equal(false);
  35. expect(a.bar).to.equal(1234);
  36. expect(a.anonymous.constructor.name).to.equal('');
  37. expect(a.getConstructorName(Object.create(null))).to.equal('');
  38. expect(a.getConstructorName(new (class {})())).to.equal('');
  39. });
  40. it('should search module from the user app', () => {
  41. comparePaths(path.normalize(remote.process.mainModule.filename), path.resolve(__dirname, 'static', 'main.js'));
  42. comparePaths(path.normalize(remote.process.mainModule.paths[0]), path.resolve(__dirname, 'static', 'node_modules'));
  43. });
  44. it('should work with function properties', () => {
  45. let a = remote.require(path.join(fixtures, 'module', 'export-function-with-properties.js'));
  46. expect(a).to.be.a('function');
  47. expect(a.bar).to.equal('baz');
  48. a = remote.require(path.join(fixtures, 'module', 'function-with-properties.js'));
  49. expect(a).to.be.an('object');
  50. expect(a.foo()).to.equal('hello');
  51. expect(a.foo.bar).to.equal('baz');
  52. expect(a.foo.nested.prop).to.equal('yes');
  53. expect(a.foo.method1()).to.equal('world');
  54. expect(a.foo.method1.prop1()).to.equal(123);
  55. expect(a.foo).to.have.a.property('bar');
  56. expect(a.foo).to.have.a.property('nested');
  57. expect(a.foo).to.have.a.property('method1');
  58. a = remote.require(path.join(fixtures, 'module', 'function-with-missing-properties.js')).setup();
  59. expect(a.bar()).to.equal(true);
  60. expect(a.bar.baz).to.be.undefined();
  61. });
  62. it('should work with static class members', () => {
  63. const a = remote.require(path.join(fixtures, 'module', 'remote-static.js'));
  64. expect(a.Foo).to.be.a('function');
  65. expect(a.Foo.foo()).to.equal(3);
  66. expect(a.Foo.bar).to.equal('baz');
  67. const foo = new a.Foo();
  68. expect(foo.baz()).to.equal(123);
  69. });
  70. it('includes the length of functions specified as arguments', () => {
  71. const a = remote.require(path.join(fixtures, 'module', 'function-with-args.js'));
  72. expect(a((a, b, c, d, f) => {})).to.equal(5);
  73. expect(a((a) => {})).to.equal(1);
  74. expect(a((...args) => {})).to.equal(0);
  75. });
  76. it('handles circular references in arrays and objects', () => {
  77. const a = remote.require(path.join(fixtures, 'module', 'circular.js'));
  78. let arrayA = ['foo'];
  79. const arrayB = [arrayA, 'bar'];
  80. arrayA.push(arrayB);
  81. expect(a.returnArgs(arrayA, arrayB)).to.deep.equal([
  82. ['foo', [null, 'bar']],
  83. [['foo', null], 'bar']
  84. ]);
  85. let objectA = { foo: 'bar' };
  86. const objectB = { baz: objectA };
  87. objectA.objectB = objectB;
  88. expect(a.returnArgs(objectA, objectB)).to.deep.equal([
  89. { foo: 'bar', objectB: { baz: null } },
  90. { baz: { foo: 'bar', objectB: null } }
  91. ]);
  92. arrayA = [1, 2, 3];
  93. expect(a.returnArgs({ foo: arrayA }, { bar: arrayA })).to.deep.equal([
  94. { foo: [1, 2, 3] },
  95. { bar: [1, 2, 3] }
  96. ]);
  97. objectA = { foo: 'bar' };
  98. expect(a.returnArgs({ foo: objectA }, { bar: objectA })).to.deep.equal([
  99. { foo: { foo: 'bar' } },
  100. { bar: { foo: 'bar' } }
  101. ]);
  102. arrayA = [];
  103. arrayA.push(arrayA);
  104. expect(a.returnArgs(arrayA)).to.deep.equal([
  105. [null]
  106. ]);
  107. objectA = {};
  108. objectA.foo = objectA;
  109. objectA.bar = 'baz';
  110. expect(a.returnArgs(objectA)).to.deep.equal([
  111. { foo: null, bar: 'baz' }
  112. ]);
  113. objectA = {};
  114. objectA.foo = { bar: objectA };
  115. objectA.bar = 'baz';
  116. expect(a.returnArgs(objectA)).to.deep.equal([
  117. { foo: { bar: null }, bar: 'baz' }
  118. ]);
  119. });
  120. });
  121. describe('remote.createFunctionWithReturnValue', () => {
  122. it('should be called in browser synchronously', () => {
  123. const buf = Buffer.from('test');
  124. const call = remote.require(path.join(fixtures, 'module', 'call.js'));
  125. const result = call.call(remote.createFunctionWithReturnValue(buf));
  126. expect(result).to.be.an.instanceOf(Buffer);
  127. });
  128. });
  129. describe('remote modules', () => {
  130. it('includes browser process modules as properties', async () => {
  131. const mainModules = await ipcRenderer.invoke('get-modules');
  132. const remoteModules = mainModules.filter(name => remote[name]);
  133. expect(remoteModules).to.be.deep.equal(mainModules);
  134. });
  135. it('returns toString() of original function via toString()', () => {
  136. const { readText } = remote.clipboard;
  137. expect(readText.toString().startsWith('function')).to.be.true();
  138. const { functionWithToStringProperty } = remote.require(path.join(fixtures, 'module', 'to-string-non-function.js'));
  139. expect(functionWithToStringProperty.toString).to.equal('hello');
  140. });
  141. });
  142. describe('remote object in renderer', () => {
  143. it('can change its properties', () => {
  144. const property = remote.require(path.join(fixtures, 'module', 'property.js'));
  145. expect(property).to.have.a.property('property').that.is.equal(1127);
  146. property.property = null;
  147. expect(property).to.have.a.property('property').that.is.null();
  148. property.property = undefined;
  149. expect(property).to.have.a.property('property').that.is.undefined();
  150. property.property = 1007;
  151. expect(property).to.have.a.property('property').that.is.equal(1007);
  152. expect(property.getFunctionProperty()).to.equal('foo-browser');
  153. property.func.property = 'bar';
  154. expect(property.getFunctionProperty()).to.equal('bar-browser');
  155. property.func.property = 'foo'; // revert back
  156. const property2 = remote.require(path.join(fixtures, 'module', 'property.js'));
  157. expect(property2.property).to.equal(1007);
  158. property.property = 1127;
  159. });
  160. it('rethrows errors getting/setting properties', () => {
  161. const foo = remote.require(path.join(fixtures, 'module', 'error-properties.js'));
  162. expect(() => {
  163. // eslint-disable-next-line
  164. foo.bar
  165. }).to.throw('getting error');
  166. expect(() => {
  167. foo.bar = 'test';
  168. }).to.throw('setting error');
  169. });
  170. it('can set a remote property with a remote object', () => {
  171. const foo = remote.require(path.join(fixtures, 'module', 'remote-object-set.js'));
  172. expect(() => {
  173. foo.bar = remote.getCurrentWindow();
  174. }).to.not.throw();
  175. });
  176. it('can construct an object from its member', () => {
  177. const call = remote.require(path.join(fixtures, 'module', 'call.js'));
  178. const obj = new call.constructor();
  179. expect(obj.test).to.equal('test');
  180. });
  181. it('can reassign and delete its member functions', () => {
  182. const remoteFunctions = remote.require(path.join(fixtures, 'module', 'function.js'));
  183. expect(remoteFunctions.aFunction()).to.equal(1127);
  184. remoteFunctions.aFunction = () => { return 1234; };
  185. expect(remoteFunctions.aFunction()).to.equal(1234);
  186. expect(delete remoteFunctions.aFunction).to.equal(true);
  187. });
  188. it('is referenced by its members', () => {
  189. const stringify = remote.getGlobal('JSON').stringify;
  190. global.gc();
  191. stringify({});
  192. });
  193. });
  194. describe('remote value in browser', () => {
  195. const print = path.join(fixtures, 'module', 'print_name.js');
  196. const printName = remote.require(print);
  197. it('converts NaN to undefined', () => {
  198. expect(printName.getNaN()).to.be.undefined();
  199. expect(printName.echo(NaN)).to.be.undefined();
  200. });
  201. it('converts Infinity to undefined', () => {
  202. expect(printName.getInfinity()).to.be.undefined();
  203. expect(printName.echo(Infinity)).to.be.undefined();
  204. });
  205. it('keeps its constructor name for objects', () => {
  206. const buf = Buffer.from('test');
  207. expect(printName.print(buf)).to.equal('Buffer');
  208. });
  209. it('supports instanceof Date', () => {
  210. const now = new Date();
  211. expect(printName.print(now)).to.equal('Date');
  212. expect(printName.echo(now)).to.deep.equal(now);
  213. });
  214. it('supports instanceof Buffer', () => {
  215. const buffer = Buffer.from('test');
  216. expect(buffer.equals(printName.echo(buffer))).to.be.true();
  217. const objectWithBuffer = { a: 'foo', b: Buffer.from('bar') };
  218. expect(objectWithBuffer.b.equals(printName.echo(objectWithBuffer).b)).to.be.true();
  219. const arrayWithBuffer = [1, 2, Buffer.from('baz')];
  220. expect(arrayWithBuffer[2].equals(printName.echo(arrayWithBuffer)[2])).to.be.true();
  221. });
  222. it('supports instanceof ArrayBuffer', () => {
  223. const buffer = new ArrayBuffer(8);
  224. const view = new DataView(buffer);
  225. view.setFloat64(0, Math.PI);
  226. expect(printName.echo(buffer)).to.deep.equal(buffer);
  227. expect(printName.print(buffer)).to.equal('ArrayBuffer');
  228. });
  229. it('supports instanceof Int8Array', () => {
  230. const values = [1, 2, 3, 4];
  231. expect([...printName.typedArray('Int8Array', values)]).to.deep.equal(values);
  232. const int8values = new Int8Array(values);
  233. expect(printName.typedArray('Int8Array', int8values)).to.deep.equal(int8values);
  234. expect(printName.print(int8values)).to.equal('Int8Array');
  235. });
  236. it('supports instanceof Uint8Array', () => {
  237. const values = [1, 2, 3, 4];
  238. expect([...printName.typedArray('Uint8Array', values)]).to.deep.equal(values);
  239. const uint8values = new Uint8Array(values);
  240. expect(printName.typedArray('Uint8Array', uint8values)).to.deep.equal(uint8values);
  241. expect(printName.print(uint8values)).to.equal('Uint8Array');
  242. });
  243. it('supports instanceof Uint8ClampedArray', () => {
  244. const values = [1, 2, 3, 4];
  245. expect([...printName.typedArray('Uint8ClampedArray', values)]).to.deep.equal(values);
  246. const uint8values = new Uint8ClampedArray(values);
  247. expect(printName.typedArray('Uint8ClampedArray', uint8values)).to.deep.equal(uint8values);
  248. expect(printName.print(uint8values)).to.equal('Uint8ClampedArray');
  249. });
  250. it('supports instanceof Int16Array', () => {
  251. const values = [0x1234, 0x2345, 0x3456, 0x4567];
  252. expect([...printName.typedArray('Int16Array', values)]).to.deep.equal(values);
  253. const int16values = new Int16Array(values);
  254. expect(printName.typedArray('Int16Array', int16values)).to.deep.equal(int16values);
  255. expect(printName.print(int16values)).to.equal('Int16Array');
  256. });
  257. it('supports instanceof Uint16Array', () => {
  258. const values = [0x1234, 0x2345, 0x3456, 0x4567];
  259. expect([...printName.typedArray('Uint16Array', values)]).to.deep.equal(values);
  260. const uint16values = new Uint16Array(values);
  261. expect(printName.typedArray('Uint16Array', uint16values)).to.deep.equal(uint16values);
  262. expect(printName.print(uint16values)).to.equal('Uint16Array');
  263. });
  264. it('supports instanceof Int32Array', () => {
  265. const values = [0x12345678, 0x23456789];
  266. expect([...printName.typedArray('Int32Array', values)]).to.deep.equal(values);
  267. const int32values = new Int32Array(values);
  268. expect(printName.typedArray('Int32Array', int32values)).to.deep.equal(int32values);
  269. expect(printName.print(int32values)).to.equal('Int32Array');
  270. });
  271. it('supports instanceof Uint32Array', () => {
  272. const values = [0x12345678, 0x23456789];
  273. expect([...printName.typedArray('Uint32Array', values)]).to.deep.equal(values);
  274. const uint32values = new Uint32Array(values);
  275. expect(printName.typedArray('Uint32Array', uint32values)).to.deep.equal(uint32values);
  276. expect(printName.print(uint32values)).to.equal('Uint32Array');
  277. });
  278. it('supports instanceof Float32Array', () => {
  279. const values = [0.5, 1.0, 1.5];
  280. expect([...printName.typedArray('Float32Array', values)]).to.deep.equal(values);
  281. const float32values = new Float32Array();
  282. expect(printName.typedArray('Float32Array', float32values)).to.deep.equal(float32values);
  283. expect(printName.print(float32values)).to.equal('Float32Array');
  284. });
  285. it('supports instanceof Float64Array', () => {
  286. const values = [0.5, 1.0, 1.5];
  287. expect([...printName.typedArray('Float64Array', values)]).to.deep.equal(values);
  288. const float64values = new Float64Array([0.5, 1.0, 1.5]);
  289. expect(printName.typedArray('Float64Array', float64values)).to.deep.equal(float64values);
  290. expect(printName.print(float64values)).to.equal('Float64Array');
  291. });
  292. });
  293. describe('remote promise', () => {
  294. it('can be used as promise in each side', (done) => {
  295. const promise = remote.require(path.join(fixtures, 'module', 'promise.js'));
  296. promise.twicePromise(Promise.resolve(1234)).then((value) => {
  297. expect(value).to.equal(2468);
  298. done();
  299. });
  300. });
  301. it('handles rejections via catch(onRejected)', (done) => {
  302. const promise = remote.require(path.join(fixtures, 'module', 'rejected-promise.js'));
  303. promise.reject(Promise.resolve(1234)).catch((error) => {
  304. expect(error.message).to.equal('rejected');
  305. done();
  306. });
  307. });
  308. it('handles rejections via then(onFulfilled, onRejected)', (done) => {
  309. const promise = remote.require(path.join(fixtures, 'module', 'rejected-promise.js'));
  310. promise.reject(Promise.resolve(1234)).then(() => {}, (error) => {
  311. expect(error.message).to.equal('rejected');
  312. done();
  313. });
  314. });
  315. it('does not emit unhandled rejection events in the main process', (done) => {
  316. remote.process.once('unhandledRejection', function (reason) {
  317. done(reason);
  318. });
  319. const promise = remote.require(path.join(fixtures, 'module', 'unhandled-rejection.js'));
  320. promise.reject().then(() => {
  321. done(new Error('Promise was not rejected'));
  322. }).catch((error) => {
  323. expect(error.message).to.equal('rejected');
  324. done();
  325. });
  326. });
  327. it('emits unhandled rejection events in the renderer process', (done) => {
  328. window.addEventListener('unhandledrejection', function handler (event) {
  329. event.preventDefault();
  330. expect(event.reason.message).to.equal('rejected');
  331. window.removeEventListener('unhandledrejection', handler);
  332. done();
  333. });
  334. const promise = remote.require(path.join(fixtures, 'module', 'unhandled-rejection.js'));
  335. promise.reject().then(() => {
  336. done(new Error('Promise was not rejected'));
  337. });
  338. });
  339. });
  340. describe('remote webContents', () => {
  341. it('can return same object with different getters', () => {
  342. const contents1 = remote.getCurrentWindow().webContents;
  343. const contents2 = remote.getCurrentWebContents();
  344. expect(contents1).to.equal(contents2);
  345. });
  346. });
  347. describe('remote class', () => {
  348. const cl = remote.require(path.join(fixtures, 'module', 'class.js'));
  349. const base = cl.base;
  350. let derived = cl.derived;
  351. it('can get methods', () => {
  352. expect(base.method()).to.equal('method');
  353. });
  354. it('can get properties', () => {
  355. expect(base.readonly).to.equal('readonly');
  356. });
  357. it('can change properties', () => {
  358. expect(base.value).to.equal('old');
  359. base.value = 'new';
  360. expect(base.value).to.equal('new');
  361. base.value = 'old';
  362. });
  363. it('has unenumerable methods', () => {
  364. expect(base).to.not.have.own.property('method');
  365. expect(Object.getPrototypeOf(base)).to.have.own.property('method');
  366. });
  367. it('keeps prototype chain in derived class', () => {
  368. expect(derived.method()).to.equal('method');
  369. expect(derived.readonly).to.equal('readonly');
  370. expect(derived).to.not.have.own.property('method');
  371. const proto = Object.getPrototypeOf(derived);
  372. expect(proto).to.not.have.own.property('method');
  373. expect(Object.getPrototypeOf(proto)).to.have.own.property('method');
  374. });
  375. it('is referenced by methods in prototype chain', () => {
  376. const method = derived.method;
  377. derived = null;
  378. global.gc();
  379. expect(method()).to.equal('method');
  380. });
  381. });
  382. describe('remote exception', () => {
  383. const throwFunction = remote.require(path.join(fixtures, 'module', 'exception.js'));
  384. it('throws errors from the main process', () => {
  385. expect(() => {
  386. throwFunction();
  387. }).to.throw();
  388. });
  389. it('throws custom errors from the main process', () => {
  390. const err = new Error('error');
  391. err.cause = new Error('cause');
  392. err.prop = 'error prop';
  393. try {
  394. throwFunction(err);
  395. } catch (error) {
  396. expect(error.from).to.equal('browser');
  397. expect(error.cause).to.deep.equal(...resolveGetters(err));
  398. }
  399. });
  400. });
  401. describe('remote function in renderer', () => {
  402. let w = null;
  403. afterEach(() => closeWindow(w).then(() => { w = null; }));
  404. afterEach(() => {
  405. ipcMain.removeAllListeners('done');
  406. });
  407. it('works when created in preload script', (done) => {
  408. ipcMain.once('done', () => w.close());
  409. const preload = path.join(fixtures, 'module', 'preload-remote-function.js');
  410. w = new BrowserWindow({
  411. show: false,
  412. webPreferences: {
  413. preload
  414. }
  415. });
  416. w.once('closed', () => done());
  417. w.loadURL('about:blank');
  418. });
  419. });
  420. describe('with an overriden global Promise constrctor', () => {
  421. let original;
  422. before(() => {
  423. original = Promise;
  424. });
  425. it('using a promise based method resolves correctly', async () => {
  426. expect(await remote.getGlobal('returnAPromise')(123)).to.equal(123);
  427. global.Promise = { resolve: () => ({}) };
  428. expect(await remote.getGlobal('returnAPromise')(456)).to.equal(456);
  429. });
  430. after(() => {
  431. global.Promise = original;
  432. });
  433. });
  434. });