guest-window-manager-spec.ts 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. import { BrowserWindow, screen } from 'electron';
  2. import { expect, assert } from 'chai';
  3. import { areColorsSimilar, captureScreen, HexColors, getPixelColor } from './lib/screen-helpers';
  4. import { ifit } from './lib/spec-helpers';
  5. import { closeAllWindows } from './lib/window-helpers';
  6. import { once } from 'node:events';
  7. import { setTimeout as setTimeoutAsync } from 'node:timers/promises';
  8. describe('webContents.setWindowOpenHandler', () => {
  9. let browserWindow: BrowserWindow;
  10. beforeEach(async () => {
  11. browserWindow = new BrowserWindow({ show: false });
  12. await browserWindow.loadURL('about:blank');
  13. });
  14. afterEach(closeAllWindows);
  15. it('does not fire window creation events if the handler callback throws an error', (done) => {
  16. const error = new Error('oh no');
  17. const listeners = process.listeners('uncaughtException');
  18. process.removeAllListeners('uncaughtException');
  19. process.on('uncaughtException', (thrown) => {
  20. try {
  21. expect(thrown).to.equal(error);
  22. done();
  23. } catch (e) {
  24. done(e);
  25. } finally {
  26. process.removeAllListeners('uncaughtException');
  27. for (const listener of listeners) {
  28. process.on('uncaughtException', listener);
  29. }
  30. }
  31. });
  32. browserWindow.webContents.on('did-create-window', () => {
  33. assert.fail('did-create-window should not be called with an overridden window.open');
  34. });
  35. browserWindow.webContents.executeJavaScript("window.open('about:blank', '', 'show=no') && true");
  36. browserWindow.webContents.setWindowOpenHandler(() => {
  37. throw error;
  38. });
  39. });
  40. it('does not fire window creation events if the handler callback returns a bad result', async () => {
  41. const bad = new Promise((resolve) => {
  42. browserWindow.webContents.setWindowOpenHandler(() => {
  43. setTimeout(resolve);
  44. return [1, 2, 3] as any;
  45. });
  46. });
  47. browserWindow.webContents.on('did-create-window', () => {
  48. assert.fail('did-create-window should not be called with an overridden window.open');
  49. });
  50. browserWindow.webContents.executeJavaScript("window.open('about:blank', '', 'show=no') && true");
  51. await bad;
  52. });
  53. it('does not fire window creation events if an override returns action: deny', async () => {
  54. const denied = new Promise((resolve) => {
  55. browserWindow.webContents.setWindowOpenHandler(() => {
  56. setTimeout(resolve);
  57. return { action: 'deny' };
  58. });
  59. });
  60. browserWindow.webContents.on('did-create-window', () => {
  61. assert.fail('did-create-window should not be called with an overridden window.open');
  62. });
  63. browserWindow.webContents.executeJavaScript("window.open('about:blank', '', 'show=no') && true");
  64. await denied;
  65. });
  66. it('is called when clicking on a target=_blank link', async () => {
  67. const denied = new Promise((resolve) => {
  68. browserWindow.webContents.setWindowOpenHandler(() => {
  69. setTimeout(resolve);
  70. return { action: 'deny' };
  71. });
  72. });
  73. browserWindow.webContents.on('did-create-window', () => {
  74. assert.fail('did-create-window should not be called with an overridden window.open');
  75. });
  76. await browserWindow.webContents.loadURL('data:text/html,<a target="_blank" href="http://example.com" style="display: block; width: 100%; height: 100%; position: fixed; left: 0; top: 0;">link</a>');
  77. browserWindow.webContents.sendInputEvent({ type: 'mouseDown', x: 10, y: 10, button: 'left', clickCount: 1 });
  78. browserWindow.webContents.sendInputEvent({ type: 'mouseUp', x: 10, y: 10, button: 'left', clickCount: 1 });
  79. await denied;
  80. });
  81. it('is called when shift-clicking on a link', async () => {
  82. const denied = new Promise((resolve) => {
  83. browserWindow.webContents.setWindowOpenHandler(() => {
  84. setTimeout(resolve);
  85. return { action: 'deny' };
  86. });
  87. });
  88. browserWindow.webContents.on('did-create-window', () => {
  89. assert.fail('did-create-window should not be called with an overridden window.open');
  90. });
  91. await browserWindow.webContents.loadURL('data:text/html,<a href="http://example.com" style="display: block; width: 100%; height: 100%; position: fixed; left: 0; top: 0;">link</a>');
  92. browserWindow.webContents.sendInputEvent({ type: 'mouseDown', x: 10, y: 10, button: 'left', clickCount: 1, modifiers: ['shift'] });
  93. browserWindow.webContents.sendInputEvent({ type: 'mouseUp', x: 10, y: 10, button: 'left', clickCount: 1, modifiers: ['shift'] });
  94. await denied;
  95. });
  96. it('fires handler with correct params', async () => {
  97. const testFrameName = 'test-frame-name';
  98. const testFeatures = 'top=10&left=10&something-unknown&show=no';
  99. const testUrl = 'app://does-not-exist/';
  100. const details = await new Promise<Electron.HandlerDetails>(resolve => {
  101. browserWindow.webContents.setWindowOpenHandler((details) => {
  102. setTimeout(() => resolve(details));
  103. return { action: 'deny' };
  104. });
  105. browserWindow.webContents.executeJavaScript(`window.open('${testUrl}', '${testFrameName}', '${testFeatures}') && true`);
  106. });
  107. const { url, frameName, features, disposition, referrer } = details;
  108. expect(url).to.equal(testUrl);
  109. expect(frameName).to.equal(testFrameName);
  110. expect(features).to.equal(testFeatures);
  111. expect(disposition).to.equal('new-window');
  112. expect(referrer).to.deep.equal({
  113. policy: 'strict-origin-when-cross-origin',
  114. url: ''
  115. });
  116. });
  117. it('includes post body', async () => {
  118. const details = await new Promise<Electron.HandlerDetails>(resolve => {
  119. browserWindow.webContents.setWindowOpenHandler((details) => {
  120. setTimeout(() => resolve(details));
  121. return { action: 'deny' };
  122. });
  123. browserWindow.webContents.loadURL(`data:text/html,${encodeURIComponent(`
  124. <form action="http://example.com" target="_blank" method="POST" id="form">
  125. <input name="key" value="value"></input>
  126. </form>
  127. <script>form.submit()</script>
  128. `)}`);
  129. });
  130. const { url, frameName, features, disposition, referrer, postBody } = details;
  131. expect(url).to.equal('http://example.com/');
  132. expect(frameName).to.equal('');
  133. expect(features).to.deep.equal('');
  134. expect(disposition).to.equal('foreground-tab');
  135. expect(referrer).to.deep.equal({
  136. policy: 'strict-origin-when-cross-origin',
  137. url: ''
  138. });
  139. expect(postBody).to.deep.equal({
  140. contentType: 'application/x-www-form-urlencoded',
  141. data: [{
  142. type: 'rawData',
  143. bytes: Buffer.from('key=value')
  144. }]
  145. });
  146. });
  147. it('does fire window creation events if an override returns action: allow', async () => {
  148. browserWindow.webContents.setWindowOpenHandler(() => ({ action: 'allow' }));
  149. setImmediate(() => {
  150. browserWindow.webContents.executeJavaScript("window.open('about:blank', '', 'show=no') && true");
  151. });
  152. await once(browserWindow.webContents, 'did-create-window');
  153. });
  154. it('can change webPreferences of child windows', async () => {
  155. browserWindow.webContents.setWindowOpenHandler(() => ({ action: 'allow', overrideBrowserWindowOptions: { webPreferences: { defaultFontSize: 30 } } }));
  156. const didCreateWindow = once(browserWindow.webContents, 'did-create-window') as Promise<[BrowserWindow, Electron.DidCreateWindowDetails]>;
  157. browserWindow.webContents.executeJavaScript("window.open('about:blank', '', 'show=no') && true");
  158. const [childWindow] = await didCreateWindow;
  159. await childWindow.webContents.executeJavaScript("document.write('hello')");
  160. const size = await childWindow.webContents.executeJavaScript("getComputedStyle(document.querySelector('body')).fontSize");
  161. expect(size).to.equal('30px');
  162. });
  163. it('does not hang parent window when denying window.open', async () => {
  164. browserWindow.webContents.setWindowOpenHandler(() => ({ action: 'deny' }));
  165. browserWindow.webContents.executeJavaScript("window.open('https://127.0.0.1')");
  166. expect(await browserWindow.webContents.executeJavaScript('42')).to.equal(42);
  167. });
  168. // Linux and arm64 platforms (WOA and macOS) do not return any capture sources
  169. ifit(process.platform === 'darwin' && process.arch === 'x64')('should not make child window background transparent', async () => {
  170. browserWindow.webContents.setWindowOpenHandler(() => ({ action: 'allow' }));
  171. const didCreateWindow = once(browserWindow.webContents, 'did-create-window');
  172. browserWindow.webContents.executeJavaScript("window.open('about:blank') && true");
  173. const [childWindow] = await didCreateWindow;
  174. const display = screen.getPrimaryDisplay();
  175. childWindow.setBounds(display.bounds);
  176. await childWindow.webContents.executeJavaScript("const meta = document.createElement('meta'); meta.name = 'color-scheme'; meta.content = 'dark'; document.head.appendChild(meta); true;");
  177. await setTimeoutAsync(1000);
  178. const screenCapture = await captureScreen();
  179. const centerColor = getPixelColor(screenCapture, {
  180. x: display.size.width / 2,
  181. y: display.size.height / 2
  182. });
  183. // color-scheme is set to dark so background should not be white
  184. expect(areColorsSimilar(centerColor, HexColors.WHITE)).to.be.false();
  185. });
  186. });