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: use delay() helper #24321

Merged
merged 1 commit into from Jun 26, 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
17 changes: 8 additions & 9 deletions spec-main/api-browser-window-spec.ts
Expand Up @@ -9,7 +9,7 @@ import { AddressInfo } from 'net';
import { app, BrowserWindow, BrowserView, ipcMain, OnBeforeSendHeadersListenerDetails, protocol, screen, webContents, session, WebContents } from 'electron/main';

import { emittedOnce, emittedUntil } from './events-helpers';
import { ifit, ifdescribe } from './spec-helpers';
import { ifit, ifdescribe, delay } from './spec-helpers';
import { closeWindow, closeAllWindows } from './window-helpers';

const features = process._linkedBinding('electron_common_features');
Expand Down Expand Up @@ -3145,7 +3145,7 @@ describe('BrowserWindow module', () => {
await w.webContents.executeJavaScript('document.body.webkitRequestFullscreen()', true);
await emittedOnce(w, 'enter-full-screen');
// Wait a tick for the full-screen state to 'stick'
await new Promise(resolve => setTimeout(resolve));
await delay();
w.setFullScreen(false);
await emittedOnce(w, 'leave-html-full-screen');
});
Expand Down Expand Up @@ -3786,7 +3786,6 @@ describe('BrowserWindow module', () => {
});

// fullscreen events are dispatched eagerly and twiddling things too fast can confuse poor Electron
const tick = () => new Promise(resolve => setTimeout(resolve));

