api-remote-spec.js 16 KB

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