api-app-spec.js 27 KB

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