api-shell-spec.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. const chai = require('chai');
  2. const dirtyChai = require('dirty-chai');
  3. const fs = require('fs');
  4. const path = require('path');
  5. const os = require('os');
  6. const http = require('http');
  7. const { shell, remote } = require('electron');
  8. const { BrowserWindow } = remote;
  9. const { closeWindow } = require('./window-helpers');
  10. const { emittedOnce } = require('./events-helpers');
  11. const { expect } = chai;
  12. chai.use(dirtyChai);
  13. describe('shell module', () => {
  14. const fixtures = path.resolve(__dirname, 'fixtures');
  15. const shortcutOptions = {
  16. target: 'C:\\target',
  17. description: 'description',
  18. cwd: 'cwd',
  19. args: 'args',
  20. appUserModelId: 'appUserModelId',
  21. icon: 'icon',
  22. iconIndex: 1
  23. };
  24. describe('shell.openExternal()', () => {
  25. let envVars = {};
  26. let w;
  27. beforeEach(function () {
  28. envVars = {
  29. display: process.env.DISPLAY,
  30. de: process.env.DE,
  31. browser: process.env.BROWSER
  32. };
  33. });
  34. afterEach(async () => {
  35. await closeWindow(w);
  36. w = null;
  37. // reset env vars to prevent side effects
  38. if (process.platform === 'linux') {
  39. process.env.DE = envVars.de;
  40. process.env.BROWSER = envVars.browser;
  41. process.env.DISPLAY = envVars.display;
  42. }
  43. });
  44. it('opens an external link', async () => {
  45. let url = 'http://127.0.0.1';
  46. let requestReceived;
  47. if (process.platform === 'linux') {
  48. process.env.BROWSER = '/bin/true';
  49. process.env.DE = 'generic';
  50. process.env.DISPLAY = '';
  51. requestReceived = Promise.resolve();
  52. } else if (process.platform === 'darwin') {
  53. // On the Mac CI machines, Safari tries to ask for a password to the
  54. // code signing keychain we set up to test code signing (see
  55. // https://github.com/electron/electron/pull/19969#issuecomment-526278890),
  56. // so use a blur event as a crude proxy.
  57. w = new BrowserWindow({ show: true });
  58. requestReceived = emittedOnce(w, 'blur');
  59. } else {
  60. const server = http.createServer((req, res) => {
  61. res.end();
  62. });
  63. await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
  64. requestReceived = new Promise(resolve => server.on('connection', () => resolve()));
  65. url = `http://127.0.0.1:${server.address().port}`;
  66. }
  67. await Promise.all([
  68. shell.openExternal(url),
  69. requestReceived
  70. ]);
  71. });
  72. });
  73. describe('shell.readShortcutLink(shortcutPath)', () => {
  74. beforeEach(function () {
  75. if (process.platform !== 'win32') this.skip();
  76. });
  77. it('throws when failed', () => {
  78. expect(() => {
  79. shell.readShortcutLink('not-exist');
  80. }).to.throw('Failed to read shortcut link');
  81. });
  82. it('reads all properties of a shortcut', () => {
  83. const shortcut = shell.readShortcutLink(path.join(fixtures, 'assets', 'shortcut.lnk'));
  84. expect(shortcut).to.deep.equal(shortcutOptions);
  85. });
  86. });
  87. describe('shell.writeShortcutLink(shortcutPath[, operation], options)', () => {
  88. beforeEach(function () {
  89. if (process.platform !== 'win32') this.skip();
  90. });
  91. const tmpShortcut = path.join(os.tmpdir(), `${Date.now()}.lnk`);
  92. afterEach(() => {
  93. fs.unlinkSync(tmpShortcut);
  94. });
  95. it('writes the shortcut', () => {
  96. expect(shell.writeShortcutLink(tmpShortcut, { target: 'C:\\' })).to.be.true();
  97. expect(fs.existsSync(tmpShortcut)).to.be.true();
  98. });
  99. it('correctly sets the fields', () => {
  100. expect(shell.writeShortcutLink(tmpShortcut, shortcutOptions)).to.be.true();
  101. expect(shell.readShortcutLink(tmpShortcut)).to.deep.equal(shortcutOptions);
  102. });
  103. it('updates the shortcut', () => {
  104. expect(shell.writeShortcutLink(tmpShortcut, 'update', shortcutOptions)).to.be.false();
  105. expect(shell.writeShortcutLink(tmpShortcut, 'create', shortcutOptions)).to.be.true();
  106. expect(shell.readShortcutLink(tmpShortcut)).to.deep.equal(shortcutOptions);
  107. const change = { target: 'D:\\' };
  108. expect(shell.writeShortcutLink(tmpShortcut, 'update', change)).to.be.true();
  109. expect(shell.readShortcutLink(tmpShortcut)).to.deep.equal(Object.assign(shortcutOptions, change));
  110. });
  111. it('replaces the shortcut', () => {
  112. expect(shell.writeShortcutLink(tmpShortcut, 'replace', shortcutOptions)).to.be.false();
  113. expect(shell.writeShortcutLink(tmpShortcut, 'create', shortcutOptions)).to.be.true();
  114. expect(shell.readShortcutLink(tmpShortcut)).to.deep.equal(shortcutOptions);
  115. const change = {
  116. target: 'D:\\',
  117. description: 'description2',
  118. cwd: 'cwd2',
  119. args: 'args2',
  120. appUserModelId: 'appUserModelId2',
  121. icon: 'icon2',
  122. iconIndex: 2
  123. };
  124. expect(shell.writeShortcutLink(tmpShortcut, 'replace', change)).to.be.true();
  125. expect(shell.readShortcutLink(tmpShortcut)).to.deep.equal(change);
  126. });
  127. });
  128. });