test: make sure tests fail properly instead of timing out (#24316)

This commit is contained in:
Milan Burda 2020-07-01 00:10:36 +02:00 committed by GitHub
parent 451086d7f2
commit c6db47182a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
20 changed files with 1484 additions and 1367 deletions

View file

@ -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, delay } from './spec-helpers';
import { ifit, ifdescribe, delay, defer } from './spec-helpers';
import { AddressInfo } from 'net';
import { PipeTransport } from './pipe-transport';
@ -257,22 +257,21 @@ describe('command line switches', () => {
});
describe('--lang switch', () => {
const currentLocale = app.getLocale();
const testLocale = (locale: string, result: string, done: () => void) => {
const testLocale = async (locale: string, result: string) => {
const appPath = path.join(fixturesPath, 'api', 'locale-check');
const electronPath = process.execPath;
let output = '';
appProcess = ChildProcess.spawn(electronPath, [appPath, `--lang=${locale}`]);
let output = '';
appProcess.stdout.on('data', (data) => { output += data; });
appProcess.stdout.on('end', () => {
output = output.replace(/(\r\n|\n|\r)/gm, '');
expect(output).to.equal(result);
done();
});
await emittedOnce(appProcess.stdout, 'end');
output = output.replace(/(\r\n|\n|\r)/gm, '');
expect(output).to.equal(result);
};
it('should set the locale', (done) => testLocale('fr', 'fr', done));
it('should not set an invalid locale', (done) => testLocale('asdfkl', currentLocale, done));
it('should set the locale', async () => testLocale('fr', 'fr'));
it('should not set an invalid locale', async () => testLocale('asdfkl', currentLocale));
});
describe('--remote-debugging-pipe switch', () => {
@ -330,10 +329,15 @@ describe('command line switches', () => {
appProcess!.stderr.removeAllListeners('data');
const port = m[1];
http.get(`http://127.0.0.1:${port}`, (res) => {
res.destroy();
expect(res.statusCode).to.eql(200);
expect(parseInt(res.headers['content-length']!)).to.be.greaterThan(0);
done();
try {
expect(res.statusCode).to.eql(200);
expect(parseInt(res.headers['content-length']!)).to.be.greaterThan(0);
done();
} catch (e) {
done(e);
} finally {
res.destroy();
}
});
}
});
@ -551,7 +555,7 @@ describe('chromium features', () => {
describe('window.open', () => {
for (const show of [true, false]) {
it(`inherits parent visibility over parent {show=${show}} option`, (done) => {
it(`inherits parent visibility over parent {show=${show}} option`, async () => {
const w = new BrowserWindow({ show });
// toggle visibility
@ -561,12 +565,12 @@ describe('chromium features', () => {
w.show();
}
w.webContents.once('new-window', (e, url, frameName, disposition, options) => {
expect(options.show).to.equal(w.isVisible());
w.close();
done();
});
defer(() => { w.close(); });
const newWindow = emittedOnce(w.webContents, 'new-window');
w.loadFile(path.join(fixturesPath, 'pages', 'window-open.html'));
const [,,,, options] = await newWindow;
expect(options.show).to.equal(w.isVisible());
});
}
@ -602,7 +606,7 @@ describe('chromium features', () => {
expect(preferences.javascript).to.be.false();
});
it('handles cycles when merging the parent options into the child options', (done) => {
it('handles cycles when merging the parent options into the child options', async () => {
const foo = {} as any;
foo.bar = foo;
foo.baz = {
@ -614,22 +618,20 @@ describe('chromium features', () => {
const w = new BrowserWindow({ show: false, foo: foo } as any);
w.loadFile(path.join(fixturesPath, 'pages', 'window-open.html'));
w.webContents.once('new-window', (event, url, frameName, disposition, options) => {
expect(options.show).to.be.false();
expect((options as any).foo).to.deep.equal({
bar: undefined,
baz: {
hello: {
world: true
}
},
baz2: {
hello: {
world: true
}
const [,,,, options] = await emittedOnce(w.webContents, 'new-window');
expect(options.show).to.be.false();
expect((options as any).foo).to.deep.equal({
bar: undefined,
baz: {
hello: {
world: true
}
});
done();
},
baz2: {
hello: {
world: true
}
}
});
});
@ -959,44 +961,39 @@ describe('chromium features', () => {
contents = null as any;
});
it('cannot access localStorage', (done) => {
ipcMain.once('local-storage-response', (event, error) => {
expect(error).to.equal('Failed to read the \'localStorage\' property from \'Window\': Access is denied for this document.');
done();
});
it('cannot access localStorage', async () => {
const response = emittedOnce(ipcMain, 'local-storage-response');
contents.loadURL(protocolName + '://host/localStorage');
const [, error] = await response;
expect(error).to.equal('Failed to read the \'localStorage\' property from \'Window\': Access is denied for this document.');
});
it('cannot access sessionStorage', (done) => {
ipcMain.once('session-storage-response', (event, error) => {
expect(error).to.equal('Failed to read the \'sessionStorage\' property from \'Window\': Access is denied for this document.');
done();
});
it('cannot access sessionStorage', async () => {
const response = emittedOnce(ipcMain, 'session-storage-response');
contents.loadURL(`${protocolName}://host/sessionStorage`);
const [, error] = await response;
expect(error).to.equal('Failed to read the \'sessionStorage\' property from \'Window\': Access is denied for this document.');
});
it('cannot access WebSQL database', (done) => {
ipcMain.once('web-sql-response', (event, error) => {
expect(error).to.equal('Failed to execute \'openDatabase\' on \'Window\': Access to the WebDatabase API is denied in this context.');
done();
});
it('cannot access WebSQL database', async () => {
const response = emittedOnce(ipcMain, 'web-sql-response');
contents.loadURL(`${protocolName}://host/WebSQL`);
const [, error] = await response;
expect(error).to.equal('Failed to execute \'openDatabase\' on \'Window\': Access to the WebDatabase API is denied in this context.');
});
it('cannot access indexedDB', (done) => {
ipcMain.once('indexed-db-response', (event, error) => {
expect(error).to.equal('Failed to execute \'open\' on \'IDBFactory\': access to the Indexed Database API is denied in this context.');
done();
});
it('cannot access indexedDB', async () => {
const response = emittedOnce(ipcMain, 'indexed-db-response');
contents.loadURL(`${protocolName}://host/indexedDB`);
const [, error] = await response;
expect(error).to.equal('Failed to execute \'open\' on \'IDBFactory\': access to the Indexed Database API is denied in this context.');
});
it('cannot access cookie', (done) => {
ipcMain.once('cookie-response', (event, error) => {
expect(error).to.equal('Failed to set the \'cookie\' property on \'Document\': Access is denied for this document.');
done();
});
it('cannot access cookie', async () => {
const response = emittedOnce(ipcMain, 'cookie-response');
contents.loadURL(`${protocolName}://host/cookie`);
const [, error] = await response;
expect(error).to.equal('Failed to set the \'cookie\' property on \'Document\': Access is denied for this document.');
});
});
@ -1034,7 +1031,7 @@ describe('chromium features', () => {
afterEach(closeAllWindows);
const testLocalStorageAfterXSiteRedirect = (testTitle: string, extraPreferences = {}) => {
it(testTitle, (done) => {
it(testTitle, async () => {
const w = new BrowserWindow({
show: false,
...extraPreferences
@ -1047,11 +1044,8 @@ describe('chromium features', () => {
expect(url).to.equal(`${serverCrossSiteUrl}/redirected`);
redirected = true;
});
w.webContents.on('did-finish-load', () => {
expect(redirected).to.be.true('didnt redirect');
done();
});
w.loadURL(`${serverUrl}/redirect-cross-site`);
await w.loadURL(`${serverUrl}/redirect-cross-site`);
expect(redirected).to.be.true('didnt redirect');
});
};
@ -1363,47 +1357,44 @@ describe('iframe using HTML fullscreen API while window is OS-fullscreened', ()
server.close();
});
it('can fullscreen from out-of-process iframes (OOPIFs)', done => {
ipcMain.once('fullscreenChange', async () => {
const fullscreenWidth = await w.webContents.executeJavaScript(
"document.querySelector('iframe').offsetWidth"
);
expect(fullscreenWidth > 0).to.be.true();
await w.webContents.executeJavaScript(
"document.querySelector('iframe').contentWindow.postMessage('exitFullscreen', '*')"
);
await delay(500);
const width = await w.webContents.executeJavaScript(
"document.querySelector('iframe').offsetWidth"
);
expect(width).to.equal(0);
done();
});
it('can fullscreen from out-of-process iframes (OOPIFs)', async () => {
const fullscreenChange = emittedOnce(ipcMain, 'fullscreenChange');
const html =
'<iframe style="width: 0" frameborder=0 src="http://localhost:8989" allowfullscreen></iframe>';
w.loadURL(`data:text/html,${html}`);
await fullscreenChange;
const fullscreenWidth = await w.webContents.executeJavaScript(
"document.querySelector('iframe').offsetWidth"
);
expect(fullscreenWidth > 0).to.be.true();
await w.webContents.executeJavaScript(
"document.querySelector('iframe').contentWindow.postMessage('exitFullscreen', '*')"
);
await delay(500);
const width = await w.webContents.executeJavaScript(
"document.querySelector('iframe').offsetWidth"
);
expect(width).to.equal(0);
});
it('can fullscreen from in-process iframes', done => {
ipcMain.once('fullscreenChange', async () => {
const fullscreenWidth = await w.webContents.executeJavaScript(
"document.querySelector('iframe').offsetWidth"
);
expect(fullscreenWidth > 0).to.true();
await w.webContents.executeJavaScript('document.exitFullscreen()');
const width = await w.webContents.executeJavaScript(
"document.querySelector('iframe').offsetWidth"
);
expect(width).to.equal(0);
done();
});
it('can fullscreen from in-process iframes', async () => {
const fullscreenChange = emittedOnce(ipcMain, 'fullscreenChange');
w.loadFile(path.join(fixturesPath, 'pages', 'fullscreen-ipif.html'));
await fullscreenChange;
const fullscreenWidth = await w.webContents.executeJavaScript(
"document.querySelector('iframe').offsetWidth"
);
expect(fullscreenWidth > 0).to.true();
await w.webContents.executeJavaScript('document.exitFullscreen()');
const width = await w.webContents.executeJavaScript(
"document.querySelector('iframe').offsetWidth"
);
expect(width).to.equal(0);
});
});