api-system-preferences-spec.ts 11 KB

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