electron/spec/chromium-spec.js

323 lines
12 KiB
JavaScript
Raw Normal View History

2020-03-20 20:28:31 +00:00
const { expect } = require('chai');
const fs = require('fs');
const http = require('http');
const path = require('path');
const url = require('url');
const ChildProcess = require('child_process');
const { ipcRenderer } = require('electron');
const { emittedOnce, waitForEvent } = require('./events-helpers');
const { ifit, ifdescribe, delay } = require('./spec-helpers');
const features = process._linkedBinding('electron_common_features');
2017-11-23 22:22:43 +00:00
/* Most of the APIs here don't use standard callbacks */
/* eslint-disable standard/no-callback-literal */
2017-11-13 20:13:19 +00:00
describe('chromium feature', () => {
2020-03-20 20:28:31 +00:00
const fixtures = path.resolve(__dirname, 'fixtures');
2016-03-25 20:03:49 +00:00
2017-11-13 20:13:19 +00:00
describe('window.open', () => {
it('inherit options of parent window', async () => {
const message = waitForEvent(window, 'message');
const b = window.open(`file://${fixtures}/pages/window-open-size.html`, '', 'show=no');
const event = await message;
b.close();
const width = outerWidth;
const height = outerHeight;
expect(event.data).to.equal(`size: ${width} ${height}`);
2020-03-20 20:28:31 +00:00
});
2016-03-25 20:03:49 +00:00
// FIXME(zcbenz): This test is making the spec runner hang on exit on Windows.
ifit(process.platform !== 'win32')('disables node integration when it is disabled on the parent window', async () => {
2017-11-13 20:13:19 +00:00
const windowUrl = require('url').format({
2016-03-24 00:40:25 +00:00
pathname: `${fixtures}/pages/window-opener-no-node-integration.html`,
protocol: 'file',
query: {
2016-03-24 00:40:25 +00:00
p: `${fixtures}/pages/window-opener-node.html`
},
slashes: true
2020-03-20 20:28:31 +00:00
});
const message = waitForEvent(window, 'message');
const b = window.open(windowUrl, '', 'nodeIntegration=no,contextIsolation=no,show=no');
const event = await message;
b.close();
expect(event.data.isProcessGlobalUndefined).to.be.true();
2020-03-20 20:28:31 +00:00
});
it('disables the <webview> tag when it is disabled on the parent window', async () => {
2017-11-13 20:13:19 +00:00
const windowUrl = require('url').format({
pathname: `${fixtures}/pages/window-opener-no-webview-tag.html`,
protocol: 'file',
query: {
p: `${fixtures}/pages/window-opener-webview.html`
},
slashes: true
2020-03-20 20:28:31 +00:00
});
const message = waitForEvent(window, 'message');
const b = window.open(windowUrl, '', 'webviewTag=no,contextIsolation=no,nodeIntegration=yes,show=no');
const event = await message;
b.close();
expect(event.data.isWebViewGlobalUndefined).to.be.true();
2020-03-20 20:28:31 +00:00
});
it('does not override child options', async () => {
2017-11-13 20:13:19 +00:00
const size = {
2016-01-12 02:40:23 +00:00
width: 350,
height: 450
2020-03-20 20:28:31 +00:00
};
const message = waitForEvent(window, 'message');
const b = window.open(`file://${fixtures}/pages/window-open-size.html`, '', 'show=no,width=' + size.width + ',height=' + size.height);
const event = await message;
b.close();
expect(event.data).to.equal(`size: ${size.width} ${size.height}`);
2020-03-20 20:28:31 +00:00
});
2016-03-25 20:03:49 +00:00
2017-11-13 20:13:19 +00:00
it('throws an exception when the arguments cannot be converted to strings', () => {
expect(() => {
2020-03-20 20:28:31 +00:00
window.open('', { toString: null });
}).to.throw('Cannot convert object to primitive value');
expect(() => {
2020-03-20 20:28:31 +00:00
window.open('', '', { toString: 3 });
}).to.throw('Cannot convert object to primitive value');
});
2017-11-13 20:13:19 +00:00
it('does not throw an exception when the features include webPreferences', () => {
2020-03-20 20:28:31 +00:00
let b = null;
expect(() => {
2020-03-20 20:28:31 +00:00
b = window.open('', '', 'webPreferences=');
}).to.not.throw();
b.close();
});
});
2017-11-13 20:13:19 +00:00
describe('window.opener', () => {
it('is not null for window opened by window.open', async () => {
const message = waitForEvent(window, 'message');
const b = window.open(`file://${fixtures}/pages/window-opener.html`, '', 'show=no');
const event = await message;
b.close();
expect(event.data).to.equal('object');
2020-03-20 20:28:31 +00:00
});
});
2016-03-25 20:03:49 +00:00
2017-11-13 20:13:19 +00:00
describe('window.opener.postMessage', () => {
it('sets source and origin correctly', async () => {
const message = waitForEvent(window, 'message');
const b = window.open(`file://${fixtures}/pages/window-opener-postMessage.html`, '', 'show=no');
const event = await message;
try {
expect(event.source).to.deep.equal(b);
2020-03-20 20:28:31 +00:00
expect(event.origin).to.equal('file://');
} finally {
b.close();
}
2020-03-20 20:28:31 +00:00
});
it('supports windows opened from a <webview>', async () => {
2020-03-20 20:28:31 +00:00
const webview = new WebView();
const consoleMessage = waitForEvent(webview, 'console-message');
2020-03-20 20:28:31 +00:00
webview.allowpopups = true;
webview.setAttribute('webpreferences', 'contextIsolation=no');
webview.src = url.format({
pathname: `${fixtures}/pages/webview-opener-postMessage.html`,
protocol: 'file',
query: {
p: `${fixtures}/pages/window-opener-postMessage.html`
},
slashes: true
2020-03-20 20:28:31 +00:00
});
document.body.appendChild(webview);
const event = await consoleMessage;
webview.remove();
expect(event.message).to.equal('message');
2020-03-20 20:28:31 +00:00
});
2017-11-13 20:13:19 +00:00
describe('targetOrigin argument', () => {
2020-03-20 20:28:31 +00:00
let serverURL;
let server;
2017-11-13 20:13:19 +00:00
beforeEach((done) => {
server = http.createServer((req, res) => {
2020-03-20 20:28:31 +00:00
res.writeHead(200);
const filePath = path.join(fixtures, 'pages', 'window-opener-targetOrigin.html');
res.end(fs.readFileSync(filePath, 'utf8'));
});
2017-11-13 20:13:19 +00:00
server.listen(0, '127.0.0.1', () => {
2020-03-20 20:28:31 +00:00
serverURL = `http://127.0.0.1:${server.address().port}`;
done();
});
});
2017-11-13 20:13:19 +00:00
afterEach(() => {
2020-03-20 20:28:31 +00:00
server.close();
});
it('delivers messages that match the origin', async () => {
const message = waitForEvent(window, 'message');
const b = window.open(serverURL, '', 'show=no,contextIsolation=no,nodeIntegration=yes');
const event = await message;
b.close();
expect(event.data).to.equal('deliver');
2020-03-20 20:28:31 +00:00
});
});
});
2016-03-25 20:03:49 +00:00
2017-11-13 20:13:19 +00:00
describe('storage', () => {
describe('DOM storage quota increase', () => {
2018-11-12 17:19:01 +00:00
['localStorage', 'sessionStorage'].forEach((storageName) => {
2020-03-20 20:28:31 +00:00
const storage = window[storageName];
it(`allows saving at least 40MiB in ${storageName}`, async () => {
// Although JavaScript strings use UTF-16, the underlying
// storage provider may encode strings differently, muddling the
// translation between character and byte counts. However,
// a string of 40 * 2^20 characters will require at least 40MiB
// and presumably no more than 80MiB, a size guaranteed to
// to exceed the original 10MiB quota yet stay within the
// new 100MiB quota.
// Note that both the key name and value affect the total size.
2020-03-20 20:28:31 +00:00
const testKeyName = '_electronDOMStorageQuotaIncreasedTest';
const length = 40 * Math.pow(2, 20) - testKeyName.length;
storage.setItem(testKeyName, 'X'.repeat(length));
// Wait at least one turn of the event loop to help avoid false positives
// Although not entirely necessary, the previous version of this test case
// failed to detect a real problem (perhaps related to DOM storage data caching)
// wherein calling `getItem` immediately after `setItem` would appear to work
// but then later (e.g. next tick) it would not.
await delay(1);
try {
2020-03-20 20:28:31 +00:00
expect(storage.getItem(testKeyName)).to.have.lengthOf(length);
} finally {
2020-03-20 20:28:31 +00:00
storage.removeItem(testKeyName);
}
2020-03-20 20:28:31 +00:00
});
it(`throws when attempting to use more than 128MiB in ${storageName}`, () => {
expect(() => {
2020-03-20 20:28:31 +00:00
const testKeyName = '_electronDOMStorageQuotaStillEnforcedTest';
const length = 128 * Math.pow(2, 20) - testKeyName.length;
try {
2020-03-20 20:28:31 +00:00
storage.setItem(testKeyName, 'X'.repeat(length));
} finally {
2020-03-20 20:28:31 +00:00
storage.removeItem(testKeyName);
}
2020-03-20 20:28:31 +00:00
}).to.throw();
});
});
});
2018-11-12 17:19:01 +00:00
it('requesting persistent quota works', async () => {
const grantedBytes = await new Promise(resolve => {
navigator.webkitPersistentStorage.requestQuota(1024 * 1024, resolve);
2020-03-20 20:28:31 +00:00
});
expect(grantedBytes).to.equal(1048576);
2020-03-20 20:28:31 +00:00
});
});
2016-03-25 20:03:49 +00:00
2017-11-13 20:13:19 +00:00
describe('Promise', () => {
it('resolves correctly in Node.js calls', (done) => {
class XElement extends HTMLElement {}
2020-03-20 20:28:31 +00:00
customElements.define('x-element', XElement);
2017-11-13 20:13:19 +00:00
setImmediate(() => {
2020-03-20 20:28:31 +00:00
let called = false;
2017-11-13 20:13:19 +00:00
Promise.resolve().then(() => {
2020-03-20 20:28:31 +00:00
done(called ? undefined : new Error('wrong sequence'));
});
document.createElement('x-element');
called = true;
});
});
2016-03-25 20:03:49 +00:00
2017-11-13 20:13:19 +00:00
it('resolves correctly in Electron calls', (done) => {
class YElement extends HTMLElement {}
2020-03-20 20:28:31 +00:00
customElements.define('y-element', YElement);
ipcRenderer.invoke('ping').then(() => {
2020-03-20 20:28:31 +00:00
let called = false;
2017-11-13 20:13:19 +00:00
Promise.resolve().then(() => {
2020-03-20 20:28:31 +00:00
done(called ? undefined : new Error('wrong sequence'));
});
document.createElement('y-element');
called = true;
});
});
});
2016-08-22 10:26:07 +00:00
2017-11-13 20:13:19 +00:00
describe('window.alert(message, title)', () => {
it('throws an exception when the arguments cannot be converted to strings', () => {
expect(() => {
2020-03-20 20:28:31 +00:00
window.alert({ toString: null });
}).to.throw('Cannot convert object to primitive value');
});
});
2017-11-13 20:13:19 +00:00
describe('window.confirm(message, title)', () => {
it('throws an exception when the arguments cannot be converted to strings', () => {
expect(() => {
2020-03-20 20:28:31 +00:00
window.confirm({ toString: null }, 'title');
}).to.throw('Cannot convert object to primitive value');
});
});
2017-11-13 20:13:19 +00:00
describe('window.history', () => {
describe('window.history.go(offset)', () => {
it('throws an exception when the argument cannot be converted to a string', () => {
expect(() => {
2020-03-20 20:28:31 +00:00
window.history.go({ toString: null });
}).to.throw('Cannot convert object to primitive value');
});
});
});
// TODO(nornagon): this is broken on CI, it triggers:
// [FATAL:speech_synthesis.mojom-shared.h(237)] The outgoing message will
// trigger VALIDATION_ERROR_UNEXPECTED_NULL_POINTER at the receiving side
// (null text in SpeechSynthesisUtterance struct).
describe.skip('SpeechSynthesis', () => {
before(function () {
if (!features.isTtsEnabled()) {
2020-03-20 20:28:31 +00:00
this.skip();
}
2020-03-20 20:28:31 +00:00
});
it('should emit lifecycle events', async () => {
const sentence = `long sentence which will take at least a few seconds to
2020-03-20 20:28:31 +00:00
utter so that it's possible to pause and resume before the end`;
const utter = new SpeechSynthesisUtterance(sentence);
// Create a dummy utterance so that speech synthesis state
// is initialized for later calls.
2020-03-20 20:28:31 +00:00
speechSynthesis.speak(new SpeechSynthesisUtterance());
speechSynthesis.cancel();
speechSynthesis.speak(utter);
// paused state after speak()
2020-03-20 20:28:31 +00:00
expect(speechSynthesis.paused).to.be.false();
await new Promise((resolve) => { utter.onstart = resolve; });
// paused state after start event
2020-03-20 20:28:31 +00:00
expect(speechSynthesis.paused).to.be.false();
2020-03-20 20:28:31 +00:00
speechSynthesis.pause();
// paused state changes async, right before the pause event
2020-03-20 20:28:31 +00:00
expect(speechSynthesis.paused).to.be.false();
await new Promise((resolve) => { utter.onpause = resolve; });
expect(speechSynthesis.paused).to.be.true();
2020-03-20 20:28:31 +00:00
speechSynthesis.resume();
await new Promise((resolve) => { utter.onresume = resolve; });
// paused state after resume event
2020-03-20 20:28:31 +00:00
expect(speechSynthesis.paused).to.be.false();
2020-03-20 20:28:31 +00:00
await new Promise((resolve) => { utter.onend = resolve; });
});
});
});
describe('console functions', () => {
it('should exist', () => {
2020-03-20 20:28:31 +00:00
expect(console.log, 'log').to.be.a('function');
expect(console.error, 'error').to.be.a('function');
expect(console.warn, 'warn').to.be.a('function');
expect(console.info, 'info').to.be.a('function');
expect(console.debug, 'debug').to.be.a('function');
expect(console.trace, 'trace').to.be.a('function');
expect(console.time, 'time').to.be.a('function');
expect(console.timeEnd, 'timeEnd').to.be.a('function');
});
});