api-web-contents-spec.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 {BrowserWindow, webContents} = remote
  7. const isCi = remote.getGlobal('isCi')
  8. describe('webContents module', function () {
  9. const fixtures = path.resolve(__dirname, 'fixtures')
  10. let w
  11. beforeEach(function () {
  12. w = new BrowserWindow({
  13. show: false,
  14. width: 400,
  15. height: 400,
  16. webPreferences: {
  17. backgroundThrottling: false
  18. }
  19. })
  20. })
  21. afterEach(function () {
  22. return closeWindow(w).then(function () { w = null })
  23. })
  24. describe('getAllWebContents() API', function () {
  25. it('returns an array of web contents', function (done) {
  26. w.webContents.on('devtools-opened', function () {
  27. const all = webContents.getAllWebContents().sort(function (a, b) {
  28. return a.getId() - b.getId()
  29. })
  30. assert.ok(all.length >= 4)
  31. assert.equal(all[0].getType(), 'window')
  32. assert.equal(all[all.length - 2].getType(), 'remote')
  33. assert.equal(all[all.length - 1].getType(), 'webview')
  34. done()
  35. })
  36. w.loadURL('file://' + path.join(fixtures, 'pages', 'webview-zoom-factor.html'))
  37. w.webContents.openDevTools()
  38. })
  39. })
  40. describe('getFocusedWebContents() API', function () {
  41. it('returns the focused web contents', function (done) {
  42. if (isCi) return done()
  43. const specWebContents = remote.getCurrentWebContents()
  44. assert.equal(specWebContents.getId(), webContents.getFocusedWebContents().getId())
  45. specWebContents.once('devtools-opened', function () {
  46. assert.equal(specWebContents.devToolsWebContents.getId(), webContents.getFocusedWebContents().getId())
  47. specWebContents.closeDevTools()
  48. })
  49. specWebContents.once('devtools-closed', function () {
  50. assert.equal(specWebContents.getId(), webContents.getFocusedWebContents().getId())
  51. done()
  52. })
  53. specWebContents.openDevTools()
  54. })
  55. it('does not crash when called on a detached dev tools window', function (done) {
  56. const specWebContents = w.webContents
  57. specWebContents.once('devtools-opened', function () {
  58. assert.doesNotThrow(function () {
  59. webContents.getFocusedWebContents()
  60. })
  61. specWebContents.closeDevTools()
  62. })
  63. specWebContents.once('devtools-closed', function () {
  64. assert.doesNotThrow(function () {
  65. webContents.getFocusedWebContents()
  66. })
  67. done()
  68. })
  69. specWebContents.openDevTools({mode: 'detach'})
  70. w.inspectElement(100, 100)
  71. })
  72. })
  73. describe('isFocused() API', function () {
  74. it('returns false when the window is hidden', function () {
  75. BrowserWindow.getAllWindows().forEach(function (window) {
  76. assert.equal(!window.isVisible() && window.webContents.isFocused(), false)
  77. })
  78. })
  79. })
  80. })