|
| 1 | +import { expect } from 'chai' |
| 2 | +import * as cp from 'child_process'; |
| 3 | +import { BrowserWindow, BrowserWindowConstructorOptions, ipcMain } from 'electron' |
| 4 | +import * as path from 'path' |
| 5 | + |
| 6 | +import { emittedOnce } from './events-helpers' |
| 7 | +import { closeWindow } from './window-helpers'; |
| 8 | +import { ifdescribe } from './spec-helpers'; |
| 9 | + |
| 10 | +// visibilityState specs pass on linux with a real window manager but on CI |
| 11 | +// the environment does not let these specs pass |
| 12 | +ifdescribe(process.platform !== 'linux' || !isCI)('document.visibilityState', () => { |
| 13 | + let w: BrowserWindow |
| 14 | + |
| 15 | + afterEach(() => { |
| 16 | + return closeWindow(w) |
| 17 | + }) |
| 18 | + |
| 19 | + const load = () => w.loadFile(path.resolve(__dirname, 'fixtures', 'chromium', 'visibilitystate.html')) |
| 20 | + |
| 21 | + const itWithOptions = (name: string, options: BrowserWindowConstructorOptions, fn: Mocha.Func) => { |
| 22 | + return it(name, async function (...args) { |
| 23 | + w = new BrowserWindow({ |
| 24 | + ...options, |
| 25 | + paintWhenInitiallyHidden: false, |
| 26 | + webPreferences: { |
| 27 | + ...(options.webPreferences || {}), |
| 28 | + nodeIntegration: true |
| 29 | + } |
| 30 | + }) |
| 31 | + await Promise.resolve(fn.apply(this, args)) |
| 32 | + }) |
| 33 | + } |
| 34 | + |
| 35 | + const getVisibilityState = async (): Promise<string> => { |
| 36 | + const response = emittedOnce(ipcMain, 'visibility-state') |
| 37 | + w.webContents.send('get-visibility-state') |
| 38 | + return (await response)[1] |
| 39 | + } |
| 40 | + |
| 41 | + itWithOptions('should be visible when the window is initially shown by default', {}, async () => { |
| 42 | + await load() |
| 43 | + const state = await getVisibilityState() |
| 44 | + expect(state).to.equal('visible') |
| 45 | + }) |
| 46 | + |
| 47 | + itWithOptions('should be visible when the window is initially shown', { |
| 48 | + show: true, |
| 49 | + }, async () => { |
| 50 | + await load() |
| 51 | + const state = await getVisibilityState() |
| 52 | + expect(state).to.equal('visible') |
| 53 | + }) |
| 54 | + |
| 55 | + itWithOptions('should be hidden when the window is initially hidden', { |
| 56 | + show: false, |
| 57 | + }, async () => { |
| 58 | + await load() |
| 59 | + const state = await getVisibilityState() |
| 60 | + expect(state).to.equal('hidden') |
| 61 | + }) |
| 62 | + |
| 63 | + itWithOptions('should be visible when the window is initially hidden but shown before the page is loaded', { |
| 64 | + show: false, |
| 65 | + }, async () => { |
| 66 | + w.show() |
| 67 | + await load() |
| 68 | + const state = await getVisibilityState() |
| 69 | + expect(state).to.equal('visible') |
| 70 | + }) |
| 71 | + |
| 72 | + itWithOptions('should be hidden when the window is initially shown but hidden before the page is loaded', { |
| 73 | + show: true, |
| 74 | + }, async () => { |
| 75 | + // TODO(MarshallOfSound): Figure out if we can work around this 1 tick issue for users |
| 76 | + if (process.platform === 'darwin') { |
| 77 | + // Wait for a tick, the window being "shown" takes 1 tick on macOS |
| 78 | + await new Promise(r => setTimeout(r, 0)) |
| 79 | + } |
| 80 | + w.hide() |
| 81 | + await load() |
| 82 | + const state = await getVisibilityState() |
| 83 | + expect(state).to.equal('hidden') |
| 84 | + }) |
| 85 | + |
| 86 | + itWithOptions('should be toggle between visible and hidden as the window is hidden and shown', {}, async () => { |
| 87 | + await load() |
| 88 | + expect(await getVisibilityState()).to.equal('visible') |
| 89 | + await emittedOnce(ipcMain, 'visibility-change', () => w.hide()) |
| 90 | + expect(await getVisibilityState()).to.equal('hidden') |
| 91 | + await emittedOnce(ipcMain, 'visibility-change', () => w.show()) |
| 92 | + expect(await getVisibilityState()).to.equal('visible') |
| 93 | + }) |
| 94 | + |
| 95 | + itWithOptions('should become hidden when a window is minimized', {}, async () => { |
| 96 | + await load() |
| 97 | + expect(await getVisibilityState()).to.equal('visible') |
| 98 | + await emittedOnce(ipcMain, 'visibility-change', () => w.minimize()) |
| 99 | + expect(await getVisibilityState()).to.equal('hidden') |
| 100 | + }) |
| 101 | + |
| 102 | + itWithOptions('should become visible when a window is restored', {}, async () => { |
| 103 | + await load() |
| 104 | + expect(await getVisibilityState()).to.equal('visible') |
| 105 | + await emittedOnce(ipcMain, 'visibility-change', () => w.minimize()) |
| 106 | + await emittedOnce(ipcMain, 'visibility-change', () => w.restore()) |
| 107 | + expect(await getVisibilityState()).to.equal('visible') |
| 108 | + }) |
| 109 | + |
| 110 | + describe('on platforms that support occlusion detection', () => { |
| 111 | + let child: cp.ChildProcess |
| 112 | + |
| 113 | + before(function() { |
| 114 | + if (process.platform !== 'darwin') this.skip() |
| 115 | + }) |
| 116 | + |
| 117 | + const makeOtherWindow = (opts: { x: number; y: number; width: number; height: number; }) => { |
| 118 | + child = cp.spawn(process.execPath, [path.resolve(__dirname, 'fixtures', 'chromium', 'other-window.js'), `${opts.x}`, `${opts.y}`, `${opts.width}`, `${opts.height}`]) |
| 119 | + return new Promise(resolve => { |
| 120 | + child.stdout!.on('data', (chunk) => { |
| 121 | + if (chunk.toString().includes('__ready__')) resolve() |
| 122 | + }) |
| 123 | + }) |
| 124 | + } |
| 125 | + |
| 126 | + afterEach(() => { |
| 127 | + if (child && !child.killed) { |
| 128 | + child.kill('SIGTERM') |
| 129 | + } |
| 130 | + }) |
| 131 | + |
| 132 | + itWithOptions('should be visible when two windows are on screen', { |
| 133 | + x: 0, |
| 134 | + y: 0, |
| 135 | + width: 200, |
| 136 | + height: 200, |
| 137 | + }, async () => { |
| 138 | + await makeOtherWindow({ |
| 139 | + x: 200, |
| 140 | + y: 0, |
| 141 | + width: 200, |
| 142 | + height: 200, |
| 143 | + }) |
| 144 | + await load() |
| 145 | + const state = await getVisibilityState() |
| 146 | + expect(state).to.equal('visible') |
| 147 | + }) |
| 148 | + |
| 149 | + itWithOptions('should be visible when two windows are on screen that overlap partially', { |
| 150 | + x: 50, |
| 151 | + y: 50, |
| 152 | + width: 150, |
| 153 | + height: 150, |
| 154 | + }, async () => { |
| 155 | + await makeOtherWindow({ |
| 156 | + x: 100, |
| 157 | + y: 0, |
| 158 | + width: 200, |
| 159 | + height: 200, |
| 160 | + }) |
| 161 | + await load() |
| 162 | + const state = await getVisibilityState() |
| 163 | + expect(state).to.equal('visible') |
| 164 | + }) |
| 165 | + |
| 166 | + itWithOptions('should be hidden when a second window completely conceals the current window', { |
| 167 | + x: 50, |
| 168 | + y: 50, |
| 169 | + width: 50, |
| 170 | + height: 50, |
| 171 | + }, async function () { |
| 172 | + this.timeout(240000) |
| 173 | + await load() |
| 174 | + await emittedOnce(ipcMain, 'visibility-change', async () => { |
| 175 | + await makeOtherWindow({ |
| 176 | + x: 0, |
| 177 | + y: 0, |
| 178 | + width: 300, |
| 179 | + height: 300, |
| 180 | + }) |
| 181 | + }) |
| 182 | + const state = await getVisibilityState() |
| 183 | + expect(state).to.equal('hidden') |
| 184 | + }) |
| 185 | + }) |
| 186 | +}) |
0 commit comments