2020-03-20 14:28:31 -06:00
|
|
|
import { expect } from 'chai';
|
2023-09-11 09:34:13 -06:00
|
|
|
import { nativeTheme, BrowserWindow, ipcMain } from 'electron/main';
|
2023-06-15 08:42:27 -06:00
|
|
|
import { once } from 'node:events';
|
|
|
|
import * as path from 'node:path';
|
|
|
|
import { setTimeout } from 'node:timers/promises';
|
2019-09-05 11:57:04 -06:00
|
|
|
|
2023-01-25 14:01:25 -07:00
|
|
|
import { closeAllWindows } from './lib/window-helpers';
|
2019-08-14 14:42:55 -06:00
|
|
|
|
2019-09-16 17:08:41 -06:00
|
|
|
describe('nativeTheme module', () => {
|
2019-08-14 14:42:55 -06:00
|
|
|
describe('nativeTheme.shouldUseDarkColors', () => {
|
|
|
|
it('returns a boolean', () => {
|
2020-03-20 14:28:31 -06:00
|
|
|
expect(nativeTheme.shouldUseDarkColors).to.be.a('boolean');
|
|
|
|
});
|
|
|
|
});
|
2019-08-14 14:42:55 -06:00
|
|
|
|
2019-09-05 11:57:04 -06:00
|
|
|
describe('nativeTheme.themeSource', () => {
|
2019-09-16 17:08:01 -06:00
|
|
|
afterEach(async () => {
|
2020-03-20 14:28:31 -06:00
|
|
|
nativeTheme.themeSource = 'system';
|
2019-09-16 17:08:01 -06:00
|
|
|
// Wait for any pending events to emit
|
2023-02-23 16:53:53 -07:00
|
|
|
await setTimeout(20);
|
2020-03-30 16:39:50 -06:00
|
|
|
|
|
|
|
closeAllWindows();
|
2020-03-20 14:28:31 -06:00
|
|
|
});
|
2019-09-05 11:57:04 -06:00
|
|
|
|
|
|
|
it('is system by default', () => {
|
2020-03-20 14:28:31 -06:00
|
|
|
expect(nativeTheme.themeSource).to.equal('system');
|
|
|
|
});
|
2019-09-05 11:57:04 -06:00
|
|
|
|
|
|
|
it('should override the value of shouldUseDarkColors', () => {
|
2020-03-20 14:28:31 -06:00
|
|
|
nativeTheme.themeSource = 'dark';
|
|
|
|
expect(nativeTheme.shouldUseDarkColors).to.equal(true);
|
|
|
|
nativeTheme.themeSource = 'light';
|
|
|
|
expect(nativeTheme.shouldUseDarkColors).to.equal(false);
|
|
|
|
});
|
2019-09-05 11:57:04 -06:00
|
|
|
|
2019-09-16 17:08:01 -06:00
|
|
|
it('should emit the "updated" event when it is set and the resulting "shouldUseDarkColors" value changes', async () => {
|
2023-01-19 19:59:20 -07:00
|
|
|
nativeTheme.themeSource = 'light';
|
2023-02-23 16:53:53 -07:00
|
|
|
let updatedEmitted = once(nativeTheme, 'updated');
|
2020-03-20 14:28:31 -06:00
|
|
|
nativeTheme.themeSource = 'dark';
|
|
|
|
await updatedEmitted;
|
2023-02-23 16:53:53 -07:00
|
|
|
updatedEmitted = once(nativeTheme, 'updated');
|
2020-03-20 14:28:31 -06:00
|
|
|
nativeTheme.themeSource = 'light';
|
|
|
|
await updatedEmitted;
|
|
|
|
});
|
2019-09-05 11:57:04 -06:00
|
|
|
|
2019-09-16 17:08:01 -06:00
|
|
|
it('should not emit the "updated" event when it is set and the resulting "shouldUseDarkColors" value is the same', async () => {
|
2020-03-20 14:28:31 -06:00
|
|
|
nativeTheme.themeSource = 'dark';
|
2019-09-16 17:08:01 -06:00
|
|
|
// Wait a few ticks to allow an async events to flush
|
2023-02-23 16:53:53 -07:00
|
|
|
await setTimeout(20);
|
2020-03-20 14:28:31 -06:00
|
|
|
let called = false;
|
2019-09-05 11:57:04 -06:00
|
|
|
nativeTheme.once('updated', () => {
|
2020-03-20 14:28:31 -06:00
|
|
|
called = true;
|
|
|
|
});
|
|
|
|
nativeTheme.themeSource = 'dark';
|
2019-09-16 17:08:01 -06:00
|
|
|
// Wait a few ticks to allow an async events to flush
|
2023-02-23 16:53:53 -07:00
|
|
|
await setTimeout(20);
|
2020-03-20 14:28:31 -06:00
|
|
|
expect(called).to.equal(false);
|
|
|
|
});
|
2020-03-30 16:39:50 -06:00
|
|
|
|
|
|
|
const getPrefersColorSchemeIsDark = async (w: Electron.BrowserWindow) => {
|
|
|
|
const isDark: boolean = await w.webContents.executeJavaScript(
|
|
|
|
'matchMedia("(prefers-color-scheme: dark)").matches'
|
|
|
|
);
|
|
|
|
return isDark;
|
|
|
|
};
|
|
|
|
|
|
|
|
it('should override the result of prefers-color-scheme CSS media query', async () => {
|
2021-05-05 19:04:38 -06:00
|
|
|
const w = new BrowserWindow({ show: false, webPreferences: { contextIsolation: false, nodeIntegration: true } });
|
2020-03-30 16:39:50 -06:00
|
|
|
await w.loadFile(path.resolve(__dirname, 'fixtures', 'blank.html'));
|
2021-05-05 19:04:38 -06:00
|
|
|
await w.webContents.executeJavaScript(`
|
|
|
|
window.matchMedia('(prefers-color-scheme: dark)')
|
|
|
|
.addEventListener('change', () => require('electron').ipcRenderer.send('theme-change'))
|
|
|
|
`);
|
2020-03-30 16:39:50 -06:00
|
|
|
const originalSystemIsDark = await getPrefersColorSchemeIsDark(w);
|
2023-06-22 12:38:52 -06:00
|
|
|
let changePromise = once(ipcMain, 'theme-change');
|
2020-03-30 16:39:50 -06:00
|
|
|
nativeTheme.themeSource = 'dark';
|
2021-05-05 19:04:38 -06:00
|
|
|
if (!originalSystemIsDark) await changePromise;
|
2020-03-30 16:39:50 -06:00
|
|
|
expect(await getPrefersColorSchemeIsDark(w)).to.equal(true);
|
2023-02-23 16:53:53 -07:00
|
|
|
changePromise = once(ipcMain, 'theme-change');
|
2020-03-30 16:39:50 -06:00
|
|
|
nativeTheme.themeSource = 'light';
|
2021-05-05 19:04:38 -06:00
|
|
|
await changePromise;
|
2020-03-30 16:39:50 -06:00
|
|
|
expect(await getPrefersColorSchemeIsDark(w)).to.equal(false);
|
2023-02-23 16:53:53 -07:00
|
|
|
changePromise = once(ipcMain, 'theme-change');
|
2020-03-30 16:39:50 -06:00
|
|
|
nativeTheme.themeSource = 'system';
|
2021-05-05 19:04:38 -06:00
|
|
|
if (originalSystemIsDark) await changePromise;
|
2020-03-30 16:39:50 -06:00
|
|
|
expect(await getPrefersColorSchemeIsDark(w)).to.equal(originalSystemIsDark);
|
|
|
|
w.close();
|
|
|
|
});
|
2020-03-20 14:28:31 -06:00
|
|
|
});
|
2019-09-05 11:57:04 -06:00
|
|
|
|
2019-08-14 14:42:55 -06:00
|
|
|
describe('nativeTheme.shouldUseInvertedColorScheme', () => {
|
|
|
|
it('returns a boolean', () => {
|
2020-03-20 14:28:31 -06:00
|
|
|
expect(nativeTheme.shouldUseInvertedColorScheme).to.be.a('boolean');
|
|
|
|
});
|
|
|
|
});
|
2019-08-14 14:42:55 -06:00
|
|
|
|
|
|
|
describe('nativeTheme.shouldUseHighContrastColors', () => {
|
|
|
|
it('returns a boolean', () => {
|
2020-03-20 14:28:31 -06:00
|
|
|
expect(nativeTheme.shouldUseHighContrastColors).to.be.a('boolean');
|
|
|
|
});
|
|
|
|
});
|
2022-03-21 03:30:02 -06:00
|
|
|
|
|
|
|
describe('nativeTheme.inForcedColorsMode', () => {
|
|
|
|
it('returns a boolean', () => {
|
|
|
|
expect(nativeTheme.inForcedColorsMode).to.be.a('boolean');
|
|
|
|
});
|
|
|
|
});
|
2020-03-20 14:28:31 -06:00
|
|
|
});
|