window-helpers.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. const { expect } = require('chai')
  2. const { remote } = require('electron')
  3. const { BrowserWindow } = remote
  4. const { emittedOnce } = require('./events-helpers')
  5. exports.closeWindow = async (window = null,
  6. { assertSingleWindow } = { assertSingleWindow: true }) => {
  7. const windowExists = (window !== null) && !window.isDestroyed()
  8. if (windowExists) {
  9. const isClosed = emittedOnce(window, 'closed')
  10. window.setClosable(true)
  11. window.close()
  12. await isClosed
  13. }
  14. if (assertSingleWindow) {
  15. // Although we want to assert that no windows were left handing around
  16. // we also want to clean up the left over windows so that no future
  17. // tests fail as a side effect
  18. const currentId = remote.getCurrentWindow().id
  19. const windows = BrowserWindow.getAllWindows()
  20. for (const win of windows) {
  21. if (win.id !== currentId) {
  22. const closePromise = emittedOnce(win, 'closed')
  23. win.close()
  24. await closePromise
  25. }
  26. }
  27. expect(windows).to.have.lengthOf(1)
  28. }
  29. }
  30. exports.waitForWebContentsToLoad = async (webContents) => {
  31. const didFinishLoadPromise = emittedOnce(webContents, 'did-finish-load')
  32. if (webContents.isLoadingMainFrame()) {
  33. await didFinishLoadPromise
  34. }
  35. }