api-browser-view-spec.ts 23 KB

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