forked from electron/electron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi-native-theme-spec.ts
118 lines (104 loc) · 4.43 KB
/
api-native-theme-spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import { expect } from 'chai';
import { nativeTheme, systemPreferences, BrowserWindow, ipcMain } from 'electron/main';
import { once } from 'node:events';
import * as path from 'node:path';
import { setTimeout } from 'node:timers/promises';
import { ifdescribe } from './lib/spec-helpers';
import { closeAllWindows } from './lib/window-helpers';
describe('nativeTheme module', () => {
describe('nativeTheme.shouldUseDarkColors', () => {
it('returns a boolean', () => {
expect(nativeTheme.shouldUseDarkColors).to.be.a('boolean');
});
});
describe('nativeTheme.themeSource', () => {
afterEach(async () => {
nativeTheme.themeSource = 'system';
// Wait for any pending events to emit
await setTimeout(20);
closeAllWindows();
});
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);
});
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');
nativeTheme.themeSource = 'dark';
await updatedEmitted;
updatedEmitted = once(nativeTheme, 'updated');
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 () => {
nativeTheme.themeSource = 'dark';
// Wait a few ticks to allow an async events to flush
await setTimeout(20);
let called = false;
nativeTheme.once('updated', () => {
called = true;
});
nativeTheme.themeSource = 'dark';
// Wait a few ticks to allow an async events to flush
await setTimeout(20);
expect(called).to.equal(false);
});
ifdescribe(process.platform === 'darwin')('on macOS', () => {
it('should update appLevelAppearance when set', () => {
nativeTheme.themeSource = 'dark';
expect(systemPreferences.appLevelAppearance).to.equal('dark');
nativeTheme.themeSource = 'light';
expect(systemPreferences.appLevelAppearance).to.equal('light');
});
});
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();
});
});
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');
});
});
describe('nativeTheme.inForcedColorsMode', () => {
it('returns a boolean', () => {
expect(nativeTheme.inForcedColorsMode).to.be.a('boolean');
});
});
});