api-browser-view-spec.ts 20 KB

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