extensions-spec.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { expect } from 'chai'
  2. import { session, BrowserWindow, ipcMain } from 'electron'
  3. import { closeAllWindows } from './window-helpers'
  4. import * as http from 'http'
  5. import { AddressInfo } from 'net'
  6. import * as path from 'path'
  7. import { ifdescribe } from './spec-helpers'
  8. import { emittedOnce } from './events-helpers'
  9. const fixtures = path.join(__dirname, 'fixtures')
  10. ifdescribe(process.electronBinding('features').isExtensionsEnabled())('chrome extensions', () => {
  11. // NB. extensions are only allowed on http://, https:// and ftp:// (!) urls by default.
  12. let server: http.Server
  13. let url: string
  14. before(async () => {
  15. server = http.createServer((req, res) => res.end())
  16. await new Promise(resolve => server.listen(0, '127.0.0.1', () => {
  17. url = `http://127.0.0.1:${(server.address() as AddressInfo).port}`
  18. resolve()
  19. }))
  20. })
  21. after(() => {
  22. server.close()
  23. })
  24. afterEach(closeAllWindows)
  25. it('loads an extension', async () => {
  26. // NB. we have to use a persist: session (i.e. non-OTR) because the
  27. // extension registry is redirected to the main session. so installing an
  28. // extension in an in-memory session results in it being installed in the
  29. // default session.
  30. const customSession = session.fromPartition(`persist:${require('uuid').v4()}`);
  31. (customSession as any).loadChromeExtension(path.join(fixtures, 'extensions', 'red-bg'));
  32. const w = new BrowserWindow({show: false, webPreferences: {session: customSession}})
  33. await w.loadURL(url);
  34. const bg = await w.webContents.executeJavaScript('document.documentElement.style.backgroundColor');
  35. expect(bg).to.equal('red');
  36. });
  37. it('confines an extension to the session it was loaded in', async () => {
  38. const customSession = session.fromPartition(`persist:${require('uuid').v4()}`);
  39. (customSession as any).loadChromeExtension(path.join(fixtures, 'extensions', 'red-bg'))
  40. const w = new BrowserWindow({show: false}) // not in the session
  41. await w.loadURL(url)
  42. const bg = await w.webContents.executeJavaScript('document.documentElement.style.backgroundColor')
  43. expect(bg).to.equal('')
  44. })
  45. describe('chrome.runtime', () => {
  46. let content: any
  47. before(async () => {
  48. const customSession = session.fromPartition(`persist:${require('uuid').v4()}`);
  49. (customSession as any).loadChromeExtension(path.join(fixtures, 'extensions', 'chrome-runtime'))
  50. const w = new BrowserWindow({show: false, webPreferences: { session: customSession }})
  51. try {
  52. await w.loadURL(url)
  53. content = JSON.parse(await w.webContents.executeJavaScript('document.documentElement.textContent'))
  54. expect(content).to.be.an('object')
  55. } finally {
  56. w.destroy()
  57. }
  58. })
  59. it('getManifest()', () => {
  60. expect(content.manifest).to.be.an('object').with.property('name', 'chrome-runtime')
  61. })
  62. it('id', () => {
  63. expect(content.id).to.be.a('string').with.lengthOf(32)
  64. })
  65. it('getURL()', () => {
  66. expect(content.url).to.be.a('string').and.match(/^chrome-extension:\/\/.*main.js$/)
  67. })
  68. })
  69. describe('chrome.storage', () => {
  70. it('stores and retrieves a key', async () => {
  71. const customSession = session.fromPartition(`persist:${require('uuid').v4()}`);
  72. (customSession as any).loadChromeExtension(path.join(fixtures, 'extensions', 'chrome-storage'))
  73. const w = new BrowserWindow({show: false, webPreferences: { session: customSession, nodeIntegration: true }})
  74. try {
  75. const p = emittedOnce(ipcMain, 'storage-success')
  76. await w.loadURL(url)
  77. const [, v] = await p
  78. expect(v).to.equal('value')
  79. } finally {
  80. w.destroy()
  81. }
  82. })
  83. })
  84. })