api-shell-spec.ts 6.0 KB

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