api-shell-spec.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. const { expect } = require('chai');
  2. const fs = require('fs');
  3. const path = require('path');
  4. const os = require('os');
  5. const http = require('http');
  6. const { shell } = require('electron');
  7. describe('shell module', () => {
  8. const fixtures = path.resolve(__dirname, 'fixtures');
  9. const shortcutOptions = {
  10. target: 'C:\\target',
  11. description: 'description',
  12. cwd: 'cwd',
  13. args: 'args',
  14. appUserModelId: 'appUserModelId',
  15. icon: 'icon',
  16. iconIndex: 1,
  17. toastActivatorClsid: '{0E3CFA27-6FEA-410B-824F-A174B6E865E5}'
  18. };
  19. describe('shell.readShortcutLink(shortcutPath)', () => {
  20. beforeEach(function () {
  21. if (process.platform !== 'win32') this.skip();
  22. });
  23. it('throws when failed', () => {
  24. expect(() => {
  25. shell.readShortcutLink('not-exist');
  26. }).to.throw('Failed to read shortcut link');
  27. });
  28. it('reads all properties of a shortcut', () => {
  29. const shortcut = shell.readShortcutLink(path.join(fixtures, 'assets', 'shortcut.lnk'));
  30. expect(shortcut).to.deep.equal(shortcutOptions);
  31. });
  32. });
  33. describe('shell.writeShortcutLink(shortcutPath[, operation], options)', () => {
  34. beforeEach(function () {
  35. if (process.platform !== 'win32') this.skip();
  36. });
  37. const tmpShortcut = path.join(os.tmpdir(), `${Date.now()}.lnk`);
  38. afterEach(() => {
  39. fs.unlinkSync(tmpShortcut);
  40. });
  41. it('writes the shortcut', () => {
  42. expect(shell.writeShortcutLink(tmpShortcut, { target: 'C:\\' })).to.be.true();
  43. expect(fs.existsSync(tmpShortcut)).to.be.true();
  44. });
  45. it('correctly sets the fields', () => {
  46. expect(shell.writeShortcutLink(tmpShortcut, shortcutOptions)).to.be.true();
  47. expect(shell.readShortcutLink(tmpShortcut)).to.deep.equal(shortcutOptions);
  48. });
  49. it('updates the shortcut', () => {
  50. expect(shell.writeShortcutLink(tmpShortcut, 'update', shortcutOptions)).to.be.false();
  51. expect(shell.writeShortcutLink(tmpShortcut, 'create', shortcutOptions)).to.be.true();
  52. expect(shell.readShortcutLink(tmpShortcut)).to.deep.equal(shortcutOptions);
  53. const change = { target: 'D:\\' };
  54. expect(shell.writeShortcutLink(tmpShortcut, 'update', change)).to.be.true();
  55. expect(shell.readShortcutLink(tmpShortcut)).to.deep.equal(Object.assign(shortcutOptions, change));
  56. });
  57. it('replaces the shortcut', () => {
  58. expect(shell.writeShortcutLink(tmpShortcut, 'replace', shortcutOptions)).to.be.false();
  59. expect(shell.writeShortcutLink(tmpShortcut, 'create', shortcutOptions)).to.be.true();
  60. expect(shell.readShortcutLink(tmpShortcut)).to.deep.equal(shortcutOptions);
  61. const change = {
  62. target: 'D:\\',
  63. description: 'description2',
  64. cwd: 'cwd2',
  65. args: 'args2',
  66. appUserModelId: 'appUserModelId2',
  67. icon: 'icon2',
  68. iconIndex: 2,
  69. toastActivatorClsid: '{C51A3996-CAD9-4934-848B-16285D4A1496}'
  70. };
  71. expect(shell.writeShortcutLink(tmpShortcut, 'replace', change)).to.be.true();
  72. expect(shell.readShortcutLink(tmpShortcut)).to.deep.equal(change);
  73. });
  74. });
  75. });