forked from electron/electron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api-system-preferences-spec.js
176 lines (151 loc) · 5.52 KB
/
api-system-preferences-spec.js
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
const assert = require('assert')
const {remote} = require('electron')
const {systemPreferences} = remote
describe('systemPreferences module', () => {
describe('systemPreferences.getAccentColor', () => {
before(function () {
if (process.platform !== 'win32') {
this.skip()
}
})
it('should return a non-empty string', () => {
let accentColor = systemPreferences.getAccentColor()
assert.notEqual(accentColor, null)
assert(accentColor.length > 0)
})
})
describe('systemPreferences.getColor(id)', () => {
before(function () {
if (process.platform !== 'win32') {
this.skip()
}
})
it('throws an error when the id is invalid', () => {
assert.throws(() => {
systemPreferences.getColor('not-a-color')
}, /Unknown color: not-a-color/)
})
it('returns a hex RGB color string', () => {
assert.equal(/^#[0-9A-F]{6}$/i.test(systemPreferences.getColor('window')), true)
})
})
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)
assert.deepEqual(actualValue, expectedValue)
}
})
it('throws when bad defaults are passed', () => {
const badDefaults = [
1,
null,
new Date(),
{ 'one': null }
]
for (const badDefault of badDefaults) {
assert.throws(() => {
systemPreferences.registerDefaults(badDefault)
}, 'Invalid userDefault data provided')
}
})
})
describe('systemPreferences.getUserDefault(key, type)', () => {
before(function () {
if (process.platform !== 'darwin') {
this.skip()
}
})
it('returns values for known user defaults', () => {
const locale = systemPreferences.getUserDefault('AppleLocale', 'string')
assert.equal(typeof locale, 'string')
assert(locale.length > 0)
const languages = systemPreferences.getUserDefault('AppleLanguages', 'array')
assert(Array.isArray(languages))
assert(languages.length > 0)
})
it('returns values for unknown user defaults', () => {
assert.equal(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'boolean'), false)
assert.equal(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'integer'), 0)
assert.equal(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'float'), 0)
assert.equal(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'double'), 0)
assert.equal(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'string'), '')
assert.equal(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'url'), '')
assert.equal(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'badtype'), undefined)
assert.deepEqual(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'array'), [])
assert.deepEqual(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]],
['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)
assert.deepEqual(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)
assert.equal(systemPreferences.getUserDefault(KEY, 'string'), '')
})
it('does not throw for missing keys', () => {
systemPreferences.removeUserDefault('some-missing-key')
})
})
describe('systemPreferences.isInvertedColorScheme()', () => {
it('returns a boolean', () => {
assert.equal(typeof systemPreferences.isInvertedColorScheme(), 'boolean')
})
})
})