2020-03-20 20:28:31 +00:00
|
|
|
import { expect } from 'chai';
|
2020-04-07 00:04:09 +00:00
|
|
|
import { systemPreferences } from 'electron/main';
|
2020-03-20 20:28:31 +00:00
|
|
|
import { ifdescribe } from './spec-helpers';
|
2019-05-20 17:04:18 +00:00
|
|
|
|
2017-12-12 18:39:38 +00:00
|
|
|
describe('systemPreferences module', () => {
|
2019-07-17 00:14:32 +00:00
|
|
|
ifdescribe(process.platform === 'win32')('systemPreferences.getAccentColor', () => {
|
2017-10-27 00:58:48 +00:00
|
|
|
it('should return a non-empty string', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const accentColor = systemPreferences.getAccentColor();
|
|
|
|
expect(accentColor).to.be.a('string').that.is.not.empty('accent color');
|
|
|
|
});
|
|
|
|
});
|
2016-06-02 01:56:25 +00:00
|
|
|
|
2019-07-17 00:14:32 +00:00
|
|
|
ifdescribe(process.platform === 'win32')('systemPreferences.getColor(id)', () => {
|
2017-10-27 00:58:48 +00:00
|
|
|
it('throws an error when the id is invalid', () => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(() => {
|
2020-03-20 20:28:31 +00:00
|
|
|
systemPreferences.getColor('not-a-color' as any);
|
|
|
|
}).to.throw('Unknown color: not-a-color');
|
|
|
|
});
|
2016-10-10 22:06:25 +00:00
|
|
|
|
2017-10-27 00:58:48 +00:00
|
|
|
it('returns a hex RGB color string', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(systemPreferences.getColor('window')).to.match(/^#[0-9A-F]{6}$/i);
|
|
|
|
});
|
|
|
|
});
|
2016-10-10 22:06:25 +00:00
|
|
|
|
2019-07-17 00:14:32 +00:00
|
|
|
ifdescribe(process.platform === 'darwin')('systemPreferences.registerDefaults(defaults)', () => {
|
2017-12-12 18:08:09 +00:00
|
|
|
it('registers defaults', () => {
|
|
|
|
const defaultsMap = [
|
|
|
|
{ key: 'one', type: 'string', value: 'ONE' },
|
|
|
|
{ key: 'two', value: 2, type: 'integer' },
|
|
|
|
{ key: 'three', value: [1, 2, 3], type: 'array' }
|
2020-03-20 20:28:31 +00:00
|
|
|
];
|
2017-12-12 18:08:09 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const defaultsDict: Record<string, any> = {};
|
|
|
|
defaultsMap.forEach(row => { defaultsDict[row.key] = row.value; });
|
2017-12-12 18:08:09 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
systemPreferences.registerDefaults(defaultsDict);
|
2017-12-12 18:08:09 +00:00
|
|
|
|
|
|
|
for (const userDefault of defaultsMap) {
|
2020-03-20 20:28:31 +00:00
|
|
|
const { key, value: expectedValue, type } = userDefault;
|
|
|
|
const actualValue = systemPreferences.getUserDefault(key, type as any);
|
|
|
|
expect(actualValue).to.deep.equal(expectedValue);
|
2017-12-12 18:08:09 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2017-12-12 18:08:09 +00:00
|
|
|
|
|
|
|
it('throws when bad defaults are passed', () => {
|
|
|
|
const badDefaults = [
|
|
|
|
1,
|
|
|
|
null,
|
|
|
|
new Date(),
|
2020-03-20 15:12:18 +00:00
|
|
|
{ one: null }
|
2020-03-20 20:28:31 +00:00
|
|
|
];
|
2017-12-12 18:08:09 +00:00
|
|
|
|
|
|
|
for (const badDefault of badDefaults) {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(() => {
|
2020-03-20 20:28:31 +00:00
|
|
|
systemPreferences.registerDefaults(badDefault as any);
|
2020-07-28 18:03:30 +00:00
|
|
|
}).to.throw('Error processing argument at index 0, conversion failure from ');
|
2017-12-12 18:08:09 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
});
|
2017-12-12 18:08:09 +00:00
|
|
|
|
2019-07-17 00:14:32 +00:00
|
|
|
ifdescribe(process.platform === 'darwin')('systemPreferences.getUserDefault(key, type)', () => {
|
2017-10-27 00:58:48 +00:00
|
|
|
it('returns values for known user defaults', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const locale = systemPreferences.getUserDefault('AppleLocale', 'string');
|
|
|
|
expect(locale).to.be.a('string').that.is.not.empty('locale');
|
2016-06-02 01:56:25 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const languages = systemPreferences.getUserDefault('AppleLanguages', 'array');
|
|
|
|
expect(languages).to.be.an('array').that.is.not.empty('languages');
|
|
|
|
});
|
2016-11-17 00:28:57 +00:00
|
|
|
|
2017-10-27 00:58:48 +00:00
|
|
|
it('returns values for unknown user defaults', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'boolean')).to.equal(false);
|
|
|
|
expect(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'integer')).to.equal(0);
|
|
|
|
expect(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'float')).to.equal(0);
|
|
|
|
expect(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'double')).to.equal(0);
|
|
|
|
expect(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'string')).to.equal('');
|
|
|
|
expect(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'url')).to.equal('');
|
|
|
|
expect(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'badtype' as any)).to.be.undefined('user default');
|
|
|
|
expect(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'array')).to.deep.equal([]);
|
|
|
|
expect(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'dictionary')).to.deep.equal({});
|
|
|
|
});
|
|
|
|
});
|
2016-10-06 22:27:24 +00:00
|
|
|
|
2019-07-17 00:14:32 +00:00
|
|
|
ifdescribe(process.platform === 'darwin')('systemPreferences.setUserDefault(key, type, value)', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const KEY = 'SystemPreferencesTest';
|
2016-11-28 23:08:22 +00:00
|
|
|
const TEST_CASES = [
|
|
|
|
['string', 'abc'],
|
|
|
|
['boolean', true],
|
|
|
|
['float', 2.5],
|
|
|
|
['double', 10.1],
|
|
|
|
['integer', 11],
|
|
|
|
['url', 'https://github.com/electron'],
|
|
|
|
['array', [1, 2, 3]],
|
2020-03-20 15:12:18 +00:00
|
|
|
['dictionary', { a: 1, b: 2 }]
|
2020-03-20 20:28:31 +00:00
|
|
|
];
|
2016-11-28 23:08:22 +00:00
|
|
|
|
|
|
|
it('sets values', () => {
|
|
|
|
for (const [type, value] of TEST_CASES) {
|
2020-03-20 20:28:31 +00:00
|
|
|
systemPreferences.setUserDefault(KEY, type as any, value as any);
|
|
|
|
const retrievedValue = systemPreferences.getUserDefault(KEY, type as any);
|
|
|
|
expect(retrievedValue).to.deep.equal(value);
|
2016-11-28 23:08:22 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2016-11-28 23:08:22 +00:00
|
|
|
|
|
|
|
it('throws when type and value conflict', () => {
|
|
|
|
for (const [type, value] of TEST_CASES) {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(() => {
|
2020-03-20 20:28:31 +00:00
|
|
|
systemPreferences.setUserDefault(KEY, type as any, typeof value === 'string' ? {} : 'foo' as any);
|
|
|
|
}).to.throw(`Unable to convert value to: ${type}`);
|
2016-11-28 23:08:22 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2016-11-28 23:08:22 +00:00
|
|
|
|
|
|
|
it('throws when type is not valid', () => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(() => {
|
2020-04-09 22:55:47 +00:00
|
|
|
systemPreferences.setUserDefault(KEY, 'abc' as any, 'foo');
|
2020-03-20 20:28:31 +00:00
|
|
|
}).to.throw('Invalid type: abc');
|
|
|
|
});
|
|
|
|
});
|
2016-11-28 23:08:22 +00:00
|
|
|
|
2022-04-13 20:02:33 +00:00
|
|
|
ifdescribe(process.platform === 'darwin')('systemPreferences.subscribeNotification(event, callback)', () => {
|
|
|
|
it('throws an error if invalid arguments are passed', () => {
|
|
|
|
const badArgs = [123, {}, ['hi', 'bye'], new Date()];
|
|
|
|
for (const bad of badArgs) {
|
|
|
|
expect(() => {
|
|
|
|
systemPreferences.subscribeNotification(bad as any, () => {});
|
|
|
|
}).to.throw('Must pass null or a string');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
ifdescribe(process.platform === 'darwin')('systemPreferences.subscribeLocalNotification(event, callback)', () => {
|
|
|
|
it('throws an error if invalid arguments are passed', () => {
|
|
|
|
const badArgs = [123, {}, ['hi', 'bye'], new Date()];
|
|
|
|
for (const bad of badArgs) {
|
|
|
|
expect(() => {
|
|
|
|
systemPreferences.subscribeNotification(bad as any, () => {});
|
|
|
|
}).to.throw('Must pass null or a string');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
ifdescribe(process.platform === 'darwin')('systemPreferences.subscribeWorkspaceNotification(event, callback)', () => {
|
|
|
|
it('throws an error if invalid arguments are passed', () => {
|
|
|
|
const badArgs = [123, {}, ['hi', 'bye'], new Date()];
|
|
|
|
for (const bad of badArgs) {
|
|
|
|
expect(() => {
|
|
|
|
systemPreferences.subscribeWorkspaceNotification(bad as any, () => {});
|
|
|
|
}).to.throw('Must pass null or a string');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-07-24 17:40:12 +00:00
|
|
|
ifdescribe(process.platform === 'darwin')('systemPreferences.getSystemColor(color)', () => {
|
2019-08-19 16:10:18 +00:00
|
|
|
it('throws on invalid system colors', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const color = 'bad-color';
|
2019-08-19 16:10:18 +00:00
|
|
|
expect(() => {
|
2020-03-20 20:28:31 +00:00
|
|
|
systemPreferences.getSystemColor(color as any);
|
|
|
|
}).to.throw(`Unknown system color: ${color}`);
|
|
|
|
});
|
2019-11-01 20:37:02 +00:00
|
|
|
|
2019-07-24 17:40:12 +00:00
|
|
|
it('returns a valid system color', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const colors = ['blue', 'brown', 'gray', 'green', 'orange', 'pink', 'purple', 'red', 'yellow'];
|
2019-11-01 20:37:02 +00:00
|
|
|
|
2019-07-24 17:40:12 +00:00
|
|
|
colors.forEach(color => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const sysColor = systemPreferences.getSystemColor(color as any);
|
|
|
|
expect(sysColor).to.be.a('string');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2019-07-24 17:40:12 +00:00
|
|
|
|
2019-10-21 21:31:03 +00:00
|
|
|
ifdescribe(process.platform === 'darwin')('systemPreferences.getColor(color)', () => {
|
|
|
|
it('throws on invalid colors', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const color = 'bad-color';
|
2019-10-21 21:31:03 +00:00
|
|
|
expect(() => {
|
2020-03-20 20:28:31 +00:00
|
|
|
systemPreferences.getColor(color as any);
|
|
|
|
}).to.throw(`Unknown color: ${color}`);
|
|
|
|
});
|
2019-11-01 20:37:02 +00:00
|
|
|
|
2019-10-21 21:31:03 +00:00
|
|
|
it('returns a valid color', () => {
|
|
|
|
const colors = [
|
|
|
|
'alternate-selected-control-text',
|
|
|
|
'control-background',
|
|
|
|
'control',
|
|
|
|
'control-text',
|
|
|
|
'disabled-control-text',
|
|
|
|
'find-highlight',
|
|
|
|
'grid',
|
|
|
|
'header-text',
|
|
|
|
'highlight',
|
|
|
|
'keyboard-focus-indicator',
|
|
|
|
'label',
|
|
|
|
'link',
|
|
|
|
'placeholder-text',
|
|
|
|
'quaternary-label',
|
|
|
|
'scrubber-textured-background',
|
|
|
|
'secondary-label',
|
|
|
|
'selected-content-background',
|
|
|
|
'selected-control',
|
|
|
|
'selected-control-text',
|
|
|
|
'selected-menu-item-text',
|
|
|
|
'selected-text-background',
|
|
|
|
'selected-text',
|
|
|
|
'separator',
|
|
|
|
'shadow',
|
|
|
|
'tertiary-label',
|
|
|
|
'text-background',
|
|
|
|
'text',
|
|
|
|
'under-page-background',
|
|
|
|
'unemphasized-selected-content-background',
|
|
|
|
'unemphasized-selected-text-background',
|
|
|
|
'unemphasized-selected-text',
|
|
|
|
'window-background',
|
|
|
|
'window-frame-text'
|
2020-03-20 20:28:31 +00:00
|
|
|
];
|
2019-11-01 20:37:02 +00:00
|
|
|
|
2019-10-21 21:31:03 +00:00
|
|
|
colors.forEach(color => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const sysColor = systemPreferences.getColor(color as any);
|
|
|
|
expect(sysColor).to.be.a('string');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2019-10-21 21:31:03 +00:00
|
|
|
|
2019-07-17 00:14:32 +00:00
|
|
|
ifdescribe(process.platform === 'darwin')('systemPreferences.appLevelAppearance', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const options = ['dark', 'light', 'unknown', null];
|
2020-03-18 01:06:52 +00:00
|
|
|
describe('with properties', () => {
|
|
|
|
it('returns a valid appearance', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const appearance = systemPreferences.appLevelAppearance;
|
|
|
|
expect(options).to.include(appearance);
|
|
|
|
});
|
2020-03-18 01:06:52 +00:00
|
|
|
|
|
|
|
it('can be changed', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
systemPreferences.appLevelAppearance = 'dark';
|
|
|
|
expect(systemPreferences.appLevelAppearance).to.eql('dark');
|
|
|
|
});
|
|
|
|
});
|
2020-03-18 01:06:52 +00:00
|
|
|
|
|
|
|
describe('with functions', () => {
|
|
|
|
it('returns a valid appearance', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const appearance = systemPreferences.getAppLevelAppearance();
|
|
|
|
expect(options).to.include(appearance);
|
|
|
|
});
|
2020-03-18 01:06:52 +00:00
|
|
|
|
|
|
|
it('can be changed', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
systemPreferences.setAppLevelAppearance('dark');
|
|
|
|
const appearance = systemPreferences.getAppLevelAppearance();
|
|
|
|
expect(appearance).to.eql('dark');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2019-05-31 00:12:46 +00:00
|
|
|
|
2020-03-18 01:06:52 +00:00
|
|
|
ifdescribe(process.platform === 'darwin')('systemPreferences.effectiveAppearance', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const options = ['dark', 'light', 'unknown'];
|
2020-03-18 01:06:52 +00:00
|
|
|
describe('with properties', () => {
|
|
|
|
it('returns a valid appearance', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const appearance = systemPreferences.effectiveAppearance;
|
|
|
|
expect(options).to.include(appearance);
|
|
|
|
});
|
|
|
|
});
|
2020-03-18 01:06:52 +00:00
|
|
|
|
|
|
|
describe('with functions', () => {
|
|
|
|
it('returns a valid appearance', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const appearance = systemPreferences.getEffectiveAppearance();
|
|
|
|
expect(options).to.include(appearance);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2019-05-31 00:12:46 +00:00
|
|
|
|
2019-07-17 00:14:32 +00:00
|
|
|
ifdescribe(process.platform === 'darwin')('systemPreferences.setUserDefault(key, type, value)', () => {
|
2017-10-10 19:55:15 +00:00
|
|
|
it('removes keys', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const KEY = 'SystemPreferencesTest';
|
|
|
|
systemPreferences.setUserDefault(KEY, 'string', 'foo');
|
|
|
|
systemPreferences.removeUserDefault(KEY);
|
|
|
|
expect(systemPreferences.getUserDefault(KEY, 'string')).to.equal('');
|
|
|
|
});
|
2017-10-10 19:55:15 +00:00
|
|
|
|
|
|
|
it('does not throw for missing keys', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
systemPreferences.removeUserDefault('some-missing-key');
|
|
|
|
});
|
|
|
|
});
|
2017-10-10 19:55:15 +00:00
|
|
|
|
2019-07-24 17:40:12 +00:00
|
|
|
ifdescribe(process.platform === 'darwin')('systemPreferences.canPromptTouchID()', () => {
|
|
|
|
it('returns a boolean', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(systemPreferences.canPromptTouchID()).to.be.a('boolean');
|
|
|
|
});
|
|
|
|
});
|
2019-07-24 17:40:12 +00:00
|
|
|
|
|
|
|
ifdescribe(process.platform === 'darwin')('systemPreferences.isTrustedAccessibilityClient(prompt)', () => {
|
|
|
|
it('returns a boolean', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const trusted = systemPreferences.isTrustedAccessibilityClient(false);
|
|
|
|
expect(trusted).to.be.a('boolean');
|
|
|
|
});
|
|
|
|
});
|
2019-07-24 17:40:12 +00:00
|
|
|
|
2020-06-25 16:47:50 +00:00
|
|
|
ifdescribe(['win32', 'darwin'].includes(process.platform))('systemPreferences.getMediaAccessStatus(mediaType)', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const statuses = ['not-determined', 'granted', 'denied', 'restricted', 'unknown'];
|
2019-11-01 20:37:02 +00:00
|
|
|
|
2019-07-24 17:40:12 +00:00
|
|
|
it('returns an access status for a camera access request', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const cameraStatus = systemPreferences.getMediaAccessStatus('camera');
|
|
|
|
expect(statuses).to.include(cameraStatus);
|
|
|
|
});
|
2019-07-24 17:40:12 +00:00
|
|
|
|
|
|
|
it('returns an access status for a microphone access request', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const microphoneStatus = systemPreferences.getMediaAccessStatus('microphone');
|
|
|
|
expect(statuses).to.include(microphoneStatus);
|
|
|
|
});
|
2019-11-13 21:47:51 +00:00
|
|
|
|
|
|
|
it('returns an access status for a screen access request', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const screenStatus = systemPreferences.getMediaAccessStatus('screen');
|
|
|
|
expect(statuses).to.include(screenStatus);
|
|
|
|
});
|
|
|
|
});
|
2019-07-24 17:40:12 +00:00
|
|
|
|
2019-03-19 19:15:40 +00:00
|
|
|
describe('systemPreferences.getAnimationSettings()', () => {
|
|
|
|
it('returns an object with all properties', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const settings = systemPreferences.getAnimationSettings();
|
|
|
|
expect(settings).to.be.an('object');
|
|
|
|
expect(settings).to.have.property('shouldRenderRichAnimation').that.is.a('boolean');
|
|
|
|
expect(settings).to.have.property('scrollAnimationsEnabledBySystem').that.is.a('boolean');
|
|
|
|
expect(settings).to.have.property('prefersReducedMotion').that.is.a('boolean');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|