api-shell-spec.js 5.1 KB

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