api-browser-window-affinity-spec.ts 6.4 KB

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