api-system-preferences-spec.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. import { expect } from 'chai';
  2. import { systemPreferences } from 'electron/main';
  3. import { ifdescribe } from './spec-helpers';
  4. describe('systemPreferences module', () => {
  5. ifdescribe(process.platform === 'win32')('systemPreferences.getAccentColor', () => {
  6. it('should return a non-empty string', () => {
  7. const accentColor = systemPreferences.getAccentColor();
  8. expect(accentColor).to.be.a('string').that.is.not.empty('accent color');
  9. });
  10. });
  11. ifdescribe(process.platform === 'win32')('systemPreferences.getColor(id)', () => {
  12. it('throws an error when the id is invalid', () => {
  13. expect(() => {
  14. systemPreferences.getColor('not-a-color' as any);
  15. }).to.throw('Unknown color: not-a-color');
  16. });
  17. it('returns a hex RGB color string', () => {
  18. expect(systemPreferences.getColor('window')).to.match(/^#[0-9A-F]{6}$/i);
  19. });
  20. });
  21. ifdescribe(process.platform === 'darwin')('systemPreferences.registerDefaults(defaults)', () => {
  22. it('registers defaults', () => {
  23. const defaultsMap = [
  24. { key: 'one', type: 'string', value: 'ONE' },
  25. { key: 'two', value: 2, type: 'integer' },
  26. { key: 'three', value: [1, 2, 3], type: 'array' }
  27. ];
  28. const defaultsDict: Record<string, any> = {};
  29. defaultsMap.forEach(row => { defaultsDict[row.key] = row.value; });
  30. systemPreferences.registerDefaults(defaultsDict);
  31. for (const userDefault of defaultsMap) {
  32. const { key, value: expectedValue, type } = userDefault;
  33. const actualValue = systemPreferences.getUserDefault(key, type as any);
  34. expect(actualValue).to.deep.equal(expectedValue);
  35. }
  36. });
  37. it('throws when bad defaults are passed', () => {
  38. const badDefaults = [
  39. 1,
  40. null,
  41. new Date(),
  42. { one: null }
  43. ];
  44. for (const badDefault of badDefaults) {
  45. expect(() => {
  46. systemPreferences.registerDefaults(badDefault as any);
  47. }).to.throw('Error processing argument at index 0, conversion failure from ');
  48. }
  49. });
  50. });
  51. ifdescribe(process.platform === 'darwin')('systemPreferences.getUserDefault(key, type)', () => {
  52. it('returns values for known user defaults', () => {
  53. const locale = systemPreferences.getUserDefault('AppleLocale', 'string');
  54. expect(locale).to.be.a('string').that.is.not.empty('locale');
  55. const languages = systemPreferences.getUserDefault('AppleLanguages', 'array');
  56. expect(languages).to.be.an('array').that.is.not.empty('languages');
  57. });
  58. it('returns values for unknown user defaults', () => {
  59. expect(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'boolean')).to.equal(false);
  60. expect(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'integer')).to.equal(0);
  61. expect(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'float')).to.equal(0);
  62. expect(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'double')).to.equal(0);
  63. expect(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'string')).to.equal('');
  64. expect(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'url')).to.equal('');
  65. expect(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'badtype' as any)).to.be.undefined('user default');
  66. expect(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'array')).to.deep.equal([]);
  67. expect(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'dictionary')).to.deep.equal({});
  68. });
  69. });
  70. ifdescribe(process.platform === 'darwin')('systemPreferences.setUserDefault(key, type, value)', () => {
  71. const KEY = 'SystemPreferencesTest';
  72. const TEST_CASES = [
  73. ['string', 'abc'],
  74. ['boolean', true],
  75. ['float', 2.5],
  76. ['double', 10.1],
  77. ['integer', 11],
  78. ['url', 'https://github.com/electron'],
  79. ['array', [1, 2, 3]],
  80. ['dictionary', { a: 1, b: 2 }]
  81. ];
  82. it('sets values', () => {
  83. for (const [type, value] of TEST_CASES) {
  84. systemPreferences.setUserDefault(KEY, type as any, value as any);
  85. const retrievedValue = systemPreferences.getUserDefault(KEY, type as any);
  86. expect(retrievedValue).to.deep.equal(value);
  87. }
  88. });
  89. it('throws when type and value conflict', () => {
  90. for (const [type, value] of TEST_CASES) {
  91. expect(() => {
  92. systemPreferences.setUserDefault(KEY, type as any, typeof value === 'string' ? {} : 'foo' as any);
  93. }).to.throw(`Unable to convert value to: ${type}`);
  94. }
  95. });
  96. it('throws when type is not valid', () => {
  97. expect(() => {
  98. systemPreferences.setUserDefault(KEY, 'abc' as any, 'foo');
  99. }).to.throw('Invalid type: abc');
  100. });
  101. });
  102. ifdescribe(process.platform === 'darwin')('systemPreferences.getSystemColor(color)', () => {
  103. it('throws on invalid system colors', () => {
  104. const color = 'bad-color';
  105. expect(() => {
  106. systemPreferences.getSystemColor(color as any);
  107. }).to.throw(`Unknown system color: ${color}`);
  108. });
  109. it('returns a valid system color', () => {
  110. const colors = ['blue', 'brown', 'gray', 'green', 'orange', 'pink', 'purple', 'red', 'yellow'];
  111. colors.forEach(color => {
  112. const sysColor = systemPreferences.getSystemColor(color as any);
  113. expect(sysColor).to.be.a('string');
  114. });
  115. });
  116. });
  117. ifdescribe(process.platform === 'darwin')('systemPreferences.getColor(color)', () => {
  118. it('throws on invalid colors', () => {
  119. const color = 'bad-color';
  120. expect(() => {
  121. systemPreferences.getColor(color as any);
  122. }).to.throw(`Unknown color: ${color}`);
  123. });
  124. it('returns a valid color', () => {
  125. const colors = [
  126. 'alternate-selected-control-text',
  127. 'control-background',
  128. 'control',
  129. 'control-text',
  130. 'disabled-control-text',
  131. 'find-highlight',
  132. 'grid',
  133. 'header-text',
  134. 'highlight',
  135. 'keyboard-focus-indicator',
  136. 'label',
  137. 'link',
  138. 'placeholder-text',
  139. 'quaternary-label',
  140. 'scrubber-textured-background',
  141. 'secondary-label',
  142. 'selected-content-background',
  143. 'selected-control',
  144. 'selected-control-text',
  145. 'selected-menu-item-text',
  146. 'selected-text-background',
  147. 'selected-text',
  148. 'separator',
  149. 'shadow',
  150. 'tertiary-label',
  151. 'text-background',
  152. 'text',
  153. 'under-page-background',
  154. 'unemphasized-selected-content-background',
  155. 'unemphasized-selected-text-background',
  156. 'unemphasized-selected-text',
  157. 'window-background',
  158. 'window-frame-text'
  159. ];
  160. colors.forEach(color => {
  161. const sysColor = systemPreferences.getColor(color as any);
  162. expect(sysColor).to.be.a('string');
  163. });
  164. });
  165. });
  166. ifdescribe(process.platform === 'darwin')('systemPreferences.appLevelAppearance', () => {
  167. const options = ['dark', 'light', 'unknown', null];
  168. describe('with properties', () => {
  169. it('returns a valid appearance', () => {
  170. const appearance = systemPreferences.appLevelAppearance;
  171. expect(options).to.include(appearance);
  172. });
  173. it('can be changed', () => {
  174. systemPreferences.appLevelAppearance = 'dark';
  175. expect(systemPreferences.appLevelAppearance).to.eql('dark');
  176. });
  177. });
  178. describe('with functions', () => {
  179. it('returns a valid appearance', () => {
  180. const appearance = systemPreferences.getAppLevelAppearance();
  181. expect(options).to.include(appearance);
  182. });
  183. it('can be changed', () => {
  184. systemPreferences.setAppLevelAppearance('dark');
  185. const appearance = systemPreferences.getAppLevelAppearance();
  186. expect(appearance).to.eql('dark');
  187. });
  188. });
  189. });
  190. ifdescribe(process.platform === 'darwin')('systemPreferences.effectiveAppearance', () => {
  191. const options = ['dark', 'light', 'unknown'];
  192. describe('with properties', () => {
  193. it('returns a valid appearance', () => {
  194. const appearance = systemPreferences.effectiveAppearance;
  195. expect(options).to.include(appearance);
  196. });
  197. });
  198. describe('with functions', () => {
  199. it('returns a valid appearance', () => {
  200. const appearance = systemPreferences.getEffectiveAppearance();
  201. expect(options).to.include(appearance);
  202. });
  203. });
  204. });
  205. ifdescribe(process.platform === 'darwin')('systemPreferences.setUserDefault(key, type, value)', () => {
  206. it('removes keys', () => {
  207. const KEY = 'SystemPreferencesTest';
  208. systemPreferences.setUserDefault(KEY, 'string', 'foo');
  209. systemPreferences.removeUserDefault(KEY);
  210. expect(systemPreferences.getUserDefault(KEY, 'string')).to.equal('');
  211. });
  212. it('does not throw for missing keys', () => {
  213. systemPreferences.removeUserDefault('some-missing-key');
  214. });
  215. });
  216. describe('systemPreferences.isInvertedColorScheme()', () => {
  217. it('returns a boolean', () => {
  218. expect(systemPreferences.isInvertedColorScheme()).to.be.a('boolean');
  219. });
  220. });
  221. describe('systemPreferences.isHighContrastColorScheme()', () => {
  222. it('returns a boolean', () => {
  223. expect(systemPreferences.isHighContrastColorScheme()).to.be.a('boolean');
  224. });
  225. });
  226. ifdescribe(process.platform === 'darwin')('systemPreferences.canPromptTouchID()', () => {
  227. it('returns a boolean', () => {
  228. expect(systemPreferences.canPromptTouchID()).to.be.a('boolean');
  229. });
  230. });
  231. ifdescribe(process.platform === 'darwin')('systemPreferences.isTrustedAccessibilityClient(prompt)', () => {
  232. it('returns a boolean', () => {
  233. const trusted = systemPreferences.isTrustedAccessibilityClient(false);
  234. expect(trusted).to.be.a('boolean');
  235. });
  236. });
  237. ifdescribe(['win32', 'darwin'].includes(process.platform))('systemPreferences.getMediaAccessStatus(mediaType)', () => {
  238. const statuses = ['not-determined', 'granted', 'denied', 'restricted', 'unknown'];
  239. it('returns an access status for a camera access request', () => {
  240. const cameraStatus = systemPreferences.getMediaAccessStatus('camera');
  241. expect(statuses).to.include(cameraStatus);
  242. });
  243. it('returns an access status for a microphone access request', () => {
  244. const microphoneStatus = systemPreferences.getMediaAccessStatus('microphone');
  245. expect(statuses).to.include(microphoneStatus);
  246. });
  247. it('returns an access status for a screen access request', () => {
  248. const screenStatus = systemPreferences.getMediaAccessStatus('screen');
  249. expect(statuses).to.include(screenStatus);
  250. });
  251. });
  252. describe('systemPreferences.getAnimationSettings()', () => {
  253. it('returns an object with all properties', () => {
  254. const settings = systemPreferences.getAnimationSettings();
  255. expect(settings).to.be.an('object');
  256. expect(settings).to.have.property('shouldRenderRichAnimation').that.is.a('boolean');
  257. expect(settings).to.have.property('scrollAnimationsEnabledBySystem').that.is.a('boolean');
  258. expect(settings).to.have.property('prefersReducedMotion').that.is.a('boolean');
  259. });
  260. });
  261. });