2019-10-11 16:56:51 +00:00
|
|
|
import { expect } from 'chai';
|
|
|
|
import * as path from 'path';
|
2021-04-27 16:07:05 +00:00
|
|
|
import * as url from 'url';
|
2022-03-23 00:37:55 +00:00
|
|
|
import { Worker } from 'worker_threads';
|
2020-04-07 00:04:09 +00:00
|
|
|
import { BrowserWindow, ipcMain } from 'electron/main';
|
2019-10-11 16:56:51 +00:00
|
|
|
import { closeAllWindows } from './window-helpers';
|
2020-06-30 22:10:36 +00:00
|
|
|
import { emittedOnce } from './events-helpers';
|
2019-10-11 16:56:51 +00:00
|
|
|
|
|
|
|
describe('asar package', () => {
|
|
|
|
const fixtures = path.join(__dirname, '..', 'spec', 'fixtures');
|
|
|
|
const asarDir = path.join(fixtures, 'test.asar');
|
|
|
|
|
|
|
|
afterEach(closeAllWindows);
|
|
|
|
|
|
|
|
describe('asar protocol', () => {
|
2020-06-30 22:10:36 +00:00
|
|
|
it('sets __dirname correctly', async function () {
|
2019-10-11 16:56:51 +00:00
|
|
|
after(function () {
|
|
|
|
ipcMain.removeAllListeners('dirname');
|
|
|
|
});
|
|
|
|
|
|
|
|
const w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
width: 400,
|
|
|
|
height: 400,
|
|
|
|
webPreferences: {
|
2021-03-01 21:52:29 +00:00
|
|
|
nodeIntegration: true,
|
|
|
|
contextIsolation: false
|
2019-10-11 16:56:51 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
const p = path.resolve(asarDir, 'web.asar', 'index.html');
|
2020-06-30 22:10:36 +00:00
|
|
|
const dirnameEvent = emittedOnce(ipcMain, 'dirname');
|
2019-10-11 16:56:51 +00:00
|
|
|
w.loadFile(p);
|
2020-06-30 22:10:36 +00:00
|
|
|
const [, dirname] = await dirnameEvent;
|
|
|
|
expect(dirname).to.equal(path.dirname(p));
|
2019-10-11 16:56:51 +00:00
|
|
|
});
|
|
|
|
|
2020-06-30 22:10:36 +00:00
|
|
|
it('loads script tag in html', async function () {
|
2019-10-11 16:56:51 +00:00
|
|
|
after(function () {
|
|
|
|
ipcMain.removeAllListeners('ping');
|
|
|
|
});
|
|
|
|
|
|
|
|
const w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
width: 400,
|
|
|
|
height: 400,
|
|
|
|
webPreferences: {
|
2021-03-01 21:52:29 +00:00
|
|
|
nodeIntegration: true,
|
|
|
|
contextIsolation: false
|
2019-10-11 16:56:51 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
const p = path.resolve(asarDir, 'script.asar', 'index.html');
|
2020-06-30 22:10:36 +00:00
|
|
|
const ping = emittedOnce(ipcMain, 'ping');
|
2019-10-11 16:56:51 +00:00
|
|
|
w.loadFile(p);
|
2020-06-30 22:10:36 +00:00
|
|
|
const [, message] = await ping;
|
|
|
|
expect(message).to.equal('pong');
|
2019-10-11 16:56:51 +00:00
|
|
|
});
|
|
|
|
|
2020-06-30 22:10:36 +00:00
|
|
|
it('loads video tag in html', async function () {
|
2019-10-11 16:56:51 +00:00
|
|
|
this.timeout(60000);
|
|
|
|
|
|
|
|
after(function () {
|
|
|
|
ipcMain.removeAllListeners('asar-video');
|
|
|
|
});
|
|
|
|
|
|
|
|
const w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
width: 400,
|
|
|
|
height: 400,
|
|
|
|
webPreferences: {
|
2021-03-01 21:52:29 +00:00
|
|
|
nodeIntegration: true,
|
|
|
|
contextIsolation: false
|
2019-10-11 16:56:51 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
const p = path.resolve(asarDir, 'video.asar', 'index.html');
|
|
|
|
w.loadFile(p);
|
2020-06-30 22:10:36 +00:00
|
|
|
const [, message, error] = await emittedOnce(ipcMain, 'asar-video');
|
|
|
|
if (message === 'ended') {
|
|
|
|
expect(error).to.be.null();
|
|
|
|
} else if (message === 'error') {
|
|
|
|
throw new Error(error);
|
|
|
|
}
|
2019-10-11 16:56:51 +00:00
|
|
|
});
|
|
|
|
});
|
2021-04-27 16:07:05 +00:00
|
|
|
|
|
|
|
describe('worker', () => {
|
|
|
|
it('Worker can load asar file', async () => {
|
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
await w.loadFile(path.join(fixtures, 'workers', 'load_worker.html'));
|
|
|
|
|
|
|
|
const workerUrl = url.format({
|
|
|
|
pathname: path.resolve(fixtures, 'workers', 'workers.asar', 'worker.js').replace(/\\/g, '/'),
|
|
|
|
protocol: 'file',
|
|
|
|
slashes: true
|
|
|
|
});
|
|
|
|
const result = await w.webContents.executeJavaScript(`loadWorker('${workerUrl}')`);
|
|
|
|
expect(result).to.equal('success');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('SharedWorker can load asar file', async () => {
|
|
|
|
const w = new BrowserWindow({ show: false });
|
|
|
|
await w.loadFile(path.join(fixtures, 'workers', 'load_shared_worker.html'));
|
|
|
|
|
|
|
|
const workerUrl = url.format({
|
|
|
|
pathname: path.resolve(fixtures, 'workers', 'workers.asar', 'shared_worker.js').replace(/\\/g, '/'),
|
|
|
|
protocol: 'file',
|
|
|
|
slashes: true
|
|
|
|
});
|
|
|
|
const result = await w.webContents.executeJavaScript(`loadSharedWorker('${workerUrl}')`);
|
|
|
|
expect(result).to.equal('success');
|
|
|
|
});
|
|
|
|
});
|
2022-03-23 00:37:55 +00:00
|
|
|
|
|
|
|
describe('worker threads', function () {
|
|
|
|
it('should start worker thread from asar file', function (callback) {
|
|
|
|
const p = path.join(asarDir, 'worker_threads.asar', 'worker.js');
|
|
|
|
const w = new Worker(p);
|
|
|
|
|
|
|
|
w.on('error', (err) => callback(err));
|
|
|
|
w.on('message', (message) => {
|
|
|
|
expect(message).to.equal('ping');
|
|
|
|
w.terminate();
|
|
|
|
|
|
|
|
callback(null);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2019-11-01 20:37:02 +00:00
|
|
|
});
|