api-app-spec.js 28 KB

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