feat: update app.{set|get}LoginItemSettings(settings) (#37244)

* feat: update app.{set|get}LoginItemSettings(settings)

* test: fixup and add tests

* docs: add type link

* chore: name -> serviceName
This commit is contained in:
Shelley Vohr 2023-10-16 18:25:11 +02:00 committed by GitHub
parent 6d0d350e13
commit f7b1c75c72
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 321 additions and 89 deletions

View file

@ -12,6 +12,7 @@ import { ifdescribe, ifit, listen, waitUntil } from './lib/spec-helpers';
import { expectDeprecationMessages } from './lib/deprecate-helpers';
import { once } from 'node:events';
import split = require('split')
import * as semver from 'semver';
const fixturesPath = path.resolve(__dirname, 'fixtures');
@ -616,6 +617,9 @@ describe('app module', () => {
});
ifdescribe(process.platform !== 'linux' && !process.mas)('app.get/setLoginItemSettings API', function () {
const isMac = process.platform === 'darwin';
const isWin = process.platform === 'win32';
const updateExe = path.resolve(path.dirname(process.execPath), '..', 'Update.exe');
const processStartArgs = [
'--processStart', `"${path.basename(process.execPath)}"`,
@ -631,6 +635,8 @@ describe('app module', () => {
'/f',
'/d'
];
const productVersion = isMac ? cp.execSync('sw_vers -productVersion').toString().trim() : '';
const isVenturaOrHigher = semver.gt(semver.coerce(productVersion) || '0.0.0', '13.0.0');
beforeEach(() => {
app.setLoginItemSettings({ openAtLogin: false });
@ -644,18 +650,19 @@ describe('app module', () => {
app.setLoginItemSettings({ name: 'additionalEntry', openAtLogin: false });
});
ifit(process.platform !== 'win32')('sets and returns the app as a login item', function () {
ifit(!isWin)('sets and returns the app as a login item', () => {
app.setLoginItemSettings({ openAtLogin: true });
expect(app.getLoginItemSettings()).to.deep.equal({
openAtLogin: true,
openAsHidden: false,
wasOpenedAtLogin: false,
wasOpenedAsHidden: false,
restoreState: false
});
const settings = app.getLoginItemSettings();
expect(settings.openAtLogin).to.equal(true);
expect(settings.openAsHidden).to.equal(false);
expect(settings.wasOpenedAtLogin).to.equal(false);
expect(settings.wasOpenedAsHidden).to.equal(false);
expect(settings.restoreState).to.equal(false);
if (isVenturaOrHigher) expect(settings.status).to.equal('enabled');
});
ifit(process.platform === 'win32')('sets and returns the app as a login item (windows)', function () {
ifit(isWin)('sets and returns the app as a login item (windows)', () => {
app.setLoginItemSettings({ openAtLogin: true, enabled: true });
expect(app.getLoginItemSettings()).to.deep.equal({
openAtLogin: true,
@ -692,18 +699,21 @@ describe('app module', () => {
});
});
ifit(process.platform !== 'win32')('adds a login item that loads in hidden mode', function () {
ifit(!isWin)('adds a login item that loads in hidden mode', () => {
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
});
const settings = app.getLoginItemSettings();
expect(settings.openAtLogin).to.equal(true);
const hasOpenAsHidden = process.platform === 'darwin' && !isVenturaOrHigher;
expect(settings.openAsHidden).to.equal(hasOpenAsHidden);
expect(settings.wasOpenedAtLogin).to.equal(false);
expect(settings.wasOpenedAsHidden).to.equal(false);
expect(settings.restoreState).to.equal(false);
if (isVenturaOrHigher) expect(settings.status).to.equal('enabled');
});
ifit(process.platform === 'win32')('adds a login item that loads in hidden mode (windows)', function () {
ifit(isWin)('adds a login item that loads in hidden mode (windows)', () => {
app.setLoginItemSettings({ openAtLogin: true, openAsHidden: true });
expect(app.getLoginItemSettings()).to.deep.equal({
openAtLogin: true,
@ -722,7 +732,7 @@ describe('app module', () => {
});
});
it('correctly sets and unsets the LoginItem', function () {
it('correctly sets and unsets the LoginItem', () => {
expect(app.getLoginItemSettings().openAtLogin).to.equal(false);
app.setLoginItemSettings({ openAtLogin: true });
@ -732,20 +742,76 @@ describe('app module', () => {
expect(app.getLoginItemSettings().openAtLogin).to.equal(false);
});
ifit(process.platform === 'darwin')('correctly sets and unsets the LoginItem as hidden', function () {
ifit(isMac)('correctly sets and unsets the LoginItem as hidden', () => {
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);
expect(app.getLoginItemSettings().openAsHidden).to.equal(!isVenturaOrHigher);
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 () {
ifdescribe(isMac)('using SMAppService', () => {
ifit(isVenturaOrHigher)('can set a login item', () => {
app.setLoginItemSettings({
openAtLogin: true,
type: 'mainAppService'
});
expect(app.getLoginItemSettings()).to.deep.equal({
status: 'enabled',
openAtLogin: true,
openAsHidden: false,
restoreState: false,
wasOpenedAtLogin: false,
wasOpenedAsHidden: false
});
});
ifit(isVenturaOrHigher)('throws when setting non-default type with no name', () => {
expect(() => {
app.setLoginItemSettings({
openAtLogin: true,
type: 'daemonService'
});
}).to.throw(/'name' is required when type is not mainAppService/);
});
ifit(isVenturaOrHigher)('throws when getting non-default type with no name', () => {
expect(() => {
app.getLoginItemSettings({
type: 'daemonService'
});
}).to.throw(/'name' is required when type is not mainAppService/);
});
ifit(isVenturaOrHigher)('can unset a login item', () => {
app.setLoginItemSettings({
openAtLogin: true,
type: 'mainAppService'
});
app.setLoginItemSettings({
openAtLogin: false,
type: 'mainAppService'
});
expect(app.getLoginItemSettings()).to.deep.equal({
status: 'not-registered',
openAtLogin: false,
openAsHidden: false,
restoreState: false,
wasOpenedAtLogin: false,
wasOpenedAsHidden: false
});
});
});
ifit(isWin)('allows you to pass a custom executable and arguments', () => {
app.setLoginItemSettings({ openAtLogin: true, path: updateExe, args: processStartArgs, enabled: true });
expect(app.getLoginItemSettings().openAtLogin).to.equal(false);
const openAtLoginTrueEnabledTrue = app.getLoginItemSettings({
@ -775,7 +841,7 @@ describe('app module', () => {
expect(openAtLoginFalseEnabledFalse.executableWillLaunchAtLogin).to.equal(false);
});
ifit(process.platform === 'win32')('allows you to pass a custom name', function () {
ifit(isWin)('allows you to pass a custom name', () => {
app.setLoginItemSettings({ openAtLogin: true });
app.setLoginItemSettings({ openAtLogin: true, name: 'additionalEntry', enabled: false });
expect(app.getLoginItemSettings()).to.deep.equal({
@ -818,7 +884,7 @@ describe('app module', () => {
});
});
ifit(process.platform === 'win32')('finds launch items independent of args', function () {
ifit(isWin)('finds launch items independent of args', () => {
app.setLoginItemSettings({ openAtLogin: true, args: ['arg1'] });
app.setLoginItemSettings({ openAtLogin: true, name: 'additionalEntry', enabled: false, args: ['arg2'] });
expect(app.getLoginItemSettings()).to.deep.equal({
@ -844,7 +910,7 @@ describe('app module', () => {
});
});
ifit(process.platform === 'win32')('finds launch items independent of path quotation or casing', function () {
ifit(isWin)('finds launch items independent of path quotation or casing', () => {
const expectation = {
openAtLogin: false,
openAsHidden: false,
@ -880,7 +946,7 @@ describe('app module', () => {
});
});
ifit(process.platform === 'win32')('detects disabled by TaskManager', async function () {
ifit(isWin)('detects disabled by TaskManager', async () => {
app.setLoginItemSettings({ openAtLogin: true, name: 'additionalEntry', enabled: true, args: ['arg1'] });
const appProcess = cp.spawn('reg', [...regAddArgs, '030000000000000000000000']);
await once(appProcess, 'exit');
@ -901,7 +967,7 @@ describe('app module', () => {
});
});
ifit(process.platform === 'win32')('detects enabled by TaskManager', async function () {
ifit(isWin)('detects enabled by TaskManager', async () => {
const expectation = {
openAtLogin: false,
openAsHidden: false,