2024-10-14 13:39:19 +00:00
|
|
|
import { BaseWindow, BrowserWindow, BrowserWindowConstructorOptions, WebContents, WebContentsView } from 'electron/main';
|
2024-10-03 03:52:01 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
import { expect } from 'chai';
|
2024-10-03 03:52:01 +00:00
|
|
|
|
2023-06-15 14:42:27 +00:00
|
|
|
import * as cp from 'node:child_process';
|
2024-10-03 03:52:01 +00:00
|
|
|
import { once } from 'node:events';
|
2023-06-15 14:42:27 +00:00
|
|
|
import * as path from 'node:path';
|
2019-09-05 17:56:06 +00:00
|
|
|
|
2024-10-14 13:39:19 +00:00
|
|
|
import { ifdescribe, waitUntil } from './lib/spec-helpers';
|
|
|
|
import { closeAllWindows } from './lib/window-helpers';
|
2019-09-05 17:56:06 +00:00
|
|
|
|
|
|
|
// visibilityState specs pass on linux with a real window manager but on CI
|
|
|
|
// the environment does not let these specs pass
|
2019-10-30 23:38:21 +00:00
|
|
|
ifdescribe(process.platform !== 'linux')('document.visibilityState', () => {
|
2023-12-13 21:01:03 +00:00
|
|
|
let w: BaseWindow & {webContents: WebContents};
|
2019-09-05 17:56:06 +00:00
|
|
|
|
2024-10-14 13:39:19 +00:00
|
|
|
before(() => {
|
|
|
|
for (const checkWin of BaseWindow.getAllWindows()) {
|
|
|
|
console.log('WINDOW EXISTS BEFORE TEST STARTED:', checkWin.title, checkWin.id);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(async () => {
|
|
|
|
await closeAllWindows();
|
|
|
|
w = null as unknown as BrowserWindow;
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-09-05 17:56:06 +00:00
|
|
|
|
2023-12-13 21:01:03 +00:00
|
|
|
const load = () => w.webContents.loadFile(path.resolve(__dirname, 'fixtures', 'chromium', 'visibilitystate.html'));
|
2019-09-05 17:56:06 +00:00
|
|
|
|
2024-10-14 13:39:19 +00:00
|
|
|
async function haveVisibilityState (state: string) {
|
|
|
|
const docVisState = await w.webContents.executeJavaScript('document.visibilityState');
|
|
|
|
return docVisState === state;
|
|
|
|
}
|
|
|
|
|
2019-09-05 17:56:06 +00:00
|
|
|
const itWithOptions = (name: string, options: BrowserWindowConstructorOptions, fn: Mocha.Func) => {
|
2023-12-13 21:01:03 +00:00
|
|
|
it(name, async function (...args) {
|
2019-09-05 17:56:06 +00:00
|
|
|
w = new BrowserWindow({
|
|
|
|
...options,
|
|
|
|
paintWhenInitiallyHidden: false,
|
|
|
|
webPreferences: {
|
|
|
|
...(options.webPreferences || {}),
|
2021-03-01 21:52:29 +00:00
|
|
|
nodeIntegration: true,
|
|
|
|
contextIsolation: false
|
2019-09-05 17:56:06 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2024-10-14 13:39:19 +00:00
|
|
|
if (options.show && process.platform === 'darwin') {
|
|
|
|
await once(w, 'show');
|
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
await Promise.resolve(fn.apply(this, args));
|
|
|
|
});
|
2023-12-13 21:01:03 +00:00
|
|
|
|
|
|
|
it(name + ' with BaseWindow', async function (...args) {
|
|
|
|
const baseWindow = new BaseWindow({
|
|
|
|
...options
|
|
|
|
});
|
|
|
|
const wcv = new WebContentsView({ webPreferences: { ...(options.webPreferences ?? {}), nodeIntegration: true, contextIsolation: false } });
|
|
|
|
baseWindow.contentView = wcv;
|
|
|
|
w = Object.assign(baseWindow, { webContents: wcv.webContents });
|
2024-10-14 13:39:19 +00:00
|
|
|
if (options.show && process.platform === 'darwin') {
|
|
|
|
await once(w, 'show');
|
|
|
|
}
|
2023-12-13 21:01:03 +00:00
|
|
|
await Promise.resolve(fn.apply(this, args));
|
|
|
|
});
|
2020-03-20 20:28:31 +00:00
|
|
|
};
|
2019-09-05 17:56:06 +00:00
|
|
|
|
|
|
|
itWithOptions('should be visible when the window is initially shown by default', {}, async () => {
|
2020-10-16 01:30:41 +00:00
|
|
|
load();
|
2024-10-14 13:39:19 +00:00
|
|
|
await expect(waitUntil(async () => await haveVisibilityState('visible'))).to.eventually.be.fulfilled();
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-09-05 17:56:06 +00:00
|
|
|
|
|
|
|
itWithOptions('should be visible when the window is initially shown', {
|
2019-11-01 20:37:02 +00:00
|
|
|
show: true
|
2019-09-05 17:56:06 +00:00
|
|
|
}, async () => {
|
2020-10-16 01:30:41 +00:00
|
|
|
load();
|
2024-10-14 13:39:19 +00:00
|
|
|
await expect(waitUntil(async () => await haveVisibilityState('visible'))).to.eventually.be.fulfilled();
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-09-05 17:56:06 +00:00
|
|
|
|
|
|
|
itWithOptions('should be hidden when the window is initially hidden', {
|
2019-11-01 20:37:02 +00:00
|
|
|
show: false
|
2019-09-05 17:56:06 +00:00
|
|
|
}, async () => {
|
2020-10-16 01:30:41 +00:00
|
|
|
load();
|
2024-10-14 13:39:19 +00:00
|
|
|
await expect(waitUntil(async () => await haveVisibilityState('hidden'))).to.eventually.be.fulfilled();
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-09-05 17:56:06 +00:00
|
|
|
|
|
|
|
itWithOptions('should be visible when the window is initially hidden but shown before the page is loaded', {
|
2019-11-01 20:37:02 +00:00
|
|
|
show: false
|
2019-09-05 17:56:06 +00:00
|
|
|
}, async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
w.show();
|
2020-10-16 01:30:41 +00:00
|
|
|
load();
|
2024-10-14 13:39:19 +00:00
|
|
|
await expect(waitUntil(async () => await haveVisibilityState('visible'))).to.eventually.be.fulfilled();
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-09-05 17:56:06 +00:00
|
|
|
|
|
|
|
itWithOptions('should be hidden when the window is initially shown but hidden before the page is loaded', {
|
2019-11-01 20:37:02 +00:00
|
|
|
show: true
|
2019-09-05 17:56:06 +00:00
|
|
|
}, async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
w.hide();
|
2020-10-16 01:30:41 +00:00
|
|
|
load();
|
2024-10-14 13:39:19 +00:00
|
|
|
await expect(waitUntil(async () => await haveVisibilityState('hidden'))).to.eventually.be.fulfilled();
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-09-05 17:56:06 +00:00
|
|
|
|
|
|
|
itWithOptions('should be toggle between visible and hidden as the window is hidden and shown', {}, async () => {
|
2020-10-16 01:30:41 +00:00
|
|
|
load();
|
2024-10-14 13:39:19 +00:00
|
|
|
await expect(waitUntil(async () => await haveVisibilityState('visible'))).to.eventually.be.fulfilled();
|
2020-10-16 01:30:41 +00:00
|
|
|
w.hide();
|
2024-10-14 13:39:19 +00:00
|
|
|
await expect(waitUntil(async () => await haveVisibilityState('hidden'))).to.eventually.be.fulfilled();
|
2020-10-16 01:30:41 +00:00
|
|
|
w.show();
|
2024-10-14 13:39:19 +00:00
|
|
|
await expect(waitUntil(async () => await haveVisibilityState('visible'))).to.eventually.be.fulfilled();
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-09-05 17:56:06 +00:00
|
|
|
|
|
|
|
itWithOptions('should become hidden when a window is minimized', {}, async () => {
|
2020-10-16 01:30:41 +00:00
|
|
|
load();
|
2024-10-14 13:39:19 +00:00
|
|
|
await expect(waitUntil(async () => await haveVisibilityState('visible'))).to.eventually.be.fulfilled();
|
2023-02-23 23:53:53 +00:00
|
|
|
w.minimize();
|
2024-10-14 13:39:19 +00:00
|
|
|
await expect(waitUntil(async () => await haveVisibilityState('hidden'))).to.eventually.be.fulfilled();
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-09-05 17:56:06 +00:00
|
|
|
|
|
|
|
itWithOptions('should become visible when a window is restored', {}, async () => {
|
2020-10-16 01:30:41 +00:00
|
|
|
load();
|
2024-10-14 13:39:19 +00:00
|
|
|
await expect(waitUntil(async () => await haveVisibilityState('visible'))).to.eventually.be.fulfilled();
|
2020-10-16 01:30:41 +00:00
|
|
|
w.minimize();
|
2024-10-14 13:39:19 +00:00
|
|
|
await expect(waitUntil(async () => await haveVisibilityState('hidden'))).to.eventually.be.fulfilled();
|
2020-10-16 01:30:41 +00:00
|
|
|
w.restore();
|
2024-10-14 13:39:19 +00:00
|
|
|
await expect(waitUntil(async () => await haveVisibilityState('visible'))).to.eventually.be.fulfilled();
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-09-05 17:56:06 +00:00
|
|
|
|
2023-04-04 13:48:51 +00:00
|
|
|
ifdescribe(process.platform === 'darwin')('on platforms that support occlusion detection', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
let child: cp.ChildProcess;
|
2019-09-05 17:56:06 +00:00
|
|
|
|
|
|
|
const makeOtherWindow = (opts: { x: number; y: number; width: number; height: number; }) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
child = cp.spawn(process.execPath, [path.resolve(__dirname, 'fixtures', 'chromium', 'other-window.js'), `${opts.x}`, `${opts.y}`, `${opts.width}`, `${opts.height}`]);
|
2021-01-22 19:25:47 +00:00
|
|
|
return new Promise<void>(resolve => {
|
2019-09-05 17:56:06 +00:00
|
|
|
child.stdout!.on('data', (chunk) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
if (chunk.toString().includes('__ready__')) resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
2019-09-05 17:56:06 +00:00
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
if (child && !child.killed) {
|
2020-03-20 20:28:31 +00:00
|
|
|
child.kill('SIGTERM');
|
2019-09-05 17:56:06 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-09-05 17:56:06 +00:00
|
|
|
|
|
|
|
itWithOptions('should be visible when two windows are on screen', {
|
|
|
|
x: 0,
|
|
|
|
y: 0,
|
|
|
|
width: 200,
|
2019-11-01 20:37:02 +00:00
|
|
|
height: 200
|
2019-09-05 17:56:06 +00:00
|
|
|
}, async () => {
|
|
|
|
await makeOtherWindow({
|
|
|
|
x: 200,
|
|
|
|
y: 0,
|
|
|
|
width: 200,
|
2019-11-01 20:37:02 +00:00
|
|
|
height: 200
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2020-10-16 01:30:41 +00:00
|
|
|
load();
|
2024-10-14 13:39:19 +00:00
|
|
|
await expect(waitUntil(async () => await haveVisibilityState('visible'))).to.eventually.be.fulfilled();
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-09-05 17:56:06 +00:00
|
|
|
|
|
|
|
itWithOptions('should be visible when two windows are on screen that overlap partially', {
|
|
|
|
x: 50,
|
|
|
|
y: 50,
|
|
|
|
width: 150,
|
2019-11-01 20:37:02 +00:00
|
|
|
height: 150
|
2019-09-05 17:56:06 +00:00
|
|
|
}, async () => {
|
|
|
|
await makeOtherWindow({
|
|
|
|
x: 100,
|
|
|
|
y: 0,
|
|
|
|
width: 200,
|
2019-11-01 20:37:02 +00:00
|
|
|
height: 200
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2020-10-16 01:30:41 +00:00
|
|
|
load();
|
2024-10-14 13:39:19 +00:00
|
|
|
await expect(waitUntil(async () => await haveVisibilityState('visible'))).to.eventually.be.fulfilled();
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-09-05 17:56:06 +00:00
|
|
|
|
2020-10-16 01:30:41 +00:00
|
|
|
itWithOptions('should be hidden when a second window completely occludes the current window', {
|
2019-09-05 17:56:06 +00:00
|
|
|
x: 50,
|
|
|
|
y: 50,
|
|
|
|
width: 50,
|
2019-11-01 20:37:02 +00:00
|
|
|
height: 50
|
2019-09-05 17:56:06 +00:00
|
|
|
}, async function () {
|
2020-03-20 20:28:31 +00:00
|
|
|
this.timeout(240000);
|
2020-10-16 01:30:41 +00:00
|
|
|
load();
|
2024-10-14 13:39:19 +00:00
|
|
|
await expect(waitUntil(async () => await haveVisibilityState('visible'))).to.eventually.be.fulfilled();
|
2020-10-16 01:30:41 +00:00
|
|
|
makeOtherWindow({
|
|
|
|
x: 0,
|
|
|
|
y: 0,
|
|
|
|
width: 300,
|
|
|
|
height: 300
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2024-10-14 13:39:19 +00:00
|
|
|
await expect(waitUntil(async () => await haveVisibilityState('hidden'))).to.eventually.be.fulfilled();
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|