test: convert a few more specs to async/await (#39712)

This commit is contained in:
Milan Burda 2023-09-04 12:33:29 +02:00 committed by GitHub
parent 54d8402a6c
commit f27b034045
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 70 additions and 89 deletions

View file

@ -300,18 +300,14 @@ describe('BrowserView module', () => {
}).to.not.throw(); }).to.not.throw();
}); });
it('can be called on a BrowserView with a destroyed webContents', (done) => { it('can be called on a BrowserView with a destroyed webContents', async () => {
view = new BrowserView(); view = new BrowserView();
w.addBrowserView(view); w.addBrowserView(view);
await view.webContents.loadURL('data:text/html,hello there');
view.webContents.on('destroyed', () => { const destroyed = once(view.webContents, 'destroyed');
w.removeBrowserView(view); view.webContents.close();
done(); await destroyed;
}); w.removeBrowserView(view);
view.webContents.loadURL('data:text/html,hello there').then(() => {
view.webContents.close();
});
}); });
}); });

View file

@ -589,7 +589,7 @@ describe('BrowserWindow module', () => {
describe('will-frame-navigate event', () => { describe('will-frame-navigate event', () => {
let server = null as unknown as http.Server; let server = null as unknown as http.Server;
let url = null as unknown as string; let url = null as unknown as string;
before((done) => { before(async () => {
server = http.createServer((req, res) => { server = http.createServer((req, res) => {
if (req.url === '/navigate-top') { if (req.url === '/navigate-top') {
res.end('<a target=_top href="/">navigate _top</a>'); res.end('<a target=_top href="/">navigate _top</a>');
@ -609,10 +609,7 @@ describe('BrowserWindow module', () => {
res.end(''); res.end('');
} }
}); });
server.listen(0, '127.0.0.1', () => { url = (await listen(server)).url;
url = `http://127.0.0.1:${(server.address() as AddressInfo).port}/`;
done();
});
}); });
after(() => { after(() => {
@ -683,7 +680,7 @@ describe('BrowserWindow module', () => {
resolve(e.url); resolve(e.url);
}); });
}); });
expect(navigatedTo).to.equal(url); expect(navigatedTo).to.equal(url + '/');
expect(w.webContents.getURL()).to.match(/^file:/); expect(w.webContents.getURL()).to.match(/^file:/);
}); });
@ -696,7 +693,7 @@ describe('BrowserWindow module', () => {
resolve(e.url); resolve(e.url);
}); });
}); });
expect(navigatedTo).to.equal(url); expect(navigatedTo).to.equal(url + '/');
expect(w.webContents.getURL()).to.equal('about:blank'); expect(w.webContents.getURL()).to.equal('about:blank');
}); });
@ -883,7 +880,7 @@ describe('BrowserWindow module', () => {
'did-frame-navigate', 'did-frame-navigate',
'did-navigate' 'did-navigate'
]; ];
before((done) => { before(async () => {
server = http.createServer((req, res) => { server = http.createServer((req, res) => {
if (req.url === '/navigate') { if (req.url === '/navigate') {
res.end('<a href="/">navigate</a>'); res.end('<a href="/">navigate</a>');
@ -899,10 +896,7 @@ describe('BrowserWindow module', () => {
res.end(''); res.end('');
} }
}); });
server.listen(0, '127.0.0.1', () => { url = (await listen(server)).url;
url = `http://127.0.0.1:${(server.address() as AddressInfo).port}/`;
done();
});
}); });
it('for initial navigation, event order is consistent', async () => { it('for initial navigation, event order is consistent', async () => {
const firedEvents: string[] = []; const firedEvents: string[] = [];
@ -929,7 +923,7 @@ describe('BrowserWindow module', () => {
'did-frame-navigate', 'did-frame-navigate',
'did-navigate' 'did-navigate'
]; ];
w.loadURL(`${url}navigate`); w.loadURL(url + '/navigate');
await once(w.webContents, 'did-navigate'); await once(w.webContents, 'did-navigate');
await setTimeout(2000); await setTimeout(2000);
Promise.all(navigationEvents.map(event => Promise.all(navigationEvents.map(event =>
@ -972,7 +966,7 @@ describe('BrowserWindow module', () => {
'did-frame-navigate', 'did-frame-navigate',
'did-navigate' 'did-navigate'
]; ];
w.loadURL(`${url}redirect`); w.loadURL(url + '/redirect');
await once(w.webContents, 'did-navigate'); await once(w.webContents, 'did-navigate');
await setTimeout(2000); await setTimeout(2000);
Promise.all(navigationEvents.map(event => Promise.all(navigationEvents.map(event =>
@ -1010,7 +1004,7 @@ describe('BrowserWindow module', () => {
'did-start-navigation', 'did-start-navigation',
'did-navigate-in-page' 'did-navigate-in-page'
]; ];
w.loadURL(`${url}in-page`); w.loadURL(url + '/in-page');
await once(w.webContents, 'did-navigate'); await once(w.webContents, 'did-navigate');
await setTimeout(2000); await setTimeout(2000);
Promise.all(navigationEvents.map(event => Promise.all(navigationEvents.map(event =>
@ -4729,7 +4723,7 @@ describe('BrowserWindow module', () => {
expect(c.isVisible()).to.be.true('child is visible'); expect(c.isVisible()).to.be.true('child is visible');
}); });
it('closes a grandchild window when a middle child window is destroyed', (done) => { it('closes a grandchild window when a middle child window is destroyed', async () => {
const w = new BrowserWindow(); const w = new BrowserWindow();
w.loadFile(path.join(fixtures, 'pages', 'base-page.html')); w.loadFile(path.join(fixtures, 'pages', 'base-page.html'));
@ -4739,12 +4733,12 @@ describe('BrowserWindow module', () => {
const childWindow = new BrowserWindow({ parent: window }); const childWindow = new BrowserWindow({ parent: window });
await setTimeout(); await setTimeout();
window.close();
childWindow.on('closed', () => { const closed = once(childWindow, 'closed');
expect(() => { BrowserWindow.getFocusedWindow(); }).to.not.throw(); window.close();
done(); await closed;
});
expect(() => { BrowserWindow.getFocusedWindow(); }).to.not.throw();
}); });
}); });

View file

@ -813,15 +813,17 @@ describe('Menu module', function () {
}).to.not.throw(); }).to.not.throw();
}); });
it('should emit menu-will-show event', (done) => { it('should emit menu-will-show event', async () => {
menu.on('menu-will-show', () => { done(); }); const menuWillShow = once(menu, 'menu-will-show');
menu.popup({ window: w }); menu.popup({ window: w });
await menuWillShow;
}); });
it('should emit menu-will-close event', (done) => { it('should emit menu-will-close event', async () => {
menu.on('menu-will-close', () => { done(); }); const menuWillClose = once(menu, 'menu-will-close');
menu.popup({ window: w }); menu.popup({ window: w });
menu.closePopup(); menu.closePopup();
await menuWillClose;
}); });
it('returns immediately', () => { it('returns immediately', () => {

View file

@ -263,7 +263,7 @@ describe('protocol module', () => {
expect(r.headers).to.have.property('x-great-header', 'sogreat'); expect(r.headers).to.have.property('x-great-header', 'sogreat');
}); });
it('can load iframes with custom protocols', (done) => { it('can load iframes with custom protocols', async () => {
registerFileProtocol('custom', (request, callback) => { registerFileProtocol('custom', (request, callback) => {
const filename = request.url.substring(9); const filename = request.url.substring(9);
const p = path.join(__dirname, 'fixtures', 'pages', filename); const p = path.join(__dirname, 'fixtures', 'pages', filename);
@ -278,8 +278,9 @@ describe('protocol module', () => {
} }
}); });
const loaded = once(ipcMain, 'loaded-iframe-custom-protocol');
w.loadFile(path.join(__dirname, 'fixtures', 'pages', 'iframe-protocol.html')); w.loadFile(path.join(__dirname, 'fixtures', 'pages', 'iframe-protocol.html'));
ipcMain.once('loaded-iframe-custom-protocol', () => done()); await loaded;
}); });
it('sends object as response', async () => { it('sends object as response', async () => {

View file

@ -3,6 +3,7 @@ import { Menu, Tray } from 'electron/main';
import { nativeImage } from 'electron/common'; import { nativeImage } from 'electron/common';
import { ifdescribe, ifit } from './lib/spec-helpers'; import { ifdescribe, ifit } from './lib/spec-helpers';
import * as path from 'node:path'; import * as path from 'node:path';
import { setTimeout } from 'node:timers/promises';
describe('tray module', () => { describe('tray module', () => {
let tray: Tray; let tray: Tray;
@ -74,12 +75,11 @@ describe('tray module', () => {
}); });
describe('tray.popUpContextMenu()', () => { describe('tray.popUpContextMenu()', () => {
ifit(process.platform === 'win32')('can be called when menu is showing', function (done) { ifit(process.platform === 'win32')('can be called when menu is showing', async function () {
tray.setContextMenu(Menu.buildFromTemplate([{ label: 'Test' }])); tray.setContextMenu(Menu.buildFromTemplate([{ label: 'Test' }]));
setTimeout(() => { const timeout = setTimeout();
tray.popUpContextMenu(); tray.popUpContextMenu();
done(); await timeout;
});
tray.popUpContextMenu(); tray.popUpContextMenu();
}); });
@ -115,14 +115,13 @@ describe('tray module', () => {
}); });
describe('tray.closeContextMenu()', () => { describe('tray.closeContextMenu()', () => {
ifit(process.platform === 'win32')('does not crash when called more than once', function (done) { ifit(process.platform === 'win32')('does not crash when called more than once', async function () {
tray.setContextMenu(Menu.buildFromTemplate([{ label: 'Test' }])); tray.setContextMenu(Menu.buildFromTemplate([{ label: 'Test' }]));
setTimeout(() => { const timeout = setTimeout();
tray.closeContextMenu();
tray.closeContextMenu();
done();
});
tray.popUpContextMenu(); tray.popUpContextMenu();
await timeout;
tray.closeContextMenu();
tray.closeContextMenu();
}); });
}); });

View file

@ -1218,8 +1218,7 @@ describe('webContents module', () => {
res.end(); res.end();
}); });
}); });
server.listen(0, '127.0.0.1', () => { listen(server).then(({ url }) => {
const url = 'http://127.0.0.1:' + (server.address() as AddressInfo).port;
const content = `<iframe src=${url}></iframe>`; const content = `<iframe src=${url}></iframe>`;
w.webContents.on('did-frame-finish-load', (e, isMainFrame) => { w.webContents.on('did-frame-finish-load', (e, isMainFrame) => {
if (!isMainFrame) { if (!isMainFrame) {
@ -1591,8 +1590,9 @@ describe('webContents module', () => {
default: default:
done('unsupported endpoint'); done('unsupported endpoint');
} }
}).listen(0, '127.0.0.1', () => { });
serverUrl = 'http://127.0.0.1:' + (server.address() as AddressInfo).port; listen(server).then(({ url }) => {
serverUrl = url;
done(); done();
}); });
}); });
@ -1721,11 +1721,10 @@ describe('webContents module', () => {
} }
res.end('<a id="a" href="/should_have_referrer" target="_blank">link</a>'); res.end('<a id="a" href="/should_have_referrer" target="_blank">link</a>');
}); });
server.listen(0, '127.0.0.1', () => { listen(server).then(({ url }) => {
const url = 'http://127.0.0.1:' + (server.address() as AddressInfo).port + '/';
w.webContents.once('did-finish-load', () => { w.webContents.once('did-finish-load', () => {
w.webContents.setWindowOpenHandler(details => { w.webContents.setWindowOpenHandler(details => {
expect(details.referrer.url).to.equal(url); expect(details.referrer.url).to.equal(url + '/');
expect(details.referrer.policy).to.equal('strict-origin-when-cross-origin'); expect(details.referrer.policy).to.equal('strict-origin-when-cross-origin');
return { action: 'allow' }; return { action: 'allow' };
}); });
@ -1748,11 +1747,10 @@ describe('webContents module', () => {
} }
res.end(''); res.end('');
}); });
server.listen(0, '127.0.0.1', () => { listen(server).then(({ url }) => {
const url = 'http://127.0.0.1:' + (server.address() as AddressInfo).port + '/';
w.webContents.once('did-finish-load', () => { w.webContents.once('did-finish-load', () => {
w.webContents.setWindowOpenHandler(details => { w.webContents.setWindowOpenHandler(details => {
expect(details.referrer.url).to.equal(url); expect(details.referrer.url).to.equal(url + '/');
expect(details.referrer.policy).to.equal('strict-origin-when-cross-origin'); expect(details.referrer.policy).to.equal('strict-origin-when-cross-origin');
return { action: 'allow' }; return { action: 'allow' };
}); });

View file

@ -7,7 +7,7 @@ import * as fs from 'node:fs';
import * as url from 'node:url'; import * as url from 'node:url';
import * as WebSocket from 'ws'; import * as WebSocket from 'ws';
import { ipcMain, protocol, session, WebContents, webContents } from 'electron/main'; import { ipcMain, protocol, session, WebContents, webContents } from 'electron/main';
import { AddressInfo, Socket } from 'node:net'; import { Socket } from 'node:net';
import { listen, defer } from './lib/spec-helpers'; import { listen, defer } from './lib/spec-helpers';
import { once } from 'node:events'; import { once } from 'node:events';
import { ReadableStream } from 'node:stream/web'; import { ReadableStream } from 'node:stream/web';
@ -60,10 +60,7 @@ describe('webRequest module', () => {
before(async () => { before(async () => {
protocol.registerStringProtocol('cors', (req, cb) => cb('')); protocol.registerStringProtocol('cors', (req, cb) => cb(''));
defaultURL = (await listen(server)).url + '/'; defaultURL = (await listen(server)).url + '/';
await new Promise<void>((resolve) => { http2URL = (await listen(h2server)).url + '/';
h2server.listen(0, '127.0.0.1', () => resolve());
});
http2URL = `https://127.0.0.1:${(h2server.address() as AddressInfo).port}/`;
console.log(http2URL); console.log(http2URL);
}); });

View file

@ -13,6 +13,7 @@ import { ifit, ifdescribe, defer, itremote, listen } from './lib/spec-helpers';
import { PipeTransport } from './pipe-transport'; import { PipeTransport } from './pipe-transport';
import * as ws from 'ws'; import * as ws from 'ws';
import { setTimeout } from 'node:timers/promises'; import { setTimeout } from 'node:timers/promises';
import { AddressInfo } from 'node:net';
const features = process._linkedBinding('electron_common_features'); const features = process._linkedBinding('electron_common_features');
@ -56,12 +57,12 @@ describe('reporting api', () => {
res.end('<script>window.navigator.vibrate(1)</script>'); res.end('<script>window.navigator.vibrate(1)</script>');
}); });
await new Promise<void>(resolve => server.listen(0, '127.0.0.1', resolve)); await listen(server);
const bw = new BrowserWindow({ show: false }); const bw = new BrowserWindow({ show: false });
try { try {
const reportGenerated = once(reporting, 'report'); const reportGenerated = once(reporting, 'report');
await bw.loadURL(`https://localhost:${(server.address() as any).port}/a`); await bw.loadURL(`https://localhost:${(server.address() as AddressInfo).port}/a`);
const [reports] = await reportGenerated; const [reports] = await reportGenerated;
expect(reports).to.be.an('array').with.lengthOf(1); expect(reports).to.be.an('array').with.lengthOf(1);

View file

@ -200,29 +200,21 @@ describe('webContents.setWindowOpenHandler', () => {
}); });
// Linux and arm64 platforms (WOA and macOS) do not return any capture sources // Linux and arm64 platforms (WOA and macOS) do not return any capture sources
ifit(process.platform === 'darwin' && process.arch === 'x64')('should not make child window background transparent', (done) => { ifit(process.platform === 'darwin' && process.arch === 'x64')('should not make child window background transparent', async () => {
browserWindow.webContents.setWindowOpenHandler(() => ({ action: 'allow' })); browserWindow.webContents.setWindowOpenHandler(() => ({ action: 'allow' }));
const didCreateWindow = once(browserWindow.webContents, 'did-create-window');
browserWindow.webContents.once('did-create-window', async (childWindow) => {
const display = screen.getPrimaryDisplay();
childWindow.setBounds(display.bounds);
await childWindow.webContents.executeJavaScript("const meta = document.createElement('meta'); meta.name = 'color-scheme'; meta.content = 'dark'; document.head.appendChild(meta); true;");
await setTimeoutAsync(1000);
const screenCapture = await captureScreen();
const centerColor = getPixelColor(screenCapture, {
x: display.size.width / 2,
y: display.size.height / 2
});
try {
// color-scheme is set to dark so background should not be white
expect(areColorsSimilar(centerColor, HexColors.WHITE)).to.be.false();
done();
} catch (err) {
done(err);
}
});
browserWindow.webContents.executeJavaScript("window.open('about:blank') && true"); browserWindow.webContents.executeJavaScript("window.open('about:blank') && true");
const [childWindow] = await didCreateWindow;
const display = screen.getPrimaryDisplay();
childWindow.setBounds(display.bounds);
await childWindow.webContents.executeJavaScript("const meta = document.createElement('meta'); meta.name = 'color-scheme'; meta.content = 'dark'; document.head.appendChild(meta); true;");
await setTimeoutAsync(1000);
const screenCapture = await captureScreen();
const centerColor = getPixelColor(screenCapture, {
x: display.size.width / 2,
y: display.size.height / 2
});
// color-scheme is set to dark so background should not be white
expect(areColorsSimilar(centerColor, HexColors.WHITE)).to.be.false();
}); });
}); });

View file

@ -2,6 +2,7 @@ import * as childProcess from 'node:child_process';
import * as path from 'node:path'; import * as path from 'node:path';
import * as http from 'node:http'; import * as http from 'node:http';
import * as https from 'node:https'; import * as https from 'node:https';
import * as http2 from 'node:http2';
import * as net from 'node:net'; import * as net from 'node:net';
import * as v8 from 'node:v8'; import * as v8 from 'node:v8';
import * as url from 'node:url'; import * as url from 'node:url';
@ -194,10 +195,10 @@ export async function itremote (name: string, fn: Function, args?: any[]) {
}); });
} }
export async function listen (server: http.Server | https.Server) { export async function listen (server: http.Server | https.Server | http2.Http2SecureServer) {
const hostname = '127.0.0.1'; const hostname = '127.0.0.1';
await new Promise<void>(resolve => server.listen(0, hostname, () => resolve())); await new Promise<void>(resolve => server.listen(0, hostname, () => resolve()));
const { port } = server.address() as net.AddressInfo; const { port } = server.address() as net.AddressInfo;
const protocol = (server instanceof https.Server) ? 'https' : 'http'; const protocol = (server instanceof http.Server) ? 'http' : 'https';
return { port, url: url.format({ protocol, hostname, port }) }; return { port, url: url.format({ protocol, hostname, port }) };
} }