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