api-app-spec.js 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103
  1. const chai = require('chai')
  2. const chaiAsPromised = require('chai-as-promised')
  3. const dirtyChai = require('dirty-chai')
  4. const ChildProcess = require('child_process')
  5. const https = require('https')
  6. const net = require('net')
  7. const fs = require('fs')
  8. const path = require('path')
  9. const { ipcRenderer, remote } = require('electron')
  10. const { emittedOnce } = require('./events-helpers')
  11. const { closeWindow } = require('./window-helpers')
  12. const { expect } = chai
  13. const { app, BrowserWindow, Menu, ipcMain } = remote
  14. const isCI = remote.getGlobal('isCi')
  15. chai.use(chaiAsPromised)
  16. chai.use(dirtyChai)
  17. describe('electron module', () => {
  18. it('does not expose internal modules to require', () => {
  19. expect(() => {
  20. require('clipboard')
  21. }).to.throw(/Cannot find module 'clipboard'/)
  22. })
  23. describe('require("electron")', () => {
  24. let window = null
  25. beforeEach(() => {
  26. window = new BrowserWindow({
  27. show: false,
  28. width: 400,
  29. height: 400
  30. })
  31. })
  32. afterEach(() => {
  33. return closeWindow(window).then(() => { window = null })
  34. })
  35. it('always returns the internal electron module', (done) => {
  36. ipcMain.once('answer', () => done())
  37. window.loadFile(path.join(__dirname, 'fixtures', 'api', 'electron-module-app', 'index.html'))
  38. })
  39. })
  40. })
  41. describe('app module', () => {
  42. let server, secureUrl
  43. const certPath = path.join(__dirname, 'fixtures', 'certificates')
  44. before((done) => {
  45. const options = {
  46. key: fs.readFileSync(path.join(certPath, 'server.key')),
  47. cert: fs.readFileSync(path.join(certPath, 'server.pem')),
  48. ca: [
  49. fs.readFileSync(path.join(certPath, 'rootCA.pem')),
  50. fs.readFileSync(path.join(certPath, 'intermediateCA.pem'))
  51. ],
  52. requestCert: true,
  53. rejectUnauthorized: false
  54. }
  55. server = https.createServer(options, (req, res) => {
  56. if (req.client.authorized) {
  57. res.writeHead(200)
  58. res.end('<title>authorized</title>')
  59. } else {
  60. res.writeHead(401)
  61. res.end('<title>denied</title>')
  62. }
  63. })
  64. server.listen(0, '127.0.0.1', () => {
  65. const port = server.address().port
  66. secureUrl = `https://127.0.0.1:${port}`
  67. done()
  68. })
  69. })
  70. after(done => {
  71. server.close(() => done())
  72. })
  73. describe('app.getVersion()', () => {
  74. it('returns the version field of package.json', () => {
  75. expect(app.getVersion()).to.equal('0.1.0')
  76. })
  77. })
  78. describe('app.setVersion(version)', () => {
  79. it('overrides the version', () => {
  80. expect(app.getVersion()).to.equal('0.1.0')
  81. app.setVersion('test-version')
  82. expect(app.getVersion()).to.equal('test-version')
  83. app.setVersion('0.1.0')
  84. })
  85. })
  86. describe('app.getName()', () => {
  87. it('returns the name field of package.json', () => {
  88. expect(app.getName()).to.equal('Electron Test')
  89. })
  90. })
  91. describe('app.setName(name)', () => {
  92. it('overrides the name', () => {
  93. expect(app.getName()).to.equal('Electron Test')
  94. app.setName('test-name')
  95. expect(app.getName()).to.equal('test-name')
  96. app.setName('Electron Test')
  97. })
  98. })
  99. describe('app.getLocale()', () => {
  100. it('should not be empty', () => {
  101. expect(app.getLocale()).to.not.be.empty()
  102. })
  103. })
  104. describe('app.isPackaged', () => {
  105. it('should be false durings tests', () => {
  106. expect(app.isPackaged).to.be.false()
  107. })
  108. })
  109. describe('app.isInApplicationsFolder()', () => {
  110. before(function () {
  111. if (process.platform !== 'darwin') {
  112. this.skip()
  113. }
  114. })
  115. it('should be false during tests', () => {
  116. expect(app.isInApplicationsFolder()).to.be.false()
  117. })
  118. })
  119. describe('app.exit(exitCode)', () => {
  120. let appProcess = null
  121. afterEach(() => {
  122. if (appProcess != null) appProcess.kill()
  123. })
  124. it('emits a process exit event with the code', async () => {
  125. const appPath = path.join(__dirname, 'fixtures', 'api', 'quit-app')
  126. const electronPath = remote.getGlobal('process').execPath
  127. let output = ''
  128. appProcess = ChildProcess.spawn(electronPath, [appPath])
  129. appProcess.stdout.on('data', data => { output += data })
  130. const [code] = await emittedOnce(appProcess, 'close')
  131. if (process.platform !== 'win32') {
  132. expect(output).to.include('Exit event with code: 123')
  133. }
  134. expect(code).to.equal(123)
  135. })
  136. it('closes all windows', async function () {
  137. const appPath = path.join(__dirname, 'fixtures', 'api', 'exit-closes-all-windows-app')
  138. const electronPath = remote.getGlobal('process').execPath
  139. appProcess = ChildProcess.spawn(electronPath, [appPath])
  140. const [code, signal] = await emittedOnce(appProcess, 'close')
  141. expect(signal).to.equal(null, 'exit signal should be null, if you see this please tag @MarshallOfSound')
  142. expect(code).to.equal(123, 'exit code should be 123, if you see this please tag @MarshallOfSound')
  143. })
  144. it('exits gracefully', async function () {
  145. if (!['darwin', 'linux'].includes(process.platform)) {
  146. this.skip()
  147. return
  148. }
  149. const electronPath = remote.getGlobal('process').execPath
  150. const appPath = path.join(__dirname, 'fixtures', 'api', 'singleton')
  151. appProcess = ChildProcess.spawn(electronPath, [appPath])
  152. // Singleton will send us greeting data to let us know it's running.
  153. // After that, ask it to exit gracefully and confirm that it does.
  154. appProcess.stdout.on('data', data => appProcess.kill())
  155. const [code, signal] = await emittedOnce(appProcess, 'close')
  156. const message = `code:\n${code}\nsignal:\n${signal}`
  157. expect(code).to.equal(0, message)
  158. expect(signal).to.be.null(message)
  159. })
  160. })
  161. describe('app.requestSingleInstanceLock', () => {
  162. it('prevents the second launch of app', function (done) {
  163. this.timeout(120000)
  164. const appPath = path.join(__dirname, 'fixtures', 'api', 'singleton')
  165. const first = ChildProcess.spawn(remote.process.execPath, [appPath])
  166. first.once('exit', code => {
  167. expect(code).to.equal(0)
  168. })
  169. // Start second app when received output.
  170. first.stdout.once('data', () => {
  171. const second = ChildProcess.spawn(remote.process.execPath, [appPath])
  172. second.once('exit', code => {
  173. expect(code).to.equal(1)
  174. done()
  175. })
  176. })
  177. })
  178. })
  179. describe('app.relaunch', () => {
  180. let server = null
  181. const socketPath = process.platform === 'win32' ? '\\\\.\\pipe\\electron-app-relaunch' : '/tmp/electron-app-relaunch'
  182. beforeEach(done => {
  183. fs.unlink(socketPath, () => {
  184. server = net.createServer()
  185. server.listen(socketPath)
  186. done()
  187. })
  188. })
  189. afterEach((done) => {
  190. server.close(() => {
  191. if (process.platform === 'win32') {
  192. done()
  193. } else {
  194. fs.unlink(socketPath, () => done())
  195. }
  196. })
  197. })
  198. it('relaunches the app', function (done) {
  199. this.timeout(120000)
  200. let state = 'none'
  201. server.once('error', error => done(error))
  202. server.on('connection', client => {
  203. client.once('data', data => {
  204. if (String(data) === 'false' && state === 'none') {
  205. state = 'first-launch'
  206. } else if (String(data) === 'true' && state === 'first-launch') {
  207. done()
  208. } else {
  209. done(`Unexpected state: ${state}`)
  210. }
  211. })
  212. })
  213. const appPath = path.join(__dirname, 'fixtures', 'api', 'relaunch')
  214. ChildProcess.spawn(remote.process.execPath, [appPath])
  215. })
  216. })
  217. describe('app.setUserActivity(type, userInfo)', () => {
  218. before(function () {
  219. if (process.platform !== 'darwin') {
  220. this.skip()
  221. }
  222. })
  223. it('sets the current activity', () => {
  224. app.setUserActivity('com.electron.testActivity', { testData: '123' })
  225. expect(app.getCurrentActivityType()).to.equal('com.electron.testActivity')
  226. })
  227. })
  228. xdescribe('app.importCertificate', () => {
  229. let w = null
  230. before(function () {
  231. if (process.platform !== 'linux') {
  232. this.skip()
  233. }
  234. })
  235. afterEach(() => closeWindow(w).then(() => { w = null }))
  236. it('can import certificate into platform cert store', done => {
  237. const options = {
  238. certificate: path.join(certPath, 'client.p12'),
  239. password: 'electron'
  240. }
  241. w = new BrowserWindow({ show: false })
  242. w.webContents.on('did-finish-load', () => {
  243. expect(w.webContents.getTitle()).to.equal('authorized')
  244. done()
  245. })
  246. ipcRenderer.once('select-client-certificate', (event, webContentsId, list) => {
  247. expect(webContentsId).to.equal(w.webContents.id)
  248. expect(list).to.have.lengthOf(1)
  249. expect(list[0]).to.deep.equal({
  250. issuerName: 'Intermediate CA',
  251. subjectName: 'Client Cert',
  252. issuer: { commonName: 'Intermediate CA' },
  253. subject: { commonName: 'Client Cert' }
  254. })
  255. event.sender.send('client-certificate-response', list[0])
  256. })
  257. app.importCertificate(options, result => {
  258. expect(result).toNotExist()
  259. ipcRenderer.sendSync('set-client-certificate-option', false)
  260. w.loadURL(secureUrl)
  261. })
  262. })
  263. })
  264. describe('BrowserWindow events', () => {
  265. let w = null
  266. afterEach(() => closeWindow(w).then(() => { w = null }))
  267. it('should emit browser-window-focus event when window is focused', (done) => {
  268. app.once('browser-window-focus', (e, window) => {
  269. expect(w.id).to.equal(window.id)
  270. done()
  271. })
  272. w = new BrowserWindow({ show: false })
  273. w.emit('focus')
  274. })
  275. it('should emit browser-window-blur event when window is blured', (done) => {
  276. app.once('browser-window-blur', (e, window) => {
  277. expect(w.id).to.equal(window.id)
  278. done()
  279. })
  280. w = new BrowserWindow({ show: false })
  281. w.emit('blur')
  282. })
  283. it('should emit browser-window-created event when window is created', (done) => {
  284. app.once('browser-window-created', (e, window) => {
  285. setImmediate(() => {
  286. expect(w.id).to.equal(window.id)
  287. done()
  288. })
  289. })
  290. w = new BrowserWindow({ show: false })
  291. })
  292. it('should emit web-contents-created event when a webContents is created', (done) => {
  293. app.once('web-contents-created', (e, webContents) => {
  294. setImmediate(() => {
  295. expect(w.webContents.id).to.equal(webContents.id)
  296. done()
  297. })
  298. })
  299. w = new BrowserWindow({ show: false })
  300. })
  301. it('should emit remote-require event when remote.require() is invoked', async () => {
  302. w = new BrowserWindow({ show: false })
  303. await w.loadURL('about:blank')
  304. const promise = emittedOnce(app, 'remote-require')
  305. w.webContents.executeJavaScript(`require('electron').remote.require('test')`)
  306. const [, webContents, moduleName] = await promise
  307. expect(webContents).to.equal(w.webContents)
  308. expect(moduleName).to.equal('test')
  309. })
  310. it('should emit remote-get-global event when remote.getGlobal() is invoked', async () => {
  311. w = new BrowserWindow({ show: false })
  312. await w.loadURL('about:blank')
  313. const promise = emittedOnce(app, 'remote-get-global')
  314. w.webContents.executeJavaScript(`require('electron').remote.getGlobal('test')`)
  315. const [, webContents, globalName] = await promise
  316. expect(webContents).to.equal(w.webContents)
  317. expect(globalName).to.equal('test')
  318. })
  319. it('should emit remote-get-builtin event when remote.getBuiltin() is invoked', async () => {
  320. w = new BrowserWindow({ show: false })
  321. await w.loadURL('about:blank')
  322. const promise = emittedOnce(app, 'remote-get-builtin')
  323. w.webContents.executeJavaScript(`require('electron').remote.app`)
  324. const [, webContents, moduleName] = await promise
  325. expect(webContents).to.equal(w.webContents)
  326. expect(moduleName).to.equal('app')
  327. })
  328. it('should emit remote-get-current-window event when remote.getCurrentWindow() is invoked', async () => {
  329. w = new BrowserWindow({ show: false })
  330. await w.loadURL('about:blank')
  331. const promise = emittedOnce(app, 'remote-get-current-window')
  332. w.webContents.executeJavaScript(`require('electron').remote.getCurrentWindow()`)
  333. const [, webContents] = await promise
  334. expect(webContents).to.equal(w.webContents)
  335. })
  336. it('should emit remote-get-current-web-contents event when remote.getCurrentWebContents() is invoked', async () => {
  337. w = new BrowserWindow({ show: false })
  338. await w.loadURL('about:blank')
  339. const promise = emittedOnce(app, 'remote-get-current-web-contents')
  340. w.webContents.executeJavaScript(`require('electron').remote.getCurrentWebContents()`)
  341. const [, webContents] = await promise
  342. expect(webContents).to.equal(w.webContents)
  343. })
  344. })
  345. describe('app.setBadgeCount', () => {
  346. const platformIsNotSupported =
  347. (process.platform === 'win32') ||
  348. (process.platform === 'linux' && !app.isUnityRunning())
  349. const platformIsSupported = !platformIsNotSupported
  350. const expectedBadgeCount = 42
  351. let returnValue = null
  352. beforeEach(() => { returnValue = app.setBadgeCount(expectedBadgeCount) })
  353. after(() => {
  354. // Remove the badge.
  355. app.setBadgeCount(0)
  356. })
  357. describe('on supported platform', () => {
  358. before(function () {
  359. if (platformIsNotSupported) {
  360. this.skip()
  361. }
  362. })
  363. it('returns true', () => {
  364. expect(returnValue).to.be.true()
  365. })
  366. it('sets a badge count', () => {
  367. expect(app.getBadgeCount()).to.equal(expectedBadgeCount)
  368. })
  369. })
  370. describe('on unsupported platform', () => {
  371. before(function () {
  372. if (platformIsSupported) {
  373. this.skip()
  374. }
  375. })
  376. it('returns false', () => {
  377. expect(returnValue).to.be.false()
  378. })
  379. it('does not set a badge count', () => {
  380. expect(app.getBadgeCount()).to.equal(0)
  381. })
  382. })
  383. })
  384. describe('app.get/setLoginItemSettings API', function () {
  385. const updateExe = path.resolve(path.dirname(process.execPath), '..', 'Update.exe')
  386. const processStartArgs = [
  387. '--processStart', `"${path.basename(process.execPath)}"`,
  388. '--process-start-args', `"--hidden"`
  389. ]
  390. before(function () {
  391. if (process.platform === 'linux' || process.mas) this.skip()
  392. })
  393. beforeEach(() => {
  394. app.setLoginItemSettings({ openAtLogin: false })
  395. app.setLoginItemSettings({ openAtLogin: false, path: updateExe, args: processStartArgs })
  396. })
  397. afterEach(() => {
  398. app.setLoginItemSettings({ openAtLogin: false })
  399. app.setLoginItemSettings({ openAtLogin: false, path: updateExe, args: processStartArgs })
  400. })
  401. it('sets and returns the app as a login item', done => {
  402. app.setLoginItemSettings({ openAtLogin: true })
  403. expect(app.getLoginItemSettings()).to.deep.equal({
  404. openAtLogin: true,
  405. openAsHidden: false,
  406. wasOpenedAtLogin: false,
  407. wasOpenedAsHidden: false,
  408. restoreState: false
  409. })
  410. done()
  411. })
  412. it('adds a login item that loads in hidden mode', done => {
  413. app.setLoginItemSettings({ openAtLogin: true, openAsHidden: true })
  414. expect(app.getLoginItemSettings()).to.deep.equal({
  415. openAtLogin: true,
  416. openAsHidden: process.platform === 'darwin' && !process.mas, // Only available on macOS
  417. wasOpenedAtLogin: false,
  418. wasOpenedAsHidden: false,
  419. restoreState: false
  420. })
  421. done()
  422. })
  423. it('correctly sets and unsets the LoginItem', function () {
  424. expect(app.getLoginItemSettings().openAtLogin).to.be.false()
  425. app.setLoginItemSettings({ openAtLogin: true })
  426. expect(app.getLoginItemSettings().openAtLogin).to.be.true()
  427. app.setLoginItemSettings({ openAtLogin: false })
  428. expect(app.getLoginItemSettings().openAtLogin).to.be.false()
  429. })
  430. it('correctly sets and unsets the LoginItem as hidden', function () {
  431. if (process.platform !== 'darwin') this.skip()
  432. expect(app.getLoginItemSettings().openAtLogin).to.be.false()
  433. expect(app.getLoginItemSettings().openAsHidden).to.be.false()
  434. app.setLoginItemSettings({ openAtLogin: true, openAsHidden: true })
  435. expect(app.getLoginItemSettings().openAtLogin).to.be.true()
  436. expect(app.getLoginItemSettings().openAsHidden).to.be.true()
  437. app.setLoginItemSettings({ openAtLogin: true, openAsHidden: false })
  438. expect(app.getLoginItemSettings().openAtLogin).to.be.true()
  439. expect(app.getLoginItemSettings().openAsHidden).to.be.false()
  440. })
  441. it('allows you to pass a custom executable and arguments', function () {
  442. if (process.platform !== 'win32') this.skip()
  443. app.setLoginItemSettings({ openAtLogin: true, path: updateExe, args: processStartArgs })
  444. expect(app.getLoginItemSettings().openAtLogin).to.be.false()
  445. expect(app.getLoginItemSettings({
  446. path: updateExe,
  447. args: processStartArgs
  448. }).openAtLogin).to.be.true()
  449. })
  450. })
  451. describe('isAccessibilitySupportEnabled API', () => {
  452. it('returns whether the Chrome has accessibility APIs enabled', () => {
  453. expect(app.isAccessibilitySupportEnabled()).to.be.a('boolean')
  454. })
  455. })
  456. describe('getPath(name)', () => {
  457. it('returns paths that exist', () => {
  458. const paths = [
  459. fs.existsSync(app.getPath('exe')),
  460. fs.existsSync(app.getPath('home')),
  461. fs.existsSync(app.getPath('temp'))
  462. ]
  463. expect(paths).to.deep.equal([true, true, true])
  464. })
  465. it('throws an error when the name is invalid', () => {
  466. expect(() => {
  467. app.getPath('does-not-exist')
  468. }).to.throw(/Failed to get 'does-not-exist' path/)
  469. })
  470. it('returns the overridden path', () => {
  471. app.setPath('music', __dirname)
  472. expect(app.getPath('music')).to.equal(__dirname)
  473. })
  474. })
  475. describe('select-client-certificate event', () => {
  476. let w = null
  477. before(function () {
  478. if (process.platform === 'linux') {
  479. this.skip()
  480. }
  481. })
  482. beforeEach(() => {
  483. w = new BrowserWindow({
  484. show: false,
  485. webPreferences: {
  486. partition: 'empty-certificate'
  487. }
  488. })
  489. })
  490. afterEach(() => closeWindow(w).then(() => { w = null }))
  491. it('can respond with empty certificate list', done => {
  492. w.webContents.on('did-finish-load', () => {
  493. expect(w.webContents.getTitle()).to.equal('denied')
  494. done()
  495. })
  496. ipcRenderer.sendSync('set-client-certificate-option', true)
  497. w.webContents.loadURL(secureUrl)
  498. })
  499. })
  500. describe('setAsDefaultProtocolClient(protocol, path, args)', () => {
  501. const protocol = 'electron-test'
  502. const updateExe = path.resolve(path.dirname(process.execPath), '..', 'Update.exe')
  503. const processStartArgs = [
  504. '--processStart', `"${path.basename(process.execPath)}"`,
  505. '--process-start-args', `"--hidden"`
  506. ]
  507. let Winreg
  508. let classesKey
  509. before(function () {
  510. if (process.platform !== 'win32') {
  511. this.skip()
  512. } else {
  513. Winreg = require('winreg')
  514. classesKey = new Winreg({
  515. hive: Winreg.HKCU,
  516. key: '\\Software\\Classes\\'
  517. })
  518. }
  519. })
  520. after(function (done) {
  521. if (process.platform !== 'win32') {
  522. done()
  523. } else {
  524. const protocolKey = new Winreg({
  525. hive: Winreg.HKCU,
  526. key: `\\Software\\Classes\\${protocol}`
  527. })
  528. // The last test leaves the registry dirty,
  529. // delete the protocol key for those of us who test at home
  530. protocolKey.destroy(() => done())
  531. }
  532. })
  533. beforeEach(() => {
  534. app.removeAsDefaultProtocolClient(protocol)
  535. app.removeAsDefaultProtocolClient(protocol, updateExe, processStartArgs)
  536. })
  537. afterEach(() => {
  538. app.removeAsDefaultProtocolClient(protocol)
  539. expect(app.isDefaultProtocolClient(protocol)).to.be.false()
  540. app.removeAsDefaultProtocolClient(protocol, updateExe, processStartArgs)
  541. expect(app.isDefaultProtocolClient(protocol, updateExe, processStartArgs)).to.be.false()
  542. })
  543. it('sets the app as the default protocol client', () => {
  544. expect(app.isDefaultProtocolClient(protocol)).to.be.false()
  545. app.setAsDefaultProtocolClient(protocol)
  546. expect(app.isDefaultProtocolClient(protocol)).to.be.true()
  547. })
  548. it('allows a custom path and args to be specified', () => {
  549. expect(app.isDefaultProtocolClient(protocol, updateExe, processStartArgs)).to.be.false()
  550. app.setAsDefaultProtocolClient(protocol, updateExe, processStartArgs)
  551. expect(app.isDefaultProtocolClient(protocol, updateExe, processStartArgs)).to.be.true()
  552. expect(app.isDefaultProtocolClient(protocol)).to.be.false()
  553. })
  554. it('creates a registry entry for the protocol class', (done) => {
  555. app.setAsDefaultProtocolClient(protocol)
  556. classesKey.keys((error, keys) => {
  557. if (error) throw error
  558. const exists = !!keys.find(key => key.key.includes(protocol))
  559. expect(exists).to.be.true()
  560. done()
  561. })
  562. })
  563. it('completely removes a registry entry for the protocol class', (done) => {
  564. app.setAsDefaultProtocolClient(protocol)
  565. app.removeAsDefaultProtocolClient(protocol)
  566. classesKey.keys((error, keys) => {
  567. if (error) throw error
  568. const exists = !!keys.find(key => key.key.includes(protocol))
  569. expect(exists).to.be.false()
  570. done()
  571. })
  572. })
  573. it('only unsets a class registry key if it contains other data', (done) => {
  574. app.setAsDefaultProtocolClient(protocol)
  575. const protocolKey = new Winreg({
  576. hive: Winreg.HKCU,
  577. key: `\\Software\\Classes\\${protocol}`
  578. })
  579. protocolKey.set('test-value', 'REG_BINARY', '123', () => {
  580. app.removeAsDefaultProtocolClient(protocol)
  581. classesKey.keys((error, keys) => {
  582. if (error) throw error
  583. const exists = !!keys.find(key => key.key.includes(protocol))
  584. expect(exists).to.be.true()
  585. done()
  586. })
  587. })
  588. })
  589. })
  590. describe('app launch through uri', () => {
  591. before(function () {
  592. if (process.platform !== 'win32') {
  593. this.skip()
  594. }
  595. })
  596. it('does not launch for argument following a URL', done => {
  597. const appPath = path.join(__dirname, 'fixtures', 'api', 'quit-app')
  598. // App should exit with non 123 code.
  599. const first = ChildProcess.spawn(remote.process.execPath, [appPath, 'electron-test:?', 'abc'])
  600. first.once('exit', code => {
  601. expect(code).to.not.equal(123)
  602. done()
  603. })
  604. })
  605. it('launches successfully for argument following a file path', done => {
  606. const appPath = path.join(__dirname, 'fixtures', 'api', 'quit-app')
  607. // App should exit with code 123.
  608. const first = ChildProcess.spawn(remote.process.execPath, [appPath, 'e:\\abc', 'abc'])
  609. first.once('exit', code => {
  610. expect(code).to.equal(123)
  611. done()
  612. })
  613. })
  614. it('launches successfully for multiple URIs following --', done => {
  615. const appPath = path.join(__dirname, 'fixtures', 'api', 'quit-app')
  616. // App should exit with code 123.
  617. const first = ChildProcess.spawn(remote.process.execPath, [appPath, '--', 'http://electronjs.org', 'electron-test://testdata'])
  618. first.once('exit', code => {
  619. expect(code).to.equal(123)
  620. done()
  621. })
  622. })
  623. })
  624. describe('getFileIcon() API', () => {
  625. const iconPath = path.join(__dirname, 'fixtures/assets/icon.ico')
  626. const sizes = {
  627. small: 16,
  628. normal: 32,
  629. large: process.platform === 'win32' ? 32 : 48
  630. }
  631. // (alexeykuzmin): `.skip()` called in `before`
  632. // doesn't affect nested `describe`s.
  633. beforeEach(function () {
  634. // FIXME Get these specs running on Linux CI
  635. if (process.platform === 'linux' && isCI) {
  636. this.skip()
  637. }
  638. })
  639. it('fetches a non-empty icon', done => {
  640. app.getFileIcon(iconPath, (err, icon) => {
  641. expect(err).to.be.null()
  642. expect(icon.isEmpty()).to.be.false()
  643. done()
  644. })
  645. })
  646. it('fetches normal icon size by default', done => {
  647. app.getFileIcon(iconPath, (err, icon) => {
  648. const size = icon.getSize()
  649. expect(err).to.be.null()
  650. expect(size.height).to.equal(sizes.normal)
  651. expect(size.width).to.equal(sizes.normal)
  652. done()
  653. })
  654. })
  655. describe('size option', () => {
  656. it('fetches a small icon', (done) => {
  657. app.getFileIcon(iconPath, { size: 'small' }, (err, icon) => {
  658. const size = icon.getSize()
  659. expect(err).to.be.null()
  660. expect(size.height).to.equal(sizes.small)
  661. expect(size.width).to.equal(sizes.small)
  662. done()
  663. })
  664. })
  665. it('fetches a normal icon', (done) => {
  666. app.getFileIcon(iconPath, { size: 'normal' }, (err, icon) => {
  667. const size = icon.getSize()
  668. expect(err).to.be.null()
  669. expect(size.height).to.equal(sizes.normal)
  670. expect(size.width).to.equal(sizes.normal)
  671. done()
  672. })
  673. })
  674. it('fetches a large icon', function (done) {
  675. // macOS does not support large icons
  676. if (process.platform === 'darwin') {
  677. // FIXME(alexeykuzmin): Skip the test.
  678. // this.skip()
  679. return done()
  680. }
  681. app.getFileIcon(iconPath, { size: 'large' }, (err, icon) => {
  682. const size = icon.getSize()
  683. expect(err).to.be.null()
  684. expect(size.height).to.equal(sizes.large)
  685. expect(size.width).to.equal(sizes.large)
  686. done()
  687. })
  688. })
  689. })
  690. })
  691. describe('getAppMetrics() API', () => {
  692. it('returns memory and cpu stats of all running electron processes', () => {
  693. const appMetrics = app.getAppMetrics()
  694. expect(appMetrics).to.be.an('array').and.have.lengthOf.at.least(1, 'App memory info object is not > 0')
  695. const types = []
  696. for (const { pid, type, cpu } of appMetrics) {
  697. expect(pid).to.be.above(0, 'pid is not > 0')
  698. expect(type).to.be.a('string').that.is.not.empty()
  699. types.push(type)
  700. expect(cpu).to.have.own.property('percentCPUUsage').that.is.a('number')
  701. expect(cpu).to.have.own.property('idleWakeupsPerSecond').that.is.a('number')
  702. }
  703. if (process.platform === 'darwin') {
  704. expect(types).to.include('GPU')
  705. }
  706. expect(types).to.include('Browser')
  707. expect(types).to.include('Tab')
  708. })
  709. })
  710. describe('getGPUFeatureStatus() API', () => {
  711. it('returns the graphic features statuses', () => {
  712. const features = app.getGPUFeatureStatus()
  713. expect(features).to.have.own.property('webgl').that.is.a('string')
  714. expect(features).to.have.own.property('gpu_compositing').that.is.a('string')
  715. })
  716. })
  717. describe('getGPUInfo() API', () => {
  718. const appPath = path.join(__dirname, 'fixtures', 'api', 'gpu-info.js')
  719. const getGPUInfo = async (type) => {
  720. const appProcess = ChildProcess.spawn(remote.process.execPath, [appPath, type])
  721. let gpuInfoData = ''
  722. let errorData = ''
  723. appProcess.stdout.on('data', (data) => {
  724. gpuInfoData += data
  725. })
  726. appProcess.stderr.on('data', (data) => {
  727. errorData += data
  728. })
  729. const [exitCode] = await emittedOnce(appProcess, 'exit')
  730. if (exitCode === 0) {
  731. // return info data on successful exit
  732. return JSON.parse(gpuInfoData)
  733. } else {
  734. // return error if not clean exit
  735. return Promise.reject(new Error(errorData))
  736. }
  737. }
  738. const verifyBasicGPUInfo = async (gpuInfo) => {
  739. // Devices information is always present in the available info.
  740. expect(gpuInfo).to.have.own.property('gpuDevice')
  741. .that.is.an('array')
  742. .and.is.not.empty()
  743. const device = gpuInfo.gpuDevice[0]
  744. expect(device).to.be.an('object')
  745. .and.to.have.property('deviceId')
  746. .that.is.a('number')
  747. .not.lessThan(0)
  748. }
  749. it('succeeds with basic GPUInfo', async () => {
  750. const gpuInfo = await getGPUInfo('basic')
  751. await verifyBasicGPUInfo(gpuInfo)
  752. })
  753. it('succeeds with complete GPUInfo', async () => {
  754. const completeInfo = await getGPUInfo('complete')
  755. if (process.platform === 'linux') {
  756. // For linux and macOS complete info is same as basic info
  757. await verifyBasicGPUInfo(completeInfo)
  758. const basicInfo = await getGPUInfo('basic')
  759. expect(completeInfo).to.deep.equal(basicInfo)
  760. } else {
  761. // Gl version is present in the complete info.
  762. expect(completeInfo).to.have.own.property('auxAttributes')
  763. .that.is.an('object')
  764. expect(completeInfo.auxAttributes).to.have.own.property('glVersion')
  765. .that.is.a('string')
  766. .and.not.empty()
  767. }
  768. })
  769. it('fails for invalid info_type', () => {
  770. const invalidType = 'invalid'
  771. const expectedErrorMessage = "Invalid info type. Use 'basic' or 'complete'"
  772. return expect(app.getGPUInfo(invalidType)).to.eventually.be.rejectedWith(expectedErrorMessage)
  773. })
  774. })
  775. describe('sandbox options', () => {
  776. let appProcess = null
  777. let server = null
  778. const socketPath = process.platform === 'win32' ? '\\\\.\\pipe\\electron-mixed-sandbox' : '/tmp/electron-mixed-sandbox'
  779. beforeEach(function (done) {
  780. // XXX(alexeykuzmin): Calling `.skip()` inside a `before` hook
  781. // doesn't affect nested `describe`s.
  782. // FIXME Get these specs running on Linux
  783. if (process.platform === 'linux') {
  784. this.skip()
  785. }
  786. fs.unlink(socketPath, () => {
  787. server = net.createServer()
  788. server.listen(socketPath)
  789. done()
  790. })
  791. })
  792. afterEach(done => {
  793. if (appProcess != null) appProcess.kill()
  794. server.close(() => {
  795. if (process.platform === 'win32') {
  796. done()
  797. } else {
  798. fs.unlink(socketPath, () => done())
  799. }
  800. })
  801. })
  802. describe('when app.enableSandbox() is called', () => {
  803. it('adds --enable-sandbox to all renderer processes', done => {
  804. const appPath = path.join(__dirname, 'fixtures', 'api', 'mixed-sandbox-app')
  805. appProcess = ChildProcess.spawn(remote.process.execPath, [appPath, '--app-enable-sandbox'])
  806. server.once('error', error => { done(error) })
  807. server.on('connection', client => {
  808. client.once('data', data => {
  809. const argv = JSON.parse(data)
  810. expect(argv.sandbox).to.include('--enable-sandbox')
  811. expect(argv.sandbox).to.not.include('--no-sandbox')
  812. expect(argv.noSandbox).to.include('--enable-sandbox')
  813. expect(argv.noSandbox).to.not.include('--no-sandbox')
  814. expect(argv.noSandboxDevtools).to.be.true()
  815. expect(argv.sandboxDevtools).to.be.true()
  816. done()
  817. })
  818. })
  819. })
  820. })
  821. describe('when the app is launched with --enable-sandbox', () => {
  822. it('adds --enable-sandbox to all renderer processes', done => {
  823. const appPath = path.join(__dirname, 'fixtures', 'api', 'mixed-sandbox-app')
  824. appProcess = ChildProcess.spawn(remote.process.execPath, [appPath, '--enable-sandbox'])
  825. server.once('error', error => { done(error) })
  826. server.on('connection', client => {
  827. client.once('data', data => {
  828. const argv = JSON.parse(data)
  829. expect(argv.sandbox).to.include('--enable-sandbox')
  830. expect(argv.sandbox).to.not.include('--no-sandbox')
  831. expect(argv.noSandbox).to.include('--enable-sandbox')
  832. expect(argv.noSandbox).to.not.include('--no-sandbox')
  833. expect(argv.noSandboxDevtools).to.be.true()
  834. expect(argv.sandboxDevtools).to.be.true()
  835. done()
  836. })
  837. })
  838. })
  839. })
  840. describe('when app.enableMixedSandbox() is called', () => {
  841. it('adds --enable-sandbox to renderer processes created with sandbox: true', done => {
  842. const appPath = path.join(__dirname, 'fixtures', 'api', 'mixed-sandbox-app')
  843. appProcess = ChildProcess.spawn(remote.process.execPath, [appPath, '--app-enable-mixed-sandbox'])
  844. server.once('error', error => { done(error) })
  845. server.on('connection', client => {
  846. client.once('data', data => {
  847. const argv = JSON.parse(data)
  848. expect(argv.sandbox).to.include('--enable-sandbox')
  849. expect(argv.sandbox).to.not.include('--no-sandbox')
  850. expect(argv.noSandbox).to.not.include('--enable-sandbox')
  851. expect(argv.noSandbox).to.include('--no-sandbox')
  852. expect(argv.noSandboxDevtools).to.be.true()
  853. expect(argv.sandboxDevtools).to.be.true()
  854. done()
  855. })
  856. })
  857. })
  858. })
  859. describe('when the app is launched with --enable-mixed-sandbox', () => {
  860. it('adds --enable-sandbox to renderer processes created with sandbox: true', done => {
  861. const appPath = path.join(__dirname, 'fixtures', 'api', 'mixed-sandbox-app')
  862. appProcess = ChildProcess.spawn(remote.process.execPath, [appPath, '--enable-mixed-sandbox'])
  863. server.once('error', error => { done(error) })
  864. server.on('connection', client => {
  865. client.once('data', data => {
  866. const argv = JSON.parse(data)
  867. expect(argv.sandbox).to.include('--enable-sandbox')
  868. expect(argv.sandbox).to.not.include('--no-sandbox')
  869. expect(argv.noSandbox).to.not.include('--enable-sandbox')
  870. expect(argv.noSandbox).to.include('--no-sandbox')
  871. expect(argv.noSandboxDevtools).to.be.true()
  872. expect(argv.sandboxDevtools).to.be.true()
  873. done()
  874. })
  875. })
  876. })
  877. })
  878. })
  879. describe('disableDomainBlockingFor3DAPIs() API', () => {
  880. it('throws when called after app is ready', () => {
  881. expect(() => {
  882. app.disableDomainBlockingFor3DAPIs()
  883. }).to.throw(/before app is ready/)
  884. })
  885. })
  886. describe('dock.setMenu', () => {
  887. before(function () {
  888. if (process.platform !== 'darwin') {
  889. this.skip()
  890. }
  891. })
  892. it('keeps references to the menu', () => {
  893. app.dock.setMenu(new Menu())
  894. const v8Util = process.atomBinding('v8_util')
  895. v8Util.requestGarbageCollectionForTesting()
  896. })
  897. })
  898. describe('whenReady', () => {
  899. it('returns a Promise', () => {
  900. expect(app.whenReady()).to.be.a('promise')
  901. })
  902. it('becomes fulfilled if the app is already ready', () => {
  903. expect(app.isReady()).to.be.true()
  904. return expect(app.whenReady()).to.be.eventually.fulfilled
  905. })
  906. })
  907. })