-
Notifications
You must be signed in to change notification settings - Fork 15k
/
api-app-spec.ts
1969 lines (1680 loc) · 66.7 KB
/
api-app-spec.ts
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
import { assert, expect } from 'chai';
import * as cp from 'child_process';
import * as https from 'https';
import * as http from 'http';
import * as net from 'net';
import * as fs from 'fs-extra';
import * as path from 'path';
import { promisify } from 'util';
import { app, BrowserWindow, Menu, session, net as electronNet } from 'electron/main';
import { emittedOnce } from './events-helpers';
import { closeWindow, closeAllWindows } from './window-helpers';
import { ifdescribe, ifit, waitUntil } from './spec-helpers';
import split = require('split')
const fixturesPath = path.resolve(__dirname, 'fixtures');
describe('electron module', () => {
it('does not expose internal modules to require', () => {
expect(() => {
require('clipboard');
}).to.throw(/Cannot find module 'clipboard'/);
});
describe('require("electron")', () => {
it('always returns the internal electron module', () => {
require('electron');
});
});
});
describe('app module', () => {
let server: https.Server;
let secureUrl: string;
const certPath = path.join(fixturesPath, '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 as any).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() as net.AddressInfo).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 name APIs', () => {
it('with properties', () => {
it('returns the name field of package.json', () => {
expect(app.name).to.equal('Electron Test Main');
});
it('overrides the name', () => {
expect(app.name).to.equal('Electron Test Main');
app.name = 'electron-test-name';
expect(app.name).to.equal('electron-test-name');
app.name = 'Electron Test Main';
});
});
it('with functions', () => {
it('returns the name field of package.json', () => {
expect(app.getName()).to.equal('Electron Test Main');
});
it('overrides the name', () => {
expect(app.getName()).to.equal('Electron Test Main');
app.setName('electron-test-name');
expect(app.getName()).to.equal('electron-test-name');
app.setName('Electron Test Main');
});
});
});
describe('app.getLocale()', () => {
it('should not be empty', () => {
expect(app.getLocale()).to.not.equal('');
});
});
describe('app.getSystemLocale()', () => {
it('should not be empty', () => {
expect(app.getSystemLocale()).to.not.equal('');
});
});
describe('app.getLocaleCountryCode()', () => {
it('should be empty or have length of two', () => {
const localeCountryCode = app.getLocaleCountryCode();
expect(localeCountryCode).to.be.a('string');
expect(localeCountryCode.length).to.be.oneOf([0, 2]);
});
});
describe('app.isPackaged', () => {
it('should be false during tests', () => {
expect(app.isPackaged).to.equal(false);
});
});
ifdescribe(process.platform === 'darwin')('app.isInApplicationsFolder()', () => {
it('should be false during tests', () => {
expect(app.isInApplicationsFolder()).to.equal(false);
});
});
describe('app.exit(exitCode)', () => {
let appProcess: cp.ChildProcess | null = null;
afterEach(() => {
if (appProcess) appProcess.kill();
});
it('emits a process exit event with the code', async () => {
const appPath = path.join(fixturesPath, 'api', 'quit-app');
const electronPath = process.execPath;
let output = '';
appProcess = cp.spawn(electronPath, [appPath]);
if (appProcess && appProcess.stdout) {
appProcess.stdout.on('data', data => { output += data; });
}
const [code] = await emittedOnce(appProcess, 'exit');
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(fixturesPath, 'api', 'exit-closes-all-windows-app');
const electronPath = process.execPath;
appProcess = cp.spawn(electronPath, [appPath]);
const [code, signal] = await emittedOnce(appProcess, 'exit');
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 = process.execPath;
const appPath = path.join(fixturesPath, 'api', 'singleton');
appProcess = cp.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.
if (appProcess && appProcess.stdout) {
appProcess.stdout.on('data', () => appProcess!.kill());
}
const [code, signal] = await emittedOnce(appProcess, 'exit');
const message = `code:\n${code}\nsignal:\n${signal}`;
expect(code).to.equal(0, message);
expect(signal).to.equal(null, message);
});
});
ifdescribe(process.platform === 'darwin')('app.setActivationPolicy', () => {
it('throws an error on invalid application policies', () => {
expect(() => {
app.setActivationPolicy('terrible' as any);
}).to.throw(/Invalid activation policy: must be one of 'regular', 'accessory', or 'prohibited'/);
});
});
describe('app.requestSingleInstanceLock', () => {
interface SingleInstanceLockTestArgs {
args: string[];
expectedAdditionalData: unknown;
}
it('prevents the second launch of app', async function () {
this.timeout(120000);
const appPath = path.join(fixturesPath, 'api', 'singleton-data');
const first = cp.spawn(process.execPath, [appPath]);
await emittedOnce(first.stdout, 'data');
// Start second app when received output.
const second = cp.spawn(process.execPath, [appPath]);
const [code2] = await emittedOnce(second, 'exit');
expect(code2).to.equal(1);
const [code1] = await emittedOnce(first, 'exit');
expect(code1).to.equal(0);
});
it('returns true when setting non-existent user data folder', async function () {
const appPath = path.join(fixturesPath, 'api', 'singleton-userdata');
const instance = cp.spawn(process.execPath, [appPath]);
const [code] = await emittedOnce(instance, 'exit');
expect(code).to.equal(0);
});
async function testArgumentPassing (testArgs: SingleInstanceLockTestArgs) {
const appPath = path.join(fixturesPath, 'api', 'singleton-data');
const first = cp.spawn(process.execPath, [appPath, ...testArgs.args]);
const firstExited = emittedOnce(first, 'exit');
// Wait for the first app to boot.
const firstStdoutLines = first.stdout.pipe(split());
while ((await emittedOnce(firstStdoutLines, 'data')).toString() !== 'started') {
// wait.
}
const additionalDataPromise = emittedOnce(firstStdoutLines, 'data');
const secondInstanceArgs = [process.execPath, appPath, ...testArgs.args, '--some-switch', 'some-arg'];
const second = cp.spawn(secondInstanceArgs[0], secondInstanceArgs.slice(1));
const secondExited = emittedOnce(second, 'exit');
const [code2] = await secondExited;
expect(code2).to.equal(1);
const [code1] = await firstExited;
expect(code1).to.equal(0);
const dataFromSecondInstance = await additionalDataPromise;
const [args, additionalData] = dataFromSecondInstance[0].toString('ascii').split('||');
const secondInstanceArgsReceived: string[] = JSON.parse(args.toString('ascii'));
const secondInstanceDataReceived = JSON.parse(additionalData.toString('ascii'));
// Ensure secondInstanceArgs is a subset of secondInstanceArgsReceived
for (const arg of secondInstanceArgs) {
expect(secondInstanceArgsReceived).to.include(arg,
`argument ${arg} is missing from received second args`);
}
expect(secondInstanceDataReceived).to.be.deep.equal(testArgs.expectedAdditionalData,
`received data ${JSON.stringify(secondInstanceDataReceived)} is not equal to expected data ${JSON.stringify(testArgs.expectedAdditionalData)}.`);
}
it('passes arguments to the second-instance event no additional data', async () => {
await testArgumentPassing({
args: [],
expectedAdditionalData: null
});
});
it('sends and receives JSON object data', async () => {
const expectedAdditionalData = {
level: 1,
testkey: 'testvalue1',
inner: {
level: 2,
testkey: 'testvalue2'
}
};
await testArgumentPassing({
args: ['--send-data'],
expectedAdditionalData
});
});
it('sends and receives numerical data', async () => {
await testArgumentPassing({
args: ['--send-data', '--data-content=2'],
expectedAdditionalData: 2
});
});
it('sends and receives string data', async () => {
await testArgumentPassing({
args: ['--send-data', '--data-content="data"'],
expectedAdditionalData: 'data'
});
});
it('sends and receives boolean data', async () => {
await testArgumentPassing({
args: ['--send-data', '--data-content=false'],
expectedAdditionalData: false
});
});
it('sends and receives array data', async () => {
await testArgumentPassing({
args: ['--send-data', '--data-content=[2, 3, 4]'],
expectedAdditionalData: [2, 3, 4]
});
});
it('sends and receives mixed array data', async () => {
await testArgumentPassing({
args: ['--send-data', '--data-content=["2", true, 4]'],
expectedAdditionalData: ['2', true, 4]
});
});
it('sends and receives null data', async () => {
await testArgumentPassing({
args: ['--send-data', '--data-content=null'],
expectedAdditionalData: null
});
});
it('cannot send or receive undefined data', async () => {
try {
await testArgumentPassing({
args: ['--send-ack', '--ack-content="undefined"', '--prevent-default', '--send-data', '--data-content="undefined"'],
expectedAdditionalData: undefined
});
assert(false);
} catch (e) {
// This is expected.
}
});
});
describe('app.relaunch', () => {
let server: net.Server | null = 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) === '--first' && state === 'none') {
state = 'first-launch';
} else if (String(data) === '--second' && state === 'first-launch') {
state = 'second-launch';
} else if (String(data) === '--third' && state === 'second-launch') {
done();
} else {
done(`Unexpected state: "${state}", data: "${data}"`);
}
});
});
const appPath = path.join(fixturesPath, 'api', 'relaunch');
const child = cp.spawn(process.execPath, [appPath, '--first']);
child.stdout.on('data', (c) => console.log(c.toString()));
child.stderr.on('data', (c) => console.log(c.toString()));
child.on('exit', (code, signal) => {
if (code !== 0) {
console.log(`Process exited with code "${code}" signal "${signal}"`);
}
});
});
});
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');
});
});
describe('certificate-error event', () => {
afterEach(closeAllWindows);
it('is emitted when visiting a server with a self-signed cert', async () => {
const w = new BrowserWindow({ show: false });
w.loadURL(secureUrl);
await emittedOnce(app, 'certificate-error');
});
describe('when denied', () => {
before(() => {
app.on('certificate-error', (event, webContents, url, error, certificate, callback) => {
callback(false);
});
});
after(() => {
app.removeAllListeners('certificate-error');
});
it('causes did-fail-load', async () => {
const w = new BrowserWindow({ show: false });
w.loadURL(secureUrl);
await emittedOnce(w.webContents, 'did-fail-load');
});
});
});
// 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,
// webPreferences: {
// nodeIntegration: true
// }
// })
// 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: BrowserWindow = null as any;
afterEach(() => closeWindow(w).then(() => { w = null as any; }));
it('should emit browser-window-focus event when window is focused', async () => {
const emitted = emittedOnce(app, 'browser-window-focus');
w = new BrowserWindow({ show: false });
w.emit('focus');
const [, window] = await emitted;
expect(window.id).to.equal(w.id);
});
it('should emit browser-window-blur event when window is blurred', async () => {
const emitted = emittedOnce(app, 'browser-window-blur');
w = new BrowserWindow({ show: false });
w.emit('blur');
const [, window] = await emitted;
expect(window.id).to.equal(w.id);
});
it('should emit browser-window-created event when window is created', async () => {
const emitted = emittedOnce(app, 'browser-window-created');
w = new BrowserWindow({ show: false });
const [, window] = await emitted;
expect(window.id).to.equal(w.id);
});
it('should emit web-contents-created event when a webContents is created', async () => {
const emitted = emittedOnce(app, 'web-contents-created');
w = new BrowserWindow({ show: false });
const [, webContents] = await emitted;
expect(webContents.id).to.equal(w.webContents.id);
});
// FIXME: re-enable this test on win32.
ifit(process.platform !== 'win32')('should emit renderer-process-crashed event when renderer crashes', async () => {
w = new BrowserWindow({
show: false,
webPreferences: {
nodeIntegration: true,
contextIsolation: false
}
});
await w.loadURL('about:blank');
const emitted = emittedOnce(app, 'renderer-process-crashed');
w.webContents.executeJavaScript('process.crash()');
const [, webContents] = await emitted;
expect(webContents).to.equal(w.webContents);
});
// FIXME: re-enable this test on win32.
ifit(process.platform !== 'win32')('should emit render-process-gone event when renderer crashes', async () => {
w = new BrowserWindow({
show: false,
webPreferences: {
nodeIntegration: true,
contextIsolation: false
}
});
await w.loadURL('about:blank');
const emitted = emittedOnce(app, 'render-process-gone');
w.webContents.executeJavaScript('process.crash()');
const [, webContents, details] = await emitted;
expect(webContents).to.equal(w.webContents);
expect(details.reason).to.be.oneOf(['crashed', 'abnormal-exit']);
});
});
describe('app.badgeCount', () => {
const platformIsNotSupported =
(process.platform === 'win32') ||
(process.platform === 'linux' && !app.isUnityRunning());
const expectedBadgeCount = 42;
after(() => { app.badgeCount = 0; });
ifdescribe(!platformIsNotSupported)('on supported platform', () => {
describe('with properties', () => {
it('sets a badge count', function () {
app.badgeCount = expectedBadgeCount;
expect(app.badgeCount).to.equal(expectedBadgeCount);
});
});
describe('with functions', () => {
it('sets a numerical badge count', function () {
app.setBadgeCount(expectedBadgeCount);
expect(app.getBadgeCount()).to.equal(expectedBadgeCount);
});
it('sets an non numeric (dot) badge count', function () {
app.setBadgeCount();
// Badge count should be zero when non numeric (dot) is requested
expect(app.getBadgeCount()).to.equal(0);
});
});
});
ifdescribe(process.platform !== 'win32' && platformIsNotSupported)('on unsupported platform', () => {
describe('with properties', () => {
it('does not set a badge count', function () {
app.badgeCount = 9999;
expect(app.badgeCount).to.equal(0);
});
});
describe('with functions', () => {
it('does not set a badge count)', function () {
app.setBadgeCount(9999);
expect(app.getBadgeCount()).to.equal(0);
});
});
});
});
ifdescribe(process.platform !== 'linux' && !process.mas)('app.get/setLoginItemSettings API', function () {
const updateExe = path.resolve(path.dirname(process.execPath), '..', 'Update.exe');
const processStartArgs = [
'--processStart', `"${path.basename(process.execPath)}"`,
'--process-start-args', '"--hidden"'
];
const regAddArgs = [
'ADD',
'HKCU\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\StartupApproved\\Run',
'/v',
'additionalEntry',
'/t',
'REG_BINARY',
'/f',
'/d'
];
beforeEach(() => {
app.setLoginItemSettings({ openAtLogin: false });
app.setLoginItemSettings({ openAtLogin: false, path: updateExe, args: processStartArgs });
app.setLoginItemSettings({ name: 'additionalEntry', openAtLogin: false });
});
afterEach(() => {
app.setLoginItemSettings({ openAtLogin: false });
app.setLoginItemSettings({ openAtLogin: false, path: updateExe, args: processStartArgs });
app.setLoginItemSettings({ name: 'additionalEntry', openAtLogin: false });
});
ifit(process.platform !== 'win32')('sets and returns the app as a login item', function () {
app.setLoginItemSettings({ openAtLogin: true });
expect(app.getLoginItemSettings()).to.deep.equal({
openAtLogin: true,
openAsHidden: false,
wasOpenedAtLogin: false,
wasOpenedAsHidden: false,
restoreState: false
});
});
ifit(process.platform === 'win32')('sets and returns the app as a login item (windows)', function () {
app.setLoginItemSettings({ openAtLogin: true, enabled: true });
expect(app.getLoginItemSettings()).to.deep.equal({
openAtLogin: true,
openAsHidden: false,
wasOpenedAtLogin: false,
wasOpenedAsHidden: false,
restoreState: false,
executableWillLaunchAtLogin: true,
launchItems: [{
name: 'electron.app.Electron',
path: process.execPath,
args: [],
scope: 'user',
enabled: true
}]
});
app.setLoginItemSettings({ openAtLogin: false });
app.setLoginItemSettings({ openAtLogin: true, enabled: false });
expect(app.getLoginItemSettings()).to.deep.equal({
openAtLogin: true,
openAsHidden: false,
wasOpenedAtLogin: false,
wasOpenedAsHidden: false,
restoreState: false,
executableWillLaunchAtLogin: false,
launchItems: [{
name: 'electron.app.Electron',
path: process.execPath,
args: [],
scope: 'user',
enabled: false
}]
});
});
ifit(process.platform !== 'win32')('adds a login item that loads in hidden mode', function () {
app.setLoginItemSettings({ openAtLogin: true, openAsHidden: true });
expect(app.getLoginItemSettings()).to.deep.equal({
openAtLogin: true,
openAsHidden: process.platform === 'darwin' && !process.mas, // Only available on macOS
wasOpenedAtLogin: false,
wasOpenedAsHidden: false,
restoreState: false
});
});
ifit(process.platform === 'win32')('adds a login item that loads in hidden mode (windows)', function () {
app.setLoginItemSettings({ openAtLogin: true, openAsHidden: true });
expect(app.getLoginItemSettings()).to.deep.equal({
openAtLogin: true,
openAsHidden: false,
wasOpenedAtLogin: false,
wasOpenedAsHidden: false,
restoreState: false,
executableWillLaunchAtLogin: true,
launchItems: [{
name: 'electron.app.Electron',
path: process.execPath,
args: [],
scope: 'user',
enabled: true
}]
});
});
it('correctly sets and unsets the LoginItem', function () {
expect(app.getLoginItemSettings().openAtLogin).to.equal(false);
app.setLoginItemSettings({ openAtLogin: true });
expect(app.getLoginItemSettings().openAtLogin).to.equal(true);
app.setLoginItemSettings({ openAtLogin: false });
expect(app.getLoginItemSettings().openAtLogin).to.equal(false);
});
it('correctly sets and unsets the LoginItem as hidden', function () {
if (process.platform !== 'darwin') this.skip();
expect(app.getLoginItemSettings().openAtLogin).to.equal(false);
expect(app.getLoginItemSettings().openAsHidden).to.equal(false);
app.setLoginItemSettings({ openAtLogin: true, openAsHidden: true });
expect(app.getLoginItemSettings().openAtLogin).to.equal(true);
expect(app.getLoginItemSettings().openAsHidden).to.equal(true);
app.setLoginItemSettings({ openAtLogin: true, openAsHidden: false });
expect(app.getLoginItemSettings().openAtLogin).to.equal(true);
expect(app.getLoginItemSettings().openAsHidden).to.equal(false);
});
ifit(process.platform === 'win32')('allows you to pass a custom executable and arguments', function () {
app.setLoginItemSettings({ openAtLogin: true, path: updateExe, args: processStartArgs, enabled: true });
expect(app.getLoginItemSettings().openAtLogin).to.equal(false);
const openAtLoginTrueEnabledTrue = app.getLoginItemSettings({
path: updateExe,
args: processStartArgs
});
expect(openAtLoginTrueEnabledTrue.openAtLogin).to.equal(true);
expect(openAtLoginTrueEnabledTrue.executableWillLaunchAtLogin).to.equal(true);
app.setLoginItemSettings({ openAtLogin: true, path: updateExe, args: processStartArgs, enabled: false });
const openAtLoginTrueEnabledFalse = app.getLoginItemSettings({
path: updateExe,
args: processStartArgs
});
expect(openAtLoginTrueEnabledFalse.openAtLogin).to.equal(true);
expect(openAtLoginTrueEnabledFalse.executableWillLaunchAtLogin).to.equal(false);
app.setLoginItemSettings({ openAtLogin: false, path: updateExe, args: processStartArgs, enabled: false });
const openAtLoginFalseEnabledFalse = app.getLoginItemSettings({
path: updateExe,
args: processStartArgs
});
expect(openAtLoginFalseEnabledFalse.openAtLogin).to.equal(false);
expect(openAtLoginFalseEnabledFalse.executableWillLaunchAtLogin).to.equal(false);
});
ifit(process.platform === 'win32')('allows you to pass a custom name', function () {
app.setLoginItemSettings({ openAtLogin: true });
app.setLoginItemSettings({ openAtLogin: true, name: 'additionalEntry', enabled: false });
expect(app.getLoginItemSettings()).to.deep.equal({
openAtLogin: true,
openAsHidden: false,
wasOpenedAtLogin: false,
wasOpenedAsHidden: false,
restoreState: false,
executableWillLaunchAtLogin: true,
launchItems: [{
name: 'additionalEntry',
path: process.execPath,
args: [],
scope: 'user',
enabled: false
}, {
name: 'electron.app.Electron',
path: process.execPath,
args: [],
scope: 'user',
enabled: true
}]
});
app.setLoginItemSettings({ openAtLogin: false, name: 'additionalEntry' });
expect(app.getLoginItemSettings()).to.deep.equal({
openAtLogin: true,
openAsHidden: false,
wasOpenedAtLogin: false,
wasOpenedAsHidden: false,
restoreState: false,
executableWillLaunchAtLogin: true,
launchItems: [{
name: 'electron.app.Electron',
path: process.execPath,
args: [],
scope: 'user',
enabled: true
}]
});
});
ifit(process.platform === 'win32')('finds launch items independent of args', function () {
app.setLoginItemSettings({ openAtLogin: true, args: ['arg1'] });
app.setLoginItemSettings({ openAtLogin: true, name: 'additionalEntry', enabled: false, args: ['arg2'] });
expect(app.getLoginItemSettings()).to.deep.equal({
openAtLogin: false,
openAsHidden: false,
wasOpenedAtLogin: false,
wasOpenedAsHidden: false,
restoreState: false,
executableWillLaunchAtLogin: true,
launchItems: [{
name: 'additionalEntry',
path: process.execPath,
args: ['arg2'],
scope: 'user',
enabled: false
}, {
name: 'electron.app.Electron',
path: process.execPath,
args: ['arg1'],
scope: 'user',
enabled: true
}]
});
});
ifit(process.platform === 'win32')('finds launch items independent of path quotation or casing', function () {
const expectation = {
openAtLogin: false,
openAsHidden: false,
wasOpenedAtLogin: false,
wasOpenedAsHidden: false,
restoreState: false,
executableWillLaunchAtLogin: true,
launchItems: [{
name: 'additionalEntry',
path: 'C:\\electron\\myapp.exe',
args: ['arg1'],
scope: 'user',
enabled: true
}]
};
app.setLoginItemSettings({ openAtLogin: true, name: 'additionalEntry', enabled: true, path: 'C:\\electron\\myapp.exe', args: ['arg1'] });
expect(app.getLoginItemSettings({ path: '"C:\\electron\\MYAPP.exe"' })).to.deep.equal(expectation);
app.setLoginItemSettings({ openAtLogin: false, name: 'additionalEntry' });
app.setLoginItemSettings({ openAtLogin: true, name: 'additionalEntry', enabled: true, path: '"C:\\electron\\MYAPP.exe"', args: ['arg1'] });
expect(app.getLoginItemSettings({ path: 'C:\\electron\\myapp.exe' })).to.deep.equal({
...expectation,
launchItems: [
{
name: 'additionalEntry',
path: 'C:\\electron\\MYAPP.exe',
args: ['arg1'],
scope: 'user',
enabled: true
}
]
});
});
ifit(process.platform === 'win32')('detects disabled by TaskManager', async function () {
app.setLoginItemSettings({ openAtLogin: true, name: 'additionalEntry', enabled: true, args: ['arg1'] });
const appProcess = cp.spawn('reg', [...regAddArgs, '030000000000000000000000']);
await emittedOnce(appProcess, 'exit');
expect(app.getLoginItemSettings()).to.deep.equal({
openAtLogin: false,
openAsHidden: false,
wasOpenedAtLogin: false,
wasOpenedAsHidden: false,
restoreState: false,
executableWillLaunchAtLogin: false,
launchItems: [{
name: 'additionalEntry',
path: process.execPath,
args: ['arg1'],
scope: 'user',
enabled: false
}]
});
});
ifit(process.platform === 'win32')('detects enabled by TaskManager', async function () {
const expectation = {
openAtLogin: false,
openAsHidden: false,
wasOpenedAtLogin: false,
wasOpenedAsHidden: false,
restoreState: false,
executableWillLaunchAtLogin: true,
launchItems: [{
name: 'additionalEntry',
path: process.execPath,
args: ['arg1'],
scope: 'user',
enabled: true
}]
};
app.setLoginItemSettings({ openAtLogin: true, name: 'additionalEntry', enabled: false, args: ['arg1'] });
let appProcess = cp.spawn('reg', [...regAddArgs, '020000000000000000000000']);
await emittedOnce(appProcess, 'exit');
expect(app.getLoginItemSettings()).to.deep.equal(expectation);
app.setLoginItemSettings({ openAtLogin: true, name: 'additionalEntry', enabled: false, args: ['arg1'] });
appProcess = cp.spawn('reg', [...regAddArgs, '000000000000000000000000']);
await emittedOnce(appProcess, 'exit');
expect(app.getLoginItemSettings()).to.deep.equal(expectation);
});
});
ifdescribe(process.platform !== 'linux')('accessibilitySupportEnabled property', () => {
it('with properties', () => {
it('can set accessibility support enabled', () => {
expect(app.accessibilitySupportEnabled).to.eql(false);
app.accessibilitySupportEnabled = true;
expect(app.accessibilitySupportEnabled).to.eql(true);
});
});
it('with functions', () => {
it('can set accessibility support enabled', () => {
expect(app.isAccessibilitySupportEnabled()).to.eql(false);
app.setAccessibilitySupportEnabled(true);
expect(app.isAccessibilitySupportEnabled()).to.eql(true);
});
});
});
describe('getAppPath', () => {
it('works for directories with package.json', async () => {
const { appPath } = await runTestApp('app-path');
expect(appPath).to.equal(path.resolve(fixturesPath, 'api/app-path'));
});
it('works for directories with index.js', async () => {
const { appPath } = await runTestApp('app-path/lib');
expect(appPath).to.equal(path.resolve(fixturesPath, 'api/app-path/lib'));
});
it('works for files without extension', async () => {
const { appPath } = await runTestApp('app-path/lib/index');
expect(appPath).to.equal(path.resolve(fixturesPath, 'api/app-path/lib'));
});
it('works for files', async () => {
const { appPath } = await runTestApp('app-path/lib/index.js');
expect(appPath).to.equal(path.resolve(fixturesPath, 'api/app-path/lib'));
});
});
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' as any);
}).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);
});
if (process.platform === 'win32') {
it('gets the folder for recent files', () => {
const recent = app.getPath('recent');
// We expect that one of our test machines have overridden this
// to be something crazy, it'll always include the word "Recent"
// unless people have been registry-hacking like crazy