api-browser-view-spec.ts 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. import { expect } from 'chai';
  2. import * as path from 'path';
  3. import { emittedOnce } from './events-helpers';
  4. import { BrowserView, BrowserWindow, webContents } from 'electron/main';
  5. import { closeWindow } from './window-helpers';
  6. import { defer, startRemoteControlApp } from './spec-helpers';
  7. describe('BrowserView module', () => {
  8. const fixtures = path.resolve(__dirname, '..', 'spec', 'fixtures');
  9. let w: BrowserWindow;
  10. let view: BrowserView;
  11. beforeEach(() => {
  12. expect(webContents.getAllWebContents()).to.have.length(0);
  13. w = new BrowserWindow({
  14. show: false,
  15. width: 400,
  16. height: 400,
  17. webPreferences: {
  18. backgroundThrottling: false
  19. }
  20. });
  21. });
  22. afterEach(async () => {
  23. const p = emittedOnce(w.webContents, 'destroyed');
  24. await closeWindow(w);
  25. w = null as any;
  26. await p;
  27. if (view) {
  28. const p = emittedOnce(view.webContents, 'destroyed');
  29. (view.webContents as any).destroy();
  30. view = null as any;
  31. await p;
  32. }
  33. expect(webContents.getAllWebContents()).to.have.length(0);
  34. });
  35. it('can be created with an existing webContents', async () => {
  36. const wc = (webContents as any).create({ sandbox: true });
  37. await wc.loadURL('about:blank');
  38. view = new BrowserView({ webContents: wc } as any);
  39. expect(view.webContents.getURL()).to.equal('about:blank');
  40. });
  41. describe('BrowserView.setBackgroundColor()', () => {
  42. it('does not throw for valid args', () => {
  43. view = new BrowserView();
  44. view.setBackgroundColor('#000');
  45. });
  46. it('throws for invalid args', () => {
  47. view = new BrowserView();
  48. expect(() => {
  49. view.setBackgroundColor(null as any);
  50. }).to.throw(/conversion failure/);
  51. });
  52. });
  53. describe('BrowserView.setAutoResize()', () => {
  54. it('does not throw for valid args', () => {
  55. view = new BrowserView();
  56. view.setAutoResize({});
  57. view.setAutoResize({ width: true, height: false });
  58. });
  59. it('throws for invalid args', () => {
  60. view = new BrowserView();
  61. expect(() => {
  62. view.setAutoResize(null as any);
  63. }).to.throw(/conversion failure/);
  64. });
  65. });
  66. describe('BrowserView.setBounds()', () => {
  67. it('does not throw for valid args', () => {
  68. view = new BrowserView();
  69. view.setBounds({ x: 0, y: 0, width: 1, height: 1 });
  70. });
  71. it('throws for invalid args', () => {
  72. view = new BrowserView();
  73. expect(() => {
  74. view.setBounds(null as any);
  75. }).to.throw(/conversion failure/);
  76. expect(() => {
  77. view.setBounds({} as any);
  78. }).to.throw(/conversion failure/);
  79. });
  80. });
  81. describe('BrowserView.getBounds()', () => {
  82. it('returns the current bounds', () => {
  83. view = new BrowserView();
  84. const bounds = { x: 10, y: 20, width: 30, height: 40 };
  85. view.setBounds(bounds);
  86. expect(view.getBounds()).to.deep.equal(bounds);
  87. });
  88. });
  89. describe('BrowserWindow.setBrowserView()', () => {
  90. it('does not throw for valid args', () => {
  91. view = new BrowserView();
  92. w.setBrowserView(view);
  93. });
  94. it('does not throw if called multiple times with same view', () => {
  95. view = new BrowserView();
  96. w.setBrowserView(view);
  97. w.setBrowserView(view);
  98. w.setBrowserView(view);
  99. });
  100. });
  101. describe('BrowserWindow.getBrowserView()', () => {
  102. it('returns the set view', () => {
  103. view = new BrowserView();
  104. w.setBrowserView(view);
  105. const view2 = w.getBrowserView();
  106. expect(view2!.webContents.id).to.equal(view.webContents.id);
  107. });
  108. it('returns null if none is set', () => {
  109. const view = w.getBrowserView();
  110. expect(view).to.be.null('view');
  111. });
  112. });
  113. describe('BrowserWindow.addBrowserView()', () => {
  114. it('does not throw for valid args', () => {
  115. const view1 = new BrowserView();
  116. defer(() => (view1.webContents as any).destroy());
  117. w.addBrowserView(view1);
  118. defer(() => w.removeBrowserView(view1));
  119. const view2 = new BrowserView();
  120. defer(() => (view2.webContents as any).destroy());
  121. w.addBrowserView(view2);
  122. defer(() => w.removeBrowserView(view2));
  123. });
  124. it('does not throw if called multiple times with same view', () => {
  125. view = new BrowserView();
  126. w.addBrowserView(view);
  127. w.addBrowserView(view);
  128. w.addBrowserView(view);
  129. });
  130. it('does not crash if the BrowserView webContents are destroyed prior to window removal', () => {
  131. expect(() => {
  132. const view1 = new BrowserView();
  133. (view1.webContents as any).destroy();
  134. w.addBrowserView(view1);
  135. }).to.not.throw();
  136. });
  137. it('can handle BrowserView reparenting', async () => {
  138. view = new BrowserView();
  139. w.addBrowserView(view);
  140. view.webContents.loadURL('about:blank');
  141. await emittedOnce(view.webContents, 'did-finish-load');
  142. const w2 = new BrowserWindow({ show: false });
  143. w2.addBrowserView(view);
  144. w.close();
  145. view.webContents.loadURL(`file://${fixtures}/pages/blank.html`);
  146. await emittedOnce(view.webContents, 'did-finish-load');
  147. // Clean up - the afterEach hook assumes the webContents on w is still alive.
  148. w = new BrowserWindow({ show: false });
  149. w2.close();
  150. w2.destroy();
  151. });
  152. });
  153. describe('BrowserWindow.removeBrowserView()', () => {
  154. it('does not throw if called multiple times with same view', () => {
  155. expect(() => {
  156. view = new BrowserView();
  157. w.addBrowserView(view);
  158. w.removeBrowserView(view);
  159. w.removeBrowserView(view);
  160. }).to.not.throw();
  161. });
  162. });
  163. describe('BrowserWindow.getBrowserViews()', () => {
  164. it('returns same views as was added', () => {
  165. const view1 = new BrowserView();
  166. defer(() => (view1.webContents as any).destroy());
  167. w.addBrowserView(view1);
  168. defer(() => w.removeBrowserView(view1));
  169. const view2 = new BrowserView();
  170. defer(() => (view2.webContents as any).destroy());
  171. w.addBrowserView(view2);
  172. defer(() => w.removeBrowserView(view2));
  173. const views = w.getBrowserViews();
  174. expect(views).to.have.lengthOf(2);
  175. expect(views[0].webContents.id).to.equal(view1.webContents.id);
  176. expect(views[1].webContents.id).to.equal(view2.webContents.id);
  177. });
  178. });
  179. describe('BrowserWindow.setTopBrowserView()', () => {
  180. it('should throw an error when a BrowserView is not attached to the window', () => {
  181. view = new BrowserView();
  182. expect(() => {
  183. w.setTopBrowserView(view);
  184. }).to.throw(/is not attached/);
  185. });
  186. it('should throw an error when a BrowserView is attached to some other window', () => {
  187. view = new BrowserView();
  188. const win2 = new BrowserWindow();
  189. w.addBrowserView(view);
  190. view.setBounds({ x: 0, y: 0, width: 100, height: 100 });
  191. win2.addBrowserView(view);
  192. expect(() => {
  193. w.setTopBrowserView(view);
  194. }).to.throw(/is not attached/);
  195. win2.close();
  196. win2.destroy();
  197. });
  198. });
  199. describe('BrowserView.webContents.getOwnerBrowserWindow()', () => {
  200. it('points to owning window', () => {
  201. view = new BrowserView();
  202. expect(view.webContents.getOwnerBrowserWindow()).to.be.null('owner browser window');
  203. w.setBrowserView(view);
  204. expect(view.webContents.getOwnerBrowserWindow()).to.equal(w);
  205. w.setBrowserView(null);
  206. expect(view.webContents.getOwnerBrowserWindow()).to.be.null('owner browser window');
  207. });
  208. });
  209. describe('shutdown behavior', () => {
  210. it('does not crash on exit', async () => {
  211. const rc = await startRemoteControlApp();
  212. await rc.remotely(() => {
  213. const { BrowserView, app } = require('electron');
  214. new BrowserView({}) // eslint-disable-line
  215. setTimeout(() => {
  216. app.quit();
  217. });
  218. });
  219. const [code] = await emittedOnce(rc.process, 'exit');
  220. expect(code).to.equal(0);
  221. });
  222. it('does not crash on exit if added to a browser window', async () => {
  223. const rc = await startRemoteControlApp();
  224. await rc.remotely(() => {
  225. const { app, BrowserView, BrowserWindow } = require('electron');
  226. const bv = new BrowserView();
  227. bv.webContents.loadURL('about:blank');
  228. const bw = new BrowserWindow({ show: false });
  229. bw.addBrowserView(bv);
  230. setTimeout(() => {
  231. app.quit();
  232. });
  233. });
  234. const [code] = await emittedOnce(rc.process, 'exit');
  235. expect(code).to.equal(0);
  236. });
  237. });
  238. describe('window.open()', () => {
  239. it('works in BrowserView', (done) => {
  240. view = new BrowserView();
  241. w.setBrowserView(view);
  242. view.webContents.setWindowOpenHandler(({ url, frameName }) => {
  243. expect(url).to.equal('http://host/');
  244. expect(frameName).to.equal('host');
  245. done();
  246. return { action: 'deny' };
  247. });
  248. view.webContents.loadFile(path.join(fixtures, 'pages', 'window-open.html'));
  249. });
  250. });
  251. });