electron/spec/chromium-spec.js

391 lines
11 KiB
JavaScript
Raw Normal View History

2016-01-19 22:49:40 +00:00
const assert = require('assert');
const http = require('http');
const path = require('path');
const ws = require('ws');
const remote = require('electron').remote;
2016-01-12 02:40:23 +00:00
2016-01-19 22:49:40 +00:00
const BrowserWindow = remote.require('electron').BrowserWindow;
const session = remote.require('electron').session;
2016-01-12 02:40:23 +00:00
describe('chromium feature', function() {
var fixtures = path.resolve(__dirname, 'fixtures');
var listener = null;
2016-01-12 02:40:23 +00:00
afterEach(function() {
if (listener != null) {
window.removeEventListener('message', listener);
}
2016-02-17 01:39:11 +00:00
listener = null;
2016-01-12 02:40:23 +00:00
});
2016-01-12 02:40:23 +00:00
xdescribe('heap snapshot', function() {
2016-02-17 01:39:11 +00:00
it('does not crash', function() {
process.atomBinding('v8_util').takeHeapSnapshot();
2016-01-12 02:40:23 +00:00
});
});
2016-01-12 02:40:23 +00:00
describe('sending request of http protocol urls', function() {
2016-02-17 01:39:11 +00:00
it('does not crash', function(done) {
2016-01-12 02:40:23 +00:00
this.timeout(5000);
2016-02-17 01:39:11 +00:00
var server = http.createServer(function(req, res) {
2016-01-12 02:40:23 +00:00
res.end();
server.close();
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
});
2016-02-17 01:39:11 +00:00
server.listen(0, '127.0.0.1', function() {
var port = server.address().port;
$.get("http://127.0.0.1:" + port);
2016-01-12 02:40:23 +00:00
});
});
});
2016-01-12 02:40:23 +00:00
describe('document.hidden', function() {
var url = "file://" + fixtures + "/pages/document-hidden.html";
var w = null;
2016-01-12 02:40:23 +00:00
afterEach(function() {
2016-02-17 01:39:11 +00:00
w != null ? w.destroy() : void 0;
2016-01-12 02:40:23 +00:00
});
2016-01-12 02:40:23 +00:00
it('is set correctly when window is not shown', function(done) {
w = new BrowserWindow({
show: false
});
w.webContents.on('ipc-message', function(event, args) {
assert.deepEqual(args, ['hidden', true]);
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
});
2016-02-17 01:39:11 +00:00
w.loadURL(url);
2016-01-12 02:40:23 +00:00
});
2016-02-17 01:39:11 +00:00
it('is set correctly when window is inactive', function(done) {
2016-01-12 02:40:23 +00:00
w = new BrowserWindow({
show: false
});
w.webContents.on('ipc-message', function(event, args) {
assert.deepEqual(args, ['hidden', false]);
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
});
w.showInactive();
2016-02-17 01:39:11 +00:00
w.loadURL(url);
2016-01-12 02:40:23 +00:00
});
});
2016-01-12 02:40:23 +00:00
xdescribe('navigator.webkitGetUserMedia', function() {
2016-02-17 01:39:11 +00:00
it('calls its callbacks', function(done) {
2016-01-12 02:40:23 +00:00
this.timeout(5000);
2016-02-17 01:39:11 +00:00
navigator.webkitGetUserMedia({
2016-01-12 02:40:23 +00:00
audio: true,
video: false
}, function() {
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
}, function() {
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
});
});
});
2016-01-12 02:40:23 +00:00
describe('navigator.language', function() {
2016-02-17 01:39:11 +00:00
it('should not be empty', function() {
assert.notEqual(navigator.language, '');
2016-01-12 02:40:23 +00:00
});
});
2016-01-12 02:40:23 +00:00
describe('navigator.serviceWorker', function() {
var url = "file://" + fixtures + "/pages/service-worker/index.html";
var w = null;
2016-01-12 02:40:23 +00:00
afterEach(function() {
2016-02-17 01:39:11 +00:00
w != null ? w.destroy() : void 0;
2016-01-12 02:40:23 +00:00
});
2016-02-17 01:39:11 +00:00
it('should register for file scheme', function(done) {
2016-01-12 02:40:23 +00:00
w = new BrowserWindow({
show: false
});
w.webContents.on('ipc-message', function(event, args) {
if (args[0] === 'reload') {
2016-02-17 01:39:11 +00:00
w.webContents.reload();
2016-01-12 02:40:23 +00:00
} else if (args[0] === 'error') {
2016-02-17 01:39:11 +00:00
done('unexpected error : ' + args[1]);
2016-01-12 02:40:23 +00:00
} else if (args[0] === 'response') {
assert.equal(args[1], 'Hello from serviceWorker!');
2016-02-17 01:39:11 +00:00
session.defaultSession.clearStorageData({
2016-01-12 02:40:23 +00:00
storages: ['serviceworkers']
}, function() {
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
});
}
});
2016-02-17 01:39:11 +00:00
sw.loadURL(url);
2016-01-12 02:40:23 +00:00
});
});
2016-01-12 02:40:23 +00:00
describe('window.open', function() {
this.timeout(20000);
2016-01-12 02:40:23 +00:00
it('returns a BrowserWindowProxy object', function() {
2016-02-17 01:39:11 +00:00
var b = window.open('about:blank', '', 'show=no');
2016-01-12 02:40:23 +00:00
assert.equal(b.closed, false);
assert.equal(b.constructor.name, 'BrowserWindowProxy');
2016-02-17 01:39:11 +00:00
b.close();
2016-01-12 02:40:23 +00:00
});
2016-01-12 02:40:23 +00:00
it('accepts "node-integration" as feature', function(done) {
var b;
listener = function(event) {
assert.equal(event.data, 'undefined');
b.close();
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
};
window.addEventListener('message', listener);
2016-02-17 01:39:11 +00:00
b = window.open("file://" + fixtures + "/pages/window-opener-node.html", '', 'nodeIntegration=no,show=no');
2016-01-12 02:40:23 +00:00
});
2016-01-12 02:40:23 +00:00
it('inherit options of parent window', function(done) {
var b;
listener = function(event) {
var height, ref1, width;
ref1 = remote.getCurrentWindow().getSize(), width = ref1[0], height = ref1[1];
assert.equal(event.data, "size: " + width + " " + height);
b.close();
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
};
window.addEventListener('message', listener);
2016-02-17 01:39:11 +00:00
b = window.open("file://" + fixtures + "/pages/window-open-size.html", '', 'show=no');
2016-01-12 02:40:23 +00:00
});
2016-02-17 01:39:11 +00:00
it('does not override child options', function(done) {
2016-01-12 02:40:23 +00:00
var b, size;
size = {
width: 350,
height: 450
};
listener = function(event) {
assert.equal(event.data, "size: " + size.width + " " + size.height);
b.close();
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
};
window.addEventListener('message', listener);
2016-02-17 01:39:11 +00:00
b = window.open("file://" + fixtures + "/pages/window-open-size.html", '', "show=no,width=" + size.width + ",height=" + size.height);
2016-01-12 02:40:23 +00:00
});
});
2016-01-12 02:40:23 +00:00
describe('window.opener', function() {
this.timeout(10000);
2016-02-17 01:39:11 +00:00
var url = "file://" + fixtures + "/pages/window-opener.html";
var w = null;
2016-01-12 02:40:23 +00:00
afterEach(function() {
2016-02-17 01:39:11 +00:00
w != null ? w.destroy() : void 0;
2016-01-12 02:40:23 +00:00
});
2016-02-17 01:39:11 +00:00
2016-01-12 02:40:23 +00:00
it('is null for main window', function(done) {
w = new BrowserWindow({
show: false
});
w.webContents.on('ipc-message', function(event, args) {
assert.deepEqual(args, ['opener', null]);
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
});
2016-02-17 01:39:11 +00:00
w.loadURL(url);
2016-01-12 02:40:23 +00:00
});
2016-02-17 01:39:11 +00:00
it('is not null for window opened by window.open', function(done) {
2016-01-12 02:40:23 +00:00
var b;
listener = function(event) {
assert.equal(event.data, 'object');
b.close();
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
};
window.addEventListener('message', listener);
2016-02-17 01:39:11 +00:00
b = window.open(url, '', 'show=no');
2016-01-12 02:40:23 +00:00
});
});
2016-01-12 02:40:23 +00:00
describe('window.postMessage', function() {
2016-02-17 01:39:11 +00:00
it('sets the source and origin correctly', function(done) {
2016-01-12 02:40:23 +00:00
var b, sourceId;
sourceId = remote.getCurrentWindow().id;
listener = function(event) {
var message;
window.removeEventListener('message', listener);
b.close();
message = JSON.parse(event.data);
assert.equal(message.data, 'testing');
assert.equal(message.origin, 'file://');
assert.equal(message.sourceEqualsOpener, true);
assert.equal(message.sourceId, sourceId);
assert.equal(event.origin, 'file://');
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
};
window.addEventListener('message', listener);
b = window.open("file://" + fixtures + "/pages/window-open-postMessage.html", '', 'show=no');
2016-02-17 01:39:11 +00:00
BrowserWindow.fromId(b.guestId).webContents.once('did-finish-load', function() {
b.postMessage('testing', '*');
2016-01-12 02:40:23 +00:00
});
});
});
2016-01-12 02:40:23 +00:00
describe('window.opener.postMessage', function() {
2016-02-17 01:39:11 +00:00
it('sets source and origin correctly', function(done) {
2016-01-12 02:40:23 +00:00
var b;
listener = function(event) {
window.removeEventListener('message', listener);
b.close();
assert.equal(event.source, b);
assert.equal(event.origin, 'file://');
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
};
window.addEventListener('message', listener);
2016-02-17 01:39:11 +00:00
b = window.open("file://" + fixtures + "/pages/window-opener-postMessage.html", '', 'show=no');
2016-01-12 02:40:23 +00:00
});
});
2016-01-12 02:40:23 +00:00
describe('creating a Uint8Array under browser side', function() {
2016-02-17 01:39:11 +00:00
it('does not crash', function() {
2016-01-12 02:40:23 +00:00
var RUint8Array;
RUint8Array = remote.getGlobal('Uint8Array');
2016-02-17 01:39:11 +00:00
new RUint8Array;
2016-01-12 02:40:23 +00:00
});
});
2016-01-12 02:40:23 +00:00
describe('webgl', function() {
2016-02-17 01:39:11 +00:00
it('can be get as context in canvas', function() {
2016-01-12 02:40:23 +00:00
var webgl;
if (process.platform === 'linux') {
return;
}
webgl = document.createElement('canvas').getContext('webgl');
2016-02-17 01:39:11 +00:00
assert.notEqual(webgl, null);
2016-01-12 02:40:23 +00:00
});
});
2016-01-12 02:40:23 +00:00
describe('web workers', function() {
it('Worker can work', function(done) {
var message, worker;
worker = new Worker('../fixtures/workers/worker.js');
message = 'ping';
worker.onmessage = function(event) {
assert.equal(event.data, message);
worker.terminate();
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
};
2016-02-17 01:39:11 +00:00
worker.postMessage(message);
2016-01-12 02:40:23 +00:00
});
2016-02-17 01:39:11 +00:00
it('SharedWorker can work', function(done) {
2016-01-12 02:40:23 +00:00
var message, worker;
worker = new SharedWorker('../fixtures/workers/shared_worker.js');
message = 'ping';
worker.port.onmessage = function(event) {
assert.equal(event.data, message);
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
};
2016-02-17 01:39:11 +00:00
worker.port.postMessage(message);
2016-01-12 02:40:23 +00:00
});
});
2016-01-12 02:40:23 +00:00
describe('iframe', function() {
var iframe = null;
2016-01-12 02:40:23 +00:00
beforeEach(function() {
2016-02-17 01:39:11 +00:00
iframe = document.createElement('iframe');
2016-01-12 02:40:23 +00:00
});
2016-01-12 02:40:23 +00:00
afterEach(function() {
2016-02-17 01:39:11 +00:00
document.body.removeChild(iframe);
2016-01-12 02:40:23 +00:00
});
2016-02-17 01:39:11 +00:00
it('does not have node integration', function(done) {
2016-01-12 02:40:23 +00:00
iframe.src = "file://" + fixtures + "/pages/set-global.html";
document.body.appendChild(iframe);
2016-02-17 01:39:11 +00:00
iframe.onload = function() {
2016-01-12 02:40:23 +00:00
assert.equal(iframe.contentWindow.test, 'undefined undefined undefined');
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
};
});
});
2016-01-12 02:40:23 +00:00
describe('storage', function() {
2016-02-17 01:39:11 +00:00
it('requesting persitent quota works', function(done) {
navigator.webkitPersistentStorage.requestQuota(1024 * 1024, function(grantedBytes) {
2016-01-12 02:40:23 +00:00
assert.equal(grantedBytes, 1048576);
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
});
});
});
2016-01-12 02:40:23 +00:00
describe('websockets', function() {
var wss = null;
var server = null;
var WebSocketServer = ws.Server;
2016-01-12 02:40:23 +00:00
afterEach(function() {
wss.close();
2016-02-17 01:39:11 +00:00
server.close();
2016-01-12 02:40:23 +00:00
});
2016-02-17 01:39:11 +00:00
it('has user agent', function(done) {
2016-01-12 02:40:23 +00:00
server = http.createServer();
2016-02-17 01:39:11 +00:00
server.listen(0, '127.0.0.1', function() {
2016-01-19 22:49:40 +00:00
var port = server.address().port;
2016-01-12 02:40:23 +00:00
wss = new WebSocketServer({
server: server
});
wss.on('error', done);
wss.on('connection', function(ws) {
if (ws.upgradeReq.headers['user-agent']) {
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
} else {
2016-02-17 01:39:11 +00:00
done('user agent is empty');
2016-01-12 02:40:23 +00:00
}
});
2016-01-19 22:49:40 +00:00
new WebSocket("ws://127.0.0.1:" + port);
2016-01-12 02:40:23 +00:00
});
});
});
2016-02-17 01:39:11 +00:00
describe('Promise', function() {
2016-01-12 02:40:23 +00:00
it('resolves correctly in Node.js calls', function(done) {
document.registerElement('x-element', {
prototype: Object.create(HTMLElement.prototype, {
createdCallback: {
value: function() {}
}
})
});
2016-02-17 01:39:11 +00:00
setImmediate(function() {
var called = false;
2016-01-12 02:40:23 +00:00
Promise.resolve().then(function() {
2016-02-17 01:39:11 +00:00
done(called ? void 0 : new Error('wrong sequence'));
2016-01-12 02:40:23 +00:00
});
document.createElement('x-element');
2016-02-17 01:39:11 +00:00
called = true;
2016-01-12 02:40:23 +00:00
});
});
2016-02-17 01:39:11 +00:00
it('resolves correctly in Electron calls', function(done) {
2016-01-12 02:40:23 +00:00
document.registerElement('y-element', {
prototype: Object.create(HTMLElement.prototype, {
createdCallback: {
value: function() {}
}
})
});
2016-02-17 01:39:11 +00:00
remote.getGlobal('setImmediate')(function() {
2016-01-12 02:40:23 +00:00
var called;
called = false;
Promise.resolve().then(function() {
2016-02-17 01:39:11 +00:00
done(called ? void 0 : new Error('wrong sequence'));
2016-01-12 02:40:23 +00:00
});
document.createElement('y-element');
2016-02-17 01:39:11 +00:00
called = true;
2016-01-12 02:40:23 +00:00
});
});
});
});