api-remote-spec.ts 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. import * as path from 'path'
  2. import { expect } from 'chai'
  3. import { closeWindow } from './window-helpers'
  4. import { ipcMain, BrowserWindow, nativeImage } from 'electron'
  5. import { serialize, deserialize } from '../lib/common/type-utils';
  6. describe('remote module', () => {
  7. const fixtures = path.join(__dirname, 'fixtures')
  8. let w = null as unknown as BrowserWindow
  9. beforeEach(async () => {
  10. w = new BrowserWindow({show: false, webPreferences: {nodeIntegration: true}})
  11. await w.loadURL('about:blank')
  12. })
  13. afterEach(async () => {
  14. await closeWindow(w)
  15. })
  16. async function remotely(script: string) {
  17. // executeJavaScript never returns if the script throws an error, so catch
  18. // any errors manually.
  19. const assembledScript = `(function() {
  20. try {
  21. return { result: ${script} }
  22. } catch (e) {
  23. return { error: e.message }
  24. }
  25. })()`
  26. const {result, error} = await w.webContents.executeJavaScript(assembledScript)
  27. if (error) {
  28. throw new Error(error)
  29. }
  30. return result
  31. }
  32. describe('typeUtils serialization/deserialization', () => {
  33. it('serializes and deserializes an empty NativeImage', () => {
  34. const image = nativeImage.createEmpty();
  35. const serializedImage = serialize(image);
  36. const empty = deserialize(serializedImage);
  37. expect(empty.isEmpty()).to.be.true();
  38. expect(empty.getAspectRatio()).to.equal(1);
  39. expect(empty.toDataURL()).to.equal('data:image/png;base64,');
  40. expect(empty.toDataURL({ scaleFactor: 2.0 })).to.equal('data:image/png;base64,');
  41. expect(empty.getSize()).to.deep.equal({ width: 0, height: 0 });
  42. expect(empty.getBitmap()).to.be.empty();
  43. expect(empty.getBitmap({ scaleFactor: 2.0 })).to.be.empty();
  44. expect(empty.toBitmap()).to.be.empty();
  45. expect(empty.toBitmap({ scaleFactor: 2.0 })).to.be.empty();
  46. expect(empty.toJPEG(100)).to.be.empty();
  47. expect(empty.toPNG()).to.be.empty();
  48. expect(empty.toPNG({ scaleFactor: 2.0 })).to.be.empty();
  49. });
  50. it('serializes and deserializes a non-empty NativeImage', () => {
  51. const dataURL = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAIAAAD91JpzAAAAFklEQVQYlWP8//8/AwMDEwMDAwMDAwAkBgMBBMzldwAAAABJRU5ErkJggg==';
  52. const image = nativeImage.createFromDataURL(dataURL);
  53. const serializedImage = serialize(image);
  54. const nonEmpty = deserialize(serializedImage);
  55. expect(nonEmpty.isEmpty()).to.be.false();
  56. expect(nonEmpty.getAspectRatio()).to.equal(1);
  57. expect(nonEmpty.toDataURL()).to.not.be.empty();
  58. expect(nonEmpty.toDataURL({ scaleFactor: 1.0 })).to.equal(dataURL);
  59. expect(nonEmpty.getSize()).to.deep.equal({ width: 2, height: 2 });
  60. expect(nonEmpty.getBitmap()).to.not.be.empty();
  61. expect(nonEmpty.toPNG()).to.not.be.empty();
  62. });
  63. it('serializes and deserializes a non-empty NativeImage with multiple representations', () => {
  64. const image = nativeImage.createEmpty();
  65. const dataURL1 = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAC0lEQVQYlWNgAAIAAAUAAdafFs0AAAAASUVORK5CYII=';
  66. image.addRepresentation({ scaleFactor: 1.0, dataURL: dataURL1 });
  67. const dataURL2 = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAIAAAD91JpzAAAAFklEQVQYlWP8//8/AwMDEwMDAwMDAwAkBgMBBMzldwAAAABJRU5ErkJggg==';
  68. image.addRepresentation({ scaleFactor: 2.0, dataURL: dataURL2 });
  69. const serializedImage = serialize(image);
  70. const nonEmpty = deserialize(serializedImage);
  71. expect(nonEmpty.isEmpty()).to.be.false();
  72. expect(nonEmpty.getAspectRatio()).to.equal(1);
  73. expect(nonEmpty.getSize()).to.deep.equal({ width: 1, height: 1 });
  74. expect(nonEmpty.getBitmap()).to.not.be.empty();
  75. expect(nonEmpty.getBitmap({ scaleFactor: 1.0 })).to.not.be.empty();
  76. expect(nonEmpty.getBitmap({ scaleFactor: 2.0 })).to.not.be.empty();
  77. expect(nonEmpty.toBitmap()).to.not.be.empty();
  78. expect(nonEmpty.toBitmap({ scaleFactor: 1.0 })).to.not.be.empty();
  79. expect(nonEmpty.toBitmap({ scaleFactor: 2.0 })).to.not.be.empty();
  80. expect(nonEmpty.toPNG()).to.not.be.empty();
  81. expect(nonEmpty.toPNG({ scaleFactor: 1.0 })).to.not.be.empty();
  82. expect(nonEmpty.toPNG({ scaleFactor: 2.0 })).to.not.be.empty();
  83. expect(nonEmpty.toDataURL()).to.not.be.empty();
  84. expect(nonEmpty.toDataURL({ scaleFactor: 1.0 })).to.equal(dataURL1);
  85. expect(nonEmpty.toDataURL({ scaleFactor: 2.0 })).to.equal(dataURL2);
  86. });
  87. it('serializes and deserializes an Array', () => {
  88. const array = [1, 2, 3, 4, 5];
  89. const serialized = serialize(array);
  90. const deserialized = deserialize(serialized);
  91. expect(deserialized).to.deep.equal(array);
  92. });
  93. it('serializes and deserializes a Buffer', () => {
  94. const buffer = Buffer.from('hello world!', 'utf-8');
  95. const serialized = serialize(buffer);
  96. const deserialized = deserialize(serialized);
  97. expect(deserialized).to.deep.equal(buffer);
  98. });
  99. it('serializes and deserializes a Boolean', () => {
  100. const bool = true;
  101. const serialized = serialize(bool);
  102. const deserialized = deserialize(serialized);
  103. expect(deserialized).to.equal(bool);
  104. });
  105. it('serializes and deserializes a Number', () => {
  106. const number = 42;
  107. const serialized = serialize(number);
  108. const deserialized = deserialize(serialized);
  109. expect(deserialized).to.equal(number);
  110. });
  111. it('serializes and deserializes a String', () => {
  112. const str = 'hello world';
  113. const serialized = serialize(str);
  114. const deserialized = deserialize(serialized);
  115. expect(deserialized).to.equal(str);
  116. });
  117. it('serializes and deserializes a simple Object', () => {
  118. const obj = { hello: 'world', 'answer-to-everything': 42 };
  119. const serialized = serialize(obj);
  120. const deserialized = deserialize(serialized);
  121. expect(deserialized).to.deep.equal(obj);
  122. });
  123. });
  124. describe('remote.getGlobal filtering', () => {
  125. it('can return custom values', async () => {
  126. w.webContents.once('remote-get-global', (event, name) => {
  127. event.returnValue = name
  128. })
  129. expect(await remotely(`require('electron').remote.getGlobal('test')`)).to.equal('test')
  130. })
  131. it('throws when no returnValue set', async () => {
  132. w.webContents.once('remote-get-global', (event, name) => {
  133. event.preventDefault()
  134. })
  135. await expect(remotely(`require('electron').remote.getGlobal('test')`)).to.eventually.be.rejected(`Blocked remote.getGlobal('test')`)
  136. })
  137. })
  138. describe('remote.getBuiltin filtering', () => {
  139. it('can return custom values', async () => {
  140. w.webContents.once('remote-get-builtin', (event, name) => {
  141. event.returnValue = name
  142. })
  143. expect(await remotely(`require('electron').remote.getBuiltin('test')`)).to.equal('test')
  144. })
  145. it('throws when no returnValue set', async () => {
  146. w.webContents.once('remote-get-builtin', (event, name) => {
  147. event.preventDefault()
  148. })
  149. await expect(remotely(`require('electron').remote.getBuiltin('test')`)).to.eventually.be.rejected(`Blocked remote.getGlobal('test')`)
  150. })
  151. })
  152. describe('remote.require filtering', () => {
  153. it('can return custom values', async () => {
  154. w.webContents.once('remote-require', (event, name) => {
  155. event.returnValue = name
  156. })
  157. expect(await remotely(`require('electron').remote.require('test')`)).to.equal('test')
  158. })
  159. it('throws when no returnValue set', async () => {
  160. w.webContents.once('remote-require', (event, name) => {
  161. event.preventDefault()
  162. })
  163. await expect(remotely(`require('electron').remote.require('test')`)).to.eventually.be.rejected(`Blocked remote.require('test')`)
  164. })
  165. })
  166. describe('remote.getCurrentWindow filtering', () => {
  167. it('can return custom value', async () => {
  168. w.webContents.once('remote-get-current-window', (e) => {
  169. e.returnValue = 'some window'
  170. })
  171. expect(await remotely(`require('electron').remote.getCurrentWindow()`)).to.equal('some window')
  172. })
  173. it('throws when no returnValue set', async () => {
  174. w.webContents.once('remote-get-current-window', (event) => {
  175. event.preventDefault()
  176. })
  177. await expect(remotely(`require('electron').remote.getCurrentWindow()`)).to.eventually.be.rejected(`Blocked remote.getCurrentWindow()`)
  178. })
  179. })
  180. describe('remote.getCurrentWebContents filtering', () => {
  181. it('can return custom value', async () => {
  182. w.webContents.once('remote-get-current-web-contents', (event) => {
  183. event.returnValue = 'some web contents'
  184. })
  185. expect(await remotely(`require('electron').remote.getCurrentWebContents()`)).to.equal('some web contents')
  186. })
  187. it('throws when no returnValue set', async () => {
  188. w.webContents.once('remote-get-current-web-contents', (event) => {
  189. event.preventDefault()
  190. })
  191. await expect(remotely(`require('electron').remote.getCurrentWebContents()`)).to.eventually.be.rejected(`Blocked remote.getCurrentWebContents()`)
  192. })
  193. })
  194. describe('remote references', () => {
  195. it('render-view-deleted is sent when page is destroyed', (done) => {
  196. w.webContents.once('render-view-deleted' as any, () => {
  197. done()
  198. })
  199. w.destroy()
  200. })
  201. // The ELECTRON_BROWSER_CONTEXT_RELEASE message relies on this to work.
  202. it('message can be sent on exit when page is being navigated', (done) => {
  203. after(() => { ipcMain.removeAllListeners('SENT_ON_EXIT') })
  204. ipcMain.once('SENT_ON_EXIT', () => {
  205. done()
  206. })
  207. w.webContents.once('did-finish-load', () => {
  208. w.webContents.loadURL('about:blank')
  209. })
  210. w.loadFile(path.join(fixtures, 'api', 'send-on-exit.html'))
  211. })
  212. })
  213. })