2020-03-20 20:28:31 +00:00
|
|
|
import { expect } from 'chai';
|
2021-05-06 01:04:38 +00:00
|
|
|
import { nativeTheme, systemPreferences, BrowserWindow, ipcMain } from 'electron/main';
|
2020-03-20 20:28:31 +00:00
|
|
|
import * as os from 'os';
|
2020-03-30 22:39:50 +00:00
|
|
|
import * as path from 'path';
|
2020-03-20 20:28:31 +00:00
|
|
|
import * as semver from 'semver';
|
2019-09-05 17:57:04 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
import { delay, ifdescribe } from './spec-helpers';
|
|
|
|
import { emittedOnce } from './events-helpers';
|
2020-03-30 22:39:50 +00:00
|
|
|
import { closeAllWindows } from './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
|
2020-03-20 20:28:31 +00:00
|
|
|
await delay(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';
|
2020-03-20 20:28:31 +00:00
|
|
|
let updatedEmitted = emittedOnce(nativeTheme, 'updated');
|
|
|
|
nativeTheme.themeSource = 'dark';
|
|
|
|
await updatedEmitted;
|
|
|
|
updatedEmitted = emittedOnce(nativeTheme, 'updated');
|
|
|
|
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
|
2020-03-20 20:28:31 +00:00
|
|
|
await delay(20);
|
|
|
|
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
|
2020-03-20 20:28:31 +00:00
|
|
|
await delay(20);
|
|
|
|
expect(called).to.equal(false);
|
|
|
|
});
|
2019-09-05 17:57:04 +00:00
|
|
|
|
|
|
|
ifdescribe(process.platform === 'darwin' && semver.gte(os.release(), '18.0.0'))('on macOS 10.14', () => {
|
|
|
|
it('should update appLevelAppearance when set', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
nativeTheme.themeSource = 'dark';
|
|
|
|
expect(systemPreferences.appLevelAppearance).to.equal('dark');
|
|
|
|
nativeTheme.themeSource = 'light';
|
|
|
|
expect(systemPreferences.appLevelAppearance).to.equal('light');
|
|
|
|
});
|
|
|
|
});
|
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);
|
2021-05-06 01:04:38 +00:00
|
|
|
let changePromise: Promise<any[]> = emittedOnce(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);
|
2021-05-06 01:04:38 +00:00
|
|
|
changePromise = emittedOnce(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);
|
2021-05-06 01:04:38 +00:00
|
|
|
changePromise = emittedOnce(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
|
|
|
});
|