api-shell-spec.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 'node:fs';
  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. async function urlOpened () {
  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. return { url, requestReceived };
  53. }
  54. it('opens an external link', async () => {
  55. const { url, requestReceived } = await urlOpened();
  56. await Promise.all<void>([
  57. shell.openExternal(url),
  58. requestReceived
  59. ]);
  60. });
  61. it('opens an external link in the renderer', async () => {
  62. const { url, requestReceived } = await urlOpened();
  63. const w = new BrowserWindow({ show: false, webPreferences: { sandbox: false, contextIsolation: false, nodeIntegration: true } });
  64. await w.loadURL('about:blank');
  65. await Promise.all<void>([
  66. w.webContents.executeJavaScript(`require("electron").shell.openExternal(${JSON.stringify(url)})`),
  67. requestReceived
  68. ]);
  69. });
  70. });
  71. describe('shell.trashItem()', () => {
  72. afterEach(closeAllWindows);
  73. it('moves an item to the trash', async () => {
  74. const dir = await fs.promises.mkdtemp(path.resolve(app.getPath('temp'), 'electron-shell-spec-'));
  75. const filename = path.join(dir, 'temp-to-be-deleted');
  76. await fs.promises.writeFile(filename, 'dummy-contents');
  77. await shell.trashItem(filename);
  78. expect(fs.existsSync(filename)).to.be.false();
  79. });
  80. it('throws when called with a nonexistent path', async () => {
  81. const filename = path.join(app.getPath('temp'), 'does-not-exist');
  82. await expect(shell.trashItem(filename)).to.eventually.be.rejected();
  83. });
  84. ifit(!(process.platform === 'win32' && process.arch === 'ia32'))('works in the renderer process', async () => {
  85. const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
  86. w.loadURL('about:blank');
  87. 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/);
  88. });
  89. });
  90. const shortcutOptions = {
  91. target: 'C:\\target',
  92. description: 'description',
  93. cwd: 'cwd',
  94. args: 'args',
  95. appUserModelId: 'appUserModelId',
  96. icon: 'icon',
  97. iconIndex: 1,
  98. toastActivatorClsid: '{0E3CFA27-6FEA-410B-824F-A174B6E865E5}'
  99. };
  100. ifdescribe(process.platform === 'win32')('shell.readShortcutLink(shortcutPath)', () => {
  101. it('throws when failed', () => {
  102. expect(() => {
  103. shell.readShortcutLink('not-exist');
  104. }).to.throw('Failed to read shortcut link');
  105. });
  106. const fixtures = path.resolve(__dirname, 'fixtures');
  107. it('reads all properties of a shortcut', () => {
  108. const shortcut = shell.readShortcutLink(path.join(fixtures, 'assets', 'shortcut.lnk'));
  109. expect(shortcut).to.deep.equal(shortcutOptions);
  110. });
  111. });
  112. ifdescribe(process.platform === 'win32')('shell.writeShortcutLink(shortcutPath[, operation], options)', () => {
  113. const tmpShortcut = path.join(os.tmpdir(), `${Date.now()}.lnk`);
  114. afterEach(() => {
  115. fs.unlinkSync(tmpShortcut);
  116. });
  117. it('writes the shortcut', () => {
  118. expect(shell.writeShortcutLink(tmpShortcut, { target: 'C:\\' })).to.be.true();
  119. expect(fs.existsSync(tmpShortcut)).to.be.true();
  120. });
  121. it('correctly sets the fields', () => {
  122. expect(shell.writeShortcutLink(tmpShortcut, shortcutOptions)).to.be.true();
  123. expect(shell.readShortcutLink(tmpShortcut)).to.deep.equal(shortcutOptions);
  124. });
  125. it('updates the shortcut', () => {
  126. expect(shell.writeShortcutLink(tmpShortcut, 'update', shortcutOptions)).to.be.false();
  127. expect(shell.writeShortcutLink(tmpShortcut, 'create', shortcutOptions)).to.be.true();
  128. expect(shell.readShortcutLink(tmpShortcut)).to.deep.equal(shortcutOptions);
  129. const change = { target: 'D:\\' };
  130. expect(shell.writeShortcutLink(tmpShortcut, 'update', change)).to.be.true();
  131. expect(shell.readShortcutLink(tmpShortcut)).to.deep.equal({ ...shortcutOptions, ...change });
  132. });
  133. it('replaces the shortcut', () => {
  134. expect(shell.writeShortcutLink(tmpShortcut, 'replace', shortcutOptions)).to.be.false();
  135. expect(shell.writeShortcutLink(tmpShortcut, 'create', shortcutOptions)).to.be.true();
  136. expect(shell.readShortcutLink(tmpShortcut)).to.deep.equal(shortcutOptions);
  137. const change = {
  138. target: 'D:\\',
  139. description: 'description2',
  140. cwd: 'cwd2',
  141. args: 'args2',
  142. appUserModelId: 'appUserModelId2',
  143. icon: 'icon2',
  144. iconIndex: 2,
  145. toastActivatorClsid: '{C51A3996-CAD9-4934-848B-16285D4A1496}'
  146. };
  147. expect(shell.writeShortcutLink(tmpShortcut, 'replace', change)).to.be.true();
  148. expect(shell.readShortcutLink(tmpShortcut)).to.deep.equal(change);
  149. });
  150. });
  151. });