From 7c2ac6b7c180d0ae248540e9bf564a8e6dd207a2 Mon Sep 17 00:00:00 2001 From: Jeremy Rose Date: Tue, 17 Nov 2020 11:12:02 -0800 Subject: [PATCH] fix: BrowserWindow.fromBrowserView in multiple-BrowserView windows (#26493) --- lib/browser/api/browser-window.ts | 6 +----- spec-main/api-browser-window-spec.ts | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/lib/browser/api/browser-window.ts b/lib/browser/api/browser-window.ts index 2aa8840f010e..d01be9c795f0 100644 --- a/lib/browser/api/browser-window.ts +++ b/lib/browser/api/browser-window.ts @@ -96,11 +96,7 @@ BrowserWindow.fromWebContents = (webContents: WebContents) => { }; BrowserWindow.fromBrowserView = (browserView: BrowserView) => { - for (const window of BrowserWindow.getAllWindows()) { - if (window.getBrowserView() === browserView) return window; - } - - return null; + return BrowserWindow.fromWebContents(browserView.webContents); }; BrowserWindow.prototype.setTouchBar = function (touchBar) { diff --git a/spec-main/api-browser-window-spec.ts b/spec-main/api-browser-window-spec.ts index 5a3d3f63eec3..587457c028b9 100644 --- a/spec-main/api-browser-window-spec.ts +++ b/spec-main/api-browser-window-spec.ts @@ -1634,7 +1634,7 @@ describe('BrowserWindow module', () => { describe('BrowserWindow.fromBrowserView(browserView)', () => { afterEach(closeAllWindows); - it('returns the window with the browserView', () => { + it('returns the window with the BrowserView', () => { const w = new BrowserWindow({ show: false }); const bv = new BrowserView(); w.setBrowserView(bv); @@ -1645,6 +1645,22 @@ describe('BrowserWindow module', () => { expect(BrowserWindow.fromBrowserView(bv)!.id).to.equal(w.id); }); + it('returns the window when there are multiple BrowserViews', () => { + const w = new BrowserWindow({ show: false }); + const bv1 = new BrowserView(); + w.addBrowserView(bv1); + const bv2 = new BrowserView(); + w.addBrowserView(bv2); + defer(() => { + w.removeBrowserView(bv1); + w.removeBrowserView(bv2); + (bv1.webContents as any).destroy(); + (bv2.webContents as any).destroy(); + }); + expect(BrowserWindow.fromBrowserView(bv1)!.id).to.equal(w.id); + expect(BrowserWindow.fromBrowserView(bv2)!.id).to.equal(w.id); + }); + it('returns undefined if not attached', () => { const bv = new BrowserView(); defer(() => {