api-shell-spec.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import { BrowserWindow, app } from 'electron/main';
  2. import { shell } from 'electron/common';
  3. import { closeAllWindows } from './lib/window-helpers';
  4. import { ifdescribe, ifit, listen } from './lib/spec-helpers';
  5. import * as http from 'node:http';
  6. import * as fs from 'fs-extra';
  7. import * as os from 'node:os';
  8. import * as path from 'node:path';
  9. import { expect } from 'chai';
  10. import { once } from 'node:events';
  11. describe('shell module', () => {
  12. describe('shell.openExternal()', () => {
  13. let envVars: Record<string, string | undefined> = {};
  14. beforeEach(function () {
  15. envVars = {
  16. display: process.env.DISPLAY,
  17. de: process.env.DE,
  18. browser: process.env.BROWSER
  19. };
  20. });
  21. afterEach(async () => {
  22. // reset env vars to prevent side effects
  23. if (process.platform === 'linux') {
  24. process.env.DE = envVars.de;
  25. process.env.BROWSER = envVars.browser;
  26. process.env.DISPLAY = envVars.display;
  27. }
  28. });
  29. afterEach(closeAllWindows);
  30. it('opens an external link', async () => {
  31. let url = 'http://127.0.0.1';
  32. let requestReceived: Promise<any>;
  33. if (process.platform === 'linux') {
  34. process.env.BROWSER = '/bin/true';
  35. process.env.DE = 'generic';
  36. process.env.DISPLAY = '';
  37. requestReceived = Promise.resolve();
  38. } else if (process.platform === 'darwin') {
  39. // On the Mac CI machines, Safari tries to ask for a password to the
  40. // code signing keychain we set up to test code signing (see
  41. // https://github.com/electron/electron/pull/19969#issuecomment-526278890),
  42. // so use a blur event as a crude proxy.
  43. const w = new BrowserWindow({ show: true });
  44. requestReceived = once(w, 'blur');
  45. } else {
  46. const server = http.createServer((req, res) => {
  47. res.end();
  48. });
  49. url = (await listen(server)).url;
  50. requestReceived = new Promise<void>(resolve => server.on('connection', () => resolve()));
  51. }
  52. await Promise.all<void>([
  53. shell.openExternal(url),
  54. requestReceived
  55. ]);
  56. });
  57. });
  58. describe('shell.trashItem()', () => {
  59. afterEach(closeAllWindows);
  60. it('moves an item to the trash', async () => {
  61. const dir = await fs.mkdtemp(path.resolve(app.getPath('temp'), 'electron-shell-spec-'));
  62. const filename = path.join(dir, 'temp-to-be-deleted');
  63. await fs.writeFile(filename, 'dummy-contents');
  64. await shell.trashItem(filename);
  65. expect(fs.existsSync(filename)).to.be.false();
  66. });
  67. it('throws when called with a nonexistent path', async () => {
  68. const filename = path.join(app.getPath('temp'), 'does-not-exist');
  69. await expect(shell.trashItem(filename)).to.eventually.be.rejected();
  70. });
  71. ifit(!(process.platform === 'win32' && process.arch === 'ia32'))('works in the renderer process', async () => {
  72. const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
  73. w.loadURL('about:blank');
  74. await expect(w.webContents.executeJavaScript('require(\'electron\').shell.trashItem(\'does-not-exist\')')).to.be.rejectedWith(/does-not-exist|Failed to move item|Failed to create FileOperation/);
  75. });
  76. });
  77. const shortcutOptions = {
  78. target: 'C:\\target',
  79. description: 'description',
  80. cwd: 'cwd',
  81. args: 'args',
  82. appUserModelId: 'appUserModelId',
  83. icon: 'icon',
  84. iconIndex: 1,
  85. toastActivatorClsid: '{0E3CFA27-6FEA-410B-824F-A174B6E865E5}'
  86. };
  87. ifdescribe(process.platform === 'win32')('shell.readShortcutLink(shortcutPath)', () => {
  88. it('throws when failed', () => {
  89. expect(() => {
  90. shell.readShortcutLink('not-exist');
  91. }).to.throw('Failed to read shortcut link');
  92. });
  93. const fixtures = path.resolve(__dirname, 'fixtures');
  94. it('reads all properties of a shortcut', () => {
  95. const shortcut = shell.readShortcutLink(path.join(fixtures, 'assets', 'shortcut.lnk'));
  96. expect(shortcut).to.deep.equal(shortcutOptions);
  97. });
  98. });
  99. ifdescribe(process.platform === 'win32')('shell.writeShortcutLink(shortcutPath[, operation], options)', () => {
  100. const tmpShortcut = path.join(os.tmpdir(), `${Date.now()}.lnk`);
  101. afterEach(() => {
  102. fs.unlinkSync(tmpShortcut);
  103. });
  104. it('writes the shortcut', () => {
  105. expect(shell.writeShortcutLink(tmpShortcut, { target: 'C:\\' })).to.be.true();
  106. expect(fs.existsSync(tmpShortcut)).to.be.true();
  107. });
  108. it('correctly sets the fields', () => {
  109. expect(shell.writeShortcutLink(tmpShortcut, shortcutOptions)).to.be.true();
  110. expect(shell.readShortcutLink(tmpShortcut)).to.deep.equal(shortcutOptions);
  111. });
  112. it('updates the shortcut', () => {
  113. expect(shell.writeShortcutLink(tmpShortcut, 'update', shortcutOptions)).to.be.false();
  114. expect(shell.writeShortcutLink(tmpShortcut, 'create', shortcutOptions)).to.be.true();
  115. expect(shell.readShortcutLink(tmpShortcut)).to.deep.equal(shortcutOptions);
  116. const change = { target: 'D:\\' };
  117. expect(shell.writeShortcutLink(tmpShortcut, 'update', change)).to.be.true();
  118. expect(shell.readShortcutLink(tmpShortcut)).to.deep.equal({ ...shortcutOptions, ...change });
  119. });
  120. it('replaces the shortcut', () => {
  121. expect(shell.writeShortcutLink(tmpShortcut, 'replace', shortcutOptions)).to.be.false();
  122. expect(shell.writeShortcutLink(tmpShortcut, 'create', shortcutOptions)).to.be.true();
  123. expect(shell.readShortcutLink(tmpShortcut)).to.deep.equal(shortcutOptions);
  124. const change = {
  125. target: 'D:\\',
  126. description: 'description2',
  127. cwd: 'cwd2',
  128. args: 'args2',
  129. appUserModelId: 'appUserModelId2',
  130. icon: 'icon2',
  131. iconIndex: 2,
  132. toastActivatorClsid: '{C51A3996-CAD9-4934-848B-16285D4A1496}'
  133. };
  134. expect(shell.writeShortcutLink(tmpShortcut, 'replace', change)).to.be.true();
  135. expect(shell.readShortcutLink(tmpShortcut)).to.deep.equal(change);
  136. });
  137. });
  138. });