api-native-theme-spec.ts 4.3 KB

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