api-browser-window-affinity-spec.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. 'use strict'
  2. const assert = require('assert')
  3. const path = require('path')
  4. const { remote } = require('electron')
  5. const { ipcMain, BrowserWindow } = remote
  6. const {closeWindow} = require('./window-helpers')
  7. describe('BrowserWindow with affinity module', () => {
  8. const fixtures = path.resolve(__dirname, 'fixtures')
  9. const myAffinityName = 'myAffinity'
  10. const myAffinityNameUpper = 'MYAFFINITY'
  11. const anotherAffinityName = 'anotherAffinity'
  12. function createWindowWithWebPrefs (webPrefs) {
  13. return new Promise((resolve, reject) => {
  14. const w = new BrowserWindow({
  15. show: false,
  16. width: 400,
  17. height: 400,
  18. webPreferences: webPrefs || {}
  19. })
  20. w.webContents.on('did-finish-load', () => {
  21. resolve(w)
  22. })
  23. w.loadURL('file://' + path.join(fixtures, 'api', 'blank.html'))
  24. })
  25. }
  26. describe(`BrowserWindow with an affinity '${myAffinityName}'`, () => {
  27. let mAffinityWindow
  28. before((done) => {
  29. createWindowWithWebPrefs({ affinity: myAffinityName })
  30. .then((w) => {
  31. mAffinityWindow = w
  32. done()
  33. })
  34. })
  35. after((done) => {
  36. closeWindow(mAffinityWindow, {assertSingleWindow: false}).then(() => {
  37. mAffinityWindow = null
  38. done()
  39. })
  40. })
  41. it('should have a different process id than a default window', (done) => {
  42. createWindowWithWebPrefs({})
  43. .then((w) => {
  44. assert.notEqual(mAffinityWindow.webContents.getOSProcessId(), w.webContents.getOSProcessId(), 'Should have the different OS process Id/s')
  45. closeWindow(w, {assertSingleWindow: false}).then(() => {
  46. done()
  47. })
  48. })
  49. })
  50. it(`should have a different process id than a window with a different affinity '${anotherAffinityName}'`, (done) => {
  51. createWindowWithWebPrefs({ affinity: anotherAffinityName })
  52. .then((w) => {
  53. assert.notEqual(mAffinityWindow.webContents.getOSProcessId(), w.webContents.getOSProcessId(), 'Should have the different OS process Id/s')
  54. closeWindow(w, {assertSingleWindow: false}).then(() => {
  55. done()
  56. })
  57. })
  58. })
  59. it(`should have the same OS process id than a window with the same affinity '${myAffinityName}'`, (done) => {
  60. createWindowWithWebPrefs({ affinity: myAffinityName })
  61. .then((w) => {
  62. assert.equal(mAffinityWindow.webContents.getOSProcessId(), w.webContents.getOSProcessId(), 'Should have the same OS process Id')
  63. closeWindow(w, {assertSingleWindow: false}).then(() => {
  64. done()
  65. })
  66. })
  67. })
  68. it(`should have the same OS process id than a window with an equivalent affinity '${myAffinityNameUpper}' (case insensitive)`, (done) => {
  69. createWindowWithWebPrefs({ affinity: myAffinityNameUpper })
  70. .then((w) => {
  71. assert.equal(mAffinityWindow.webContents.getOSProcessId(), w.webContents.getOSProcessId(), 'Should have the same OS process Id')
  72. closeWindow(w, {assertSingleWindow: false}).then(() => {
  73. done()
  74. })
  75. })
  76. })
  77. })
  78. describe(`BrowserWindow with an affinity : nodeIntegration=false`, () => {
  79. const preload = path.join(fixtures, 'module', 'send-later.js')
  80. const affinityWithNodeTrue = 'affinityWithNodeTrue'
  81. const affinityWithNodeFalse = 'affinityWithNodeFalse'
  82. function testNodeIntegration (present) {
  83. return new Promise((resolve, reject) => {
  84. ipcMain.once('answer', (event, typeofProcess, typeofBuffer) => {
  85. if (present) {
  86. assert.notEqual(typeofProcess, 'undefined')
  87. assert.notEqual(typeofBuffer, 'undefined')
  88. } else {
  89. assert.equal(typeofProcess, 'undefined')
  90. assert.equal(typeofBuffer, 'undefined')
  91. }
  92. resolve()
  93. })
  94. })
  95. }
  96. it('disables node integration when specified to false', (done) => {
  97. Promise.all([testNodeIntegration(false), createWindowWithWebPrefs({ affinity: affinityWithNodeTrue, preload: preload, nodeIntegration: false })])
  98. .then((args) => {
  99. closeWindow(args[1], {assertSingleWindow: false}).then(() => {
  100. done()
  101. })
  102. })
  103. })
  104. it('disables node integration when first window is false', (done) => {
  105. Promise.all([testNodeIntegration(false), createWindowWithWebPrefs({ affinity: affinityWithNodeTrue, preload: preload, nodeIntegration: false })])
  106. .then((args) => {
  107. let w1 = args[1]
  108. return Promise.all([testNodeIntegration(false), w1, createWindowWithWebPrefs({ affinity: affinityWithNodeTrue, preload: preload, nodeIntegration: true })])
  109. })
  110. .then((ws) => {
  111. return Promise.all([closeWindow(ws[1], {assertSingleWindow: false}), closeWindow(ws[2], {assertSingleWindow: false})])
  112. })
  113. .then(() => {
  114. done()
  115. })
  116. })
  117. it('enables node integration when specified to true', (done) => {
  118. Promise.all([testNodeIntegration(true), createWindowWithWebPrefs({ affinity: affinityWithNodeFalse, preload: preload, nodeIntegration: true })])
  119. .then((args) => {
  120. closeWindow(args[1], {assertSingleWindow: false}).then(() => {
  121. done()
  122. })
  123. })
  124. })
  125. it('enables node integration when first window is true', (done) => {
  126. Promise.all([testNodeIntegration(true), createWindowWithWebPrefs({ affinity: affinityWithNodeFalse, preload: preload, nodeIntegration: true })])
  127. .then((args) => {
  128. let w1 = args[1]
  129. return Promise.all([testNodeIntegration(true), w1, createWindowWithWebPrefs({ affinity: affinityWithNodeFalse, preload: preload, nodeIntegration: false })])
  130. })
  131. .then((ws) => {
  132. return Promise.all([closeWindow(ws[1], {assertSingleWindow: false}), closeWindow(ws[2], {assertSingleWindow: false})])
  133. })
  134. .then(() => {
  135. done()
  136. })
  137. })
  138. })
  139. })