api-browser-view-spec.ts 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. import { BrowserView, BrowserWindow, screen, session, webContents } from 'electron/main';
  2. import { expect } from 'chai';
  3. import { once } from 'node:events';
  4. import * as path from 'node:path';
  5. import { ScreenCapture, hasCapturableScreen } from './lib/screen-helpers';
  6. import { defer, ifit, startRemoteControlApp } from './lib/spec-helpers';
  7. import { closeWindow } from './lib/window-helpers';
  8. describe('BrowserView module', () => {
  9. const fixtures = path.resolve(__dirname, 'fixtures');
  10. const ses = session.fromPartition(crypto.randomUUID());
  11. let w: BrowserWindow;
  12. let view: BrowserView;
  13. const getSessionWebContents = () =>
  14. webContents.getAllWebContents().filter(wc => wc.session === ses);
  15. beforeEach(() => {
  16. expect(getSessionWebContents().length).to.equal(0, 'expected no webContents to exist');
  17. w = new BrowserWindow({
  18. show: false,
  19. width: 400,
  20. height: 400,
  21. webPreferences: {
  22. backgroundThrottling: false,
  23. session: ses
  24. }
  25. });
  26. });
  27. afterEach(async () => {
  28. if (w && !w.isDestroyed()) {
  29. const p = once(w.webContents, 'destroyed');
  30. await closeWindow(w);
  31. w = null as any;
  32. await p;
  33. }
  34. if (view && view.webContents) {
  35. const p = once(view.webContents, 'destroyed');
  36. view.webContents.destroy();
  37. view = null as any;
  38. await p;
  39. }
  40. expect(getSessionWebContents().length).to.equal(0, 'expected no webContents to exist');
  41. });
  42. it('sets the correct class name on the prototype', () => {
  43. expect(BrowserView.prototype.constructor.name).to.equal('BrowserView');
  44. });
  45. it('can be created with an existing webContents', async () => {
  46. const wc = (webContents as typeof ElectronInternal.WebContents).create({ session: ses, sandbox: true });
  47. await wc.loadURL('about:blank');
  48. view = new BrowserView({ webContents: wc } as any);
  49. expect(view.webContents === wc).to.be.true('view.webContents === wc');
  50. expect(view.webContents.getURL()).to.equal('about:blank');
  51. });
  52. it('has type browserView', () => {
  53. view = new BrowserView();
  54. expect(view.webContents.getType()).to.equal('browserView');
  55. });
  56. describe('BrowserView.setBackgroundColor()', () => {
  57. it('does not throw for valid args', () => {
  58. view = new BrowserView();
  59. view.setBackgroundColor('#000');
  60. });
  61. // We now treat invalid args as "no background".
  62. it('does not throw for invalid args', () => {
  63. view = new BrowserView();
  64. expect(() => {
  65. view.setBackgroundColor({} as any);
  66. }).not.to.throw();
  67. });
  68. ifit(hasCapturableScreen())('sets the background color to transparent if none is set', async () => {
  69. const display = screen.getPrimaryDisplay();
  70. const WINDOW_BACKGROUND_COLOR = '#55ccbb';
  71. w.show();
  72. w.setBounds(display.bounds);
  73. w.setBackgroundColor(WINDOW_BACKGROUND_COLOR);
  74. await w.loadURL('about:blank');
  75. view = new BrowserView();
  76. view.setBounds(display.bounds);
  77. w.setBrowserView(view);
  78. await view.webContents.loadURL('data:text/html,hello there');
  79. const screenCapture = new ScreenCapture(display);
  80. await screenCapture.expectColorAtCenterMatches(WINDOW_BACKGROUND_COLOR);
  81. });
  82. ifit(hasCapturableScreen())('successfully applies the background color', async () => {
  83. const WINDOW_BACKGROUND_COLOR = '#55ccbb';
  84. const VIEW_BACKGROUND_COLOR = '#ff00ff';
  85. const display = screen.getPrimaryDisplay();
  86. w.show();
  87. w.setBounds(display.bounds);
  88. w.setBackgroundColor(WINDOW_BACKGROUND_COLOR);
  89. await w.loadURL('about:blank');
  90. view = new BrowserView();
  91. view.setBounds(display.bounds);
  92. w.setBrowserView(view);
  93. w.setBackgroundColor(VIEW_BACKGROUND_COLOR);
  94. await view.webContents.loadURL('data:text/html,hello there');
  95. const screenCapture = new ScreenCapture(display);
  96. await screenCapture.expectColorAtCenterMatches(VIEW_BACKGROUND_COLOR);
  97. });
  98. });
  99. describe('BrowserView.setAutoResize()', () => {
  100. it('does not throw for valid args', () => {
  101. view = new BrowserView();
  102. view.setAutoResize({});
  103. view.setAutoResize({ width: true, height: false });
  104. });
  105. it('throws for invalid args', () => {
  106. view = new BrowserView();
  107. expect(() => {
  108. view.setAutoResize(null as any);
  109. }).to.throw(/Invalid auto resize options/);
  110. });
  111. it('does not resize when the BrowserView has no AutoResize', () => {
  112. view = new BrowserView();
  113. w.addBrowserView(view);
  114. view.setBounds({ x: 0, y: 0, width: 400, height: 200 });
  115. expect(view.getBounds()).to.deep.equal({
  116. x: 0,
  117. y: 0,
  118. width: 400,
  119. height: 200
  120. });
  121. w.setSize(800, 400);
  122. expect(view.getBounds()).to.deep.equal({
  123. x: 0,
  124. y: 0,
  125. width: 400,
  126. height: 200
  127. });
  128. });
  129. it('resizes horizontally when the window is resized horizontally', () => {
  130. view = new BrowserView();
  131. view.setAutoResize({ width: true, height: false });
  132. w.addBrowserView(view);
  133. view.setBounds({ x: 0, y: 0, width: 400, height: 200 });
  134. expect(view.getBounds()).to.deep.equal({
  135. x: 0,
  136. y: 0,
  137. width: 400,
  138. height: 200
  139. });
  140. w.setSize(800, 400);
  141. expect(view.getBounds()).to.deep.equal({
  142. x: 0,
  143. y: 0,
  144. width: 800,
  145. height: 200
  146. });
  147. });
  148. it('resizes vertically when the window is resized vertically', () => {
  149. view = new BrowserView();
  150. view.setAutoResize({ width: false, height: true });
  151. w.addBrowserView(view);
  152. view.setBounds({ x: 0, y: 0, width: 200, height: 400 });
  153. expect(view.getBounds()).to.deep.equal({
  154. x: 0,
  155. y: 0,
  156. width: 200,
  157. height: 400
  158. });
  159. w.setSize(400, 800);
  160. expect(view.getBounds()).to.deep.equal({
  161. x: 0,
  162. y: 0,
  163. width: 200,
  164. height: 800
  165. });
  166. });
  167. it('resizes both vertically and horizontally when the window is resized', () => {
  168. view = new BrowserView();
  169. view.setAutoResize({ width: true, height: true });
  170. w.addBrowserView(view);
  171. view.setBounds({ x: 0, y: 0, width: 400, height: 400 });
  172. expect(view.getBounds()).to.deep.equal({
  173. x: 0,
  174. y: 0,
  175. width: 400,
  176. height: 400
  177. });
  178. w.setSize(800, 800);
  179. expect(view.getBounds()).to.deep.equal({
  180. x: 0,
  181. y: 0,
  182. width: 800,
  183. height: 800
  184. });
  185. });
  186. it('resizes proportionally', () => {
  187. view = new BrowserView();
  188. view.setAutoResize({ width: true, height: false });
  189. w.addBrowserView(view);
  190. view.setBounds({ x: 0, y: 0, width: 200, height: 100 });
  191. expect(view.getBounds()).to.deep.equal({
  192. x: 0,
  193. y: 0,
  194. width: 200,
  195. height: 100
  196. });
  197. w.setSize(800, 400);
  198. expect(view.getBounds()).to.deep.equal({
  199. x: 0,
  200. y: 0,
  201. width: 600,
  202. height: 100
  203. });
  204. });
  205. it('does not move x if horizontal: false', () => {
  206. view = new BrowserView();
  207. view.setAutoResize({ width: true });
  208. w.addBrowserView(view);
  209. view.setBounds({ x: 200, y: 0, width: 200, height: 100 });
  210. w.setSize(800, 400);
  211. expect(view.getBounds()).to.deep.equal({
  212. x: 200,
  213. y: 0,
  214. width: 600,
  215. height: 100
  216. });
  217. });
  218. it('moves x if horizontal: true', () => {
  219. view = new BrowserView();
  220. view.setAutoResize({ horizontal: true });
  221. w.addBrowserView(view);
  222. view.setBounds({ x: 200, y: 0, width: 200, height: 100 });
  223. w.setSize(800, 400);
  224. expect(view.getBounds()).to.deep.equal({
  225. x: 400,
  226. y: 0,
  227. width: 400,
  228. height: 100
  229. });
  230. });
  231. it('moves x if horizontal: true width: true', () => {
  232. view = new BrowserView();
  233. view.setAutoResize({ horizontal: true, width: true });
  234. w.addBrowserView(view);
  235. view.setBounds({ x: 200, y: 0, width: 200, height: 100 });
  236. w.setSize(800, 400);
  237. expect(view.getBounds()).to.deep.equal({
  238. x: 400,
  239. y: 0,
  240. width: 400,
  241. height: 100
  242. });
  243. });
  244. });
  245. describe('BrowserView.setBounds()', () => {
  246. it('does not throw for valid args', () => {
  247. view = new BrowserView();
  248. view.setBounds({ x: 0, y: 0, width: 1, height: 1 });
  249. });
  250. it('throws for invalid args', () => {
  251. view = new BrowserView();
  252. expect(() => {
  253. view.setBounds(null as any);
  254. }).to.throw(/conversion failure/);
  255. expect(() => {
  256. view.setBounds({} as any);
  257. }).to.throw(/conversion failure/);
  258. });
  259. it('can set bounds after view is added to window', () => {
  260. view = new BrowserView();
  261. const bounds = { x: 0, y: 0, width: 50, height: 50 };
  262. w.addBrowserView(view);
  263. view.setBounds(bounds);
  264. expect(view.getBounds()).to.deep.equal(bounds);
  265. });
  266. it('can set bounds before view is added to window', () => {
  267. view = new BrowserView();
  268. const bounds = { x: 0, y: 0, width: 50, height: 50 };
  269. view.setBounds(bounds);
  270. w.addBrowserView(view);
  271. expect(view.getBounds()).to.deep.equal(bounds);
  272. });
  273. it('can update bounds', () => {
  274. view = new BrowserView();
  275. w.addBrowserView(view);
  276. const bounds1 = { x: 0, y: 0, width: 50, height: 50 };
  277. view.setBounds(bounds1);
  278. expect(view.getBounds()).to.deep.equal(bounds1);
  279. const bounds2 = { x: 0, y: 150, width: 50, height: 50 };
  280. view.setBounds(bounds2);
  281. expect(view.getBounds()).to.deep.equal(bounds2);
  282. });
  283. });
  284. describe('BrowserView.getBounds()', () => {
  285. it('returns the current bounds', () => {
  286. view = new BrowserView();
  287. const bounds = { x: 10, y: 20, width: 30, height: 40 };
  288. view.setBounds(bounds);
  289. expect(view.getBounds()).to.deep.equal(bounds);
  290. });
  291. it('does not changer after being added to a window', () => {
  292. view = new BrowserView();
  293. const bounds = { x: 10, y: 20, width: 30, height: 40 };
  294. view.setBounds(bounds);
  295. expect(view.getBounds()).to.deep.equal(bounds);
  296. w.addBrowserView(view);
  297. expect(view.getBounds()).to.deep.equal(bounds);
  298. });
  299. });
  300. describe('BrowserWindow.setBrowserView()', () => {
  301. it('does not throw for valid args', () => {
  302. view = new BrowserView();
  303. w.setBrowserView(view);
  304. });
  305. it('does not throw if called multiple times with same view', () => {
  306. view = new BrowserView();
  307. w.setBrowserView(view);
  308. w.setBrowserView(view);
  309. w.setBrowserView(view);
  310. });
  311. });
  312. describe('BrowserWindow.getBrowserView()', () => {
  313. it('returns the set view', () => {
  314. view = new BrowserView();
  315. w.setBrowserView(view);
  316. const view2 = w.getBrowserView();
  317. expect(view2!.webContents.id).to.equal(view.webContents.id);
  318. });
  319. it('returns null if none is set', () => {
  320. const view = w.getBrowserView();
  321. expect(view).to.be.null('view');
  322. });
  323. it('throws if multiple BrowserViews are attached', () => {
  324. view = new BrowserView();
  325. w.setBrowserView(view);
  326. const view2 = new BrowserView();
  327. defer(() => view2.webContents.destroy());
  328. w.addBrowserView(view2);
  329. defer(() => w.removeBrowserView(view2));
  330. expect(() => {
  331. w.getBrowserView();
  332. }).to.throw(/has multiple BrowserViews/);
  333. });
  334. });
  335. describe('BrowserWindow.addBrowserView()', () => {
  336. it('does not throw for valid args', () => {
  337. const view1 = new BrowserView();
  338. defer(() => view1.webContents.destroy());
  339. w.addBrowserView(view1);
  340. defer(() => w.removeBrowserView(view1));
  341. const view2 = new BrowserView();
  342. defer(() => view2.webContents.destroy());
  343. w.addBrowserView(view2);
  344. defer(() => w.removeBrowserView(view2));
  345. });
  346. it('does not throw if called multiple times with same view', () => {
  347. view = new BrowserView();
  348. w.addBrowserView(view);
  349. w.addBrowserView(view);
  350. w.addBrowserView(view);
  351. });
  352. it('does not crash if the webContents is destroyed after a URL is loaded', () => {
  353. view = new BrowserView();
  354. expect(async () => {
  355. view.setBounds({ x: 0, y: 0, width: 400, height: 300 });
  356. await view.webContents.loadURL('data:text/html,hello there');
  357. view.webContents.destroy();
  358. }).to.not.throw();
  359. });
  360. it('can handle BrowserView reparenting', async () => {
  361. view = new BrowserView();
  362. expect(view.ownerWindow).to.be.null('ownerWindow');
  363. w.addBrowserView(view);
  364. view.webContents.loadURL('about:blank');
  365. await once(view.webContents, 'did-finish-load');
  366. expect(view.ownerWindow).to.equal(w);
  367. const w2 = new BrowserWindow({ show: false });
  368. w2.addBrowserView(view);
  369. expect(view.ownerWindow).to.equal(w2);
  370. w.close();
  371. view.webContents.loadURL(`file://${fixtures}/pages/blank.html`);
  372. await once(view.webContents, 'did-finish-load');
  373. // Clean up - the afterEach hook assumes the webContents on w is still alive.
  374. w = new BrowserWindow({ show: false });
  375. w2.close();
  376. w2.destroy();
  377. });
  378. it('allows attaching a BrowserView with a previously-closed webContents', async () => {
  379. const w2 = new BrowserWindow({ show: false });
  380. const view = new BrowserView();
  381. expect(view.ownerWindow).to.be.null('ownerWindow');
  382. view.webContents.close();
  383. w2.addBrowserView(view);
  384. expect(view.ownerWindow).to.equal(w2);
  385. w2.webContents.loadURL('about:blank');
  386. await once(w2.webContents, 'did-finish-load');
  387. w2.close();
  388. });
  389. it('allows attaching a BrowserView with a previously-destroyed webContents', async () => {
  390. const view = new BrowserView();
  391. expect(view.ownerWindow).to.be.null('ownerWindow');
  392. view.webContents.destroy();
  393. w.addBrowserView(view);
  394. expect(view.ownerWindow).to.equal(w);
  395. w.webContents.loadURL('about:blank');
  396. await once(w.webContents, 'did-finish-load');
  397. });
  398. });
  399. describe('BrowserWindow.removeBrowserView()', () => {
  400. it('does not throw if called multiple times with same view', () => {
  401. expect(() => {
  402. view = new BrowserView();
  403. w.addBrowserView(view);
  404. w.removeBrowserView(view);
  405. w.removeBrowserView(view);
  406. }).to.not.throw();
  407. });
  408. it('can be called on a BrowserView with a destroyed webContents', async () => {
  409. view = new BrowserView();
  410. w.addBrowserView(view);
  411. await view.webContents.loadURL('data:text/html,hello there');
  412. const destroyed = once(view.webContents, 'destroyed');
  413. view.webContents.close();
  414. await destroyed;
  415. w.removeBrowserView(view);
  416. });
  417. });
  418. describe('BrowserWindow.getBrowserViews()', () => {
  419. it('returns same views as was added', () => {
  420. const view1 = new BrowserView();
  421. defer(() => view1.webContents.destroy());
  422. w.addBrowserView(view1);
  423. defer(() => w.removeBrowserView(view1));
  424. const view2 = new BrowserView();
  425. defer(() => view2.webContents.destroy());
  426. w.addBrowserView(view2);
  427. defer(() => w.removeBrowserView(view2));
  428. const views = w.getBrowserViews();
  429. expect(views).to.have.lengthOf(2);
  430. expect(views[0].webContents.id).to.equal(view1.webContents.id);
  431. expect(views[1].webContents.id).to.equal(view2.webContents.id);
  432. });
  433. it('persists ordering by z-index', () => {
  434. const view1 = new BrowserView();
  435. defer(() => view1.webContents.destroy());
  436. w.addBrowserView(view1);
  437. defer(() => w.removeBrowserView(view1));
  438. const view2 = new BrowserView();
  439. defer(() => view2.webContents.destroy());
  440. w.addBrowserView(view2);
  441. defer(() => w.removeBrowserView(view2));
  442. w.setTopBrowserView(view1);
  443. const views = w.getBrowserViews();
  444. expect(views).to.have.lengthOf(2);
  445. expect(views[0].webContents.id).to.equal(view2.webContents.id);
  446. expect(views[1].webContents.id).to.equal(view1.webContents.id);
  447. });
  448. });
  449. describe('BrowserWindow.setTopBrowserView()', () => {
  450. it('should throw an error when a BrowserView is not attached to the window', () => {
  451. view = new BrowserView();
  452. expect(() => {
  453. w.setTopBrowserView(view);
  454. }).to.throw(/is not attached/);
  455. });
  456. it('should throw an error when a BrowserView is attached to some other window', () => {
  457. view = new BrowserView();
  458. const win2 = new BrowserWindow();
  459. w.addBrowserView(view);
  460. view.setBounds({ x: 0, y: 0, width: 100, height: 100 });
  461. win2.addBrowserView(view);
  462. expect(() => {
  463. w.setTopBrowserView(view);
  464. }).to.throw(/is not attached/);
  465. win2.close();
  466. win2.destroy();
  467. });
  468. it('should reorder the BrowserView to the top if it is already in the window', () => {
  469. view = new BrowserView();
  470. const view2 = new BrowserView();
  471. defer(() => view2.webContents.destroy());
  472. w.addBrowserView(view);
  473. w.addBrowserView(view2);
  474. defer(() => w.removeBrowserView(view2));
  475. w.setTopBrowserView(view);
  476. const views = w.getBrowserViews();
  477. expect(views.indexOf(view)).to.equal(views.length - 1);
  478. });
  479. });
  480. describe('BrowserView owning window', () => {
  481. it('points to owning window', () => {
  482. view = new BrowserView();
  483. expect(view.webContents.getOwnerBrowserWindow()).to.be.null('owner browser window');
  484. expect(view.ownerWindow).to.be.null('ownerWindow');
  485. w.setBrowserView(view);
  486. expect(view.webContents.getOwnerBrowserWindow()).to.equal(w);
  487. expect(view.ownerWindow).to.equal(w);
  488. w.setBrowserView(null);
  489. expect(view.webContents.getOwnerBrowserWindow()).to.be.null('owner browser window');
  490. expect(view.ownerWindow).to.be.null('ownerWindow');
  491. });
  492. it('works correctly when the webContents is destroyed', async () => {
  493. view = new BrowserView();
  494. w.setBrowserView(view);
  495. expect(view.webContents.getOwnerBrowserWindow()).to.equal(w);
  496. expect(view.ownerWindow).to.equal(w);
  497. const destroyed = once(view.webContents, 'destroyed');
  498. view.webContents.close();
  499. await destroyed;
  500. expect(view.ownerWindow).to.equal(w);
  501. });
  502. it('works correctly when owner window is closed', async () => {
  503. view = new BrowserView();
  504. w.setBrowserView(view);
  505. expect(view.webContents.getOwnerBrowserWindow()).to.equal(w);
  506. expect(view.ownerWindow).to.equal(w);
  507. const destroyed = once(w, 'closed');
  508. w.close();
  509. await destroyed;
  510. expect(view.ownerWindow).to.equal(null);
  511. });
  512. });
  513. describe('shutdown behavior', () => {
  514. it('emits the destroyed event when the host BrowserWindow is closed', async () => {
  515. view = new BrowserView();
  516. w.addBrowserView(view);
  517. await view.webContents.loadURL(`data:text/html,
  518. <html>
  519. <body>
  520. <div id="bv_id">HELLO BROWSERVIEW</div>
  521. </body>
  522. </html>
  523. `);
  524. const query = 'document.getElementById("bv_id").textContent';
  525. const contentBefore = await view.webContents.executeJavaScript(query);
  526. expect(contentBefore).to.equal('HELLO BROWSERVIEW');
  527. w.close();
  528. const destroyed = once(view.webContents, 'destroyed');
  529. const closed = once(w, 'closed');
  530. await Promise.all([destroyed, closed]);
  531. });
  532. it('does not destroy its webContents if an owner BrowserWindow close event is prevented', async () => {
  533. view = new BrowserView();
  534. w.addBrowserView(view);
  535. await view.webContents.loadURL(`data:text/html,
  536. <html>
  537. <body>
  538. <div id="bv_id">HELLO BROWSERVIEW</div>
  539. </body>
  540. </html>
  541. `);
  542. const query = 'document.getElementById("bv_id").textContent';
  543. const contentBefore = await view.webContents.executeJavaScript(query);
  544. expect(contentBefore).to.equal('HELLO BROWSERVIEW');
  545. w.once('close', (e) => {
  546. e.preventDefault();
  547. });
  548. w.close();
  549. const contentAfter = await view.webContents.executeJavaScript(query);
  550. expect(contentAfter).to.equal('HELLO BROWSERVIEW');
  551. });
  552. it('does not crash on exit', async () => {
  553. const rc = await startRemoteControlApp();
  554. await rc.remotely(() => {
  555. const { BrowserView, app } = require('electron');
  556. // eslint-disable-next-line no-new
  557. new BrowserView({});
  558. setTimeout(() => {
  559. app.quit();
  560. });
  561. });
  562. const [code] = await once(rc.process, 'exit');
  563. expect(code).to.equal(0);
  564. });
  565. it('does not crash on exit if added to a browser window', async () => {
  566. const rc = await startRemoteControlApp();
  567. await rc.remotely(() => {
  568. const { app, BrowserView, BrowserWindow } = require('electron');
  569. const bv = new BrowserView();
  570. bv.webContents.loadURL('about:blank');
  571. const bw = new BrowserWindow({ show: false });
  572. bw.addBrowserView(bv);
  573. setTimeout(() => {
  574. app.quit();
  575. });
  576. });
  577. const [code] = await once(rc.process, 'exit');
  578. expect(code).to.equal(0);
  579. });
  580. it('emits the destroyed event when webContents.close() is called', async () => {
  581. view = new BrowserView();
  582. w.setBrowserView(view);
  583. await view.webContents.loadFile(path.join(fixtures, 'pages', 'a.html'));
  584. view.webContents.close();
  585. await once(view.webContents, 'destroyed');
  586. });
  587. it('emits the destroyed event when window.close() is called', async () => {
  588. view = new BrowserView();
  589. w.setBrowserView(view);
  590. await view.webContents.loadFile(path.join(fixtures, 'pages', 'a.html'));
  591. view.webContents.executeJavaScript('window.close()');
  592. await once(view.webContents, 'destroyed');
  593. });
  594. });
  595. describe('window.open()', () => {
  596. it('works in BrowserView', (done) => {
  597. view = new BrowserView();
  598. w.setBrowserView(view);
  599. view.webContents.setWindowOpenHandler(({ url, frameName }) => {
  600. expect(url).to.equal('http://host/');
  601. expect(frameName).to.equal('host');
  602. done();
  603. return { action: 'deny' };
  604. });
  605. view.webContents.loadFile(path.join(fixtures, 'pages', 'window-open.html'));
  606. });
  607. });
  608. describe('BrowserView.capturePage(rect)', () => {
  609. it('returns a Promise with a Buffer', async () => {
  610. view = new BrowserView({
  611. webPreferences: {
  612. backgroundThrottling: false
  613. }
  614. });
  615. w.addBrowserView(view);
  616. view.setBounds({
  617. ...w.getBounds(),
  618. x: 0,
  619. y: 0
  620. });
  621. const image = await view.webContents.capturePage({
  622. x: 0,
  623. y: 0,
  624. width: 100,
  625. height: 100
  626. });
  627. expect(image.isEmpty()).to.equal(true);
  628. });
  629. xit('resolves after the window is hidden and capturer count is non-zero', async () => {
  630. view = new BrowserView({
  631. webPreferences: {
  632. backgroundThrottling: false
  633. }
  634. });
  635. w.setBrowserView(view);
  636. view.setBounds({
  637. ...w.getBounds(),
  638. x: 0,
  639. y: 0
  640. });
  641. await view.webContents.loadFile(path.join(fixtures, 'pages', 'a.html'));
  642. const image = await view.webContents.capturePage();
  643. expect(image.isEmpty()).to.equal(false);
  644. });
  645. });
  646. });