api-system-preferences-spec.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. import { expect } from 'chai';
  2. import { systemPreferences } from 'electron/main';
  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 RGB color string', () => {
  19. expect(systemPreferences.getColor('window')).to.match(/^#[0-9A-F]{6}$/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. ];
  29. const defaultsDict: Record<string, any> = {};
  30. defaultsMap.forEach(row => { 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 as any);
  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 === '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', async () => {
  155. const colors = [
  156. 'control-background',
  157. 'control',
  158. 'control-text',
  159. 'disabled-control-text',
  160. 'find-highlight',
  161. 'grid',
  162. 'header-text',
  163. 'highlight',
  164. 'keyboard-focus-indicator',
  165. 'label',
  166. 'link',
  167. 'placeholder-text',
  168. 'quaternary-label',
  169. 'scrubber-textured-background',
  170. 'secondary-label',
  171. 'selected-content-background',
  172. 'selected-control',
  173. 'selected-control-text',
  174. 'selected-menu-item-text',
  175. 'selected-text-background',
  176. 'selected-text',
  177. 'separator',
  178. 'shadow',
  179. 'tertiary-label',
  180. 'text-background',
  181. 'text',
  182. 'under-page-background',
  183. 'unemphasized-selected-content-background',
  184. 'unemphasized-selected-text-background',
  185. 'unemphasized-selected-text',
  186. 'window-background',
  187. 'window-frame-text'
  188. ] as const;
  189. colors.forEach(color => {
  190. const sysColor = systemPreferences.getColor(color);
  191. expect(sysColor).to.be.a('string');
  192. });
  193. await expectDeprecationMessages(
  194. () => {
  195. const sysColor = systemPreferences.getColor('alternate-selected-control-text');
  196. expect(sysColor).to.be.a('string');
  197. },
  198. "'alternate-selected-control-text' is deprecated as an input to getColor. Use 'selected-content-background' instead."
  199. );
  200. });
  201. });
  202. ifdescribe(process.platform === 'darwin')('systemPreferences.appLevelAppearance', () => {
  203. const options = ['dark', 'light', 'unknown', null];
  204. describe('with properties', () => {
  205. it('returns a valid appearance', () => {
  206. const appearance = systemPreferences.appLevelAppearance;
  207. expect(options).to.include(appearance);
  208. });
  209. it('can be changed', () => {
  210. systemPreferences.appLevelAppearance = 'dark';
  211. expect(systemPreferences.appLevelAppearance).to.eql('dark');
  212. });
  213. });
  214. describe('with functions', () => {
  215. it('returns a valid appearance', async () => {
  216. await expectDeprecationMessages(
  217. () => {
  218. const appearance = systemPreferences.getAppLevelAppearance();
  219. expect(options).to.include(appearance);
  220. },
  221. "(electron) 'getAppLevelAppearance function' is deprecated and will be removed."
  222. );
  223. });
  224. it('can be changed', async () => {
  225. await expectDeprecationMessages(
  226. () => {
  227. systemPreferences.setAppLevelAppearance('dark');
  228. const appearance = systemPreferences.getAppLevelAppearance();
  229. expect(appearance).to.eql('dark');
  230. },
  231. "(electron) 'setAppLevelAppearance function' is deprecated and will be removed."
  232. );
  233. });
  234. });
  235. });
  236. ifdescribe(process.platform === 'darwin')('systemPreferences.effectiveAppearance', () => {
  237. const options = ['dark', 'light', 'unknown'];
  238. describe('with properties', () => {
  239. it('returns a valid appearance', () => {
  240. const appearance = systemPreferences.effectiveAppearance;
  241. expect(options).to.include(appearance);
  242. });
  243. });
  244. describe('with functions', () => {
  245. it('returns a valid appearance', () => {
  246. const appearance = systemPreferences.getEffectiveAppearance();
  247. expect(options).to.include(appearance);
  248. });
  249. });
  250. });
  251. ifdescribe(process.platform === 'darwin')('systemPreferences.setUserDefault(key, type, value)', () => {
  252. it('removes keys', () => {
  253. const KEY = 'SystemPreferencesTest';
  254. systemPreferences.setUserDefault(KEY, 'string', 'foo');
  255. systemPreferences.removeUserDefault(KEY);
  256. expect(systemPreferences.getUserDefault(KEY, 'string')).to.equal('');
  257. });
  258. it('does not throw for missing keys', () => {
  259. systemPreferences.removeUserDefault('some-missing-key');
  260. });
  261. });
  262. ifdescribe(process.platform === 'darwin')('systemPreferences.canPromptTouchID()', () => {
  263. it('returns a boolean', () => {
  264. expect(systemPreferences.canPromptTouchID()).to.be.a('boolean');
  265. });
  266. });
  267. ifdescribe(process.platform === 'darwin')('systemPreferences.isTrustedAccessibilityClient(prompt)', () => {
  268. it('returns a boolean', () => {
  269. const trusted = systemPreferences.isTrustedAccessibilityClient(false);
  270. expect(trusted).to.be.a('boolean');
  271. });
  272. });
  273. ifdescribe(['win32', 'darwin'].includes(process.platform))('systemPreferences.getMediaAccessStatus(mediaType)', () => {
  274. const statuses = ['not-determined', 'granted', 'denied', 'restricted', 'unknown'];
  275. it('returns an access status for a camera access request', () => {
  276. const cameraStatus = systemPreferences.getMediaAccessStatus('camera');
  277. expect(statuses).to.include(cameraStatus);
  278. });
  279. it('returns an access status for a microphone access request', () => {
  280. const microphoneStatus = systemPreferences.getMediaAccessStatus('microphone');
  281. expect(statuses).to.include(microphoneStatus);
  282. });
  283. it('returns an access status for a screen access request', () => {
  284. const screenStatus = systemPreferences.getMediaAccessStatus('screen');
  285. expect(statuses).to.include(screenStatus);
  286. });
  287. });
  288. describe('systemPreferences.getAnimationSettings()', () => {
  289. it('returns an object with all properties', () => {
  290. const settings = systemPreferences.getAnimationSettings();
  291. expect(settings).to.be.an('object');
  292. expect(settings).to.have.property('shouldRenderRichAnimation').that.is.a('boolean');
  293. expect(settings).to.have.property('scrollAnimationsEnabledBySystem').that.is.a('boolean');
  294. expect(settings).to.have.property('prefersReducedMotion').that.is.a('boolean');
  295. });
  296. });
  297. });