electron/spec-main/visibility-state-spec.ts

191 lines
6 KiB
TypeScript
Raw Normal View History

2020-03-20 20:28:31 +00:00
import { expect } from 'chai';
import * as cp from 'child_process';
import { BrowserWindow, BrowserWindowConstructorOptions, ipcMain } from 'electron/main';
2020-03-20 20:28:31 +00:00
import * as path from 'path';
2020-03-20 20:28:31 +00:00
import { emittedOnce } from './events-helpers';
import { closeWindow } from './window-helpers';
import { ifdescribe, delay } from './spec-helpers';
// 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', () => {
2020-03-20 20:28:31 +00:00
let w: BrowserWindow;
afterEach(() => {
2020-03-20 20:28:31 +00:00
return closeWindow(w);
});
2020-03-20 20:28:31 +00:00
const load = () => w.loadFile(path.resolve(__dirname, 'fixtures', 'chromium', 'visibilitystate.html'));
const itWithOptions = (name: string, options: BrowserWindowConstructorOptions, fn: Mocha.Func) => {
return it(name, async function (...args) {
// document.visibilityState tests are very flaky, this is probably because
// Electron implements it via async IPC messages.
2020-03-20 20:28:31 +00:00
this.retries(3);
w = new BrowserWindow({
...options,
paintWhenInitiallyHidden: false,
webPreferences: {
...(options.webPreferences || {}),
nodeIntegration: true
}
2020-03-20 20:28:31 +00:00
});
await Promise.resolve(fn.apply(this, args));
});
};
const getVisibilityState = async (): Promise<string> => {
2020-03-20 20:28:31 +00:00
const response = emittedOnce(ipcMain, 'visibility-state');
w.webContents.send('get-visibility-state');
return (await response)[1];
};
itWithOptions('should be visible when the window is initially shown by default', {}, async () => {
2020-03-20 20:28:31 +00:00
await load();
const state = await getVisibilityState();
expect(state).to.equal('visible');
});
itWithOptions('should be visible when the window is initially shown', {
2019-11-01 20:37:02 +00:00
show: true
}, async () => {
2020-03-20 20:28:31 +00:00
await load();
const state = await getVisibilityState();
expect(state).to.equal('visible');
});
itWithOptions('should be hidden when the window is initially hidden', {
2019-11-01 20:37:02 +00:00
show: false
}, async () => {
2020-03-20 20:28:31 +00:00
await load();
const state = await getVisibilityState();
expect(state).to.equal('hidden');
});
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
}, async () => {
2020-03-20 20:28:31 +00:00
w.show();
await load();
const state = await getVisibilityState();
expect(state).to.equal('visible');
});
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
}, async () => {
// TODO(MarshallOfSound): Figure out if we can work around this 1 tick issue for users
if (process.platform === 'darwin') {
// Wait for a tick, the window being "shown" takes 1 tick on macOS
2020-03-20 20:28:31 +00:00
await delay(0);
}
2020-03-20 20:28:31 +00:00
w.hide();
await load();
const state = await getVisibilityState();
expect(state).to.equal('hidden');
});
itWithOptions('should be toggle between visible and hidden as the window is hidden and shown', {}, async () => {
2020-03-20 20:28:31 +00:00
await load();
expect(await getVisibilityState()).to.equal('visible');
await emittedOnce(ipcMain, 'visibility-change', () => w.hide());
expect(await getVisibilityState()).to.equal('hidden');
await emittedOnce(ipcMain, 'visibility-change', () => w.show());
expect(await getVisibilityState()).to.equal('visible');
});
itWithOptions('should become hidden when a window is minimized', {}, async () => {
2020-03-20 20:28:31 +00:00
await load();
expect(await getVisibilityState()).to.equal('visible');
await emittedOnce(ipcMain, 'visibility-change', () => w.minimize());
expect(await getVisibilityState()).to.equal('hidden');
});
itWithOptions('should become visible when a window is restored', {}, async () => {
2020-03-20 20:28:31 +00:00
await load();
expect(await getVisibilityState()).to.equal('visible');
await emittedOnce(ipcMain, 'visibility-change', () => w.minimize());
await emittedOnce(ipcMain, 'visibility-change', () => w.restore());
expect(await getVisibilityState()).to.equal('visible');
});
describe('on platforms that support occlusion detection', () => {
2020-03-20 20:28:31 +00:00
let child: cp.ChildProcess;
2019-11-01 20:37:02 +00:00
before(function () {
2020-03-20 20:28:31 +00:00
if (process.platform !== 'darwin') this.skip();
});
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}`]);
return new Promise(resolve => {
child.stdout!.on('data', (chunk) => {
2020-03-20 20:28:31 +00:00
if (chunk.toString().includes('__ready__')) resolve();
});
});
};
afterEach(() => {
if (child && !child.killed) {
2020-03-20 20:28:31 +00:00
child.kill('SIGTERM');
}
2020-03-20 20:28:31 +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
}, 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
});
await load();
const state = await getVisibilityState();
expect(state).to.equal('visible');
});
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
}, 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
});
await load();
const state = await getVisibilityState();
expect(state).to.equal('visible');
});
itWithOptions('should be hidden when a second window completely conceals the current window', {
x: 50,
y: 50,
width: 50,
2019-11-01 20:37:02 +00:00
height: 50
}, async function () {
2020-03-20 20:28:31 +00:00
this.timeout(240000);
await load();
await emittedOnce(ipcMain, 'visibility-change', async () => {
await makeOtherWindow({
x: 0,
y: 0,
width: 300,
2019-11-01 20:37:02 +00:00
height: 300
2020-03-20 20:28:31 +00:00
});
});
const state = await getVisibilityState();
expect(state).to.equal('hidden');
});
});
});