2019-05-20 17:04:18 +00:00
|
|
|
const chai = require('chai')
|
|
|
|
const dirtyChai = require('dirty-chai')
|
|
|
|
|
2018-09-13 16:10:51 +00:00
|
|
|
const { remote } = require('electron')
|
|
|
|
const { systemPreferences } = remote
|
2016-06-02 01:56:25 +00:00
|
|
|
|
2019-05-20 17:04:18 +00:00
|
|
|
const { expect } = chai
|
|
|
|
chai.use(dirtyChai)
|
|
|
|
|
2017-12-12 18:39:38 +00:00
|
|
|
describe('systemPreferences module', () => {
|
2017-10-27 00:58:48 +00:00
|
|
|
describe('systemPreferences.getAccentColor', () => {
|
2017-11-15 21:05:46 +00:00
|
|
|
before(function () {
|
|
|
|
if (process.platform !== 'win32') {
|
|
|
|
this.skip()
|
|
|
|
}
|
|
|
|
})
|
2016-09-16 16:13:09 +00:00
|
|
|
|
2017-10-27 00:58:48 +00:00
|
|
|
it('should return a non-empty string', () => {
|
2018-10-02 01:56:31 +00:00
|
|
|
const accentColor = systemPreferences.getAccentColor()
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(accentColor).to.be.a('string').that.is.not.empty()
|
2016-09-16 16:13:09 +00:00
|
|
|
})
|
|
|
|
})
|
2016-06-02 01:56:25 +00:00
|
|
|
|
2017-10-27 00:58:48 +00:00
|
|
|
describe('systemPreferences.getColor(id)', () => {
|
2017-11-15 21:05:46 +00:00
|
|
|
before(function () {
|
|
|
|
if (process.platform !== 'win32') {
|
|
|
|
this.skip()
|
|
|
|
}
|
|
|
|
})
|
2016-10-10 22:06:25 +00:00
|
|
|
|
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(() => {
|
2016-10-10 22:06:25 +00:00
|
|
|
systemPreferences.getColor('not-a-color')
|
2019-05-20 17:04:18 +00:00
|
|
|
}).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', () => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(systemPreferences.getColor('window')).to.match(/^#[0-9A-F]{6}$/i)
|
2016-10-10 22:06:25 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-12-12 18:08:09 +00:00
|
|
|
describe('systemPreferences.registerDefaults(defaults)', () => {
|
|
|
|
before(function () {
|
|
|
|
if (process.platform !== 'darwin') {
|
|
|
|
this.skip()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
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' }
|
|
|
|
]
|
|
|
|
|
|
|
|
const defaultsDict = {}
|
|
|
|
defaultsMap.forEach(row => { defaultsDict[row.key] = row.value })
|
|
|
|
|
|
|
|
systemPreferences.registerDefaults(defaultsDict)
|
|
|
|
|
|
|
|
for (const userDefault of defaultsMap) {
|
|
|
|
const { key, value: expectedValue, type } = userDefault
|
|
|
|
const actualValue = systemPreferences.getUserDefault(key, type)
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(actualValue).to.deep.equal(expectedValue)
|
2017-12-12 18:08:09 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
it('throws when bad defaults are passed', () => {
|
|
|
|
const badDefaults = [
|
|
|
|
1,
|
|
|
|
null,
|
|
|
|
new Date(),
|
2017-12-13 19:02:43 +00:00
|
|
|
{ 'one': null }
|
2017-12-12 18:08:09 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
for (const badDefault of badDefaults) {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(() => {
|
2017-12-12 18:08:09 +00:00
|
|
|
systemPreferences.registerDefaults(badDefault)
|
2019-05-20 17:04:18 +00:00
|
|
|
}).to.throw('Invalid userDefault data provided')
|
2017-12-12 18:08:09 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-27 00:58:48 +00:00
|
|
|
describe('systemPreferences.getUserDefault(key, type)', () => {
|
2017-11-15 21:05:46 +00:00
|
|
|
before(function () {
|
|
|
|
if (process.platform !== 'darwin') {
|
|
|
|
this.skip()
|
|
|
|
}
|
|
|
|
})
|
2016-09-16 16:13:09 +00:00
|
|
|
|
2017-10-27 00:58:48 +00:00
|
|
|
it('returns values for known user defaults', () => {
|
2016-11-17 00:28:57 +00:00
|
|
|
const locale = systemPreferences.getUserDefault('AppleLocale', 'string')
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(locale).to.be.a('string').that.is.not.empty()
|
2016-06-02 01:56:25 +00:00
|
|
|
|
2016-11-17 00:28:57 +00:00
|
|
|
const languages = systemPreferences.getUserDefault('AppleLanguages', 'array')
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(languages).to.be.an('array').that.is.not.empty()
|
2016-06-02 02:29:24 +00:00
|
|
|
})
|
2016-11-17 00:28:57 +00:00
|
|
|
|
2017-10-27 00:58:48 +00:00
|
|
|
it('returns values for unknown user defaults', () => {
|
2019-05-20 17:04:18 +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')).to.be.undefined()
|
|
|
|
expect(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'array')).to.deep.equal([])
|
|
|
|
expect(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'dictionary')).to.deep.equal({})
|
2016-11-17 00:28:57 +00:00
|
|
|
})
|
2016-06-02 01:56:25 +00:00
|
|
|
})
|
2016-10-06 22:27:24 +00:00
|
|
|
|
2016-11-28 23:08:22 +00:00
|
|
|
describe('systemPreferences.setUserDefault(key, type, value)', () => {
|
|
|
|
const KEY = 'SystemPreferencesTest'
|
|
|
|
const TEST_CASES = [
|
|
|
|
['string', 'abc'],
|
|
|
|
['boolean', true],
|
|
|
|
['float', 2.5],
|
|
|
|
['double', 10.1],
|
|
|
|
['integer', 11],
|
|
|
|
['url', 'https://github.com/electron'],
|
|
|
|
['array', [1, 2, 3]],
|
2018-09-13 16:10:51 +00:00
|
|
|
['dictionary', { 'a': 1, 'b': 2 }]
|
2016-11-28 23:08:22 +00:00
|
|
|
]
|
|
|
|
|
2017-11-15 21:05:46 +00:00
|
|
|
before(function () {
|
|
|
|
if (process.platform !== 'darwin') {
|
|
|
|
this.skip()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2016-11-28 23:08:22 +00:00
|
|
|
it('sets values', () => {
|
|
|
|
for (const [type, value] of TEST_CASES) {
|
|
|
|
systemPreferences.setUserDefault(KEY, type, value)
|
|
|
|
const retrievedValue = systemPreferences.getUserDefault(KEY, type)
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(retrievedValue).to.deep.equal(value)
|
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(() => {
|
2016-11-28 23:08:22 +00:00
|
|
|
systemPreferences.setUserDefault(KEY, type, typeof value === 'string' ? {} : 'foo')
|
2019-05-20 17:04:18 +00:00
|
|
|
}).to.throw(`Unable to convert value to: ${type}`)
|
2016-11-28 23:08:22 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
it('throws when type is not valid', () => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(() => {
|
2016-11-28 23:08:22 +00:00
|
|
|
systemPreferences.setUserDefault(KEY, 'abc', 'foo')
|
2019-05-20 17:04:18 +00:00
|
|
|
}).to.throw('Invalid type: abc')
|
2016-11-28 23:08:22 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-05-31 00:12:46 +00:00
|
|
|
describe('systemPreferences.appLevelAppearance', () => {
|
|
|
|
before(function () {
|
|
|
|
if (process.platform !== 'darwin') this.skip()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('has an appLevelAppearance property', () => {
|
|
|
|
expect(systemPreferences).to.have.a.property('appLevelAppearance')
|
|
|
|
|
|
|
|
// TODO(codebytere): remove when propertyification is complete
|
|
|
|
expect(systemPreferences.getAppLevelAppearance).to.be.a('function')
|
|
|
|
expect(systemPreferences.setAppLevelAppearance).to.be.a('function')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-10 19:55:15 +00:00
|
|
|
describe('systemPreferences.setUserDefault(key, type, value)', () => {
|
2017-11-15 21:05:46 +00:00
|
|
|
before(function () {
|
|
|
|
if (process.platform !== 'darwin') {
|
|
|
|
this.skip()
|
|
|
|
}
|
|
|
|
})
|
2017-10-10 19:55:15 +00:00
|
|
|
|
|
|
|
it('removes keys', () => {
|
|
|
|
const KEY = 'SystemPreferencesTest'
|
|
|
|
systemPreferences.setUserDefault(KEY, 'string', 'foo')
|
|
|
|
systemPreferences.removeUserDefault(KEY)
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(systemPreferences.getUserDefault(KEY, 'string')).to.equal('')
|
2017-10-10 19:55:15 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('does not throw for missing keys', () => {
|
|
|
|
systemPreferences.removeUserDefault('some-missing-key')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-27 00:58:48 +00:00
|
|
|
describe('systemPreferences.isInvertedColorScheme()', () => {
|
|
|
|
it('returns a boolean', () => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(systemPreferences.isInvertedColorScheme()).to.be.a('boolean')
|
2016-10-06 22:27:24 +00:00
|
|
|
})
|
|
|
|
})
|
2019-03-19 19:15:40 +00:00
|
|
|
|
|
|
|
describe('systemPreferences.getAnimationSettings()', () => {
|
|
|
|
it('returns an object with all properties', () => {
|
|
|
|
const settings = systemPreferences.getAnimationSettings()
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(settings).to.be.an('object')
|
|
|
|
expect(settings).to.have.a.property('shouldRenderRichAnimation').that.is.a('boolean')
|
|
|
|
expect(settings).to.have.a.property('scrollAnimationsEnabledBySystem').that.is.a('boolean')
|
|
|
|
expect(settings).to.have.a.property('prefersReducedMotion').that.is.a('boolean')
|
2019-03-19 19:15:40 +00:00
|
|
|
})
|
|
|
|
})
|
2016-06-02 01:56:25 +00:00
|
|
|
})
|