api-shell-spec.js 4.6 KB

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