api-remote-spec.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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 TypedArray', () => {
  213. const values = [1, 2, 3, 4]
  214. assert.deepEqual(printName.typedArray(values), values)
  215. const int16values = new Int16Array([1, 2, 3, 4])
  216. assert.deepEqual(printName.typedArray(int16values), int16values)
  217. })
  218. })
  219. describe('remote promise', () => {
  220. it('can be used as promise in each side', (done) => {
  221. const promise = remote.require(path.join(fixtures, 'module', 'promise.js'))
  222. promise.twicePromise(Promise.resolve(1234)).then((value) => {
  223. assert.equal(value, 2468)
  224. done()
  225. })
  226. })
  227. it('handles rejections via catch(onRejected)', (done) => {
  228. const promise = remote.require(path.join(fixtures, 'module', 'rejected-promise.js'))
  229. promise.reject(Promise.resolve(1234)).catch((error) => {
  230. assert.equal(error.message, 'rejected')
  231. done()
  232. })
  233. })
  234. it('handles rejections via then(onFulfilled, onRejected)', (done) => {
  235. const promise = remote.require(path.join(fixtures, 'module', 'rejected-promise.js'))
  236. promise.reject(Promise.resolve(1234)).then(() => {}, (error) => {
  237. assert.equal(error.message, 'rejected')
  238. done()
  239. })
  240. })
  241. it('does not emit unhandled rejection events in the main process', (done) => {
  242. remote.process.once('unhandledRejection', function (reason) {
  243. done(reason)
  244. })
  245. const promise = remote.require(path.join(fixtures, 'module', 'unhandled-rejection.js'))
  246. promise.reject().then(() => {
  247. done(new Error('Promise was not rejected'))
  248. }).catch((error) => {
  249. assert.equal(error.message, 'rejected')
  250. done()
  251. })
  252. })
  253. it('emits unhandled rejection events in the renderer process', (done) => {
  254. window.addEventListener('unhandledrejection', function (event) {
  255. event.preventDefault()
  256. assert.equal(event.reason.message, 'rejected')
  257. done()
  258. })
  259. const promise = remote.require(path.join(fixtures, 'module', 'unhandled-rejection.js'))
  260. promise.reject().then(() => {
  261. done(new Error('Promise was not rejected'))
  262. })
  263. })
  264. })
  265. describe('remote webContents', () => {
  266. it('can return same object with different getters', () => {
  267. const contents1 = remote.getCurrentWindow().webContents
  268. const contents2 = remote.getCurrentWebContents()
  269. assert(contents1 === contents2)
  270. })
  271. })
  272. describe('remote class', () => {
  273. const cl = remote.require(path.join(fixtures, 'module', 'class.js'))
  274. const base = cl.base
  275. let derived = cl.derived
  276. it('can get methods', () => {
  277. assert.equal(base.method(), 'method')
  278. })
  279. it('can get properties', () => {
  280. assert.equal(base.readonly, 'readonly')
  281. })
  282. it('can change properties', () => {
  283. assert.equal(base.value, 'old')
  284. base.value = 'new'
  285. assert.equal(base.value, 'new')
  286. base.value = 'old'
  287. })
  288. it('has unenumerable methods', () => {
  289. assert(!base.hasOwnProperty('method'))
  290. assert(Object.getPrototypeOf(base).hasOwnProperty('method'))
  291. })
  292. it('keeps prototype chain in derived class', () => {
  293. assert.equal(derived.method(), 'method')
  294. assert.equal(derived.readonly, 'readonly')
  295. assert(!derived.hasOwnProperty('method'))
  296. let proto = Object.getPrototypeOf(derived)
  297. assert(!proto.hasOwnProperty('method'))
  298. assert(Object.getPrototypeOf(proto).hasOwnProperty('method'))
  299. })
  300. it('is referenced by methods in prototype chain', () => {
  301. let method = derived.method
  302. derived = null
  303. global.gc()
  304. assert.equal(method(), 'method')
  305. })
  306. })
  307. })