| 
									
										
										
										
											2016-06-01 18:56:25 -07:00
										 |  |  | const assert = require('assert') | 
					
						
							| 
									
										
										
										
											2018-09-14 02:10:51 +10:00
										 |  |  | const { remote } = require('electron') | 
					
						
							|  |  |  | const { systemPreferences } = remote | 
					
						
							| 
									
										
										
										
											2016-06-01 18:56:25 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-12 13:39:38 -05:00
										 |  |  | describe('systemPreferences module', () => { | 
					
						
							| 
									
										
										
										
											2017-10-26 20:58:48 -04:00
										 |  |  |   describe('systemPreferences.getAccentColor', () => { | 
					
						
							| 
									
										
										
										
											2017-11-16 00:05:46 +03:00
										 |  |  |     before(function () { | 
					
						
							|  |  |  |       if (process.platform !== 'win32') { | 
					
						
							|  |  |  |         this.skip() | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }) | 
					
						
							| 
									
										
										
										
											2016-09-17 02:13:09 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-26 20:58:48 -04:00
										 |  |  |     it('should return a non-empty string', () => { | 
					
						
							| 
									
										
										
										
											2018-10-02 03:56:31 +02:00
										 |  |  |       const accentColor = systemPreferences.getAccentColor() | 
					
						
							| 
									
										
										
										
											2018-09-14 02:10:51 +10:00
										 |  |  |       assert.notStrictEqual(accentColor, null) | 
					
						
							| 
									
										
										
										
											2016-09-17 02:27:46 +10:00
										 |  |  |       assert(accentColor.length > 0) | 
					
						
							| 
									
										
										
										
											2016-09-17 02:13:09 +10:00
										 |  |  |     }) | 
					
						
							|  |  |  |   }) | 
					
						
							| 
									
										
										
										
											2016-06-01 18:56:25 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-26 20:58:48 -04:00
										 |  |  |   describe('systemPreferences.getColor(id)', () => { | 
					
						
							| 
									
										
										
										
											2017-11-16 00:05:46 +03:00
										 |  |  |     before(function () { | 
					
						
							|  |  |  |       if (process.platform !== 'win32') { | 
					
						
							|  |  |  |         this.skip() | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }) | 
					
						
							| 
									
										
										
										
											2016-10-10 15:06:25 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-26 20:58:48 -04:00
										 |  |  |     it('throws an error when the id is invalid', () => { | 
					
						
							|  |  |  |       assert.throws(() => { | 
					
						
							| 
									
										
										
										
											2016-10-10 15:06:25 -07:00
										 |  |  |         systemPreferences.getColor('not-a-color') | 
					
						
							|  |  |  |       }, /Unknown color: not-a-color/) | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-26 20:58:48 -04:00
										 |  |  |     it('returns a hex RGB color string', () => { | 
					
						
							| 
									
										
										
										
											2018-09-14 02:10:51 +10:00
										 |  |  |       assert.strictEqual(/^#[0-9A-F]{6}$/i.test(systemPreferences.getColor('window')), true) | 
					
						
							| 
									
										
										
										
											2016-10-10 15:06:25 -07:00
										 |  |  |     }) | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-12 13:08:09 -05: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-14 02:10:51 +10:00
										 |  |  |         assert.deepStrictEqual(actualValue, expectedValue) | 
					
						
							| 
									
										
										
										
											2017-12-12 13:08:09 -05:00
										 |  |  |       } | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('throws when bad defaults are passed', () => { | 
					
						
							|  |  |  |       const badDefaults = [ | 
					
						
							|  |  |  |         1, | 
					
						
							|  |  |  |         null, | 
					
						
							|  |  |  |         new Date(), | 
					
						
							| 
									
										
										
										
											2017-12-13 14:02:43 -05:00
										 |  |  |         { 'one': null } | 
					
						
							| 
									
										
										
										
											2017-12-12 13:08:09 -05:00
										 |  |  |       ] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       for (const badDefault of badDefaults) { | 
					
						
							|  |  |  |         assert.throws(() => { | 
					
						
							|  |  |  |           systemPreferences.registerDefaults(badDefault) | 
					
						
							|  |  |  |         }, 'Invalid userDefault data provided') | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-26 20:58:48 -04:00
										 |  |  |   describe('systemPreferences.getUserDefault(key, type)', () => { | 
					
						
							| 
									
										
										
										
											2017-11-16 00:05:46 +03:00
										 |  |  |     before(function () { | 
					
						
							|  |  |  |       if (process.platform !== 'darwin') { | 
					
						
							|  |  |  |         this.skip() | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }) | 
					
						
							| 
									
										
										
										
											2016-09-17 02:13:09 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-26 20:58:48 -04:00
										 |  |  |     it('returns values for known user defaults', () => { | 
					
						
							| 
									
										
										
										
											2016-11-16 16:28:57 -08:00
										 |  |  |       const locale = systemPreferences.getUserDefault('AppleLocale', 'string') | 
					
						
							| 
									
										
										
										
											2018-09-14 02:10:51 +10:00
										 |  |  |       assert.strictEqual(typeof locale, 'string') | 
					
						
							| 
									
										
										
										
											2016-06-01 19:29:24 -07:00
										 |  |  |       assert(locale.length > 0) | 
					
						
							| 
									
										
										
										
											2016-06-01 18:56:25 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-16 16:28:57 -08:00
										 |  |  |       const languages = systemPreferences.getUserDefault('AppleLanguages', 'array') | 
					
						
							|  |  |  |       assert(Array.isArray(languages)) | 
					
						
							| 
									
										
										
										
											2016-06-01 19:29:24 -07:00
										 |  |  |       assert(languages.length > 0) | 
					
						
							|  |  |  |     }) | 
					
						
							| 
									
										
										
										
											2016-11-16 16:28:57 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-26 20:58:48 -04:00
										 |  |  |     it('returns values for unknown user defaults', () => { | 
					
						
							| 
									
										
										
										
											2018-09-14 02:10:51 +10: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'), {}) | 
					
						
							| 
									
										
										
										
											2016-11-16 16:28:57 -08:00
										 |  |  |     }) | 
					
						
							| 
									
										
										
										
											2016-06-01 18:56:25 -07:00
										 |  |  |   }) | 
					
						
							| 
									
										
										
										
											2016-10-06 15:27:24 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-28 15:08:22 -08: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-14 02:10:51 +10:00
										 |  |  |       ['dictionary', { 'a': 1, 'b': 2 }] | 
					
						
							| 
									
										
										
										
											2016-11-28 15:08:22 -08:00
										 |  |  |     ] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-16 00:05:46 +03:00
										 |  |  |     before(function () { | 
					
						
							|  |  |  |       if (process.platform !== 'darwin') { | 
					
						
							|  |  |  |         this.skip() | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-28 15:08:22 -08:00
										 |  |  |     it('sets values', () => { | 
					
						
							|  |  |  |       for (const [type, value] of TEST_CASES) { | 
					
						
							|  |  |  |         systemPreferences.setUserDefault(KEY, type, value) | 
					
						
							|  |  |  |         const retrievedValue = systemPreferences.getUserDefault(KEY, type) | 
					
						
							| 
									
										
										
										
											2018-09-14 02:10:51 +10:00
										 |  |  |         assert.deepStrictEqual(retrievedValue, value) | 
					
						
							| 
									
										
										
										
											2016-11-28 15:08:22 -08:00
										 |  |  |       } | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     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') | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-10 22:55:15 +03:00
										 |  |  |   describe('systemPreferences.setUserDefault(key, type, value)', () => { | 
					
						
							| 
									
										
										
										
											2017-11-16 00:05:46 +03:00
										 |  |  |     before(function () { | 
					
						
							|  |  |  |       if (process.platform !== 'darwin') { | 
					
						
							|  |  |  |         this.skip() | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }) | 
					
						
							| 
									
										
										
										
											2017-10-10 22:55:15 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     it('removes keys', () => { | 
					
						
							|  |  |  |       const KEY = 'SystemPreferencesTest' | 
					
						
							|  |  |  |       systemPreferences.setUserDefault(KEY, 'string', 'foo') | 
					
						
							|  |  |  |       systemPreferences.removeUserDefault(KEY) | 
					
						
							| 
									
										
										
										
											2018-09-14 02:10:51 +10:00
										 |  |  |       assert.strictEqual(systemPreferences.getUserDefault(KEY, 'string'), '') | 
					
						
							| 
									
										
										
										
											2017-10-10 22:55:15 +03:00
										 |  |  |     }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('does not throw for missing keys', () => { | 
					
						
							|  |  |  |       systemPreferences.removeUserDefault('some-missing-key') | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-26 20:58:48 -04:00
										 |  |  |   describe('systemPreferences.isInvertedColorScheme()', () => { | 
					
						
							|  |  |  |     it('returns a boolean', () => { | 
					
						
							| 
									
										
										
										
											2018-09-14 02:10:51 +10:00
										 |  |  |       assert.strictEqual(typeof systemPreferences.isInvertedColorScheme(), 'boolean') | 
					
						
							| 
									
										
										
										
											2016-10-06 15:27:24 -07:00
										 |  |  |     }) | 
					
						
							|  |  |  |   }) | 
					
						
							| 
									
										
										
										
											2016-06-01 18:56:25 -07:00
										 |  |  | }) |