api-native-theme-spec.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import { expect } from 'chai';
  2. import { nativeTheme, systemPreferences, BrowserWindow, ipcMain } from 'electron/main';
  3. import { once } from 'events';
  4. import * as os from 'os';
  5. import * as path from 'path';
  6. import * as semver from 'semver';
  7. import { setTimeout } from 'timers/promises';
  8. import { ifdescribe } from './lib/spec-helpers';
  9. import { closeAllWindows } from './lib/window-helpers';
  10. describe('nativeTheme module', () => {
  11. describe('nativeTheme.shouldUseDarkColors', () => {
  12. it('returns a boolean', () => {
  13. expect(nativeTheme.shouldUseDarkColors).to.be.a('boolean');
  14. });
  15. });
  16. describe('nativeTheme.themeSource', () => {
  17. afterEach(async () => {
  18. nativeTheme.themeSource = 'system';
  19. // Wait for any pending events to emit
  20. await setTimeout(20);
  21. closeAllWindows();
  22. });
  23. it('is system by default', () => {
  24. expect(nativeTheme.themeSource).to.equal('system');
  25. });
  26. it('should override the value of shouldUseDarkColors', () => {
  27. nativeTheme.themeSource = 'dark';
  28. expect(nativeTheme.shouldUseDarkColors).to.equal(true);
  29. nativeTheme.themeSource = 'light';
  30. expect(nativeTheme.shouldUseDarkColors).to.equal(false);
  31. });
  32. it('should emit the "updated" event when it is set and the resulting "shouldUseDarkColors" value changes', async () => {
  33. nativeTheme.themeSource = 'light';
  34. let updatedEmitted = once(nativeTheme, 'updated');
  35. nativeTheme.themeSource = 'dark';
  36. await updatedEmitted;
  37. updatedEmitted = once(nativeTheme, 'updated');
  38. nativeTheme.themeSource = 'light';
  39. await updatedEmitted;
  40. });
  41. it('should not emit the "updated" event when it is set and the resulting "shouldUseDarkColors" value is the same', async () => {
  42. nativeTheme.themeSource = 'dark';
  43. // Wait a few ticks to allow an async events to flush
  44. await setTimeout(20);
  45. let called = false;
  46. nativeTheme.once('updated', () => {
  47. called = true;
  48. });
  49. nativeTheme.themeSource = 'dark';
  50. // Wait a few ticks to allow an async events to flush
  51. await setTimeout(20);
  52. expect(called).to.equal(false);
  53. });
  54. ifdescribe(process.platform === 'darwin' && semver.gte(os.release(), '18.0.0'))('on macOS 10.14', () => {
  55. it('should update appLevelAppearance when set', () => {
  56. nativeTheme.themeSource = 'dark';
  57. expect(systemPreferences.appLevelAppearance).to.equal('dark');
  58. nativeTheme.themeSource = 'light';
  59. expect(systemPreferences.appLevelAppearance).to.equal('light');
  60. });
  61. });
  62. const getPrefersColorSchemeIsDark = async (w: Electron.BrowserWindow) => {
  63. const isDark: boolean = await w.webContents.executeJavaScript(
  64. 'matchMedia("(prefers-color-scheme: dark)").matches'
  65. );
  66. return isDark;
  67. };
  68. it('should override the result of prefers-color-scheme CSS media query', async () => {
  69. const w = new BrowserWindow({ show: false, webPreferences: { contextIsolation: false, nodeIntegration: true } });
  70. await w.loadFile(path.resolve(__dirname, 'fixtures', 'blank.html'));
  71. await w.webContents.executeJavaScript(`
  72. window.matchMedia('(prefers-color-scheme: dark)')
  73. .addEventListener('change', () => require('electron').ipcRenderer.send('theme-change'))
  74. `);
  75. const originalSystemIsDark = await getPrefersColorSchemeIsDark(w);
  76. let changePromise: Promise<any[]> = once(ipcMain, 'theme-change');
  77. nativeTheme.themeSource = 'dark';
  78. if (!originalSystemIsDark) await changePromise;
  79. expect(await getPrefersColorSchemeIsDark(w)).to.equal(true);
  80. changePromise = once(ipcMain, 'theme-change');
  81. nativeTheme.themeSource = 'light';
  82. await changePromise;
  83. expect(await getPrefersColorSchemeIsDark(w)).to.equal(false);
  84. changePromise = once(ipcMain, 'theme-change');
  85. nativeTheme.themeSource = 'system';
  86. if (originalSystemIsDark) await changePromise;
  87. expect(await getPrefersColorSchemeIsDark(w)).to.equal(originalSystemIsDark);
  88. w.close();
  89. });
  90. });
  91. describe('nativeTheme.shouldUseInvertedColorScheme', () => {
  92. it('returns a boolean', () => {
  93. expect(nativeTheme.shouldUseInvertedColorScheme).to.be.a('boolean');
  94. });
  95. });
  96. describe('nativeTheme.shouldUseHighContrastColors', () => {
  97. it('returns a boolean', () => {
  98. expect(nativeTheme.shouldUseHighContrastColors).to.be.a('boolean');
  99. });
  100. });
  101. describe('nativeTheme.inForcedColorsMode', () => {
  102. it('returns a boolean', () => {
  103. expect(nativeTheme.inForcedColorsMode).to.be.a('boolean');
  104. });
  105. });
  106. });