forked from electron/electron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisibility-state-spec.ts
186 lines (165 loc) · 5.79 KB
/
visibility-state-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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import { expect } from 'chai'
import * as cp from 'child_process';
import { BrowserWindow, BrowserWindowConstructorOptions, ipcMain } from 'electron'
import * as path from 'path'
import { emittedOnce } from './events-helpers'
import { closeWindow } from './window-helpers';
import { ifdescribe } from './spec-helpers';
// visibilityState specs pass on linux with a real window manager but on CI
// the environment does not let these specs pass
ifdescribe(process.platform !== 'linux' || !isCI)('document.visibilityState', () => {
let w: BrowserWindow
afterEach(() => {
return closeWindow(w)
})
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) {
w = new BrowserWindow({
...options,
paintWhenInitiallyHidden: false,
webPreferences: {
...(options.webPreferences || {}),
nodeIntegration: true
}
})
await Promise.resolve(fn.apply(this, args))
})
}
const getVisibilityState = async (): Promise<string> => {
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 () => {
await load()
const state = await getVisibilityState()
expect(state).to.equal('visible')
})
itWithOptions('should be visible when the window is initially shown', {
show: true,
}, async () => {
await load()
const state = await getVisibilityState()
expect(state).to.equal('visible')
})
itWithOptions('should be hidden when the window is initially hidden', {
show: false,
}, async () => {
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', {
show: false,
}, async () => {
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', {
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
await new Promise(r => setTimeout(r, 0))
}
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 () => {
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 () => {
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 () => {
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', () => {
let child: cp.ChildProcess
before(function() {
if (process.platform !== 'darwin') this.skip()
})
const makeOtherWindow = (opts: { x: number; y: number; width: number; height: number; }) => {
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) => {
if (chunk.toString().includes('__ready__')) resolve()
})
})
}
afterEach(() => {
if (child && !child.killed) {
child.kill('SIGTERM')
}
})
itWithOptions('should be visible when two windows are on screen', {
x: 0,
y: 0,
width: 200,
height: 200,
}, async () => {
await makeOtherWindow({
x: 200,
y: 0,
width: 200,
height: 200,
})
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,
height: 150,
}, async () => {
await makeOtherWindow({
x: 100,
y: 0,
width: 200,
height: 200,
})
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,
height: 50,
}, async function () {
this.timeout(240000)
await load()
await emittedOnce(ipcMain, 'visibility-change', async () => {
await makeOtherWindow({
x: 0,
y: 0,
width: 300,
height: 300,
})
})
const state = await getVisibilityState()
expect(state).to.equal('hidden')
})
})
})