From 40e832fce14fcb819528f711b9e5485094ce9b71 Mon Sep 17 00:00:00 2001 From: samuelmaddock Date: Tue, 20 Oct 2020 02:03:07 -0400 Subject: [PATCH] test: add focus and blur WebContents event tests --- spec-main/api-web-contents-spec.ts | 35 +++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/spec-main/api-web-contents-spec.ts b/spec-main/api-web-contents-spec.ts index d45ce0517bd3b..6346971eed3aa 100644 --- a/spec-main/api-web-contents-spec.ts +++ b/spec-main/api-web-contents-spec.ts @@ -766,10 +766,10 @@ describe('webContents module', () => { }); }); - describe('focus()', () => { - describe('when the web contents is hidden', () => { + describe('focus APIs', () => { + describe('focus()', () => { afterEach(closeAllWindows); - it('does not blur the focused window', async () => { + it('does not blur the focused window when the web contents is hidden', async () => { const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true } }); w.show(); await w.loadURL('about:blank'); @@ -784,6 +784,35 @@ describe('webContents module', () => { expect(childFocused).to.be.false(); }); }); + + describe('focus event', () => { + afterEach(closeAllWindows); + it('is triggered when web contents is focused', async () => { + const w = new BrowserWindow({ show: false }); + await w.loadURL('about:blank'); + const devToolsOpened = emittedOnce(w.webContents, 'devtools-opened'); + w.webContents.openDevTools(); + await devToolsOpened; + w.webContents.devToolsWebContents!.focus(); + const focusPromise = emittedOnce(w.webContents, 'focus'); + w.webContents.focus(); + await expect(focusPromise).to.eventually.be.fulfilled(); + }); + }); + + describe('blur event', () => { + afterEach(closeAllWindows); + it('is triggered when web contents is blurred', async () => { + const w = new BrowserWindow({ show: false }); + await w.loadURL('about:blank'); + const blurPromise = emittedOnce(w.webContents, 'blur'); + const devToolsOpened = emittedOnce(w.webContents, 'devtools-opened'); + w.webContents.openDevTools(); + await devToolsOpened; + w.webContents.devToolsWebContents!.focus(); + await expect(blurPromise).to.eventually.be.fulfilled(); + }); + }); }); describe('getOSProcessId()', () => {