api-browser-view-spec.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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()).to.have.length(0);
  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()).to.have.length(0);
  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.getURL()).to.equal('about:blank');
  44. });
  45. describe('BrowserView.setBackgroundColor()', () => {
  46. it('does not throw for valid args', () => {
  47. view = new BrowserView();
  48. view.setBackgroundColor('#000');
  49. });
  50. it('throws for invalid args', () => {
  51. view = new BrowserView();
  52. expect(() => {
  53. view.setBackgroundColor(null as any);
  54. }).to.throw(/conversion failure/);
  55. });
  56. // Linux and arm64 platforms (WOA and macOS) do not return any capture sources
  57. ifit(process.platform === 'darwin' && process.arch === 'x64')('sets the background color to transparent if none is set', async () => {
  58. const display = screen.getPrimaryDisplay();
  59. const WINDOW_BACKGROUND_COLOR = '#55ccbb';
  60. w.show();
  61. w.setBounds(display.bounds);
  62. w.setBackgroundColor(WINDOW_BACKGROUND_COLOR);
  63. await w.loadURL('about:blank');
  64. view = new BrowserView();
  65. view.setBounds(display.bounds);
  66. w.setBrowserView(view);
  67. await view.webContents.loadURL('data:text/html,hello there');
  68. const screenCapture = await captureScreen();
  69. const centerColor = getPixelColor(screenCapture, {
  70. x: display.size.width / 2,
  71. y: display.size.height / 2
  72. });
  73. expect(areColorsSimilar(centerColor, WINDOW_BACKGROUND_COLOR)).to.be.true();
  74. });
  75. // Linux and arm64 platforms (WOA and macOS) do not return any capture sources
  76. ifit(process.platform === 'darwin' && process.arch === 'x64')('successfully applies the background color', async () => {
  77. const WINDOW_BACKGROUND_COLOR = '#55ccbb';
  78. const VIEW_BACKGROUND_COLOR = '#ff00ff';
  79. const display = screen.getPrimaryDisplay();
  80. w.show();
  81. w.setBounds(display.bounds);
  82. w.setBackgroundColor(WINDOW_BACKGROUND_COLOR);
  83. await w.loadURL('about:blank');
  84. view = new BrowserView();
  85. view.setBounds(display.bounds);
  86. w.setBrowserView(view);
  87. w.setBackgroundColor(VIEW_BACKGROUND_COLOR);
  88. await view.webContents.loadURL('data:text/html,hello there');
  89. const screenCapture = await captureScreen();
  90. const centerColor = getPixelColor(screenCapture, {
  91. x: display.size.width / 2,
  92. y: display.size.height / 2
  93. });
  94. expect(areColorsSimilar(centerColor, VIEW_BACKGROUND_COLOR)).to.be.true();
  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(/conversion failure/);
  108. });
  109. });
  110. describe('BrowserView.setBounds()', () => {
  111. it('does not throw for valid args', () => {
  112. view = new BrowserView();
  113. view.setBounds({ x: 0, y: 0, width: 1, height: 1 });
  114. });
  115. it('throws for invalid args', () => {
  116. view = new BrowserView();
  117. expect(() => {
  118. view.setBounds(null as any);
  119. }).to.throw(/conversion failure/);
  120. expect(() => {
  121. view.setBounds({} as any);
  122. }).to.throw(/conversion failure/);
  123. });
  124. it('can set bounds after view is added to window', () => {
  125. view = new BrowserView();
  126. const bounds = { x: 0, y: 0, width: 50, height: 50 };
  127. w.addBrowserView(view);
  128. view.setBounds(bounds);
  129. expect(view.getBounds()).to.deep.equal(bounds);
  130. });
  131. it('can set bounds before view is added to window', () => {
  132. view = new BrowserView();
  133. const bounds = { x: 0, y: 0, width: 50, height: 50 };
  134. view.setBounds(bounds);
  135. w.addBrowserView(view);
  136. expect(view.getBounds()).to.deep.equal(bounds);
  137. });
  138. it('can update bounds', () => {
  139. view = new BrowserView();
  140. w.addBrowserView(view);
  141. const bounds1 = { x: 0, y: 0, width: 50, height: 50 };
  142. view.setBounds(bounds1);
  143. expect(view.getBounds()).to.deep.equal(bounds1);
  144. const bounds2 = { x: 0, y: 150, width: 50, height: 50 };
  145. view.setBounds(bounds2);
  146. expect(view.getBounds()).to.deep.equal(bounds2);
  147. });
  148. });
  149. describe('BrowserView.getBounds()', () => {
  150. it('returns the current bounds', () => {
  151. view = new BrowserView();
  152. const bounds = { x: 10, y: 20, width: 30, height: 40 };
  153. view.setBounds(bounds);
  154. expect(view.getBounds()).to.deep.equal(bounds);
  155. });
  156. it('does not changer after being added to a window', () => {
  157. view = new BrowserView();
  158. const bounds = { x: 10, y: 20, width: 30, height: 40 };
  159. view.setBounds(bounds);
  160. expect(view.getBounds()).to.deep.equal(bounds);
  161. w.addBrowserView(view);
  162. expect(view.getBounds()).to.deep.equal(bounds);
  163. });
  164. });
  165. describe('BrowserWindow.setBrowserView()', () => {
  166. it('does not throw for valid args', () => {
  167. view = new BrowserView();
  168. w.setBrowserView(view);
  169. });
  170. it('does not throw if called multiple times with same view', () => {
  171. view = new BrowserView();
  172. w.setBrowserView(view);
  173. w.setBrowserView(view);
  174. w.setBrowserView(view);
  175. });
  176. });
  177. describe('BrowserWindow.getBrowserView()', () => {
  178. it('returns the set view', () => {
  179. view = new BrowserView();
  180. w.setBrowserView(view);
  181. const view2 = w.getBrowserView();
  182. expect(view2!.webContents.id).to.equal(view.webContents.id);
  183. });
  184. it('returns null if none is set', () => {
  185. const view = w.getBrowserView();
  186. expect(view).to.be.null('view');
  187. });
  188. });
  189. describe('BrowserWindow.addBrowserView()', () => {
  190. it('does not throw for valid args', () => {
  191. const view1 = new BrowserView();
  192. defer(() => view1.webContents.destroy());
  193. w.addBrowserView(view1);
  194. defer(() => w.removeBrowserView(view1));
  195. const view2 = new BrowserView();
  196. defer(() => view2.webContents.destroy());
  197. w.addBrowserView(view2);
  198. defer(() => w.removeBrowserView(view2));
  199. });
  200. it('does not throw if called multiple times with same view', () => {
  201. view = new BrowserView();
  202. w.addBrowserView(view);
  203. w.addBrowserView(view);
  204. w.addBrowserView(view);
  205. });
  206. it('does not crash if the BrowserView webContents are destroyed prior to window addition', () => {
  207. expect(() => {
  208. const view1 = new BrowserView();
  209. view1.webContents.destroy();
  210. w.addBrowserView(view1);
  211. }).to.not.throw();
  212. });
  213. it('does not crash if the webContents is destroyed after a URL is loaded', () => {
  214. view = new BrowserView();
  215. expect(async () => {
  216. view.setBounds({ x: 0, y: 0, width: 400, height: 300 });
  217. await view.webContents.loadURL('data:text/html,hello there');
  218. view.webContents.destroy();
  219. }).to.not.throw();
  220. });
  221. it('can handle BrowserView reparenting', async () => {
  222. view = new BrowserView();
  223. w.addBrowserView(view);
  224. view.webContents.loadURL('about:blank');
  225. await once(view.webContents, 'did-finish-load');
  226. const w2 = new BrowserWindow({ show: false });
  227. w2.addBrowserView(view);
  228. w.close();
  229. view.webContents.loadURL(`file://${fixtures}/pages/blank.html`);
  230. await once(view.webContents, 'did-finish-load');
  231. // Clean up - the afterEach hook assumes the webContents on w is still alive.
  232. w = new BrowserWindow({ show: false });
  233. w2.close();
  234. w2.destroy();
  235. });
  236. });
  237. describe('BrowserWindow.removeBrowserView()', () => {
  238. it('does not throw if called multiple times with same view', () => {
  239. expect(() => {
  240. view = new BrowserView();
  241. w.addBrowserView(view);
  242. w.removeBrowserView(view);
  243. w.removeBrowserView(view);
  244. }).to.not.throw();
  245. });
  246. it('can be called on a BrowserView with a destroyed webContents', async () => {
  247. view = new BrowserView();
  248. w.addBrowserView(view);
  249. await view.webContents.loadURL('data:text/html,hello there');
  250. const destroyed = once(view.webContents, 'destroyed');
  251. view.webContents.close();
  252. await destroyed;
  253. w.removeBrowserView(view);
  254. });
  255. });
  256. describe('BrowserWindow.getBrowserViews()', () => {
  257. it('returns same views as was added', () => {
  258. const view1 = new BrowserView();
  259. defer(() => view1.webContents.destroy());
  260. w.addBrowserView(view1);
  261. defer(() => w.removeBrowserView(view1));
  262. const view2 = new BrowserView();
  263. defer(() => view2.webContents.destroy());
  264. w.addBrowserView(view2);
  265. defer(() => w.removeBrowserView(view2));
  266. const views = w.getBrowserViews();
  267. expect(views).to.have.lengthOf(2);
  268. expect(views[0].webContents.id).to.equal(view1.webContents.id);
  269. expect(views[1].webContents.id).to.equal(view2.webContents.id);
  270. });
  271. it('persists ordering by z-index', () => {
  272. const view1 = new BrowserView();
  273. defer(() => view1.webContents.destroy());
  274. w.addBrowserView(view1);
  275. defer(() => w.removeBrowserView(view1));
  276. const view2 = new BrowserView();
  277. defer(() => view2.webContents.destroy());
  278. w.addBrowserView(view2);
  279. defer(() => w.removeBrowserView(view2));
  280. w.setTopBrowserView(view1);
  281. const views = w.getBrowserViews();
  282. expect(views).to.have.lengthOf(2);
  283. expect(views[0].webContents.id).to.equal(view2.webContents.id);
  284. expect(views[1].webContents.id).to.equal(view1.webContents.id);
  285. });
  286. });
  287. describe('BrowserWindow.setTopBrowserView()', () => {
  288. it('should throw an error when a BrowserView is not attached to the window', () => {
  289. view = new BrowserView();
  290. expect(() => {
  291. w.setTopBrowserView(view);
  292. }).to.throw(/is not attached/);
  293. });
  294. it('should throw an error when a BrowserView is attached to some other window', () => {
  295. view = new BrowserView();
  296. const win2 = new BrowserWindow();
  297. w.addBrowserView(view);
  298. view.setBounds({ x: 0, y: 0, width: 100, height: 100 });
  299. win2.addBrowserView(view);
  300. expect(() => {
  301. w.setTopBrowserView(view);
  302. }).to.throw(/is not attached/);
  303. win2.close();
  304. win2.destroy();
  305. });
  306. });
  307. describe('BrowserView.webContents.getOwnerBrowserWindow()', () => {
  308. it('points to owning window', () => {
  309. view = new BrowserView();
  310. expect(view.webContents.getOwnerBrowserWindow()).to.be.null('owner browser window');
  311. w.setBrowserView(view);
  312. expect(view.webContents.getOwnerBrowserWindow()).to.equal(w);
  313. w.setBrowserView(null);
  314. expect(view.webContents.getOwnerBrowserWindow()).to.be.null('owner browser window');
  315. });
  316. });
  317. describe('shutdown behavior', () => {
  318. it('does not crash on exit', async () => {
  319. const rc = await startRemoteControlApp();
  320. await rc.remotely(() => {
  321. const { BrowserView, app } = require('electron');
  322. // eslint-disable-next-line no-new
  323. new BrowserView({});
  324. setTimeout(() => {
  325. app.quit();
  326. });
  327. });
  328. const [code] = await once(rc.process, 'exit');
  329. expect(code).to.equal(0);
  330. });
  331. it('does not crash on exit if added to a browser window', async () => {
  332. const rc = await startRemoteControlApp();
  333. await rc.remotely(() => {
  334. const { app, BrowserView, BrowserWindow } = require('electron');
  335. const bv = new BrowserView();
  336. bv.webContents.loadURL('about:blank');
  337. const bw = new BrowserWindow({ show: false });
  338. bw.addBrowserView(bv);
  339. setTimeout(() => {
  340. app.quit();
  341. });
  342. });
  343. const [code] = await once(rc.process, 'exit');
  344. expect(code).to.equal(0);
  345. });
  346. it('emits the destroyed event when webContents.close() is called', async () => {
  347. view = new BrowserView();
  348. w.setBrowserView(view);
  349. await view.webContents.loadFile(path.join(fixtures, 'pages', 'a.html'));
  350. view.webContents.close();
  351. await once(view.webContents, 'destroyed');
  352. });
  353. it('emits the destroyed event when window.close() is called', async () => {
  354. view = new BrowserView();
  355. w.setBrowserView(view);
  356. await view.webContents.loadFile(path.join(fixtures, 'pages', 'a.html'));
  357. view.webContents.executeJavaScript('window.close()');
  358. await once(view.webContents, 'destroyed');
  359. });
  360. });
  361. describe('window.open()', () => {
  362. it('works in BrowserView', (done) => {
  363. view = new BrowserView();
  364. w.setBrowserView(view);
  365. view.webContents.setWindowOpenHandler(({ url, frameName }) => {
  366. expect(url).to.equal('http://host/');
  367. expect(frameName).to.equal('host');
  368. done();
  369. return { action: 'deny' };
  370. });
  371. view.webContents.loadFile(path.join(fixtures, 'pages', 'window-open.html'));
  372. });
  373. });
  374. describe('BrowserView.capturePage(rect)', () => {
  375. it('returns a Promise with a Buffer', async () => {
  376. view = new BrowserView({
  377. webPreferences: {
  378. backgroundThrottling: false
  379. }
  380. });
  381. w.addBrowserView(view);
  382. view.setBounds({
  383. ...w.getBounds(),
  384. x: 0,
  385. y: 0
  386. });
  387. const image = await view.webContents.capturePage({
  388. x: 0,
  389. y: 0,
  390. width: 100,
  391. height: 100
  392. });
  393. expect(image.isEmpty()).to.equal(true);
  394. });
  395. xit('resolves after the window is hidden and capturer count is non-zero', async () => {
  396. view = new BrowserView({
  397. webPreferences: {
  398. backgroundThrottling: false
  399. }
  400. });
  401. w.setBrowserView(view);
  402. view.setBounds({
  403. ...w.getBounds(),
  404. x: 0,
  405. y: 0
  406. });
  407. await view.webContents.loadFile(path.join(fixtures, 'pages', 'a.html'));
  408. const image = await view.webContents.capturePage();
  409. expect(image.isEmpty()).to.equal(false);
  410. });
  411. });
  412. });