api-browser-view-spec.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. });
  125. describe('BrowserView.getBounds()', () => {
  126. it('returns the current bounds', () => {
  127. view = new BrowserView();
  128. const bounds = { x: 10, y: 20, width: 30, height: 40 };
  129. view.setBounds(bounds);
  130. expect(view.getBounds()).to.deep.equal(bounds);
  131. });
  132. });
  133. describe('BrowserWindow.setBrowserView()', () => {
  134. it('does not throw for valid args', () => {
  135. view = new BrowserView();
  136. w.setBrowserView(view);
  137. });
  138. it('does not throw if called multiple times with same view', () => {
  139. view = new BrowserView();
  140. w.setBrowserView(view);
  141. w.setBrowserView(view);
  142. w.setBrowserView(view);
  143. });
  144. });
  145. describe('BrowserWindow.getBrowserView()', () => {
  146. it('returns the set view', () => {
  147. view = new BrowserView();
  148. w.setBrowserView(view);
  149. const view2 = w.getBrowserView();
  150. expect(view2!.webContents.id).to.equal(view.webContents.id);
  151. });
  152. it('returns null if none is set', () => {
  153. const view = w.getBrowserView();
  154. expect(view).to.be.null('view');
  155. });
  156. });
  157. describe('BrowserWindow.addBrowserView()', () => {
  158. it('does not throw for valid args', () => {
  159. const view1 = new BrowserView();
  160. defer(() => view1.webContents.destroy());
  161. w.addBrowserView(view1);
  162. defer(() => w.removeBrowserView(view1));
  163. const view2 = new BrowserView();
  164. defer(() => view2.webContents.destroy());
  165. w.addBrowserView(view2);
  166. defer(() => w.removeBrowserView(view2));
  167. });
  168. it('does not throw if called multiple times with same view', () => {
  169. view = new BrowserView();
  170. w.addBrowserView(view);
  171. w.addBrowserView(view);
  172. w.addBrowserView(view);
  173. });
  174. it('does not crash if the BrowserView webContents are destroyed prior to window addition', () => {
  175. expect(() => {
  176. const view1 = new BrowserView();
  177. view1.webContents.destroy();
  178. w.addBrowserView(view1);
  179. }).to.not.throw();
  180. });
  181. it('does not crash if the webContents is destroyed after a URL is loaded', () => {
  182. view = new BrowserView();
  183. expect(async () => {
  184. view.setBounds({ x: 0, y: 0, width: 400, height: 300 });
  185. await view.webContents.loadURL('data:text/html,hello there');
  186. view.webContents.destroy();
  187. }).to.not.throw();
  188. });
  189. it('can handle BrowserView reparenting', async () => {
  190. view = new BrowserView();
  191. w.addBrowserView(view);
  192. view.webContents.loadURL('about:blank');
  193. await once(view.webContents, 'did-finish-load');
  194. const w2 = new BrowserWindow({ show: false });
  195. w2.addBrowserView(view);
  196. w.close();
  197. view.webContents.loadURL(`file://${fixtures}/pages/blank.html`);
  198. await once(view.webContents, 'did-finish-load');
  199. // Clean up - the afterEach hook assumes the webContents on w is still alive.
  200. w = new BrowserWindow({ show: false });
  201. w2.close();
  202. w2.destroy();
  203. });
  204. });
  205. describe('BrowserWindow.removeBrowserView()', () => {
  206. it('does not throw if called multiple times with same view', () => {
  207. expect(() => {
  208. view = new BrowserView();
  209. w.addBrowserView(view);
  210. w.removeBrowserView(view);
  211. w.removeBrowserView(view);
  212. }).to.not.throw();
  213. });
  214. it('can be called on a BrowserView with a destroyed webContents', (done) => {
  215. view = new BrowserView();
  216. w.addBrowserView(view);
  217. view.webContents.on('destroyed', () => {
  218. w.removeBrowserView(view);
  219. done();
  220. });
  221. view.webContents.loadURL('data:text/html,hello there').then(() => {
  222. view.webContents.close();
  223. });
  224. });
  225. });
  226. describe('BrowserWindow.getBrowserViews()', () => {
  227. it('returns same views as was added', () => {
  228. const view1 = new BrowserView();
  229. defer(() => view1.webContents.destroy());
  230. w.addBrowserView(view1);
  231. defer(() => w.removeBrowserView(view1));
  232. const view2 = new BrowserView();
  233. defer(() => view2.webContents.destroy());
  234. w.addBrowserView(view2);
  235. defer(() => w.removeBrowserView(view2));
  236. const views = w.getBrowserViews();
  237. expect(views).to.have.lengthOf(2);
  238. expect(views[0].webContents.id).to.equal(view1.webContents.id);
  239. expect(views[1].webContents.id).to.equal(view2.webContents.id);
  240. });
  241. it('persists ordering by z-index', () => {
  242. const view1 = new BrowserView();
  243. defer(() => view1.webContents.destroy());
  244. w.addBrowserView(view1);
  245. defer(() => w.removeBrowserView(view1));
  246. const view2 = new BrowserView();
  247. defer(() => view2.webContents.destroy());
  248. w.addBrowserView(view2);
  249. defer(() => w.removeBrowserView(view2));
  250. w.setTopBrowserView(view1);
  251. const views = w.getBrowserViews();
  252. expect(views).to.have.lengthOf(2);
  253. expect(views[0].webContents.id).to.equal(view2.webContents.id);
  254. expect(views[1].webContents.id).to.equal(view1.webContents.id);
  255. });
  256. });
  257. describe('BrowserWindow.setTopBrowserView()', () => {
  258. it('should throw an error when a BrowserView is not attached to the window', () => {
  259. view = new BrowserView();
  260. expect(() => {
  261. w.setTopBrowserView(view);
  262. }).to.throw(/is not attached/);
  263. });
  264. it('should throw an error when a BrowserView is attached to some other window', () => {
  265. view = new BrowserView();
  266. const win2 = new BrowserWindow();
  267. w.addBrowserView(view);
  268. view.setBounds({ x: 0, y: 0, width: 100, height: 100 });
  269. win2.addBrowserView(view);
  270. expect(() => {
  271. w.setTopBrowserView(view);
  272. }).to.throw(/is not attached/);
  273. win2.close();
  274. win2.destroy();
  275. });
  276. });
  277. describe('BrowserView.webContents.getOwnerBrowserWindow()', () => {
  278. it('points to owning window', () => {
  279. view = new BrowserView();
  280. expect(view.webContents.getOwnerBrowserWindow()).to.be.null('owner browser window');
  281. w.setBrowserView(view);
  282. expect(view.webContents.getOwnerBrowserWindow()).to.equal(w);
  283. w.setBrowserView(null);
  284. expect(view.webContents.getOwnerBrowserWindow()).to.be.null('owner browser window');
  285. });
  286. });
  287. describe('shutdown behavior', () => {
  288. it('does not crash on exit', async () => {
  289. const rc = await startRemoteControlApp();
  290. await rc.remotely(() => {
  291. const { BrowserView, app } = require('electron');
  292. // eslint-disable-next-line no-new
  293. new BrowserView({});
  294. setTimeout(() => {
  295. app.quit();
  296. });
  297. });
  298. const [code] = await once(rc.process, 'exit');
  299. expect(code).to.equal(0);
  300. });
  301. it('does not crash on exit if added to a browser window', async () => {
  302. const rc = await startRemoteControlApp();
  303. await rc.remotely(() => {
  304. const { app, BrowserView, BrowserWindow } = require('electron');
  305. const bv = new BrowserView();
  306. bv.webContents.loadURL('about:blank');
  307. const bw = new BrowserWindow({ show: false });
  308. bw.addBrowserView(bv);
  309. setTimeout(() => {
  310. app.quit();
  311. });
  312. });
  313. const [code] = await once(rc.process, 'exit');
  314. expect(code).to.equal(0);
  315. });
  316. it('emits the destroyed event when webContents.close() is called', async () => {
  317. view = new BrowserView();
  318. w.setBrowserView(view);
  319. await view.webContents.loadFile(path.join(fixtures, 'pages', 'a.html'));
  320. view.webContents.close();
  321. await once(view.webContents, 'destroyed');
  322. });
  323. it('emits the destroyed event when window.close() is called', async () => {
  324. view = new BrowserView();
  325. w.setBrowserView(view);
  326. await view.webContents.loadFile(path.join(fixtures, 'pages', 'a.html'));
  327. view.webContents.executeJavaScript('window.close()');
  328. await once(view.webContents, 'destroyed');
  329. });
  330. });
  331. describe('window.open()', () => {
  332. it('works in BrowserView', (done) => {
  333. view = new BrowserView();
  334. w.setBrowserView(view);
  335. view.webContents.setWindowOpenHandler(({ url, frameName }) => {
  336. expect(url).to.equal('http://host/');
  337. expect(frameName).to.equal('host');
  338. done();
  339. return { action: 'deny' };
  340. });
  341. view.webContents.loadFile(path.join(fixtures, 'pages', 'window-open.html'));
  342. });
  343. });
  344. describe('BrowserView.capturePage(rect)', () => {
  345. it('returns a Promise with a Buffer', async () => {
  346. view = new BrowserView({
  347. webPreferences: {
  348. backgroundThrottling: false
  349. }
  350. });
  351. w.addBrowserView(view);
  352. view.setBounds({
  353. ...w.getBounds(),
  354. x: 0,
  355. y: 0
  356. });
  357. const image = await view.webContents.capturePage({
  358. x: 0,
  359. y: 0,
  360. width: 100,
  361. height: 100
  362. });
  363. expect(image.isEmpty()).to.equal(true);
  364. });
  365. xit('resolves after the window is hidden and capturer count is non-zero', async () => {
  366. view = new BrowserView({
  367. webPreferences: {
  368. backgroundThrottling: false
  369. }
  370. });
  371. w.setBrowserView(view);
  372. view.setBounds({
  373. ...w.getBounds(),
  374. x: 0,
  375. y: 0
  376. });
  377. await view.webContents.loadFile(path.join(fixtures, 'pages', 'a.html'));
  378. const image = await view.webContents.capturePage();
  379. expect(image.isEmpty()).to.equal(false);
  380. });
  381. });
  382. });