api-system-preferences-spec.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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.subscribeNotification(event, callback)', () => {
  103. it('throws an error if invalid arguments are passed', () => {
  104. const badArgs = [123, {}, ['hi', 'bye'], new Date()];
  105. for (const bad of badArgs) {
  106. expect(() => {
  107. systemPreferences.subscribeNotification(bad as any, () => {});
  108. }).to.throw('Must pass null or a string');
  109. }
  110. });
  111. });
  112. ifdescribe(process.platform === 'darwin')('systemPreferences.subscribeLocalNotification(event, callback)', () => {
  113. it('throws an error if invalid arguments are passed', () => {
  114. const badArgs = [123, {}, ['hi', 'bye'], new Date()];
  115. for (const bad of badArgs) {
  116. expect(() => {
  117. systemPreferences.subscribeNotification(bad as any, () => {});
  118. }).to.throw('Must pass null or a string');
  119. }
  120. });
  121. });
  122. ifdescribe(process.platform === 'darwin')('systemPreferences.subscribeWorkspaceNotification(event, callback)', () => {
  123. it('throws an error if invalid arguments are passed', () => {
  124. const badArgs = [123, {}, ['hi', 'bye'], new Date()];
  125. for (const bad of badArgs) {
  126. expect(() => {
  127. systemPreferences.subscribeWorkspaceNotification(bad as any, () => {});
  128. }).to.throw('Must pass null or a string');
  129. }
  130. });
  131. });
  132. ifdescribe(process.platform === 'darwin')('systemPreferences.getSystemColor(color)', () => {
  133. it('throws on invalid system colors', () => {
  134. const color = 'bad-color';
  135. expect(() => {
  136. systemPreferences.getSystemColor(color as any);
  137. }).to.throw(`Unknown system color: ${color}`);
  138. });
  139. it('returns a valid system color', () => {
  140. const colors = ['blue', 'brown', 'gray', 'green', 'orange', 'pink', 'purple', 'red', 'yellow'];
  141. colors.forEach(color => {
  142. const sysColor = systemPreferences.getSystemColor(color as any);
  143. expect(sysColor).to.be.a('string');
  144. });
  145. });
  146. });
  147. ifdescribe(process.platform === 'darwin')('systemPreferences.getColor(color)', () => {
  148. it('throws on invalid colors', () => {
  149. const color = 'bad-color';
  150. expect(() => {
  151. systemPreferences.getColor(color as any);
  152. }).to.throw(`Unknown color: ${color}`);
  153. });
  154. it('returns a valid color', () => {
  155. const colors = [
  156. 'alternate-selected-control-text',
  157. 'control-background',
  158. 'control',
  159. 'control-text',
  160. 'disabled-control-text',
  161. 'find-highlight',
  162. 'grid',
  163. 'header-text',
  164. 'highlight',
  165. 'keyboard-focus-indicator',
  166. 'label',
  167. 'link',
  168. 'placeholder-text',
  169. 'quaternary-label',
  170. 'scrubber-textured-background',
  171. 'secondary-label',
  172. 'selected-content-background',
  173. 'selected-control',
  174. 'selected-control-text',
  175. 'selected-menu-item-text',
  176. 'selected-text-background',
  177. 'selected-text',
  178. 'separator',
  179. 'shadow',
  180. 'tertiary-label',
  181. 'text-background',
  182. 'text',
  183. 'under-page-background',
  184. 'unemphasized-selected-content-background',
  185. 'unemphasized-selected-text-background',
  186. 'unemphasized-selected-text',
  187. 'window-background',
  188. 'window-frame-text'
  189. ];
  190. colors.forEach(color => {
  191. const sysColor = systemPreferences.getColor(color as any);
  192. expect(sysColor).to.be.a('string');
  193. });
  194. });
  195. });
  196. ifdescribe(process.platform === 'darwin')('systemPreferences.appLevelAppearance', () => {
  197. const options = ['dark', 'light', 'unknown', null];
  198. describe('with properties', () => {
  199. it('returns a valid appearance', () => {
  200. const appearance = systemPreferences.appLevelAppearance;
  201. expect(options).to.include(appearance);
  202. });
  203. it('can be changed', () => {
  204. systemPreferences.appLevelAppearance = 'dark';
  205. expect(systemPreferences.appLevelAppearance).to.eql('dark');
  206. });
  207. });
  208. describe('with functions', () => {
  209. it('returns a valid appearance', () => {
  210. const appearance = systemPreferences.getAppLevelAppearance();
  211. expect(options).to.include(appearance);
  212. });
  213. it('can be changed', () => {
  214. systemPreferences.setAppLevelAppearance('dark');
  215. const appearance = systemPreferences.getAppLevelAppearance();
  216. expect(appearance).to.eql('dark');
  217. });
  218. });
  219. });
  220. ifdescribe(process.platform === 'darwin')('systemPreferences.effectiveAppearance', () => {
  221. const options = ['dark', 'light', 'unknown'];
  222. describe('with properties', () => {
  223. it('returns a valid appearance', () => {
  224. const appearance = systemPreferences.effectiveAppearance;
  225. expect(options).to.include(appearance);
  226. });
  227. });
  228. describe('with functions', () => {
  229. it('returns a valid appearance', () => {
  230. const appearance = systemPreferences.getEffectiveAppearance();
  231. expect(options).to.include(appearance);
  232. });
  233. });
  234. });
  235. ifdescribe(process.platform === 'darwin')('systemPreferences.setUserDefault(key, type, value)', () => {
  236. it('removes keys', () => {
  237. const KEY = 'SystemPreferencesTest';
  238. systemPreferences.setUserDefault(KEY, 'string', 'foo');
  239. systemPreferences.removeUserDefault(KEY);
  240. expect(systemPreferences.getUserDefault(KEY, 'string')).to.equal('');
  241. });
  242. it('does not throw for missing keys', () => {
  243. systemPreferences.removeUserDefault('some-missing-key');
  244. });
  245. });
  246. ifdescribe(process.platform === 'darwin')('systemPreferences.canPromptTouchID()', () => {
  247. it('returns a boolean', () => {
  248. expect(systemPreferences.canPromptTouchID()).to.be.a('boolean');
  249. });
  250. });
  251. ifdescribe(process.platform === 'darwin')('systemPreferences.isTrustedAccessibilityClient(prompt)', () => {
  252. it('returns a boolean', () => {
  253. const trusted = systemPreferences.isTrustedAccessibilityClient(false);
  254. expect(trusted).to.be.a('boolean');
  255. });
  256. });
  257. ifdescribe(['win32', 'darwin'].includes(process.platform))('systemPreferences.getMediaAccessStatus(mediaType)', () => {
  258. const statuses = ['not-determined', 'granted', 'denied', 'restricted', 'unknown'];
  259. it('returns an access status for a camera access request', () => {
  260. const cameraStatus = systemPreferences.getMediaAccessStatus('camera');
  261. expect(statuses).to.include(cameraStatus);
  262. });
  263. it('returns an access status for a microphone access request', () => {
  264. const microphoneStatus = systemPreferences.getMediaAccessStatus('microphone');
  265. expect(statuses).to.include(microphoneStatus);
  266. });
  267. it('returns an access status for a screen access request', () => {
  268. const screenStatus = systemPreferences.getMediaAccessStatus('screen');
  269. expect(statuses).to.include(screenStatus);
  270. });
  271. });
  272. describe('systemPreferences.getAnimationSettings()', () => {
  273. it('returns an object with all properties', () => {
  274. const settings = systemPreferences.getAnimationSettings();
  275. expect(settings).to.be.an('object');
  276. expect(settings).to.have.property('shouldRenderRichAnimation').that.is.a('boolean');
  277. expect(settings).to.have.property('scrollAnimationsEnabledBySystem').that.is.a('boolean');
  278. expect(settings).to.have.property('prefersReducedMotion').that.is.a('boolean');
  279. });
  280. });
  281. });