api-remote-spec.js 19 KB

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