api-browser-view-spec.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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 addition', () => {
  131. expect(() => {
  132. const view1 = new BrowserView();
  133. (view1.webContents as any).destroy();
  134. w.addBrowserView(view1);
  135. }).to.not.throw();
  136. });
  137. it('does not crash if the webContents is destroyed after a URL is loaded', () => {
  138. view = new BrowserView();
  139. expect(async () => {
  140. view.setBounds({ x: 0, y: 0, width: 400, height: 300 });
  141. await view.webContents.loadURL('data:text/html,hello there');
  142. view.webContents.destroy();
  143. }).to.not.throw();
  144. });
  145. it('can handle BrowserView reparenting', async () => {
  146. view = new BrowserView();
  147. w.addBrowserView(view);
  148. view.webContents.loadURL('about:blank');
  149. await emittedOnce(view.webContents, 'did-finish-load');
  150. const w2 = new BrowserWindow({ show: false });
  151. w2.addBrowserView(view);
  152. w.close();
  153. view.webContents.loadURL(`file://${fixtures}/pages/blank.html`);
  154. await emittedOnce(view.webContents, 'did-finish-load');
  155. // Clean up - the afterEach hook assumes the webContents on w is still alive.
  156. w = new BrowserWindow({ show: false });
  157. w2.close();
  158. w2.destroy();
  159. });
  160. });
  161. describe('BrowserWindow.removeBrowserView()', () => {
  162. it('does not throw if called multiple times with same view', () => {
  163. expect(() => {
  164. view = new BrowserView();
  165. w.addBrowserView(view);
  166. w.removeBrowserView(view);
  167. w.removeBrowserView(view);
  168. }).to.not.throw();
  169. });
  170. });
  171. describe('BrowserWindow.getBrowserViews()', () => {
  172. it('returns same views as was added', () => {
  173. const view1 = new BrowserView();
  174. defer(() => (view1.webContents as any).destroy());
  175. w.addBrowserView(view1);
  176. defer(() => w.removeBrowserView(view1));
  177. const view2 = new BrowserView();
  178. defer(() => (view2.webContents as any).destroy());
  179. w.addBrowserView(view2);
  180. defer(() => w.removeBrowserView(view2));
  181. const views = w.getBrowserViews();
  182. expect(views).to.have.lengthOf(2);
  183. expect(views[0].webContents.id).to.equal(view1.webContents.id);
  184. expect(views[1].webContents.id).to.equal(view2.webContents.id);
  185. });
  186. });
  187. describe('BrowserWindow.setTopBrowserView()', () => {
  188. it('should throw an error when a BrowserView is not attached to the window', () => {
  189. view = new BrowserView();
  190. expect(() => {
  191. w.setTopBrowserView(view);
  192. }).to.throw(/is not attached/);
  193. });
  194. it('should throw an error when a BrowserView is attached to some other window', () => {
  195. view = new BrowserView();
  196. const win2 = new BrowserWindow();
  197. w.addBrowserView(view);
  198. view.setBounds({ x: 0, y: 0, width: 100, height: 100 });
  199. win2.addBrowserView(view);
  200. expect(() => {
  201. w.setTopBrowserView(view);
  202. }).to.throw(/is not attached/);
  203. win2.close();
  204. win2.destroy();
  205. });
  206. });
  207. describe('BrowserView.webContents.getOwnerBrowserWindow()', () => {
  208. it('points to owning window', () => {
  209. view = new BrowserView();
  210. expect(view.webContents.getOwnerBrowserWindow()).to.be.null('owner browser window');
  211. w.setBrowserView(view);
  212. expect(view.webContents.getOwnerBrowserWindow()).to.equal(w);
  213. w.setBrowserView(null);
  214. expect(view.webContents.getOwnerBrowserWindow()).to.be.null('owner browser window');
  215. });
  216. });
  217. describe('shutdown behavior', () => {
  218. it('does not crash on exit', async () => {
  219. const rc = await startRemoteControlApp();
  220. await rc.remotely(() => {
  221. const { BrowserView, app } = require('electron');
  222. new BrowserView({}) // eslint-disable-line
  223. setTimeout(() => {
  224. app.quit();
  225. });
  226. });
  227. const [code] = await emittedOnce(rc.process, 'exit');
  228. expect(code).to.equal(0);
  229. });
  230. it('does not crash on exit if added to a browser window', async () => {
  231. const rc = await startRemoteControlApp();
  232. await rc.remotely(() => {
  233. const { app, BrowserView, BrowserWindow } = require('electron');
  234. const bv = new BrowserView();
  235. bv.webContents.loadURL('about:blank');
  236. const bw = new BrowserWindow({ show: false });
  237. bw.addBrowserView(bv);
  238. setTimeout(() => {
  239. app.quit();
  240. });
  241. });
  242. const [code] = await emittedOnce(rc.process, 'exit');
  243. expect(code).to.equal(0);
  244. });
  245. });
  246. describe('window.open()', () => {
  247. it('works in BrowserView', (done) => {
  248. view = new BrowserView();
  249. w.setBrowserView(view);
  250. view.webContents.setWindowOpenHandler(({ url, frameName }) => {
  251. expect(url).to.equal('http://host/');
  252. expect(frameName).to.equal('host');
  253. done();
  254. return { action: 'deny' };
  255. });
  256. view.webContents.loadFile(path.join(fixtures, 'pages', 'window-open.html'));
  257. });
  258. });
  259. describe('BrowserView.capturePage(rect)', () => {
  260. it('returns a Promise with a Buffer', async () => {
  261. view = new BrowserView({
  262. webPreferences: {
  263. backgroundThrottling: false
  264. }
  265. });
  266. w.addBrowserView(view);
  267. view.setBounds({
  268. ...w.getBounds(),
  269. x: 0,
  270. y: 0
  271. });
  272. const image = await view.webContents.capturePage({
  273. x: 0,
  274. y: 0,
  275. width: 100,
  276. height: 100
  277. });
  278. expect(image.isEmpty()).to.equal(true);
  279. });
  280. xit('resolves after the window is hidden and capturer count is non-zero', async () => {
  281. view = new BrowserView({
  282. webPreferences: {
  283. backgroundThrottling: false
  284. }
  285. });
  286. w.setBrowserView(view);
  287. view.setBounds({
  288. ...w.getBounds(),
  289. x: 0,
  290. y: 0
  291. });
  292. await view.webContents.loadFile(path.join(fixtures, 'pages', 'a.html'));
  293. view.webContents.incrementCapturerCount();
  294. const image = await view.webContents.capturePage();
  295. expect(image.isEmpty()).to.equal(false);
  296. });
  297. it('should increase the capturer count', () => {
  298. view = new BrowserView({
  299. webPreferences: {
  300. backgroundThrottling: false
  301. }
  302. });
  303. w.setBrowserView(view);
  304. view.setBounds({
  305. ...w.getBounds(),
  306. x: 0,
  307. y: 0
  308. });
  309. view.webContents.incrementCapturerCount();
  310. expect(view.webContents.isBeingCaptured()).to.be.true();
  311. view.webContents.decrementCapturerCount();
  312. expect(view.webContents.isBeingCaptured()).to.be.false();
  313. });
  314. });
  315. });