api-system-preferences-spec.ts 11 KB

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