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