asar-spec.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import { expect } from 'chai';
  2. import * as path from 'path';
  3. import * as url from 'url';
  4. import { Worker } from 'worker_threads';
  5. import { BrowserWindow, ipcMain } from 'electron/main';
  6. import { closeAllWindows } from './window-helpers';
  7. import { emittedOnce } from './events-helpers';
  8. describe('asar package', () => {
  9. const fixtures = path.join(__dirname, '..', 'spec', 'fixtures');
  10. const asarDir = path.join(fixtures, 'test.asar');
  11. afterEach(closeAllWindows);
  12. describe('asar protocol', () => {
  13. it('sets __dirname correctly', async function () {
  14. after(function () {
  15. ipcMain.removeAllListeners('dirname');
  16. });
  17. const w = new BrowserWindow({
  18. show: false,
  19. width: 400,
  20. height: 400,
  21. webPreferences: {
  22. nodeIntegration: true,
  23. contextIsolation: false
  24. }
  25. });
  26. const p = path.resolve(asarDir, 'web.asar', 'index.html');
  27. const dirnameEvent = emittedOnce(ipcMain, 'dirname');
  28. w.loadFile(p);
  29. const [, dirname] = await dirnameEvent;
  30. expect(dirname).to.equal(path.dirname(p));
  31. });
  32. it('loads script tag in html', async function () {
  33. after(function () {
  34. ipcMain.removeAllListeners('ping');
  35. });
  36. const w = new BrowserWindow({
  37. show: false,
  38. width: 400,
  39. height: 400,
  40. webPreferences: {
  41. nodeIntegration: true,
  42. contextIsolation: false
  43. }
  44. });
  45. const p = path.resolve(asarDir, 'script.asar', 'index.html');
  46. const ping = emittedOnce(ipcMain, 'ping');
  47. w.loadFile(p);
  48. const [, message] = await ping;
  49. expect(message).to.equal('pong');
  50. });
  51. it('loads video tag in html', async function () {
  52. this.timeout(60000);
  53. after(function () {
  54. ipcMain.removeAllListeners('asar-video');
  55. });
  56. const w = new BrowserWindow({
  57. show: false,
  58. width: 400,
  59. height: 400,
  60. webPreferences: {
  61. nodeIntegration: true,
  62. contextIsolation: false
  63. }
  64. });
  65. const p = path.resolve(asarDir, 'video.asar', 'index.html');
  66. w.loadFile(p);
  67. const [, message, error] = await emittedOnce(ipcMain, 'asar-video');
  68. if (message === 'ended') {
  69. expect(error).to.be.null();
  70. } else if (message === 'error') {
  71. throw new Error(error);
  72. }
  73. });
  74. });
  75. describe('worker', () => {
  76. it('Worker can load asar file', async () => {
  77. const w = new BrowserWindow({ show: false });
  78. await w.loadFile(path.join(fixtures, 'workers', 'load_worker.html'));
  79. const workerUrl = url.format({
  80. pathname: path.resolve(fixtures, 'workers', 'workers.asar', 'worker.js').replace(/\\/g, '/'),
  81. protocol: 'file',
  82. slashes: true
  83. });
  84. const result = await w.webContents.executeJavaScript(`loadWorker('${workerUrl}')`);
  85. expect(result).to.equal('success');
  86. });
  87. it('SharedWorker can load asar file', async () => {
  88. const w = new BrowserWindow({ show: false });
  89. await w.loadFile(path.join(fixtures, 'workers', 'load_shared_worker.html'));
  90. const workerUrl = url.format({
  91. pathname: path.resolve(fixtures, 'workers', 'workers.asar', 'shared_worker.js').replace(/\\/g, '/'),
  92. protocol: 'file',
  93. slashes: true
  94. });
  95. const result = await w.webContents.executeJavaScript(`loadSharedWorker('${workerUrl}')`);
  96. expect(result).to.equal('success');
  97. });
  98. });
  99. describe('worker threads', function () {
  100. it('should start worker thread from asar file', function (callback) {
  101. const p = path.join(asarDir, 'worker_threads.asar', 'worker.js');
  102. const w = new Worker(p);
  103. w.on('error', (err) => callback(err));
  104. w.on('message', (message) => {
  105. expect(message).to.equal('ping');
  106. w.terminate();
  107. callback(null);
  108. });
  109. });
  110. });
  111. });