Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: convert more tests to async / await #24373

Merged
merged 1 commit into from Jul 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 15 additions & 30 deletions spec-main/api-app-spec.ts
Expand Up @@ -5,6 +5,7 @@ import * as http from 'http';
import * as net from 'net';
import * as fs from 'fs';
import * as path from 'path';
import { promisify } from 'util';
import { app, BrowserWindow, Menu, session } from 'electron/main';
import { emittedOnce } from './events-helpers';
import { closeWindow, closeAllWindows } from './window-helpers';
Expand Down Expand Up @@ -891,53 +892,37 @@ describe('app module', () => {
expect(app.isDefaultProtocolClient(protocol)).to.equal(false);
});

it('creates a registry entry for the protocol class', (done) => {
it('creates a registry entry for the protocol class', async () => {
app.setAsDefaultProtocolClient(protocol);

classesKey.keys((error: Error, keys: any[]) => {
if (error) throw error;

const exists = !!keys.find(key => key.key.includes(protocol));
expect(exists).to.equal(true);

done();
});
const keys = await promisify(classesKey.keys).call(classesKey) as any[];
jkleinsc marked this conversation as resolved.
Show resolved Hide resolved
const exists = !!keys.find(key => key.key.includes(protocol));
expect(exists).to.equal(true);
});

it('completely removes a registry entry for the protocol class', (done) => {
it('completely removes a registry entry for the protocol class', async () => {
app.setAsDefaultProtocolClient(protocol);
app.removeAsDefaultProtocolClient(protocol);

classesKey.keys((error: Error, keys: any[]) => {
if (error) throw error;

const exists = !!keys.find(key => key.key.includes(protocol));
expect(exists).to.equal(false);

done();
});
const keys = await promisify(classesKey.keys).call(classesKey) as any[];
const exists = !!keys.find(key => key.key.includes(protocol));
expect(exists).to.equal(false);
});

it('only unsets a class registry key if it contains other data', (done) => {
it('only unsets a class registry key if it contains other data', async () => {
app.setAsDefaultProtocolClient(protocol);

const protocolKey = new Winreg({
hive: Winreg.HKCU,
key: `\\Software\\Classes\\${protocol}`
});

protocolKey.set('test-value', 'REG_BINARY', '123', () => {
app.removeAsDefaultProtocolClient(protocol);

classesKey.keys((error: Error, keys: any[]) => {
if (error) throw error;

const exists = !!keys.find(key => key.key.includes(protocol));
expect(exists).to.equal(true);
await promisify(protocolKey.set).call(protocolKey, 'test-value', 'REG_BINARY', '123');
app.removeAsDefaultProtocolClient(protocol);

done();
});
});
const keys = await promisify(classesKey.keys).call(classesKey) as any[];
const exists = !!keys.find(key => key.key.includes(protocol));
expect(exists).to.equal(true);
});

it('sets the default client such that getApplicationNameForProtocol returns Electron', () => {
Expand Down
100 changes: 48 additions & 52 deletions spec-main/api-browser-window-spec.ts
Expand Up @@ -311,13 +311,15 @@ describe('BrowserWindow module', () => {
server.close();
});

it('should emit did-start-loading event', (done) => {
w.webContents.on('did-start-loading', () => { done(); });
it('should emit did-start-loading event', async () => {
const didStartLoading = emittedOnce(w.webContents, 'did-start-loading');
w.loadURL('about:blank');
await didStartLoading;
});
it('should emit ready-to-show event', (done) => {
w.on('ready-to-show', () => { done(); });
it('should emit ready-to-show event', async () => {
const readyToShow = emittedOnce(w, 'ready-to-show');
w.loadURL('about:blank');
await readyToShow;
});
// TODO(deepak1556): The error code now seems to be `ERR_FAILED`, verify what
// changed and adjust the test.
Expand Down Expand Up @@ -539,11 +541,10 @@ describe('BrowserWindow module', () => {
after(() => {
server.close();
});
it('is emitted on redirects', (done) => {
w.webContents.on('will-redirect', () => {
done();
});
it('is emitted on redirects', async () => {
const willRedirect = emittedOnce(w.webContents, 'will-redirect');
w.loadURL(`${url}/302`);
await willRedirect;
});

it('is emitted after will-navigate on redirects', async () => {
Expand Down Expand Up @@ -1986,15 +1987,6 @@ describe('BrowserWindow module', () => {
});

describe('"sandbox" option', () => {
function waitForEvents<T> (emitter: { once: Function }, events: string[], callback: () => void) {
let count = events.length;
for (const event of events) {
emitter.once(event, () => {
if (!--count) callback();
});
}
}

const preload = path.join(path.resolve(__dirname, 'fixtures'), 'module', 'preload-sandbox.js');

let server: http.Server = null as unknown as http.Server;
Expand Down Expand Up @@ -2201,7 +2193,7 @@ describe('BrowserWindow module', () => {
expect(webPreferences.foo).to.equal('bar');
});

it('should set ipc event sender correctly', (done) => {
it('should set ipc event sender correctly', async () => {
const w = new BrowserWindow({
show: false,
webPreferences: {
Expand All @@ -2224,36 +2216,38 @@ describe('BrowserWindow module', () => {
expect(event.sender).to.equal(childWc, 'sender should be the child');
event.sender.send('verified');
});
waitForEvents(ipcMain, [

const done = Promise.all([
'parent-answer',
'child-answer'
], done);
].map(name => emittedOnce(ipcMain, name)));
w.loadFile(path.join(__dirname, 'fixtures', 'api', 'sandbox.html'), { search: 'verify-ipc-sender' });
await done;
});

describe('event handling', () => {
let w: BrowserWindow = null as unknown as BrowserWindow;
beforeEach(() => {
w = new BrowserWindow({ show: false, webPreferences: { sandbox: true } });
});
it('works for window events', (done) => {
waitForEvents(w, [
'page-title-updated'
], done);
it('works for window events', async () => {
const pageTitleUpdated = emittedOnce(w, 'page-title-updated');
w.loadURL('data:text/html,<script>document.title = \'changed\'</script>');
await pageTitleUpdated;
});

it('works for stop events', (done) => {
waitForEvents(w.webContents, [
it('works for stop events', async () => {
const done = Promise.all([
'did-navigate',
'did-fail-load',
'did-stop-loading'
], done);
].map(name => emittedOnce(w.webContents, name)));
w.loadURL('data:text/html,<script>stop()</script>');
await done;
});

it('works for web contents events', (done) => {
waitForEvents(w.webContents, [
it('works for web contents events', async () => {
const done = Promise.all([
'did-finish-load',
'did-frame-finish-load',
'did-navigate-in-page',
Expand All @@ -2262,8 +2256,9 @@ describe('BrowserWindow module', () => {
'did-stop-loading',
'did-frame-finish-load',
'dom-ready'
], done);
].map(name => emittedOnce(w.webContents, name)));
w.loadFile(path.join(__dirname, 'fixtures', 'api', 'sandbox.html'), { search: 'webcontents-events' });
await done;
});
});

Expand Down Expand Up @@ -2929,26 +2924,29 @@ describe('BrowserWindow module', () => {

ifdescribe(process.platform !== 'linux')('max/minimize events', () => {
afterEach(closeAllWindows);
it('emits an event when window is maximized', (done) => {
it('emits an event when window is maximized', async () => {
const w = new BrowserWindow({ show: false });
w.once('maximize', () => { done(); });
const maximize = emittedOnce(w, 'maximize');
w.show();
w.maximize();
await maximize;
});

it('emits an event when window is unmaximized', (done) => {
it('emits an event when window is unmaximized', async () => {
const w = new BrowserWindow({ show: false });
w.once('unmaximize', () => { done(); });
const unmaximize = emittedOnce(w, 'unmaximize');
w.show();
w.maximize();
w.unmaximize();
await unmaximize;
});

it('emits an event when window is minimized', (done) => {
it('emits an event when window is minimized', async () => {
const w = new BrowserWindow({ show: false });
w.once('minimize', () => { done(); });
const minimize = emittedOnce(w, 'minimize');
w.show();
w.minimize();
await minimize;
});
});

Expand Down Expand Up @@ -3162,26 +3160,26 @@ describe('BrowserWindow module', () => {
describe('parent window', () => {
afterEach(closeAllWindows);

ifit(process.platform === 'darwin')('sheet-begin event emits when window opens a sheet', (done) => {
ifit(process.platform === 'darwin')('sheet-begin event emits when window opens a sheet', async () => {
const w = new BrowserWindow();
w.once('sheet-begin', () => {
done();
});
const sheetBegin = emittedOnce(w, 'sheet-begin');
// eslint-disable-next-line no-new
new BrowserWindow({
modal: true,
parent: w
});
await sheetBegin;
});

ifit(process.platform === 'darwin')('sheet-end event emits when window has closed a sheet', (done) => {
ifit(process.platform === 'darwin')('sheet-end event emits when window has closed a sheet', async () => {
const w = new BrowserWindow();
const sheet = new BrowserWindow({
modal: true,
parent: w
});
w.once('sheet-end', () => { done(); });
const sheetEnd = emittedOnce(w, 'sheet-end');
sheet.close();
await sheetEnd;
});

describe('parent option', () => {
Expand Down Expand Up @@ -3872,24 +3870,22 @@ describe('BrowserWindow module', () => {
expect(w.isFullScreen()).to.be.false('isFullScreen');
});

it('does not crash when exiting simpleFullScreen (properties)', (done) => {
it('does not crash when exiting simpleFullScreen (properties)', async () => {
const w = new BrowserWindow();
w.setSimpleFullScreen(true);

setTimeout(() => {
w.setFullScreen(!w.isFullScreen());
done();
}, 1000);
await delay(1000);

w.setFullScreen(!w.isFullScreen());
});

it('does not crash when exiting simpleFullScreen (functions)', (done) => {
it('does not crash when exiting simpleFullScreen (functions)', async () => {
const w = new BrowserWindow();
w.simpleFullScreen = true;

setTimeout(() => {
w.setFullScreen(!w.isFullScreen());
done();
}, 1000);
await delay(1000);

w.setFullScreen(!w.isFullScreen());
});

it('should not be changed by setKiosk method', async () => {
Expand Down
34 changes: 17 additions & 17 deletions spec-main/api-menu-spec.ts
Expand Up @@ -4,7 +4,7 @@ import { expect } from 'chai';
import { BrowserWindow, Menu, MenuItem } from 'electron/main';
import { sortMenuItems } from '../lib/browser/api/menu-utils';
import { emittedOnce } from './events-helpers';
import { ifit } from './spec-helpers';
import { ifit, delay } from './spec-helpers';
import { closeWindow } from './window-helpers';

const fixturesPath = path.resolve(__dirname, 'fixtures');
Expand Down Expand Up @@ -836,29 +836,29 @@ describe('Menu module', function () {
});
});

it('prevents menu from getting garbage-collected when popuping', (done) => {
it('prevents menu from getting garbage-collected when popuping', async () => {
const menu = Menu.buildFromTemplate([{ role: 'paste' }]);
menu.popup({ window: w });

// Keep a weak reference to the menu.
// eslint-disable-next-line no-undef
const wr = new (globalThis as any).WeakRef(menu);

setTimeout(() => {
// Do garbage collection, since |menu| is not referenced in this closure
// it would be gone after next call.
const v8Util = process._linkedBinding('electron_common_v8_util');
v8Util.requestGarbageCollectionForTesting();
setTimeout(() => {
// Try to receive menu from weak reference.
if (wr.deref()) {
wr.deref().closePopup();
done();
} else {
done('Menu is garbage-collected while popuping');
}
});
});
await delay();

// Do garbage collection, since |menu| is not referenced in this closure
// it would be gone after next call.
const v8Util = process._linkedBinding('electron_common_v8_util');
v8Util.requestGarbageCollectionForTesting();

await delay();

// Try to receive menu from weak reference.
if (wr.deref()) {
wr.deref().closePopup();
} else {
throw new Error('Menu is garbage-collected while popuping');
}
});
});

Expand Down
4 changes: 2 additions & 2 deletions spec-main/api-web-contents-spec.ts
Expand Up @@ -1003,6 +1003,8 @@ describe('webContents module', () => {
defer(() => {
w2.setClosable(true);
w2.close();

protocol.unregisterProtocol(scheme);
});

await w.loadURL(`${scheme}://host3`);
Expand All @@ -1015,8 +1017,6 @@ describe('webContents module', () => {
const zoomLevel2 = w2.webContents.zoomLevel;
expect(zoomLevel2).to.equal(0);
expect(zoomLevel1).to.not.equal(zoomLevel2);

protocol.unregisterProtocol(scheme);
});

it('can persist when it contains iframe', (done) => {
Expand Down