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