api-browser-view-spec.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  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. const p = once(w.webContents, 'destroyed');
  25. await closeWindow(w);
  26. w = null as any;
  27. await p;
  28. if (view && view.webContents) {
  29. const p = once(view.webContents, 'destroyed');
  30. view.webContents.destroy();
  31. view = null as any;
  32. await p;
  33. }
  34. expect(webContents.getAllWebContents().length).to.equal(0, 'expected no webContents to exist');
  35. });
  36. it('sets the correct class name on the prototype', () => {
  37. expect(BrowserView.prototype.constructor.name).to.equal('BrowserView');
  38. });
  39. it('can be created with an existing webContents', async () => {
  40. const wc = (webContents as typeof ElectronInternal.WebContents).create({ sandbox: true });
  41. await wc.loadURL('about:blank');
  42. view = new BrowserView({ webContents: wc } as any);
  43. expect(view.webContents === wc).to.be.true('view.webContents === wc');
  44. expect(view.webContents.getURL()).to.equal('about:blank');
  45. });
  46. it('has type browserView', () => {
  47. view = new BrowserView();
  48. expect(view.webContents.getType()).to.equal('browserView');
  49. });
  50. describe('BrowserView.setBackgroundColor()', () => {
  51. it('does not throw for valid args', () => {
  52. view = new BrowserView();
  53. view.setBackgroundColor('#000');
  54. });
  55. // We now treat invalid args as "no background".
  56. it('does not throw for invalid args', () => {
  57. view = new BrowserView();
  58. expect(() => {
  59. view.setBackgroundColor({} as any);
  60. }).not.to.throw();
  61. });
  62. // Linux and arm64 platforms (WOA and macOS) do not return any capture sources
  63. ifit(process.platform === 'darwin' && process.arch === 'x64')('sets the background color to transparent if none is set', async () => {
  64. const display = screen.getPrimaryDisplay();
  65. const WINDOW_BACKGROUND_COLOR = '#55ccbb';
  66. w.show();
  67. w.setBounds(display.bounds);
  68. w.setBackgroundColor(WINDOW_BACKGROUND_COLOR);
  69. await w.loadURL('about:blank');
  70. view = new BrowserView();
  71. view.setBounds(display.bounds);
  72. w.setBrowserView(view);
  73. await view.webContents.loadURL('data:text/html,hello there');
  74. const screenCapture = await ScreenCapture.createForDisplay(display);
  75. await screenCapture.expectColorAtCenterMatches(WINDOW_BACKGROUND_COLOR);
  76. });
  77. // Linux and arm64 platforms (WOA and macOS) do not return any capture sources
  78. ifit(process.platform === 'darwin' && process.arch === 'x64')('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 = await ScreenCapture.createForDisplay(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. });
  320. describe('BrowserWindow.addBrowserView()', () => {
  321. it('does not throw for valid args', () => {
  322. const view1 = new BrowserView();
  323. defer(() => view1.webContents.destroy());
  324. w.addBrowserView(view1);
  325. defer(() => w.removeBrowserView(view1));
  326. const view2 = new BrowserView();
  327. defer(() => view2.webContents.destroy());
  328. w.addBrowserView(view2);
  329. defer(() => w.removeBrowserView(view2));
  330. });
  331. it('does not throw if called multiple times with same view', () => {
  332. view = new BrowserView();
  333. w.addBrowserView(view);
  334. w.addBrowserView(view);
  335. w.addBrowserView(view);
  336. });
  337. it('does not crash if the BrowserView webContents are destroyed prior to window addition', () => {
  338. expect(() => {
  339. const view1 = new BrowserView();
  340. view1.webContents.destroy();
  341. w.addBrowserView(view1);
  342. }).to.not.throw();
  343. });
  344. it('does not crash if the webContents is destroyed after a URL is loaded', () => {
  345. view = new BrowserView();
  346. expect(async () => {
  347. view.setBounds({ x: 0, y: 0, width: 400, height: 300 });
  348. await view.webContents.loadURL('data:text/html,hello there');
  349. view.webContents.destroy();
  350. }).to.not.throw();
  351. });
  352. it('can handle BrowserView reparenting', async () => {
  353. view = new BrowserView();
  354. w.addBrowserView(view);
  355. view.webContents.loadURL('about:blank');
  356. await once(view.webContents, 'did-finish-load');
  357. const w2 = new BrowserWindow({ show: false });
  358. w2.addBrowserView(view);
  359. w.close();
  360. view.webContents.loadURL(`file://${fixtures}/pages/blank.html`);
  361. await once(view.webContents, 'did-finish-load');
  362. // Clean up - the afterEach hook assumes the webContents on w is still alive.
  363. w = new BrowserWindow({ show: false });
  364. w2.close();
  365. w2.destroy();
  366. });
  367. it('does not cause a crash when used for view with destroyed web contents', async () => {
  368. const w2 = new BrowserWindow({ show: false });
  369. const view = new BrowserView();
  370. view.webContents.close();
  371. w2.addBrowserView(view);
  372. w2.webContents.loadURL('about:blank');
  373. await once(w2.webContents, 'did-finish-load');
  374. w2.close();
  375. });
  376. });
  377. describe('BrowserWindow.removeBrowserView()', () => {
  378. it('does not throw if called multiple times with same view', () => {
  379. expect(() => {
  380. view = new BrowserView();
  381. w.addBrowserView(view);
  382. w.removeBrowserView(view);
  383. w.removeBrowserView(view);
  384. }).to.not.throw();
  385. });
  386. it('can be called on a BrowserView with a destroyed webContents', async () => {
  387. view = new BrowserView();
  388. w.addBrowserView(view);
  389. await view.webContents.loadURL('data:text/html,hello there');
  390. const destroyed = once(view.webContents, 'destroyed');
  391. view.webContents.close();
  392. await destroyed;
  393. w.removeBrowserView(view);
  394. });
  395. });
  396. describe('BrowserWindow.getBrowserViews()', () => {
  397. it('returns same views as was added', () => {
  398. const view1 = new BrowserView();
  399. defer(() => view1.webContents.destroy());
  400. w.addBrowserView(view1);
  401. defer(() => w.removeBrowserView(view1));
  402. const view2 = new BrowserView();
  403. defer(() => view2.webContents.destroy());
  404. w.addBrowserView(view2);
  405. defer(() => w.removeBrowserView(view2));
  406. const views = w.getBrowserViews();
  407. expect(views).to.have.lengthOf(2);
  408. expect(views[0].webContents.id).to.equal(view1.webContents.id);
  409. expect(views[1].webContents.id).to.equal(view2.webContents.id);
  410. });
  411. it('persists ordering by z-index', () => {
  412. const view1 = new BrowserView();
  413. defer(() => view1.webContents.destroy());
  414. w.addBrowserView(view1);
  415. defer(() => w.removeBrowserView(view1));
  416. const view2 = new BrowserView();
  417. defer(() => view2.webContents.destroy());
  418. w.addBrowserView(view2);
  419. defer(() => w.removeBrowserView(view2));
  420. w.setTopBrowserView(view1);
  421. const views = w.getBrowserViews();
  422. expect(views).to.have.lengthOf(2);
  423. expect(views[0].webContents.id).to.equal(view2.webContents.id);
  424. expect(views[1].webContents.id).to.equal(view1.webContents.id);
  425. });
  426. });
  427. describe('BrowserWindow.setTopBrowserView()', () => {
  428. it('should throw an error when a BrowserView is not attached to the window', () => {
  429. view = new BrowserView();
  430. expect(() => {
  431. w.setTopBrowserView(view);
  432. }).to.throw(/is not attached/);
  433. });
  434. it('should throw an error when a BrowserView is attached to some other window', () => {
  435. view = new BrowserView();
  436. const win2 = new BrowserWindow();
  437. w.addBrowserView(view);
  438. view.setBounds({ x: 0, y: 0, width: 100, height: 100 });
  439. win2.addBrowserView(view);
  440. expect(() => {
  441. w.setTopBrowserView(view);
  442. }).to.throw(/is not attached/);
  443. win2.close();
  444. win2.destroy();
  445. });
  446. });
  447. describe('BrowserView.webContents.getOwnerBrowserWindow()', () => {
  448. it('points to owning window', () => {
  449. view = new BrowserView();
  450. expect(view.webContents.getOwnerBrowserWindow()).to.be.null('owner browser window');
  451. w.setBrowserView(view);
  452. expect(view.webContents.getOwnerBrowserWindow()).to.equal(w);
  453. w.setBrowserView(null);
  454. expect(view.webContents.getOwnerBrowserWindow()).to.be.null('owner browser window');
  455. });
  456. });
  457. describe('shutdown behavior', () => {
  458. it('does not crash on exit', async () => {
  459. const rc = await startRemoteControlApp();
  460. await rc.remotely(() => {
  461. const { BrowserView, app } = require('electron');
  462. // eslint-disable-next-line no-new
  463. new BrowserView({});
  464. setTimeout(() => {
  465. app.quit();
  466. });
  467. });
  468. const [code] = await once(rc.process, 'exit');
  469. expect(code).to.equal(0);
  470. });
  471. it('does not crash on exit if added to a browser window', async () => {
  472. const rc = await startRemoteControlApp();
  473. await rc.remotely(() => {
  474. const { app, BrowserView, BrowserWindow } = require('electron');
  475. const bv = new BrowserView();
  476. bv.webContents.loadURL('about:blank');
  477. const bw = new BrowserWindow({ show: false });
  478. bw.addBrowserView(bv);
  479. setTimeout(() => {
  480. app.quit();
  481. });
  482. });
  483. const [code] = await once(rc.process, 'exit');
  484. expect(code).to.equal(0);
  485. });
  486. it('emits the destroyed event when webContents.close() is called', async () => {
  487. view = new BrowserView();
  488. w.setBrowserView(view);
  489. await view.webContents.loadFile(path.join(fixtures, 'pages', 'a.html'));
  490. view.webContents.close();
  491. await once(view.webContents, 'destroyed');
  492. });
  493. it('emits the destroyed event when window.close() is called', async () => {
  494. view = new BrowserView();
  495. w.setBrowserView(view);
  496. await view.webContents.loadFile(path.join(fixtures, 'pages', 'a.html'));
  497. view.webContents.executeJavaScript('window.close()');
  498. await once(view.webContents, 'destroyed');
  499. });
  500. });
  501. describe('window.open()', () => {
  502. it('works in BrowserView', (done) => {
  503. view = new BrowserView();
  504. w.setBrowserView(view);
  505. view.webContents.setWindowOpenHandler(({ url, frameName }) => {
  506. expect(url).to.equal('http://host/');
  507. expect(frameName).to.equal('host');
  508. done();
  509. return { action: 'deny' };
  510. });
  511. view.webContents.loadFile(path.join(fixtures, 'pages', 'window-open.html'));
  512. });
  513. });
  514. describe('BrowserView.capturePage(rect)', () => {
  515. it('returns a Promise with a Buffer', async () => {
  516. view = new BrowserView({
  517. webPreferences: {
  518. backgroundThrottling: false
  519. }
  520. });
  521. w.addBrowserView(view);
  522. view.setBounds({
  523. ...w.getBounds(),
  524. x: 0,
  525. y: 0
  526. });
  527. const image = await view.webContents.capturePage({
  528. x: 0,
  529. y: 0,
  530. width: 100,
  531. height: 100
  532. });
  533. expect(image.isEmpty()).to.equal(true);
  534. });
  535. xit('resolves after the window is hidden and capturer count is non-zero', async () => {
  536. view = new BrowserView({
  537. webPreferences: {
  538. backgroundThrottling: false
  539. }
  540. });
  541. w.setBrowserView(view);
  542. view.setBounds({
  543. ...w.getBounds(),
  544. x: 0,
  545. y: 0
  546. });
  547. await view.webContents.loadFile(path.join(fixtures, 'pages', 'a.html'));
  548. const image = await view.webContents.capturePage();
  549. expect(image.isEmpty()).to.equal(false);
  550. });
  551. });
  552. });