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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. 'use strict';
  2. const { expect } = require('chai');
  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. async function createWindowWithWebPrefs (webPrefs) {
  13. const w = new BrowserWindow({
  14. show: false,
  15. width: 400,
  16. height: 400,
  17. webPreferences: webPrefs || {}
  18. });
  19. await w.loadFile(path.join(fixtures, 'api', 'blank.html'));
  20. return w;
  21. }
  22. function testAffinityProcessIds (name, webPreferences = {}) {
  23. describe(name, () => {
  24. let mAffinityWindow;
  25. before(async () => {
  26. mAffinityWindow = await createWindowWithWebPrefs({ affinity: myAffinityName, ...webPreferences });
  27. });
  28. after(async () => {
  29. await closeWindow(mAffinityWindow, { assertSingleWindow: false });
  30. mAffinityWindow = null;
  31. });
  32. it('should have a different process id than a default window', async () => {
  33. const w = await createWindowWithWebPrefs({ ...webPreferences });
  34. const affinityID = mAffinityWindow.webContents.getOSProcessId();
  35. const wcID = w.webContents.getOSProcessId();
  36. expect(affinityID).to.not.equal(wcID, 'Should have different OS process IDs');
  37. await closeWindow(w, { assertSingleWindow: false });
  38. });
  39. it(`should have a different process id than a window with a different affinity '${anotherAffinityName}'`, async () => {
  40. const w = await createWindowWithWebPrefs({ affinity: anotherAffinityName, ...webPreferences });
  41. const affinityID = mAffinityWindow.webContents.getOSProcessId();
  42. const wcID = w.webContents.getOSProcessId();
  43. expect(affinityID).to.not.equal(wcID, 'Should have different OS process IDs');
  44. await closeWindow(w, { assertSingleWindow: false });
  45. });
  46. it(`should have the same OS process id than a window with the same affinity '${myAffinityName}'`, async () => {
  47. const w = await createWindowWithWebPrefs({ affinity: myAffinityName, ...webPreferences });
  48. const affinityID = mAffinityWindow.webContents.getOSProcessId();
  49. const wcID = w.webContents.getOSProcessId();
  50. expect(affinityID).to.equal(wcID, 'Should have the same OS process ID');
  51. await closeWindow(w, { assertSingleWindow: false });
  52. });
  53. it(`should have the same OS process id than a window with an equivalent affinity '${myAffinityNameUpper}' (case insensitive)`, async () => {
  54. const w = await createWindowWithWebPrefs({ affinity: myAffinityNameUpper, ...webPreferences });
  55. const affinityID = mAffinityWindow.webContents.getOSProcessId();
  56. const wcID = w.webContents.getOSProcessId();
  57. expect(affinityID).to.equal(wcID, 'Should have the same OS process ID');
  58. await closeWindow(w, { assertSingleWindow: false });
  59. });
  60. });
  61. }
  62. testAffinityProcessIds(`BrowserWindow with an affinity '${myAffinityName}'`);
  63. testAffinityProcessIds(`BrowserWindow with an affinity '${myAffinityName}' and sandbox enabled`, { sandbox: true });
  64. testAffinityProcessIds(`BrowserWindow with an affinity '${myAffinityName}' and nativeWindowOpen enabled`, { nativeWindowOpen: true });
  65. describe(`BrowserWindow with an affinity : nodeIntegration=false`, () => {
  66. const preload = path.join(fixtures, 'module', 'send-later.js');
  67. const affinityWithNodeTrue = 'affinityWithNodeTrue';
  68. const affinityWithNodeFalse = 'affinityWithNodeFalse';
  69. function testNodeIntegration (present) {
  70. return new Promise((resolve, reject) => {
  71. ipcMain.once('answer', (event, typeofProcess, typeofBuffer) => {
  72. if (present) {
  73. expect(typeofProcess).to.not.equal('undefined');
  74. expect(typeofBuffer).to.not.equal('undefined');
  75. } else {
  76. expect(typeofProcess).to.equal('undefined');
  77. expect(typeofBuffer).to.equal('undefined');
  78. }
  79. resolve();
  80. });
  81. });
  82. }
  83. it('disables node integration when specified to false', async () => {
  84. const [, w] = await Promise.all([
  85. testNodeIntegration(false),
  86. createWindowWithWebPrefs({
  87. affinity: affinityWithNodeTrue,
  88. preload,
  89. nodeIntegration: false
  90. })
  91. ]);
  92. await closeWindow(w, { assertSingleWindow: false });
  93. });
  94. it('disables node integration when first window is false', async () => {
  95. const [, w1] = await Promise.all([
  96. testNodeIntegration(false),
  97. createWindowWithWebPrefs({
  98. affinity: affinityWithNodeTrue,
  99. preload,
  100. nodeIntegration: false
  101. })
  102. ]);
  103. const [, w2] = await Promise.all([
  104. testNodeIntegration(false),
  105. createWindowWithWebPrefs({
  106. affinity: affinityWithNodeTrue,
  107. preload,
  108. nodeIntegration: true
  109. })
  110. ]);
  111. await Promise.all([
  112. closeWindow(w1, { assertSingleWindow: false }),
  113. closeWindow(w2, { assertSingleWindow: false })
  114. ]);
  115. });
  116. it('enables node integration when specified to true', async () => {
  117. const [, w] = await Promise.all([
  118. testNodeIntegration(true),
  119. createWindowWithWebPrefs({
  120. affinity: affinityWithNodeFalse,
  121. preload,
  122. nodeIntegration: true
  123. })
  124. ]);
  125. await closeWindow(w, { assertSingleWindow: false });
  126. });
  127. it('enables node integration when first window is true', async () => {
  128. const [, w1] = await Promise.all([
  129. testNodeIntegration(true),
  130. createWindowWithWebPrefs({
  131. affinity: affinityWithNodeFalse,
  132. preload,
  133. nodeIntegration: true
  134. })
  135. ]);
  136. const [, w2] = await Promise.all([
  137. testNodeIntegration(true),
  138. createWindowWithWebPrefs({
  139. affinity: affinityWithNodeFalse,
  140. preload,
  141. nodeIntegration: false
  142. })
  143. ]);
  144. await Promise.all([
  145. closeWindow(w1, { assertSingleWindow: false }),
  146. closeWindow(w2, { assertSingleWindow: false })
  147. ]);
  148. });
  149. });
  150. });