ifdescribe(process.platform === 'darwin')('kiosk state', () => {
it('with properties', () => {
Expand All @@ -3798,7 +3797,7 @@ describe('BrowserWindow module', () => {
it('can be changed ', (done) => {
const w = new BrowserWindow();
w.once('enter-full-screen', async () => {
await tick();
await delay();
w.kiosk = false;
expect(w.kiosk).to.be.false();
});
Expand All @@ -3819,7 +3818,7 @@ describe('BrowserWindow module', () => {
it('can be changed ', (done) => {
const w = new BrowserWindow();
w.once('enter-full-screen', async () => {
await tick();
await delay();
w.setKiosk(false);
expect(w.isKiosk()).to.be.false('isKiosk');
});
Expand All @@ -3837,7 +3836,7 @@ describe('BrowserWindow module', () => {
const w = new BrowserWindow({ resizable: false });
w.once('enter-full-screen', async () => {
expect(w.resizable).to.be.true('resizable');
await tick();
await delay();
w.setFullScreen(false);
});
w.once('leave-full-screen', () => {
Expand All @@ -3853,7 +3852,7 @@ describe('BrowserWindow module', () => {
const w = new BrowserWindow();
w.once('enter-full-screen', async () => {
expect(w.isFullScreen()).to.be.true('isFullScreen');
await tick();
await delay();
w.setFullScreen(false);
});
w.once('leave-full-screen', () => {
Expand Down Expand Up @@ -3887,9 +3886,9 @@ describe('BrowserWindow module', () => {
const w = new BrowserWindow();
w.once('enter-full-screen', async () => {
expect(w.isFullScreen()).to.be.true('isFullScreen');
await tick();
await delay();
w.setKiosk(true);
await tick();
await delay();
w.setKiosk(false);
expect(w.isFullScreen()).to.be.true('isFullScreen');
w.setFullScreen(false);
Expand Down
10 changes: 2 additions & 8 deletions spec-main/api-content-tracing-spec.ts
Expand Up @@ -2,21 +2,15 @@ import { expect } from 'chai';
import { app, contentTracing, TraceConfig, TraceCategoriesAndOptions } from 'electron/main';
import * as fs from 'fs';
import * as path from 'path';
import { ifdescribe } from './spec-helpers';

const timeout = async (milliseconds: number) => {
return new Promise((resolve) => {
setTimeout(resolve, milliseconds);
});
};
import { ifdescribe, delay } from './spec-helpers';

// FIXME: The tests are skipped on arm/arm64.
ifdescribe(!(process.platform === 'linux' && ['arm', 'arm64'].includes(process.arch)))('contentTracing', () => {
const record = async (options: TraceConfig | TraceCategoriesAndOptions, outputFilePath: string | undefined, recordTimeInMilliseconds = 1e1) => {
await app.whenReady();

await contentTracing.startRecording(options);
await timeout(recordTimeInMilliseconds);
await delay(recordTimeInMilliseconds);
const resultFilePath = await contentTracing.stopRecording(outputFilePath);

return resultFilePath;
Expand Down
4 changes: 2 additions & 2 deletions spec-main/api-crash-reporter-spec.ts
Expand Up @@ -3,7 +3,7 @@ import * as childProcess from 'child_process';
import * as http from 'http';
import * as Busboy from 'busboy';
import * as path from 'path';
import { ifdescribe, ifit, defer, startRemoteControlApp } from './spec-helpers';
import { ifdescribe, ifit, defer, startRemoteControlApp, delay } from './spec-helpers';
import { app } from 'electron/main';
import { crashReporter } from 'electron/common';
import { AddressInfo } from 'net';
Expand Down Expand Up @@ -281,7 +281,7 @@ ifdescribe(!isLinuxOnArm && !process.mas && !process.env.DISABLE_CRASH_REPORTER_
waitForCrash().then(() => expect.fail('expected not to receive a dump'));
await runCrashApp('renderer', port, ['--no-upload']);
// wait a sec in case the crash reporter is about to upload a crash
await new Promise(resolve => setTimeout(resolve, 1000));
await delay(1000);
expect(getCrashes()).to.have.length(0);
});

Expand Down
4 changes: 2 additions & 2 deletions spec-main/api-power-monitor-spec.ts
Expand Up @@ -8,7 +8,7 @@
// python-dbusmock.
import { expect } from 'chai';
import * as dbus from 'dbus-native';
import { ifdescribe } from './spec-helpers';
import { ifdescribe, delay } from './spec-helpers';
import { promisify } from 'util';

describe('powerMonitor', () => {
Expand Down Expand Up @@ -60,7 +60,7 @@ describe('powerMonitor', () => {
while (retriesRemaining-- > 0) {
calls = await getCalls();
if (calls.length > 0) break;
await new Promise(resolve => setTimeout(resolve, 1000));
await delay(1000);
}
expect(calls).to.be.an('array').that.has.lengthOf(1);
expect(calls[0].slice(1)).to.deep.equal([
Expand Down
7 changes: 1 addition & 6 deletions spec-main/api-protocol-spec.ts
Expand Up @@ -11,6 +11,7 @@ import { EventEmitter } from 'events';
import { closeWindow } from './window-helpers';
import { emittedOnce } from './events-helpers';
import { WebmGenerator } from './video-helpers';
import { delay } from './spec-helpers';

const fixturesPath = path.resolve(__dirname, '..', 'spec', 'fixtures');

Expand All @@ -33,12 +34,6 @@ const postData = {
type: 'string'
};

function delay (ms: number) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}

function getStream (chunkSize = text.length, data: Buffer | string = text) {
const body = new stream.PassThrough();

Expand Down
6 changes: 3 additions & 3 deletions spec-main/api-web-contents-spec.ts
Expand Up @@ -8,7 +8,7 @@ import { BrowserWindow, ipcMain, webContents, session, WebContents, app } from '
import { clipboard } from 'electron/common';
import { emittedOnce } from './events-helpers';
import { closeAllWindows } from './window-helpers';
import { ifdescribe, ifit } from './spec-helpers';
import { ifdescribe, ifit, delay } from './spec-helpers';

const pdfjs = require('pdfjs-dist');
const fixturesPath = path.resolve(__dirname, '..', 'spec', 'fixtures');
Expand Down Expand Up @@ -406,7 +406,7 @@ describe('webContents module', () => {
expect(() => { webContents.getFocusedWebContents(); }).to.not.throw();

// Work around https://github.com/electron/electron/issues/19985
await new Promise(resolve => setTimeout(resolve, 0));
await delay();

const devToolsClosed = emittedOnce(w.webContents, 'devtools-closed');
w.webContents.closeDevTools();
Expand Down Expand Up @@ -490,7 +490,7 @@ describe('webContents module', () => {
emittedOnce(w.webContents, 'devtools-opened'),
emittedOnce(w.webContents, 'devtools-focused')
]);
await new Promise(resolve => setTimeout(resolve, 0));
await delay();
expect(w.isFocused()).to.be.false();
});

Expand Down
4 changes: 2 additions & 2 deletions spec-main/chromium-spec.ts
Expand Up @@ -10,7 +10,7 @@ import * as url from 'url';
import * as ChildProcess from 'child_process';
import { EventEmitter } from 'events';
import { promisify } from 'util';
import { ifit, ifdescribe } from './spec-helpers';
import { ifit, ifdescribe, delay } from './spec-helpers';
import { AddressInfo } from 'net';
import { PipeTransport } from './pipe-transport';

Expand Down Expand Up @@ -1374,7 +1374,7 @@ describe('iframe using HTML fullscreen API while window is OS-fullscreened', ()
"document.querySelector('iframe').contentWindow.postMessage('exitFullscreen', '*')"
);

await new Promise(resolve => setTimeout(resolve, 500));
await delay(500);

const width = await w.webContents.executeJavaScript(
"document.querySelector('iframe').offsetWidth"
Expand Down
2 changes: 1 addition & 1 deletion spec-main/spec-helpers.ts
Expand Up @@ -6,7 +6,7 @@ import * as v8 from 'v8';
export const ifit = (condition: boolean) => (condition ? it : it.skip);
export const ifdescribe = (condition: boolean) => (condition ? describe : describe.skip);

export const delay = (time: number) => new Promise(resolve => setTimeout(resolve, time));
export const delay = (time: number = 0) => new Promise(resolve => setTimeout(resolve, time));

type CleanupFunction = (() => void) | (() => Promise<void>)
const cleanupFunctions: CleanupFunction[] = [];
Expand Down
6 changes: 3 additions & 3 deletions spec-main/spellchecker-spec.ts
Expand Up @@ -4,7 +4,7 @@ import { expect } from 'chai';
import * as path from 'path';
import { closeWindow } from './window-helpers';
import { emittedOnce } from './events-helpers';
import { ifit, ifdescribe } from './spec-helpers';
import { ifit, ifdescribe, delay } from './spec-helpers';

const features = process._linkedBinding('electron_common_features');

Expand Down Expand Up @@ -33,7 +33,7 @@ ifdescribe(features.isBuiltinSpellCheckerEnabled())('spellchecker', () => {
await w.webContents.executeJavaScript('document.body.querySelector("textarea").focus()');
const contextMenuPromise = emittedOnce(w.webContents, 'context-menu');
// Wait for spellchecker to load
await new Promise(resolve => setTimeout(resolve, 500));
await delay(500);
w.webContents.sendInputEvent({
type: 'mouseDown',
button: 'right',
Expand All @@ -50,7 +50,7 @@ ifdescribe(features.isBuiltinSpellCheckerEnabled())('spellchecker', () => {
await w.webContents.executeJavaScript('document.body.querySelector("textarea").focus()');
const contextMenuPromise = emittedOnce(w.webContents, 'context-menu');
// Wait for spellchecker to load
await new Promise(resolve => setTimeout(resolve, 500));
await delay(500);
w.webContents.sendInputEvent({
type: 'mouseDown',
button: 'right',
Expand Down