api-browser-view-spec.ts 23 KB

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