api-system-preferences-spec.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. const assert = require('assert')
  2. const {remote} = require('electron')
  3. const {systemPreferences} = remote
  4. describe('systemPreferences module', () => {
  5. describe('systemPreferences.getAccentColor', () => {
  6. before(function () {
  7. if (process.platform !== 'win32') {
  8. this.skip()
  9. }
  10. })
  11. it('should return a non-empty string', () => {
  12. let accentColor = systemPreferences.getAccentColor()
  13. assert.notEqual(accentColor, null)
  14. assert(accentColor.length > 0)
  15. })
  16. })
  17. describe('systemPreferences.getColor(id)', () => {
  18. before(function () {
  19. if (process.platform !== 'win32') {
  20. this.skip()
  21. }
  22. })
  23. it('throws an error when the id is invalid', () => {
  24. assert.throws(() => {
  25. systemPreferences.getColor('not-a-color')
  26. }, /Unknown color: not-a-color/)
  27. })
  28. it('returns a hex RGB color string', () => {
  29. assert.equal(/^#[0-9A-F]{6}$/i.test(systemPreferences.getColor('window')), true)
  30. })
  31. })
  32. describe('systemPreferences.registerDefaults(defaults)', () => {
  33. before(function () {
  34. if (process.platform !== 'darwin') {
  35. this.skip()
  36. }
  37. })
  38. it('registers defaults', () => {
  39. const defaultsMap = [
  40. { key: 'one', type: 'string', value: 'ONE' },
  41. { key: 'two', value: 2, type: 'integer' },
  42. { key: 'three', value: [1, 2, 3], type: 'array' }
  43. ]
  44. const defaultsDict = {}
  45. defaultsMap.forEach(row => { defaultsDict[row.key] = row.value })
  46. systemPreferences.registerDefaults(defaultsDict)
  47. for (const userDefault of defaultsMap) {
  48. const { key, value: expectedValue, type } = userDefault
  49. const actualValue = systemPreferences.getUserDefault(key, type)
  50. assert.deepEqual(actualValue, expectedValue)
  51. }
  52. })
  53. it('throws when bad defaults are passed', () => {
  54. const badDefaults = [
  55. 1,
  56. null,
  57. new Date(),
  58. { 'one': null }
  59. ]
  60. for (const badDefault of badDefaults) {
  61. assert.throws(() => {
  62. systemPreferences.registerDefaults(badDefault)
  63. }, 'Invalid userDefault data provided')
  64. }
  65. })
  66. })
  67. describe('systemPreferences.getUserDefault(key, type)', () => {
  68. before(function () {
  69. if (process.platform !== 'darwin') {
  70. this.skip()
  71. }
  72. })
  73. it('returns values for known user defaults', () => {
  74. const locale = systemPreferences.getUserDefault('AppleLocale', 'string')
  75. assert.equal(typeof locale, 'string')
  76. assert(locale.length > 0)
  77. const languages = systemPreferences.getUserDefault('AppleLanguages', 'array')
  78. assert(Array.isArray(languages))
  79. assert(languages.length > 0)
  80. })
  81. it('returns values for unknown user defaults', () => {
  82. assert.equal(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'boolean'), false)
  83. assert.equal(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'integer'), 0)
  84. assert.equal(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'float'), 0)
  85. assert.equal(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'double'), 0)
  86. assert.equal(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'string'), '')
  87. assert.equal(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'url'), '')
  88. assert.equal(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'badtype'), undefined)
  89. assert.deepEqual(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'array'), [])
  90. assert.deepEqual(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'dictionary'), {})
  91. })
  92. })
  93. describe('systemPreferences.setUserDefault(key, type, value)', () => {
  94. const KEY = 'SystemPreferencesTest'
  95. const TEST_CASES = [
  96. ['string', 'abc'],
  97. ['boolean', true],
  98. ['float', 2.5],
  99. ['double', 10.1],
  100. ['integer', 11],
  101. ['url', 'https://github.com/electron'],
  102. ['array', [1, 2, 3]],
  103. ['dictionary', {'a': 1, 'b': 2}]
  104. ]
  105. before(function () {
  106. if (process.platform !== 'darwin') {
  107. this.skip()
  108. }
  109. })
  110. it('sets values', () => {
  111. for (const [type, value] of TEST_CASES) {
  112. systemPreferences.setUserDefault(KEY, type, value)
  113. const retrievedValue = systemPreferences.getUserDefault(KEY, type)
  114. assert.deepEqual(retrievedValue, value)
  115. }
  116. })
  117. it('throws when type and value conflict', () => {
  118. for (const [type, value] of TEST_CASES) {
  119. assert.throws(() => {
  120. systemPreferences.setUserDefault(KEY, type, typeof value === 'string' ? {} : 'foo')
  121. }, `Unable to convert value to: ${type}`)
  122. }
  123. })
  124. it('throws when type is not valid', () => {
  125. assert.throws(() => {
  126. systemPreferences.setUserDefault(KEY, 'abc', 'foo')
  127. }, 'Invalid type: abc')
  128. })
  129. })
  130. describe('systemPreferences.setUserDefault(key, type, value)', () => {
  131. before(function () {
  132. if (process.platform !== 'darwin') {
  133. this.skip()
  134. }
  135. })
  136. it('removes keys', () => {
  137. const KEY = 'SystemPreferencesTest'
  138. systemPreferences.setUserDefault(KEY, 'string', 'foo')
  139. systemPreferences.removeUserDefault(KEY)
  140. assert.equal(systemPreferences.getUserDefault(KEY, 'string'), '')
  141. })
  142. it('does not throw for missing keys', () => {
  143. systemPreferences.removeUserDefault('some-missing-key')
  144. })
  145. })
  146. describe('systemPreferences.isInvertedColorScheme()', () => {
  147. it('returns a boolean', () => {
  148. assert.equal(typeof systemPreferences.isInvertedColorScheme(), 'boolean')
  149. })
  150. })
  151. })