asar-spec.ts 3.4 KB

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