api-shell-spec.ts 7.1 KB

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