api-browser-view-spec.ts 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  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. });
  322. describe('BrowserWindow.addBrowserView()', () => {
  323. it('does not throw for valid args', () => {
  324. const view1 = new BrowserView();
  325. defer(() => view1.webContents.destroy());
  326. w.addBrowserView(view1);
  327. defer(() => w.removeBrowserView(view1));
  328. const view2 = new BrowserView();
  329. defer(() => view2.webContents.destroy());
  330. w.addBrowserView(view2);
  331. defer(() => w.removeBrowserView(view2));
  332. });
  333. it('does not throw if called multiple times with same view', () => {
  334. view = new BrowserView();
  335. w.addBrowserView(view);
  336. w.addBrowserView(view);
  337. w.addBrowserView(view);
  338. });
  339. it('does not crash if the BrowserView webContents are destroyed prior to window addition', () => {
  340. expect(() => {
  341. const view1 = new BrowserView();
  342. view1.webContents.destroy();
  343. w.addBrowserView(view1);
  344. }).to.not.throw();
  345. });
  346. it('does not crash if the webContents is destroyed after a URL is loaded', () => {
  347. view = new BrowserView();
  348. expect(async () => {
  349. view.setBounds({ x: 0, y: 0, width: 400, height: 300 });
  350. await view.webContents.loadURL('data:text/html,hello there');
  351. view.webContents.destroy();
  352. }).to.not.throw();
  353. });
  354. it('can handle BrowserView reparenting', async () => {
  355. view = new BrowserView();
  356. w.addBrowserView(view);
  357. view.webContents.loadURL('about:blank');
  358. await once(view.webContents, 'did-finish-load');
  359. const w2 = new BrowserWindow({ show: false });
  360. w2.addBrowserView(view);
  361. w.close();
  362. view.webContents.loadURL(`file://${fixtures}/pages/blank.html`);
  363. await once(view.webContents, 'did-finish-load');
  364. // Clean up - the afterEach hook assumes the webContents on w is still alive.
  365. w = new BrowserWindow({ show: false });
  366. w2.close();
  367. w2.destroy();
  368. });
  369. it('does not cause a crash when used for view with destroyed web contents', async () => {
  370. const w2 = new BrowserWindow({ show: false });
  371. const view = new BrowserView();
  372. view.webContents.close();
  373. w2.addBrowserView(view);
  374. w2.webContents.loadURL('about:blank');
  375. await once(w2.webContents, 'did-finish-load');
  376. w2.close();
  377. });
  378. });
  379. describe('BrowserWindow.removeBrowserView()', () => {
  380. it('does not throw if called multiple times with same view', () => {
  381. expect(() => {
  382. view = new BrowserView();
  383. w.addBrowserView(view);
  384. w.removeBrowserView(view);
  385. w.removeBrowserView(view);
  386. }).to.not.throw();
  387. });
  388. it('can be called on a BrowserView with a destroyed webContents', async () => {
  389. view = new BrowserView();
  390. w.addBrowserView(view);
  391. await view.webContents.loadURL('data:text/html,hello there');
  392. const destroyed = once(view.webContents, 'destroyed');
  393. view.webContents.close();
  394. await destroyed;
  395. w.removeBrowserView(view);
  396. });
  397. });
  398. describe('BrowserWindow.getBrowserViews()', () => {
  399. it('returns same views as was added', () => {
  400. const view1 = new BrowserView();
  401. defer(() => view1.webContents.destroy());
  402. w.addBrowserView(view1);
  403. defer(() => w.removeBrowserView(view1));
  404. const view2 = new BrowserView();
  405. defer(() => view2.webContents.destroy());
  406. w.addBrowserView(view2);
  407. defer(() => w.removeBrowserView(view2));
  408. const views = w.getBrowserViews();
  409. expect(views).to.have.lengthOf(2);
  410. expect(views[0].webContents.id).to.equal(view1.webContents.id);
  411. expect(views[1].webContents.id).to.equal(view2.webContents.id);
  412. });
  413. it('persists ordering by z-index', () => {
  414. const view1 = new BrowserView();
  415. defer(() => view1.webContents.destroy());
  416. w.addBrowserView(view1);
  417. defer(() => w.removeBrowserView(view1));
  418. const view2 = new BrowserView();
  419. defer(() => view2.webContents.destroy());
  420. w.addBrowserView(view2);
  421. defer(() => w.removeBrowserView(view2));
  422. w.setTopBrowserView(view1);
  423. const views = w.getBrowserViews();
  424. expect(views).to.have.lengthOf(2);
  425. expect(views[0].webContents.id).to.equal(view2.webContents.id);
  426. expect(views[1].webContents.id).to.equal(view1.webContents.id);
  427. });
  428. });
  429. describe('BrowserWindow.setTopBrowserView()', () => {
  430. it('should throw an error when a BrowserView is not attached to the window', () => {
  431. view = new BrowserView();
  432. expect(() => {
  433. w.setTopBrowserView(view);
  434. }).to.throw(/is not attached/);
  435. });
  436. it('should throw an error when a BrowserView is attached to some other window', () => {
  437. view = new BrowserView();
  438. const win2 = new BrowserWindow();
  439. w.addBrowserView(view);
  440. view.setBounds({ x: 0, y: 0, width: 100, height: 100 });
  441. win2.addBrowserView(view);
  442. expect(() => {
  443. w.setTopBrowserView(view);
  444. }).to.throw(/is not attached/);
  445. win2.close();
  446. win2.destroy();
  447. });
  448. });
  449. describe('BrowserView.webContents.getOwnerBrowserWindow()', () => {
  450. it('points to owning window', () => {
  451. view = new BrowserView();
  452. expect(view.webContents.getOwnerBrowserWindow()).to.be.null('owner browser window');
  453. w.setBrowserView(view);
  454. expect(view.webContents.getOwnerBrowserWindow()).to.equal(w);
  455. w.setBrowserView(null);
  456. expect(view.webContents.getOwnerBrowserWindow()).to.be.null('owner browser window');
  457. });
  458. });
  459. describe('shutdown behavior', () => {
  460. it('emits the destroyed event when the host BrowserWindow is closed', async () => {
  461. view = new BrowserView();
  462. w.addBrowserView(view);
  463. await view.webContents.loadURL(`data:text/html,
  464. <html>
  465. <body>
  466. <div id="bv_id">HELLO BROWSERVIEW</div>
  467. </body>
  468. </html>
  469. `);
  470. const query = 'document.getElementById("bv_id").textContent';
  471. const contentBefore = await view.webContents.executeJavaScript(query);
  472. expect(contentBefore).to.equal('HELLO BROWSERVIEW');
  473. w.close();
  474. const destroyed = once(view.webContents, 'destroyed');
  475. const closed = once(w, 'closed');
  476. await Promise.all([destroyed, closed]);
  477. });
  478. it('does not destroy its webContents if an owner BrowserWindow close event is prevented', async () => {
  479. view = new BrowserView();
  480. w.addBrowserView(view);
  481. await view.webContents.loadURL(`data:text/html,
  482. <html>
  483. <body>
  484. <div id="bv_id">HELLO BROWSERVIEW</div>
  485. </body>
  486. </html>
  487. `);
  488. const query = 'document.getElementById("bv_id").textContent';
  489. const contentBefore = await view.webContents.executeJavaScript(query);
  490. expect(contentBefore).to.equal('HELLO BROWSERVIEW');
  491. w.once('close', (e) => {
  492. e.preventDefault();
  493. });
  494. w.close();
  495. const contentAfter = await view.webContents.executeJavaScript(query);
  496. expect(contentAfter).to.equal('HELLO BROWSERVIEW');
  497. });
  498. it('does not crash on exit', async () => {
  499. const rc = await startRemoteControlApp();
  500. await rc.remotely(() => {
  501. const { BrowserView, app } = require('electron');
  502. // eslint-disable-next-line no-new
  503. new BrowserView({});
  504. setTimeout(() => {
  505. app.quit();
  506. });
  507. });
  508. const [code] = await once(rc.process, 'exit');
  509. expect(code).to.equal(0);
  510. });
  511. it('does not crash on exit if added to a browser window', async () => {
  512. const rc = await startRemoteControlApp();
  513. await rc.remotely(() => {
  514. const { app, BrowserView, BrowserWindow } = require('electron');
  515. const bv = new BrowserView();
  516. bv.webContents.loadURL('about:blank');
  517. const bw = new BrowserWindow({ show: false });
  518. bw.addBrowserView(bv);
  519. setTimeout(() => {
  520. app.quit();
  521. });
  522. });
  523. const [code] = await once(rc.process, 'exit');
  524. expect(code).to.equal(0);
  525. });
  526. it('emits the destroyed event when webContents.close() is called', async () => {
  527. view = new BrowserView();
  528. w.setBrowserView(view);
  529. await view.webContents.loadFile(path.join(fixtures, 'pages', 'a.html'));
  530. view.webContents.close();
  531. await once(view.webContents, 'destroyed');
  532. });
  533. it('emits the destroyed event when window.close() is called', async () => {
  534. view = new BrowserView();
  535. w.setBrowserView(view);
  536. await view.webContents.loadFile(path.join(fixtures, 'pages', 'a.html'));
  537. view.webContents.executeJavaScript('window.close()');
  538. await once(view.webContents, 'destroyed');
  539. });
  540. });
  541. describe('window.open()', () => {
  542. it('works in BrowserView', (done) => {
  543. view = new BrowserView();
  544. w.setBrowserView(view);
  545. view.webContents.setWindowOpenHandler(({ url, frameName }) => {
  546. expect(url).to.equal('http://host/');
  547. expect(frameName).to.equal('host');
  548. done();
  549. return { action: 'deny' };
  550. });
  551. view.webContents.loadFile(path.join(fixtures, 'pages', 'window-open.html'));
  552. });
  553. });
  554. describe('BrowserView.capturePage(rect)', () => {
  555. it('returns a Promise with a Buffer', async () => {
  556. view = new BrowserView({
  557. webPreferences: {
  558. backgroundThrottling: false
  559. }
  560. });
  561. w.addBrowserView(view);
  562. view.setBounds({
  563. ...w.getBounds(),
  564. x: 0,
  565. y: 0
  566. });
  567. const image = await view.webContents.capturePage({
  568. x: 0,
  569. y: 0,
  570. width: 100,
  571. height: 100
  572. });
  573. expect(image.isEmpty()).to.equal(true);
  574. });
  575. xit('resolves after the window is hidden and capturer count is non-zero', async () => {
  576. view = new BrowserView({
  577. webPreferences: {
  578. backgroundThrottling: false
  579. }
  580. });
  581. w.setBrowserView(view);
  582. view.setBounds({
  583. ...w.getBounds(),
  584. x: 0,
  585. y: 0
  586. });
  587. await view.webContents.loadFile(path.join(fixtures, 'pages', 'a.html'));
  588. const image = await view.webContents.capturePage();
  589. expect(image.isEmpty()).to.equal(false);
  590. });
  591. });
  592. });