electron/spec/api-native-theme-spec.ts

109 lines
4 KiB
TypeScript
Raw Normal View History

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