electron/spec/api-system-preferences-spec.js

187 lines
6.1 KiB
JavaScript
Raw Normal View History

const assert = require('assert')
2018-09-13 16:10:51 +00:00
const { remote } = require('electron')
const { systemPreferences } = remote
2017-12-12 18:39:38 +00:00
describe('systemPreferences module', () => {
2017-10-27 00:58:48 +00:00
describe('systemPreferences.getAccentColor', () => {
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', () => {
const accentColor = systemPreferences.getAccentColor()
2018-09-13 16:10:51 +00:00
assert.notStrictEqual(accentColor, null)
2016-09-16 16:27:46 +00:00
assert(accentColor.length > 0)
2016-09-16 16:13:09 +00:00
})
})
2017-10-27 00:58:48 +00:00
describe('systemPreferences.getColor(id)', () => {
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', () => {
assert.throws(() => {
2016-10-10 22:06:25 +00:00
systemPreferences.getColor('not-a-color')
}, /Unknown color: not-a-color/)
})
2017-10-27 00:58:48 +00:00
it('returns a hex RGB color string', () => {
2018-09-13 16:10:51 +00:00
assert.strictEqual(/^#[0-9A-F]{6}$/i.test(systemPreferences.getColor('window')), true)
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)
2018-09-13 16:10:51 +00:00
assert.deepStrictEqual(actualValue, 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) {
assert.throws(() => {
systemPreferences.registerDefaults(badDefault)
}, 'Invalid userDefault data provided')
}
})
})
2017-10-27 00:58:48 +00:00
describe('systemPreferences.getUserDefault(key, type)', () => {
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', () => {
const locale = systemPreferences.getUserDefault('AppleLocale', 'string')
2018-09-13 16:10:51 +00:00
assert.strictEqual(typeof locale, 'string')
2016-06-02 02:29:24 +00:00
assert(locale.length > 0)
const languages = systemPreferences.getUserDefault('AppleLanguages', 'array')
assert(Array.isArray(languages))
2016-06-02 02:29:24 +00:00
assert(languages.length > 0)
})
2017-10-27 00:58:48 +00:00
it('returns values for unknown user defaults', () => {
2018-09-13 16:10:51 +00:00
assert.strictEqual(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'boolean'), false)
assert.strictEqual(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'integer'), 0)
assert.strictEqual(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'float'), 0)
assert.strictEqual(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'double'), 0)
assert.strictEqual(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'string'), '')
assert.strictEqual(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'url'), '')
assert.strictEqual(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'badtype'), undefined)
assert.deepStrictEqual(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'array'), [])
assert.deepStrictEqual(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'dictionary'), {})
})
})
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 }]
]
before(function () {
if (process.platform !== 'darwin') {
this.skip()
}
})
it('sets values', () => {
for (const [type, value] of TEST_CASES) {
systemPreferences.setUserDefault(KEY, type, value)
const retrievedValue = systemPreferences.getUserDefault(KEY, type)
2018-09-13 16:10:51 +00:00
assert.deepStrictEqual(retrievedValue, value)
}
})
it('throws when type and value conflict', () => {
for (const [type, value] of TEST_CASES) {
assert.throws(() => {
systemPreferences.setUserDefault(KEY, type, typeof value === 'string' ? {} : 'foo')
}, `Unable to convert value to: ${type}`)
}
})
it('throws when type is not valid', () => {
assert.throws(() => {
systemPreferences.setUserDefault(KEY, 'abc', 'foo')
}, 'Invalid type: abc')
})
})
describe('systemPreferences.setUserDefault(key, type, value)', () => {
before(function () {
if (process.platform !== 'darwin') {
this.skip()
}
})
it('removes keys', () => {
const KEY = 'SystemPreferencesTest'
systemPreferences.setUserDefault(KEY, 'string', 'foo')
systemPreferences.removeUserDefault(KEY)
2018-09-13 16:10:51 +00:00
assert.strictEqual(systemPreferences.getUserDefault(KEY, 'string'), '')
})
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', () => {
2018-09-13 16:10:51 +00:00
assert.strictEqual(typeof systemPreferences.isInvertedColorScheme(), 'boolean')
})
})
describe('systemPreferences.getAnimationSettings()', () => {
it('returns an object with all properties', () => {
const settings = systemPreferences.getAnimationSettings()
assert.strictEqual(typeof settings, 'object')
assert.strictEqual(typeof settings.shouldRenderRichAnimation, 'boolean')
assert.strictEqual(typeof settings.scrollAnimationsEnabledBySystem, 'boolean')
assert.strictEqual(typeof settings.prefersReducedMotion, 'boolean')
})
})
})