api-web-contents-view-spec.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. 'use strict'
  2. const assert = require('assert')
  3. const chai = require('chai')
  4. const ChildProcess = require('child_process')
  5. const dirtyChai = require('dirty-chai')
  6. const path = require('path')
  7. const { emittedOnce } = require('./events-helpers')
  8. const { closeWindow } = require('./window-helpers')
  9. const { remote } = require('electron')
  10. const { webContents, TopLevelWindow, WebContentsView } = remote
  11. const { expect } = chai
  12. chai.use(dirtyChai)
  13. describe('WebContentsView', () => {
  14. let w = null
  15. afterEach(() => closeWindow(w).then(() => { w = null }))
  16. it('can be used as content view', () => {
  17. const web = webContents.create({})
  18. w = new TopLevelWindow({ show: false })
  19. w.setContentView(new WebContentsView(web))
  20. })
  21. it('prevents adding same WebContents', () => {
  22. const web = webContents.create({})
  23. w = new TopLevelWindow({ show: false })
  24. w.setContentView(new WebContentsView(web))
  25. assert.throws(() => {
  26. w.setContentView(new WebContentsView(web))
  27. }, /The WebContents has already been added to a View/)
  28. })
  29. describe('new WebContentsView()', () => {
  30. it('does not crash on exit', async () => {
  31. const appPath = path.join(__dirname, 'fixtures', 'api', 'leak-exit-webcontentsview.js')
  32. const electronPath = remote.getGlobal('process').execPath
  33. const appProcess = ChildProcess.spawn(electronPath, [appPath])
  34. const [code] = await emittedOnce(appProcess, 'close')
  35. expect(code).to.equal(0)
  36. })
  37. })
  38. })